66191f9f3a421f8332faa6015dc1cb337c3d7285
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify" 
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49
50 (defcustom gnus-kill-summary-on-exit t
51   "*If non-nil, kill the summary buffer when you exit from it.
52 If nil, the summary will become a \"*Dead Summary*\" buffer, and
53 it will be killed sometime later."
54   :group 'gnus-summary-exit
55   :type 'boolean)
56
57 (defcustom gnus-fetch-old-headers nil
58   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
59 If an unread article in the group refers to an older, already read (or
60 just marked as read) article, the old article will not normally be
61 displayed in the Summary buffer.  If this variable is non-nil, Gnus
62 will attempt to grab the headers to the old articles, and thereby
63 build complete threads.  If it has the value `some', only enough
64 headers to connect otherwise loose threads will be displayed.  This
65 variable can also be a number.  In that case, no more than that number
66 of old headers will be fetched.  If it has the value `invisible', all
67 old headers will be fetched, but none will be displayed.
68
69 The server has to support NOV for any of this to work."
70   :group 'gnus-thread
71   :type '(choice (const :tag "off" nil)
72                  (const some)
73                  number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-refer-thread-limit 200
77   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
78 If t, fetch all the available old headers."
79   :group 'gnus-thread
80   :type '(choice number
81                  (sexp :menu-tag "other" t)))
82
83 (defcustom gnus-summary-make-false-root 'adopt
84   "*nil means that Gnus won't gather loose threads.
85 If the root of a thread has expired or been read in a previous
86 session, the information necessary to build a complete thread has been
87 lost.  Instead of having many small sub-threads from this original thread
88 scattered all over the summary buffer, Gnus can gather them.
89
90 If non-nil, Gnus will try to gather all loose sub-threads from an
91 original thread into one large thread.
92
93 If this variable is non-nil, it should be one of `none', `adopt',
94 `dummy' or `empty'.
95
96 If this variable is `none', Gnus will not make a false root, but just
97 present the sub-threads after another.
98 If this variable is `dummy', Gnus will create a dummy root that will
99 have all the sub-threads as children.
100 If this variable is `adopt', Gnus will make one of the \"children\"
101 the parent and mark all the step-children as such.
102 If this variable is `empty', the \"children\" are printed with empty
103 subject fields.  (Or rather, they will be printed with a string
104 given by the `gnus-summary-same-subject' variable.)"
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const none)
108                  (const dummy)
109                  (const adopt)
110                  (const empty)))
111
112 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
113   "*A regexp to match subjects to be excluded from loose thread gathering.
114 As loose thread gathering is done on subjects only, that means that
115 there can be many false gatherings performed.  By rooting out certain
116 common subjects, gathering might become saner."
117   :group 'gnus-thread
118   :type 'regexp)
119
120 (defcustom gnus-summary-gather-subject-limit nil
121   "*Maximum length of subject comparisons when gathering loose threads.
122 Use nil to compare full subjects.  Setting this variable to a low
123 number will help gather threads that have been corrupted by
124 newsreaders chopping off subject lines, but it might also mean that
125 unrelated articles that have subject that happen to begin with the
126 same few characters will be incorrectly gathered.
127
128 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
129 comparing subjects."
130   :group 'gnus-thread
131   :type '(choice (const :tag "off" nil)
132                  (const fuzzy)
133                  (sexp :menu-tag "on" t)))
134
135 (defcustom gnus-simplify-subject-functions nil
136   "List of functions taking a string argument that simplify subjects.
137 The functions are applied recursively.
138
139 Useful functions to put in this list include: `gnus-simplify-subject-re',
140 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
141   :group 'gnus-thread
142   :type '(repeat function))
143
144 (defcustom gnus-simplify-ignored-prefixes nil
145   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  regexp))
149
150 (defcustom gnus-build-sparse-threads nil
151   "*If non-nil, fill in the gaps in threads.
152 If `some', only fill in the gaps that are needed to tie loose threads
153 together.  If `more', fill in all leaf nodes that Gnus can find.  If
154 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  (const some)
158                  (const more)
159                  (sexp :menu-tag "all" t)))
160
161 (defcustom gnus-summary-thread-gathering-function
162   'gnus-gather-threads-by-subject
163   "*Function used for gathering loose threads.
164 There are two pre-defined functions: `gnus-gather-threads-by-subject',
165 which only takes Subjects into consideration; and
166 `gnus-gather-threads-by-references', which compared the References
167 headers of the articles to find matches."
168   :group 'gnus-thread
169   :type '(radio (function-item gnus-gather-threads-by-subject)
170                 (function-item gnus-gather-threads-by-references)
171                 (function :tag "other")))
172
173 (defcustom gnus-summary-same-subject ""
174   "*String indicating that the current article has the same subject as the previous.
175 This variable will only be used if the value of
176 `gnus-summary-make-false-root' is `empty'."
177   :group 'gnus-summary-format
178   :type 'string)
179
180 (defcustom gnus-summary-goto-unread t
181   "*If t, many commands will go to the next unread article.
182 This applies to marking commands as well as other commands that
183 \"naturally\" select the next article, like, for instance, `SPC' at
184 the end of an article.
185
186 If nil, the marking commands do NOT go to the next unread article
187 (they go to the next article instead).  If `never', commands that
188 usually go to the next unread article, will go to the next article,
189 whether it is read or not."
190   :group 'gnus-summary-marks
191   :link '(custom-manual "(gnus)Setting Marks")
192   :type '(choice (const :tag "off" nil)
193                  (const never)
194                  (sexp :menu-tag "on" t)))
195
196 (defcustom gnus-summary-default-score 0
197   "*Default article score level.
198 All scores generated by the score files will be added to this score.
199 If this variable is nil, scoring will be disabled."
200   :group 'gnus-score-default
201   :type '(choice (const :tag "disable")
202                  integer))
203
204 (defcustom gnus-summary-default-high-score 0
205   "*Default threshold for a high scored article.
206 An article will be highlighted as high scored if its score is greater
207 than this score."
208   :group 'gnus-score-default
209   :type 'integer)
210
211 (defcustom gnus-summary-default-low-score 0
212   "*Default threshold for a low scored article.
213 An article will be highlighted as low scored if its score is smaller
214 than this score."
215   :group 'gnus-score-default
216   :type 'integer)
217
218 (defcustom gnus-summary-zcore-fuzz 0
219   "*Fuzziness factor for the zcore in the summary buffer.
220 Articles with scores closer than this to `gnus-summary-default-score'
221 will not be marked."
222   :group 'gnus-summary-format
223   :type 'integer)
224
225 (defcustom gnus-simplify-subject-fuzzy-regexp nil
226   "*Strings to be removed when doing fuzzy matches.
227 This can either be a regular expression or list of regular expressions
228 that will be removed from subject strings if fuzzy subject
229 simplification is selected."
230   :group 'gnus-thread
231   :type '(repeat regexp))
232
233 (defcustom gnus-show-threads t
234   "*If non-nil, display threads in summary mode."
235   :group 'gnus-thread
236   :type 'boolean)
237
238 (defcustom gnus-thread-hide-subtree nil
239   "*If non-nil, hide all threads initially.
240 This can be a predicate specifier which says which threads to hide.
241 If threads are hidden, you have to run the command
242 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
243 to expose hidden threads."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-killed t
248   "*If non-nil, hide killed threads automatically."
249   :group 'gnus-thread
250   :type 'boolean)
251
252 (defcustom gnus-thread-ignore-subject t
253   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
254 If nil, articles that have different subjects from their parents will
255 start separate threads."
256   :group 'gnus-thread
257   :type 'boolean)
258
259 (defcustom gnus-thread-operation-ignore-subject t
260   "*If non-nil, subjects will be ignored when doing thread commands.
261 This affects commands like `gnus-summary-kill-thread' and
262 `gnus-summary-lower-thread'.
263
264 If this variable is nil, articles in the same thread with different
265 subjects will not be included in the operation in question.  If this
266 variable is `fuzzy', only articles that have subjects that are fuzzily
267 equal will be included."
268   :group 'gnus-thread
269   :type '(choice (const :tag "off" nil)
270                  (const fuzzy)
271                  (sexp :tag "on" t)))
272
273 (defcustom gnus-thread-indent-level 4
274   "*Number that says how much each sub-thread should be indented."
275   :group 'gnus-thread
276   :type 'integer)
277
278 (defcustom gnus-auto-extend-newsgroup t
279   "*If non-nil, extend newsgroup forward and backward when requested."
280   :group 'gnus-summary-choose
281   :type 'boolean)
282
283 (defcustom gnus-auto-select-first t
284   "*If non-nil, select the article under point.
285 Which article this is is controlled by the `gnus-auto-select-subject'
286 variable.
287
288 If you want to prevent automatic selection of articles in some
289 newsgroups, set the variable to nil in `gnus-select-group-hook'."
290   :group 'gnus-group-select
291   :type '(choice (const :tag "none" nil)
292                  (sexp :menu-tag "first" t)))
293
294 (defcustom gnus-auto-select-subject 'unread
295   "*Says what subject to place under point when entering a group.
296
297 This variable can either be the symbols `first' (place point on the
298 first subject), `unread' (place point on the subject line of the first
299 unread article), `best' (place point on the subject line of the
300 higest-scored article), `unseen' (place point on the subject line of
301 the first unseen article), 'unseen-or-unread' (place point on the subject
302 line of the first unseen article or, if all article have been seen, on the
303 subject line of the first unread article), or a function to be called to
304 place point on some subject line."
305   :group 'gnus-group-select
306   :type '(choice (const best)
307                  (const unread)
308                  (const first)
309                  (const unseen)
310                  (const unseen-or-unread)))
311
312 (defcustom gnus-auto-select-next t
313   "*If non-nil, offer to go to the next group from the end of the previous.
314 If the value is t and the next newsgroup is empty, Gnus will exit
315 summary mode and go back to group mode.  If the value is neither nil
316 nor t, Gnus will select the following unread newsgroup.  In
317 particular, if the value is the symbol `quietly', the next unread
318 newsgroup will be selected without any confirmation, and if it is
319 `almost-quietly', the next group will be selected without any
320 confirmation if you are located on the last article in the group.
321 Finally, if this variable is `slightly-quietly', the `Z n' command
322 will go to the next group without confirmation."
323   :group 'gnus-summary-maneuvering
324   :type '(choice (const :tag "off" nil)
325                  (const quietly)
326                  (const almost-quietly)
327                  (const slightly-quietly)
328                  (sexp :menu-tag "on" t)))
329
330 (defcustom gnus-auto-select-same nil
331   "*If non-nil, select the next article with the same subject.
332 If there are no more articles with the same subject, go to
333 the first unread article."
334   :group 'gnus-summary-maneuvering
335   :type 'boolean)
336
337 (defcustom gnus-summary-check-current nil
338   "*If non-nil, consider the current article when moving.
339 The \"unread\" movement commands will stay on the same line if the
340 current article is unread."
341   :group 'gnus-summary-maneuvering
342   :type 'boolean)
343
344 (defcustom gnus-auto-center-summary t
345   "*If non-nil, always center the current summary buffer.
346 In particular, if `vertical' do only vertical recentering.  If non-nil
347 and non-`vertical', do both horizontal and vertical recentering."
348   :group 'gnus-summary-maneuvering
349   :type '(choice (const :tag "none" nil)
350                  (const vertical)
351                  (integer :tag "height")
352                  (sexp :menu-tag "both" t)))
353
354 (defcustom gnus-show-all-headers nil
355   "*If non-nil, don't hide any headers."
356   :group 'gnus-article-hiding
357   :group 'gnus-article-headers
358   :type 'boolean)
359
360 (defcustom gnus-summary-ignore-duplicates nil
361   "*If non-nil, ignore articles with identical Message-ID headers."
362   :group 'gnus-summary
363   :type 'boolean)
364
365 (defcustom gnus-single-article-buffer t
366   "*If non-nil, display all articles in the same buffer.
367 If nil, each group will get its own article buffer."
368   :group 'gnus-article-various
369   :type 'boolean)
370
371 (defcustom gnus-break-pages t
372   "*If non-nil, do page breaking on articles.
373 The page delimiter is specified by the `gnus-page-delimiter'
374 variable."
375   :group 'gnus-article-various
376   :type 'boolean)
377
378 (defcustom gnus-move-split-methods nil
379   "*Variable used to suggest where articles are to be moved to.
380 It uses the same syntax as the `gnus-split-methods' variable.
381 However, whereas `gnus-split-methods' specifies file names as targets,
382 this variable specifies group names."
383   :group 'gnus-summary-mail
384   :type '(repeat (choice (list :value (fun) function)
385                          (cons :value ("" "") regexp (repeat string))
386                          (sexp :value nil))))
387
388 (defcustom gnus-unread-mark ?           ;Whitespace
389   "*Mark used for unread articles."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-ticked-mark ?!
394   "*Mark used for ticked articles."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-dormant-mark ??
399   "*Mark used for dormant articles."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-del-mark ?r
404   "*Mark used for del'd articles."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-read-mark ?R
409   "*Mark used for read articles."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-expirable-mark ?E
414   "*Mark used for expirable articles."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-killed-mark ?K
419   "*Mark used for killed articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-souped-mark ?F
424   "*Mark used for souped articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-kill-file-mark ?X
429   "*Mark used for articles killed by kill files."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-low-score-mark ?Y
434   "*Mark used for articles with a low score."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-catchup-mark ?C
439   "*Mark used for articles that are caught up."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-replied-mark ?A
444   "*Mark used for articles that have been replied to."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-forwarded-mark ?F
449   "*Mark used for articles that have been forwarded."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-recent-mark ?N
454   "*Mark used for articles that are recent."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-cached-mark ?*
459   "*Mark used for articles that are in the cache."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-saved-mark ?S
464   "*Mark used for articles that have been saved."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-unseen-mark ?.
469   "*Mark used for articles that haven't been seen."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-no-mark ?               ;Whitespace
474   "*Mark used for articles that have no other secondary mark."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-ancient-mark ?O
479   "*Mark used for ancient articles."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-sparse-mark ?Q
484   "*Mark used for sparsely reffed articles."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-canceled-mark ?G
489   "*Mark used for canceled articles."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-duplicate-mark ?M
494   "*Mark used for duplicate articles."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-undownloaded-mark ?@
499   "*Mark used for articles that weren't downloaded."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-downloadable-mark ?%
504   "*Mark used for articles that are to be downloaded."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-unsendable-mark ?=
509   "*Mark used for articles that won't be sent."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-score-over-mark ?+
514   "*Score mark used for articles with high scores."
515   :group 'gnus-summary-marks
516   :type 'character)
517
518 (defcustom gnus-score-below-mark ?-
519   "*Score mark used for articles with low scores."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-empty-thread-mark ?     ;Whitespace
524   "*There is no thread under the article."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-not-empty-thread-mark ?=
529   "*There is a thread under the article."
530   :group 'gnus-summary-marks
531   :type 'character)
532
533 (defcustom gnus-view-pseudo-asynchronously nil
534   "*If non-nil, Gnus will view pseudo-articles asynchronously."
535   :group 'gnus-extract-view
536   :type 'boolean)
537
538 (defcustom gnus-auto-expirable-marks
539   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
540         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
541         gnus-souped-mark gnus-duplicate-mark)
542   "*The list of marks converted into expiration if a group is auto-expirable."
543   :version "21.1"
544   :group 'gnus-summary
545   :type '(repeat character))
546
547 (defcustom gnus-inhibit-user-auto-expire t
548   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
549   :version "21.1"
550   :group 'gnus-summary
551   :type 'boolean)
552
553 (defcustom gnus-view-pseudos nil
554   "*If `automatic', pseudo-articles will be viewed automatically.
555 If `not-confirm', pseudos will be viewed automatically, and the user
556 will not be asked to confirm the command."
557   :group 'gnus-extract-view
558   :type '(choice (const :tag "off" nil)
559                  (const automatic)
560                  (const not-confirm)))
561
562 (defcustom gnus-view-pseudos-separately t
563   "*If non-nil, one pseudo-article will be created for each file to be viewed.
564 If nil, all files that use the same viewing command will be given as a
565 list of parameters to that command."
566   :group 'gnus-extract-view
567   :type 'boolean)
568
569 (defcustom gnus-insert-pseudo-articles t
570   "*If non-nil, insert pseudo-articles when decoding articles."
571   :group 'gnus-extract-view
572   :type 'boolean)
573
574 (defcustom gnus-summary-dummy-line-format
575   "  %(:                          :%) %S\n"
576   "*The format specification for the dummy roots in the summary buffer.
577 It works along the same lines as a normal formatting string,
578 with some simple extensions.
579
580 %S  The subject
581
582 General format specifiers can also be used.
583 See (gnus)Formatting Variables."
584   :link '(custom-manual "(gnus)Formatting Variables")
585   :group 'gnus-threading
586   :type 'string)
587
588 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
589   "*The format specification for the summary mode line.
590 It works along the same lines as a normal formatting string,
591 with some simple extensions:
592
593 %G  Group name
594 %p  Unprefixed group name
595 %A  Current article number
596 %z  Current article score
597 %V  Gnus version
598 %U  Number of unread articles in the group
599 %e  Number of unselected articles in the group
600 %Z  A string with unread/unselected article counts
601 %g  Shortish group name
602 %S  Subject of the current article
603 %u  User-defined spec
604 %s  Current score file name
605 %d  Number of dormant articles
606 %r  Number of articles that have been marked as read in this session
607 %E  Number of articles expunged by the score files"
608   :group 'gnus-summary-format
609   :type 'string)
610
611 (defcustom gnus-list-identifiers nil
612   "Regexp that matches list identifiers to be removed from subject.
613 This can also be a list of regexps."
614   :version "21.1"
615   :group 'gnus-summary-format
616   :group 'gnus-article-hiding
617   :type '(choice (const :tag "none" nil)
618                  (regexp :value ".*")
619                  (repeat :value (".*") regexp)))
620
621 (defcustom gnus-summary-mark-below 0
622   "*Mark all articles with a score below this variable as read.
623 This variable is local to each summary buffer and usually set by the
624 score file."
625   :group 'gnus-score-default
626   :type 'integer)
627
628 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
629   "*List of functions used for sorting articles in the summary buffer.
630
631 Each function takes two articles and returns non-nil if the first
632 article should be sorted before the other.  If you use more than one
633 function, the primary sort function should be the last.  You should
634 probably always include `gnus-article-sort-by-number' in the list of
635 sorting functions -- preferably first.  Also note that sorting by date
636 is often much slower than sorting by number, and the sorting order is
637 very similar.  (Sorting by date means sorting by the time the message
638 was sent, sorting by number means sorting by arrival time.)
639
640 Ready-made functions include `gnus-article-sort-by-number',
641 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
642 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
643
644 When threading is turned on, the variable `gnus-thread-sort-functions'
645 controls how articles are sorted."
646   :group 'gnus-summary-sort
647   :type '(repeat (choice (function-item gnus-article-sort-by-number)
648                          (function-item gnus-article-sort-by-author)
649                          (function-item gnus-article-sort-by-subject)
650                          (function-item gnus-article-sort-by-date)
651                          (function-item gnus-article-sort-by-score)
652                          (function :tag "other"))))
653
654 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
655   "*List of functions used for sorting threads in the summary buffer.
656 By default, threads are sorted by article number.
657
658 Each function takes two threads and returns non-nil if the first
659 thread should be sorted before the other.  If you use more than one
660 function, the primary sort function should be the last.  You should
661 probably always include `gnus-thread-sort-by-number' in the list of
662 sorting functions -- preferably first.  Also note that sorting by date
663 is often much slower than sorting by number, and the sorting order is
664 very similar.  (Sorting by date means sorting by the time the message
665 was sent, sorting by number means sorting by arrival time.)
666
667 Ready-made functions include `gnus-thread-sort-by-number',
668 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
669 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
670 `gnus-thread-sort-by-most-recent-number',
671 `gnus-thread-sort-by-most-recent-date', and
672 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
673
674 When threading is turned off, the variable
675 `gnus-article-sort-functions' controls how articles are sorted."
676   :group 'gnus-summary-sort
677   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
678                          (function-item gnus-thread-sort-by-author)
679                          (function-item gnus-thread-sort-by-subject)
680                          (function-item gnus-thread-sort-by-date)
681                          (function-item gnus-thread-sort-by-score)
682                          (function-item gnus-thread-sort-by-total-score)
683                          (function :tag "other"))))
684
685 (defcustom gnus-thread-score-function '+
686   "*Function used for calculating the total score of a thread.
687
688 The function is called with the scores of the article and each
689 subthread and should then return the score of the thread.
690
691 Some functions you can use are `+', `max', or `min'."
692   :group 'gnus-summary-sort
693   :type 'function)
694
695 (defcustom gnus-summary-expunge-below nil
696   "All articles that have a score less than this variable will be expunged.
697 This variable is local to the summary buffers."
698   :group 'gnus-score-default
699   :type '(choice (const :tag "off" nil)
700                  integer))
701
702 (defcustom gnus-thread-expunge-below nil
703   "All threads that have a total score less than this variable will be expunged.
704 See `gnus-thread-score-function' for en explanation of what a
705 \"thread score\" is.
706
707 This variable is local to the summary buffers."
708   :group 'gnus-threading
709   :group 'gnus-score-default
710   :type '(choice (const :tag "off" nil)
711                  integer))
712
713 (defcustom gnus-summary-mode-hook nil
714   "*A hook for Gnus summary mode.
715 This hook is run before any variables are set in the summary buffer."
716   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
717   :group 'gnus-summary-various
718   :type 'hook)
719
720 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
721 (when (featurep 'xemacs)
722   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
723   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
724   (add-hook 'gnus-summary-mode-hook
725             'gnus-xmas-switch-horizontal-scrollbar-off))
726
727 (defcustom gnus-summary-menu-hook nil
728   "*Hook run after the creation of the summary mode menu."
729   :group 'gnus-summary-visual
730   :type 'hook)
731
732 (defcustom gnus-summary-exit-hook nil
733   "*A hook called on exit from the summary buffer.
734 It will be called with point in the group buffer."
735   :group 'gnus-summary-exit
736   :type 'hook)
737
738 (defcustom gnus-summary-prepare-hook nil
739   "*A hook called after the summary buffer has been generated.
740 If you want to modify the summary buffer, you can use this hook."
741   :group 'gnus-summary-various
742   :type 'hook)
743
744 (defcustom gnus-summary-prepared-hook nil
745   "*A hook called as the last thing after the summary buffer has been generated."
746   :group 'gnus-summary-various
747   :type 'hook)
748
749 (defcustom gnus-summary-generate-hook nil
750   "*A hook run just before generating the summary buffer.
751 This hook is commonly used to customize threading variables and the
752 like."
753   :group 'gnus-summary-various
754   :type 'hook)
755
756 (defcustom gnus-select-group-hook nil
757   "*A hook called when a newsgroup is selected.
758
759 If you'd like to simplify subjects like the
760 `gnus-summary-next-same-subject' command does, you can use the
761 following hook:
762
763  (add-hook gnus-select-group-hook
764            (lambda ()
765              (mapcar (lambda (header)
766                        (mail-header-set-subject
767                         header
768                         (gnus-simplify-subject
769                          (mail-header-subject header) 're-only)))
770                      gnus-newsgroup-headers)))"
771   :group 'gnus-group-select
772   :type 'hook)
773
774 (defcustom gnus-select-article-hook nil
775   "*A hook called when an article is selected."
776   :group 'gnus-summary-choose
777   :type 'hook)
778
779 (defcustom gnus-visual-mark-article-hook
780   (list 'gnus-highlight-selected-summary)
781   "*Hook run after selecting an article in the summary buffer.
782 It is meant to be used for highlighting the article in some way.  It
783 is not run if `gnus-visual' is nil."
784   :group 'gnus-summary-visual
785   :type 'hook)
786
787 (defcustom gnus-parse-headers-hook nil
788   "*A hook called before parsing the headers."
789   :group 'gnus-various
790   :type 'hook)
791
792 (defcustom gnus-exit-group-hook nil
793   "*A hook called when exiting summary mode.
794 This hook is not called from the non-updating exit commands like `Q'."
795   :group 'gnus-various
796   :type 'hook)
797
798 (defcustom gnus-summary-update-hook
799   (list 'gnus-summary-highlight-line)
800   "*A hook called when a summary line is changed.
801 The hook will not be called if `gnus-visual' is nil.
802
803 The default function `gnus-summary-highlight-line' will
804 highlight the line according to the `gnus-summary-highlight'
805 variable."
806   :group 'gnus-summary-visual
807   :type 'hook)
808
809 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
810   "*A hook called when an article is selected for the first time.
811 The hook is intended to mark an article as read (or unread)
812 automatically when it is selected."
813   :group 'gnus-summary-choose
814   :type 'hook)
815
816 (defcustom gnus-group-no-more-groups-hook nil
817   "*A hook run when returning to group mode having no more (unread) groups."
818   :group 'gnus-group-select
819   :type 'hook)
820
821 (defcustom gnus-ps-print-hook nil
822   "*A hook run before ps-printing something from Gnus."
823   :group 'gnus-summary
824   :type 'hook)
825
826 (defcustom gnus-summary-display-arrow
827   (and (fboundp 'display-graphic-p)
828        (display-graphic-p))
829   "*If non-nil, display an arrow highlighting the current article."
830   :version "21.1"
831   :group 'gnus-summary
832   :type 'boolean)
833
834 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
835   "Face used for highlighting the current article in the summary buffer."
836   :group 'gnus-summary-visual
837   :type 'face)
838
839 (defcustom gnus-summary-highlight
840   '(((= mark gnus-canceled-mark)
841      . gnus-summary-cancelled-face)
842     ((and (> score default-high)
843           (or (= mark gnus-dormant-mark)
844               (= mark gnus-ticked-mark)))
845      . gnus-summary-high-ticked-face)
846     ((and (< score default-low)
847           (or (= mark gnus-dormant-mark)
848               (= mark gnus-ticked-mark)))
849      . gnus-summary-low-ticked-face)
850     ((or (= mark gnus-dormant-mark)
851          (= mark gnus-ticked-mark))
852      . gnus-summary-normal-ticked-face)
853     ((and (> score default-high) (= mark gnus-ancient-mark))
854      . gnus-summary-high-ancient-face)
855     ((and (< score default-low) (= mark gnus-ancient-mark))
856      . gnus-summary-low-ancient-face)
857     ((= mark gnus-ancient-mark)
858      . gnus-summary-normal-ancient-face)
859     ((and (> score default-high) (= mark gnus-unread-mark))
860      . gnus-summary-high-unread-face)
861     ((and (< score default-low) (= mark gnus-unread-mark))
862      . gnus-summary-low-unread-face)
863     ((= mark gnus-unread-mark)
864      . gnus-summary-normal-unread-face)
865     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
866                                                   gnus-undownloaded-mark)))
867      . gnus-summary-high-unread-face)
868     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
869                                                  gnus-undownloaded-mark)))
870      . gnus-summary-low-unread-face)
871     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
872           (memq article gnus-newsgroup-unreads))
873      . gnus-summary-normal-unread-face)
874     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
875      . gnus-summary-normal-read-face)
876     ((> score default-high)
877      . gnus-summary-high-read-face)
878     ((< score default-low)
879      . gnus-summary-low-read-face)
880     (t
881      . gnus-summary-normal-read-face))
882   "*Controls the highlighting of summary buffer lines.
883
884 A list of (FORM . FACE) pairs.  When deciding how a a particular
885 summary line should be displayed, each form is evaluated.  The content
886 of the face field after the first true form is used.  You can change
887 how those summary lines are displayed, by editing the face field.
888
889 You can use the following variables in the FORM field.
890
891 score:        The article's score
892 default:      The default article score.
893 default-high: The default score for high scored articles.
894 default-low:  The default score for low scored articles.
895 below:        The score below which articles are automatically marked as read.
896 mark:         The articles mark."
897   :group 'gnus-summary-visual
898   :type '(repeat (cons (sexp :tag "Form" nil)
899                        face)))
900
901 (defcustom gnus-alter-header-function nil
902   "Function called to allow alteration of article header structures.
903 The function is called with one parameter, the article header vector,
904 which it may alter in any way.")
905
906 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
907   "Variable that says which function should be used to decode a string with encoded words.")
908
909 (defcustom gnus-extra-headers '(To Newsgroups)
910   "*Extra headers to parse."
911   :version "21.1"
912   :group 'gnus-summary
913   :type '(repeat symbol))
914
915 (defcustom gnus-ignored-from-addresses
916   (and user-mail-address (regexp-quote user-mail-address))
917   "*Regexp of From headers that may be suppressed in favor of To headers."
918   :version "21.1"
919   :group 'gnus-summary
920   :type 'regexp)
921
922 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
923   "List of charsets that should be ignored.
924 When these charsets are used in the \"charset\" parameter, the
925 default charset will be used instead."
926   :version "21.1"
927   :type '(repeat symbol)
928   :group 'gnus-charset)
929
930 (gnus-define-group-parameter
931  ignored-charsets
932  :type list
933  :function-document
934  "Return the ignored charsets of GROUP."
935  :variable gnus-group-ignored-charsets-alist
936  :variable-default
937  '(("alt\\.chinese\\.text" iso-8859-1))
938  :variable-document
939  "Alist of regexps (to match group names) and charsets that should be ignored.
940 When these charsets are used in the \"charset\" parameter, the
941 default charset will be used instead."
942  :variable-group gnus-charset
943  :variable-type '(repeat (cons (regexp :tag "Group")
944                                (repeat symbol)))
945  :parameter-type '(choice :tag "Ignored charsets"
946                           :value nil
947                           (repeat (symbol)))
948  :parameter-document       "\
949 List of charsets that should be ignored.
950
951 When these charsets are used in the \"charset\" parameter, the
952 default charset will be used instead.")
953
954 (defcustom gnus-group-highlight-words-alist nil
955   "Alist of group regexps and highlight regexps.
956 This variable uses the same syntax as `gnus-emphasis-alist'."
957   :version "21.1"
958   :type '(repeat (cons (regexp :tag "Group")
959                        (repeat (list (regexp :tag "Highlight regexp")
960                                      (number :tag "Group for entire word" 0)
961                                      (number :tag "Group for displayed part" 0)
962                                      (symbol :tag "Face"
963                                              gnus-emphasis-highlight-words)))))
964   :group 'gnus-summary-visual)
965
966 (defcustom gnus-summary-show-article-charset-alist
967   nil
968   "Alist of number and charset.
969 The article will be shown with the charset corresponding to the
970 numbered argument.
971 For example: ((1 . cn-gb-2312) (2 . big5))."
972   :version "21.1"
973   :type '(repeat (cons (number :tag "Argument" 1)
974                        (symbol :tag "Charset")))
975   :group 'gnus-charset)
976
977 (defcustom gnus-preserve-marks t
978   "Whether marks are preserved when moving, copying and respooling messages."
979   :version "21.1"
980   :type 'boolean
981   :group 'gnus-summary-marks)
982
983 (defcustom gnus-alter-articles-to-read-function nil
984   "Function to be called to alter the list of articles to be selected."
985   :type '(choice (const nil) function)
986   :group 'gnus-summary)
987
988 (defcustom gnus-orphan-score nil
989   "*All orphans get this score added.  Set in the score file."
990   :group 'gnus-score-default
991   :type '(choice (const nil)
992                  integer))
993
994 (defcustom gnus-summary-save-parts-default-mime "image/.*"
995   "*A regexp to match MIME parts when saving multiple parts of a message
996 with gnus-summary-save-parts (X m). This regexp will be used by default
997 when prompting the user for which type of files to save."
998   :group 'gnus-summary
999   :type 'regexp)
1000
1001 (defcustom gnus-read-all-available-headers nil
1002   "Whether Gnus should parse all headers made available to it.
1003 This is mostly relevant for slow backends where the user may
1004 wish to widen the summary buffer to include all headers
1005 that were fetched.  Say, for nnultimate groups."
1006   :group 'gnus-summary
1007   :type '(choice boolean regexp))
1008
1009 (defcustom gnus-summary-muttprint-program "muttprint"
1010   "Command (and optional arguments) used to run Muttprint."
1011   :version "21.3"
1012   :group 'gnus-summary
1013   :type 'string)
1014
1015 (defcustom gnus-article-loose-mime nil
1016   "If non-nil, don't require MIME-Version header.
1017 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1018 supply the MIME-Version header or deliberately strip it From the mail.
1019 Set it to non-nil, Gnus will treat some articles as MIME even if
1020 the MIME-Version header is missed."
1021   :version "21.3"
1022   :type 'boolean
1023   :group 'gnus-article)
1024
1025 ;;; Internal variables
1026
1027 (defvar gnus-summary-display-cache nil)
1028 (defvar gnus-article-mime-handles nil)
1029 (defvar gnus-article-decoded-p nil)
1030 (defvar gnus-article-charset nil)
1031 (defvar gnus-article-ignored-charsets nil)
1032 (defvar gnus-scores-exclude-files nil)
1033 (defvar gnus-page-broken nil)
1034 (defvar gnus-inhibit-mime-unbuttonizing nil)
1035
1036 (defvar gnus-original-article nil)
1037 (defvar gnus-article-internal-prepare-hook nil)
1038 (defvar gnus-newsgroup-process-stack nil)
1039
1040 (defvar gnus-thread-indent-array nil)
1041 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1042 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1043   "Function called to sort the articles within a thread after it has been gathered together.")
1044
1045 (defvar gnus-summary-save-parts-type-history nil)
1046 (defvar gnus-summary-save-parts-last-directory nil)
1047
1048 ;; Avoid highlighting in kill files.
1049 (defvar gnus-summary-inhibit-highlight nil)
1050 (defvar gnus-newsgroup-selected-overlay nil)
1051 (defvar gnus-inhibit-limiting nil)
1052 (defvar gnus-newsgroup-adaptive-score-file nil)
1053 (defvar gnus-current-score-file nil)
1054 (defvar gnus-current-move-group nil)
1055 (defvar gnus-current-copy-group nil)
1056 (defvar gnus-current-crosspost-group nil)
1057 (defvar gnus-newsgroup-display nil)
1058
1059 (defvar gnus-newsgroup-dependencies nil)
1060 (defvar gnus-newsgroup-adaptive nil)
1061 (defvar gnus-summary-display-article-function nil)
1062 (defvar gnus-summary-highlight-line-function nil
1063   "Function called after highlighting a summary line.")
1064
1065 (defvar gnus-summary-line-format-alist
1066   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1067     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1068     (?s gnus-tmp-subject-or-nil ?s)
1069     (?n gnus-tmp-name ?s)
1070     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1071         ?s)
1072     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1073             gnus-tmp-from) ?s)
1074     (?F gnus-tmp-from ?s)
1075     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1076     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1077     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1078     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1079     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1080     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1081     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1082     (?L gnus-tmp-lines ?s)
1083     (?I gnus-tmp-indentation ?s)
1084     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1085     (?R gnus-tmp-replied ?c)
1086     (?\[ gnus-tmp-opening-bracket ?c)
1087     (?\] gnus-tmp-closing-bracket ?c)
1088     (?\> (make-string gnus-tmp-level ? ) ?s)
1089     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1090     (?i gnus-tmp-score ?d)
1091     (?z gnus-tmp-score-char ?c)
1092     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1093     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1094     (?U gnus-tmp-unread ?c)
1095     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1096         ?s)
1097     (?t (gnus-summary-number-of-articles-in-thread
1098          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1099         ?d)
1100     (?e (gnus-summary-number-of-articles-in-thread
1101          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1102         ?c)
1103     (?u gnus-tmp-user-defined ?s)
1104     (?P (gnus-pick-line-number) ?d)
1105     (?B gnus-tmp-thread-tree-header-string ?s)
1106     (user-date (gnus-user-date
1107                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1108   "An alist of format specifications that can appear in summary lines.
1109 These are paired with what variables they correspond with, along with
1110 the type of the variable (string, integer, character, etc).")
1111
1112 (defvar gnus-summary-dummy-line-format-alist
1113   `((?S gnus-tmp-subject ?s)
1114     (?N gnus-tmp-number ?d)
1115     (?u gnus-tmp-user-defined ?s)))
1116
1117 (defvar gnus-summary-mode-line-format-alist
1118   `((?G gnus-tmp-group-name ?s)
1119     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1120     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1121     (?A gnus-tmp-article-number ?d)
1122     (?Z gnus-tmp-unread-and-unselected ?s)
1123     (?V gnus-version ?s)
1124     (?U gnus-tmp-unread-and-unticked ?d)
1125     (?S gnus-tmp-subject ?s)
1126     (?e gnus-tmp-unselected ?d)
1127     (?u gnus-tmp-user-defined ?s)
1128     (?d (length gnus-newsgroup-dormant) ?d)
1129     (?t (length gnus-newsgroup-marked) ?d)
1130     (?r (length gnus-newsgroup-reads) ?d)
1131     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1132     (?E gnus-newsgroup-expunged-tally ?d)
1133     (?s (gnus-current-score-file-nondirectory) ?s)))
1134
1135 (defvar gnus-last-search-regexp nil
1136   "Default regexp for article search command.")
1137
1138 (defvar gnus-last-shell-command nil
1139   "Default shell command on article.")
1140
1141 (defvar gnus-newsgroup-begin nil)
1142 (defvar gnus-newsgroup-end nil)
1143 (defvar gnus-newsgroup-last-rmail nil)
1144 (defvar gnus-newsgroup-last-mail nil)
1145 (defvar gnus-newsgroup-last-folder nil)
1146 (defvar gnus-newsgroup-last-file nil)
1147 (defvar gnus-newsgroup-auto-expire nil)
1148 (defvar gnus-newsgroup-active nil)
1149
1150 (defvar gnus-newsgroup-data nil)
1151 (defvar gnus-newsgroup-data-reverse nil)
1152 (defvar gnus-newsgroup-limit nil)
1153 (defvar gnus-newsgroup-limits nil)
1154
1155 (defvar gnus-newsgroup-unreads nil
1156   "Sorted list of unread articles in the current newsgroup.")
1157
1158 (defvar gnus-newsgroup-unselected nil
1159   "Sorted list of unselected unread articles in the current newsgroup.")
1160
1161 (defvar gnus-newsgroup-reads nil
1162   "Alist of read articles and article marks in the current newsgroup.")
1163
1164 (defvar gnus-newsgroup-expunged-tally nil)
1165
1166 (defvar gnus-newsgroup-marked nil
1167   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1168
1169 (defvar gnus-newsgroup-killed nil
1170   "List of ranges of articles that have been through the scoring process.")
1171
1172 (defvar gnus-newsgroup-cached nil
1173   "Sorted list of articles that come from the article cache.")
1174
1175 (defvar gnus-newsgroup-saved nil
1176   "List of articles that have been saved.")
1177
1178 (defvar gnus-newsgroup-kill-headers nil)
1179
1180 (defvar gnus-newsgroup-replied nil
1181   "List of articles that have been replied to in the current newsgroup.")
1182
1183 (defvar gnus-newsgroup-forwarded nil
1184   "List of articles that have been forwarded in the current newsgroup.")
1185
1186 (defvar gnus-newsgroup-recent nil
1187   "List of articles that have are recent in the current newsgroup.")
1188
1189 (defvar gnus-newsgroup-expirable nil
1190   "Sorted list of articles in the current newsgroup that can be expired.")
1191
1192 (defvar gnus-newsgroup-processable nil
1193   "List of articles in the current newsgroup that can be processed.")
1194
1195 (defvar gnus-newsgroup-downloadable nil
1196   "Sorted list of articles in the current newsgroup that can be processed.")
1197
1198 (defvar gnus-newsgroup-undownloaded nil
1199   "List of articles in the current newsgroup that haven't been downloaded..")
1200
1201 (defvar gnus-newsgroup-unsendable nil
1202   "List of articles in the current newsgroup that won't be sent.")
1203
1204 (defvar gnus-newsgroup-bookmarks nil
1205   "List of articles in the current newsgroup that have bookmarks.")
1206
1207 (defvar gnus-newsgroup-dormant nil
1208   "Sorted list of dormant articles in the current newsgroup.")
1209
1210 (defvar gnus-newsgroup-unseen nil
1211   "List of unseen articles in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-seen nil
1214   "Range of seen articles in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-articles nil
1217   "List of articles in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-scored nil
1220   "List of scored articles in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-headers nil
1223   "List of article headers in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-threads nil)
1226
1227 (defvar gnus-newsgroup-prepared nil
1228   "Whether the current group has been prepared properly.")
1229
1230 (defvar gnus-newsgroup-ancient nil
1231   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-sparse nil)
1234
1235 (defvar gnus-current-article nil)
1236 (defvar gnus-article-current nil)
1237 (defvar gnus-current-headers nil)
1238 (defvar gnus-have-all-headers nil)
1239 (defvar gnus-last-article nil)
1240 (defvar gnus-newsgroup-history nil)
1241 (defvar gnus-newsgroup-charset nil)
1242 (defvar gnus-newsgroup-ephemeral-charset nil)
1243 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1244
1245 (defvar gnus-article-before-search nil)
1246
1247 (defconst gnus-summary-local-variables
1248   '(gnus-newsgroup-name
1249     gnus-newsgroup-begin gnus-newsgroup-end
1250     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1251     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1252     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1253     gnus-newsgroup-unselected gnus-newsgroup-marked
1254     gnus-newsgroup-reads gnus-newsgroup-saved
1255     gnus-newsgroup-replied gnus-newsgroup-forwarded
1256     gnus-newsgroup-recent
1257     gnus-newsgroup-expirable
1258     gnus-newsgroup-processable gnus-newsgroup-killed
1259     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1260     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1261     gnus-newsgroup-seen gnus-newsgroup-articles
1262     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1263     gnus-newsgroup-headers gnus-newsgroup-threads
1264     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1265     gnus-current-article gnus-current-headers gnus-have-all-headers
1266     gnus-last-article gnus-article-internal-prepare-hook
1267     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1268     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1269     gnus-thread-expunge-below
1270     gnus-score-alist gnus-current-score-file
1271     (gnus-summary-expunge-below . global)
1272     (gnus-summary-mark-below . global)
1273     (gnus-orphan-score . global)
1274     gnus-newsgroup-active gnus-scores-exclude-files
1275     gnus-newsgroup-history gnus-newsgroup-ancient
1276     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1277     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1278     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1279     (gnus-newsgroup-expunged-tally . 0)
1280     gnus-cache-removable-articles gnus-newsgroup-cached
1281     gnus-newsgroup-data gnus-newsgroup-data-reverse
1282     gnus-newsgroup-limit gnus-newsgroup-limits
1283     gnus-newsgroup-charset gnus-newsgroup-display)
1284   "Variables that are buffer-local to the summary buffers.")
1285
1286 (defvar gnus-newsgroup-variables nil
1287   "A list of variables that have separate values in different newsgroups.
1288 A list of newsgroup (summary buffer) local variables, or cons of
1289 variables and their default values (when the default values are not
1290 nil), that should be made global while the summary buffer is active.
1291 These variables can be used to set variables in the group parameters
1292 while still allowing them to affect operations done in other
1293 buffers. For example:
1294
1295 \(setq gnus-newsgroup-variables
1296      '(message-use-followup-to
1297        (gnus-visible-headers .
1298          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1299 ")
1300
1301 ;; Byte-compiler warning.
1302 (eval-when-compile (defvar gnus-article-mode-map))
1303
1304 ;; MIME stuff.
1305
1306 (defvar gnus-decode-encoded-word-methods
1307   '(mail-decode-encoded-word-string)
1308   "List of methods used to decode encoded words.
1309
1310 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1311 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1312 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1313 whose names match REGEXP.
1314
1315 For example:
1316 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1317  mail-decode-encoded-word-string
1318  (\"chinese\" . rfc1843-decode-string))")
1319
1320 (defvar gnus-decode-encoded-word-methods-cache nil)
1321
1322 (defun gnus-multi-decode-encoded-word-string (string)
1323   "Apply the functions from `gnus-encoded-word-methods' that match."
1324   (unless (and gnus-decode-encoded-word-methods-cache
1325                (eq gnus-newsgroup-name
1326                    (car gnus-decode-encoded-word-methods-cache)))
1327     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1328     (mapcar (lambda (x)
1329               (if (symbolp x)
1330                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1331                 (if (and gnus-newsgroup-name
1332                          (string-match (car x) gnus-newsgroup-name))
1333                     (nconc gnus-decode-encoded-word-methods-cache
1334                            (list (cdr x))))))
1335             gnus-decode-encoded-word-methods))
1336   (let ((xlist gnus-decode-encoded-word-methods-cache))
1337     (pop xlist)
1338     (while xlist
1339       (setq string (funcall (pop xlist) string))))
1340   string)
1341
1342 ;; Subject simplification.
1343
1344 (defun gnus-simplify-whitespace (str)
1345   "Remove excessive whitespace from STR."
1346   (let ((mystr str))
1347     ;; Multiple spaces.
1348     (while (string-match "[ \t][ \t]+" mystr)
1349       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1350                           " "
1351                           (substring mystr (match-end 0)))))
1352     ;; Leading spaces.
1353     (when (string-match "^[ \t]+" mystr)
1354       (setq mystr (substring mystr (match-end 0))))
1355     ;; Trailing spaces.
1356     (when (string-match "[ \t]+$" mystr)
1357       (setq mystr (substring mystr 0 (match-beginning 0))))
1358     mystr))
1359
1360 (defsubst gnus-simplify-subject-re (subject)
1361   "Remove \"Re:\" from subject lines."
1362   (if (string-match message-subject-re-regexp subject)
1363       (substring subject (match-end 0))
1364     subject))
1365
1366 (defun gnus-simplify-subject (subject &optional re-only)
1367   "Remove `Re:' and words in parentheses.
1368 If RE-ONLY is non-nil, strip leading `Re:'s only."
1369   (let ((case-fold-search t))           ;Ignore case.
1370     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1371     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1372       (setq subject (substring subject (match-end 0))))
1373     ;; Remove uninteresting prefixes.
1374     (when (and (not re-only)
1375                gnus-simplify-ignored-prefixes
1376                (string-match gnus-simplify-ignored-prefixes subject))
1377       (setq subject (substring subject (match-end 0))))
1378     ;; Remove words in parentheses from end.
1379     (unless re-only
1380       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1381         (setq subject (substring subject 0 (match-beginning 0)))))
1382     ;; Return subject string.
1383     subject))
1384
1385 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1386 ;; all whitespace.
1387 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1388   (goto-char (point-min))
1389   (while (re-search-forward regexp nil t)
1390     (replace-match (or newtext ""))))
1391
1392 (defun gnus-simplify-buffer-fuzzy ()
1393   "Simplify string in the buffer fuzzily.
1394 The string in the accessible portion of the current buffer is simplified.
1395 It is assumed to be a single-line subject.
1396 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1397 matter is removed.  Additional things can be deleted by setting
1398 `gnus-simplify-subject-fuzzy-regexp'."
1399   (let ((case-fold-search t)
1400         (modified-tick))
1401     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1402
1403     (while (not (eq modified-tick (buffer-modified-tick)))
1404       (setq modified-tick (buffer-modified-tick))
1405       (cond
1406        ((listp gnus-simplify-subject-fuzzy-regexp)
1407         (mapcar 'gnus-simplify-buffer-fuzzy-step
1408                 gnus-simplify-subject-fuzzy-regexp))
1409        (gnus-simplify-subject-fuzzy-regexp
1410         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1411       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1412       (gnus-simplify-buffer-fuzzy-step
1413        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1414       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1415
1416     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1417     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1418     (gnus-simplify-buffer-fuzzy-step " $")
1419     (gnus-simplify-buffer-fuzzy-step "^ +")))
1420
1421 (defun gnus-simplify-subject-fuzzy (subject)
1422   "Simplify a subject string fuzzily.
1423 See `gnus-simplify-buffer-fuzzy' for details."
1424   (save-excursion
1425     (gnus-set-work-buffer)
1426     (let ((case-fold-search t))
1427       ;; Remove uninteresting prefixes.
1428       (when (and gnus-simplify-ignored-prefixes
1429                  (string-match gnus-simplify-ignored-prefixes subject))
1430         (setq subject (substring subject (match-end 0))))
1431       (insert subject)
1432       (inline (gnus-simplify-buffer-fuzzy))
1433       (buffer-string))))
1434
1435 (defsubst gnus-simplify-subject-fully (subject)
1436   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1437   (cond
1438    (gnus-simplify-subject-functions
1439     (gnus-map-function gnus-simplify-subject-functions subject))
1440    ((null gnus-summary-gather-subject-limit)
1441     (gnus-simplify-subject-re subject))
1442    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1443     (gnus-simplify-subject-fuzzy subject))
1444    ((numberp gnus-summary-gather-subject-limit)
1445     (gnus-limit-string (gnus-simplify-subject-re subject)
1446                        gnus-summary-gather-subject-limit))
1447    (t
1448     subject)))
1449
1450 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1451   "Check whether two subjects are equal.
1452 If optional argument simple-first is t, first argument is already
1453 simplified."
1454   (cond
1455    ((null simple-first)
1456     (equal (gnus-simplify-subject-fully s1)
1457            (gnus-simplify-subject-fully s2)))
1458    (t
1459     (equal s1
1460            (gnus-simplify-subject-fully s2)))))
1461
1462 (defun gnus-summary-bubble-group ()
1463   "Increase the score of the current group.
1464 This is a handy function to add to `gnus-summary-exit-hook' to
1465 increase the score of each group you read."
1466   (gnus-group-add-score gnus-newsgroup-name))
1467
1468 \f
1469 ;;;
1470 ;;; Gnus summary mode
1471 ;;;
1472
1473 (put 'gnus-summary-mode 'mode-class 'special)
1474
1475 (defvar gnus-article-commands-menu)
1476
1477 (when t
1478   ;; Non-orthogonal keys
1479
1480   (gnus-define-keys gnus-summary-mode-map
1481     " " gnus-summary-next-page
1482     "\177" gnus-summary-prev-page
1483     [delete] gnus-summary-prev-page
1484     [backspace] gnus-summary-prev-page
1485     "\r" gnus-summary-scroll-up
1486     "\M-\r" gnus-summary-scroll-down
1487     "n" gnus-summary-next-unread-article
1488     "p" gnus-summary-prev-unread-article
1489     "N" gnus-summary-next-article
1490     "P" gnus-summary-prev-article
1491     "\M-\C-n" gnus-summary-next-same-subject
1492     "\M-\C-p" gnus-summary-prev-same-subject
1493     "\M-n" gnus-summary-next-unread-subject
1494     "\M-p" gnus-summary-prev-unread-subject
1495     "." gnus-summary-first-unread-article
1496     "," gnus-summary-best-unread-article
1497     "\M-s" gnus-summary-search-article-forward
1498     "\M-r" gnus-summary-search-article-backward
1499     "<" gnus-summary-beginning-of-article
1500     ">" gnus-summary-end-of-article
1501     "j" gnus-summary-goto-article
1502     "^" gnus-summary-refer-parent-article
1503     "\M-^" gnus-summary-refer-article
1504     "u" gnus-summary-tick-article-forward
1505     "!" gnus-summary-tick-article-forward
1506     "U" gnus-summary-tick-article-backward
1507     "d" gnus-summary-mark-as-read-forward
1508     "D" gnus-summary-mark-as-read-backward
1509     "E" gnus-summary-mark-as-expirable
1510     "\M-u" gnus-summary-clear-mark-forward
1511     "\M-U" gnus-summary-clear-mark-backward
1512     "k" gnus-summary-kill-same-subject-and-select
1513     "\C-k" gnus-summary-kill-same-subject
1514     "\M-\C-k" gnus-summary-kill-thread
1515     "\M-\C-l" gnus-summary-lower-thread
1516     "e" gnus-summary-edit-article
1517     "#" gnus-summary-mark-as-processable
1518     "\M-#" gnus-summary-unmark-as-processable
1519     "\M-\C-t" gnus-summary-toggle-threads
1520     "\M-\C-s" gnus-summary-show-thread
1521     "\M-\C-h" gnus-summary-hide-thread
1522     "\M-\C-f" gnus-summary-next-thread
1523     "\M-\C-b" gnus-summary-prev-thread
1524     [(meta down)] gnus-summary-next-thread
1525     [(meta up)] gnus-summary-prev-thread
1526     "\M-\C-u" gnus-summary-up-thread
1527     "\M-\C-d" gnus-summary-down-thread
1528     "&" gnus-summary-execute-command
1529     "c" gnus-summary-catchup-and-exit
1530     "\C-w" gnus-summary-mark-region-as-read
1531     "\C-t" gnus-summary-toggle-truncation
1532     "?" gnus-summary-mark-as-dormant
1533     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1534     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1535     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1536     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1537     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1538     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1539     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1540     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1541     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1542     "=" gnus-summary-expand-window
1543     "\C-x\C-s" gnus-summary-reselect-current-group
1544     "\M-g" gnus-summary-rescan-group
1545     "w" gnus-summary-stop-page-breaking
1546     "\C-c\C-r" gnus-summary-caesar-message
1547     "f" gnus-summary-followup
1548     "F" gnus-summary-followup-with-original
1549     "C" gnus-summary-cancel-article
1550     "r" gnus-summary-reply
1551     "R" gnus-summary-reply-with-original
1552     "\C-c\C-f" gnus-summary-mail-forward
1553     "o" gnus-summary-save-article
1554     "\C-o" gnus-summary-save-article-mail
1555     "|" gnus-summary-pipe-output
1556     "\M-k" gnus-summary-edit-local-kill
1557     "\M-K" gnus-summary-edit-global-kill
1558     ;; "V" gnus-version
1559     "\C-c\C-d" gnus-summary-describe-group
1560     "q" gnus-summary-exit
1561     "Q" gnus-summary-exit-no-update
1562     "\C-c\C-i" gnus-info-find-node
1563     gnus-mouse-2 gnus-mouse-pick-article
1564     "m" gnus-summary-mail-other-window
1565     "a" gnus-summary-post-news
1566     "i" gnus-summary-news-other-window
1567     "x" gnus-summary-limit-to-unread
1568     "s" gnus-summary-isearch-article
1569     "t" gnus-summary-toggle-header
1570     "g" gnus-summary-show-article
1571     "l" gnus-summary-goto-last-article
1572     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1573     "\C-d" gnus-summary-enter-digest-group
1574     "\M-\C-d" gnus-summary-read-document
1575     "\M-\C-e" gnus-summary-edit-parameters
1576     "\M-\C-a" gnus-summary-customize-parameters
1577     "\C-c\C-b" gnus-bug
1578     "*" gnus-cache-enter-article
1579     "\M-*" gnus-cache-remove-article
1580     "\M-&" gnus-summary-universal-argument
1581     "\C-l" gnus-recenter
1582     "I" gnus-summary-increase-score
1583     "L" gnus-summary-lower-score
1584     "\M-i" gnus-symbolic-argument
1585     "h" gnus-summary-select-article-buffer
1586
1587     "b" gnus-article-view-part
1588     "\M-t" gnus-summary-toggle-display-buttonized
1589
1590     "V" gnus-summary-score-map
1591     "X" gnus-uu-extract-map
1592     "S" gnus-summary-send-map)
1593
1594   ;; Sort of orthogonal keymap
1595   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1596     "t" gnus-summary-tick-article-forward
1597     "!" gnus-summary-tick-article-forward
1598     "d" gnus-summary-mark-as-read-forward
1599     "r" gnus-summary-mark-as-read-forward
1600     "c" gnus-summary-clear-mark-forward
1601     " " gnus-summary-clear-mark-forward
1602     "e" gnus-summary-mark-as-expirable
1603     "x" gnus-summary-mark-as-expirable
1604     "?" gnus-summary-mark-as-dormant
1605     "b" gnus-summary-set-bookmark
1606     "B" gnus-summary-remove-bookmark
1607     "#" gnus-summary-mark-as-processable
1608     "\M-#" gnus-summary-unmark-as-processable
1609     "S" gnus-summary-limit-include-expunged
1610     "C" gnus-summary-catchup
1611     "H" gnus-summary-catchup-to-here
1612     "h" gnus-summary-catchup-from-here
1613     "\C-c" gnus-summary-catchup-all
1614     "k" gnus-summary-kill-same-subject-and-select
1615     "K" gnus-summary-kill-same-subject
1616     "P" gnus-uu-mark-map)
1617
1618   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1619     "c" gnus-summary-clear-above
1620     "u" gnus-summary-tick-above
1621     "m" gnus-summary-mark-above
1622     "k" gnus-summary-kill-below)
1623
1624   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1625     "/" gnus-summary-limit-to-subject
1626     "n" gnus-summary-limit-to-articles
1627     "w" gnus-summary-pop-limit
1628     "s" gnus-summary-limit-to-subject
1629     "a" gnus-summary-limit-to-author
1630     "u" gnus-summary-limit-to-unread
1631     "m" gnus-summary-limit-to-marks
1632     "M" gnus-summary-limit-exclude-marks
1633     "v" gnus-summary-limit-to-score
1634     "*" gnus-summary-limit-include-cached
1635     "D" gnus-summary-limit-include-dormant
1636     "T" gnus-summary-limit-include-thread
1637     "d" gnus-summary-limit-exclude-dormant
1638     "t" gnus-summary-limit-to-age
1639     "x" gnus-summary-limit-to-extra
1640     "p" gnus-summary-limit-to-display-predicate
1641     "E" gnus-summary-limit-include-expunged
1642     "c" gnus-summary-limit-exclude-childless-dormant
1643     "C" gnus-summary-limit-mark-excluded-as-read
1644     "o" gnus-summary-insert-old-articles
1645     "N" gnus-summary-insert-new-articles)
1646
1647   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1648     "n" gnus-summary-next-unread-article
1649     "p" gnus-summary-prev-unread-article
1650     "N" gnus-summary-next-article
1651     "P" gnus-summary-prev-article
1652     "\C-n" gnus-summary-next-same-subject
1653     "\C-p" gnus-summary-prev-same-subject
1654     "\M-n" gnus-summary-next-unread-subject
1655     "\M-p" gnus-summary-prev-unread-subject
1656     "f" gnus-summary-first-unread-article
1657     "b" gnus-summary-best-unread-article
1658     "j" gnus-summary-goto-article
1659     "g" gnus-summary-goto-subject
1660     "l" gnus-summary-goto-last-article
1661     "o" gnus-summary-pop-article)
1662
1663   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1664     "k" gnus-summary-kill-thread
1665     "l" gnus-summary-lower-thread
1666     "i" gnus-summary-raise-thread
1667     "T" gnus-summary-toggle-threads
1668     "t" gnus-summary-rethread-current
1669     "^" gnus-summary-reparent-thread
1670     "s" gnus-summary-show-thread
1671     "S" gnus-summary-show-all-threads
1672     "h" gnus-summary-hide-thread
1673     "H" gnus-summary-hide-all-threads
1674     "n" gnus-summary-next-thread
1675     "p" gnus-summary-prev-thread
1676     "u" gnus-summary-up-thread
1677     "o" gnus-summary-top-thread
1678     "d" gnus-summary-down-thread
1679     "#" gnus-uu-mark-thread
1680     "\M-#" gnus-uu-unmark-thread)
1681
1682   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1683     "g" gnus-summary-prepare
1684     "c" gnus-summary-insert-cached-articles)
1685
1686   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1687     "c" gnus-summary-catchup-and-exit
1688     "C" gnus-summary-catchup-all-and-exit
1689     "E" gnus-summary-exit-no-update
1690     "Q" gnus-summary-exit
1691     "Z" gnus-summary-exit
1692     "n" gnus-summary-catchup-and-goto-next-group
1693     "R" gnus-summary-reselect-current-group
1694     "G" gnus-summary-rescan-group
1695     "N" gnus-summary-next-group
1696     "s" gnus-summary-save-newsrc
1697     "P" gnus-summary-prev-group)
1698
1699   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1700     " " gnus-summary-next-page
1701     "n" gnus-summary-next-page
1702     "\177" gnus-summary-prev-page
1703     [delete] gnus-summary-prev-page
1704     "p" gnus-summary-prev-page
1705     "\r" gnus-summary-scroll-up
1706     "\M-\r" gnus-summary-scroll-down
1707     "<" gnus-summary-beginning-of-article
1708     ">" gnus-summary-end-of-article
1709     "b" gnus-summary-beginning-of-article
1710     "e" gnus-summary-end-of-article
1711     "^" gnus-summary-refer-parent-article
1712     "r" gnus-summary-refer-parent-article
1713     "D" gnus-summary-enter-digest-group
1714     "R" gnus-summary-refer-references
1715     "T" gnus-summary-refer-thread
1716     "g" gnus-summary-show-article
1717     "s" gnus-summary-isearch-article
1718     "P" gnus-summary-print-article
1719     "M" gnus-mailing-list-insinuate
1720     "t" gnus-article-babel)
1721
1722   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1723     "b" gnus-article-add-buttons
1724     "B" gnus-article-add-buttons-to-head
1725     "o" gnus-article-treat-overstrike
1726     "e" gnus-article-emphasize
1727     "w" gnus-article-fill-cited-article
1728     "Q" gnus-article-fill-long-lines
1729     "C" gnus-article-capitalize-sentences
1730     "c" gnus-article-remove-cr
1731     "q" gnus-article-de-quoted-unreadable
1732     "6" gnus-article-de-base64-unreadable
1733     "Z" gnus-article-decode-HZ
1734     "h" gnus-article-wash-html
1735     "u" gnus-article-unsplit-urls
1736     "s" gnus-summary-force-verify-and-decrypt
1737     "f" gnus-article-display-x-face
1738     "l" gnus-summary-stop-page-breaking
1739     "r" gnus-summary-caesar-message
1740     "t" gnus-summary-toggle-header
1741     "g" gnus-treat-smiley
1742     "v" gnus-summary-verbose-headers
1743     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1744     "p" gnus-article-verify-x-pgp-sig
1745     "d" gnus-article-treat-dumbquotes
1746     "k" gnus-article-outlook-deuglify-article)
1747
1748   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1749     "a" gnus-article-hide
1750     "h" gnus-article-hide-headers
1751     "b" gnus-article-hide-boring-headers
1752     "s" gnus-article-hide-signature
1753     "c" gnus-article-hide-citation
1754     "C" gnus-article-hide-citation-in-followups
1755     "l" gnus-article-hide-list-identifiers
1756     "p" gnus-article-hide-pgp
1757     "B" gnus-article-strip-banner
1758     "P" gnus-article-hide-pem
1759     "\C-c" gnus-article-hide-citation-maybe)
1760
1761   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1762     "a" gnus-article-highlight
1763     "h" gnus-article-highlight-headers
1764     "c" gnus-article-highlight-citation
1765     "s" gnus-article-highlight-signature)
1766
1767   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1768     "f" gnus-article-treat-fold-headers
1769     "u" gnus-article-treat-unfold-headers
1770     "n" gnus-article-treat-fold-newsgroups)
1771
1772   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1773     "x" gnus-article-display-x-face
1774     "s" gnus-treat-smiley
1775     "D" gnus-article-remove-images
1776     "f" gnus-treat-from-picon
1777     "m" gnus-treat-mail-picon
1778     "n" gnus-treat-newsgroups-picon)
1779
1780   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1781     "w" gnus-article-decode-mime-words
1782     "c" gnus-article-decode-charset
1783     "v" gnus-mime-view-all-parts
1784     "b" gnus-article-view-part)
1785
1786   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1787     "z" gnus-article-date-ut
1788     "u" gnus-article-date-ut
1789     "l" gnus-article-date-local
1790     "p" gnus-article-date-english
1791     "e" gnus-article-date-lapsed
1792     "o" gnus-article-date-original
1793     "i" gnus-article-date-iso8601
1794     "s" gnus-article-date-user)
1795
1796   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1797     "t" gnus-article-remove-trailing-blank-lines
1798     "l" gnus-article-strip-leading-blank-lines
1799     "m" gnus-article-strip-multiple-blank-lines
1800     "a" gnus-article-strip-blank-lines
1801     "A" gnus-article-strip-all-blank-lines
1802     "s" gnus-article-strip-leading-space
1803     "e" gnus-article-strip-trailing-space
1804     "w" gnus-article-remove-leading-whitespace)
1805
1806   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1807     "v" gnus-version
1808     "f" gnus-summary-fetch-faq
1809     "d" gnus-summary-describe-group
1810     "h" gnus-summary-describe-briefly
1811     "i" gnus-info-find-node)
1812
1813   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1814     "e" gnus-summary-expire-articles
1815     "\M-\C-e" gnus-summary-expire-articles-now
1816     "\177" gnus-summary-delete-article
1817     [delete] gnus-summary-delete-article
1818     [backspace] gnus-summary-delete-article
1819     "m" gnus-summary-move-article
1820     "r" gnus-summary-respool-article
1821     "w" gnus-summary-edit-article
1822     "c" gnus-summary-copy-article
1823     "B" gnus-summary-crosspost-article
1824     "q" gnus-summary-respool-query
1825     "t" gnus-summary-respool-trace
1826     "i" gnus-summary-import-article
1827     "I" gnus-summary-create-article
1828     "p" gnus-summary-article-posted-p)
1829
1830   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1831     "o" gnus-summary-save-article
1832     "m" gnus-summary-save-article-mail
1833     "F" gnus-summary-write-article-file
1834     "r" gnus-summary-save-article-rmail
1835     "f" gnus-summary-save-article-file
1836     "b" gnus-summary-save-article-body-file
1837     "h" gnus-summary-save-article-folder
1838     "v" gnus-summary-save-article-vm
1839     "p" gnus-summary-pipe-output
1840     "P" gnus-summary-muttprint
1841     "s" gnus-soup-add-article)
1842
1843   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1844     "b" gnus-summary-display-buttonized
1845     "m" gnus-summary-repair-multipart
1846     "v" gnus-article-view-part
1847     "o" gnus-article-save-part
1848     "c" gnus-article-copy-part
1849     "C" gnus-article-view-part-as-charset
1850     "e" gnus-article-view-part-externally
1851     "E" gnus-article-encrypt-body
1852     "i" gnus-article-inline-part
1853     "|" gnus-article-pipe-part)
1854
1855   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1856     "p" gnus-summary-mark-as-processable
1857     "u" gnus-summary-unmark-as-processable
1858     "U" gnus-summary-unmark-all-processable
1859     "v" gnus-uu-mark-over
1860     "s" gnus-uu-mark-series
1861     "r" gnus-uu-mark-region
1862     "g" gnus-uu-unmark-region
1863     "R" gnus-uu-mark-by-regexp
1864     "G" gnus-uu-unmark-by-regexp
1865     "t" gnus-uu-mark-thread
1866     "T" gnus-uu-unmark-thread
1867     "a" gnus-uu-mark-all
1868     "b" gnus-uu-mark-buffer
1869     "S" gnus-uu-mark-sparse
1870     "k" gnus-summary-kill-process-mark
1871     "y" gnus-summary-yank-process-mark
1872     "w" gnus-summary-save-process-mark
1873     "i" gnus-uu-invert-processable)
1874
1875   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1876     ;;"x" gnus-uu-extract-any
1877     "m" gnus-summary-save-parts
1878     "u" gnus-uu-decode-uu
1879     "U" gnus-uu-decode-uu-and-save
1880     "s" gnus-uu-decode-unshar
1881     "S" gnus-uu-decode-unshar-and-save
1882     "o" gnus-uu-decode-save
1883     "O" gnus-uu-decode-save
1884     "b" gnus-uu-decode-binhex
1885     "B" gnus-uu-decode-binhex
1886     "p" gnus-uu-decode-postscript
1887     "P" gnus-uu-decode-postscript-and-save)
1888
1889   (gnus-define-keys
1890       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1891     "u" gnus-uu-decode-uu-view
1892     "U" gnus-uu-decode-uu-and-save-view
1893     "s" gnus-uu-decode-unshar-view
1894     "S" gnus-uu-decode-unshar-and-save-view
1895     "o" gnus-uu-decode-save-view
1896     "O" gnus-uu-decode-save-view
1897     "b" gnus-uu-decode-binhex-view
1898     "B" gnus-uu-decode-binhex-view
1899     "p" gnus-uu-decode-postscript-view
1900     "P" gnus-uu-decode-postscript-and-save-view))
1901
1902 (defvar gnus-article-post-menu nil)
1903
1904 (defun gnus-summary-make-menu-bar ()
1905   (gnus-turn-off-edit-menu 'summary)
1906
1907   (unless (boundp 'gnus-summary-misc-menu)
1908
1909     (easy-menu-define
1910       gnus-summary-kill-menu gnus-summary-mode-map ""
1911       (cons
1912        "Score"
1913        (nconc
1914         (list
1915          ["Customize" gnus-score-customize t])
1916         (gnus-make-score-map 'increase)
1917         (gnus-make-score-map 'lower)
1918         '(("Mark"
1919            ["Kill below" gnus-summary-kill-below t]
1920            ["Mark above" gnus-summary-mark-above t]
1921            ["Tick above" gnus-summary-tick-above t]
1922            ["Clear above" gnus-summary-clear-above t])
1923           ["Current score" gnus-summary-current-score t]
1924           ["Set score" gnus-summary-set-score t]
1925           ["Switch current score file..." gnus-score-change-score-file t]
1926           ["Set mark below..." gnus-score-set-mark-below t]
1927           ["Set expunge below..." gnus-score-set-expunge-below t]
1928           ["Edit current score file" gnus-score-edit-current-scores t]
1929           ["Edit score file" gnus-score-edit-file t]
1930           ["Trace score" gnus-score-find-trace t]
1931           ["Find words" gnus-score-find-favourite-words t]
1932           ["Rescore buffer" gnus-summary-rescore t]
1933           ["Increase score..." gnus-summary-increase-score t]
1934           ["Lower score..." gnus-summary-lower-score t]))))
1935
1936     ;; Define both the Article menu in the summary buffer and the
1937     ;; equivalent Commands menu in the article buffer here for
1938     ;; consistency.
1939     (let ((innards
1940            `(("Hide"
1941               ["All" gnus-article-hide t]
1942               ["Headers" gnus-article-hide-headers t]
1943               ["Signature" gnus-article-hide-signature t]
1944               ["Citation" gnus-article-hide-citation t]
1945               ["List identifiers" gnus-article-hide-list-identifiers t]
1946               ["PGP" gnus-article-hide-pgp t]
1947               ["Banner" gnus-article-strip-banner t]
1948               ["Boring headers" gnus-article-hide-boring-headers t])
1949              ("Highlight"
1950               ["All" gnus-article-highlight t]
1951               ["Headers" gnus-article-highlight-headers t]
1952               ["Signature" gnus-article-highlight-signature t]
1953               ["Citation" gnus-article-highlight-citation t])
1954              ("MIME"
1955               ["Words" gnus-article-decode-mime-words t]
1956               ["Charset" gnus-article-decode-charset t]
1957               ["QP" gnus-article-de-quoted-unreadable t]
1958               ["Base64" gnus-article-de-base64-unreadable t]
1959               ["View all" gnus-mime-view-all-parts t]
1960               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1961               ["Encrypt body" gnus-article-encrypt-body t]
1962               ["Extract all parts" gnus-summary-save-parts t])
1963              ("Date"
1964               ["Local" gnus-article-date-local t]
1965               ["ISO8601" gnus-article-date-iso8601 t]
1966               ["UT" gnus-article-date-ut t]
1967               ["Original" gnus-article-date-original t]
1968               ["Lapsed" gnus-article-date-lapsed t]
1969               ["User-defined" gnus-article-date-user t])
1970              ("Display"
1971               ["Remove images" gnus-article-remove-images t]
1972               ["Toggle smiley" gnus-treat-smiley t]
1973               ["Show X-Face" gnus-article-display-x-face t]
1974               ["Show picons in From" gnus-treat-from-picon t]
1975               ["Show picons in mail headers" gnus-treat-mail-picon t]
1976               ["Show picons in news headers" gnus-treat-newsgroups-picon t])
1977              ("Washing"
1978               ("Remove Blanks"
1979                ["Leading" gnus-article-strip-leading-blank-lines t]
1980                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1981                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1982                ["All of the above" gnus-article-strip-blank-lines t]
1983                ["All" gnus-article-strip-all-blank-lines t]
1984                ["Leading space" gnus-article-strip-leading-space t]
1985                ["Trailing space" gnus-article-strip-trailing-space t]
1986                ["Leading space in headers"
1987                 gnus-article-remove-leading-whitespace t])
1988               ["Overstrike" gnus-article-treat-overstrike t]
1989               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1990               ["Emphasis" gnus-article-emphasize t]
1991               ["Word wrap" gnus-article-fill-cited-article t]
1992               ["Fill long lines" gnus-article-fill-long-lines t]
1993               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1994               ["CR" gnus-article-remove-cr t]
1995               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1996               ["Base64" gnus-article-de-base64-unreadable t]
1997               ["Rot 13" gnus-summary-caesar-message
1998                ,@(if (featurep 'xemacs) '(t)
1999                    '(:help "\"Caesar rotate\" article by 13"))]
2000               ["Unix pipe" gnus-summary-pipe-message t]
2001               ["Add buttons" gnus-article-add-buttons t]
2002               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2003               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2004               ["Verbose header" gnus-summary-verbose-headers t]
2005               ["Toggle header" gnus-summary-toggle-header t]
2006               ["Unfold headers" gnus-article-treat-unfold-headers t]
2007               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2008               ["Html" gnus-article-wash-html t]
2009               ["URLs" gnus-article-unsplit-urls t]
2010               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2011               ["HZ" gnus-article-decode-HZ t]
2012               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2013               )
2014              ("Output"
2015               ["Save in default format" gnus-summary-save-article
2016                ,@(if (featurep 'xemacs) '(t)
2017                    '(:help "Save article using default method"))]
2018               ["Save in file" gnus-summary-save-article-file
2019                ,@(if (featurep 'xemacs) '(t)
2020                    '(:help "Save article in file"))]
2021               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2022               ["Save in MH folder" gnus-summary-save-article-folder t]
2023               ["Save in VM folder" gnus-summary-save-article-vm t]
2024               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2025               ["Save body in file" gnus-summary-save-article-body-file t]
2026               ["Pipe through a filter" gnus-summary-pipe-output t]
2027               ["Add to SOUP packet" gnus-soup-add-article t]
2028               ["Print with Muttprint" gnus-summary-muttprint t]
2029               ["Print" gnus-summary-print-article t])
2030              ("Backend"
2031               ["Respool article..." gnus-summary-respool-article t]
2032               ["Move article..." gnus-summary-move-article
2033                (gnus-check-backend-function
2034                 'request-move-article gnus-newsgroup-name)]
2035               ["Copy article..." gnus-summary-copy-article t]
2036               ["Crosspost article..." gnus-summary-crosspost-article
2037                (gnus-check-backend-function
2038                 'request-replace-article gnus-newsgroup-name)]
2039               ["Import file..." gnus-summary-import-article t]
2040               ["Create article..." gnus-summary-create-article t]
2041               ["Check if posted" gnus-summary-article-posted-p t]
2042               ["Edit article" gnus-summary-edit-article
2043                (not (gnus-group-read-only-p))]
2044               ["Delete article" gnus-summary-delete-article
2045                (gnus-check-backend-function
2046                 'request-expire-articles gnus-newsgroup-name)]
2047               ["Query respool" gnus-summary-respool-query t]
2048               ["Trace respool" gnus-summary-respool-trace t]
2049               ["Delete expirable articles" gnus-summary-expire-articles-now
2050                (gnus-check-backend-function
2051                 'request-expire-articles gnus-newsgroup-name)])
2052              ("Extract"
2053               ["Uudecode" gnus-uu-decode-uu
2054                ,@(if (featurep 'xemacs) '(t)
2055                    '(:help "Decode uuencoded article(s)"))]
2056               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2057               ["Unshar" gnus-uu-decode-unshar t]
2058               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2059               ["Save" gnus-uu-decode-save t]
2060               ["Binhex" gnus-uu-decode-binhex t]
2061               ["Postscript" gnus-uu-decode-postscript t]
2062               ["all MIME parts" gnus-summary-save-parts t])
2063              ("Cache"
2064               ["Enter article" gnus-cache-enter-article t]
2065               ["Remove article" gnus-cache-remove-article t])
2066              ["Translate" gnus-article-babel t]
2067              ["Select article buffer" gnus-summary-select-article-buffer t]
2068              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2069              ["Isearch article..." gnus-summary-isearch-article t]
2070              ["Beginning of the article" gnus-summary-beginning-of-article t]
2071              ["End of the article" gnus-summary-end-of-article t]
2072              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2073              ["Fetch referenced articles" gnus-summary-refer-references t]
2074              ["Fetch current thread" gnus-summary-refer-thread t]
2075              ["Fetch article with id..." gnus-summary-refer-article t]
2076              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2077              ["Redisplay" gnus-summary-show-article t]
2078              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2079       (easy-menu-define
2080         gnus-summary-article-menu gnus-summary-mode-map ""
2081         (cons "Article" innards))
2082
2083       (if (not (keymapp gnus-summary-article-menu))
2084           (easy-menu-define
2085             gnus-article-commands-menu gnus-article-mode-map ""
2086             (cons "Commands" innards))
2087         ;; in Emacs, don't share menu.
2088         (setq gnus-article-commands-menu
2089               (copy-keymap gnus-summary-article-menu))
2090         (define-key gnus-article-mode-map [menu-bar commands]
2091           (cons "Commands" gnus-article-commands-menu))))
2092
2093     (easy-menu-define
2094       gnus-summary-thread-menu gnus-summary-mode-map ""
2095       '("Threads"
2096         ["Toggle threading" gnus-summary-toggle-threads t]
2097         ["Hide threads" gnus-summary-hide-all-threads t]
2098         ["Show threads" gnus-summary-show-all-threads t]
2099         ["Hide thread" gnus-summary-hide-thread t]
2100         ["Show thread" gnus-summary-show-thread t]
2101         ["Go to next thread" gnus-summary-next-thread t]
2102         ["Go to previous thread" gnus-summary-prev-thread t]
2103         ["Go down thread" gnus-summary-down-thread t]
2104         ["Go up thread" gnus-summary-up-thread t]
2105         ["Top of thread" gnus-summary-top-thread t]
2106         ["Mark thread as read" gnus-summary-kill-thread t]
2107         ["Lower thread score" gnus-summary-lower-thread t]
2108         ["Raise thread score" gnus-summary-raise-thread t]
2109         ["Rethread current" gnus-summary-rethread-current t]))
2110
2111     (easy-menu-define
2112       gnus-summary-post-menu gnus-summary-mode-map ""
2113       `("Post"
2114         ["Send a message (mail or news)" gnus-summary-post-news
2115          ,@(if (featurep 'xemacs) '(t)
2116              '(:help "Post an article"))]
2117         ["Followup" gnus-summary-followup
2118          ,@(if (featurep 'xemacs) '(t)
2119              '(:help "Post followup to this article"))]
2120         ["Followup and yank" gnus-summary-followup-with-original
2121          ,@(if (featurep 'xemacs) '(t)
2122              '(:help "Post followup to this article, quoting its contents"))]
2123         ["Supersede article" gnus-summary-supersede-article t]
2124         ["Cancel article" gnus-summary-cancel-article
2125          ,@(if (featurep 'xemacs) '(t)
2126              '(:help "Cancel an article you posted"))]
2127         ["Reply" gnus-summary-reply t]
2128         ["Reply and yank" gnus-summary-reply-with-original t]
2129         ["Wide reply" gnus-summary-wide-reply t]
2130         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2131          ,@(if (featurep 'xemacs) '(t)
2132              '(:help "Mail a reply, quoting this article"))]
2133         ["Very wide reply" gnus-summary-very-wide-reply t]
2134         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2135          ,@(if (featurep 'xemacs) '(t)
2136              '(:help "Mail a very wide reply, quoting this article"))]
2137         ["Mail forward" gnus-summary-mail-forward t]
2138         ["Post forward" gnus-summary-post-forward t]
2139         ["Digest and mail" gnus-uu-digest-mail-forward t]
2140         ["Digest and post" gnus-uu-digest-post-forward t]
2141         ["Resend message" gnus-summary-resend-message t]
2142         ["Resend message edit" gnus-summary-resend-message-edit t]
2143         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2144         ["Send a mail" gnus-summary-mail-other-window t]
2145         ["Create a local message" gnus-summary-news-other-window t]
2146         ["Uuencode and post" gnus-uu-post-news
2147          ,@(if (featurep 'xemacs) '(t)
2148              '(:help "Post a uuencoded article"))]
2149         ["Followup via news" gnus-summary-followup-to-mail t]
2150         ["Followup via news and yank"
2151          gnus-summary-followup-to-mail-with-original t]
2152         ;;("Draft"
2153         ;;["Send" gnus-summary-send-draft t]
2154         ;;["Send bounced" gnus-resend-bounced-mail t])
2155         ))
2156
2157     (cond
2158      ((not (keymapp gnus-summary-post-menu))
2159       (setq gnus-article-post-menu gnus-summary-post-menu))
2160      ((not gnus-article-post-menu)
2161       ;; Don't share post menu.
2162       (setq gnus-article-post-menu
2163             (copy-keymap gnus-summary-post-menu))))
2164     (define-key gnus-article-mode-map [menu-bar post]
2165       (cons "Post" gnus-article-post-menu))
2166
2167     (easy-menu-define
2168       gnus-summary-misc-menu gnus-summary-mode-map ""
2169       `("Gnus"
2170         ("Mark Read"
2171          ["Mark as read" gnus-summary-mark-as-read-forward t]
2172          ["Mark same subject and select"
2173           gnus-summary-kill-same-subject-and-select t]
2174          ["Mark same subject" gnus-summary-kill-same-subject t]
2175          ["Catchup" gnus-summary-catchup
2176           ,@(if (featurep 'xemacs) '(t)
2177               '(:help "Mark unread articles in this group as read"))]
2178          ["Catchup all" gnus-summary-catchup-all t]
2179          ["Catchup to here" gnus-summary-catchup-to-here t]
2180          ["Catchup from here" gnus-summary-catchup-from-here t]
2181          ["Catchup region" gnus-summary-mark-region-as-read t]
2182          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2183         ("Mark Various"
2184          ["Tick" gnus-summary-tick-article-forward t]
2185          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2186          ["Remove marks" gnus-summary-clear-mark-forward t]
2187          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2188          ["Set bookmark" gnus-summary-set-bookmark t]
2189          ["Remove bookmark" gnus-summary-remove-bookmark t])
2190         ("Limit to"
2191          ["Marks..." gnus-summary-limit-to-marks t]
2192          ["Subject..." gnus-summary-limit-to-subject t]
2193          ["Author..." gnus-summary-limit-to-author t]
2194          ["Age..." gnus-summary-limit-to-age t]
2195          ["Extra..." gnus-summary-limit-to-extra t]
2196          ["Score" gnus-summary-limit-to-score t]
2197          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2198          ["Unread" gnus-summary-limit-to-unread t]
2199          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2200          ["Articles" gnus-summary-limit-to-articles t]
2201          ["Pop limit" gnus-summary-pop-limit t]
2202          ["Show dormant" gnus-summary-limit-include-dormant t]
2203          ["Hide childless dormant"
2204           gnus-summary-limit-exclude-childless-dormant t]
2205          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2206          ["Hide marked" gnus-summary-limit-exclude-marks t]
2207          ["Show expunged" gnus-summary-limit-include-expunged t])
2208         ("Process Mark"
2209          ["Set mark" gnus-summary-mark-as-processable t]
2210          ["Remove mark" gnus-summary-unmark-as-processable t]
2211          ["Remove all marks" gnus-summary-unmark-all-processable t]
2212          ["Mark above" gnus-uu-mark-over t]
2213          ["Mark series" gnus-uu-mark-series t]
2214          ["Mark region" gnus-uu-mark-region t]
2215          ["Unmark region" gnus-uu-unmark-region t]
2216          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2217          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2218          ["Mark all" gnus-uu-mark-all t]
2219          ["Mark buffer" gnus-uu-mark-buffer t]
2220          ["Mark sparse" gnus-uu-mark-sparse t]
2221          ["Mark thread" gnus-uu-mark-thread t]
2222          ["Unmark thread" gnus-uu-unmark-thread t]
2223          ("Process Mark Sets"
2224           ["Kill" gnus-summary-kill-process-mark t]
2225           ["Yank" gnus-summary-yank-process-mark
2226            gnus-newsgroup-process-stack]
2227           ["Save" gnus-summary-save-process-mark t]))
2228         ("Scroll article"
2229          ["Page forward" gnus-summary-next-page
2230           ,@(if (featurep 'xemacs) '(t)
2231               '(:help "Show next page of article"))]
2232          ["Page backward" gnus-summary-prev-page
2233           ,@(if (featurep 'xemacs) '(t)
2234               '(:help "Show previous page of article"))]
2235          ["Line forward" gnus-summary-scroll-up t])
2236         ("Move"
2237          ["Next unread article" gnus-summary-next-unread-article t]
2238          ["Previous unread article" gnus-summary-prev-unread-article t]
2239          ["Next article" gnus-summary-next-article t]
2240          ["Previous article" gnus-summary-prev-article t]
2241          ["Next unread subject" gnus-summary-next-unread-subject t]
2242          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2243          ["Next article same subject" gnus-summary-next-same-subject t]
2244          ["Previous article same subject" gnus-summary-prev-same-subject t]
2245          ["First unread article" gnus-summary-first-unread-article t]
2246          ["Best unread article" gnus-summary-best-unread-article t]
2247          ["Go to subject number..." gnus-summary-goto-subject t]
2248          ["Go to article number..." gnus-summary-goto-article t]
2249          ["Go to the last article" gnus-summary-goto-last-article t]
2250          ["Pop article off history" gnus-summary-pop-article t])
2251         ("Sort"
2252          ["Sort by number" gnus-summary-sort-by-number t]
2253          ["Sort by author" gnus-summary-sort-by-author t]
2254          ["Sort by subject" gnus-summary-sort-by-subject t]
2255          ["Sort by date" gnus-summary-sort-by-date t]
2256          ["Sort by score" gnus-summary-sort-by-score t]
2257          ["Sort by lines" gnus-summary-sort-by-lines t]
2258          ["Sort by characters" gnus-summary-sort-by-chars t]
2259          ["Original sort" gnus-summary-sort-by-original t])
2260         ("Help"
2261          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2262          ["Describe group" gnus-summary-describe-group t]
2263          ["Read manual" gnus-info-find-node t])
2264         ("Modes"
2265          ["Pick and read" gnus-pick-mode t]
2266          ["Binary" gnus-binary-mode t])
2267         ("Regeneration"
2268          ["Regenerate" gnus-summary-prepare t]
2269          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2270          ["Toggle threading" gnus-summary-toggle-threads t])
2271         ["See old articles" gnus-summary-insert-old-articles t]
2272         ["See new articles" gnus-summary-insert-new-articles t]
2273         ["Filter articles..." gnus-summary-execute-command t]
2274         ["Run command on subjects..." gnus-summary-universal-argument t]
2275         ["Search articles forward..." gnus-summary-search-article-forward t]
2276         ["Search articles backward..." gnus-summary-search-article-backward t]
2277         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2278         ["Expand window" gnus-summary-expand-window t]
2279         ["Expire expirable articles" gnus-summary-expire-articles
2280          (gnus-check-backend-function
2281           'request-expire-articles gnus-newsgroup-name)]
2282         ["Edit local kill file" gnus-summary-edit-local-kill t]
2283         ["Edit main kill file" gnus-summary-edit-global-kill t]
2284         ["Edit group parameters" gnus-summary-edit-parameters t]
2285         ["Customize group parameters" gnus-summary-customize-parameters t]
2286         ["Send a bug report" gnus-bug t]
2287         ("Exit"
2288          ["Catchup and exit" gnus-summary-catchup-and-exit
2289           ,@(if (featurep 'xemacs) '(t)
2290               '(:help "Mark unread articles in this group as read, then exit"))]
2291          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2292          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2293          ["Exit group" gnus-summary-exit
2294           ,@(if (featurep 'xemacs) '(t)
2295               '(:help "Exit current group, return to group selection mode"))]
2296          ["Exit group without updating" gnus-summary-exit-no-update t]
2297          ["Exit and goto next group" gnus-summary-next-group t]
2298          ["Exit and goto prev group" gnus-summary-prev-group t]
2299          ["Reselect group" gnus-summary-reselect-current-group t]
2300          ["Rescan group" gnus-summary-rescan-group t]
2301          ["Update dribble" gnus-summary-save-newsrc t])))
2302
2303     (gnus-run-hooks 'gnus-summary-menu-hook)))
2304
2305 (defvar gnus-summary-tool-bar-map nil)
2306
2307 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2308 (defun gnus-summary-make-tool-bar ()
2309   (if (and (fboundp 'tool-bar-add-item-from-menu)
2310            (default-value 'tool-bar-mode)
2311            (not gnus-summary-tool-bar-map))
2312       (setq gnus-summary-tool-bar-map
2313             (let ((tool-bar-map (make-sparse-keymap))
2314                   (load-path (mm-image-load-path)))
2315               (tool-bar-add-item-from-menu
2316                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2317               (tool-bar-add-item-from-menu
2318                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2319               (tool-bar-add-item-from-menu
2320                'gnus-summary-post-news "post" gnus-summary-mode-map)
2321               (tool-bar-add-item-from-menu
2322                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2323               (tool-bar-add-item-from-menu
2324                'gnus-summary-followup "followup" gnus-summary-mode-map)
2325               (tool-bar-add-item-from-menu
2326                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2327               (tool-bar-add-item-from-menu
2328                'gnus-summary-reply "reply" gnus-summary-mode-map)
2329               (tool-bar-add-item-from-menu
2330                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2331               (tool-bar-add-item-from-menu
2332                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2333               (tool-bar-add-item-from-menu
2334                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2335               (tool-bar-add-item-from-menu
2336                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2337               (tool-bar-add-item-from-menu
2338                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2339               (tool-bar-add-item-from-menu
2340                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2341               (tool-bar-add-item-from-menu
2342                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2343               (tool-bar-add-item-from-menu
2344                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2345               tool-bar-map)))
2346   (if gnus-summary-tool-bar-map
2347       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2348
2349 (defun gnus-score-set-default (var value)
2350   "A version of set that updates the GNU Emacs menu-bar."
2351   (set var value)
2352   ;; It is the message that forces the active status to be updated.
2353   (message ""))
2354
2355 (defun gnus-make-score-map (type)
2356   "Make a summary score map of type TYPE."
2357   (if t
2358       nil
2359     (let ((headers '(("author" "from" string)
2360                      ("subject" "subject" string)
2361                      ("article body" "body" string)
2362                      ("article head" "head" string)
2363                      ("xref" "xref" string)
2364                      ("extra header" "extra" string)
2365                      ("lines" "lines" number)
2366                      ("followups to author" "followup" string)))
2367           (types '((number ("less than" <)
2368                            ("greater than" >)
2369                            ("equal" =))
2370                    (string ("substring" s)
2371                            ("exact string" e)
2372                            ("fuzzy string" f)
2373                            ("regexp" r))))
2374           (perms '(("temporary" (current-time-string))
2375                    ("permanent" nil)
2376                    ("immediate" now)))
2377           header)
2378       (list
2379        (apply
2380         'nconc
2381         (list
2382          (if (eq type 'lower)
2383              "Lower score"
2384            "Increase score"))
2385         (let (outh)
2386           (while headers
2387             (setq header (car headers))
2388             (setq outh
2389                   (cons
2390                    (apply
2391                     'nconc
2392                     (list (car header))
2393                     (let ((ts (cdr (assoc (nth 2 header) types)))
2394                           outt)
2395                       (while ts
2396                         (setq outt
2397                               (cons
2398                                (apply
2399                                 'nconc
2400                                 (list (caar ts))
2401                                 (let ((ps perms)
2402                                       outp)
2403                                   (while ps
2404                                     (setq outp
2405                                           (cons
2406                                            (vector
2407                                             (caar ps)
2408                                             (list
2409                                              'gnus-summary-score-entry
2410                                              (nth 1 header)
2411                                              (if (or (string= (nth 1 header)
2412                                                               "head")
2413                                                      (string= (nth 1 header)
2414                                                               "body"))
2415                                                  ""
2416                                                (list 'gnus-summary-header
2417                                                      (nth 1 header)))
2418                                              (list 'quote (nth 1 (car ts)))
2419                                              (list 'gnus-score-delta-default
2420                                                    nil)
2421                                              (nth 1 (car ps))
2422                                              t)
2423                                             t)
2424                                            outp))
2425                                     (setq ps (cdr ps)))
2426                                   (list (nreverse outp))))
2427                                outt))
2428                         (setq ts (cdr ts)))
2429                       (list (nreverse outt))))
2430                    outh))
2431             (setq headers (cdr headers)))
2432           (list (nreverse outh))))))))
2433
2434 \f
2435
2436 (defun gnus-summary-mode (&optional group)
2437   "Major mode for reading articles.
2438
2439 All normal editing commands are switched off.
2440 \\<gnus-summary-mode-map>
2441 Each line in this buffer represents one article.  To read an
2442 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2443 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2444 respectively.
2445
2446 You can also post articles and send mail from this buffer.  To
2447 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2448 of an article, type `\\[gnus-summary-reply]'.
2449
2450 There are approx. one gazillion commands you can execute in this
2451 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2452
2453 The following commands are available:
2454
2455 \\{gnus-summary-mode-map}"
2456   (interactive)
2457   (kill-all-local-variables)
2458   (when (gnus-visual-p 'summary-menu 'menu)
2459     (gnus-summary-make-menu-bar)
2460     (gnus-summary-make-tool-bar))
2461   (gnus-summary-make-local-variables)
2462   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2463     (gnus-summary-make-local-variables))
2464   (gnus-make-thread-indent-array)
2465   (gnus-simplify-mode-line)
2466   (setq major-mode 'gnus-summary-mode)
2467   (setq mode-name "Summary")
2468   (make-local-variable 'minor-mode-alist)
2469   (use-local-map gnus-summary-mode-map)
2470   (buffer-disable-undo)
2471   (setq buffer-read-only t)             ;Disable modification
2472   (setq truncate-lines t)
2473   (setq selective-display t)
2474   (setq selective-display-ellipses t)   ;Display `...'
2475   (gnus-summary-set-display-table)
2476   (gnus-set-default-directory)
2477   (setq gnus-newsgroup-name group)
2478   (make-local-variable 'gnus-summary-line-format)
2479   (make-local-variable 'gnus-summary-line-format-spec)
2480   (make-local-variable 'gnus-summary-dummy-line-format)
2481   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2482   (make-local-variable 'gnus-summary-mark-positions)
2483   (make-local-hook 'pre-command-hook)
2484   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2485   (gnus-run-hooks 'gnus-summary-mode-hook)
2486   (turn-on-gnus-mailing-list-mode)
2487   (mm-enable-multibyte)
2488   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2489   (gnus-update-summary-mark-positions))
2490
2491 (defun gnus-summary-make-local-variables ()
2492   "Make all the local summary buffer variables."
2493   (let (global)
2494     (dolist (local gnus-summary-local-variables)
2495       (if (consp local)
2496           (progn
2497             (if (eq (cdr local) 'global)
2498                 ;; Copy the global value of the variable.
2499                 (setq global (symbol-value (car local)))
2500               ;; Use the value from the list.
2501               (setq global (eval (cdr local))))
2502             (set (make-local-variable (car local)) global))
2503         ;; Simple nil-valued local variable.
2504         (set (make-local-variable local) nil)))))
2505
2506 (defun gnus-summary-clear-local-variables ()
2507   (let ((locals gnus-summary-local-variables))
2508     (while locals
2509       (if (consp (car locals))
2510           (and (vectorp (caar locals))
2511                (set (caar locals) nil))
2512         (and (vectorp (car locals))
2513              (set (car locals) nil)))
2514       (setq locals (cdr locals)))))
2515
2516 ;; Summary data functions.
2517
2518 (defmacro gnus-data-number (data)
2519   `(car ,data))
2520
2521 (defmacro gnus-data-set-number (data number)
2522   `(setcar ,data ,number))
2523
2524 (defmacro gnus-data-mark (data)
2525   `(nth 1 ,data))
2526
2527 (defmacro gnus-data-set-mark (data mark)
2528   `(setcar (nthcdr 1 ,data) ,mark))
2529
2530 (defmacro gnus-data-pos (data)
2531   `(nth 2 ,data))
2532
2533 (defmacro gnus-data-set-pos (data pos)
2534   `(setcar (nthcdr 2 ,data) ,pos))
2535
2536 (defmacro gnus-data-header (data)
2537   `(nth 3 ,data))
2538
2539 (defmacro gnus-data-set-header (data header)
2540   `(setf (nth 3 ,data) ,header))
2541
2542 (defmacro gnus-data-level (data)
2543   `(nth 4 ,data))
2544
2545 (defmacro gnus-data-unread-p (data)
2546   `(= (nth 1 ,data) gnus-unread-mark))
2547
2548 (defmacro gnus-data-read-p (data)
2549   `(/= (nth 1 ,data) gnus-unread-mark))
2550
2551 (defmacro gnus-data-pseudo-p (data)
2552   `(consp (nth 3 ,data)))
2553
2554 (defmacro gnus-data-find (number)
2555   `(assq ,number gnus-newsgroup-data))
2556
2557 (defmacro gnus-data-find-list (number &optional data)
2558   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2559      (memq (assq ,number bdata)
2560            bdata)))
2561
2562 (defmacro gnus-data-make (number mark pos header level)
2563   `(list ,number ,mark ,pos ,header ,level))
2564
2565 (defun gnus-data-enter (after-article number mark pos header level offset)
2566   (let ((data (gnus-data-find-list after-article)))
2567     (unless data
2568       (error "No such article: %d" after-article))
2569     (setcdr data (cons (gnus-data-make number mark pos header level)
2570                        (cdr data)))
2571     (setq gnus-newsgroup-data-reverse nil)
2572     (gnus-data-update-list (cddr data) offset)))
2573
2574 (defun gnus-data-enter-list (after-article list &optional offset)
2575   (when list
2576     (let ((data (and after-article (gnus-data-find-list after-article)))
2577           (ilist list))
2578       (if (not (or data
2579                    after-article))
2580           (let ((odata gnus-newsgroup-data))
2581             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2582             (when offset
2583               (gnus-data-update-list odata offset)))
2584       ;; Find the last element in the list to be spliced into the main
2585         ;; list.
2586         (while (cdr list)
2587           (setq list (cdr list)))
2588         (if (not data)
2589             (progn
2590               (setcdr list gnus-newsgroup-data)
2591               (setq gnus-newsgroup-data ilist)
2592               (when offset
2593                 (gnus-data-update-list (cdr list) offset)))
2594           (setcdr list (cdr data))
2595           (setcdr data ilist)
2596           (when offset
2597             (gnus-data-update-list (cdr list) offset))))
2598       (setq gnus-newsgroup-data-reverse nil))))
2599
2600 (defun gnus-data-remove (article &optional offset)
2601   (let ((data gnus-newsgroup-data))
2602     (if (= (gnus-data-number (car data)) article)
2603         (progn
2604           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2605                 gnus-newsgroup-data-reverse nil)
2606           (when offset
2607             (gnus-data-update-list gnus-newsgroup-data offset)))
2608       (while (cdr data)
2609         (when (= (gnus-data-number (cadr data)) article)
2610           (setcdr data (cddr data))
2611           (when offset
2612             (gnus-data-update-list (cdr data) offset))
2613           (setq data nil
2614                 gnus-newsgroup-data-reverse nil))
2615         (setq data (cdr data))))))
2616
2617 (defmacro gnus-data-list (backward)
2618   `(if ,backward
2619        (or gnus-newsgroup-data-reverse
2620            (setq gnus-newsgroup-data-reverse
2621                  (reverse gnus-newsgroup-data)))
2622      gnus-newsgroup-data))
2623
2624 (defun gnus-data-update-list (data offset)
2625   "Add OFFSET to the POS of all data entries in DATA."
2626   (setq gnus-newsgroup-data-reverse nil)
2627   (while data
2628     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2629     (setq data (cdr data))))
2630
2631 (defun gnus-summary-article-pseudo-p (article)
2632   "Say whether this article is a pseudo article or not."
2633   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2634
2635 (defmacro gnus-summary-article-sparse-p (article)
2636   "Say whether this article is a sparse article or not."
2637   `(memq ,article gnus-newsgroup-sparse))
2638
2639 (defmacro gnus-summary-article-ancient-p (article)
2640   "Say whether this article is a sparse article or not."
2641   `(memq ,article gnus-newsgroup-ancient))
2642
2643 (defun gnus-article-parent-p (number)
2644   "Say whether this article is a parent or not."
2645   (let ((data (gnus-data-find-list number)))
2646     (and (cdr data)              ; There has to be an article after...
2647          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2648             (gnus-data-level (nth 1 data))))))
2649
2650 (defun gnus-article-children (number)
2651   "Return a list of all children to NUMBER."
2652   (let* ((data (gnus-data-find-list number))
2653          (level (gnus-data-level (car data)))
2654          children)
2655     (setq data (cdr data))
2656     (while (and data
2657                 (= (gnus-data-level (car data)) (1+ level)))
2658       (push (gnus-data-number (car data)) children)
2659       (setq data (cdr data)))
2660     children))
2661
2662 (defmacro gnus-summary-skip-intangible ()
2663   "If the current article is intangible, then jump to a different article."
2664   '(let ((to (get-text-property (point) 'gnus-intangible)))
2665      (and to (gnus-summary-goto-subject to))))
2666
2667 (defmacro gnus-summary-article-intangible-p ()
2668   "Say whether this article is intangible or not."
2669   '(get-text-property (point) 'gnus-intangible))
2670
2671 (defun gnus-article-read-p (article)
2672   "Say whether ARTICLE is read or not."
2673   (not (or (memq article gnus-newsgroup-marked)
2674            (memq article gnus-newsgroup-unreads)
2675            (memq article gnus-newsgroup-unselected)
2676            (memq article gnus-newsgroup-dormant))))
2677
2678 ;; Some summary mode macros.
2679
2680 (defmacro gnus-summary-article-number ()
2681   "The article number of the article on the current line.
2682 If there isn's an article number here, then we return the current
2683 article number."
2684   '(progn
2685      (gnus-summary-skip-intangible)
2686      (or (get-text-property (point) 'gnus-number)
2687          (gnus-summary-last-subject))))
2688
2689 (defmacro gnus-summary-article-header (&optional number)
2690   "Return the header of article NUMBER."
2691   `(gnus-data-header (gnus-data-find
2692                       ,(or number '(gnus-summary-article-number)))))
2693
2694 (defmacro gnus-summary-thread-level (&optional number)
2695   "Return the level of thread that starts with article NUMBER."
2696   `(if (and (eq gnus-summary-make-false-root 'dummy)
2697             (get-text-property (point) 'gnus-intangible))
2698        0
2699      (gnus-data-level (gnus-data-find
2700                        ,(or number '(gnus-summary-article-number))))))
2701
2702 (defmacro gnus-summary-article-mark (&optional number)
2703   "Return the mark of article NUMBER."
2704   `(gnus-data-mark (gnus-data-find
2705                     ,(or number '(gnus-summary-article-number)))))
2706
2707 (defmacro gnus-summary-article-pos (&optional number)
2708   "Return the position of the line of article NUMBER."
2709   `(gnus-data-pos (gnus-data-find
2710                    ,(or number '(gnus-summary-article-number)))))
2711
2712 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2713 (defmacro gnus-summary-article-subject (&optional number)
2714   "Return current subject string or nil if nothing."
2715   `(let ((headers
2716           ,(if number
2717                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2718              '(gnus-data-header (assq (gnus-summary-article-number)
2719                                       gnus-newsgroup-data)))))
2720      (and headers
2721           (vectorp headers)
2722           (mail-header-subject headers))))
2723
2724 (defmacro gnus-summary-article-score (&optional number)
2725   "Return current article score."
2726   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2727                   gnus-newsgroup-scored))
2728        gnus-summary-default-score 0))
2729
2730 (defun gnus-summary-article-children (&optional number)
2731   "Return a list of article numbers that are children of article NUMBER."
2732   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2733          (level (gnus-data-level (car data)))
2734          l children)
2735     (while (and (setq data (cdr data))
2736                 (> (setq l (gnus-data-level (car data))) level))
2737       (and (= (1+ level) l)
2738            (push (gnus-data-number (car data))
2739                  children)))
2740     (nreverse children)))
2741
2742 (defun gnus-summary-article-parent (&optional number)
2743   "Return the article number of the parent of article NUMBER."
2744   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2745                                     (gnus-data-list t)))
2746          (level (gnus-data-level (car data))))
2747     (if (zerop level)
2748         ()                              ; This is a root.
2749       ;; We search until we find an article with a level less than
2750       ;; this one.  That function has to be the parent.
2751       (while (and (setq data (cdr data))
2752                   (not (< (gnus-data-level (car data)) level))))
2753       (and data (gnus-data-number (car data))))))
2754
2755 (defun gnus-unread-mark-p (mark)
2756   "Say whether MARK is the unread mark."
2757   (= mark gnus-unread-mark))
2758
2759 (defun gnus-read-mark-p (mark)
2760   "Say whether MARK is one of the marks that mark as read.
2761 This is all marks except unread, ticked, dormant, and expirable."
2762   (not (or (= mark gnus-unread-mark)
2763            (= mark gnus-ticked-mark)
2764            (= mark gnus-dormant-mark)
2765            (= mark gnus-expirable-mark))))
2766
2767 (defmacro gnus-article-mark (number)
2768   "Return the MARK of article NUMBER.
2769 This macro should only be used when computing the mark the \"first\"
2770 time; i.e., when generating the summary lines.  After that,
2771 `gnus-summary-article-mark' should be used to examine the
2772 marks of articles."
2773   `(cond
2774     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2775     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2776     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2777     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2778     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2779     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2780     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2781     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2782            gnus-ancient-mark))))
2783
2784 ;; Saving hidden threads.
2785
2786 (defmacro gnus-save-hidden-threads (&rest forms)
2787   "Save hidden threads, eval FORMS, and restore the hidden threads."
2788   (let ((config (make-symbol "config")))
2789     `(let ((,config (gnus-hidden-threads-configuration)))
2790        (unwind-protect
2791            (save-excursion
2792              ,@forms)
2793          (gnus-restore-hidden-threads-configuration ,config)))))
2794 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2795 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2796
2797 (defun gnus-data-compute-positions ()
2798   "Compute the positions of all articles."
2799   (setq gnus-newsgroup-data-reverse nil)
2800   (let ((data gnus-newsgroup-data))
2801     (save-excursion
2802       (gnus-save-hidden-threads
2803         (gnus-summary-show-all-threads)
2804         (goto-char (point-min))
2805         (while data
2806           (while (get-text-property (point) 'gnus-intangible)
2807             (forward-line 1))
2808           (gnus-data-set-pos (car data) (+ (point) 3))
2809           (setq data (cdr data))
2810           (forward-line 1))))))
2811
2812 (defun gnus-hidden-threads-configuration ()
2813   "Return the current hidden threads configuration."
2814   (save-excursion
2815     (let (config)
2816       (goto-char (point-min))
2817       (while (search-forward "\r" nil t)
2818         (push (1- (point)) config))
2819       config)))
2820
2821 (defun gnus-restore-hidden-threads-configuration (config)
2822   "Restore hidden threads configuration from CONFIG."
2823   (save-excursion
2824     (let (point buffer-read-only)
2825       (while (setq point (pop config))
2826         (when (and (< point (point-max))
2827                    (goto-char point)
2828                    (eq (char-after) ?\n))
2829           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2830
2831 ;; Various summary mode internalish functions.
2832
2833 (defun gnus-mouse-pick-article (e)
2834   (interactive "e")
2835   (mouse-set-point e)
2836   (gnus-summary-next-page nil t))
2837
2838 (defun gnus-summary-set-display-table ()
2839   "Change the display table.
2840 Odd characters have a tendency to mess
2841 up nicely formatted displays - we make all possible glyphs
2842 display only a single character."
2843
2844   ;; We start from the standard display table, if any.
2845   (let ((table (or (copy-sequence standard-display-table)
2846                    (make-display-table)))
2847         (i 32))
2848     ;; Nix out all the control chars...
2849     (while (>= (setq i (1- i)) 0)
2850       (aset table i [??]))
2851    ;; ... but not newline and cr, of course.  (cr is necessary for the
2852     ;; selective display).
2853     (aset table ?\n nil)
2854     (aset table ?\r nil)
2855     ;; We keep TAB as well.
2856     (aset table ?\t nil)
2857     ;; We nix out any glyphs over 126 that are not set already.
2858     (let ((i 256))
2859       (while (>= (setq i (1- i)) 127)
2860         ;; Only modify if the entry is nil.
2861         (unless (aref table i)
2862           (aset table i [??]))))
2863     (setq buffer-display-table table)))
2864
2865 (defun gnus-summary-set-article-display-arrow (pos)
2866   "Update the overlay arrow to point to line at position POS."
2867   (when (and gnus-summary-display-arrow
2868              (boundp 'overlay-arrow-position)
2869              (boundp 'overlay-arrow-string))
2870     (save-excursion
2871       (goto-char pos)
2872       (beginning-of-line)
2873       (unless overlay-arrow-position
2874         (setq overlay-arrow-position (make-marker)))
2875       (setq overlay-arrow-string "=>"
2876             overlay-arrow-position (set-marker overlay-arrow-position
2877                                                (point)
2878                                                (current-buffer))))))
2879
2880 (defun gnus-summary-buffer-name (group)
2881   "Return the summary buffer name of GROUP."
2882   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2883
2884 (defun gnus-summary-setup-buffer (group)
2885   "Initialize summary buffer."
2886   (let ((buffer (gnus-summary-buffer-name group))
2887         (dead-name (concat "*Dead Summary "
2888                            (gnus-group-decoded-name group) "*")))
2889     ;; If a dead summary buffer exists, we kill it.
2890     (when (gnus-buffer-live-p dead-name)
2891       (gnus-kill-buffer dead-name))
2892     (if (get-buffer buffer)
2893         (progn
2894           (set-buffer buffer)
2895           (setq gnus-summary-buffer (current-buffer))
2896           (not gnus-newsgroup-prepared))
2897       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2898       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2899       (gnus-summary-mode group)
2900       (when gnus-carpal
2901         (gnus-carpal-setup-buffer 'summary))
2902       (unless gnus-single-article-buffer
2903         (make-local-variable 'gnus-article-buffer)
2904         (make-local-variable 'gnus-article-current)
2905         (make-local-variable 'gnus-original-article-buffer))
2906       (setq gnus-newsgroup-name group)
2907       ;; Set any local variables in the group parameters.
2908       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2909       t)))
2910
2911 (defun gnus-set-global-variables ()
2912   "Set the global equivalents of the buffer-local variables.
2913 They are set to the latest values they had.  These reflect the summary
2914 buffer that was in action when the last article was fetched."
2915   (when (eq major-mode 'gnus-summary-mode)
2916     (setq gnus-summary-buffer (current-buffer))
2917     (let ((name gnus-newsgroup-name)
2918           (marked gnus-newsgroup-marked)
2919           (unread gnus-newsgroup-unreads)
2920           (headers gnus-current-headers)
2921           (data gnus-newsgroup-data)
2922           (summary gnus-summary-buffer)
2923           (article-buffer gnus-article-buffer)
2924           (original gnus-original-article-buffer)
2925           (gac gnus-article-current)
2926           (reffed gnus-reffed-article-number)
2927           (score-file gnus-current-score-file)
2928           (default-charset gnus-newsgroup-charset)
2929           vlist)
2930       (let ((locals gnus-newsgroup-variables))
2931         (while locals
2932           (if (consp (car locals))
2933               (push (eval (caar locals)) vlist)
2934             (push (eval (car locals)) vlist))
2935           (setq locals (cdr locals)))
2936         (setq vlist (nreverse vlist)))
2937       (save-excursion
2938         (set-buffer gnus-group-buffer)
2939         (setq gnus-newsgroup-name name
2940               gnus-newsgroup-marked marked
2941               gnus-newsgroup-unreads unread
2942               gnus-current-headers headers
2943               gnus-newsgroup-data data
2944               gnus-article-current gac
2945               gnus-summary-buffer summary
2946               gnus-article-buffer article-buffer
2947               gnus-original-article-buffer original
2948               gnus-reffed-article-number reffed
2949               gnus-current-score-file score-file
2950               gnus-newsgroup-charset default-charset)
2951         (let ((locals gnus-newsgroup-variables))
2952           (while locals
2953             (if (consp (car locals))
2954                 (set (caar locals) (pop vlist))
2955               (set (car locals) (pop vlist)))
2956             (setq locals (cdr locals))))
2957         ;; The article buffer also has local variables.
2958         (when (gnus-buffer-live-p gnus-article-buffer)
2959           (set-buffer gnus-article-buffer)
2960           (setq gnus-summary-buffer summary))))))
2961
2962 (defun gnus-summary-article-unread-p (article)
2963   "Say whether ARTICLE is unread or not."
2964   (memq article gnus-newsgroup-unreads))
2965
2966 (defun gnus-summary-first-article-p (&optional article)
2967   "Return whether ARTICLE is the first article in the buffer."
2968   (if (not (setq article (or article (gnus-summary-article-number))))
2969       nil
2970     (eq article (caar gnus-newsgroup-data))))
2971
2972 (defun gnus-summary-last-article-p (&optional article)
2973   "Return whether ARTICLE is the last article in the buffer."
2974   (if (not (setq article (or article (gnus-summary-article-number))))
2975       ;; All non-existent numbers are the last article.  :-)
2976       t
2977     (not (cdr (gnus-data-find-list article)))))
2978
2979 (defun gnus-make-thread-indent-array ()
2980   (let ((n 200))
2981     (unless (and gnus-thread-indent-array
2982                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2983       (setq gnus-thread-indent-array (make-vector 201 "")
2984             gnus-thread-indent-array-level gnus-thread-indent-level)
2985       (while (>= n 0)
2986         (aset gnus-thread-indent-array n
2987               (make-string (* n gnus-thread-indent-level) ? ))
2988         (setq n (1- n))))))
2989
2990 (defun gnus-update-summary-mark-positions ()
2991   "Compute where the summary marks are to go."
2992   (save-excursion
2993     (when (gnus-buffer-exists-p gnus-summary-buffer)
2994       (set-buffer gnus-summary-buffer))
2995     (let ((gnus-replied-mark 129)
2996           (gnus-score-below-mark 130)
2997           (gnus-score-over-mark 130)
2998           (gnus-download-mark 131)
2999           (spec gnus-summary-line-format-spec)
3000           gnus-visual pos)
3001       (save-excursion
3002         (gnus-set-work-buffer)
3003         (let ((gnus-summary-line-format-spec spec)
3004               (gnus-newsgroup-downloadable '((0 . t))))
3005           (gnus-summary-insert-line
3006            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3007            0 nil 128 t nil "" nil 1)
3008           (goto-char (point-min))
3009           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3010                                              (- (point) 2)))))
3011           (goto-char (point-min))
3012           (push (cons 'replied (and (search-forward "\201" nil t)
3013                                     (- (point) 2)))
3014                 pos)
3015           (goto-char (point-min))
3016           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
3017                 pos)
3018           (goto-char (point-min))
3019           (push (cons 'download
3020                       (and (search-forward "\203" nil t) (- (point) 2)))
3021                 pos)))
3022       (setq gnus-summary-mark-positions pos))))
3023
3024 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3025   "Insert a dummy root in the summary buffer."
3026   (beginning-of-line)
3027   (gnus-add-text-properties
3028    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3029    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3030
3031 (defun gnus-summary-extract-address-component (from)
3032   (or (car (funcall gnus-extract-address-components from))
3033       from))
3034
3035 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3036   (let ((mail-parse-charset gnus-newsgroup-charset)
3037         ; Is it really necessary to do this next part for each summary line?
3038         ; Luckily, doesn't seem to slow things down much.
3039         (mail-parse-ignored-charsets
3040          (save-excursion (set-buffer gnus-summary-buffer)
3041                          gnus-newsgroup-ignored-charsets)))
3042     (or
3043      (and gnus-ignored-from-addresses
3044           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3045           (let ((extra-headers (mail-header-extra header))
3046                 to
3047                 newsgroups)
3048             (cond
3049              ((setq to (cdr (assq 'To extra-headers)))
3050               (concat "-> "
3051                       (inline
3052                         (gnus-summary-extract-address-component
3053                          (funcall gnus-decode-encoded-word-function to)))))
3054              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3055               (concat "=> " newsgroups)))))
3056      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3057
3058 (defun gnus-summary-insert-line (gnus-tmp-header
3059                                  gnus-tmp-level gnus-tmp-current
3060                                  gnus-tmp-unread gnus-tmp-replied
3061                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3062                                  &optional gnus-tmp-dummy gnus-tmp-score
3063                                  gnus-tmp-process)
3064   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3065          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3066          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3067          (gnus-tmp-score-char
3068           (if (or (null gnus-summary-default-score)
3069                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3070                       gnus-summary-zcore-fuzz))
3071               ?                         ;Whitespace
3072             (if (< gnus-tmp-score gnus-summary-default-score)
3073                 gnus-score-below-mark gnus-score-over-mark)))
3074          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3075          (gnus-tmp-replied
3076           (cond (gnus-tmp-process gnus-process-mark)
3077                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3078                  gnus-cached-mark)
3079                 (gnus-tmp-replied gnus-replied-mark)
3080                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3081                  gnus-forwarded-mark)
3082                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3083                  gnus-saved-mark)
3084                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3085                  gnus-recent-mark)
3086                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3087                  gnus-unseen-mark)
3088                 (t gnus-no-mark)))
3089          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3090          (gnus-tmp-name
3091           (cond
3092            ((string-match "<[^>]+> *$" gnus-tmp-from)
3093             (let ((beg (match-beginning 0)))
3094               (or (and (string-match "^\".+\"" gnus-tmp-from)
3095                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3096                   (substring gnus-tmp-from 0 beg))))
3097            ((string-match "(.+)" gnus-tmp-from)
3098             (substring gnus-tmp-from
3099                        (1+ (match-beginning 0)) (1- (match-end 0))))
3100            (t gnus-tmp-from)))
3101          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3102          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3103          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3104          (buffer-read-only nil))
3105     (when (string= gnus-tmp-name "")
3106       (setq gnus-tmp-name gnus-tmp-from))
3107     (unless (numberp gnus-tmp-lines)
3108       (setq gnus-tmp-lines -1))
3109     (if (= gnus-tmp-lines -1)
3110         (setq gnus-tmp-lines "?")
3111       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3112     (gnus-put-text-property
3113      (point)
3114      (progn (eval gnus-summary-line-format-spec) (point))
3115      'gnus-number gnus-tmp-number)
3116     (when (gnus-visual-p 'summary-highlight 'highlight)
3117       (forward-line -1)
3118       (gnus-run-hooks 'gnus-summary-update-hook)
3119       (forward-line 1))))
3120
3121 (defun gnus-summary-update-line (&optional dont-update)
3122   "Update summary line after change."
3123   (when (and gnus-summary-default-score
3124              (not gnus-summary-inhibit-highlight))
3125     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3126            (article (gnus-summary-article-number))
3127            (score (gnus-summary-article-score article)))
3128       (unless dont-update
3129         (if (and gnus-summary-mark-below
3130                  (< (gnus-summary-article-score)
3131                     gnus-summary-mark-below))
3132             ;; This article has a low score, so we mark it as read.
3133             (when (memq article gnus-newsgroup-unreads)
3134               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3135           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3136             ;; This article was previously marked as read on account
3137             ;; of a low score, but now it has risen, so we mark it as
3138             ;; unread.
3139             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3140         (gnus-summary-update-mark
3141          (if (or (null gnus-summary-default-score)
3142                  (<= (abs (- score gnus-summary-default-score))
3143                      gnus-summary-zcore-fuzz))
3144              ?                          ;Whitespace
3145            (if (< score gnus-summary-default-score)
3146                gnus-score-below-mark gnus-score-over-mark))
3147          'score))
3148       ;; Do visual highlighting.
3149       (when (gnus-visual-p 'summary-highlight 'highlight)
3150         (gnus-run-hooks 'gnus-summary-update-hook)))))
3151
3152 (defvar gnus-tmp-new-adopts nil)
3153
3154 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3155   "Return the number of articles in THREAD.
3156 This may be 0 in some cases -- if none of the articles in
3157 the thread are to be displayed."
3158   (let* ((number
3159          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3160           (cond
3161            ((not (listp thread))
3162             1)
3163            ((and (consp thread) (cdr thread))
3164             (apply
3165              '+ 1 (mapcar
3166                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3167            ((null thread)
3168             1)
3169            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3170             1)
3171            (t 0))))
3172     (when (and level (zerop level) gnus-tmp-new-adopts)
3173       (incf number
3174             (apply '+ (mapcar
3175                        'gnus-summary-number-of-articles-in-thread
3176                        gnus-tmp-new-adopts))))
3177     (if char
3178         (if (> number 1) gnus-not-empty-thread-mark
3179           gnus-empty-thread-mark)
3180       number)))
3181
3182 (defun gnus-summary-set-local-parameters (group)
3183   "Go through the local params of GROUP and set all variable specs in that list."
3184   (let ((params (gnus-group-find-parameter group))
3185         (vars '(quit-config))           ; Ignore quit-config.
3186         elem)
3187     (while params
3188       (setq elem (car params)
3189             params (cdr params))
3190       (and (consp elem)                 ; Has to be a cons.
3191            (consp (cdr elem))           ; The cdr has to be a list.
3192            (symbolp (car elem))         ; Has to be a symbol in there.
3193            (not (memq (car elem) vars))
3194            (ignore-errors               ; So we set it.
3195              (push (car elem) vars)
3196              (make-local-variable (car elem))
3197              (set (car elem) (eval (nth 1 elem))))))))
3198
3199 (defun gnus-summary-read-group (group &optional show-all no-article
3200                                       kill-buffer no-display backward
3201                                       select-articles)
3202   "Start reading news in newsgroup GROUP.
3203 If SHOW-ALL is non-nil, already read articles are also listed.
3204 If NO-ARTICLE is non-nil, no article is selected initially.
3205 If NO-DISPLAY, don't generate a summary buffer."
3206   (let (result)
3207     (while (and group
3208                 (null (setq result
3209                             (let ((gnus-auto-select-next nil))
3210                               (or (gnus-summary-read-group-1
3211                                    group show-all no-article
3212                                    kill-buffer no-display
3213                                    select-articles)
3214                                   (setq show-all nil
3215                                         select-articles nil)))))
3216                 (eq gnus-auto-select-next 'quietly))
3217       (set-buffer gnus-group-buffer)
3218       ;; The entry function called above goes to the next
3219       ;; group automatically, so we go two groups back
3220       ;; if we are searching for the previous group.
3221       (when backward
3222         (gnus-group-prev-unread-group 2))
3223       (if (not (equal group (gnus-group-group-name)))
3224           (setq group (gnus-group-group-name))
3225         (setq group nil)))
3226     result))
3227
3228 (defun gnus-summary-read-group-1 (group show-all no-article
3229                                         kill-buffer no-display
3230                                         &optional select-articles)
3231   ;; Killed foreign groups can't be entered.
3232   ;;  (when (and (not (gnus-group-native-p group))
3233   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3234   ;;    (error "Dead non-native groups can't be entered"))
3235   (gnus-message 5 "Retrieving newsgroup: %s..."
3236                 (gnus-group-decoded-name group))
3237   (let* ((new-group (gnus-summary-setup-buffer group))
3238          (quit-config (gnus-group-quit-config group))
3239          (did-select (and new-group (gnus-select-newsgroup
3240                                      group show-all select-articles))))
3241     (cond
3242      ;; This summary buffer exists already, so we just select it.
3243      ((not new-group)
3244       (gnus-set-global-variables)
3245       (when kill-buffer
3246         (gnus-kill-or-deaden-summary kill-buffer))
3247       (gnus-configure-windows 'summary 'force)
3248       (gnus-set-mode-line 'summary)
3249       (gnus-summary-position-point)
3250       (message "")
3251       t)
3252      ;; We couldn't select this group.
3253      ((null did-select)
3254       (when (and (eq major-mode 'gnus-summary-mode)
3255                  (not (equal (current-buffer) kill-buffer)))
3256         (kill-buffer (current-buffer))
3257         (if (not quit-config)
3258             (progn
3259               ;; Update the info -- marks might need to be removed,
3260               ;; for instance.
3261               (gnus-summary-update-info)
3262               (set-buffer gnus-group-buffer)
3263               (gnus-group-jump-to-group group)
3264               (gnus-group-next-unread-group 1))
3265           (gnus-handle-ephemeral-exit quit-config)))
3266       (let ((grpinfo (gnus-get-info group)))
3267         (if (null (gnus-info-read grpinfo))
3268             (gnus-message 3 "Group %s contains no messages"
3269                           (gnus-group-decoded-name group))
3270           (gnus-message 3 "Can't select group")))
3271       nil)
3272      ;; The user did a `C-g' while prompting for number of articles,
3273      ;; so we exit this group.
3274      ((eq did-select 'quit)
3275       (and (eq major-mode 'gnus-summary-mode)
3276            (not (equal (current-buffer) kill-buffer))
3277            (kill-buffer (current-buffer)))
3278       (when kill-buffer
3279         (gnus-kill-or-deaden-summary kill-buffer))
3280       (if (not quit-config)
3281           (progn
3282             (set-buffer gnus-group-buffer)
3283             (gnus-group-jump-to-group group)
3284             (gnus-group-next-unread-group 1)
3285             (gnus-configure-windows 'group 'force))
3286         (gnus-handle-ephemeral-exit quit-config))
3287       ;; Finally signal the quit.
3288       (signal 'quit nil))
3289      ;; The group was successfully selected.
3290      (t
3291       (gnus-set-global-variables)
3292       ;; Save the active value in effect when the group was entered.
3293       (setq gnus-newsgroup-active
3294             (gnus-copy-sequence
3295              (gnus-active gnus-newsgroup-name)))
3296       ;; You can change the summary buffer in some way with this hook.
3297       (gnus-run-hooks 'gnus-select-group-hook)
3298       (gnus-update-format-specifications
3299        nil 'summary 'summary-mode 'summary-dummy)
3300       (gnus-update-summary-mark-positions)
3301       ;; Do score processing.
3302       (when gnus-use-scoring
3303         (gnus-possibly-score-headers))
3304       ;; Check whether to fill in the gaps in the threads.
3305       (when gnus-build-sparse-threads
3306         (gnus-build-sparse-threads))
3307       ;; Find the initial limit.
3308       (if gnus-show-threads
3309           (if show-all
3310               (let ((gnus-newsgroup-dormant nil))
3311                 (gnus-summary-initial-limit show-all))
3312             (gnus-summary-initial-limit show-all))
3313         ;; When untreaded, all articles are always shown.
3314         (setq gnus-newsgroup-limit
3315               (mapcar
3316                (lambda (header) (mail-header-number header))
3317                gnus-newsgroup-headers)))
3318       ;; Generate the summary buffer.
3319       (unless no-display
3320         (gnus-summary-prepare))
3321       (when gnus-use-trees
3322         (gnus-tree-open group)
3323         (setq gnus-summary-highlight-line-function
3324               'gnus-tree-highlight-article))
3325       ;; If the summary buffer is empty, but there are some low-scored
3326       ;; articles or some excluded dormants, we include these in the
3327       ;; buffer.
3328       (when (and (zerop (buffer-size))
3329                  (not no-display))
3330         (cond (gnus-newsgroup-dormant
3331                (gnus-summary-limit-include-dormant))
3332               ((and gnus-newsgroup-scored show-all)
3333                (gnus-summary-limit-include-expunged t))))
3334       ;; Function `gnus-apply-kill-file' must be called in this hook.
3335       (gnus-run-hooks 'gnus-apply-kill-hook)
3336       (if (and (zerop (buffer-size))
3337                (not no-display))
3338           (progn
3339             ;; This newsgroup is empty.
3340             (gnus-summary-catchup-and-exit nil t)
3341             (gnus-message 6 "No unread news")
3342             (when kill-buffer
3343               (gnus-kill-or-deaden-summary kill-buffer))
3344             ;; Return nil from this function.
3345             nil)
3346         ;; Hide conversation thread subtrees.  We cannot do this in
3347         ;; gnus-summary-prepare-hook since kill processing may not
3348         ;; work with hidden articles.
3349         (gnus-summary-maybe-hide-threads)
3350         (when kill-buffer
3351           (gnus-kill-or-deaden-summary kill-buffer))
3352         (gnus-summary-auto-select-subject)
3353         ;; Show first unread article if requested.
3354         (if (and (not no-article)
3355                  (not no-display)
3356                  gnus-newsgroup-unreads
3357                  gnus-auto-select-first)
3358             (progn
3359               (gnus-configure-windows 'summary)
3360               (let ((art (gnus-summary-article-number)))
3361                 (unless (or (memq art gnus-newsgroup-undownloaded)
3362                             (memq art gnus-newsgroup-downloadable))
3363                   (gnus-summary-goto-article art))))
3364           ;; Don't select any articles.
3365           (gnus-summary-position-point)
3366           (gnus-configure-windows 'summary 'force)
3367           (gnus-set-mode-line 'summary))
3368         (when (get-buffer-window gnus-group-buffer t)
3369           ;; Gotta use windows, because recenter does weird stuff if
3370           ;; the current buffer ain't the displayed window.
3371           (let ((owin (selected-window)))
3372             (select-window (get-buffer-window gnus-group-buffer t))
3373             (when (gnus-group-goto-group group)
3374               (recenter))
3375             (select-window owin)))
3376         ;; Mark this buffer as "prepared".
3377         (setq gnus-newsgroup-prepared t)
3378         (gnus-run-hooks 'gnus-summary-prepared-hook)
3379         t)))))
3380
3381 (defun gnus-summary-auto-select-subject ()
3382   "Select the subject line on initial group entry."
3383   (goto-char (point-min))
3384   (cond
3385    ((eq gnus-auto-select-subject 'best)
3386     (gnus-summary-best-unread-subject))
3387    ((eq gnus-auto-select-subject 'unread)
3388     (gnus-summary-first-unread-subject))
3389    ((eq gnus-auto-select-subject 'unseen)
3390     (gnus-summary-first-unseen-subject))
3391    ((eq gnus-auto-select-subject 'unseen-or-unread)
3392     (gnus-summary-first-unseen-or-unread-subject))
3393    ((eq gnus-auto-select-subject 'first)
3394     ;; Do nothing.
3395     )
3396    ((gnus-functionp gnus-auto-select-subject)
3397     (funcall gnus-auto-select-subject))))
3398
3399 (defun gnus-summary-prepare ()
3400   "Generate the summary buffer."
3401   (interactive)
3402   (let ((buffer-read-only nil))
3403     (erase-buffer)
3404     (setq gnus-newsgroup-data nil
3405           gnus-newsgroup-data-reverse nil)
3406     (gnus-run-hooks 'gnus-summary-generate-hook)
3407     ;; Generate the buffer, either with threads or without.
3408     (when gnus-newsgroup-headers
3409       (gnus-summary-prepare-threads
3410        (if gnus-show-threads
3411            (gnus-sort-gathered-threads
3412             (funcall gnus-summary-thread-gathering-function
3413                      (gnus-sort-threads
3414                       (gnus-cut-threads (gnus-make-threads)))))
3415          ;; Unthreaded display.
3416          (gnus-sort-articles gnus-newsgroup-headers))))
3417     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3418     ;; Call hooks for modifying summary buffer.
3419     (goto-char (point-min))
3420     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3421
3422 (defsubst gnus-general-simplify-subject (subject)
3423   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3424   (setq subject
3425         (cond
3426          ;; Truncate the subject.
3427          (gnus-simplify-subject-functions
3428           (gnus-map-function gnus-simplify-subject-functions subject))
3429          ((numberp gnus-summary-gather-subject-limit)
3430           (setq subject (gnus-simplify-subject-re subject))
3431           (if (> (length subject) gnus-summary-gather-subject-limit)
3432               (substring subject 0 gnus-summary-gather-subject-limit)
3433             subject))
3434          ;; Fuzzily simplify it.
3435          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3436           (gnus-simplify-subject-fuzzy subject))
3437          ;; Just remove the leading "Re:".
3438          (t
3439           (gnus-simplify-subject-re subject))))
3440
3441   (if (and gnus-summary-gather-exclude-subject
3442            (string-match gnus-summary-gather-exclude-subject subject))
3443       nil                         ; This article shouldn't be gathered
3444     subject))
3445
3446 (defun gnus-summary-simplify-subject-query ()
3447   "Query where the respool algorithm would put this article."
3448   (interactive)
3449   (gnus-summary-select-article)
3450   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3451
3452 (defun gnus-gather-threads-by-subject (threads)
3453   "Gather threads by looking at Subject headers."
3454   (if (not gnus-summary-make-false-root)
3455       threads
3456     (let ((hashtb (gnus-make-hashtable 1024))
3457           (prev threads)
3458           (result threads)
3459           subject hthread whole-subject)
3460       (while threads
3461         (setq subject (gnus-general-simplify-subject
3462                        (setq whole-subject (mail-header-subject
3463                                             (caar threads)))))
3464         (when subject
3465           (if (setq hthread (gnus-gethash subject hashtb))
3466               (progn
3467                 ;; We enter a dummy root into the thread, if we
3468                 ;; haven't done that already.
3469                 (unless (stringp (caar hthread))
3470                   (setcar hthread (list whole-subject (car hthread))))
3471                 ;; We add this new gathered thread to this gathered
3472                 ;; thread.
3473                 (setcdr (car hthread)
3474                         (nconc (cdar hthread) (list (car threads))))
3475                 ;; Remove it from the list of threads.
3476                 (setcdr prev (cdr threads))
3477                 (setq threads prev))
3478             ;; Enter this thread into the hash table.
3479             (gnus-sethash subject threads hashtb)))
3480         (setq prev threads)
3481         (setq threads (cdr threads)))
3482       result)))
3483
3484 (defun gnus-gather-threads-by-references (threads)
3485   "Gather threads by looking at References headers."
3486   (let ((idhashtb (gnus-make-hashtable 1024))
3487         (thhashtb (gnus-make-hashtable 1024))
3488         (prev threads)
3489         (result threads)
3490         ids references id gthread gid entered ref)
3491     (while threads
3492       (when (setq references (mail-header-references (caar threads)))
3493         (setq id (mail-header-id (caar threads))
3494               ids (inline (gnus-split-references references))
3495               entered nil)
3496         (while (setq ref (pop ids))
3497           (setq ids (delete ref ids))
3498           (if (not (setq gid (gnus-gethash ref idhashtb)))
3499               (progn
3500                 (gnus-sethash ref id idhashtb)
3501                 (gnus-sethash id threads thhashtb))
3502             (setq gthread (gnus-gethash gid thhashtb))
3503             (unless entered
3504               ;; We enter a dummy root into the thread, if we
3505               ;; haven't done that already.
3506               (unless (stringp (caar gthread))
3507                 (setcar gthread (list (mail-header-subject (caar gthread))
3508                                       (car gthread))))
3509               ;; We add this new gathered thread to this gathered
3510               ;; thread.
3511               (setcdr (car gthread)
3512                       (nconc (cdar gthread) (list (car threads)))))
3513             ;; Add it into the thread hash table.
3514             (gnus-sethash id gthread thhashtb)
3515             (setq entered t)
3516             ;; Remove it from the list of threads.
3517             (setcdr prev (cdr threads))
3518             (setq threads prev))))
3519       (setq prev threads)
3520       (setq threads (cdr threads)))
3521     result))
3522
3523 (defun gnus-sort-gathered-threads (threads)
3524   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3525   (let ((result threads))
3526     (while threads
3527       (when (stringp (caar threads))
3528         (setcdr (car threads)
3529                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3530       (setq threads (cdr threads)))
3531     result))
3532
3533 (defun gnus-thread-loop-p (root thread)
3534   "Say whether ROOT is in THREAD."
3535   (let ((stack (list thread))
3536         (infloop 0)
3537         th)
3538     (while (setq thread (pop stack))
3539       (setq th (cdr thread))
3540       (while (and th
3541                   (not (eq (caar th) root)))
3542         (pop th))
3543       (if th
3544           ;; We have found a loop.
3545           (let (ref-dep)
3546             (setcdr thread (delq (car th) (cdr thread)))
3547             (if (boundp (setq ref-dep (intern "none"
3548                                               gnus-newsgroup-dependencies)))
3549                 (setcdr (symbol-value ref-dep)
3550                         (nconc (cdr (symbol-value ref-dep))
3551                                (list (car th))))
3552               (set ref-dep (list nil (car th))))
3553             (setq infloop 1
3554                   stack nil))
3555         ;; Push all the subthreads onto the stack.
3556         (push (cdr thread) stack)))
3557     infloop))
3558
3559 (defun gnus-make-threads ()
3560   "Go through the dependency hashtb and find the roots.  Return all threads."
3561   (let (threads)
3562     (while (catch 'infloop
3563              (mapatoms
3564               (lambda (refs)
3565                 ;; Deal with self-referencing References loops.
3566                 (when (and (car (symbol-value refs))
3567                            (not (zerop
3568                                  (apply
3569                                   '+
3570                                   (mapcar
3571                                    (lambda (thread)
3572                                      (gnus-thread-loop-p
3573                                       (car (symbol-value refs)) thread))
3574                                    (cdr (symbol-value refs)))))))
3575                   (setq threads nil)
3576                   (throw 'infloop t))
3577                 (unless (car (symbol-value refs))
3578                   ;; These threads do not refer back to any other
3579                   ;; articles, so they're roots.
3580                   (setq threads (append (cdr (symbol-value refs)) threads))))
3581               gnus-newsgroup-dependencies)))
3582     threads))
3583
3584 ;; Build the thread tree.
3585 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3586   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3587
3588 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3589 if it was already present.
3590
3591 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3592 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3593 Message-IDs will be renamed to a unique Message-ID before being
3594 entered.
3595
3596 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3597   (let* ((id (mail-header-id header))
3598          (id-dep (and id (intern id dependencies)))
3599          ref ref-dep ref-header replaced)
3600     ;; Enter this `header' in the `dependencies' table.
3601     (cond
3602      ((not id-dep)
3603       (setq header nil))
3604      ;; The first two cases do the normal part: enter a new `header'
3605      ;; in the `dependencies' table.
3606      ((not (boundp id-dep))
3607       (set id-dep (list header)))
3608      ((null (car (symbol-value id-dep)))
3609       (setcar (symbol-value id-dep) header))
3610
3611      ;; From here the `header' was already present in the
3612      ;; `dependencies' table.
3613      (force-new
3614       ;; Overrides an existing entry;
3615       ;; just set the header part of the entry.
3616       (setcar (symbol-value id-dep) header)
3617       (setq replaced t))
3618
3619      ;; Renames the existing `header' to a unique Message-ID.
3620      ((not gnus-summary-ignore-duplicates)
3621       ;; An article with this Message-ID has already been seen.
3622       ;; We rename the Message-ID.
3623       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3624            (list header))
3625       (mail-header-set-id header id))
3626
3627      ;; The last case ignores an existing entry, except it adds any
3628      ;; additional Xrefs (in case the two articles came from different
3629      ;; servers.
3630      ;; Also sets `header' to `nil' meaning that the `dependencies'
3631      ;; table was *not* modified.
3632      (t
3633       (mail-header-set-xref
3634        (car (symbol-value id-dep))
3635        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3636                    "")
3637                (or (mail-header-xref header) "")))
3638       (setq header nil)))
3639
3640     (when (and header (not replaced))
3641       ;; First check that we are not creating a References loop.
3642       (setq ref (gnus-parent-id (mail-header-references header)))
3643       (while (and ref
3644                   (setq ref-dep (intern-soft ref dependencies))
3645                   (boundp ref-dep)
3646                   (setq ref-header (car (symbol-value ref-dep))))
3647         (if (string= id ref)
3648             ;; Yuk!  This is a reference loop.  Make the article be a
3649             ;; root article.
3650             (progn
3651               (mail-header-set-references (car (symbol-value id-dep)) "none")
3652               (setq ref nil))
3653           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3654       (setq ref (gnus-parent-id (mail-header-references header)))
3655       (setq ref-dep (intern (or ref "none") dependencies))
3656       (if (boundp ref-dep)
3657           (setcdr (symbol-value ref-dep)
3658                   (nconc (cdr (symbol-value ref-dep))
3659                          (list (symbol-value id-dep))))
3660         (set ref-dep (list nil (symbol-value id-dep)))))
3661     header))
3662
3663 (defun gnus-extract-message-id-from-in-reply-to (string)
3664   (if (string-match "<[^>]+>" string)
3665       (substring string (match-beginning 0) (match-end 0))
3666     nil))
3667
3668 (defun gnus-build-sparse-threads ()
3669   (let ((headers gnus-newsgroup-headers)
3670         (mail-parse-charset gnus-newsgroup-charset)
3671         (gnus-summary-ignore-duplicates t)
3672         header references generation relations
3673         subject child end new-child date)
3674     ;; First we create an alist of generations/relations, where
3675     ;; generations is how much we trust the relation, and the relation
3676     ;; is parent/child.
3677     (gnus-message 7 "Making sparse threads...")
3678     (save-excursion
3679       (nnheader-set-temp-buffer " *gnus sparse threads*")
3680       (while (setq header (pop headers))
3681         (when (and (setq references (mail-header-references header))
3682                    (not (string= references "")))
3683           (insert references)
3684           (setq child (mail-header-id header)
3685                 subject (mail-header-subject header)
3686                 date (mail-header-date header)
3687                 generation 0)
3688           (while (search-backward ">" nil t)
3689             (setq end (1+ (point)))
3690             (when (search-backward "<" nil t)
3691               (setq new-child (buffer-substring (point) end))
3692               (push (list (incf generation)
3693                           child (setq child new-child)
3694                           subject date)
3695                     relations)))
3696           (when child
3697             (push (list (1+ generation) child nil subject) relations))
3698           (erase-buffer)))
3699       (kill-buffer (current-buffer)))
3700     ;; Sort over trustworthiness.
3701     (mapcar
3702      (lambda (relation)
3703        (when (gnus-dependencies-add-header
3704               (make-full-mail-header
3705                gnus-reffed-article-number
3706                (nth 3 relation) "" (or (nth 4 relation) "")
3707                (nth 1 relation)
3708                (or (nth 2 relation) "") 0 0 "")
3709               gnus-newsgroup-dependencies nil)
3710          (push gnus-reffed-article-number gnus-newsgroup-limit)
3711          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3712          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3713                gnus-newsgroup-reads)
3714          (decf gnus-reffed-article-number)))
3715      (sort relations 'car-less-than-car))
3716     (gnus-message 7 "Making sparse threads...done")))
3717
3718 (defun gnus-build-old-threads ()
3719   ;; Look at all the articles that refer back to old articles, and
3720   ;; fetch the headers for the articles that aren't there.  This will
3721   ;; build complete threads - if the roots haven't been expired by the
3722   ;; server, that is.
3723   (let ((mail-parse-charset gnus-newsgroup-charset)
3724         id heads)
3725     (mapatoms
3726      (lambda (refs)
3727        (when (not (car (symbol-value refs)))
3728          (setq heads (cdr (symbol-value refs)))
3729          (while heads
3730            (if (memq (mail-header-number (caar heads))
3731                      gnus-newsgroup-dormant)
3732                (setq heads (cdr heads))
3733              (setq id (symbol-name refs))
3734              (while (and (setq id (gnus-build-get-header id))
3735                          (not (car (gnus-id-to-thread id)))))
3736              (setq heads nil)))))
3737      gnus-newsgroup-dependencies)))
3738
3739 ;; This function has to be called with point after the article number
3740 ;; on the beginning of the line.
3741 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3742   (let ((eol (gnus-point-at-eol))
3743         (buffer (current-buffer))
3744         header references in-reply-to)
3745
3746     ;; overview: [num subject from date id refs chars lines misc]
3747     (unwind-protect
3748         (progn
3749           (narrow-to-region (point) eol)
3750           (unless (eobp)
3751             (forward-char))
3752
3753           (setq header
3754                 (make-full-mail-header
3755                  number                 ; number
3756                  (funcall gnus-decode-encoded-word-function
3757                           (nnheader-nov-field)) ; subject
3758                  (funcall gnus-decode-encoded-word-function
3759                           (nnheader-nov-field)) ; from
3760                  (nnheader-nov-field)   ; date
3761                  (nnheader-nov-read-message-id) ; id
3762                  (setq references (nnheader-nov-field)) ; refs
3763                  (nnheader-nov-read-integer) ; chars
3764                  (nnheader-nov-read-integer) ; lines
3765                  (unless (eobp)
3766                    (if (looking-at "Xref: ")
3767                        (goto-char (match-end 0)))
3768                    (nnheader-nov-field)) ; Xref
3769                  (nnheader-nov-parse-extra)))) ; extra
3770
3771       (widen))
3772
3773     (when (and (string= references "")
3774                (setq in-reply-to (mail-header-extra header))
3775                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3776       (mail-header-set-references
3777        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3778
3779     (when gnus-alter-header-function
3780       (funcall gnus-alter-header-function header))
3781     (gnus-dependencies-add-header header dependencies force-new)))
3782
3783 (defun gnus-build-get-header (id)
3784   "Look through the buffer of NOV lines and find the header to ID.
3785 Enter this line into the dependencies hash table, and return
3786 the id of the parent article (if any)."
3787   (let ((deps gnus-newsgroup-dependencies)
3788         found header)
3789     (prog1
3790         (save-excursion
3791           (set-buffer nntp-server-buffer)
3792           (let ((case-fold-search nil))
3793             (goto-char (point-min))
3794             (while (and (not found)
3795                         (search-forward id nil t))
3796               (beginning-of-line)
3797               (setq found (looking-at
3798                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3799                                    (regexp-quote id))))
3800               (or found (beginning-of-line 2)))
3801             (when found
3802               (beginning-of-line)
3803               (and
3804                (setq header (gnus-nov-parse-line
3805                              (read (current-buffer)) deps))
3806                (gnus-parent-id (mail-header-references header))))))
3807       (when header
3808         (let ((number (mail-header-number header)))
3809           (push number gnus-newsgroup-limit)
3810           (push header gnus-newsgroup-headers)
3811           (if (memq number gnus-newsgroup-unselected)
3812               (progn
3813                 (setq gnus-newsgroup-unreads
3814                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3815                                                number))
3816                 (setq gnus-newsgroup-unselected
3817                       (delq number gnus-newsgroup-unselected)))
3818             (push number gnus-newsgroup-ancient)))))))
3819
3820 (defun gnus-build-all-threads ()
3821   "Read all the headers."
3822   (let ((gnus-summary-ignore-duplicates t)
3823         (mail-parse-charset gnus-newsgroup-charset)
3824         (dependencies gnus-newsgroup-dependencies)
3825         header article)
3826     (save-excursion
3827       (set-buffer nntp-server-buffer)
3828       (let ((case-fold-search nil))
3829         (goto-char (point-min))
3830         (while (not (eobp))
3831           (ignore-errors
3832             (setq article (read (current-buffer))
3833                   header (gnus-nov-parse-line article dependencies)))
3834           (when header
3835             (save-excursion
3836               (set-buffer gnus-summary-buffer)
3837               (push header gnus-newsgroup-headers)
3838               (if (memq (setq article (mail-header-number header))
3839                         gnus-newsgroup-unselected)
3840                   (progn
3841                     (setq gnus-newsgroup-unreads
3842                           (gnus-add-to-sorted-list
3843                            gnus-newsgroup-unreads article))
3844                     (setq gnus-newsgroup-unselected
3845                           (delq article gnus-newsgroup-unselected)))
3846                 (push article gnus-newsgroup-ancient)))
3847             (forward-line 1)))))))
3848
3849 (defun gnus-summary-update-article-line (article header)
3850   "Update the line for ARTICLE using HEADERS."
3851   (let* ((id (mail-header-id header))
3852          (thread (gnus-id-to-thread id)))
3853     (unless thread
3854       (error "Article in no thread"))
3855     ;; Update the thread.
3856     (setcar thread header)
3857     (gnus-summary-goto-subject article)
3858     (let* ((datal (gnus-data-find-list article))
3859            (data (car datal))
3860            (length (when (cdr datal)
3861                      (- (gnus-data-pos data)
3862                         (gnus-data-pos (cadr datal)))))
3863            (buffer-read-only nil)
3864            (level (gnus-summary-thread-level)))
3865       (gnus-delete-line)
3866       (gnus-summary-insert-line
3867        header level nil (gnus-article-mark article)
3868        (memq article gnus-newsgroup-replied)
3869        (memq article gnus-newsgroup-expirable)
3870        ;; Only insert the Subject string when it's different
3871        ;; from the previous Subject string.
3872        (if (and
3873             gnus-show-threads
3874             (gnus-subject-equal
3875              (condition-case ()
3876                  (mail-header-subject
3877                   (gnus-data-header
3878                    (cadr
3879                     (gnus-data-find-list
3880                      article
3881                      (gnus-data-list t)))))
3882                ;; Error on the side of excessive subjects.
3883                (error ""))
3884              (mail-header-subject header)))
3885            ""
3886          (mail-header-subject header))
3887        nil (cdr (assq article gnus-newsgroup-scored))
3888        (memq article gnus-newsgroup-processable))
3889       (when length
3890         (gnus-data-update-list
3891          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3892
3893 (defun gnus-summary-update-article (article &optional iheader)
3894   "Update ARTICLE in the summary buffer."
3895   (set-buffer gnus-summary-buffer)
3896   (let* ((header (gnus-summary-article-header article))
3897          (id (mail-header-id header))
3898          (data (gnus-data-find article))
3899          (thread (gnus-id-to-thread id))
3900          (references (mail-header-references header))
3901          (parent
3902           (gnus-id-to-thread
3903            (or (gnus-parent-id
3904                 (when (and references
3905                            (not (equal "" references)))
3906                   references))
3907                "none")))
3908          (buffer-read-only nil)
3909          (old (car thread)))
3910     (when thread
3911       (unless iheader
3912         (setcar thread nil)
3913         (when parent
3914           (delq thread parent)))
3915       (if (gnus-summary-insert-subject id header)
3916        ;; Set the (possibly) new article number in the data structure.
3917           (gnus-data-set-number data (gnus-id-to-article id))
3918         (setcar thread old)
3919         nil))))
3920
3921 (defun gnus-rebuild-thread (id &optional line)
3922   "Rebuild the thread containing ID.
3923 If LINE, insert the rebuilt thread starting on line LINE."
3924   (let ((buffer-read-only nil)
3925         old-pos current thread data)
3926     (if (not gnus-show-threads)
3927         (setq thread (list (car (gnus-id-to-thread id))))
3928       ;; Get the thread this article is part of.
3929       (setq thread (gnus-remove-thread id)))
3930     (setq old-pos (gnus-point-at-bol))
3931     (setq current (save-excursion
3932                     (and (re-search-backward "[\r\n]" nil t)
3933                          (gnus-summary-article-number))))
3934     ;; If this is a gathered thread, we have to go some re-gathering.
3935     (when (stringp (car thread))
3936       (let ((subject (car thread))
3937             roots thr)
3938         (setq thread (cdr thread))
3939         (while thread
3940           (unless (memq (setq thr (gnus-id-to-thread
3941                                    (gnus-root-id
3942                                     (mail-header-id (caar thread)))))
3943                         roots)
3944             (push thr roots))
3945           (setq thread (cdr thread)))
3946         ;; We now have all (unique) roots.
3947         (if (= (length roots) 1)
3948             ;; All the loose roots are now one solid root.
3949             (setq thread (car roots))
3950           (setq thread (cons subject (gnus-sort-threads roots))))))
3951     (let (threads)
3952       ;; We then insert this thread into the summary buffer.
3953       (when line
3954         (goto-char (point-min))
3955         (forward-line (1- line)))
3956       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3957         (if gnus-show-threads
3958             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3959           (gnus-summary-prepare-unthreaded thread))
3960         (setq data (nreverse gnus-newsgroup-data))
3961         (setq threads gnus-newsgroup-threads))
3962       ;; We splice the new data into the data structure.
3963       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3964       ;;!!! then we want to insert at the beginning of the buffer.
3965       ;;!!! That happens to be true with Gnus now, but that may
3966       ;;!!! change in the future.  Perhaps.
3967       (gnus-data-enter-list
3968        (if line nil current) data (- (point) old-pos))
3969       (setq gnus-newsgroup-threads
3970             (nconc threads gnus-newsgroup-threads))
3971       (gnus-data-compute-positions))))
3972
3973 (defun gnus-number-to-header (number)
3974   "Return the header for article NUMBER."
3975   (let ((headers gnus-newsgroup-headers))
3976     (while (and headers
3977                 (not (= number (mail-header-number (car headers)))))
3978       (pop headers))
3979     (when headers
3980       (car headers))))
3981
3982 (defun gnus-parent-headers (in-headers &optional generation)
3983   "Return the headers of the GENERATIONeth parent of HEADERS."
3984   (unless generation
3985     (setq generation 1))
3986   (let ((parent t)
3987         (headers in-headers)
3988         references)
3989     (while (and parent
3990                 (not (zerop generation))
3991                 (setq references (mail-header-references headers)))
3992       (setq headers (if (and references
3993                              (setq parent (gnus-parent-id references)))
3994                         (car (gnus-id-to-thread parent))
3995                       nil))
3996       (decf generation))
3997     (and (not (eq headers in-headers))
3998          headers)))
3999
4000 (defun gnus-id-to-thread (id)
4001   "Return the (sub-)thread where ID appears."
4002   (gnus-gethash id gnus-newsgroup-dependencies))
4003
4004 (defun gnus-id-to-article (id)
4005   "Return the article number of ID."
4006   (let ((thread (gnus-id-to-thread id)))
4007     (when (and thread
4008                (car thread))
4009       (mail-header-number (car thread)))))
4010
4011 (defun gnus-id-to-header (id)
4012   "Return the article headers of ID."
4013   (car (gnus-id-to-thread id)))
4014
4015 (defun gnus-article-displayed-root-p (article)
4016   "Say whether ARTICLE is a root(ish) article."
4017   (let ((level (gnus-summary-thread-level article))
4018         (refs (mail-header-references  (gnus-summary-article-header article)))
4019         particle)
4020     (cond
4021      ((null level) nil)
4022      ((zerop level) t)
4023      ((null refs) t)
4024      ((null (gnus-parent-id refs)) t)
4025      ((and (= 1 level)
4026            (null (setq particle (gnus-id-to-article
4027                                  (gnus-parent-id refs))))
4028            (null (gnus-summary-thread-level particle)))))))
4029
4030 (defun gnus-root-id (id)
4031   "Return the id of the root of the thread where ID appears."
4032   (let (last-id prev)
4033     (while (and id (setq prev (car (gnus-id-to-thread id))))
4034       (setq last-id id
4035             id (gnus-parent-id (mail-header-references prev))))
4036     last-id))
4037
4038 (defun gnus-articles-in-thread (thread)
4039   "Return the list of articles in THREAD."
4040   (cons (mail-header-number (car thread))
4041         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4042
4043 (defun gnus-remove-thread (id &optional dont-remove)
4044   "Remove the thread that has ID in it."
4045   (let (headers thread last-id)
4046     ;; First go up in this thread until we find the root.
4047     (setq last-id (gnus-root-id id)
4048           headers (message-flatten-list (gnus-id-to-thread last-id)))
4049     ;; We have now found the real root of this thread.  It might have
4050     ;; been gathered into some loose thread, so we have to search
4051     ;; through the threads to find the thread we wanted.
4052     (let ((threads gnus-newsgroup-threads)
4053           sub)
4054       (while threads
4055         (setq sub (car threads))
4056         (if (stringp (car sub))
4057             ;; This is a gathered thread, so we look at the roots
4058             ;; below it to find whether this article is in this
4059             ;; gathered root.
4060             (progn
4061               (setq sub (cdr sub))
4062               (while sub
4063                 (when (member (caar sub) headers)
4064                   (setq thread (car threads)
4065                         threads nil
4066                         sub nil))
4067                 (setq sub (cdr sub))))
4068           ;; It's an ordinary thread, so we check it.
4069           (when (eq (car sub) (car headers))
4070             (setq thread sub
4071                   threads nil)))
4072         (setq threads (cdr threads)))
4073       ;; If this article is in no thread, then it's a root.
4074       (if thread
4075           (unless dont-remove
4076             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4077         (setq thread (gnus-id-to-thread last-id)))
4078       (when thread
4079         (prog1
4080             thread                      ; We return this thread.
4081           (unless dont-remove
4082             (if (stringp (car thread))
4083                 (progn
4084                   ;; If we use dummy roots, then we have to remove the
4085                   ;; dummy root as well.
4086                   (when (eq gnus-summary-make-false-root 'dummy)
4087                     ;; We go to the dummy root by going to
4088                     ;; the first sub-"thread", and then one line up.
4089                     (gnus-summary-goto-article
4090                      (mail-header-number (caadr thread)))
4091                     (forward-line -1)
4092                     (gnus-delete-line)
4093                     (gnus-data-compute-positions))
4094                   (setq thread (cdr thread))
4095                   (while thread
4096                     (gnus-remove-thread-1 (car thread))
4097                     (setq thread (cdr thread))))
4098               (gnus-remove-thread-1 thread))))))))
4099
4100 (defun gnus-remove-thread-1 (thread)
4101   "Remove the thread THREAD recursively."
4102   (let ((number (mail-header-number (pop thread)))
4103         d)
4104     (setq thread (reverse thread))
4105     (while thread
4106       (gnus-remove-thread-1 (pop thread)))
4107     (when (setq d (gnus-data-find number))
4108       (goto-char (gnus-data-pos d))
4109       (gnus-summary-show-thread)
4110       (gnus-data-remove
4111        number
4112        (- (gnus-point-at-bol)
4113           (prog1
4114               (1+ (gnus-point-at-eol))
4115             (gnus-delete-line)))))))
4116
4117 (defun gnus-sort-threads-1 (threads func)
4118   (sort (mapcar (lambda (thread)
4119                   (cons (car thread)
4120                         (and (cdr thread)
4121                              (gnus-sort-threads-1 (cdr thread) func))))
4122                 threads) func))
4123
4124 (defun gnus-sort-threads (threads)
4125   "Sort THREADS."
4126   (if (not gnus-thread-sort-functions)
4127       threads
4128     (gnus-message 8 "Sorting threads...")
4129     (prog1
4130         (gnus-sort-threads-1
4131          threads
4132          (gnus-make-sort-function gnus-thread-sort-functions))
4133       (gnus-message 8 "Sorting threads...done"))))
4134
4135 (defun gnus-sort-articles (articles)
4136   "Sort ARTICLES."
4137   (when gnus-article-sort-functions
4138     (gnus-message 7 "Sorting articles...")
4139     (prog1
4140         (setq gnus-newsgroup-headers
4141               (sort articles (gnus-make-sort-function
4142                               gnus-article-sort-functions)))
4143       (gnus-message 7 "Sorting articles...done"))))
4144
4145 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4146 (defmacro gnus-thread-header (thread)
4147   "Return header of first article in THREAD.
4148 Note that THREAD must never, ever be anything else than a variable -
4149 using some other form will lead to serious barfage."
4150   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4151   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4152   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4153         (vector thread) 2))
4154
4155 (defsubst gnus-article-sort-by-number (h1 h2)
4156   "Sort articles by article number."
4157   (< (mail-header-number h1)
4158      (mail-header-number h2)))
4159
4160 (defun gnus-thread-sort-by-number (h1 h2)
4161   "Sort threads by root article number."
4162   (gnus-article-sort-by-number
4163    (gnus-thread-header h1) (gnus-thread-header h2)))
4164
4165 (defsubst gnus-article-sort-by-lines (h1 h2)
4166   "Sort articles by article Lines header."
4167   (< (mail-header-lines h1)
4168      (mail-header-lines h2)))
4169
4170 (defun gnus-thread-sort-by-lines (h1 h2)
4171   "Sort threads by root article Lines header."
4172   (gnus-article-sort-by-lines
4173    (gnus-thread-header h1) (gnus-thread-header h2)))
4174
4175 (defsubst gnus-article-sort-by-chars (h1 h2)
4176   "Sort articles by octet length."
4177   (< (mail-header-chars h1)
4178      (mail-header-chars h2)))
4179
4180 (defun gnus-thread-sort-by-chars (h1 h2)
4181   "Sort threads by root article octet length."
4182   (gnus-article-sort-by-chars
4183    (gnus-thread-header h1) (gnus-thread-header h2)))
4184
4185 (defsubst gnus-article-sort-by-author (h1 h2)
4186   "Sort articles by root author."
4187   (string-lessp
4188    (let ((extract (funcall
4189                    gnus-extract-address-components
4190                    (mail-header-from h1))))
4191      (or (car extract) (cadr extract) ""))
4192    (let ((extract (funcall
4193                    gnus-extract-address-components
4194                    (mail-header-from h2))))
4195      (or (car extract) (cadr extract) ""))))
4196
4197 (defun gnus-thread-sort-by-author (h1 h2)
4198   "Sort threads by root author."
4199   (gnus-article-sort-by-author
4200    (gnus-thread-header h1)  (gnus-thread-header h2)))
4201
4202 (defsubst gnus-article-sort-by-subject (h1 h2)
4203   "Sort articles by root subject."
4204   (string-lessp
4205    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4206    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4207
4208 (defun gnus-thread-sort-by-subject (h1 h2)
4209   "Sort threads by root subject."
4210   (gnus-article-sort-by-subject
4211    (gnus-thread-header h1) (gnus-thread-header h2)))
4212
4213 (defsubst gnus-article-sort-by-date (h1 h2)
4214   "Sort articles by root article date."
4215   (time-less-p
4216    (gnus-date-get-time (mail-header-date h1))
4217    (gnus-date-get-time (mail-header-date h2))))
4218
4219 (defun gnus-thread-sort-by-date (h1 h2)
4220   "Sort threads by root article date."
4221   (gnus-article-sort-by-date
4222    (gnus-thread-header h1) (gnus-thread-header h2)))
4223
4224 (defsubst gnus-article-sort-by-score (h1 h2)
4225   "Sort articles by root article score.
4226 Unscored articles will be counted as having a score of zero."
4227   (> (or (cdr (assq (mail-header-number h1)
4228                     gnus-newsgroup-scored))
4229          gnus-summary-default-score 0)
4230      (or (cdr (assq (mail-header-number h2)
4231                     gnus-newsgroup-scored))
4232          gnus-summary-default-score 0)))
4233
4234 (defun gnus-thread-sort-by-score (h1 h2)
4235   "Sort threads by root article score."
4236   (gnus-article-sort-by-score
4237    (gnus-thread-header h1) (gnus-thread-header h2)))
4238
4239 (defun gnus-thread-sort-by-total-score (h1 h2)
4240   "Sort threads by the sum of all scores in the thread.
4241 Unscored articles will be counted as having a score of zero."
4242   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4243
4244 (defun gnus-thread-total-score (thread)
4245   ;; This function find the total score of THREAD.
4246   (cond
4247    ((null thread)
4248     0)
4249    ((consp thread)
4250     (if (stringp (car thread))
4251         (apply gnus-thread-score-function 0
4252                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4253       (gnus-thread-total-score-1 thread)))
4254    (t
4255     (gnus-thread-total-score-1 (list thread)))))
4256
4257 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4258   "Sort threads such that the thread with the most recently arrived article comes first."
4259   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4260
4261 (defun gnus-thread-highest-number (thread)
4262   "Return the highest article number in THREAD."
4263   (apply 'max (mapcar (lambda (header)
4264                         (mail-header-number header))
4265                       (message-flatten-list thread))))
4266
4267 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4268   "Sort threads such that the thread with the most recently dated article comes first."
4269   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4270
4271 (defun gnus-thread-latest-date (thread)
4272   "Return the highest article date in THREAD."
4273   (let ((previous-time 0))
4274     (apply 'max (mapcar
4275                  (lambda (header)
4276                    (setq previous-time
4277                          (time-to-seconds
4278                           (mail-header-parse-date
4279                            (condition-case ()
4280                                (mail-header-date header)
4281                              (error previous-time))))))
4282                  (sort
4283                   (message-flatten-list thread)
4284                   (lambda (h1 h2)
4285                     (< (mail-header-number h1)
4286                        (mail-header-number h2))))))))
4287
4288 (defun gnus-thread-total-score-1 (root)
4289   ;; This function find the total score of the thread below ROOT.
4290   (setq root (car root))
4291   (apply gnus-thread-score-function
4292          (or (append
4293               (mapcar 'gnus-thread-total-score
4294                       (cdr (gnus-id-to-thread (mail-header-id root))))
4295               (when (> (mail-header-number root) 0)
4296                 (list (or (cdr (assq (mail-header-number root)
4297                                      gnus-newsgroup-scored))
4298                           gnus-summary-default-score 0))))
4299              (list gnus-summary-default-score)
4300              '(0))))
4301
4302 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4303 (defvar gnus-tmp-prev-subject nil)
4304 (defvar gnus-tmp-false-parent nil)
4305 (defvar gnus-tmp-root-expunged nil)
4306 (defvar gnus-tmp-dummy-line nil)
4307
4308 (eval-when-compile (defvar gnus-tmp-header))
4309 (defun gnus-extra-header (type &optional header)
4310   "Return the extra header of TYPE."
4311   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4312       ""))
4313
4314 (defvar gnus-tmp-thread-tree-header-string "")
4315
4316 (defcustom gnus-sum-thread-tree-root "> "
4317   "With %B spec, used for the root of a thread.
4318 If nil, use subject instead."
4319   :type 'string
4320   :group 'gnus-thread)
4321 (defcustom gnus-sum-thread-tree-single-indent ""
4322   "With %B spec, used for a thread with just one message.
4323 If nil, use subject instead."
4324   :type 'string
4325   :group 'gnus-thread)
4326 (defcustom gnus-sum-thread-tree-vertical "| "
4327   "With %B spec, used for drawing a vertical line."
4328   :type 'string
4329   :group 'gnus-thread)
4330 (defcustom gnus-sum-thread-tree-indent "  "
4331   "With %B spec, used for indenting."
4332   :type 'string
4333   :group 'gnus-thread)
4334 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4335   "With %B spec, used for a leaf with brothers."
4336   :type 'string
4337   :group 'gnus-thread)
4338 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4339   "With %B spec, used for a leaf without brothers."
4340   :type 'string
4341   :group 'gnus-thread)
4342
4343 (defun gnus-summary-prepare-threads (threads)
4344   "Prepare summary buffer from THREADS and indentation LEVEL.
4345 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4346 or a straight list of headers."
4347   (gnus-message 7 "Generating summary...")
4348
4349   (setq gnus-newsgroup-threads threads)
4350   (beginning-of-line)
4351
4352   (let ((gnus-tmp-level 0)
4353         (default-score (or gnus-summary-default-score 0))
4354         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4355         thread number subject stack state gnus-tmp-gathered beg-match
4356         new-roots gnus-tmp-new-adopts thread-end
4357         gnus-tmp-header gnus-tmp-unread
4358         gnus-tmp-replied gnus-tmp-subject-or-nil
4359         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4360         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4361         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4362         tree-stack)
4363
4364     (setq gnus-tmp-prev-subject nil)
4365
4366     (if (vectorp (car threads))
4367         ;; If this is a straight (sic) list of headers, then a
4368         ;; threaded summary display isn't required, so we just create
4369         ;; an unthreaded one.
4370         (gnus-summary-prepare-unthreaded threads)
4371
4372       ;; Do the threaded display.
4373
4374       (while (or threads stack gnus-tmp-new-adopts new-roots)
4375
4376         (if (and (= gnus-tmp-level 0)
4377                  (or (not stack)
4378                      (= (caar stack) 0))
4379                  (not gnus-tmp-false-parent)
4380                  (or gnus-tmp-new-adopts new-roots))
4381             (if gnus-tmp-new-adopts
4382                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4383                       thread (list (car gnus-tmp-new-adopts))
4384                       gnus-tmp-header (caar thread)
4385                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4386               (when new-roots
4387                 (setq thread (list (car new-roots))
4388                       gnus-tmp-header (caar thread)
4389                       new-roots (cdr new-roots))))
4390
4391           (if threads
4392               ;; If there are some threads, we do them before the
4393               ;; threads on the stack.
4394               (setq thread threads
4395                     gnus-tmp-header (caar thread))
4396             ;; There were no current threads, so we pop something off
4397             ;; the stack.
4398             (setq state (car stack)
4399                   gnus-tmp-level (car state)
4400                   tree-stack (cadr state)
4401                   thread (caddr state)
4402                   stack (cdr stack)
4403                   gnus-tmp-header (caar thread))))
4404
4405         (setq gnus-tmp-false-parent nil)
4406         (setq gnus-tmp-root-expunged nil)
4407         (setq thread-end nil)
4408
4409         (if (stringp gnus-tmp-header)
4410             ;; The header is a dummy root.
4411             (cond
4412              ((eq gnus-summary-make-false-root 'adopt)
4413               ;; We let the first article adopt the rest.
4414               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4415                                                (cddar thread)))
4416               (setq gnus-tmp-gathered
4417                     (nconc (mapcar
4418                             (lambda (h) (mail-header-number (car h)))
4419                             (cddar thread))
4420                            gnus-tmp-gathered))
4421               (setq thread (cons (list (caar thread)
4422                                        (cadar thread))
4423                                  (cdr thread)))
4424               (setq gnus-tmp-level -1
4425                     gnus-tmp-false-parent t))
4426              ((eq gnus-summary-make-false-root 'empty)
4427               ;; We print adopted articles with empty subject fields.
4428               (setq gnus-tmp-gathered
4429                     (nconc (mapcar
4430                             (lambda (h) (mail-header-number (car h)))
4431                             (cddar thread))
4432                            gnus-tmp-gathered))
4433               (setq gnus-tmp-level -1))
4434              ((eq gnus-summary-make-false-root 'dummy)
4435               ;; We remember that we probably want to output a dummy
4436               ;; root.
4437               (setq gnus-tmp-dummy-line gnus-tmp-header)
4438               (setq gnus-tmp-prev-subject gnus-tmp-header))
4439              (t
4440               ;; We do not make a root for the gathered
4441               ;; sub-threads at all.
4442               (setq gnus-tmp-level -1)))
4443
4444           (setq number (mail-header-number gnus-tmp-header)
4445                 subject (mail-header-subject gnus-tmp-header))
4446
4447           (cond
4448            ;; If the thread has changed subject, we might want to make
4449            ;; this subthread into a root.
4450            ((and (null gnus-thread-ignore-subject)
4451                  (not (zerop gnus-tmp-level))
4452                  gnus-tmp-prev-subject
4453                  (not (inline
4454                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4455             (setq new-roots (nconc new-roots (list (car thread)))
4456                   thread-end t
4457                   gnus-tmp-header nil))
4458            ;; If the article lies outside the current limit,
4459            ;; then we do not display it.
4460            ((not (memq number gnus-newsgroup-limit))
4461             (setq gnus-tmp-gathered
4462                   (nconc (mapcar
4463                           (lambda (h) (mail-header-number (car h)))
4464                           (cdar thread))
4465                          gnus-tmp-gathered))
4466             (setq gnus-tmp-new-adopts (if (cdar thread)
4467                                           (append gnus-tmp-new-adopts
4468                                                   (cdar thread))
4469                                         gnus-tmp-new-adopts)
4470                   thread-end t
4471                   gnus-tmp-header nil)
4472             (when (zerop gnus-tmp-level)
4473               (setq gnus-tmp-root-expunged t)))
4474            ;; Perhaps this article is to be marked as read?
4475            ((and gnus-summary-mark-below
4476                  (< (or (cdr (assq number gnus-newsgroup-scored))
4477                         default-score)
4478                     gnus-summary-mark-below)
4479                  ;; Don't touch sparse articles.
4480                  (not (gnus-summary-article-sparse-p number))
4481                  (not (gnus-summary-article-ancient-p number)))
4482             (setq gnus-newsgroup-unreads
4483                   (delq number gnus-newsgroup-unreads))
4484             (if gnus-newsgroup-auto-expire
4485                 (setq gnus-newsgroup-expirable
4486                       (gnus-add-to-sorted-list
4487                        gnus-newsgroup-expirable number))
4488               (push (cons number gnus-low-score-mark)
4489                     gnus-newsgroup-reads))))
4490
4491           (when gnus-tmp-header
4492             ;; We may have an old dummy line to output before this
4493             ;; article.
4494             (when (and gnus-tmp-dummy-line
4495                        (gnus-subject-equal
4496                         gnus-tmp-dummy-line
4497                         (mail-header-subject gnus-tmp-header)))
4498               (gnus-summary-insert-dummy-line
4499                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4500               (setq gnus-tmp-dummy-line nil))
4501
4502             ;; Compute the mark.
4503             (setq gnus-tmp-unread (gnus-article-mark number))
4504
4505             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4506                                   gnus-tmp-header gnus-tmp-level)
4507                   gnus-newsgroup-data)
4508
4509             ;; Actually insert the line.
4510             (setq
4511              gnus-tmp-subject-or-nil
4512              (cond
4513               ((and gnus-thread-ignore-subject
4514                     gnus-tmp-prev-subject
4515                     (not (inline (gnus-subject-equal
4516                                   gnus-tmp-prev-subject subject))))
4517                subject)
4518               ((zerop gnus-tmp-level)
4519                (if (and (eq gnus-summary-make-false-root 'empty)
4520                         (memq number gnus-tmp-gathered)
4521                         gnus-tmp-prev-subject
4522                         (inline (gnus-subject-equal
4523                                  gnus-tmp-prev-subject subject)))
4524                    gnus-summary-same-subject
4525                  subject))
4526               (t gnus-summary-same-subject)))
4527             (if (and (eq gnus-summary-make-false-root 'adopt)
4528                      (= gnus-tmp-level 1)
4529                      (memq number gnus-tmp-gathered))
4530                 (setq gnus-tmp-opening-bracket ?\<
4531                       gnus-tmp-closing-bracket ?\>)
4532               (setq gnus-tmp-opening-bracket ?\[
4533                     gnus-tmp-closing-bracket ?\]))
4534             (setq
4535              gnus-tmp-indentation
4536              (aref gnus-thread-indent-array gnus-tmp-level)
4537              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4538              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4539                                 gnus-summary-default-score 0)
4540              gnus-tmp-score-char
4541              (if (or (null gnus-summary-default-score)
4542                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4543                          gnus-summary-zcore-fuzz))
4544                  ?                      ;Whitespace
4545                (if (< gnus-tmp-score gnus-summary-default-score)
4546                    gnus-score-below-mark gnus-score-over-mark))
4547              gnus-tmp-replied
4548              (cond ((memq number gnus-newsgroup-processable)
4549                     gnus-process-mark)
4550                    ((memq number gnus-newsgroup-cached)
4551                     gnus-cached-mark)
4552                    ((memq number gnus-newsgroup-replied)
4553                     gnus-replied-mark)
4554                    ((memq number gnus-newsgroup-forwarded)
4555                     gnus-forwarded-mark)
4556                    ((memq number gnus-newsgroup-saved)
4557                     gnus-saved-mark)
4558                    ((memq number gnus-newsgroup-recent)
4559                     gnus-recent-mark)
4560                    ((memq number gnus-newsgroup-unseen)
4561                     gnus-unseen-mark)
4562                    (t gnus-no-mark))
4563              gnus-tmp-from (mail-header-from gnus-tmp-header)
4564              gnus-tmp-name
4565              (cond
4566               ((string-match "<[^>]+> *$" gnus-tmp-from)
4567                (setq beg-match (match-beginning 0))
4568                (or (and (string-match "^\".+\"" gnus-tmp-from)
4569                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4570                    (substring gnus-tmp-from 0 beg-match)))
4571               ((string-match "(.+)" gnus-tmp-from)
4572                (substring gnus-tmp-from
4573                           (1+ (match-beginning 0)) (1- (match-end 0))))
4574               (t gnus-tmp-from))
4575              gnus-tmp-thread-tree-header-string
4576              (cond
4577               ((not gnus-show-threads) "")
4578               ((zerop gnus-tmp-level)
4579                (if (cdar thread)
4580                    (or gnus-sum-thread-tree-root subject)
4581                  (or gnus-sum-thread-tree-single-indent subject)))
4582               (t
4583                (concat (apply 'concat
4584                               (mapcar (lambda (item)
4585                                         (if (= item 1)
4586                                             gnus-sum-thread-tree-vertical
4587                                           gnus-sum-thread-tree-indent))
4588                                       (cdr (reverse tree-stack))))
4589                        (if (nth 1 thread)
4590                            gnus-sum-thread-tree-leaf-with-other
4591                          gnus-sum-thread-tree-single-leaf)))))
4592             (when (string= gnus-tmp-name "")
4593               (setq gnus-tmp-name gnus-tmp-from))
4594             (unless (numberp gnus-tmp-lines)
4595               (setq gnus-tmp-lines -1))
4596             (if (= gnus-tmp-lines -1)
4597                 (setq gnus-tmp-lines "?")
4598               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4599             (gnus-put-text-property
4600              (point)
4601              (progn (eval gnus-summary-line-format-spec) (point))
4602              'gnus-number number)
4603             (when gnus-visual-p
4604               (forward-line -1)
4605               (gnus-run-hooks 'gnus-summary-update-hook)
4606               (forward-line 1))
4607
4608             (setq gnus-tmp-prev-subject subject)))
4609
4610         (when (nth 1 thread)
4611           (push (list (max 0 gnus-tmp-level)
4612                       (copy-list tree-stack)
4613                       (nthcdr 1 thread))
4614                 stack))
4615         (push (if (nth 1 thread) 1 0) tree-stack)
4616         (incf gnus-tmp-level)
4617         (setq threads (if thread-end nil (cdar thread)))
4618         (unless threads
4619           (setq gnus-tmp-level 0)))))
4620   (gnus-message 7 "Generating summary...done"))
4621
4622 (defun gnus-summary-prepare-unthreaded (headers)
4623   "Generate an unthreaded summary buffer based on HEADERS."
4624   (let (header number mark)
4625
4626     (beginning-of-line)
4627
4628     (while headers
4629       ;; We may have to root out some bad articles...
4630       (when (memq (setq number (mail-header-number
4631                                 (setq header (pop headers))))
4632                   gnus-newsgroup-limit)
4633         ;; Mark article as read when it has a low score.
4634         (when (and gnus-summary-mark-below
4635                    (< (or (cdr (assq number gnus-newsgroup-scored))
4636                           gnus-summary-default-score 0)
4637                       gnus-summary-mark-below)
4638                    (not (gnus-summary-article-ancient-p number)))
4639           (setq gnus-newsgroup-unreads
4640                 (delq number gnus-newsgroup-unreads))
4641           (if gnus-newsgroup-auto-expire
4642               (push number gnus-newsgroup-expirable)
4643             (push (cons number gnus-low-score-mark)
4644                   gnus-newsgroup-reads)))
4645
4646         (setq mark (gnus-article-mark number))
4647         (push (gnus-data-make number mark (1+ (point)) header 0)
4648               gnus-newsgroup-data)
4649         (gnus-summary-insert-line
4650          header 0 number
4651          mark (memq number gnus-newsgroup-replied)
4652          (memq number gnus-newsgroup-expirable)
4653          (mail-header-subject header) nil
4654          (cdr (assq number gnus-newsgroup-scored))
4655          (memq number gnus-newsgroup-processable))))))
4656
4657 (defun gnus-summary-remove-list-identifiers ()
4658   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4659   (let ((regexp (if (consp gnus-list-identifiers)
4660                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4661                   gnus-list-identifiers))
4662         changed subject)
4663     (when regexp
4664       (dolist (header gnus-newsgroup-headers)
4665         (setq subject (mail-header-subject header)
4666               changed nil)
4667         (while (string-match
4668                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4669                 subject)
4670           (setq subject
4671                 (concat (substring subject 0 (match-beginning 2))
4672                         (substring subject (match-end 0)))
4673                 changed t))
4674         (when (and changed
4675                    (string-match
4676                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4677           (setq subject
4678                 (concat (substring subject 0 (match-beginning 1))
4679                         (substring subject (match-end 1)))))
4680         (when changed
4681           (mail-header-set-subject header subject))))))
4682
4683 (defun gnus-fetch-headers (articles)
4684   "Fetch headers of ARTICLES."
4685   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4686     (gnus-message 5 "Fetching headers for %s..." name)
4687     (prog1
4688         (if (eq 'nov
4689                 (setq gnus-headers-retrieved-by
4690                       (gnus-retrieve-headers
4691                        articles gnus-newsgroup-name
4692                        ;; We might want to fetch old headers, but
4693                        ;; not if there is only 1 article.
4694                        (and (or (and
4695                                  (not (eq gnus-fetch-old-headers 'some))
4696                                  (not (numberp gnus-fetch-old-headers)))
4697                                 (> (length articles) 1))
4698                             gnus-fetch-old-headers))))
4699             (gnus-get-newsgroup-headers-xover
4700              articles nil nil gnus-newsgroup-name t)
4701           (gnus-get-newsgroup-headers))
4702       (gnus-message 5 "Fetching headers for %s...done" name))))
4703
4704 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4705   "Select newsgroup GROUP.
4706 If READ-ALL is non-nil, all articles in the group are selected.
4707 If SELECT-ARTICLES, only select those articles from GROUP."
4708   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4709          ;;!!! Dirty hack; should be removed.
4710          (gnus-summary-ignore-duplicates
4711           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4712               t
4713             gnus-summary-ignore-duplicates))
4714          (info (nth 2 entry))
4715          articles fetched-articles cached)
4716
4717     (unless (gnus-check-server
4718              (set (make-local-variable 'gnus-current-select-method)
4719                   (gnus-find-method-for-group group)))
4720       (error "Couldn't open server"))
4721
4722     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4723         (gnus-activate-group group)     ; Or we can activate it...
4724         (progn                          ; Or we bug out.
4725           (when (equal major-mode 'gnus-summary-mode)
4726             (kill-buffer (current-buffer)))
4727           (error "Couldn't activate group %s: %s"
4728                  group (gnus-status-message group))))
4729
4730     (unless (gnus-request-group group t)
4731       (when (equal major-mode 'gnus-summary-mode)
4732         (kill-buffer (current-buffer)))
4733       (error "Couldn't request group %s: %s"
4734              group (gnus-status-message group)))
4735
4736     (setq gnus-newsgroup-name group
4737           gnus-newsgroup-unselected nil
4738           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4739
4740     (let ((display (gnus-group-find-parameter group 'display)))
4741       (setq gnus-newsgroup-display
4742             (cond
4743              ((not (zerop (or (car-safe read-all) 0)))
4744               ;; The user entered the group with C-u SPC/RET, let's show
4745               ;; all articles.
4746               'gnus-not-ignore)
4747              ((eq display 'all)
4748               'gnus-not-ignore)
4749              ((arrayp display)
4750               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4751              ((numberp display)
4752               ;; The following is probably the "correct" solution, but
4753               ;; it makes Gnus fetch all headers and then limit the
4754               ;; articles (which is slow), so instead we hack the
4755               ;; select-articles parameter instead. -- Simon Josefsson
4756               ;; <jas@kth.se>
4757               ;;
4758               ;; (gnus-byte-compile
4759               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4760               ;;                         display)))))
4761               (setq select-articles
4762                     (gnus-uncompress-range
4763                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4764                              (if (> tmp 0)
4765                                  tmp
4766                                1))
4767                            (cdr (gnus-active group)))))
4768               nil)
4769              (t
4770               nil))))
4771
4772     (gnus-summary-setup-default-charset)
4773
4774     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4775     (when (gnus-virtual-group-p group)
4776       (setq cached gnus-newsgroup-cached))
4777
4778     (setq gnus-newsgroup-unreads
4779           (gnus-sorted-ndifference
4780            (gnus-sorted-ndifference gnus-newsgroup-unreads
4781                                     gnus-newsgroup-marked)
4782            gnus-newsgroup-dormant))
4783
4784     (setq gnus-newsgroup-processable nil)
4785
4786     (gnus-update-read-articles group gnus-newsgroup-unreads)
4787
4788     ;; Adjust and set lists of article marks.
4789     (when info
4790       (gnus-adjust-marked-articles info))
4791
4792     (if (setq articles select-articles)
4793         (setq gnus-newsgroup-unselected
4794               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4795       (setq articles (gnus-articles-to-read group read-all)))
4796
4797     (cond
4798      ((null articles)
4799       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4800       'quit)
4801      ((eq articles 0) nil)
4802      (t
4803       ;; Init the dependencies hash table.
4804       (setq gnus-newsgroup-dependencies
4805             (gnus-make-hashtable (length articles)))
4806       (gnus-set-global-variables)
4807       ;; Retrieve the headers and read them in.
4808       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4809
4810       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4811       (when cached
4812         (setq gnus-newsgroup-cached cached))
4813
4814       ;; Suppress duplicates?
4815       (when gnus-suppress-duplicates
4816         (gnus-dup-suppress-articles))
4817
4818       ;; Set the initial limit.
4819       (setq gnus-newsgroup-limit (copy-sequence articles))
4820       ;; Remove canceled articles from the list of unread articles.
4821       (setq fetched-articles
4822             (mapcar (lambda (headers) (mail-header-number headers))
4823                     gnus-newsgroup-headers))
4824       (setq gnus-newsgroup-articles fetched-articles)
4825       (setq gnus-newsgroup-unreads
4826             (gnus-sorted-nintersection
4827              gnus-newsgroup-unreads fetched-articles))
4828       (gnus-compute-unseen-list)
4829
4830       ;; Removed marked articles that do not exist.
4831       (gnus-update-missing-marks
4832        (gnus-sorted-difference articles fetched-articles))
4833       ;; We might want to build some more threads first.
4834       (when (and gnus-fetch-old-headers
4835                  (eq gnus-headers-retrieved-by 'nov))
4836         (if (eq gnus-fetch-old-headers 'invisible)
4837             (gnus-build-all-threads)
4838           (gnus-build-old-threads)))
4839       ;; Let the Gnus agent mark articles as read.
4840       (when gnus-agent
4841         (gnus-agent-get-undownloaded-list))
4842       ;; Remove list identifiers from subject
4843       (when gnus-list-identifiers
4844         (gnus-summary-remove-list-identifiers))
4845       ;; Check whether auto-expire is to be done in this group.
4846       (setq gnus-newsgroup-auto-expire
4847             (gnus-group-auto-expirable-p group))
4848       ;; Set up the article buffer now, if necessary.
4849       (unless gnus-single-article-buffer
4850         (gnus-article-setup-buffer))
4851       ;; First and last article in this newsgroup.
4852       (when gnus-newsgroup-headers
4853         (setq gnus-newsgroup-begin
4854               (mail-header-number (car gnus-newsgroup-headers))
4855               gnus-newsgroup-end
4856               (mail-header-number
4857                (gnus-last-element gnus-newsgroup-headers))))
4858       ;; GROUP is successfully selected.
4859       (or gnus-newsgroup-headers t)))))
4860
4861 (defun gnus-compute-unseen-list ()
4862   ;; The `seen' marks are treated specially.
4863   (if (not gnus-newsgroup-seen)
4864       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4865     (setq gnus-newsgroup-unseen
4866           (gnus-inverse-list-range-intersection
4867            gnus-newsgroup-articles gnus-newsgroup-seen))))
4868
4869 (defun gnus-summary-display-make-predicate (display)
4870   (require 'gnus-agent)
4871   (when (= (length display) 1)
4872     (setq display (car display)))
4873   (unless gnus-summary-display-cache
4874     (dolist (elem (append '((unread . unread)
4875                             (read . read)
4876                             (unseen . unseen))
4877                           gnus-article-mark-lists))
4878       (push (cons (cdr elem)
4879                   (gnus-byte-compile
4880                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4881             gnus-summary-display-cache)))
4882   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
4883         (gnus-category-predicate-cache gnus-summary-display-cache))
4884     (gnus-get-predicate display)))
4885
4886 ;; Uses the dynamically bound `number' variable.
4887 (defvar number)
4888 (defun gnus-article-marked-p (type &optional article)
4889   (let ((article (or article number)))
4890     (cond
4891      ((eq type 'tick)
4892       (memq article gnus-newsgroup-marked))
4893      ((eq type 'unsend)
4894       (memq article gnus-newsgroup-unsendable))
4895      ((eq type 'undownload)
4896       (memq article gnus-newsgroup-undownloaded))
4897      ((eq type 'download)
4898       (memq article gnus-newsgroup-downloadable))
4899      ((eq type 'unread)
4900       (memq article gnus-newsgroup-unreads))
4901      ((eq type 'read)
4902       (memq article gnus-newsgroup-reads))
4903      ((eq type 'dormant)
4904       (memq article gnus-newsgroup-dormant) )
4905      ((eq type 'expire)
4906       (memq article gnus-newsgroup-expirable))
4907      ((eq type 'reply)
4908       (memq article gnus-newsgroup-replied))
4909      ((eq type 'killed)
4910       (memq article gnus-newsgroup-killed))
4911      ((eq type 'bookmark)
4912       (assq article gnus-newsgroup-bookmarks))
4913      ((eq type 'score)
4914       (assq article gnus-newsgroup-scored))
4915      ((eq type 'save)
4916       (memq article gnus-newsgroup-saved))
4917      ((eq type 'cache)
4918       (memq article gnus-newsgroup-cached))
4919      ((eq type 'forward)
4920       (memq article gnus-newsgroup-forwarded))
4921      ((eq type 'seen)
4922       (not (memq article gnus-newsgroup-unseen)))
4923      ((eq type 'recent)
4924       (memq article gnus-newsgroup-recent))
4925      (t t))))
4926
4927 (defun gnus-articles-to-read (group &optional read-all)
4928   "Find out what articles the user wants to read."
4929   (let* ((articles
4930           ;; Select all articles if `read-all' is non-nil, or if there
4931           ;; are no unread articles.
4932           (if (or read-all
4933                   (and (zerop (length gnus-newsgroup-marked))
4934                        (zerop (length gnus-newsgroup-unreads)))
4935                   ;; Fetch all if the predicate is non-nil.
4936                   gnus-newsgroup-display)
4937               ;; We want to select the headers for all the articles in
4938               ;; the group, so we select either all the active
4939               ;; articles in the group, or (if that's nil), the
4940               ;; articles in the cache.
4941               (or
4942                (gnus-uncompress-range (gnus-active group))
4943                (gnus-cache-articles-in-group group))
4944             ;; Select only the "normal" subset of articles.
4945             (gnus-sorted-nunion
4946              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
4947              gnus-newsgroup-unreads)))
4948          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4949          (scored (length scored-list))
4950          (number (length articles))
4951          (marked (+ (length gnus-newsgroup-marked)
4952                     (length gnus-newsgroup-dormant)))
4953          (select
4954           (cond
4955            ((numberp read-all)
4956             read-all)
4957            ((numberp gnus-newsgroup-display)
4958             gnus-newsgroup-display)
4959            (t
4960             (condition-case ()
4961                 (cond
4962                  ((and (or (<= scored marked) (= scored number))
4963                        (numberp gnus-large-newsgroup)
4964                        (> number gnus-large-newsgroup))
4965                   (let* ((cursor-in-echo-area nil)
4966                          (initial (gnus-parameter-large-newsgroup-initial 
4967                                    gnus-newsgroup-name))
4968                          (input
4969                           (read-string
4970                            (format
4971                             "How many articles from %s (%s %d): "
4972                             (gnus-limit-string
4973                              (gnus-group-decoded-name gnus-newsgroup-name)
4974                              35)
4975                             (if initial "max" "default")
4976                             number)
4977                            (if initial
4978                                (cons (number-to-string initial)
4979                                      0)))))
4980                     (if (string-match "^[ \t]*$" input) number input)))
4981                  ((and (> scored marked) (< scored number)
4982                        (> (- scored number) 20))
4983                   (let ((input
4984                          (read-string
4985                           (format "%s %s (%d scored, %d total): "
4986                                   "How many articles from"
4987                                   (gnus-group-decoded-name group)
4988                                   scored number))))
4989                     (if (string-match "^[ \t]*$" input)
4990                         number input)))
4991                  (t number))
4992               (quit
4993                (message "Quit getting the articles to read")
4994                nil))))))
4995     (setq select (if (stringp select) (string-to-number select) select))
4996     (if (or (null select) (zerop select))
4997         select
4998       (if (and (not (zerop scored)) (<= (abs select) scored))
4999           (progn
5000             (setq articles (sort scored-list '<))
5001             (setq number (length articles)))
5002         (setq articles (copy-sequence articles)))
5003
5004       (when (< (abs select) number)
5005         (if (< select 0)
5006             ;; Select the N oldest articles.
5007             (setcdr (nthcdr (1- (abs select)) articles) nil)
5008           ;; Select the N most recent articles.
5009           (setq articles (nthcdr (- number select) articles))))
5010       (setq gnus-newsgroup-unselected
5011             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5012       (when gnus-alter-articles-to-read-function
5013         (setq gnus-newsgroup-unreads
5014               (sort
5015                (funcall gnus-alter-articles-to-read-function
5016                         gnus-newsgroup-name gnus-newsgroup-unreads)
5017                '<)))
5018       articles)))
5019
5020 (defun gnus-killed-articles (killed articles)
5021   (let (out)
5022     (while articles
5023       (when (inline (gnus-member-of-range (car articles) killed))
5024         (push (car articles) out))
5025       (setq articles (cdr articles)))
5026     out))
5027
5028 (defun gnus-uncompress-marks (marks)
5029   "Uncompress the mark ranges in MARKS."
5030   (let ((uncompressed '(score bookmark))
5031         out)
5032     (while marks
5033       (if (memq (caar marks) uncompressed)
5034           (push (car marks) out)
5035         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5036       (setq marks (cdr marks)))
5037     out))
5038
5039 (defun gnus-article-mark-to-type (mark)
5040   "Return the type of MARK."
5041   (or (cadr (assq mark gnus-article-special-mark-lists))
5042       'list))
5043
5044 (defun gnus-article-unpropagatable-p (mark)
5045   "Return whether MARK should be propagated to backend."
5046   (memq mark gnus-article-unpropagated-mark-lists))
5047
5048 (defun gnus-adjust-marked-articles (info)
5049   "Set all article lists and remove all marks that are no longer valid."
5050   (let* ((marked-lists (gnus-info-marks info))
5051          (active (gnus-active (gnus-info-group info)))
5052          (min (car active))
5053          (max (cdr active))
5054          (types gnus-article-mark-lists)
5055          marks var articles article mark mark-type)
5056
5057     (dolist (marks marked-lists)
5058       (setq mark (car marks)
5059             mark-type (gnus-article-mark-to-type mark)
5060             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5061
5062       ;; We set the variable according to the type of the marks list,
5063       ;; and then adjust the marks to a subset of the active articles.
5064       (cond
5065        ;; Adjust "simple" lists.
5066        ((eq mark-type 'list)
5067         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5068         (when (memq mark '(tick dormant expire reply save))
5069           (while articles
5070             (when (or (< (setq article (pop articles)) min) (> article max))
5071               (set var (delq article (symbol-value var)))))))
5072        ;; Adjust assocs.
5073        ((eq mark-type 'tuple)
5074         (set var (setq articles (cdr marks)))
5075         (when (not (listp (cdr (symbol-value var))))
5076           (set var (list (symbol-value var))))
5077         (when (not (listp (cdr articles)))
5078           (setq articles (list articles)))
5079         (while articles
5080           (when (or (not (consp (setq article (pop articles))))
5081                     (< (car article) min)
5082                     (> (car article) max))
5083             (set var (delq article (symbol-value var))))))
5084        ;; Adjust ranges (sloppily).
5085        ((eq mark-type 'range)
5086         (cond
5087          ((eq mark 'seen)
5088           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5089           ;; It should be (seen (NUM1 . NUM2)).
5090           (when (numberp (cddr marks))
5091             (setcdr marks (list (cdr marks))))
5092           (setq articles (cdr marks))
5093           (while (and articles
5094                       (or (and (consp (car articles))
5095                                (> min (cdar articles)))
5096                           (and (numberp (car articles))
5097                                (> min (car articles)))))
5098             (pop articles))
5099           (set var articles))))))))
5100
5101 (defun gnus-update-missing-marks (missing)
5102   "Go through the list of MISSING articles and remove them from the mark lists."
5103   (when missing
5104     (let (var m)
5105       ;; Go through all types.
5106       (dolist (elem gnus-article-mark-lists)
5107         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5108           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5109           (when (symbol-value var)
5110             ;; This list has articles.  So we delete all missing
5111             ;; articles from it.
5112             (setq m missing)
5113             (while m
5114               (set var (delq (pop m) (symbol-value var))))))))))
5115
5116 (defun gnus-update-marks ()
5117   "Enter the various lists of marked articles into the newsgroup info list."
5118   (let ((types gnus-article-mark-lists)
5119         (info (gnus-get-info gnus-newsgroup-name))
5120         type list newmarked symbol delta-marks)
5121     (when info
5122       ;; Add all marks lists to the list of marks lists.
5123       (while (setq type (pop types))
5124         (setq list (symbol-value
5125                     (setq symbol
5126                           (intern (format "gnus-newsgroup-%s" (car type))))))
5127
5128         (when list
5129           ;; Get rid of the entries of the articles that have the
5130           ;; default score.
5131           (when (and (eq (cdr type) 'score)
5132                      gnus-save-score
5133                      list)
5134             (let* ((arts list)
5135                    (prev (cons nil list))
5136                    (all prev))
5137               (while arts
5138                 (if (or (not (consp (car arts)))
5139                         (= (cdar arts) gnus-summary-default-score))
5140                     (setcdr prev (cdr arts))
5141                   (setq prev arts))
5142                 (setq arts (cdr arts)))
5143               (setq list (cdr all)))))
5144
5145         (when (eq (cdr type) 'seen)
5146           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5147
5148         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5149           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5150
5151         (when (and (gnus-check-backend-function
5152                     'request-set-mark gnus-newsgroup-name)
5153                    (not (gnus-article-unpropagatable-p (cdr type))))
5154           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5155                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5156                  (add (gnus-remove-from-range
5157                        (gnus-copy-sequence list) old)))
5158             (when add
5159               (push (list add 'add (list (cdr type))) delta-marks))
5160             (when del
5161               (push (list del 'del (list (cdr type))) delta-marks))))
5162
5163         (when list
5164           (push (cons (cdr type) list) newmarked)))
5165
5166       (when delta-marks
5167         (unless (gnus-check-group gnus-newsgroup-name)
5168           (error "Can't open server for %s" gnus-newsgroup-name))
5169         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5170
5171       ;; Enter these new marks into the info of the group.
5172       (if (nthcdr 3 info)
5173           (setcar (nthcdr 3 info) newmarked)
5174         ;; Add the marks lists to the end of the info.
5175         (when newmarked
5176           (setcdr (nthcdr 2 info) (list newmarked))))
5177
5178       ;; Cut off the end of the info if there's nothing else there.
5179       (let ((i 5))
5180         (while (and (> i 2)
5181                     (not (nth i info)))
5182           (when (nthcdr (decf i) info)
5183             (setcdr (nthcdr i info) nil)))))))
5184
5185 (defun gnus-set-mode-line (where)
5186   "Set the mode line of the article or summary buffers.
5187 If WHERE is `summary', the summary mode line format will be used."
5188   ;; Is this mode line one we keep updated?
5189   (when (and (memq where gnus-updated-mode-lines)
5190              (symbol-value
5191               (intern (format "gnus-%s-mode-line-format-spec" where))))
5192     (let (mode-string)
5193       (save-excursion
5194         ;; We evaluate this in the summary buffer since these
5195         ;; variables are buffer-local to that buffer.
5196         (set-buffer gnus-summary-buffer)
5197        ;; We bind all these variables that are used in the `eval' form
5198         ;; below.
5199         (let* ((mformat (symbol-value
5200                          (intern
5201                           (format "gnus-%s-mode-line-format-spec" where))))
5202                (gnus-tmp-group-name (gnus-group-decoded-name
5203                                      gnus-newsgroup-name))
5204                (gnus-tmp-article-number (or gnus-current-article 0))
5205                (gnus-tmp-unread gnus-newsgroup-unreads)
5206                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5207                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5208                (gnus-tmp-unread-and-unselected
5209                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5210                             (zerop gnus-tmp-unselected))
5211                        "")
5212                       ((zerop gnus-tmp-unselected)
5213                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5214                       (t (format "{%d(+%d) more}"
5215                                  gnus-tmp-unread-and-unticked
5216                                  gnus-tmp-unselected))))
5217                (gnus-tmp-subject
5218                 (if (and gnus-current-headers
5219                          (vectorp gnus-current-headers))
5220                     (gnus-mode-string-quote
5221                      (mail-header-subject gnus-current-headers))
5222                   ""))
5223                bufname-length max-len
5224                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5225           (setq mode-string (eval mformat))
5226           (setq bufname-length (if (string-match "%b" mode-string)
5227                                    (- (length
5228                                        (buffer-name
5229                                         (if (eq where 'summary)
5230                                             nil
5231                                           (get-buffer gnus-article-buffer))))
5232                                       2)
5233                                  0))
5234           (setq max-len (max 4 (if gnus-mode-non-string-length
5235                                    (- (window-width)
5236                                       gnus-mode-non-string-length
5237                                       bufname-length)
5238                                  (length mode-string))))
5239           ;; We might have to chop a bit of the string off...
5240           (when (> (length mode-string) max-len)
5241             (setq mode-string
5242                   (concat (truncate-string-to-width mode-string (- max-len 3))
5243                           "...")))
5244           ;; Pad the mode string a bit.
5245           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5246       ;; Update the mode line.
5247       (setq mode-line-buffer-identification
5248             (gnus-mode-line-buffer-identification (list mode-string)))
5249       (set-buffer-modified-p t))))
5250
5251 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5252   "Go through the HEADERS list and add all Xrefs to a hash table.
5253 The resulting hash table is returned, or nil if no Xrefs were found."
5254   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5255          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5256          (xref-hashtb (gnus-make-hashtable))
5257          start group entry number xrefs header)
5258     (while headers
5259       (setq header (pop headers))
5260       (when (and (setq xrefs (mail-header-xref header))
5261                  (not (memq (setq number (mail-header-number header))
5262                             unreads)))
5263         (setq start 0)
5264         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5265           (setq start (match-end 0))
5266           (setq group (if prefix
5267                           (concat prefix (substring xrefs (match-beginning 1)
5268                                                     (match-end 1)))
5269                         (substring xrefs (match-beginning 1) (match-end 1))))
5270           (setq number
5271                 (string-to-int (substring xrefs (match-beginning 2)
5272                                           (match-end 2))))
5273           (if (setq entry (gnus-gethash group xref-hashtb))
5274               (setcdr entry (cons number (cdr entry)))
5275             (gnus-sethash group (cons number nil) xref-hashtb)))))
5276     (and start xref-hashtb)))
5277
5278 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5279   "Look through all the headers and mark the Xrefs as read."
5280   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5281         name entry info xref-hashtb idlist method nth4)
5282     (save-excursion
5283       (set-buffer gnus-group-buffer)
5284       (when (setq xref-hashtb
5285                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5286         (mapatoms
5287          (lambda (group)
5288            (unless (string= from-newsgroup (setq name (symbol-name group)))
5289              (setq idlist (symbol-value group))
5290              ;; Dead groups are not updated.
5291              (and (prog1
5292                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5293                             info (nth 2 entry))
5294                     (when (stringp (setq nth4 (gnus-info-method info)))
5295                       (setq nth4 (gnus-server-to-method nth4))))
5296                   ;; Only do the xrefs if the group has the same
5297                   ;; select method as the group we have just read.
5298                   (or (gnus-methods-equal-p
5299                        nth4 (gnus-find-method-for-group from-newsgroup))
5300                       virtual
5301                       (equal nth4 (setq method (gnus-find-method-for-group
5302                                                 from-newsgroup)))
5303                       (and (equal (car nth4) (car method))
5304                            (equal (nth 1 nth4) (nth 1 method))))
5305                   gnus-use-cross-reference
5306                   (or (not (eq gnus-use-cross-reference t))
5307                       virtual
5308                       ;; Only do cross-references on subscribed
5309                       ;; groups, if that is what is wanted.
5310                       (<= (gnus-info-level info) gnus-level-subscribed))
5311                   (gnus-group-make-articles-read name idlist))))
5312          xref-hashtb)))))
5313
5314 (defun gnus-compute-read-articles (group articles)
5315   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5316          (info (nth 2 entry))
5317          (active (gnus-active group))
5318          ninfo)
5319     (when entry
5320       ;; First peel off all invalid article numbers.
5321       (when active
5322         (let ((ids articles)
5323               id first)
5324           (while (setq id (pop ids))
5325             (when (and first (> id (cdr active)))
5326               ;; We'll end up in this situation in one particular
5327               ;; obscure situation.  If you re-scan a group and get
5328               ;; a new article that is cross-posted to a different
5329               ;; group that has not been re-scanned, you might get
5330               ;; crossposted article that has a higher number than
5331               ;; Gnus believes possible.  So we re-activate this
5332               ;; group as well.  This might mean doing the
5333               ;; crossposting thingy will *increase* the number
5334               ;; of articles in some groups.  Tsk, tsk.
5335               (setq active (or (gnus-activate-group group) active)))
5336             (when (or (> id (cdr active))
5337                       (< id (car active)))
5338               (setq articles (delq id articles))))))
5339       ;; If the read list is nil, we init it.
5340       (if (and active
5341                (null (gnus-info-read info))
5342                (> (car active) 1))
5343           (setq ninfo (cons 1 (1- (car active))))
5344         (setq ninfo (gnus-info-read info)))
5345       ;; Then we add the read articles to the range.
5346       (gnus-add-to-range
5347        ninfo (setq articles (sort articles '<))))))
5348
5349 (defun gnus-group-make-articles-read (group articles)
5350   "Update the info of GROUP to say that ARTICLES are read."
5351   (let* ((num 0)
5352          (entry (gnus-gethash group gnus-newsrc-hashtb))
5353          (info (nth 2 entry))
5354          (active (gnus-active group))
5355          range)
5356     (when entry
5357       (setq range (gnus-compute-read-articles group articles))
5358       (save-excursion
5359         (set-buffer gnus-group-buffer)
5360         (gnus-undo-register
5361           `(progn
5362              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5363              (gnus-info-set-read ',info ',(gnus-info-read info))
5364              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5365              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5366              (gnus-group-update-group ,group t))))
5367       ;; Add the read articles to the range.
5368       (gnus-info-set-read info range)
5369       (gnus-request-set-mark group (list (list range 'add '(read))))
5370       ;; Then we have to re-compute how many unread
5371       ;; articles there are in this group.
5372       (when active
5373         (cond
5374          ((not range)
5375           (setq num (- (1+ (cdr active)) (car active))))
5376          ((not (listp (cdr range)))
5377           (setq num (- (cdr active) (- (1+ (cdr range))
5378                                        (car range)))))
5379          (t
5380           (while range
5381             (if (numberp (car range))
5382                 (setq num (1+ num))
5383               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5384             (setq range (cdr range)))
5385           (setq num (- (cdr active) num))))
5386         ;; Update the number of unread articles.
5387         (setcar entry num)
5388         ;; Update the group buffer.
5389         (gnus-group-update-group group t)))))
5390
5391 (defvar gnus-newsgroup-none-id 0)
5392
5393 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5394   (let ((cur nntp-server-buffer)
5395         (dependencies
5396          (or dependencies
5397              (save-excursion (set-buffer gnus-summary-buffer)
5398                              gnus-newsgroup-dependencies)))
5399         headers id end ref
5400         (mail-parse-charset gnus-newsgroup-charset)
5401         (mail-parse-ignored-charsets
5402          (save-excursion (condition-case nil
5403                              (set-buffer gnus-summary-buffer)
5404                            (error))
5405                          gnus-newsgroup-ignored-charsets)))
5406     (save-excursion
5407       (set-buffer nntp-server-buffer)
5408       ;; Translate all TAB characters into SPACE characters.
5409       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5410       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5411       (gnus-run-hooks 'gnus-parse-headers-hook)
5412       (let ((case-fold-search t)
5413             in-reply-to header p lines chars)
5414         (goto-char (point-min))
5415         ;; Search to the beginning of the next header.  Error messages
5416         ;; do not begin with 2 or 3.
5417         (while (re-search-forward "^[23][0-9]+ " nil t)
5418           (setq id nil
5419                 ref nil)
5420           ;; This implementation of this function, with nine
5421           ;; search-forwards instead of the one re-search-forward and
5422           ;; a case (which basically was the old function) is actually
5423           ;; about twice as fast, even though it looks messier.  You
5424           ;; can't have everything, I guess.  Speed and elegance
5425           ;; doesn't always go hand in hand.
5426           (setq
5427            header
5428            (vector
5429             ;; Number.
5430             (prog1
5431                 (read cur)
5432               (end-of-line)
5433               (setq p (point))
5434               (narrow-to-region (point)
5435                                 (or (and (search-forward "\n.\n" nil t)
5436                                          (- (point) 2))
5437                                     (point))))
5438             ;; Subject.
5439             (progn
5440               (goto-char p)
5441               (if (search-forward "\nsubject:" nil t)
5442                   (funcall gnus-decode-encoded-word-function
5443                            (nnheader-header-value))
5444                 "(none)"))
5445             ;; From.
5446             (progn
5447               (goto-char p)
5448               (if (search-forward "\nfrom:" nil t)
5449                   (funcall gnus-decode-encoded-word-function
5450                            (nnheader-header-value))
5451                 "(nobody)"))
5452             ;; Date.
5453             (progn
5454               (goto-char p)
5455               (if (search-forward "\ndate:" nil t)
5456                   (nnheader-header-value) ""))
5457             ;; Message-ID.
5458             (progn
5459               (goto-char p)
5460               (setq id (if (re-search-forward
5461                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5462                            ;; We do it this way to make sure the Message-ID
5463                            ;; is (somewhat) syntactically valid.
5464                            (buffer-substring (match-beginning 1)
5465                                              (match-end 1))
5466                          ;; If there was no message-id, we just fake one
5467                          ;; to make subsequent routines simpler.
5468                          (nnheader-generate-fake-message-id))))
5469             ;; References.
5470             (progn
5471               (goto-char p)
5472               (if (search-forward "\nreferences:" nil t)
5473                   (progn
5474                     (setq end (point))
5475                     (prog1
5476                         (nnheader-header-value)
5477                       (setq ref
5478                             (buffer-substring
5479                              (progn
5480                                (end-of-line)
5481                                (search-backward ">" end t)
5482                                (1+ (point)))
5483                              (progn
5484                                (search-backward "<" end t)
5485                                (point))))))
5486                 ;; Get the references from the in-reply-to header if there
5487                 ;; were no references and the in-reply-to header looks
5488                 ;; promising.
5489                 (if (and (search-forward "\nin-reply-to:" nil t)
5490                          (setq in-reply-to (nnheader-header-value))
5491                          (string-match "<[^>]+>" in-reply-to))
5492                     (let (ref2)
5493                       (setq ref (substring in-reply-to (match-beginning 0)
5494                                            (match-end 0)))
5495                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5496                         (setq ref2 (substring in-reply-to (match-beginning 0)
5497                                               (match-end 0)))
5498                         (when (> (length ref2) (length ref))
5499                           (setq ref ref2)))
5500                       ref)
5501                   (setq ref nil))))
5502             ;; Chars.
5503             (progn
5504               (goto-char p)
5505               (if (search-forward "\nchars: " nil t)
5506                   (if (numberp (setq chars (ignore-errors (read cur))))
5507                       chars -1)
5508                 -1))
5509             ;; Lines.
5510             (progn
5511               (goto-char p)
5512               (if (search-forward "\nlines: " nil t)
5513                   (if (numberp (setq lines (ignore-errors (read cur))))
5514                       lines -1)
5515                 -1))
5516             ;; Xref.
5517             (progn
5518               (goto-char p)
5519               (and (search-forward "\nxref:" nil t)
5520                    (nnheader-header-value)))
5521             ;; Extra.
5522             (when gnus-extra-headers
5523               (let ((extra gnus-extra-headers)
5524                     out)
5525                 (while extra
5526                   (goto-char p)
5527                   (when (search-forward
5528                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5529                     (push (cons (car extra) (nnheader-header-value))
5530                           out))
5531                   (pop extra))
5532                 out))))
5533           (when (equal id ref)
5534             (setq ref nil))
5535
5536           (when gnus-alter-header-function
5537             (funcall gnus-alter-header-function header)
5538             (setq id (mail-header-id header)
5539                   ref (gnus-parent-id (mail-header-references header))))
5540
5541           (when (setq header
5542                       (gnus-dependencies-add-header
5543                        header dependencies force-new))
5544             (push header headers))
5545           (goto-char (point-max))
5546           (widen))
5547         (nreverse headers)))))
5548
5549 ;; Goes through the xover lines and returns a list of vectors
5550 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5551                                                   force-new dependencies
5552                                                   group also-fetch-heads)
5553   "Parse the news overview data in the server buffer.
5554 Return a list of headers that match SEQUENCE (see
5555 `nntp-retrieve-headers')."
5556   ;; Get the Xref when the users reads the articles since most/some
5557   ;; NNTP servers do not include Xrefs when using XOVER.
5558   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5559   (let ((mail-parse-charset gnus-newsgroup-charset)
5560         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5561         (cur nntp-server-buffer)
5562         (dependencies (or dependencies gnus-newsgroup-dependencies))
5563         (allp (cond
5564                ((eq gnus-read-all-available-headers t)
5565                 t)
5566                ((stringp gnus-read-all-available-headers)
5567                 (string-match gnus-read-all-available-headers group))
5568                (t
5569                 nil)))
5570         number headers header)
5571     (save-excursion
5572       (set-buffer nntp-server-buffer)
5573       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5574       ;; Allow the user to mangle the headers before parsing them.
5575       (gnus-run-hooks 'gnus-parse-headers-hook)
5576       (goto-char (point-min))
5577       (gnus-parse-without-error
5578         (while (and (or sequence allp)
5579                     (not (eobp)))
5580           (setq number (read cur))
5581           (when (not allp)
5582             (while (and sequence
5583                         (< (car sequence) number))
5584               (setq sequence (cdr sequence))))
5585           (when (and (or allp
5586                          (and sequence
5587                               (eq number (car sequence))))
5588                      (progn
5589                        (setq sequence (cdr sequence))
5590                        (setq header (inline
5591                                       (gnus-nov-parse-line
5592                                        number dependencies force-new)))))
5593             (push header headers))
5594           (forward-line 1)))
5595       ;; A common bug in inn is that if you have posted an article and
5596       ;; then retrieves the active file, it will answer correctly --
5597       ;; the new article is included.  However, a NOV entry for the
5598       ;; article may not have been generated yet, so this may fail.
5599       ;; We work around this problem by retrieving the last few
5600       ;; headers using HEAD.
5601       (if (or (not also-fetch-heads)
5602               (not sequence))
5603           ;; We (probably) got all the headers.
5604           (nreverse headers)
5605         (let ((gnus-nov-is-evil t))
5606           (nconc
5607            (nreverse headers)
5608            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5609              (gnus-get-newsgroup-headers))))))))
5610
5611 (defun gnus-article-get-xrefs ()
5612   "Fill in the Xref value in `gnus-current-headers', if necessary.
5613 This is meant to be called in `gnus-article-internal-prepare-hook'."
5614   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5615                                  gnus-current-headers)))
5616     (or (not gnus-use-cross-reference)
5617         (not headers)
5618         (and (mail-header-xref headers)
5619              (not (string= (mail-header-xref headers) "")))
5620         (let ((case-fold-search t)
5621               xref)
5622           (save-restriction
5623             (nnheader-narrow-to-headers)
5624             (goto-char (point-min))
5625             (when (or (and (not (eobp))
5626                            (eq (downcase (char-after)) ?x)
5627                            (looking-at "Xref:"))
5628                       (search-forward "\nXref:" nil t))
5629               (goto-char (1+ (match-end 0)))
5630               (setq xref (buffer-substring (point)
5631                                            (progn (end-of-line) (point))))
5632               (mail-header-set-xref headers xref)))))))
5633
5634 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5635   "Find article ID and insert the summary line for that article.
5636 OLD-HEADER can either be a header or a line number to insert
5637 the subject line on."
5638   (let* ((line (and (numberp old-header) old-header))
5639          (old-header (and (vectorp old-header) old-header))
5640          (header (cond ((and old-header use-old-header)
5641                         old-header)
5642                        ((and (numberp id)
5643                              (gnus-number-to-header id))
5644                         (gnus-number-to-header id))
5645                        (t
5646                         (gnus-read-header id))))
5647          (number (and (numberp id) id))
5648          d)
5649     (when header
5650       ;; Rebuild the thread that this article is part of and go to the
5651       ;; article we have fetched.
5652       (when (and (not gnus-show-threads)
5653                  old-header)
5654         (when (and number
5655                    (setq d (gnus-data-find (mail-header-number old-header))))
5656           (goto-char (gnus-data-pos d))
5657           (gnus-data-remove
5658            number
5659            (- (gnus-point-at-bol)
5660               (prog1
5661                   (1+ (gnus-point-at-eol))
5662                 (gnus-delete-line))))))
5663       (when old-header
5664         (mail-header-set-number header (mail-header-number old-header)))
5665       (setq gnus-newsgroup-sparse
5666             (delq (setq number (mail-header-number header))
5667                   gnus-newsgroup-sparse))
5668       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5669       (push number gnus-newsgroup-limit)
5670       (gnus-rebuild-thread (mail-header-id header) line)
5671       (gnus-summary-goto-subject number nil t))
5672     (when (and (numberp number)
5673                (> number 0))
5674       ;; We have to update the boundaries even if we can't fetch the
5675       ;; article if ID is a number -- so that the next `P' or `N'
5676       ;; command will fetch the previous (or next) article even
5677       ;; if the one we tried to fetch this time has been canceled.
5678       (when (> number gnus-newsgroup-end)
5679         (setq gnus-newsgroup-end number))
5680       (when (< number gnus-newsgroup-begin)
5681         (setq gnus-newsgroup-begin number))
5682       (setq gnus-newsgroup-unselected
5683             (delq number gnus-newsgroup-unselected)))
5684     ;; Report back a success?
5685     (and header (mail-header-number header))))
5686
5687 ;;; Process/prefix in the summary buffer
5688
5689 (defun gnus-summary-work-articles (n)
5690   "Return a list of articles to be worked upon.
5691 The prefix argument, the list of process marked articles, and the
5692 current article will be taken into consideration."
5693   (save-excursion
5694     (set-buffer gnus-summary-buffer)
5695     (cond
5696      (n
5697       ;; A numerical prefix has been given.
5698       (setq n (prefix-numeric-value n))
5699       (let ((backward (< n 0))
5700             (n (abs (prefix-numeric-value n)))
5701             articles article)
5702         (save-excursion
5703           (while
5704               (and (> n 0)
5705                    (push (setq article (gnus-summary-article-number))
5706                          articles)
5707                    (if backward
5708                        (gnus-summary-find-prev nil article)
5709                      (gnus-summary-find-next nil article)))
5710             (decf n)))
5711         (nreverse articles)))
5712      ((and (gnus-region-active-p) (mark))
5713       (message "region active")
5714       ;; Work on the region between point and mark.
5715       (let ((max (max (point) (mark)))
5716             articles article)
5717         (save-excursion
5718           (goto-char (min (min (point) (mark))))
5719           (while
5720               (and
5721                (push (setq article (gnus-summary-article-number)) articles)
5722                (gnus-summary-find-next nil article)
5723                (< (point) max)))
5724           (nreverse articles))))
5725      (gnus-newsgroup-processable
5726       ;; There are process-marked articles present.
5727       ;; Save current state.
5728       (gnus-summary-save-process-mark)
5729       ;; Return the list.
5730       (reverse gnus-newsgroup-processable))
5731      (t
5732       ;; Just return the current article.
5733       (list (gnus-summary-article-number))))))
5734
5735 (defmacro gnus-summary-iterate (arg &rest forms)
5736   "Iterate over the process/prefixed articles and do FORMS.
5737 ARG is the interactive prefix given to the command.  FORMS will be
5738 executed with point over the summary line of the articles."
5739   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5740     `(let ((,articles (gnus-summary-work-articles ,arg)))
5741        (while ,articles
5742          (gnus-summary-goto-subject (car ,articles))
5743          ,@forms
5744          (pop ,articles)))))
5745
5746 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5747 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5748
5749 (defun gnus-summary-save-process-mark ()
5750   "Push the current set of process marked articles on the stack."
5751   (interactive)
5752   (push (copy-sequence gnus-newsgroup-processable)
5753         gnus-newsgroup-process-stack))
5754
5755 (defun gnus-summary-kill-process-mark ()
5756   "Push the current set of process marked articles on the stack and unmark."
5757   (interactive)
5758   (gnus-summary-save-process-mark)
5759   (gnus-summary-unmark-all-processable))
5760
5761 (defun gnus-summary-yank-process-mark ()
5762   "Pop the last process mark state off the stack and restore it."
5763   (interactive)
5764   (unless gnus-newsgroup-process-stack
5765     (error "Empty mark stack"))
5766   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5767
5768 (defun gnus-summary-process-mark-set (set)
5769   "Make SET into the current process marked articles."
5770   (gnus-summary-unmark-all-processable)
5771   (while set
5772     (gnus-summary-set-process-mark (pop set))))
5773
5774 ;;; Searching and stuff
5775
5776 (defun gnus-summary-search-group (&optional backward use-level)
5777   "Search for next unread newsgroup.
5778 If optional argument BACKWARD is non-nil, search backward instead."
5779   (save-excursion
5780     (set-buffer gnus-group-buffer)
5781     (when (gnus-group-search-forward
5782            backward nil (if use-level (gnus-group-group-level) nil))
5783       (gnus-group-group-name))))
5784
5785 (defun gnus-summary-best-group (&optional exclude-group)
5786   "Find the name of the best unread group.
5787 If EXCLUDE-GROUP, do not go to this group."
5788   (save-excursion
5789     (set-buffer gnus-group-buffer)
5790     (save-excursion
5791       (gnus-group-best-unread-group exclude-group))))
5792
5793 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5794   (if backward (gnus-summary-find-prev)
5795     (let* ((dummy (gnus-summary-article-intangible-p))
5796            (article (or article (gnus-summary-article-number)))
5797            (arts (gnus-data-find-list article))
5798            result)
5799       (when (and (not dummy)
5800                  (or (not gnus-summary-check-current)
5801                      (not unread)
5802                      (not (gnus-data-unread-p (car arts)))))
5803         (setq arts (cdr arts)))
5804       (when (setq result
5805                   (if unread
5806                       (progn
5807                         (while arts
5808                           (when (or (and undownloaded
5809                                          (eq gnus-undownloaded-mark
5810                                              (gnus-data-mark (car arts))))
5811                                     (gnus-data-unread-p (car arts)))
5812                             (setq result (car arts)
5813                                   arts nil))
5814                           (setq arts (cdr arts)))
5815                         result)
5816                     (car arts)))
5817         (goto-char (gnus-data-pos result))
5818         (gnus-data-number result)))))
5819
5820 (defun gnus-summary-find-prev (&optional unread article)
5821   (let* ((eobp (eobp))
5822          (article (or article (gnus-summary-article-number)))
5823          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5824          result)
5825     (when (and (not eobp)
5826                (or (not gnus-summary-check-current)
5827                    (not unread)
5828                    (not (gnus-data-unread-p (car arts)))))
5829       (setq arts (cdr arts)))
5830     (when (setq result
5831                 (if unread
5832                     (progn
5833                       (while arts
5834                         (when (gnus-data-unread-p (car arts))
5835                           (setq result (car arts)
5836                                 arts nil))
5837                         (setq arts (cdr arts)))
5838                       result)
5839                   (car arts)))
5840       (goto-char (gnus-data-pos result))
5841       (gnus-data-number result))))
5842
5843 (defun gnus-summary-find-subject (subject &optional unread backward article)
5844   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5845          (article (or article (gnus-summary-article-number)))
5846          (articles (gnus-data-list backward))
5847          (arts (gnus-data-find-list article articles))
5848          result)
5849     (when (or (not gnus-summary-check-current)
5850               (not unread)
5851               (not (gnus-data-unread-p (car arts))))
5852       (setq arts (cdr arts)))
5853     (while arts
5854       (and (or (not unread)
5855                (gnus-data-unread-p (car arts)))
5856            (vectorp (gnus-data-header (car arts)))
5857            (gnus-subject-equal
5858             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5859            (setq result (car arts)
5860                  arts nil))
5861       (setq arts (cdr arts)))
5862     (and result
5863          (goto-char (gnus-data-pos result))
5864          (gnus-data-number result))))
5865
5866 (defun gnus-summary-search-forward (&optional unread subject backward)
5867   "Search forward for an article.
5868 If UNREAD, look for unread articles.  If SUBJECT, look for
5869 articles with that subject.  If BACKWARD, search backward instead."
5870   (cond (subject (gnus-summary-find-subject subject unread backward))
5871         (backward (gnus-summary-find-prev unread))
5872         (t (gnus-summary-find-next unread))))
5873
5874 (defun gnus-recenter (&optional n)
5875   "Center point in window and redisplay frame.
5876 Also do horizontal recentering."
5877   (interactive "P")
5878   (when (and gnus-auto-center-summary
5879              (not (eq gnus-auto-center-summary 'vertical)))
5880     (gnus-horizontal-recenter))
5881   (recenter n))
5882
5883 (defun gnus-summary-recenter ()
5884   "Center point in the summary window.
5885 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5886 displayed, no centering will be performed."
5887   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5888 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5889   (interactive)
5890   (let* ((top (cond ((< (window-height) 4) 0)
5891                     ((< (window-height) 7) 1)
5892                     (t (if (numberp gnus-auto-center-summary)
5893                            gnus-auto-center-summary
5894                          2))))
5895          (height (1- (window-height)))
5896          (bottom (save-excursion (goto-char (point-max))
5897                                  (forward-line (- height))
5898                                  (point)))
5899          (window (get-buffer-window (current-buffer))))
5900     ;; The user has to want it.
5901     (when gnus-auto-center-summary
5902       (when (get-buffer-window gnus-article-buffer)
5903         ;; Only do recentering when the article buffer is displayed,
5904       ;; Set the window start to either `bottom', which is the biggest
5905         ;; possible valid number, or the second line from the top,
5906         ;; whichever is the least.
5907         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5908           (if (> bottom top-pos)
5909               ;; Keep the second line from the top visible
5910               (set-window-start window top-pos t)
5911             ;; Try to keep the bottom line visible; if it's partially
5912             ;; obscured, either scroll one more line to make it fully
5913             ;; visible, or revert to using TOP-POS.
5914             (save-excursion
5915               (goto-char (point-max))
5916               (forward-line -1)
5917               (let ((last-line-start (point)))
5918                 (goto-char bottom)
5919                 (set-window-start window (point) t)
5920                 (when (not (pos-visible-in-window-p last-line-start window))
5921                   (forward-line 1)
5922                   (set-window-start window (min (point) top-pos) t)))))))
5923       ;; Do horizontal recentering while we're at it.
5924       (when (and (get-buffer-window (current-buffer) t)
5925                  (not (eq gnus-auto-center-summary 'vertical)))
5926         (let ((selected (selected-window)))
5927           (select-window (get-buffer-window (current-buffer) t))
5928           (gnus-summary-position-point)
5929           (gnus-horizontal-recenter)
5930           (select-window selected))))))
5931
5932 (defun gnus-summary-jump-to-group (newsgroup)
5933   "Move point to NEWSGROUP in group mode buffer."
5934   ;; Keep update point of group mode buffer if visible.
5935   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5936       (save-window-excursion
5937         ;; Take care of tree window mode.
5938         (when (get-buffer-window gnus-group-buffer)
5939           (pop-to-buffer gnus-group-buffer))
5940         (gnus-group-jump-to-group newsgroup))
5941     (save-excursion
5942       ;; Take care of tree window mode.
5943       (if (get-buffer-window gnus-group-buffer)
5944           (pop-to-buffer gnus-group-buffer)
5945         (set-buffer gnus-group-buffer))
5946       (gnus-group-jump-to-group newsgroup))))
5947
5948 ;; This function returns a list of article numbers based on the
5949 ;; difference between the ranges of read articles in this group and
5950 ;; the range of active articles.
5951 (defun gnus-list-of-unread-articles (group)
5952   (let* ((read (gnus-info-read (gnus-get-info group)))
5953          (active (or (gnus-active group) (gnus-activate-group group)))
5954          (last (cdr active))
5955          first nlast unread)
5956     ;; If none are read, then all are unread.
5957     (if (not read)
5958         (setq first (car active))
5959       ;; If the range of read articles is a single range, then the
5960       ;; first unread article is the article after the last read
5961       ;; article.  Sounds logical, doesn't it?
5962       (if (and (not (listp (cdr read)))
5963                (or (< (car read) (car active))
5964                    (progn (setq read (list read))
5965                           nil)))
5966           (setq first (max (car active) (1+ (cdr read))))
5967         ;; `read' is a list of ranges.
5968         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5969                                   (caar read)))
5970                   1)
5971           (setq first (car active)))
5972         (while read
5973           (when first
5974             (while (< first nlast)
5975               (push first unread)
5976               (setq first (1+ first))))
5977           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5978           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5979           (setq read (cdr read)))))
5980     ;; And add the last unread articles.
5981     (while (<= first last)
5982       (push first unread)
5983       (setq first (1+ first)))
5984     ;; Return the list of unread articles.
5985     (delq 0 (nreverse unread))))
5986
5987 (defun gnus-list-of-read-articles (group)
5988   "Return a list of unread, unticked and non-dormant articles."
5989   (let* ((info (gnus-get-info group))
5990          (marked (gnus-info-marks info))
5991          (active (gnus-active group)))
5992     (and info active
5993          (gnus-list-range-difference
5994           (gnus-list-range-difference
5995            (gnus-sorted-complement
5996             (gnus-uncompress-range active)
5997             (gnus-list-of-unread-articles group))
5998            (cdr (assq 'dormant marked)))
5999           (cdr (assq 'tick marked))))))
6000
6001 ;; Various summary commands
6002
6003 (defun gnus-summary-select-article-buffer ()
6004   "Reconfigure windows to show article buffer."
6005   (interactive)
6006   (if (not (gnus-buffer-live-p gnus-article-buffer))
6007       (error "There is no article buffer for this summary buffer")
6008     (gnus-configure-windows 'article)
6009     (select-window (get-buffer-window gnus-article-buffer))))
6010
6011 (defun gnus-summary-universal-argument (arg)
6012   "Perform any operation on all articles that are process/prefixed."
6013   (interactive "P")
6014   (let ((articles (gnus-summary-work-articles arg))
6015         func article)
6016     (if (eq
6017          (setq
6018           func
6019           (key-binding
6020            (read-key-sequence
6021             (substitute-command-keys
6022              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6023          'undefined)
6024         (gnus-error 1 "Undefined key")
6025       (save-excursion
6026         (while articles
6027           (gnus-summary-goto-subject (setq article (pop articles)))
6028           (let (gnus-newsgroup-processable)
6029             (command-execute func))
6030           (gnus-summary-remove-process-mark article)))))
6031   (gnus-summary-position-point))
6032
6033 (defun gnus-summary-toggle-truncation (&optional arg)
6034   "Toggle truncation of summary lines.
6035 With arg, turn line truncation on iff arg is positive."
6036   (interactive "P")
6037   (setq truncate-lines
6038         (if (null arg) (not truncate-lines)
6039           (> (prefix-numeric-value arg) 0)))
6040   (redraw-display))
6041
6042 (defun gnus-summary-reselect-current-group (&optional all rescan)
6043   "Exit and then reselect the current newsgroup.
6044 The prefix argument ALL means to select all articles."
6045   (interactive "P")
6046   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6047     (error "Ephemeral groups can't be reselected"))
6048   (let ((current-subject (gnus-summary-article-number))
6049         (group gnus-newsgroup-name))
6050     (setq gnus-newsgroup-begin nil)
6051     (gnus-summary-exit)
6052     ;; We have to adjust the point of group mode buffer because
6053     ;; point was moved to the next unread newsgroup by exiting.
6054     (gnus-summary-jump-to-group group)
6055     (when rescan
6056       (save-excursion
6057         (gnus-group-get-new-news-this-group 1)))
6058     (gnus-group-read-group all t)
6059     (gnus-summary-goto-subject current-subject nil t)))
6060
6061 (defun gnus-summary-rescan-group (&optional all)
6062   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6063   (interactive "P")
6064   (gnus-summary-reselect-current-group all t))
6065
6066 (defun gnus-summary-update-info (&optional non-destructive)
6067   (save-excursion
6068     (let ((group gnus-newsgroup-name))
6069       (when group
6070         (when gnus-newsgroup-kill-headers
6071           (setq gnus-newsgroup-killed
6072                 (gnus-compress-sequence
6073                  (gnus-sorted-union
6074                   (gnus-list-range-intersection
6075                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6076                   gnus-newsgroup-unreads)
6077                  t)))
6078         (unless (listp (cdr gnus-newsgroup-killed))
6079           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6080         (let ((headers gnus-newsgroup-headers))
6081           ;; Set the new ranges of read articles.
6082           (save-excursion
6083             (set-buffer gnus-group-buffer)
6084             (gnus-undo-force-boundary))
6085           (gnus-update-read-articles
6086            group (gnus-sorted-union
6087                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6088           ;; Set the current article marks.
6089           (let ((gnus-newsgroup-scored
6090                  (if (and (not gnus-save-score)
6091                           (not non-destructive))
6092                      nil
6093                    gnus-newsgroup-scored)))
6094             (save-excursion
6095               (gnus-update-marks)))
6096           ;; Do the cross-ref thing.
6097           (when gnus-use-cross-reference
6098             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6099           ;; Do not switch windows but change the buffer to work.
6100           (set-buffer gnus-group-buffer)
6101           (unless (gnus-ephemeral-group-p group)
6102             (gnus-group-update-group group)))))))
6103
6104 (defun gnus-summary-save-newsrc (&optional force)
6105   "Save the current number of read/marked articles in the dribble buffer.
6106 The dribble buffer will then be saved.
6107 If FORCE (the prefix), also save the .newsrc file(s)."
6108   (interactive "P")
6109   (gnus-summary-update-info t)
6110   (if force
6111       (gnus-save-newsrc-file)
6112     (gnus-dribble-save)))
6113
6114 (defun gnus-summary-exit (&optional temporary)
6115   "Exit reading current newsgroup, and then return to group selection mode.
6116 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6117   (interactive)
6118   (gnus-set-global-variables)
6119   (when (gnus-buffer-live-p gnus-article-buffer)
6120     (save-excursion
6121       (set-buffer gnus-article-buffer)
6122       (mm-destroy-parts gnus-article-mime-handles)
6123       ;; Set it to nil for safety reason.
6124       (setq gnus-article-mime-handle-alist nil)
6125       (setq gnus-article-mime-handles nil)))
6126   (gnus-kill-save-kill-buffer)
6127   (gnus-async-halt-prefetch)
6128   (let* ((group gnus-newsgroup-name)
6129          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6130          (mode major-mode)
6131          (group-point nil)
6132          (buf (current-buffer)))
6133     (unless quit-config
6134       ;; Do adaptive scoring, and possibly save score files.
6135       (when gnus-newsgroup-adaptive
6136         (gnus-score-adaptive))
6137       (when gnus-use-scoring
6138         (gnus-score-save)))
6139     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6140     ;; If we have several article buffers, we kill them at exit.
6141     (unless gnus-single-article-buffer
6142       (gnus-kill-buffer gnus-original-article-buffer)
6143       (setq gnus-article-current nil))
6144     (when gnus-use-cache
6145       (gnus-cache-possibly-remove-articles)
6146       (gnus-cache-save-buffers))
6147     (gnus-async-prefetch-remove-group group)
6148     (when gnus-suppress-duplicates
6149       (gnus-dup-enter-articles))
6150     (when gnus-use-trees
6151       (gnus-tree-close group))
6152     (when gnus-use-cache
6153       (gnus-cache-write-active))
6154     ;; Remove entries for this group.
6155     (nnmail-purge-split-history (gnus-group-real-name group))
6156     ;; Make all changes in this group permanent.
6157     (unless quit-config
6158       (gnus-run-hooks 'gnus-exit-group-hook)
6159       (gnus-summary-update-info))
6160     (gnus-close-group group)
6161     ;; Make sure where we were, and go to next newsgroup.
6162     (set-buffer gnus-group-buffer)
6163     (unless quit-config
6164       (gnus-group-jump-to-group group))
6165     (gnus-run-hooks 'gnus-summary-exit-hook)
6166     (unless (or quit-config
6167                 ;; If this group has disappeared from the summary
6168                 ;; buffer, don't skip forwards.
6169                 (not (string= group (gnus-group-group-name))))
6170       (gnus-group-next-unread-group 1))
6171     (setq group-point (point))
6172     (if temporary
6173         nil                             ;Nothing to do.
6174       ;; If we have several article buffers, we kill them at exit.
6175       (unless gnus-single-article-buffer
6176         (gnus-kill-buffer gnus-article-buffer)
6177         (gnus-kill-buffer gnus-original-article-buffer)
6178         (setq gnus-article-current nil))
6179       (set-buffer buf)
6180       (if (not gnus-kill-summary-on-exit)
6181           (progn
6182             (gnus-deaden-summary)
6183             (setq mode nil))
6184        ;; We set all buffer-local variables to nil.  It is unclear why
6185         ;; this is needed, but if we don't, buffer-local variables are
6186         ;; not garbage-collected, it seems.  This would the lead to en
6187         ;; ever-growing Emacs.
6188         (gnus-summary-clear-local-variables)
6189         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6190           (gnus-summary-clear-local-variables))
6191         (when (get-buffer gnus-article-buffer)
6192           (bury-buffer gnus-article-buffer))
6193         ;; We clear the global counterparts of the buffer-local
6194         ;; variables as well, just to be on the safe side.
6195         (set-buffer gnus-group-buffer)
6196         (gnus-summary-clear-local-variables)
6197         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6198           (gnus-summary-clear-local-variables)))
6199       (setq gnus-current-select-method gnus-select-method)
6200       (pop-to-buffer gnus-group-buffer)
6201       (if (not quit-config)
6202           (progn
6203             (goto-char group-point)
6204             (gnus-configure-windows 'group 'force))
6205         (gnus-handle-ephemeral-exit quit-config))
6206       ;; Return to group mode buffer.
6207       (when (eq mode 'gnus-summary-mode)
6208         (gnus-kill-buffer buf))
6209       ;; Clear the current group name.
6210       (unless quit-config
6211         (setq gnus-newsgroup-name nil)))))
6212
6213 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6214 (defun gnus-summary-exit-no-update (&optional no-questions)
6215   "Quit reading current newsgroup without updating read article info."
6216   (interactive)
6217   (let* ((group gnus-newsgroup-name)
6218          (quit-config (gnus-group-quit-config group)))
6219     (when (or no-questions
6220               gnus-expert-user
6221               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6222       (gnus-async-halt-prefetch)
6223       (mapcar 'funcall
6224               (delq 'gnus-summary-expire-articles
6225                     (copy-sequence gnus-summary-prepare-exit-hook)))
6226       (when (gnus-buffer-live-p gnus-article-buffer)
6227         (save-excursion
6228           (set-buffer gnus-article-buffer)
6229           (mm-destroy-parts gnus-article-mime-handles)
6230           ;; Set it to nil for safety reason.
6231           (setq gnus-article-mime-handle-alist nil)
6232           (setq gnus-article-mime-handles nil)))
6233       ;; If we have several article buffers, we kill them at exit.
6234       (unless gnus-single-article-buffer
6235         (gnus-kill-buffer gnus-article-buffer)
6236         (gnus-kill-buffer gnus-original-article-buffer)
6237         (setq gnus-article-current nil))
6238       (if (not gnus-kill-summary-on-exit)
6239           (gnus-deaden-summary)
6240         (gnus-close-group group)
6241         (gnus-summary-clear-local-variables)
6242         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6243           (gnus-summary-clear-local-variables))
6244         (set-buffer gnus-group-buffer)
6245         (gnus-summary-clear-local-variables)
6246         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6247           (gnus-summary-clear-local-variables))
6248         (when (get-buffer gnus-summary-buffer)
6249           (kill-buffer gnus-summary-buffer)))
6250       (unless gnus-single-article-buffer
6251         (setq gnus-article-current nil))
6252       (when gnus-use-trees
6253         (gnus-tree-close group))
6254       (gnus-async-prefetch-remove-group group)
6255       (when (get-buffer gnus-article-buffer)
6256         (bury-buffer gnus-article-buffer))
6257       ;; Return to the group buffer.
6258       (gnus-configure-windows 'group 'force)
6259       ;; Clear the current group name.
6260       (setq gnus-newsgroup-name nil)
6261       (when (equal (gnus-group-group-name) group)
6262         (gnus-group-next-unread-group 1))
6263       (when quit-config
6264         (gnus-handle-ephemeral-exit quit-config)))))
6265
6266 (defun gnus-handle-ephemeral-exit (quit-config)
6267   "Handle movement when leaving an ephemeral group.
6268 The state which existed when entering the ephemeral is reset."
6269   (if (not (buffer-name (car quit-config)))
6270       (gnus-configure-windows 'group 'force)
6271     (set-buffer (car quit-config))
6272     (cond ((eq major-mode 'gnus-summary-mode)
6273            (gnus-set-global-variables))
6274           ((eq major-mode 'gnus-article-mode)
6275            (save-excursion
6276              ;; The `gnus-summary-buffer' variable may point
6277              ;; to the old summary buffer when using a single
6278              ;; article buffer.
6279              (unless (gnus-buffer-live-p gnus-summary-buffer)
6280                (set-buffer gnus-group-buffer))
6281              (set-buffer gnus-summary-buffer)
6282              (gnus-set-global-variables))))
6283     (if (or (eq (cdr quit-config) 'article)
6284             (eq (cdr quit-config) 'pick))
6285         (progn
6286           ;; The current article may be from the ephemeral group
6287           ;; thus it is best that we reload this article
6288           (gnus-summary-show-article)
6289           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6290               (gnus-configure-windows 'pick 'force)
6291             (gnus-configure-windows (cdr quit-config) 'force)))
6292       (gnus-configure-windows (cdr quit-config) 'force))
6293     (when (eq major-mode 'gnus-summary-mode)
6294       (gnus-summary-next-subject 1 nil t)
6295       (gnus-summary-recenter)
6296       (gnus-summary-position-point))))
6297
6298 ;;; Dead summaries.
6299
6300 (defvar gnus-dead-summary-mode-map nil)
6301
6302 (unless gnus-dead-summary-mode-map
6303   (setq gnus-dead-summary-mode-map (make-keymap))
6304   (suppress-keymap gnus-dead-summary-mode-map)
6305   (substitute-key-definition
6306    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6307   (dolist (key '("\C-d" "\r" "\177" [delete]))
6308     (define-key gnus-dead-summary-mode-map
6309       key 'gnus-summary-wake-up-the-dead))
6310   (dolist (key '("q" "Q"))
6311     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6312
6313 (defvar gnus-dead-summary-mode nil
6314   "Minor mode for Gnus summary buffers.")
6315
6316 (defun gnus-dead-summary-mode (&optional arg)
6317   "Minor mode for Gnus summary buffers."
6318   (interactive "P")
6319   (when (eq major-mode 'gnus-summary-mode)
6320     (make-local-variable 'gnus-dead-summary-mode)
6321     (setq gnus-dead-summary-mode
6322           (if (null arg) (not gnus-dead-summary-mode)
6323             (> (prefix-numeric-value arg) 0)))
6324     (when gnus-dead-summary-mode
6325       (gnus-add-minor-mode
6326        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6327
6328 (defun gnus-deaden-summary ()
6329   "Make the current summary buffer into a dead summary buffer."
6330   ;; Kill any previous dead summary buffer.
6331   (when (and gnus-dead-summary
6332              (buffer-name gnus-dead-summary))
6333     (save-excursion
6334       (set-buffer gnus-dead-summary)
6335       (when gnus-dead-summary-mode
6336         (kill-buffer (current-buffer)))))
6337   ;; Make this the current dead summary.
6338   (setq gnus-dead-summary (current-buffer))
6339   (gnus-dead-summary-mode 1)
6340   (let ((name (buffer-name)))
6341     (when (string-match "Summary" name)
6342       (rename-buffer
6343        (concat (substring name 0 (match-beginning 0)) "Dead "
6344                (substring name (match-beginning 0)))
6345        t)
6346       (bury-buffer))))
6347
6348 (defun gnus-kill-or-deaden-summary (buffer)
6349   "Kill or deaden the summary BUFFER."
6350   (save-excursion
6351     (when (and (buffer-name buffer)
6352                (not gnus-single-article-buffer))
6353       (save-excursion
6354         (set-buffer buffer)
6355         (gnus-kill-buffer gnus-article-buffer)
6356         (gnus-kill-buffer gnus-original-article-buffer)))
6357     (cond
6358      ;; Kill the buffer.
6359      (gnus-kill-summary-on-exit
6360       (when (and gnus-use-trees
6361                  (gnus-buffer-exists-p buffer))
6362         (save-excursion
6363           (set-buffer buffer)
6364           (gnus-tree-close gnus-newsgroup-name)))
6365       (gnus-kill-buffer buffer))
6366      ;; Deaden the buffer.
6367      ((gnus-buffer-exists-p buffer)
6368       (save-excursion
6369         (set-buffer buffer)
6370         (gnus-deaden-summary))))))
6371
6372 (defun gnus-summary-wake-up-the-dead (&rest args)
6373   "Wake up the dead summary buffer."
6374   (interactive)
6375   (gnus-dead-summary-mode -1)
6376   (let ((name (buffer-name)))
6377     (when (string-match "Dead " name)
6378       (rename-buffer
6379        (concat (substring name 0 (match-beginning 0))
6380                (substring name (match-end 0)))
6381        t)))
6382   (gnus-message 3 "This dead summary is now alive again"))
6383
6384 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6385 (defun gnus-summary-fetch-faq (&optional faq-dir)
6386   "Fetch the FAQ for the current group.
6387 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6388 in."
6389   (interactive
6390    (list
6391     (when current-prefix-arg
6392       (completing-read
6393        "Faq dir: " (and (listp gnus-group-faq-directory)
6394                         (mapcar (lambda (file) (list file))
6395                                 gnus-group-faq-directory))))))
6396   (let (gnus-faq-buffer)
6397     (when (setq gnus-faq-buffer
6398                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6399       (gnus-configure-windows 'summary-faq))))
6400
6401 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6402 (defun gnus-summary-describe-group (&optional force)
6403   "Describe the current newsgroup."
6404   (interactive "P")
6405   (gnus-group-describe-group force gnus-newsgroup-name))
6406
6407 (defun gnus-summary-describe-briefly ()
6408   "Describe summary mode commands briefly."
6409   (interactive)
6410   (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")))
6411
6412 ;; Walking around group mode buffer from summary mode.
6413
6414 (defun gnus-summary-next-group (&optional no-article target-group backward)
6415   "Exit current newsgroup and then select next unread newsgroup.
6416 If prefix argument NO-ARTICLE is non-nil, no article is selected
6417 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6418 previous group instead."
6419   (interactive "P")
6420   ;; Stop pre-fetching.
6421   (gnus-async-halt-prefetch)
6422   (let ((current-group gnus-newsgroup-name)
6423         (current-buffer (current-buffer))
6424         entered)
6425    ;; First we semi-exit this group to update Xrefs and all variables.
6426     ;; We can't do a real exit, because the window conf must remain
6427     ;; the same in case the user is prompted for info, and we don't
6428     ;; want the window conf to change before that...
6429     (gnus-summary-exit t)
6430     (while (not entered)
6431       ;; Then we find what group we are supposed to enter.
6432       (set-buffer gnus-group-buffer)
6433       (gnus-group-jump-to-group current-group)
6434       (setq target-group
6435             (or target-group
6436                 (if (eq gnus-keep-same-level 'best)
6437                     (gnus-summary-best-group gnus-newsgroup-name)
6438                   (gnus-summary-search-group backward gnus-keep-same-level))))
6439       (if (not target-group)
6440           ;; There are no further groups, so we return to the group
6441           ;; buffer.
6442           (progn
6443             (gnus-message 5 "Returning to the group buffer")
6444             (setq entered t)
6445             (when (gnus-buffer-live-p current-buffer)
6446               (set-buffer current-buffer)
6447               (gnus-summary-exit))
6448             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6449         ;; We try to enter the target group.
6450         (gnus-group-jump-to-group target-group)
6451         (let ((unreads (gnus-group-group-unread)))
6452           (if (and (or (eq t unreads)
6453                        (and unreads (not (zerop unreads))))
6454                    (gnus-summary-read-group
6455                     target-group nil no-article
6456                     (and (buffer-name current-buffer) current-buffer)
6457                     nil backward))
6458               (setq entered t)
6459             (setq current-group target-group
6460                   target-group nil)))))))
6461
6462 (defun gnus-summary-prev-group (&optional no-article)
6463   "Exit current newsgroup and then select previous unread newsgroup.
6464 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6465   (interactive "P")
6466   (gnus-summary-next-group no-article nil t))
6467
6468 ;; Walking around summary lines.
6469
6470 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6471   "Go to the first unread subject.
6472 If UNREAD is non-nil, go to the first unread article.
6473 Returns the article selected or nil if there are no unread articles."
6474   (interactive "P")
6475   (prog1
6476       (cond
6477        ;; Empty summary.
6478        ((null gnus-newsgroup-data)
6479         (gnus-message 3 "No articles in the group")
6480         nil)
6481        ;; Pick the first article.
6482        ((not unread)
6483         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6484         (gnus-data-number (car gnus-newsgroup-data)))
6485        ;; No unread articles.
6486        ((null gnus-newsgroup-unreads)
6487         (gnus-message 3 "No more unread articles")
6488         nil)
6489        ;; Find the first unread article.
6490        (t
6491         (let ((data gnus-newsgroup-data))
6492           (while (and data
6493                       (and (not (and undownloaded
6494                                      (eq gnus-undownloaded-mark
6495                                          (gnus-data-mark (car data)))))
6496                            (if unseen
6497                                (or (not (memq
6498                                          (gnus-data-number (car data))
6499                                          gnus-newsgroup-unseen))
6500                                    (not (gnus-data-unread-p (car data))))
6501                              (not (gnus-data-unread-p (car data))))))
6502             (setq data (cdr data)))
6503           (when data
6504             (goto-char (gnus-data-pos (car data)))
6505             (gnus-data-number (car data))))))
6506     (gnus-summary-position-point)))
6507
6508 (defun gnus-summary-next-subject (n &optional unread dont-display)
6509   "Go to next N'th summary line.
6510 If N is negative, go to the previous N'th subject line.
6511 If UNREAD is non-nil, only unread articles are selected.
6512 The difference between N and the actual number of steps taken is
6513 returned."
6514   (interactive "p")
6515   (let ((backward (< n 0))
6516         (n (abs n)))
6517     (while (and (> n 0)
6518                 (if backward
6519                     (gnus-summary-find-prev unread)
6520                   (gnus-summary-find-next unread)))
6521       (unless (zerop (setq n (1- n)))
6522         (gnus-summary-show-thread)))
6523     (when (/= 0 n)
6524       (gnus-message 7 "No more%s articles"
6525                     (if unread " unread" "")))
6526     (unless dont-display
6527       (gnus-summary-recenter)
6528       (gnus-summary-position-point))
6529     n))
6530
6531 (defun gnus-summary-next-unread-subject (n)
6532   "Go to next N'th unread summary line."
6533   (interactive "p")
6534   (gnus-summary-next-subject n t))
6535
6536 (defun gnus-summary-prev-subject (n &optional unread)
6537   "Go to previous N'th summary line.
6538 If optional argument UNREAD is non-nil, only unread article is selected."
6539   (interactive "p")
6540   (gnus-summary-next-subject (- n) unread))
6541
6542 (defun gnus-summary-prev-unread-subject (n)
6543   "Go to previous N'th unread summary line."
6544   (interactive "p")
6545   (gnus-summary-next-subject (- n) t))
6546
6547 (defun gnus-summary-goto-subject (article &optional force silent)
6548   "Go the subject line of ARTICLE.
6549 If FORCE, also allow jumping to articles not currently shown."
6550   (interactive "nArticle number: ")
6551   (unless (numberp article)
6552     (error "Article %s is not a number" article))
6553   (let ((b (point))
6554         (data (gnus-data-find article)))
6555     ;; We read in the article if we have to.
6556     (and (not data)
6557          force
6558          (gnus-summary-insert-subject
6559           article
6560           (if (or (numberp force) (vectorp force)) force)
6561           t)
6562          (setq data (gnus-data-find article)))
6563     (goto-char b)
6564     (if (not data)
6565         (progn
6566           (unless silent
6567             (gnus-message 3 "Can't find article %d" article))
6568           nil)
6569       (let ((pt (gnus-data-pos data)))
6570         (goto-char pt)
6571         (gnus-summary-set-article-display-arrow pt))
6572       (gnus-summary-position-point)
6573       article)))
6574
6575 ;; Walking around summary lines with displaying articles.
6576
6577 (defun gnus-summary-expand-window (&optional arg)
6578   "Make the summary buffer take up the entire Emacs frame.
6579 Given a prefix, will force an `article' buffer configuration."
6580   (interactive "P")
6581   (if arg
6582       (gnus-configure-windows 'article 'force)
6583     (gnus-configure-windows 'summary 'force)))
6584
6585 (defun gnus-summary-display-article (article &optional all-header)
6586   "Display ARTICLE in article buffer."
6587   (when (gnus-buffer-live-p gnus-article-buffer)
6588     (with-current-buffer gnus-article-buffer
6589       (mm-enable-multibyte)))
6590   (gnus-set-global-variables)
6591   (when (gnus-buffer-live-p gnus-article-buffer)
6592     (with-current-buffer gnus-article-buffer
6593       (setq gnus-article-charset gnus-newsgroup-charset)
6594       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6595       (mm-enable-multibyte)))
6596   (if (null article)
6597       nil
6598     (prog1
6599         (if gnus-summary-display-article-function
6600             (funcall gnus-summary-display-article-function article all-header)
6601           (gnus-article-prepare article all-header))
6602       (gnus-run-hooks 'gnus-select-article-hook)
6603       (when (and gnus-current-article
6604                  (not (zerop gnus-current-article)))
6605         (gnus-summary-goto-subject gnus-current-article))
6606       (gnus-summary-recenter)
6607       (when (and gnus-use-trees gnus-show-threads)
6608         (gnus-possibly-generate-tree article)
6609         (gnus-highlight-selected-tree article))
6610       ;; Successfully display article.
6611       (gnus-article-set-window-start
6612        (cdr (assq article gnus-newsgroup-bookmarks))))))
6613
6614 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6615   "Select the current article.
6616 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6617 non-nil, the article will be re-fetched even if it already present in
6618 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6619 be displayed."
6620   ;; Make sure we are in the summary buffer to work around bbdb bug.
6621   (unless (eq major-mode 'gnus-summary-mode)
6622     (set-buffer gnus-summary-buffer))
6623   (let ((article (or article (gnus-summary-article-number)))
6624         (all-headers (not (not all-headers))) ;Must be t or nil.
6625         gnus-summary-display-article-function)
6626     (and (not pseudo)
6627          (gnus-summary-article-pseudo-p article)
6628          (error "This is a pseudo-article"))
6629     (save-excursion
6630       (set-buffer gnus-summary-buffer)
6631       (if (or (and gnus-single-article-buffer
6632                    (or (null gnus-current-article)
6633                        (null gnus-article-current)
6634                        (null (get-buffer gnus-article-buffer))
6635                        (not (eq article (cdr gnus-article-current)))
6636                        (not (equal (car gnus-article-current)
6637                                    gnus-newsgroup-name))))
6638               (and (not gnus-single-article-buffer)
6639                    (or (null gnus-current-article)
6640                        (not (eq gnus-current-article article))))
6641               force)
6642           ;; The requested article is different from the current article.
6643           (progn
6644             (gnus-summary-display-article article all-headers)
6645             (when (gnus-buffer-live-p gnus-article-buffer)
6646               (with-current-buffer gnus-article-buffer
6647                 (if (not gnus-article-decoded-p) ;; a local variable
6648                     (mm-disable-multibyte))))
6649             (gnus-article-set-window-start
6650              (cdr (assq article gnus-newsgroup-bookmarks)))
6651             article)
6652         'old))))
6653
6654 (defun gnus-summary-force-verify-and-decrypt ()
6655   (interactive)
6656   (let ((mm-verify-option 'known)
6657         (mm-decrypt-option 'known))
6658     (gnus-summary-select-article nil 'force)))
6659
6660 (defun gnus-summary-set-current-mark (&optional current-mark)
6661   "Obsolete function."
6662   nil)
6663
6664 (defun gnus-summary-next-article (&optional unread subject backward push)
6665   "Select the next article.
6666 If UNREAD, only unread articles are selected.
6667 If SUBJECT, only articles with SUBJECT are selected.
6668 If BACKWARD, the previous article is selected instead of the next."
6669   (interactive "P")
6670   (cond
6671    ;; Is there such an article?
6672    ((and (gnus-summary-search-forward unread subject backward)
6673          (or (gnus-summary-display-article (gnus-summary-article-number))
6674              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6675     (gnus-summary-position-point))
6676    ;; If not, we try the first unread, if that is wanted.
6677    ((and subject
6678          gnus-auto-select-same
6679          (gnus-summary-first-unread-article))
6680     (gnus-summary-position-point)
6681     (gnus-message 6 "Wrapped"))
6682    ;; Try to get next/previous article not displayed in this group.
6683    ((and gnus-auto-extend-newsgroup
6684          (not unread) (not subject))
6685     (gnus-summary-goto-article
6686      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6687      nil (count-lines (point-min) (point))))
6688    ;; Go to next/previous group.
6689    (t
6690     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6691       (gnus-summary-jump-to-group gnus-newsgroup-name))
6692     (let ((cmd last-command-char)
6693           (point
6694            (save-excursion
6695              (set-buffer gnus-group-buffer)
6696              (point)))
6697           (group
6698            (if (eq gnus-keep-same-level 'best)
6699                (gnus-summary-best-group gnus-newsgroup-name)
6700              (gnus-summary-search-group backward gnus-keep-same-level))))
6701       ;; For some reason, the group window gets selected.  We change
6702       ;; it back.
6703       (select-window (get-buffer-window (current-buffer)))
6704       ;; Select next unread newsgroup automagically.
6705       (cond
6706        ((or (not gnus-auto-select-next)
6707             (not cmd))
6708         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6709        ((or (eq gnus-auto-select-next 'quietly)
6710             (and (eq gnus-auto-select-next 'slightly-quietly)
6711                  push)
6712             (and (eq gnus-auto-select-next 'almost-quietly)
6713                  (gnus-summary-last-article-p)))
6714         ;; Select quietly.
6715         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6716             (gnus-summary-exit)
6717           (gnus-message 7 "No more%s articles (%s)..."
6718                         (if unread " unread" "")
6719                         (if group (concat "selecting " group)
6720                           "exiting"))
6721           (gnus-summary-next-group nil group backward)))
6722        (t
6723         (when (gnus-key-press-event-p last-input-event)
6724           (gnus-summary-walk-group-buffer
6725            gnus-newsgroup-name cmd unread backward point))))))))
6726
6727 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6728   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6729                       (?\C-p (gnus-group-prev-unread-group 1))))
6730         (cursor-in-echo-area t)
6731         keve key group ended)
6732     (save-excursion
6733       (set-buffer gnus-group-buffer)
6734       (goto-char start)
6735       (setq group
6736             (if (eq gnus-keep-same-level 'best)
6737                 (gnus-summary-best-group gnus-newsgroup-name)
6738               (gnus-summary-search-group backward gnus-keep-same-level))))
6739     (while (not ended)
6740       (gnus-message
6741        5 "No more%s articles%s" (if unread " unread" "")
6742        (if (and group
6743                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6744            (format " (Type %s for %s [%s])"
6745                    (single-key-description cmd) group
6746                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6747          (format " (Type %s to exit %s)"
6748                  (single-key-description cmd)
6749                  gnus-newsgroup-name)))
6750       ;; Confirm auto selection.
6751       (setq key (car (setq keve (gnus-read-event-char))))
6752       (setq ended t)
6753       (cond
6754        ((assq key keystrokes)
6755         (let ((obuf (current-buffer)))
6756           (switch-to-buffer gnus-group-buffer)
6757           (when group
6758             (gnus-group-jump-to-group group))
6759           (eval (cadr (assq key keystrokes)))
6760           (setq group (gnus-group-group-name))
6761           (switch-to-buffer obuf))
6762         (setq ended nil))
6763        ((equal key cmd)
6764         (if (or (not group)
6765                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6766             (gnus-summary-exit)
6767           (gnus-summary-next-group nil group backward)))
6768        (t
6769         (push (cdr keve) unread-command-events))))))
6770
6771 (defun gnus-summary-next-unread-article ()
6772   "Select unread article after current one."
6773   (interactive)
6774   (gnus-summary-next-article
6775    (or (not (eq gnus-summary-goto-unread 'never))
6776        (gnus-summary-last-article-p (gnus-summary-article-number)))
6777    (and gnus-auto-select-same
6778         (gnus-summary-article-subject))))
6779
6780 (defun gnus-summary-prev-article (&optional unread subject)
6781   "Select the article after the current one.
6782 If UNREAD is non-nil, only unread articles are selected."
6783   (interactive "P")
6784   (gnus-summary-next-article unread subject t))
6785
6786 (defun gnus-summary-prev-unread-article ()
6787   "Select unread article before current one."
6788   (interactive)
6789   (gnus-summary-prev-article
6790    (or (not (eq gnus-summary-goto-unread 'never))
6791        (gnus-summary-first-article-p (gnus-summary-article-number)))
6792    (and gnus-auto-select-same
6793         (gnus-summary-article-subject))))
6794
6795 (defun gnus-summary-next-page (&optional lines circular)
6796   "Show next page of the selected article.
6797 If at the end of the current article, select the next article.
6798 LINES says how many lines should be scrolled up.
6799
6800 If CIRCULAR is non-nil, go to the start of the article instead of
6801 selecting the next article when reaching the end of the current
6802 article."
6803   (interactive "P")
6804   (setq gnus-summary-buffer (current-buffer))
6805   (gnus-set-global-variables)
6806   (let ((article (gnus-summary-article-number))
6807         (article-window (get-buffer-window gnus-article-buffer t))
6808         endp)
6809     ;; If the buffer is empty, we have no article.
6810     (unless article
6811       (error "No article to select"))
6812     (gnus-configure-windows 'article)
6813     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6814         (if (and (eq gnus-summary-goto-unread 'never)
6815                  (not (gnus-summary-last-article-p article)))
6816             (gnus-summary-next-article)
6817           (gnus-summary-next-unread-article))
6818       (if (or (null gnus-current-article)
6819               (null gnus-article-current)
6820               (/= article (cdr gnus-article-current))
6821               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6822           ;; Selected subject is different from current article's.
6823           (gnus-summary-display-article article)
6824         (when article-window
6825           (gnus-eval-in-buffer-window gnus-article-buffer
6826             (setq endp (gnus-article-next-page lines)))
6827           (when endp
6828             (cond (circular
6829                    (gnus-summary-beginning-of-article))
6830                   (lines
6831                    (gnus-message 3 "End of message"))
6832                   ((null lines)
6833                    (if (and (eq gnus-summary-goto-unread 'never)
6834                             (not (gnus-summary-last-article-p article)))
6835                        (gnus-summary-next-article)
6836                      (gnus-summary-next-unread-article))))))))
6837     (gnus-summary-recenter)
6838     (gnus-summary-position-point)))
6839
6840 (defun gnus-summary-prev-page (&optional lines move)
6841   "Show previous page of selected article.
6842 Argument LINES specifies lines to be scrolled down.
6843 If MOVE, move to the previous unread article if point is at
6844 the beginning of the buffer."
6845   (interactive "P")
6846   (let ((article (gnus-summary-article-number))
6847         (article-window (get-buffer-window gnus-article-buffer t))
6848         endp)
6849     (gnus-configure-windows 'article)
6850     (if (or (null gnus-current-article)
6851             (null gnus-article-current)
6852             (/= article (cdr gnus-article-current))
6853             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6854         ;; Selected subject is different from current article's.
6855         (gnus-summary-display-article article)
6856       (gnus-summary-recenter)
6857       (when article-window
6858         (gnus-eval-in-buffer-window gnus-article-buffer
6859           (setq endp (gnus-article-prev-page lines)))
6860         (when (and move endp)
6861           (cond (lines
6862                  (gnus-message 3 "Beginning of message"))
6863                 ((null lines)
6864                  (if (and (eq gnus-summary-goto-unread 'never)
6865                           (not (gnus-summary-first-article-p article)))
6866                      (gnus-summary-prev-article)
6867                    (gnus-summary-prev-unread-article))))))))
6868   (gnus-summary-position-point))
6869
6870 (defun gnus-summary-prev-page-or-article (&optional lines)
6871   "Show previous page of selected article.
6872 Argument LINES specifies lines to be scrolled down.
6873 If at the beginning of the article, go to the next article."
6874   (interactive "P")
6875   (gnus-summary-prev-page lines t))
6876
6877 (defun gnus-summary-scroll-up (lines)
6878   "Scroll up (or down) one line current article.
6879 Argument LINES specifies lines to be scrolled up (or down if negative)."
6880   (interactive "p")
6881   (gnus-configure-windows 'article)
6882   (gnus-summary-show-thread)
6883   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6884     (gnus-eval-in-buffer-window gnus-article-buffer
6885       (cond ((> lines 0)
6886              (when (gnus-article-next-page lines)
6887                (gnus-message 3 "End of message")))
6888             ((< lines 0)
6889              (gnus-article-prev-page (- lines))))))
6890   (gnus-summary-recenter)
6891   (gnus-summary-position-point))
6892
6893 (defun gnus-summary-scroll-down (lines)
6894   "Scroll down (or up) one line current article.
6895 Argument LINES specifies lines to be scrolled down (or up if negative)."
6896   (interactive "p")
6897   (gnus-summary-scroll-up (- lines)))
6898
6899 (defun gnus-summary-next-same-subject ()
6900   "Select next article which has the same subject as current one."
6901   (interactive)
6902   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6903
6904 (defun gnus-summary-prev-same-subject ()
6905   "Select previous article which has the same subject as current one."
6906   (interactive)
6907   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6908
6909 (defun gnus-summary-next-unread-same-subject ()
6910   "Select next unread article which has the same subject as current one."
6911   (interactive)
6912   (gnus-summary-next-article t (gnus-summary-article-subject)))
6913
6914 (defun gnus-summary-prev-unread-same-subject ()
6915   "Select previous unread article which has the same subject as current one."
6916   (interactive)
6917   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6918
6919 (defun gnus-summary-first-unread-article ()
6920   "Select the first unread article.
6921 Return nil if there are no unread articles."
6922   (interactive)
6923   (prog1
6924       (when (gnus-summary-first-subject t)
6925         (gnus-summary-show-thread)
6926         (gnus-summary-first-subject t)
6927         (gnus-summary-display-article (gnus-summary-article-number)))
6928     (gnus-summary-position-point)))
6929
6930 (defun gnus-summary-first-unread-subject ()
6931   "Place the point on the subject line of the first unread article.
6932 Return nil if there are no unread articles."
6933   (interactive)
6934   (prog1
6935       (when (gnus-summary-first-subject t)
6936         (gnus-summary-show-thread)
6937         (gnus-summary-first-subject t))
6938     (gnus-summary-position-point)))
6939
6940 (defun gnus-summary-first-unseen-subject ()
6941   "Place the point on the subject line of the first unseen article.
6942 Return nil if there are no unseen articles."
6943   (interactive)
6944   (prog1
6945       (when (gnus-summary-first-subject t t t)
6946         (gnus-summary-show-thread)
6947         (gnus-summary-first-subject t t t))
6948     (gnus-summary-position-point)))
6949
6950 (defun gnus-summary-first-unseen-or-unread-subject ()
6951   "Place the point on the subject line of the first unseen article.
6952 Return nil if there are no unseen articles."
6953   (interactive)
6954   (prog1
6955       (unless (when (gnus-summary-first-subject t t t)
6956                 (gnus-summary-show-thread)
6957                 (gnus-summary-first-subject t t t))
6958         (when (gnus-summary-first-subject t)
6959           (gnus-summary-show-thread)
6960           (gnus-summary-first-subject t)))
6961     (gnus-summary-position-point)))
6962
6963 (defun gnus-summary-first-article ()
6964   "Select the first article.
6965 Return nil if there are no articles."
6966   (interactive)
6967   (prog1
6968       (when (gnus-summary-first-subject)
6969         (gnus-summary-show-thread)
6970         (gnus-summary-first-subject)
6971         (gnus-summary-display-article (gnus-summary-article-number)))
6972     (gnus-summary-position-point)))
6973
6974 (defun gnus-summary-best-unread-article (&optional arg)
6975   "Select the unread article with the highest score.
6976 If given a prefix argument, select the next unread article that has a
6977 score higher than the default score."
6978   (interactive "P")
6979   (let ((article (if arg
6980                      (gnus-summary-better-unread-subject)
6981                    (gnus-summary-best-unread-subject))))
6982     (if article
6983         (gnus-summary-goto-article article)
6984       (error "No unread articles"))))
6985
6986 (defun gnus-summary-best-unread-subject ()
6987   "Select the unread subject with the highest score."
6988   (interactive)
6989   (let ((best -1000000)
6990         (data gnus-newsgroup-data)
6991         article score)
6992     (while data
6993       (and (gnus-data-unread-p (car data))
6994            (> (setq score
6995                     (gnus-summary-article-score (gnus-data-number (car data))))
6996               best)
6997            (setq best score
6998                  article (gnus-data-number (car data))))
6999       (setq data (cdr data)))
7000     (when article
7001       (gnus-summary-goto-subject article))
7002     (gnus-summary-position-point)
7003     article))
7004
7005 (defun gnus-summary-better-unread-subject ()
7006   "Select the first unread subject that has a score over the default score."
7007   (interactive)
7008   (let ((data gnus-newsgroup-data)
7009         article score)
7010     (while (and (setq article (gnus-data-number (car data)))
7011                 (or (gnus-data-read-p (car data))
7012                     (not (> (gnus-summary-article-score article)
7013                             gnus-summary-default-score))))
7014       (setq data (cdr data)))
7015     (when article
7016       (gnus-summary-goto-subject article))
7017     (gnus-summary-position-point)
7018     article))
7019
7020 (defun gnus-summary-last-subject ()
7021   "Go to the last displayed subject line in the group."
7022   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7023     (when article
7024       (gnus-summary-goto-subject article))))
7025
7026 (defun gnus-summary-goto-article (article &optional all-headers force)
7027   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7028 If ALL-HEADERS is non-nil, no header lines are hidden.
7029 If FORCE, go to the article even if it isn't displayed.  If FORCE
7030 is a number, it is the line the article is to be displayed on."
7031   (interactive
7032    (list
7033     (completing-read
7034      "Article number or Message-ID: "
7035      (mapcar (lambda (number) (list (int-to-string number)))
7036              gnus-newsgroup-limit))
7037     current-prefix-arg
7038     t))
7039   (prog1
7040       (if (and (stringp article)
7041                (string-match "@" article))
7042           (gnus-summary-refer-article article)
7043         (when (stringp article)
7044           (setq article (string-to-number article)))
7045         (if (gnus-summary-goto-subject article force)
7046             (gnus-summary-display-article article all-headers)
7047           (gnus-message 4 "Couldn't go to article %s" article) nil))
7048     (gnus-summary-position-point)))
7049
7050 (defun gnus-summary-goto-last-article ()
7051   "Go to the previously read article."
7052   (interactive)
7053   (prog1
7054       (when gnus-last-article
7055         (gnus-summary-goto-article gnus-last-article nil t))
7056     (gnus-summary-position-point)))
7057
7058 (defun gnus-summary-pop-article (number)
7059   "Pop one article off the history and go to the previous.
7060 NUMBER articles will be popped off."
7061   (interactive "p")
7062   (let (to)
7063     (setq gnus-newsgroup-history
7064           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7065     (if to
7066         (gnus-summary-goto-article (car to) nil t)
7067       (error "Article history empty")))
7068   (gnus-summary-position-point))
7069
7070 ;; Summary commands and functions for limiting the summary buffer.
7071
7072 (defun gnus-summary-limit-to-articles (n)
7073   "Limit the summary buffer to the next N articles.
7074 If not given a prefix, use the process marked articles instead."
7075   (interactive "P")
7076   (prog1
7077       (let ((articles (gnus-summary-work-articles n)))
7078         (setq gnus-newsgroup-processable nil)
7079         (gnus-summary-limit articles))
7080     (gnus-summary-position-point)))
7081
7082 (defun gnus-summary-pop-limit (&optional total)
7083   "Restore the previous limit.
7084 If given a prefix, remove all limits."
7085   (interactive "P")
7086   (when total
7087     (setq gnus-newsgroup-limits
7088           (list (mapcar (lambda (h) (mail-header-number h))
7089                         gnus-newsgroup-headers))))
7090   (unless gnus-newsgroup-limits
7091     (error "No limit to pop"))
7092   (prog1
7093       (gnus-summary-limit nil 'pop)
7094     (gnus-summary-position-point)))
7095
7096 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7097   "Limit the summary buffer to articles that have subjects that match a regexp.
7098 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7099   (interactive
7100    (list (read-string (if current-prefix-arg
7101                           "Exclude subject (regexp): "
7102                         "Limit to subject (regexp): "))
7103          nil current-prefix-arg))
7104   (unless header
7105     (setq header "subject"))
7106   (when (not (equal "" subject))
7107     (prog1
7108         (let ((articles (gnus-summary-find-matching
7109                          (or header "subject") subject 'all nil nil
7110                          not-matching)))
7111           (unless articles
7112             (error "Found no matches for \"%s\"" subject))
7113           (gnus-summary-limit articles))
7114       (gnus-summary-position-point))))
7115
7116 (defun gnus-summary-limit-to-author (from &optional not-matching)
7117   "Limit the summary buffer to articles that have authors that match a regexp.
7118 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7119   (interactive
7120    (list (read-string (if current-prefix-arg
7121                           "Exclude author (regexp): "
7122                         "Limit to author (regexp): "))
7123          current-prefix-arg))
7124   (gnus-summary-limit-to-subject from "from" not-matching))
7125
7126 (defun gnus-summary-limit-to-age (age &optional younger-p)
7127   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7128 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7129 articles that are younger than AGE days."
7130   (interactive
7131    (let ((younger current-prefix-arg)
7132          (days-got nil)
7133          days)
7134      (while (not days-got)
7135        (setq days (if younger
7136                       (read-string "Limit to articles within (in days): ")
7137                     (read-string "Limit to articles older than (in days): ")))
7138        (when (> (length days) 0)
7139          (setq days (read days)))
7140        (if (numberp days)
7141            (progn
7142              (setq days-got t)
7143              (if (< days 0)
7144                  (progn
7145                    (setq younger (not younger))
7146                    (setq days (* days -1)))))
7147          (message "Please enter a number.")
7148          (sleep-for 1)))
7149      (list days younger)))
7150   (prog1
7151       (let ((data gnus-newsgroup-data)
7152             (cutoff (days-to-time age))
7153             articles d date is-younger)
7154         (while (setq d (pop data))
7155           (when (and (vectorp (gnus-data-header d))
7156                      (setq date (mail-header-date (gnus-data-header d))))
7157             (setq is-younger (time-less-p
7158                               (time-since (condition-case ()
7159                                               (date-to-time date)
7160                                             (error '(0 0))))
7161                               cutoff))
7162             (when (if younger-p
7163                       is-younger
7164                     (not is-younger))
7165               (push (gnus-data-number d) articles))))
7166         (gnus-summary-limit (nreverse articles)))
7167     (gnus-summary-position-point)))
7168
7169 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7170   "Limit the summary buffer to articles that match an 'extra' header."
7171   (interactive
7172    (let ((header
7173           (intern
7174            (gnus-completing-read-with-default
7175             (symbol-name (car gnus-extra-headers))
7176             (if current-prefix-arg
7177                 "Exclude extra header:"
7178               "Limit extra header:")
7179             (mapcar (lambda (x)
7180                       (cons (symbol-name x) x))
7181                     gnus-extra-headers)
7182             nil
7183             t))))
7184      (list header
7185            (read-string (format "%s header %s (regexp): "
7186                                 (if current-prefix-arg "Exclude" "Limit to")
7187                                 header))
7188            current-prefix-arg)))
7189   (when (not (equal "" regexp))
7190     (prog1
7191         (let ((articles (gnus-summary-find-matching
7192                          (cons 'extra header) regexp 'all nil nil
7193                          not-matching)))
7194           (unless articles
7195             (error "Found no matches for \"%s\"" regexp))
7196           (gnus-summary-limit articles))
7197       (gnus-summary-position-point))))
7198
7199 (defun gnus-summary-limit-to-display-predicate ()
7200   "Limit the summary buffer to the predicated in the `display' group parameter."
7201   (interactive)
7202   (unless gnus-newsgroup-display
7203     (error "There is no `display' group parameter"))
7204   (let (articles)
7205     (dolist (number gnus-newsgroup-articles)
7206       (when (funcall gnus-newsgroup-display)
7207         (push number articles)))
7208     (gnus-summary-limit articles))
7209   (gnus-summary-position-point))
7210
7211 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7212 (make-obsolete
7213  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7214
7215 (defun gnus-summary-limit-to-unread (&optional all)
7216   "Limit the summary buffer to articles that are not marked as read.
7217 If ALL is non-nil, limit strictly to unread articles."
7218   (interactive "P")
7219   (if all
7220       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7221     (gnus-summary-limit-to-marks
7222      ;; Concat all the marks that say that an article is read and have
7223      ;; those removed.
7224      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7225            gnus-killed-mark gnus-kill-file-mark
7226            gnus-low-score-mark gnus-expirable-mark
7227            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7228            gnus-duplicate-mark gnus-souped-mark)
7229      'reverse)))
7230
7231 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7232 (make-obsolete 'gnus-summary-delete-marked-with
7233                'gnus-summary-limit-exlude-marks)
7234
7235 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7236   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7237 If REVERSE, limit the summary buffer to articles that are marked
7238 with MARKS.  MARKS can either be a string of marks or a list of marks.
7239 Returns how many articles were removed."
7240   (interactive "sMarks: ")
7241   (gnus-summary-limit-to-marks marks t))
7242
7243 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7244   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7245 If REVERSE (the prefix), limit the summary buffer to articles that are
7246 not marked with MARKS.  MARKS can either be a string of marks or a
7247 list of marks.
7248 Returns how many articles were removed."
7249   (interactive "sMarks: \nP")
7250   (prog1
7251       (let ((data gnus-newsgroup-data)
7252             (marks (if (listp marks) marks
7253                      (append marks nil))) ; Transform to list.
7254             articles)
7255         (while data
7256           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7257                   (memq (gnus-data-mark (car data)) marks))
7258             (push (gnus-data-number (car data)) articles))
7259           (setq data (cdr data)))
7260         (gnus-summary-limit articles))
7261     (gnus-summary-position-point)))
7262
7263 (defun gnus-summary-limit-to-score (score)
7264   "Limit to articles with score at or above SCORE."
7265   (interactive "NLimit to articles with score of at least: ")
7266   (let ((data gnus-newsgroup-data)
7267         articles)
7268     (while data
7269       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7270                 score)
7271         (push (gnus-data-number (car data)) articles))
7272       (setq data (cdr data)))
7273     (prog1
7274         (gnus-summary-limit articles)
7275       (gnus-summary-position-point))))
7276
7277 (defun gnus-summary-limit-include-thread (id)
7278   "Display all the hidden articles that is in the thread with ID in it.
7279 When called interactively, ID is the Message-ID of the current
7280 article."
7281   (interactive (list (mail-header-id (gnus-summary-article-header))))
7282   (let ((articles (gnus-articles-in-thread
7283                    (gnus-id-to-thread (gnus-root-id id)))))
7284     (prog1
7285         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7286       (gnus-summary-limit-include-matching-articles
7287        "subject"
7288        (regexp-quote (gnus-simplify-subject-re
7289                       (mail-header-subject (gnus-id-to-header id)))))
7290       (gnus-summary-position-point))))
7291
7292 (defun gnus-summary-limit-include-matching-articles (header regexp)
7293   "Display all the hidden articles that have HEADERs that match REGEXP."
7294   (interactive (list (read-string "Match on header: ")
7295                      (read-string "Regexp: ")))
7296   (let ((articles (gnus-find-matching-articles header regexp)))
7297     (prog1
7298         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7299       (gnus-summary-position-point))))
7300
7301 (defun gnus-summary-limit-include-dormant ()
7302   "Display all the hidden articles that are marked as dormant.
7303 Note that this command only works on a subset of the articles currently
7304 fetched for this group."
7305   (interactive)
7306   (unless gnus-newsgroup-dormant
7307     (error "There are no dormant articles in this group"))
7308   (prog1
7309       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7310     (gnus-summary-position-point)))
7311
7312 (defun gnus-summary-limit-exclude-dormant ()
7313   "Hide all dormant articles."
7314   (interactive)
7315   (prog1
7316       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7317     (gnus-summary-position-point)))
7318
7319 (defun gnus-summary-limit-exclude-childless-dormant ()
7320   "Hide all dormant articles that have no children."
7321   (interactive)
7322   (let ((data (gnus-data-list t))
7323         articles d children)
7324     ;; Find all articles that are either not dormant or have
7325     ;; children.
7326     (while (setq d (pop data))
7327       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7328                 (and (setq children
7329                            (gnus-article-children (gnus-data-number d)))
7330                      (let (found)
7331                        (while children
7332                          (when (memq (car children) articles)
7333                            (setq children nil
7334                                  found t))
7335                          (pop children))
7336                        found)))
7337         (push (gnus-data-number d) articles)))
7338     ;; Do the limiting.
7339     (prog1
7340         (gnus-summary-limit articles)
7341       (gnus-summary-position-point))))
7342
7343 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7344   "Mark all unread excluded articles as read.
7345 If ALL, mark even excluded ticked and dormants as read."
7346   (interactive "P")
7347   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7348   (let ((articles (gnus-sorted-ndifference
7349                    (sort
7350                     (mapcar (lambda (h) (mail-header-number h))
7351                             gnus-newsgroup-headers)
7352                     '<)
7353                    gnus-newsgroup-limit))
7354         article)
7355     (setq gnus-newsgroup-unreads
7356           (gnus-sorted-intersection gnus-newsgroup-unreads
7357                                     gnus-newsgroup-limit))
7358     (if all
7359         (setq gnus-newsgroup-dormant nil
7360               gnus-newsgroup-marked nil
7361               gnus-newsgroup-reads
7362               (nconc
7363                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7364                gnus-newsgroup-reads))
7365       (while (setq article (pop articles))
7366         (unless (or (memq article gnus-newsgroup-dormant)
7367                     (memq article gnus-newsgroup-marked))
7368           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7369
7370 (defun gnus-summary-limit (articles &optional pop)
7371   (if pop
7372       ;; We pop the previous limit off the stack and use that.
7373       (setq articles (car gnus-newsgroup-limits)
7374             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7375     ;; We use the new limit, so we push the old limit on the stack.
7376     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7377   ;; Set the limit.
7378   (setq gnus-newsgroup-limit articles)
7379   (let ((total (length gnus-newsgroup-data))
7380         (data (gnus-data-find-list (gnus-summary-article-number)))
7381         (gnus-summary-mark-below nil)   ; Inhibit this.
7382         found)
7383     ;; This will do all the work of generating the new summary buffer
7384     ;; according to the new limit.
7385     (gnus-summary-prepare)
7386     ;; Hide any threads, possibly.
7387     (gnus-summary-maybe-hide-threads)
7388     ;; Try to return to the article you were at, or one in the
7389     ;; neighborhood.
7390     (when data
7391       ;; We try to find some article after the current one.
7392       (while data
7393         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7394           (setq data nil
7395                 found t))
7396         (setq data (cdr data))))
7397     (unless found
7398       ;; If there is no data, that means that we were after the last
7399       ;; article.  The same goes when we can't find any articles
7400       ;; after the current one.
7401       (goto-char (point-max))
7402       (gnus-summary-find-prev))
7403     (gnus-set-mode-line 'summary)
7404     ;; We return how many articles were removed from the summary
7405     ;; buffer as a result of the new limit.
7406     (- total (length gnus-newsgroup-data))))
7407
7408 (defsubst gnus-invisible-cut-children (threads)
7409   (let ((num 0))
7410     (while threads
7411       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7412         (incf num))
7413       (pop threads))
7414     (< num 2)))
7415
7416 (defsubst gnus-cut-thread (thread)
7417   "Go forwards in the thread until we find an article that we want to display."
7418   (when (or (eq gnus-fetch-old-headers 'some)
7419             (eq gnus-fetch-old-headers 'invisible)
7420             (numberp gnus-fetch-old-headers)
7421             (eq gnus-build-sparse-threads 'some)
7422             (eq gnus-build-sparse-threads 'more))
7423     ;; Deal with old-fetched headers and sparse threads.
7424     (while (and
7425             thread
7426             (or
7427              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7428              (gnus-summary-article-ancient-p
7429               (mail-header-number (car thread))))
7430             (if (or (<= (length (cdr thread)) 1)
7431                     (eq gnus-fetch-old-headers 'invisible))
7432                 (setq gnus-newsgroup-limit
7433                       (delq (mail-header-number (car thread))
7434                             gnus-newsgroup-limit)
7435                       thread (cadr thread))
7436               (when (gnus-invisible-cut-children (cdr thread))
7437                 (let ((th (cdr thread)))
7438                   (while th
7439                     (if (memq (mail-header-number (caar th))
7440                               gnus-newsgroup-limit)
7441                         (setq thread (car th)
7442                               th nil)
7443                       (setq th (cdr th))))))))))
7444   thread)
7445
7446 (defun gnus-cut-threads (threads)
7447   "Cut off all uninteresting articles from the beginning of threads."
7448   (when (or (eq gnus-fetch-old-headers 'some)
7449             (eq gnus-fetch-old-headers 'invisible)
7450             (numberp gnus-fetch-old-headers)
7451             (eq gnus-build-sparse-threads 'some)
7452             (eq gnus-build-sparse-threads 'more))
7453     (let ((th threads))
7454       (while th
7455         (setcar th (gnus-cut-thread (car th)))
7456         (setq th (cdr th)))))
7457   ;; Remove nixed out threads.
7458   (delq nil threads))
7459
7460 (defun gnus-summary-initial-limit (&optional show-if-empty)
7461   "Figure out what the initial limit is supposed to be on group entry.
7462 This entails weeding out unwanted dormants, low-scored articles,
7463 fetch-old-headers verbiage, and so on."
7464   ;; Most groups have nothing to remove.
7465   (if (or gnus-inhibit-limiting
7466           (and (null gnus-newsgroup-dormant)
7467                (eq gnus-newsgroup-display 'gnus-not-ignore)
7468                (not (eq gnus-fetch-old-headers 'some))
7469                (not (numberp gnus-fetch-old-headers))
7470                (not (eq gnus-fetch-old-headers 'invisible))
7471                (null gnus-summary-expunge-below)
7472                (not (eq gnus-build-sparse-threads 'some))
7473                (not (eq gnus-build-sparse-threads 'more))
7474                (null gnus-thread-expunge-below)
7475                (not gnus-use-nocem)))
7476       ()                                ; Do nothing.
7477     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7478     (setq gnus-newsgroup-limit nil)
7479     (mapatoms
7480      (lambda (node)
7481        (unless (car (symbol-value node))
7482          ;; These threads have no parents -- they are roots.
7483          (let ((nodes (cdr (symbol-value node)))
7484                thread)
7485            (while nodes
7486              (if (and gnus-thread-expunge-below
7487                       (< (gnus-thread-total-score (car nodes))
7488                          gnus-thread-expunge-below))
7489                  (gnus-expunge-thread (pop nodes))
7490                (setq thread (pop nodes))
7491                (gnus-summary-limit-children thread))))))
7492      gnus-newsgroup-dependencies)
7493     ;; If this limitation resulted in an empty group, we might
7494     ;; pop the previous limit and use it instead.
7495     (when (and (not gnus-newsgroup-limit)
7496                show-if-empty)
7497       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7498     gnus-newsgroup-limit))
7499
7500 (defun gnus-summary-limit-children (thread)
7501   "Return 1 if this subthread is visible and 0 if it is not."
7502   ;; First we get the number of visible children to this thread.  This
7503   ;; is done by recursing down the thread using this function, so this
7504   ;; will really go down to a leaf article first, before slowly
7505   ;; working its way up towards the root.
7506   (when thread
7507     (let ((children
7508            (if (cdr thread)
7509                (apply '+ (mapcar 'gnus-summary-limit-children
7510                                  (cdr thread)))
7511              0))
7512           (number (mail-header-number (car thread)))
7513           score)
7514       (if (and
7515            (not (memq number gnus-newsgroup-marked))
7516            (or
7517             ;; If this article is dormant and has absolutely no visible
7518             ;; children, then this article isn't visible.
7519             (and (memq number gnus-newsgroup-dormant)
7520                  (zerop children))
7521             ;; If this is "fetch-old-headered" and there is no
7522             ;; visible children, then we don't want this article.
7523             (and (or (eq gnus-fetch-old-headers 'some)
7524                      (numberp gnus-fetch-old-headers))
7525                  (gnus-summary-article-ancient-p number)
7526                  (zerop children))
7527             ;; If this is "fetch-old-headered" and `invisible', then
7528             ;; we don't want this article.
7529             (and (eq gnus-fetch-old-headers 'invisible)
7530                  (gnus-summary-article-ancient-p number))
7531             ;; If this is a sparsely inserted article with no children,
7532             ;; we don't want it.
7533             (and (eq gnus-build-sparse-threads 'some)
7534                  (gnus-summary-article-sparse-p number)
7535                  (zerop children))
7536             ;; If we use expunging, and this article is really
7537             ;; low-scored, then we don't want this article.
7538             (when (and gnus-summary-expunge-below
7539                        (< (setq score
7540                                 (or (cdr (assq number gnus-newsgroup-scored))
7541                                     gnus-summary-default-score))
7542                           gnus-summary-expunge-below))
7543               ;; We increase the expunge-tally here, but that has
7544               ;; nothing to do with the limits, really.
7545               (incf gnus-newsgroup-expunged-tally)
7546               ;; We also mark as read here, if that's wanted.
7547               (when (and gnus-summary-mark-below
7548                          (< score gnus-summary-mark-below))
7549                 (setq gnus-newsgroup-unreads
7550                       (delq number gnus-newsgroup-unreads))
7551                 (if gnus-newsgroup-auto-expire
7552                     (push number gnus-newsgroup-expirable)
7553                   (push (cons number gnus-low-score-mark)
7554                         gnus-newsgroup-reads)))
7555               t)
7556             ;; Do the `display' group parameter.
7557             (and gnus-newsgroup-display
7558                  (not (funcall gnus-newsgroup-display)))
7559             ;; Check NoCeM things.
7560             (if (and gnus-use-nocem
7561                      (gnus-nocem-unwanted-article-p
7562                       (mail-header-id (car thread))))
7563                 (progn
7564                   (setq gnus-newsgroup-unreads
7565                         (delq number gnus-newsgroup-unreads))
7566                   t))))
7567           ;; Nope, invisible article.
7568           0
7569         ;; Ok, this article is to be visible, so we add it to the limit
7570         ;; and return 1.
7571         (push number gnus-newsgroup-limit)
7572         1))))
7573
7574 (defun gnus-expunge-thread (thread)
7575   "Mark all articles in THREAD as read."
7576   (let* ((number (mail-header-number (car thread))))
7577     (incf gnus-newsgroup-expunged-tally)
7578     ;; We also mark as read here, if that's wanted.
7579     (setq gnus-newsgroup-unreads
7580           (delq number gnus-newsgroup-unreads))
7581     (if gnus-newsgroup-auto-expire
7582         (push number gnus-newsgroup-expirable)
7583       (push (cons number gnus-low-score-mark)
7584             gnus-newsgroup-reads)))
7585   ;; Go recursively through all subthreads.
7586   (mapcar 'gnus-expunge-thread (cdr thread)))
7587
7588 ;; Summary article oriented commands
7589
7590 (defun gnus-summary-refer-parent-article (n)
7591   "Refer parent article N times.
7592 If N is negative, go to ancestor -N instead.
7593 The difference between N and the number of articles fetched is returned."
7594   (interactive "p")
7595   (let ((skip 1)
7596         error header ref)
7597     (when (not (natnump n))
7598       (setq skip (abs n)
7599             n 1))
7600     (while (and (> n 0)
7601                 (not error))
7602       (setq header (gnus-summary-article-header))
7603       (if (and (eq (mail-header-number header)
7604                    (cdr gnus-article-current))
7605                (equal gnus-newsgroup-name
7606                       (car gnus-article-current)))
7607           ;; If we try to find the parent of the currently
7608           ;; displayed article, then we take a look at the actual
7609           ;; References header, since this is slightly more
7610           ;; reliable than the References field we got from the
7611           ;; server.
7612           (save-excursion
7613             (set-buffer gnus-original-article-buffer)
7614             (nnheader-narrow-to-headers)
7615             (unless (setq ref (message-fetch-field "references"))
7616               (setq ref (message-fetch-field "in-reply-to")))
7617             (widen))
7618         (setq ref
7619               ;; It's not the current article, so we take a bet on
7620               ;; the value we got from the server.
7621               (mail-header-references header)))
7622       (if (and ref
7623                (not (equal ref "")))
7624           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7625             (gnus-message 1 "Couldn't find parent"))
7626         (gnus-message 1 "No references in article %d"
7627                       (gnus-summary-article-number))
7628         (setq error t))
7629       (decf n))
7630     (gnus-summary-position-point)
7631     n))
7632
7633 (defun gnus-summary-refer-references ()
7634   "Fetch all articles mentioned in the References header.
7635 Return the number of articles fetched."
7636   (interactive)
7637   (let ((ref (mail-header-references (gnus-summary-article-header)))
7638         (current (gnus-summary-article-number))
7639         (n 0))
7640     (if (or (not ref)
7641             (equal ref ""))
7642         (error "No References in the current article")
7643       ;; For each Message-ID in the References header...
7644       (while (string-match "<[^>]*>" ref)
7645         (incf n)
7646         ;; ... fetch that article.
7647         (gnus-summary-refer-article
7648          (prog1 (match-string 0 ref)
7649            (setq ref (substring ref (match-end 0))))))
7650       (gnus-summary-goto-subject current)
7651       (gnus-summary-position-point)
7652       n)))
7653
7654 (defun gnus-summary-refer-thread (&optional limit)
7655   "Fetch all articles in the current thread.
7656 If LIMIT (the numerical prefix), fetch that many old headers instead
7657 of what's specified by the `gnus-refer-thread-limit' variable."
7658   (interactive "P")
7659   (let ((id (mail-header-id (gnus-summary-article-header)))
7660         (limit (if limit (prefix-numeric-value limit)
7661                  gnus-refer-thread-limit)))
7662     ;; We want to fetch LIMIT *old* headers, but we also have to
7663     ;; re-fetch all the headers in the current buffer, because many of
7664     ;; them may be undisplayed.  So we adjust LIMIT.
7665     (when (numberp limit)
7666       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7667     (unless (eq gnus-fetch-old-headers 'invisible)
7668       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7669       ;; Retrieve the headers and read them in.
7670       (if (eq (gnus-retrieve-headers
7671                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7672               'nov)
7673           (gnus-build-all-threads)
7674         (error "Can't fetch thread from backends that don't support NOV"))
7675       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7676     (gnus-summary-limit-include-thread id)))
7677
7678 (defun gnus-summary-refer-article (message-id)
7679   "Fetch an article specified by MESSAGE-ID."
7680   (interactive "sMessage-ID: ")
7681   (when (and (stringp message-id)
7682              (not (zerop (length message-id))))
7683     ;; Construct the correct Message-ID if necessary.
7684     ;; Suggested by tale@pawl.rpi.edu.
7685     (unless (string-match "^<" message-id)
7686       (setq message-id (concat "<" message-id)))
7687     (unless (string-match ">$" message-id)
7688       (setq message-id (concat message-id ">")))
7689     (let* ((header (gnus-id-to-header message-id))
7690            (sparse (and header
7691                         (gnus-summary-article-sparse-p
7692                          (mail-header-number header))
7693                         (memq (mail-header-number header)
7694                               gnus-newsgroup-limit)))
7695            number)
7696       (cond
7697        ;; If the article is present in the buffer we just go to it.
7698        ((and header
7699              (or (not (gnus-summary-article-sparse-p
7700                        (mail-header-number header)))
7701                  sparse))
7702         (prog1
7703             (gnus-summary-goto-article
7704              (mail-header-number header) nil t)
7705           (when sparse
7706             (gnus-summary-update-article (mail-header-number header)))))
7707        (t
7708         ;; We fetch the article.
7709         (catch 'found
7710           (dolist (gnus-override-method (gnus-refer-article-methods))
7711             (gnus-check-server gnus-override-method)
7712             ;; Fetch the header, and display the article.
7713             (when (setq number (gnus-summary-insert-subject message-id))
7714               (gnus-summary-select-article nil nil nil number)
7715               (throw 'found t)))
7716           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7717
7718 (defun gnus-refer-article-methods ()
7719   "Return a list of referrable methods."
7720   (cond
7721    ;; No method, so we default to current and native.
7722    ((null gnus-refer-article-method)
7723     (list gnus-current-select-method gnus-select-method))
7724    ;; Current.
7725    ((eq 'current gnus-refer-article-method)
7726     (list gnus-current-select-method))
7727    ;; List of select methods.
7728    ((not (and (symbolp (car gnus-refer-article-method))
7729               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7730     (let (out)
7731       (dolist (method gnus-refer-article-method)
7732         (push (if (eq 'current method)
7733                   gnus-current-select-method
7734                 method)
7735               out))
7736       (nreverse out)))
7737    ;; One single select method.
7738    (t
7739     (list gnus-refer-article-method))))
7740
7741 (defun gnus-summary-edit-parameters ()
7742   "Edit the group parameters of the current group."
7743   (interactive)
7744   (gnus-group-edit-group gnus-newsgroup-name 'params))
7745
7746 (defun gnus-summary-customize-parameters ()
7747   "Customize the group parameters of the current group."
7748   (interactive)
7749   (gnus-group-customize gnus-newsgroup-name))
7750
7751 (defun gnus-summary-enter-digest-group (&optional force)
7752   "Enter an nndoc group based on the current article.
7753 If FORCE, force a digest interpretation.  If not, try
7754 to guess what the document format is."
7755   (interactive "P")
7756   (let ((conf gnus-current-window-configuration))
7757     (save-excursion
7758       (gnus-summary-select-article))
7759     (setq gnus-current-window-configuration conf)
7760     (let* ((name (format "%s-%d"
7761                          (gnus-group-prefixed-name
7762                           gnus-newsgroup-name (list 'nndoc ""))
7763                          (save-excursion
7764                            (set-buffer gnus-summary-buffer)
7765                            gnus-current-article)))
7766            (ogroup gnus-newsgroup-name)
7767            (params (append (gnus-info-params (gnus-get-info ogroup))
7768                            (list (cons 'to-group ogroup))
7769                            (list (cons 'save-article-group ogroup))))
7770            (case-fold-search t)
7771            (buf (current-buffer))
7772            dig to-address)
7773       (save-excursion
7774         (set-buffer gnus-original-article-buffer)
7775         ;; Have the digest group inherit the main mail address of
7776         ;; the parent article.
7777         (when (setq to-address (or (message-fetch-field "reply-to")
7778                                    (message-fetch-field "from")))
7779           (setq params (append
7780                         (list (cons 'to-address
7781                                     (funcall gnus-decode-encoded-word-function
7782                                              to-address))))))
7783         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7784         (insert-buffer-substring gnus-original-article-buffer)
7785         ;; Remove lines that may lead nndoc to misinterpret the
7786         ;; document type.
7787         (narrow-to-region
7788          (goto-char (point-min))
7789          (or (search-forward "\n\n" nil t) (point)))
7790         (goto-char (point-min))
7791         (delete-matching-lines "^Path:\\|^From ")
7792         (widen))
7793       (unwind-protect
7794           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7795                     (gnus-newsgroup-ephemeral-ignored-charsets
7796                      gnus-newsgroup-ignored-charsets))
7797                 (gnus-group-read-ephemeral-group
7798                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7799                               (nndoc-article-type
7800                                ,(if force 'mbox 'guess)))
7801                  t nil nil nil
7802                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7803                                                         "ADAPT")))))
7804               ;; Make all postings to this group go to the parent group.
7805               (nconc (gnus-info-params (gnus-get-info name))
7806                      params)
7807             ;; Couldn't select this doc group.
7808             (switch-to-buffer buf)
7809             (gnus-set-global-variables)
7810             (gnus-configure-windows 'summary)
7811             (gnus-message 3 "Article couldn't be entered?"))
7812         (kill-buffer dig)))))
7813
7814 (defun gnus-summary-read-document (n)
7815   "Open a new group based on the current article(s).
7816 This will allow you to read digests and other similar
7817 documents as newsgroups.
7818 Obeys the standard process/prefix convention."
7819   (interactive "P")
7820   (let* ((articles (gnus-summary-work-articles n))
7821          (ogroup gnus-newsgroup-name)
7822          (params (append (gnus-info-params (gnus-get-info ogroup))
7823                          (list (cons 'to-group ogroup))))
7824          article group egroup groups vgroup)
7825     (while (setq article (pop articles))
7826       (setq group (format "%s-%d" gnus-newsgroup-name article))
7827       (gnus-summary-remove-process-mark article)
7828       (when (gnus-summary-display-article article)
7829         (save-excursion
7830           (with-temp-buffer
7831             (insert-buffer-substring gnus-original-article-buffer)
7832             ;; Remove some headers that may lead nndoc to make
7833             ;; the wrong guess.
7834             (message-narrow-to-head)
7835             (goto-char (point-min))
7836             (delete-matching-lines "^\\(Path\\):\\|^From ")
7837             (widen)
7838             (if (setq egroup
7839                       (gnus-group-read-ephemeral-group
7840                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7841                                      (nndoc-article-type guess))
7842                        t nil t))
7843                 (progn
7844             ;; Make all postings to this group go to the parent group.
7845                   (nconc (gnus-info-params (gnus-get-info egroup))
7846                          params)
7847                   (push egroup groups))
7848               ;; Couldn't select this doc group.
7849               (gnus-error 3 "Article couldn't be entered"))))))
7850     ;; Now we have selected all the documents.
7851     (cond
7852      ((not groups)
7853       (error "None of the articles could be interpreted as documents"))
7854      ((gnus-group-read-ephemeral-group
7855        (setq vgroup (format
7856                      "nnvirtual:%s-%s" gnus-newsgroup-name
7857                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7858        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7859        t
7860        (cons (current-buffer) 'summary)))
7861      (t
7862       (error "Couldn't select virtual nndoc group")))))
7863
7864 (defun gnus-summary-isearch-article (&optional regexp-p)
7865   "Do incremental search forward on the current article.
7866 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7867   (interactive "P")
7868   (gnus-summary-select-article)
7869   (gnus-configure-windows 'article)
7870   (gnus-eval-in-buffer-window gnus-article-buffer
7871     (save-restriction
7872       (widen)
7873       (isearch-forward regexp-p))))
7874
7875 (defun gnus-summary-search-article-forward (regexp &optional backward)
7876   "Search for an article containing REGEXP forward.
7877 If BACKWARD, search backward instead."
7878   (interactive
7879    (list (read-string
7880           (format "Search article %s (regexp%s): "
7881                   (if current-prefix-arg "backward" "forward")
7882                   (if gnus-last-search-regexp
7883                       (concat ", default " gnus-last-search-regexp)
7884                     "")))
7885          current-prefix-arg))
7886   (if (string-equal regexp "")
7887       (setq regexp (or gnus-last-search-regexp ""))
7888     (setq gnus-last-search-regexp regexp)
7889     (setq gnus-article-before-search gnus-current-article))
7890   ;; Intentionally set gnus-last-article.
7891   (setq gnus-last-article gnus-article-before-search)
7892   (let ((gnus-last-article gnus-last-article))
7893     (if (gnus-summary-search-article regexp backward)
7894         (gnus-summary-show-thread)
7895       (error "Search failed: \"%s\"" regexp))))
7896
7897 (defun gnus-summary-search-article-backward (regexp)
7898   "Search for an article containing REGEXP backward."
7899   (interactive
7900    (list (read-string
7901           (format "Search article backward (regexp%s): "
7902                   (if gnus-last-search-regexp
7903                       (concat ", default " gnus-last-search-regexp)
7904                     "")))))
7905   (gnus-summary-search-article-forward regexp 'backward))
7906
7907 (defun gnus-summary-search-article (regexp &optional backward)
7908   "Search for an article containing REGEXP.
7909 Optional argument BACKWARD means do search for backward.
7910 `gnus-select-article-hook' is not called during the search."
7911   ;; We have to require this here to make sure that the following
7912   ;; dynamic binding isn't shadowed by autoloading.
7913   (require 'gnus-async)
7914   (require 'gnus-art)
7915   (let ((gnus-select-article-hook nil)  ;Disable hook.
7916         (gnus-article-prepare-hook nil)
7917         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7918         (gnus-use-article-prefetch nil)
7919         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7920         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7921         (sum (current-buffer))
7922         (gnus-display-mime-function nil)
7923         (found nil)
7924         point)
7925     (gnus-save-hidden-threads
7926       (gnus-summary-select-article)
7927       (set-buffer gnus-article-buffer)
7928       (goto-char (window-point (get-buffer-window (current-buffer))))
7929       (when backward
7930         (forward-line -1))
7931       (while (not found)
7932         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7933         (if (if backward
7934                 (re-search-backward regexp nil t)
7935               (re-search-forward regexp nil t))
7936             ;; We found the regexp.
7937             (progn
7938               (setq found 'found)
7939               (beginning-of-line)
7940               (set-window-start
7941                (get-buffer-window (current-buffer))
7942                (point))
7943               (forward-line 1)
7944               (set-window-point
7945                (get-buffer-window (current-buffer))
7946                (point))
7947               (set-buffer sum)
7948               (setq point (point)))
7949           ;; We didn't find it, so we go to the next article.
7950           (set-buffer sum)
7951           (setq found 'not)
7952           (while (eq found 'not)
7953             (if (not (if backward (gnus-summary-find-prev)
7954                        (gnus-summary-find-next)))
7955                 ;; No more articles.
7956                 (setq found t)
7957               ;; Select the next article and adjust point.
7958               (unless (gnus-summary-article-sparse-p
7959                        (gnus-summary-article-number))
7960                 (setq found nil)
7961                 (gnus-summary-select-article)
7962                 (set-buffer gnus-article-buffer)
7963                 (widen)
7964                 (goto-char (if backward (point-max) (point-min))))))))
7965       (gnus-message 7 ""))
7966     ;; Return whether we found the regexp.
7967     (when (eq found 'found)
7968       (goto-char point)
7969       (gnus-summary-show-thread)
7970       (gnus-summary-goto-subject gnus-current-article)
7971       (gnus-summary-position-point)
7972       t)))
7973
7974 (defun gnus-find-matching-articles (header regexp)
7975   "Return a list of all articles that match REGEXP on HEADER.
7976 This search includes all articles in the current group that Gnus has
7977 fetched headers for, whether they are displayed or not."
7978   (let ((articles nil)
7979         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7980         (case-fold-search t))
7981     (dolist (header gnus-newsgroup-headers)
7982       (when (string-match regexp (funcall func header))
7983         (push (mail-header-number header) articles)))
7984     (nreverse articles)))
7985
7986 (defun gnus-summary-find-matching (header regexp &optional backward unread
7987                                           not-case-fold not-matching)
7988   "Return a list of all articles that match REGEXP on HEADER.
7989 The search stars on the current article and goes forwards unless
7990 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7991 If UNREAD is non-nil, only unread articles will
7992 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7993 in the comparisons. If NOT-MATCHING, return a list of all articles that
7994 not match REGEXP on HEADER."
7995   (let ((case-fold-search (not not-case-fold))
7996         articles d func)
7997     (if (consp header)
7998         (if (eq (car header) 'extra)
7999             (setq func
8000                   `(lambda (h)
8001                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8002                          "")))
8003           (error "%s is an invalid header" header))
8004       (unless (fboundp (intern (concat "mail-header-" header)))
8005         (error "%s is not a valid header" header))
8006       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8007     (dolist (d (if (eq backward 'all)
8008                    gnus-newsgroup-data
8009                  (gnus-data-find-list
8010                   (gnus-summary-article-number)
8011                   (gnus-data-list backward))))
8012       (when (and (or (not unread)       ; We want all articles...
8013                      (gnus-data-unread-p d)) ; Or just unreads.
8014                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8015                  (if not-matching
8016                      (not (string-match
8017                            regexp
8018                            (funcall func (gnus-data-header d))))
8019                    (string-match regexp
8020                                  (funcall func (gnus-data-header d)))))
8021         (push (gnus-data-number d) articles))) ; Success!
8022     (nreverse articles)))
8023
8024 (defun gnus-summary-execute-command (header regexp command &optional backward)
8025   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8026 If HEADER is an empty string (or nil), the match is done on the entire
8027 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8028   (interactive
8029    (list (let ((completion-ignore-case t))
8030            (completing-read
8031             "Header name: "
8032             (mapcar (lambda (header) (list (format "%s" header)))
8033                     (append
8034                      '("Number" "Subject" "From" "Lines" "Date"
8035                        "Message-ID" "Xref" "References" "Body")
8036                      gnus-extra-headers))
8037             nil 'require-match))
8038          (read-string "Regexp: ")
8039          (read-key-sequence "Command: ")
8040          current-prefix-arg))
8041   (when (equal header "Body")
8042     (setq header ""))
8043   ;; Hidden thread subtrees must be searched as well.
8044   (gnus-summary-show-all-threads)
8045   ;; We don't want to change current point nor window configuration.
8046   (save-excursion
8047     (save-window-excursion
8048       (gnus-message 6 "Executing %s..." (key-description command))
8049 ;; We'd like to execute COMMAND interactively so as to give arguments.
8050       (gnus-execute header regexp
8051                     `(call-interactively ',(key-binding command))
8052                     backward)
8053       (gnus-message 6 "Executing %s...done" (key-description command)))))
8054
8055 (defun gnus-summary-beginning-of-article ()
8056   "Scroll the article back to the beginning."
8057   (interactive)
8058   (gnus-summary-select-article)
8059   (gnus-configure-windows 'article)
8060   (gnus-eval-in-buffer-window gnus-article-buffer
8061     (widen)
8062     (goto-char (point-min))
8063     (when gnus-page-broken
8064       (gnus-narrow-to-page))))
8065
8066 (defun gnus-summary-end-of-article ()
8067   "Scroll to the end of the article."
8068   (interactive)
8069   (gnus-summary-select-article)
8070   (gnus-configure-windows 'article)
8071   (gnus-eval-in-buffer-window gnus-article-buffer
8072     (widen)
8073     (goto-char (point-max))
8074     (recenter -3)
8075     (when gnus-page-broken
8076       (gnus-narrow-to-page))))
8077
8078 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8079   "Truncate to LEN and quote all \"(\"'s in STRING."
8080   (gnus-replace-in-string (if (and len (> (length string) len))
8081                               (substring string 0 len)
8082                             string)
8083                           "[()]" "\\\\\\&"))
8084
8085 (defun gnus-summary-print-article (&optional filename n)
8086   "Generate and print a PostScript image of the N next (mail) articles.
8087
8088 If N is negative, print the N previous articles.  If N is nil and articles
8089 have been marked with the process mark, print these instead.
8090
8091 If the optional first argument FILENAME is nil, send the image to the
8092 printer.  If FILENAME is a string, save the PostScript image in a file with
8093 that name.  If FILENAME is a number, prompt the user for the name of the file
8094 to save in."
8095   (interactive (list (ps-print-preprint current-prefix-arg)))
8096   (dolist (article (gnus-summary-work-articles n))
8097     (gnus-summary-select-article nil nil 'pseudo article)
8098     (gnus-eval-in-buffer-window gnus-article-buffer
8099       (gnus-print-buffer))
8100     (gnus-summary-remove-process-mark article))
8101   (ps-despool filename))
8102
8103 (defun gnus-print-buffer ()
8104   (let ((buffer (generate-new-buffer " *print*")))
8105     (unwind-protect
8106         (progn
8107           (copy-to-buffer buffer (point-min) (point-max))
8108           (set-buffer buffer)
8109           (gnus-article-delete-invisible-text)
8110           (when (gnus-visual-p 'article-highlight 'highlight)
8111             ;; Copy-to-buffer doesn't copy overlay.  So redo
8112             ;; highlight.
8113             (let ((gnus-article-buffer buffer))
8114               (gnus-article-highlight-citation t)
8115               (gnus-article-highlight-signature)))
8116           (let ((ps-left-header
8117                  (list
8118                   (concat "("
8119                           (gnus-summary-print-truncate-and-quote
8120                            (mail-header-subject gnus-current-headers)
8121                            66) ")")
8122                   (concat "("
8123                           (gnus-summary-print-truncate-and-quote
8124                            (mail-header-from gnus-current-headers)
8125                            45) ")")))
8126                 (ps-right-header
8127                  (list
8128                   "/pagenumberstring load"
8129                   (concat "("
8130                           (mail-header-date gnus-current-headers) ")"))))
8131             (gnus-run-hooks 'gnus-ps-print-hook)
8132             (save-excursion
8133               (if window-system
8134                   (ps-spool-buffer-with-faces)
8135                 (ps-spool-buffer)))))
8136       (kill-buffer buffer))))
8137
8138 (defun gnus-summary-show-article (&optional arg)
8139   "Force redisplaying of the current article.
8140 If ARG (the prefix) is a number, show the article with the charset
8141 defined in `gnus-summary-show-article-charset-alist', or the charset
8142 input.
8143 If ARG (the prefix) is non-nil and not a number, show the raw article
8144 without any article massaging functions being run.  Normally, the key strokes
8145 are `C-u g'."
8146   (interactive "P")
8147   (cond
8148    ((numberp arg)
8149     (gnus-summary-show-article t)
8150     (let ((gnus-newsgroup-charset
8151            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8152                (mm-read-coding-system
8153                 "View as charset: " ;; actually it is coding system.
8154                 (save-excursion
8155                   (set-buffer gnus-article-buffer)
8156                   (mm-detect-coding-region (point) (point-max))))))
8157           (gnus-newsgroup-ignored-charsets 'gnus-all))
8158       (gnus-summary-select-article nil 'force)
8159       (let ((deps gnus-newsgroup-dependencies)
8160             head header lines)
8161         (save-excursion
8162           (set-buffer gnus-original-article-buffer)
8163           (save-restriction
8164             (message-narrow-to-head)
8165             (setq head (buffer-string))
8166             (goto-char (point-min))
8167             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8168               (goto-char (point-max))
8169               (widen)
8170               (setq lines (1- (count-lines (point) (point-max))))))
8171           (with-temp-buffer
8172             (insert (format "211 %d Article retrieved.\n"
8173                             (cdr gnus-article-current)))
8174             (insert head)
8175             (if lines (insert (format "Lines: %d\n" lines)))
8176             (insert ".\n")
8177             (let ((nntp-server-buffer (current-buffer)))
8178               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8179         (gnus-data-set-header
8180          (gnus-data-find (cdr gnus-article-current))
8181          header)
8182         (gnus-summary-update-article-line
8183          (cdr gnus-article-current) header)
8184         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8185           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8186    ((not arg)
8187     ;; Select the article the normal way.
8188     (gnus-summary-select-article nil 'force))
8189    (t
8190     ;; We have to require this here to make sure that the following
8191     ;; dynamic binding isn't shadowed by autoloading.
8192     (require 'gnus-async)
8193     (require 'gnus-art)
8194     ;; Bind the article treatment functions to nil.
8195     (let ((gnus-have-all-headers t)
8196           gnus-article-prepare-hook
8197           gnus-article-decode-hook
8198           gnus-display-mime-function
8199           gnus-break-pages)
8200       ;; Destroy any MIME parts.
8201       (when (gnus-buffer-live-p gnus-article-buffer)
8202         (save-excursion
8203           (set-buffer gnus-article-buffer)
8204           (mm-destroy-parts gnus-article-mime-handles)
8205           ;; Set it to nil for safety reason.
8206           (setq gnus-article-mime-handle-alist nil)
8207           (setq gnus-article-mime-handles nil)))
8208       (gnus-summary-select-article nil 'force))))
8209   (gnus-summary-goto-subject gnus-current-article)
8210   (gnus-summary-position-point))
8211
8212 (defun gnus-summary-show-raw-article ()
8213   "Show the raw article without any article massaging functions being run."
8214   (interactive)
8215   (gnus-summary-show-article t))
8216
8217 (defun gnus-summary-verbose-headers (&optional arg)
8218   "Toggle permanent full header display.
8219 If ARG is a positive number, turn header display on.
8220 If ARG is a negative number, turn header display off."
8221   (interactive "P")
8222   (setq gnus-show-all-headers
8223         (cond ((or (not (numberp arg))
8224                    (zerop arg))
8225                (not gnus-show-all-headers))
8226               ((natnump arg)
8227                t)))
8228   (gnus-summary-show-article))
8229
8230 (defun gnus-summary-toggle-header (&optional arg)
8231   "Show the headers if they are hidden, or hide them if they are shown.
8232 If ARG is a positive number, show the entire header.
8233 If ARG is a negative number, hide the unwanted header lines."
8234   (interactive "P")
8235   (save-excursion
8236     (set-buffer gnus-article-buffer)
8237     (save-restriction
8238       (let* ((buffer-read-only nil)
8239              (inhibit-point-motion-hooks t)
8240              hidden e)
8241         (save-restriction
8242           (article-narrow-to-head)
8243           (setq e (point-max)
8244                 hidden (if (numberp arg)
8245                            (>= arg 0)
8246                          (gnus-article-hidden-text-p 'headers))))
8247         (delete-region (point-min) e)
8248         (goto-char (point-min))
8249         (save-excursion
8250           (set-buffer gnus-original-article-buffer)
8251           (goto-char (point-min))
8252           (setq e (search-forward "\n\n" nil t)
8253                 e (if e (1- e) (point-max))))
8254         (insert-buffer-substring gnus-original-article-buffer 1 e)
8255         (save-restriction
8256           (narrow-to-region (point-min) (point))
8257           (article-decode-encoded-words)
8258           (if  hidden
8259               (let ((gnus-treat-hide-headers nil)
8260                     (gnus-treat-hide-boring-headers nil))
8261                 (gnus-delete-wash-type 'headers)
8262                 (gnus-treat-article 'head))
8263             (gnus-treat-article 'head)))
8264         (gnus-set-mode-line 'article)))))
8265
8266 (defun gnus-summary-show-all-headers ()
8267   "Make all header lines visible."
8268   (interactive)
8269   (gnus-summary-toggle-header 1))
8270
8271 (defun gnus-summary-caesar-message (&optional arg)
8272   "Caesar rotate the current article by 13.
8273 The numerical prefix specifies how many places to rotate each letter
8274 forward."
8275   (interactive "P")
8276   (gnus-summary-select-article)
8277   (let ((mail-header-separator ""))
8278     (gnus-eval-in-buffer-window gnus-article-buffer
8279       (save-restriction
8280         (widen)
8281         (let ((start (window-start))
8282               buffer-read-only)
8283           (message-caesar-buffer-body arg)
8284           (set-window-start (get-buffer-window (current-buffer)) start))))))
8285
8286 (defun gnus-summary-stop-page-breaking ()
8287   "Stop page breaking in the current article."
8288   (interactive)
8289   (gnus-summary-select-article)
8290   (gnus-eval-in-buffer-window gnus-article-buffer
8291     (widen)
8292     (when (gnus-visual-p 'page-marker)
8293       (let ((buffer-read-only nil))
8294         (gnus-remove-text-with-property 'gnus-prev)
8295         (gnus-remove-text-with-property 'gnus-next))
8296       (setq gnus-page-broken nil))))
8297
8298 (defun gnus-summary-move-article (&optional n to-newsgroup
8299                                             select-method action)
8300   "Move the current article to a different newsgroup.
8301 If N is a positive number, move the N next articles.
8302 If N is a negative number, move the N previous articles.
8303 If N is nil and any articles have been marked with the process mark,
8304 move those articles instead.
8305 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8306 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8307 re-spool using this method.
8308
8309 For this function to work, both the current newsgroup and the
8310 newsgroup that you want to move to have to support the `request-move'
8311 and `request-accept' functions.
8312
8313 ACTION can be either `move' (the default), `crosspost' or `copy'."
8314   (interactive "P")
8315   (unless action
8316     (setq action 'move))
8317   ;; Check whether the source group supports the required functions.
8318   (cond ((and (eq action 'move)
8319               (not (gnus-check-backend-function
8320                     'request-move-article gnus-newsgroup-name)))
8321          (error "The current group does not support article moving"))
8322         ((and (eq action 'crosspost)
8323               (not (gnus-check-backend-function
8324                     'request-replace-article gnus-newsgroup-name)))
8325          (error "The current group does not support article editing")))
8326   (let ((articles (gnus-summary-work-articles n))
8327         (prefix (if (gnus-check-backend-function
8328                      'request-move-article gnus-newsgroup-name)
8329                     (gnus-group-real-prefix gnus-newsgroup-name)
8330                   ""))
8331         (names '((move "Move" "Moving")
8332                  (copy "Copy" "Copying")
8333                  (crosspost "Crosspost" "Crossposting")))
8334         (copy-buf (save-excursion
8335                     (nnheader-set-temp-buffer " *copy article*")))
8336         art-group to-method new-xref article to-groups)
8337     (unless (assq action names)
8338       (error "Unknown action %s" action))
8339     ;; Read the newsgroup name.
8340     (when (and (not to-newsgroup)
8341                (not select-method))
8342       (if (and gnus-move-split-methods
8343                (not
8344                 (and (memq gnus-current-article articles)
8345                      (gnus-buffer-live-p gnus-original-article-buffer))))
8346           ;; When `gnus-move-split-methods' is non-nil, we have to
8347           ;; select an article to give `gnus-read-move-group-name' an
8348           ;; opportunity to suggest an appropriate default.  However,
8349           ;; we needn't render or mark the article.
8350           (let ((gnus-display-mime-function nil)
8351                 (gnus-article-prepare-hook nil)
8352                 (gnus-mark-article-hook nil))
8353             (gnus-summary-select-article nil nil nil (car articles))))
8354       (setq to-newsgroup
8355             (gnus-read-move-group-name
8356              (cadr (assq action names))
8357              (symbol-value (intern (format "gnus-current-%s-group" action)))
8358              articles prefix))
8359       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8360     (setq to-method (or select-method
8361                         (gnus-server-to-method
8362                          (gnus-group-method to-newsgroup))))
8363     ;; Check the method we are to move this article to...
8364     (unless (gnus-check-backend-function
8365              'request-accept-article (car to-method))
8366       (error "%s does not support article copying" (car to-method)))
8367     (unless (gnus-check-server to-method)
8368       (error "Can't open server %s" (car to-method)))
8369     (gnus-message 6 "%s to %s: %s..."
8370                   (caddr (assq action names))
8371                   (or (car select-method) to-newsgroup) articles)
8372     (while articles
8373       (setq article (pop articles))
8374       (setq
8375        art-group
8376        (cond
8377         ;; Move the article.
8378         ((eq action 'move)
8379          ;; Remove this article from future suppression.
8380          (gnus-dup-unsuppress-article article)
8381          (gnus-request-move-article
8382           article                       ; Article to move
8383           gnus-newsgroup-name           ; From newsgroup
8384           (nth 1 (gnus-find-method-for-group
8385                   gnus-newsgroup-name)) ; Server
8386           (list 'gnus-request-accept-article
8387                 to-newsgroup (list 'quote select-method)
8388                 (not articles) t)       ; Accept form
8389           (not articles)))              ; Only save nov last time
8390         ;; Copy the article.
8391         ((eq action 'copy)
8392          (save-excursion
8393            (set-buffer copy-buf)
8394            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8395              (gnus-request-accept-article
8396               to-newsgroup select-method (not articles) t))))
8397         ;; Crosspost the article.
8398         ((eq action 'crosspost)
8399          (let ((xref (message-tokenize-header
8400                       (mail-header-xref (gnus-summary-article-header article))
8401                       " ")))
8402            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8403                                   ":" (number-to-string article)))
8404            (unless xref
8405              (setq xref (list (system-name))))
8406            (setq new-xref
8407                  (concat
8408                   (mapconcat 'identity
8409                              (delete "Xref:" (delete new-xref xref))
8410                              " ")
8411                   " " new-xref))
8412            (save-excursion
8413              (set-buffer copy-buf)
8414              ;; First put the article in the destination group.
8415              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8416              (when (consp (setq art-group
8417                                 (gnus-request-accept-article
8418                                  to-newsgroup select-method (not articles))))
8419                (setq new-xref (concat new-xref " " (car art-group)
8420                                       ":"
8421                                       (number-to-string (cdr art-group))))
8422                ;; Now we have the new Xrefs header, so we insert
8423                ;; it and replace the new article.
8424                (nnheader-replace-header "Xref" new-xref)
8425                (gnus-request-replace-article
8426                 (cdr art-group) to-newsgroup (current-buffer))
8427                art-group))))))
8428       (cond
8429        ((not art-group)
8430         (gnus-message 1 "Couldn't %s article %s: %s"
8431                       (cadr (assq action names)) article
8432                       (nnheader-get-report (car to-method))))
8433        ((eq art-group 'junk)
8434         (when (eq action 'move)
8435           (gnus-summary-mark-article article gnus-canceled-mark)
8436           (gnus-message 4 "Deleted article %s" article)))
8437        (t
8438         (let* ((pto-group (gnus-group-prefixed-name
8439                            (car art-group) to-method))
8440                (entry
8441                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8442                (info (nth 2 entry))
8443                (to-group (gnus-info-group info))
8444                to-marks)
8445           ;; Update the group that has been moved to.
8446           (when (and info
8447                      (memq action '(move copy)))
8448             (unless (member to-group to-groups)
8449               (push to-group to-groups))
8450
8451             (unless (memq article gnus-newsgroup-unreads)
8452               (push 'read to-marks)
8453               (gnus-info-set-read
8454                info (gnus-add-to-range (gnus-info-read info)
8455                                        (list (cdr art-group)))))
8456
8457             ;; See whether the article is to be put in the cache.
8458             (let ((marks gnus-article-mark-lists)
8459                   (to-article (cdr art-group)))
8460
8461               ;; Enter the article into the cache in the new group,
8462               ;; if that is required.
8463               (when gnus-use-cache
8464                 (gnus-cache-possibly-enter-article
8465                  to-group to-article
8466                  (memq article gnus-newsgroup-marked)
8467                  (memq article gnus-newsgroup-dormant)
8468                  (memq article gnus-newsgroup-unreads)))
8469
8470               (when gnus-preserve-marks
8471                 ;; Copy any marks over to the new group.
8472                 (when (and (equal to-group gnus-newsgroup-name)
8473                            (not (memq article gnus-newsgroup-unreads)))
8474                   ;; Mark this article as read in this group.
8475                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8476                   (setcdr (gnus-active to-group) to-article)
8477                   (setcdr gnus-newsgroup-active to-article))
8478
8479                 (while marks
8480                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8481                     (when (memq article (symbol-value
8482                                          (intern (format "gnus-newsgroup-%s"
8483                                                          (caar marks)))))
8484                       (push (cdar marks) to-marks)
8485                       ;; If the other group is the same as this group,
8486                       ;; then we have to add the mark to the list.
8487                       (when (equal to-group gnus-newsgroup-name)
8488                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8489                              (cons to-article
8490                                    (symbol-value
8491                                     (intern (format "gnus-newsgroup-%s"
8492                                                     (caar marks)))))))
8493                       ;; Copy the marks to other group.
8494                       (gnus-add-marked-articles
8495                        to-group (cdar marks) (list to-article) info)))
8496                   (setq marks (cdr marks)))
8497
8498                 (gnus-request-set-mark to-group (list (list (list to-article)
8499                                                             'add
8500                                                             to-marks))))
8501
8502               (gnus-dribble-enter
8503                (concat "(gnus-group-set-info '"
8504                        (gnus-prin1-to-string (gnus-get-info to-group))
8505                        ")"))))
8506
8507           ;; Update the Xref header in this article to point to
8508           ;; the new crossposted article we have just created.
8509           (when (eq action 'crosspost)
8510             (save-excursion
8511               (set-buffer copy-buf)
8512               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8513               (nnheader-replace-header "Xref" new-xref)
8514               (gnus-request-replace-article
8515                article gnus-newsgroup-name (current-buffer)))))
8516
8517         ;;;!!!Why is this necessary?
8518         (set-buffer gnus-summary-buffer)
8519
8520         (gnus-summary-goto-subject article)
8521         (when (eq action 'move)
8522           (gnus-summary-mark-article article gnus-canceled-mark))))
8523       (gnus-summary-remove-process-mark article))
8524     ;; Re-activate all groups that have been moved to.
8525     (save-excursion
8526       (set-buffer gnus-group-buffer)
8527       (let ((gnus-group-marked to-groups))
8528         (gnus-group-get-new-news-this-group nil t)))
8529
8530     (gnus-kill-buffer copy-buf)
8531     (gnus-summary-position-point)
8532     (gnus-set-mode-line 'summary)))
8533
8534 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8535   "Move the current article to a different newsgroup.
8536 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8537 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8538 re-spool using this method."
8539   (interactive "P")
8540   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8541
8542 (defun gnus-summary-crosspost-article (&optional n)
8543   "Crosspost the current article to some other group."
8544   (interactive "P")
8545   (gnus-summary-move-article n nil nil 'crosspost))
8546
8547 (defcustom gnus-summary-respool-default-method nil
8548   "Default method type for respooling an article.
8549 If nil, use to the current newsgroup method."
8550   :type 'symbol
8551   :group 'gnus-summary-mail)
8552
8553 (defun gnus-summary-respool-article (&optional n method)
8554   "Respool the current article.
8555 The article will be squeezed through the mail spooling process again,
8556 which means that it will be put in some mail newsgroup or other
8557 depending on `nnmail-split-methods'.
8558 If N is a positive number, respool the N next articles.
8559 If N is a negative number, respool the N previous articles.
8560 If N is nil and any articles have been marked with the process mark,
8561 respool those articles instead.
8562
8563 Respooling can be done both from mail groups and \"real\" newsgroups.
8564 In the former case, the articles in question will be moved from the
8565 current group into whatever groups they are destined to.  In the
8566 latter case, they will be copied into the relevant groups."
8567   (interactive
8568    (list current-prefix-arg
8569          (let* ((methods (gnus-methods-using 'respool))
8570                 (methname
8571                  (symbol-name (or gnus-summary-respool-default-method
8572                                   (car (gnus-find-method-for-group
8573                                         gnus-newsgroup-name)))))
8574                 (method
8575                  (gnus-completing-read-with-default
8576                   methname "What backend do you want to use when respooling?"
8577                   methods nil t nil 'gnus-mail-method-history))
8578                 ms)
8579            (cond
8580             ((zerop (length (setq ms (gnus-servers-using-backend
8581                                       (intern method)))))
8582              (list (intern method) ""))
8583             ((= 1 (length ms))
8584              (car ms))
8585             (t
8586              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8587                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8588                            ms-alist))))))))
8589   (unless method
8590     (error "No method given for respooling"))
8591   (if (assoc (symbol-name
8592               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8593              (gnus-methods-using 'respool))
8594       (gnus-summary-move-article n nil method)
8595     (gnus-summary-copy-article n nil method)))
8596
8597 (defun gnus-summary-import-article (file &optional edit)
8598   "Import an arbitrary file into a mail newsgroup."
8599   (interactive "fImport file: \nP")
8600   (let ((group gnus-newsgroup-name)
8601         (now (current-time))
8602         atts lines group-art)
8603     (unless (gnus-check-backend-function 'request-accept-article group)
8604       (error "%s does not support article importing" group))
8605     (or (file-readable-p file)
8606         (not (file-regular-p file))
8607         (error "Can't read %s" file))
8608     (save-excursion
8609       (set-buffer (gnus-get-buffer-create " *import file*"))
8610       (erase-buffer)
8611       (nnheader-insert-file-contents file)
8612       (goto-char (point-min))
8613       (if (nnheader-article-p)
8614           (save-restriction
8615             (goto-char (point-min))
8616             (search-forward "\n\n" nil t)
8617             (narrow-to-region (point-min) (1- (point)))
8618             (goto-char (point-min))
8619             (unless (re-search-forward "^date:" nil t)
8620               (goto-char (point-max))
8621               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8622        ;; This doesn't look like an article, so we fudge some headers.
8623         (setq atts (file-attributes file)
8624               lines (count-lines (point-min) (point-max)))
8625         (insert "From: " (read-string "From: ") "\n"
8626                 "Subject: " (read-string "Subject: ") "\n"
8627                 "Date: " (message-make-date (nth 5 atts)) "\n"
8628                 "Message-ID: " (message-make-message-id) "\n"
8629                 "Lines: " (int-to-string lines) "\n"
8630                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8631       (setq group-art (gnus-request-accept-article group nil t))
8632       (kill-buffer (current-buffer)))
8633     (setq gnus-newsgroup-active (gnus-activate-group group))
8634     (forward-line 1)
8635     (gnus-summary-goto-article (cdr group-art) nil t)
8636     (when edit
8637       (gnus-summary-edit-article))))
8638
8639 (defun gnus-summary-create-article ()
8640   "Create an article in a mail newsgroup."
8641   (interactive)
8642   (let ((group gnus-newsgroup-name)
8643         (now (current-time))
8644         group-art)
8645     (unless (gnus-check-backend-function 'request-accept-article group)
8646       (error "%s does not support article importing" group))
8647     (save-excursion
8648       (set-buffer (gnus-get-buffer-create " *import file*"))
8649       (erase-buffer)
8650       (goto-char (point-min))
8651       ;; This doesn't look like an article, so we fudge some headers.
8652       (insert "From: " (read-string "From: ") "\n"
8653               "Subject: " (read-string "Subject: ") "\n"
8654               "Date: " (message-make-date now) "\n"
8655               "Message-ID: " (message-make-message-id) "\n")
8656       (setq group-art (gnus-request-accept-article group nil t))
8657       (kill-buffer (current-buffer)))
8658     (setq gnus-newsgroup-active (gnus-activate-group group))
8659     (forward-line 1)
8660     (gnus-summary-goto-article (cdr group-art) nil t)
8661     (gnus-summary-edit-article)))
8662
8663 (defun gnus-summary-article-posted-p ()
8664   "Say whether the current (mail) article is available from news as well.
8665 This will be the case if the article has both been mailed and posted."
8666   (interactive)
8667   (let ((id (mail-header-references (gnus-summary-article-header)))
8668         (gnus-override-method (car (gnus-refer-article-methods))))
8669     (if (gnus-request-head id "")
8670         (gnus-message 2 "The current message was found on %s"
8671                       gnus-override-method)
8672       (gnus-message 2 "The current message couldn't be found on %s"
8673                     gnus-override-method)
8674       nil)))
8675
8676 (defun gnus-summary-expire-articles (&optional now)
8677   "Expire all articles that are marked as expirable in the current group."
8678   (interactive)
8679   (when (gnus-check-backend-function
8680          'request-expire-articles gnus-newsgroup-name)
8681     ;; This backend supports expiry.
8682     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8683            (expirable (if total
8684                           (progn
8685                             ;; We need to update the info for
8686                             ;; this group for `gnus-list-of-read-articles'
8687                             ;; to give us the right answer.
8688                             (gnus-run-hooks 'gnus-exit-group-hook)
8689                             (gnus-summary-update-info)
8690                             (gnus-list-of-read-articles gnus-newsgroup-name))
8691                         (setq gnus-newsgroup-expirable
8692                               (sort gnus-newsgroup-expirable '<))))
8693            (expiry-wait (if now 'immediate
8694                           (gnus-group-find-parameter
8695                            gnus-newsgroup-name 'expiry-wait)))
8696            (nnmail-expiry-target
8697             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8698                 nnmail-expiry-target))
8699            es)
8700       (when expirable
8701         ;; There are expirable articles in this group, so we run them
8702         ;; through the expiry process.
8703         (gnus-message 6 "Expiring articles...")
8704         (unless (gnus-check-group gnus-newsgroup-name)
8705           (error "Can't open server for %s" gnus-newsgroup-name))
8706         ;; The list of articles that weren't expired is returned.
8707         (save-excursion
8708           (if expiry-wait
8709               (let ((nnmail-expiry-wait-function nil)
8710                     (nnmail-expiry-wait expiry-wait))
8711                 (setq es (gnus-request-expire-articles
8712                           expirable gnus-newsgroup-name)))
8713             (setq es (gnus-request-expire-articles
8714                       expirable gnus-newsgroup-name)))
8715           (unless total
8716             (setq gnus-newsgroup-expirable es))
8717           ;; We go through the old list of expirable, and mark all
8718           ;; really expired articles as nonexistent.
8719           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8720             (let ((gnus-use-cache nil))
8721               (dolist (article expirable)
8722                 (when (and (not (memq article es))
8723                            (gnus-data-find article))
8724                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8725         (gnus-message 6 "Expiring articles...done")))))
8726
8727 (defun gnus-summary-expire-articles-now ()
8728   "Expunge all expirable articles in the current group.
8729 This means that *all* articles that are marked as expirable will be
8730 deleted forever, right now."
8731   (interactive)
8732   (or gnus-expert-user
8733       (gnus-yes-or-no-p
8734        "Are you really, really, really sure you want to delete all these messages? ")
8735       (error "Phew!"))
8736   (gnus-summary-expire-articles t))
8737
8738 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8739 (defun gnus-summary-delete-article (&optional n)
8740   "Delete the N next (mail) articles.
8741 This command actually deletes articles.  This is not a marking
8742 command.  The article will disappear forever from your life, never to
8743 return.
8744 If N is negative, delete backwards.
8745 If N is nil and articles have been marked with the process mark,
8746 delete these instead."
8747   (interactive "P")
8748   (unless (gnus-check-backend-function 'request-expire-articles
8749                                        gnus-newsgroup-name)
8750     (error "The current newsgroup does not support article deletion"))
8751   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8752     (error "Couldn't open server"))
8753   ;; Compute the list of articles to delete.
8754   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8755         not-deleted)
8756     (if (and gnus-novice-user
8757              (not (gnus-yes-or-no-p
8758                    (format "Do you really want to delete %s forever? "
8759                            (if (> (length articles) 1)
8760                                (format "these %s articles" (length articles))
8761                              "this article")))))
8762         ()
8763       ;; Delete the articles.
8764       (setq not-deleted (gnus-request-expire-articles
8765                          articles gnus-newsgroup-name 'force))
8766       (while articles
8767         (gnus-summary-remove-process-mark (car articles))
8768         ;; The backend might not have been able to delete the article
8769         ;; after all.
8770         (unless (memq (car articles) not-deleted)
8771           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8772         (setq articles (cdr articles)))
8773       (when not-deleted
8774         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8775     (gnus-summary-position-point)
8776     (gnus-set-mode-line 'summary)
8777     not-deleted))
8778
8779 (defun gnus-summary-edit-article (&optional arg)
8780   "Edit the current article.
8781 This will have permanent effect only in mail groups.
8782 If ARG is nil, edit the decoded articles.
8783 If ARG is 1, edit the raw articles.
8784 If ARG is 2, edit the raw articles even in read-only groups.
8785 If ARG is 3, edit the articles with the current handles.
8786 Otherwise, allow editing of articles even in read-only
8787 groups."
8788   (interactive "P")
8789   (let (force raw current-handles)
8790     (cond
8791      ((null arg))
8792      ((eq arg 1)
8793       (setq raw t))
8794      ((eq arg 2)
8795       (setq raw t
8796             force t))
8797      ((eq arg 3)
8798       (setq current-handles
8799             (and (gnus-buffer-live-p gnus-article-buffer)
8800                  (with-current-buffer gnus-article-buffer
8801                    (prog1
8802                        gnus-article-mime-handles
8803                      (setq gnus-article-mime-handles nil))))))
8804      (t
8805       (setq force t)))
8806     (when (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8807       (error "Can't edit the raw article in group nndraft:drafts"))
8808     (save-excursion
8809       (set-buffer gnus-summary-buffer)
8810       (let ((mail-parse-charset gnus-newsgroup-charset)
8811             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8812         (gnus-set-global-variables)
8813         (when (and (not force)
8814                    (gnus-group-read-only-p))
8815           (error "The current newsgroup does not support article editing"))
8816         (gnus-summary-show-article t)
8817         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8818           (with-current-buffer gnus-article-buffer
8819             (mm-enable-multibyte)))
8820         (if (equal gnus-newsgroup-name "nndraft:drafts")
8821             (setq raw t))
8822         (gnus-article-edit-article
8823          (if raw 'ignore
8824            `(lambda ()
8825               (let ((mbl mml-buffer-list))
8826                 (setq mml-buffer-list nil)
8827                 (mime-to-mml ,'current-handles)
8828                 (let ((mbl1 mml-buffer-list))
8829                   (setq mml-buffer-list mbl)
8830                   (set (make-local-variable 'mml-buffer-list) mbl1))
8831                 (make-local-hook 'kill-buffer-hook)
8832                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8833          `(lambda (no-highlight)
8834             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8835                   (message-options message-options)
8836                   (message-options-set-recipient)
8837                   (mail-parse-ignored-charsets
8838                    ',gnus-newsgroup-ignored-charsets))
8839               ,(if (not raw) '(progn
8840                                 (mml-to-mime)
8841                                 (mml-destroy-buffers)
8842                                 (remove-hook 'kill-buffer-hook
8843                                              'mml-destroy-buffers t)
8844                                 (kill-local-variable 'mml-buffer-list)))
8845               (gnus-summary-edit-article-done
8846                ,(or (mail-header-references gnus-current-headers) "")
8847                ,(gnus-group-read-only-p)
8848                ,gnus-summary-buffer no-highlight))))))))
8849
8850 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8851
8852 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8853                                                  no-highlight)
8854   "Make edits to the current article permanent."
8855   (interactive)
8856   (save-excursion
8857    ;; The buffer restriction contains the entire article if it exists.
8858     (when (article-goto-body)
8859       (let ((lines (count-lines (point) (point-max)))
8860             (length (- (point-max) (point)))
8861             (case-fold-search t)
8862             (body (copy-marker (point))))
8863         (goto-char (point-min))
8864         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8865           (delete-region (match-beginning 1) (match-end 1))
8866           (insert (number-to-string length)))
8867         (goto-char (point-min))
8868         (when (re-search-forward
8869                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8870           (delete-region (match-beginning 1) (match-end 1))
8871           (insert (number-to-string length)))
8872         (goto-char (point-min))
8873         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8874           (delete-region (match-beginning 1) (match-end 1))
8875           (insert (number-to-string lines))))))
8876   ;; Replace the article.
8877   (let ((buf (current-buffer)))
8878     (with-temp-buffer
8879       (insert-buffer-substring buf)
8880
8881       (if (and (not read-only)
8882                (not (gnus-request-replace-article
8883                      (cdr gnus-article-current) (car gnus-article-current)
8884                      (current-buffer) t)))
8885           (error "Couldn't replace article")
8886         ;; Update the summary buffer.
8887         (if (and references
8888                  (equal (message-tokenize-header references " ")
8889                         (message-tokenize-header
8890                          (or (message-fetch-field "references") "") " ")))
8891             ;; We only have to update this line.
8892             (save-excursion
8893               (save-restriction
8894                 (message-narrow-to-head)
8895                 (let ((head (buffer-string))
8896                       header)
8897                   (with-temp-buffer
8898                     (insert (format "211 %d Article retrieved.\n"
8899                                     (cdr gnus-article-current)))
8900                     (insert head)
8901                     (insert ".\n")
8902                     (let ((nntp-server-buffer (current-buffer)))
8903                       (setq header (car (gnus-get-newsgroup-headers
8904                                          nil t))))
8905                     (save-excursion
8906                       (set-buffer gnus-summary-buffer)
8907                       (gnus-data-set-header
8908                        (gnus-data-find (cdr gnus-article-current))
8909                        header)
8910                       (gnus-summary-update-article-line
8911                        (cdr gnus-article-current) header)
8912                       (if (gnus-summary-goto-subject
8913                            (cdr gnus-article-current) nil t)
8914                           (gnus-summary-update-secondary-mark
8915                            (cdr gnus-article-current))))))))
8916           ;; Update threads.
8917           (set-buffer (or buffer gnus-summary-buffer))
8918           (gnus-summary-update-article (cdr gnus-article-current))
8919           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8920               (gnus-summary-update-secondary-mark
8921                (cdr gnus-article-current))))
8922         ;; Prettify the article buffer again.
8923         (unless no-highlight
8924           (save-excursion
8925             (set-buffer gnus-article-buffer)
8926             ;;;!!! Fix this -- article should be rehighlighted.
8927             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8928             (set-buffer gnus-original-article-buffer)
8929             (gnus-request-article
8930              (cdr gnus-article-current)
8931              (car gnus-article-current) (current-buffer))))
8932         ;; Prettify the summary buffer line.
8933         (when (gnus-visual-p 'summary-highlight 'highlight)
8934           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8935
8936 (defun gnus-summary-edit-wash (key)
8937   "Perform editing command KEY in the article buffer."
8938   (interactive
8939    (list
8940     (progn
8941       (message "%s" (concat (this-command-keys) "- "))
8942       (read-char))))
8943   (message "")
8944   (gnus-summary-edit-article)
8945   (execute-kbd-macro (concat (this-command-keys) key))
8946   (gnus-article-edit-done))
8947
8948 ;;; Respooling
8949
8950 (defun gnus-summary-respool-query (&optional silent trace)
8951   "Query where the respool algorithm would put this article."
8952   (interactive)
8953   (let (gnus-mark-article-hook)
8954     (gnus-summary-select-article)
8955     (save-excursion
8956       (set-buffer gnus-original-article-buffer)
8957       (save-restriction
8958         (message-narrow-to-head)
8959         (let ((groups (nnmail-article-group 'identity trace)))
8960           (unless silent
8961             (if groups
8962                 (message "This message would go to %s"
8963                          (mapconcat 'car groups ", "))
8964               (message "This message would go to no groups"))
8965             groups))))))
8966
8967 (defun gnus-summary-respool-trace ()
8968   "Trace where the respool algorithm would put this article.
8969 Display a buffer showing all fancy splitting patterns which matched."
8970   (interactive)
8971   (gnus-summary-respool-query nil t))
8972
8973 ;; Summary marking commands.
8974
8975 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8976   "Mark articles which has the same subject as read, and then select the next.
8977 If UNMARK is positive, remove any kind of mark.
8978 If UNMARK is negative, tick articles."
8979   (interactive "P")
8980   (when unmark
8981     (setq unmark (prefix-numeric-value unmark)))
8982   (let ((count
8983          (gnus-summary-mark-same-subject
8984           (gnus-summary-article-subject) unmark)))
8985     ;; Select next unread article.  If auto-select-same mode, should
8986     ;; select the first unread article.
8987     (gnus-summary-next-article t (and gnus-auto-select-same
8988                                       (gnus-summary-article-subject)))
8989     (gnus-message 7 "%d article%s marked as %s"
8990                   count (if (= count 1) " is" "s are")
8991                   (if unmark "unread" "read"))))
8992
8993 (defun gnus-summary-kill-same-subject (&optional unmark)
8994   "Mark articles which has the same subject as read.
8995 If UNMARK is positive, remove any kind of mark.
8996 If UNMARK is negative, tick articles."
8997   (interactive "P")
8998   (when unmark
8999     (setq unmark (prefix-numeric-value unmark)))
9000   (let ((count
9001          (gnus-summary-mark-same-subject
9002           (gnus-summary-article-subject) unmark)))
9003     ;; If marked as read, go to next unread subject.
9004     (when (null unmark)
9005       ;; Go to next unread subject.
9006       (gnus-summary-next-subject 1 t))
9007     (gnus-message 7 "%d articles are marked as %s"
9008                   count (if unmark "unread" "read"))))
9009
9010 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9011   "Mark articles with same SUBJECT as read, and return marked number.
9012 If optional argument UNMARK is positive, remove any kinds of marks.
9013 If optional argument UNMARK is negative, mark articles as unread instead."
9014   (let ((count 1))
9015     (save-excursion
9016       (cond
9017        ((null unmark)                   ; Mark as read.
9018         (while (and
9019                 (progn
9020                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9021                   (gnus-summary-show-thread) t)
9022                 (gnus-summary-find-subject subject))
9023           (setq count (1+ count))))
9024        ((> unmark 0)                    ; Tick.
9025         (while (and
9026                 (progn
9027                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9028                   (gnus-summary-show-thread) t)
9029                 (gnus-summary-find-subject subject))
9030           (setq count (1+ count))))
9031        (t                               ; Mark as unread.
9032         (while (and
9033                 (progn
9034                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9035                   (gnus-summary-show-thread) t)
9036                 (gnus-summary-find-subject subject))
9037           (setq count (1+ count)))))
9038       (gnus-set-mode-line 'summary)
9039       ;; Return the number of marked articles.
9040       count)))
9041
9042 (defun gnus-summary-mark-as-processable (n &optional unmark)
9043   "Set the process mark on the next N articles.
9044 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9045 the process mark instead.  The difference between N and the actual
9046 number of articles marked is returned."
9047   (interactive "P")
9048   (if (and (null n) (gnus-region-active-p))
9049       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9050     (setq n (prefix-numeric-value n))
9051     (let ((backward (< n 0))
9052           (n (abs n)))
9053       (while (and
9054               (> n 0)
9055               (if unmark
9056                   (gnus-summary-remove-process-mark
9057                    (gnus-summary-article-number))
9058                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9059               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9060         (setq n (1- n)))
9061       (when (/= 0 n)
9062         (gnus-message 7 "No more articles"))
9063       (gnus-summary-recenter)
9064       (gnus-summary-position-point)
9065       n)))
9066
9067 (defun gnus-summary-unmark-as-processable (n)
9068   "Remove the process mark from the next N articles.
9069 If N is negative, unmark backward instead.  The difference between N and
9070 the actual number of articles unmarked is returned."
9071   (interactive "P")
9072   (gnus-summary-mark-as-processable n t))
9073
9074 (defun gnus-summary-unmark-all-processable ()
9075   "Remove the process mark from all articles."
9076   (interactive)
9077   (save-excursion
9078     (while gnus-newsgroup-processable
9079       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9080   (gnus-summary-position-point))
9081
9082 (defun gnus-summary-add-mark (article type)
9083   "Mark ARTICLE with a mark of TYPE."
9084   (let ((vtype (car (assq type gnus-article-mark-lists)))
9085         var)
9086     (if (not vtype)
9087         (error "No such mark type: %s" type)
9088       (setq var (intern (format "gnus-newsgroup-%s" type)))
9089       (set var (cons article (symbol-value var)))
9090       (if (memq type '(processable cached replied forwarded recent saved))
9091           (gnus-summary-update-secondary-mark article)
9092         ;;; !!! This is bogus.  We should find out what primary
9093         ;;; !!! mark we want to set.
9094         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9095
9096 (defun gnus-summary-mark-as-expirable (n)
9097   "Mark N articles forward as expirable.
9098 If N is negative, mark backward instead.  The difference between N and
9099 the actual number of articles marked is returned."
9100   (interactive "p")
9101   (gnus-summary-mark-forward n gnus-expirable-mark))
9102
9103 (defun gnus-summary-mark-article-as-replied (article)
9104   "Mark ARTICLE as replied to and update the summary line.
9105 ARTICLE can also be a list of articles."
9106   (interactive (list (gnus-summary-article-number)))
9107   (let ((articles (if (listp article) article (list article))))
9108     (dolist (article articles)
9109       (push article gnus-newsgroup-replied)
9110       (let ((buffer-read-only nil))
9111         (when (gnus-summary-goto-subject article nil t)
9112           (gnus-summary-update-secondary-mark article))))))
9113
9114 (defun gnus-summary-mark-article-as-forwarded (article)
9115   "Mark ARTICLE as forwarded and update the summary line.
9116 ARTICLE can also be a list of articles."
9117   (let ((articles (if (listp article) article (list article))))
9118     (dolist (article articles)
9119       (push article gnus-newsgroup-forwarded)
9120       (let ((buffer-read-only nil))
9121         (when (gnus-summary-goto-subject article nil t)
9122           (gnus-summary-update-secondary-mark article))))))
9123
9124 (defun gnus-summary-set-bookmark (article)
9125   "Set a bookmark in current article."
9126   (interactive (list (gnus-summary-article-number)))
9127   (when (or (not (get-buffer gnus-article-buffer))
9128             (not gnus-current-article)
9129             (not gnus-article-current)
9130             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9131     (error "No current article selected"))
9132   ;; Remove old bookmark, if one exists.
9133   (let ((old (assq article gnus-newsgroup-bookmarks)))
9134     (when old
9135       (setq gnus-newsgroup-bookmarks
9136             (delq old gnus-newsgroup-bookmarks))))
9137   ;; Set the new bookmark, which is on the form
9138   ;; (article-number . line-number-in-body).
9139   (push
9140    (cons article
9141          (save-excursion
9142            (set-buffer gnus-article-buffer)
9143            (count-lines
9144             (min (point)
9145                  (save-excursion
9146                    (goto-char (point-min))
9147                    (search-forward "\n\n" nil t)
9148                    (point)))
9149             (point))))
9150    gnus-newsgroup-bookmarks)
9151   (gnus-message 6 "A bookmark has been added to the current article."))
9152
9153 (defun gnus-summary-remove-bookmark (article)
9154   "Remove the bookmark from the current article."
9155   (interactive (list (gnus-summary-article-number)))
9156   ;; Remove old bookmark, if one exists.
9157   (let ((old (assq article gnus-newsgroup-bookmarks)))
9158     (if old
9159         (progn
9160           (setq gnus-newsgroup-bookmarks
9161                 (delq old gnus-newsgroup-bookmarks))
9162           (gnus-message 6 "Removed bookmark."))
9163       (gnus-message 6 "No bookmark in current article."))))
9164
9165 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9166 (defun gnus-summary-mark-as-dormant (n)
9167   "Mark N articles forward as dormant.
9168 If N is negative, mark backward instead.  The difference between N and
9169 the actual number of articles marked is returned."
9170   (interactive "p")
9171   (gnus-summary-mark-forward n gnus-dormant-mark))
9172
9173 (defun gnus-summary-set-process-mark (article)
9174   "Set the process mark on ARTICLE and update the summary line."
9175   (setq gnus-newsgroup-processable
9176         (cons article
9177               (delq article gnus-newsgroup-processable)))
9178   (when (gnus-summary-goto-subject article)
9179     (gnus-summary-show-thread)
9180     (gnus-summary-goto-subject article)
9181     (gnus-summary-update-secondary-mark article)))
9182
9183 (defun gnus-summary-remove-process-mark (article)
9184   "Remove the process mark from ARTICLE and update the summary line."
9185   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9186   (when (gnus-summary-goto-subject article)
9187     (gnus-summary-show-thread)
9188     (gnus-summary-goto-subject article)
9189     (gnus-summary-update-secondary-mark article)))
9190
9191 (defun gnus-summary-set-saved-mark (article)
9192   "Set the process mark on ARTICLE and update the summary line."
9193   (push article gnus-newsgroup-saved)
9194   (when (gnus-summary-goto-subject article)
9195     (gnus-summary-update-secondary-mark article)))
9196
9197 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9198   "Mark N articles as read forwards.
9199 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9200 The difference between N and the actual number of articles marked is
9201 returned.
9202 Iff NO-EXPIRE, auto-expiry will be inhibited."
9203   (interactive "p")
9204   (gnus-summary-show-thread)
9205   (let ((backward (< n 0))
9206         (gnus-summary-goto-unread
9207          (and gnus-summary-goto-unread
9208               (not (eq gnus-summary-goto-unread 'never))
9209               (not (memq mark (list gnus-unread-mark
9210                                     gnus-ticked-mark gnus-dormant-mark)))))
9211         (n (abs n))
9212         (mark (or mark gnus-del-mark)))
9213     (while (and (> n 0)
9214                 (gnus-summary-mark-article nil mark no-expire)
9215                 (zerop (gnus-summary-next-subject
9216                         (if backward -1 1)
9217                         (and gnus-summary-goto-unread
9218                              (not (eq gnus-summary-goto-unread 'never)))
9219                         t)))
9220       (setq n (1- n)))
9221     (when (/= 0 n)
9222       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9223     (gnus-summary-recenter)
9224     (gnus-summary-position-point)
9225     (gnus-set-mode-line 'summary)
9226     n))
9227
9228 (defun gnus-summary-mark-article-as-read (mark)
9229   "Mark the current article quickly as read with MARK."
9230   (let ((article (gnus-summary-article-number)))
9231     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9232     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9233     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9234     (push (cons article mark) gnus-newsgroup-reads)
9235     ;; Possibly remove from cache, if that is used.
9236     (when gnus-use-cache
9237       (gnus-cache-enter-remove-article article))
9238     ;; Allow the backend to change the mark.
9239     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9240     ;; Check for auto-expiry.
9241     (when (and gnus-newsgroup-auto-expire
9242                (memq mark gnus-auto-expirable-marks))
9243       (setq mark gnus-expirable-mark)
9244       ;; Let the backend know about the mark change.
9245       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9246       (push article gnus-newsgroup-expirable))
9247     ;; Set the mark in the buffer.
9248     (gnus-summary-update-mark mark 'unread)
9249     t))
9250
9251 (defun gnus-summary-mark-article-as-unread (mark)
9252   "Mark the current article quickly as unread with MARK."
9253   (let* ((article (gnus-summary-article-number))
9254          (old-mark (gnus-summary-article-mark article)))
9255     ;; Allow the backend to change the mark.
9256     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9257     (if (eq mark old-mark)
9258         t
9259       (if (<= article 0)
9260           (progn
9261             (gnus-error 1 "Can't mark negative article numbers")
9262             nil)
9263         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9264         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9265         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9266         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9267         (cond ((= mark gnus-ticked-mark)
9268                (setq gnus-newsgroup-marked
9269                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9270                                               article)))
9271               ((= mark gnus-dormant-mark)
9272                (setq gnus-newsgroup-dormant
9273                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9274                                               article)))
9275               (t
9276                (setq gnus-newsgroup-unreads
9277                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9278                                               article))))
9279         (gnus-pull article gnus-newsgroup-reads)
9280
9281         ;; See whether the article is to be put in the cache.
9282         (and gnus-use-cache
9283              (vectorp (gnus-summary-article-header article))
9284              (save-excursion
9285                (gnus-cache-possibly-enter-article
9286                 gnus-newsgroup-name article
9287                 (= mark gnus-ticked-mark)
9288                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9289
9290         ;; Fix the mark.
9291         (gnus-summary-update-mark mark 'unread)
9292         t))))
9293
9294 (defun gnus-summary-mark-article (&optional article mark no-expire)
9295   "Mark ARTICLE with MARK.  MARK can be any character.
9296 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9297 `??' (dormant) and `?E' (expirable).
9298 If MARK is nil, then the default character `?r' is used.
9299 If ARTICLE is nil, then the article on the current line will be
9300 marked.
9301 Iff NO-EXPIRE, auto-expiry will be inhibited."
9302   ;; The mark might be a string.
9303   (when (stringp mark)
9304     (setq mark (aref mark 0)))
9305   ;; If no mark is given, then we check auto-expiring.
9306   (when (null mark)
9307     (setq mark gnus-del-mark))
9308   (when (and (not no-expire)
9309              gnus-newsgroup-auto-expire
9310              (memq mark gnus-auto-expirable-marks))
9311     (setq mark gnus-expirable-mark))
9312   (let ((article (or article (gnus-summary-article-number)))
9313         (old-mark (gnus-summary-article-mark article)))
9314     ;; Allow the backend to change the mark.
9315     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9316     (if (eq mark old-mark)
9317         t
9318       (unless article
9319         (error "No article on current line"))
9320       (if (not (if (or (= mark gnus-unread-mark)
9321                        (= mark gnus-ticked-mark)
9322                        (= mark gnus-dormant-mark))
9323                    (gnus-mark-article-as-unread article mark)
9324                  (gnus-mark-article-as-read article mark)))
9325           t
9326         ;; See whether the article is to be put in the cache.
9327         (and gnus-use-cache
9328              (not (= mark gnus-canceled-mark))
9329              (vectorp (gnus-summary-article-header article))
9330              (save-excursion
9331                (gnus-cache-possibly-enter-article
9332                 gnus-newsgroup-name article
9333                 (= mark gnus-ticked-mark)
9334                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9335
9336         (when (gnus-summary-goto-subject article nil t)
9337           (let ((buffer-read-only nil))
9338             (gnus-summary-show-thread)
9339             ;; Fix the mark.
9340             (gnus-summary-update-mark mark 'unread)
9341             t))))))
9342
9343 (defun gnus-summary-update-secondary-mark (article)
9344   "Update the secondary (read, process, cache) mark."
9345   (gnus-summary-update-mark
9346    (cond ((memq article gnus-newsgroup-processable)
9347           gnus-process-mark)
9348          ((memq article gnus-newsgroup-cached)
9349           gnus-cached-mark)
9350          ((memq article gnus-newsgroup-replied)
9351           gnus-replied-mark)
9352          ((memq article gnus-newsgroup-forwarded)
9353           gnus-forwarded-mark)
9354          ((memq article gnus-newsgroup-saved)
9355           gnus-saved-mark)
9356          ((memq article gnus-newsgroup-recent)
9357           gnus-recent-mark)
9358          ((memq article gnus-newsgroup-unseen)
9359           gnus-unseen-mark)
9360          (t gnus-no-mark))
9361    'replied)
9362   (when (gnus-visual-p 'summary-highlight 'highlight)
9363     (gnus-run-hooks 'gnus-summary-update-hook))
9364   t)
9365
9366 (defun gnus-summary-update-mark (mark type)
9367   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9368         (buffer-read-only nil))
9369     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9370     (when forward
9371       (when (looking-at "\r")
9372         (incf forward))
9373       (when (<= (+ forward (point)) (point-max))
9374         ;; Go to the right position on the line.
9375         (goto-char (+ forward (point)))
9376         ;; Replace the old mark with the new mark.
9377         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9378         ;; Optionally update the marks by some user rule.
9379         (when (eq type 'unread)
9380           (gnus-data-set-mark
9381            (gnus-data-find (gnus-summary-article-number)) mark)
9382           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9383
9384 (defun gnus-mark-article-as-read (article &optional mark)
9385   "Enter ARTICLE in the pertinent lists and remove it from others."
9386   ;; Make the article expirable.
9387   (let ((mark (or mark gnus-del-mark)))
9388     (setq gnus-newsgroup-expirable
9389           (if (= mark gnus-expirable-mark)
9390               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9391             (delq article gnus-newsgroup-expirable)))
9392     ;; Remove from unread and marked lists.
9393     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9394     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9395     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9396     (push (cons article mark) gnus-newsgroup-reads)
9397     ;; Possibly remove from cache, if that is used.
9398     (when gnus-use-cache
9399       (gnus-cache-enter-remove-article article))
9400     t))
9401
9402 (defun gnus-mark-article-as-unread (article &optional mark)
9403   "Enter ARTICLE in the pertinent lists and remove it from others."
9404   (let ((mark (or mark gnus-ticked-mark)))
9405     (if (<= article 0)
9406         (progn
9407           (gnus-error 1 "Can't mark negative article numbers")
9408           nil)
9409       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9410             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9411             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9412             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9413
9414       ;; Unsuppress duplicates?
9415       (when gnus-suppress-duplicates
9416         (gnus-dup-unsuppress-article article))
9417
9418       (cond ((= mark gnus-ticked-mark)
9419              (setq gnus-newsgroup-marked
9420                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9421             ((= mark gnus-dormant-mark)
9422              (setq gnus-newsgroup-dormant
9423                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9424             (t
9425              (setq gnus-newsgroup-unreads
9426                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9427       (gnus-pull article gnus-newsgroup-reads)
9428       t)))
9429
9430 (defalias 'gnus-summary-mark-as-unread-forward
9431   'gnus-summary-tick-article-forward)
9432 (make-obsolete 'gnus-summary-mark-as-unread-forward
9433                'gnus-summary-tick-article-forward)
9434 (defun gnus-summary-tick-article-forward (n)
9435   "Tick N articles forwards.
9436 If N is negative, tick backwards instead.
9437 The difference between N and the number of articles ticked is returned."
9438   (interactive "p")
9439   (gnus-summary-mark-forward n gnus-ticked-mark))
9440
9441 (defalias 'gnus-summary-mark-as-unread-backward
9442   'gnus-summary-tick-article-backward)
9443 (make-obsolete 'gnus-summary-mark-as-unread-backward
9444                'gnus-summary-tick-article-backward)
9445 (defun gnus-summary-tick-article-backward (n)
9446   "Tick N articles backwards.
9447 The difference between N and the number of articles ticked is returned."
9448   (interactive "p")
9449   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9450
9451 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9452 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9453 (defun gnus-summary-tick-article (&optional article clear-mark)
9454   "Mark current article as unread.
9455 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9456 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9457   (interactive)
9458   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9459                                        gnus-ticked-mark)))
9460
9461 (defun gnus-summary-mark-as-read-forward (n)
9462   "Mark N articles as read forwards.
9463 If N is negative, mark backwards instead.
9464 The difference between N and the actual number of articles marked is
9465 returned."
9466   (interactive "p")
9467   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9468
9469 (defun gnus-summary-mark-as-read-backward (n)
9470   "Mark the N articles as read backwards.
9471 The difference between N and the actual number of articles marked is
9472 returned."
9473   (interactive "p")
9474   (gnus-summary-mark-forward
9475    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9476
9477 (defun gnus-summary-mark-as-read (&optional article mark)
9478   "Mark current article as read.
9479 ARTICLE specifies the article to be marked as read.
9480 MARK specifies a string to be inserted at the beginning of the line."
9481   (gnus-summary-mark-article article mark))
9482
9483 (defun gnus-summary-clear-mark-forward (n)
9484   "Clear marks from N articles forward.
9485 If N is negative, clear backward instead.
9486 The difference between N and the number of marks cleared is returned."
9487   (interactive "p")
9488   (gnus-summary-mark-forward n gnus-unread-mark))
9489
9490 (defun gnus-summary-clear-mark-backward (n)
9491   "Clear marks from N articles backward.
9492 The difference between N and the number of marks cleared is returned."
9493   (interactive "p")
9494   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9495
9496 (defun gnus-summary-mark-unread-as-read ()
9497   "Intended to be used by `gnus-summary-mark-article-hook'."
9498   (when (memq gnus-current-article gnus-newsgroup-unreads)
9499     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9500
9501 (defun gnus-summary-mark-read-and-unread-as-read ()
9502   "Intended to be used by `gnus-summary-mark-article-hook'."
9503   (let ((mark (gnus-summary-article-mark)))
9504     (when (or (gnus-unread-mark-p mark)
9505               (gnus-read-mark-p mark))
9506       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9507
9508 (defun gnus-summary-mark-unread-as-ticked ()
9509   "Intended to be used by `gnus-summary-mark-article-hook'."
9510   (when (memq gnus-current-article gnus-newsgroup-unreads)
9511     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9512
9513 (defun gnus-summary-mark-region-as-read (point mark all)
9514   "Mark all unread articles between point and mark as read.
9515 If given a prefix, mark all articles between point and mark as read,
9516 even ticked and dormant ones."
9517   (interactive "r\nP")
9518   (save-excursion
9519     (let (article)
9520       (goto-char point)
9521       (beginning-of-line)
9522       (while (and
9523               (< (point) mark)
9524               (progn
9525                 (when (or all
9526                           (memq (setq article (gnus-summary-article-number))
9527                                 gnus-newsgroup-unreads))
9528                   (gnus-summary-mark-article article gnus-del-mark))
9529                 t)
9530               (gnus-summary-find-next))))))
9531
9532 (defun gnus-summary-mark-below (score mark)
9533   "Mark articles with score less than SCORE with MARK."
9534   (interactive "P\ncMark: ")
9535   (setq score (if score
9536                   (prefix-numeric-value score)
9537                 (or gnus-summary-default-score 0)))
9538   (save-excursion
9539     (set-buffer gnus-summary-buffer)
9540     (goto-char (point-min))
9541     (while
9542         (progn
9543           (and (< (gnus-summary-article-score) score)
9544                (gnus-summary-mark-article nil mark))
9545           (gnus-summary-find-next)))))
9546
9547 (defun gnus-summary-kill-below (&optional score)
9548   "Mark articles with score below SCORE as read."
9549   (interactive "P")
9550   (gnus-summary-mark-below score gnus-killed-mark))
9551
9552 (defun gnus-summary-clear-above (&optional score)
9553   "Clear all marks from articles with score above SCORE."
9554   (interactive "P")
9555   (gnus-summary-mark-above score gnus-unread-mark))
9556
9557 (defun gnus-summary-tick-above (&optional score)
9558   "Tick all articles with score above SCORE."
9559   (interactive "P")
9560   (gnus-summary-mark-above score gnus-ticked-mark))
9561
9562 (defun gnus-summary-mark-above (score mark)
9563   "Mark articles with score over SCORE with MARK."
9564   (interactive "P\ncMark: ")
9565   (setq score (if score
9566                   (prefix-numeric-value score)
9567                 (or gnus-summary-default-score 0)))
9568   (save-excursion
9569     (set-buffer gnus-summary-buffer)
9570     (goto-char (point-min))
9571     (while (and (progn
9572                   (when (> (gnus-summary-article-score) score)
9573                     (gnus-summary-mark-article nil mark))
9574                   t)
9575                 (gnus-summary-find-next)))))
9576
9577 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9578 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9579 (defun gnus-summary-limit-include-expunged (&optional no-error)
9580   "Display all the hidden articles that were expunged for low scores."
9581   (interactive)
9582   (let ((buffer-read-only nil))
9583     (let ((scored gnus-newsgroup-scored)
9584           headers h)
9585       (while scored
9586         (unless (gnus-summary-article-header (caar scored))
9587           (and (setq h (gnus-number-to-header (caar scored)))
9588                (< (cdar scored) gnus-summary-expunge-below)
9589                (push h headers)))
9590         (setq scored (cdr scored)))
9591       (if (not headers)
9592           (when (not no-error)
9593             (error "No expunged articles hidden"))
9594         (goto-char (point-min))
9595         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9596         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9597         (mapcar (lambda (x) (push (mail-header-number x)
9598                                   gnus-newsgroup-limit))
9599                 headers)
9600         (gnus-summary-prepare-unthreaded (nreverse headers))
9601         (goto-char (point-min))
9602         (gnus-summary-position-point)
9603         t))))
9604
9605 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9606   "Mark all unread articles in this newsgroup as read.
9607 If prefix argument ALL is non-nil, ticked and dormant articles will
9608 also be marked as read.
9609 If QUIETLY is non-nil, no questions will be asked.
9610 If TO-HERE is non-nil, it should be a point in the buffer.  All
9611 articles before (after, if REVERSE is set) this point will be marked as read.
9612 Note that this function will only catch up the unread article
9613 in the current summary buffer limitation.
9614 The number of articles marked as read is returned."
9615   (interactive "P")
9616   (prog1
9617       (save-excursion
9618         (when (or quietly
9619                   (not gnus-interactive-catchup) ;Without confirmation?
9620                   gnus-expert-user
9621                   (gnus-y-or-n-p
9622                    (if all
9623                        "Mark absolutely all articles as read? "
9624                      "Mark all unread articles as read? ")))
9625           (if (and not-mark
9626                    (not gnus-newsgroup-adaptive)
9627                    (not gnus-newsgroup-auto-expire)
9628                    (not gnus-suppress-duplicates)
9629                    (or (not gnus-use-cache)
9630                        (eq gnus-use-cache 'passive)))
9631               (progn
9632                 (when all
9633                   (setq gnus-newsgroup-marked nil
9634                         gnus-newsgroup-dormant nil))
9635                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9636             ;; We actually mark all articles as canceled, which we
9637             ;; have to do when using auto-expiry or adaptive scoring.
9638             (gnus-summary-show-all-threads)
9639             (if (and to-here reverse)
9640                 (progn
9641                   (goto-char to-here)
9642                   (while (and
9643                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9644                           (gnus-summary-find-next (not all) nil nil t))))
9645               (when (gnus-summary-first-subject (not all) t)
9646                 (while (and
9647                         (if to-here (< (point) to-here) t)
9648                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9649                         (gnus-summary-find-next (not all) nil nil t)))))
9650             (gnus-set-mode-line 'summary))
9651           t))
9652     (gnus-summary-position-point)))
9653
9654 (defun gnus-summary-catchup-to-here (&optional all)
9655   "Mark all unticked articles before the current one as read.
9656 If ALL is non-nil, also mark ticked and dormant articles as read."
9657   (interactive "P")
9658   (save-excursion
9659     (gnus-save-hidden-threads
9660       (let ((beg (point)))
9661         ;; We check that there are unread articles.
9662         (when (or all (gnus-summary-find-prev))
9663           (gnus-summary-catchup all t beg)))))
9664   (gnus-summary-position-point))
9665
9666 (defun gnus-summary-catchup-from-here (&optional all)
9667   "Mark all unticked articles after the current one as read.
9668 If ALL is non-nil, also mark ticked and dormant articles as read."
9669   (interactive "P")
9670   (save-excursion
9671     (gnus-save-hidden-threads
9672       (let ((beg (point)))
9673         ;; We check that there are unread articles.
9674         (when (or all (gnus-summary-find-next))
9675           (gnus-summary-catchup all t beg nil t)))))
9676
9677   (gnus-summary-position-point))
9678 (defun gnus-summary-catchup-all (&optional quietly)
9679   "Mark all articles in this newsgroup as read."
9680   (interactive "P")
9681   (gnus-summary-catchup t quietly))
9682
9683 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9684   "Mark all unread articles in this group as read, then exit.
9685 If prefix argument ALL is non-nil, all articles are marked as read.
9686 If QUIETLY is non-nil, no questions will be asked."
9687   (interactive "P")
9688   (when (gnus-summary-catchup all quietly nil 'fast)
9689     ;; Select next newsgroup or exit.
9690     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9691              (eq gnus-auto-select-next 'quietly))
9692         (gnus-summary-next-group nil)
9693       (gnus-summary-exit))))
9694
9695 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9696   "Mark all articles in this newsgroup as read, and then exit."
9697   (interactive "P")
9698   (gnus-summary-catchup-and-exit t quietly))
9699
9700 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9701   "Mark all articles in this group as read and select the next group.
9702 If given a prefix, mark all articles, unread as well as ticked, as
9703 read."
9704   (interactive "P")
9705   (save-excursion
9706     (gnus-summary-catchup all))
9707   (gnus-summary-next-group))
9708
9709 ;;;
9710 ;;; with article
9711 ;;;
9712
9713 (defmacro gnus-with-article (article &rest forms)
9714   "Select ARTICLE and perform FORMS in the original article buffer.
9715 Then replace the article with the result."
9716   `(progn
9717      ;; We don't want the article to be marked as read.
9718      (let (gnus-mark-article-hook)
9719        (gnus-summary-select-article t t nil ,article))
9720      (set-buffer gnus-original-article-buffer)
9721      ,@forms
9722      (if (not (gnus-check-backend-function
9723                'request-replace-article (car gnus-article-current)))
9724          (gnus-message 5 "Read-only group; not replacing")
9725        (unless (gnus-request-replace-article
9726                 ,article (car gnus-article-current)
9727                 (current-buffer) t)
9728          (error "Couldn't replace article")))
9729      ;; The cache and backlog have to be flushed somewhat.
9730      (when gnus-keep-backlog
9731        (gnus-backlog-remove-article
9732         (car gnus-article-current) (cdr gnus-article-current)))
9733      (when gnus-use-cache
9734        (gnus-cache-update-article
9735         (car gnus-article-current) (cdr gnus-article-current)))))
9736
9737 (put 'gnus-with-article 'lisp-indent-function 1)
9738 (put 'gnus-with-article 'edebug-form-spec '(form body))
9739
9740 ;; Thread-based commands.
9741
9742 (defun gnus-summary-articles-in-thread (&optional article)
9743   "Return a list of all articles in the current thread.
9744 If ARTICLE is non-nil, return all articles in the thread that starts
9745 with that article."
9746   (let* ((article (or article (gnus-summary-article-number)))
9747          (data (gnus-data-find-list article))
9748          (top-level (gnus-data-level (car data)))
9749          (top-subject
9750           (cond ((null gnus-thread-operation-ignore-subject)
9751                  (gnus-simplify-subject-re
9752                   (mail-header-subject (gnus-data-header (car data)))))
9753                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9754                  (gnus-simplify-subject-fuzzy
9755                   (mail-header-subject (gnus-data-header (car data)))))
9756                 (t nil)))
9757          (end-point (save-excursion
9758                       (if (gnus-summary-go-to-next-thread)
9759                           (point) (point-max))))
9760          articles)
9761     (while (and data
9762                 (< (gnus-data-pos (car data)) end-point))
9763       (when (or (not top-subject)
9764                 (string= top-subject
9765                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9766                              (gnus-simplify-subject-fuzzy
9767                               (mail-header-subject
9768                                (gnus-data-header (car data))))
9769                            (gnus-simplify-subject-re
9770                             (mail-header-subject
9771                              (gnus-data-header (car data)))))))
9772         (push (gnus-data-number (car data)) articles))
9773       (unless (and (setq data (cdr data))
9774                    (> (gnus-data-level (car data)) top-level))
9775         (setq data nil)))
9776     ;; Return the list of articles.
9777     (nreverse articles)))
9778
9779 (defun gnus-summary-rethread-current ()
9780   "Rethread the thread the current article is part of."
9781   (interactive)
9782   (let* ((gnus-show-threads t)
9783          (article (gnus-summary-article-number))
9784          (id (mail-header-id (gnus-summary-article-header)))
9785          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9786     (unless id
9787       (error "No article on the current line"))
9788     (gnus-rebuild-thread id)
9789     (gnus-summary-goto-subject article)))
9790
9791 (defun gnus-summary-reparent-thread ()
9792   "Make the current article child of the marked (or previous) article.
9793
9794 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9795 is non-nil or the Subject: of both articles are the same."
9796   (interactive)
9797   (unless (not (gnus-group-read-only-p))
9798     (error "The current newsgroup does not support article editing"))
9799   (unless (<= (length gnus-newsgroup-processable) 1)
9800     (error "No more than one article may be marked"))
9801   (save-window-excursion
9802     (let ((gnus-article-buffer " *reparent*")
9803           (current-article (gnus-summary-article-number))
9804           ;; First grab the marked article, otherwise one line up.
9805           (parent-article (if (not (null gnus-newsgroup-processable))
9806                               (car gnus-newsgroup-processable)
9807                             (save-excursion
9808                               (if (eq (forward-line -1) 0)
9809                                   (gnus-summary-article-number)
9810                                 (error "Beginning of summary buffer"))))))
9811       (unless (not (eq current-article parent-article))
9812         (error "An article may not be self-referential"))
9813       (let ((message-id (mail-header-id
9814                          (gnus-summary-article-header parent-article))))
9815         (unless (and message-id (not (equal message-id "")))
9816           (error "No message-id in desired parent"))
9817         (gnus-with-article current-article
9818           (save-restriction
9819             (goto-char (point-min))
9820             (message-narrow-to-head)
9821             (if (re-search-forward "^References: " nil t)
9822                 (progn
9823                   (re-search-forward "^[^ \t]" nil t)
9824                   (forward-line -1)
9825                   (end-of-line)
9826                   (insert " " message-id))
9827               (insert "References: " message-id "\n"))))
9828         (set-buffer gnus-summary-buffer)
9829         (gnus-summary-unmark-all-processable)
9830         (gnus-summary-update-article current-article)
9831         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9832             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9833         (gnus-summary-rethread-current)
9834         (gnus-message 3 "Article %d is now the child of article %d"
9835                       current-article parent-article)))))
9836
9837 (defun gnus-summary-toggle-threads (&optional arg)
9838   "Toggle showing conversation threads.
9839 If ARG is positive number, turn showing conversation threads on."
9840   (interactive "P")
9841   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9842     (setq gnus-show-threads
9843           (if (null arg) (not gnus-show-threads)
9844             (> (prefix-numeric-value arg) 0)))
9845     (gnus-summary-prepare)
9846     (gnus-summary-goto-subject current)
9847     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9848     (gnus-summary-position-point)))
9849
9850 (defun gnus-summary-show-all-threads ()
9851   "Show all threads."
9852   (interactive)
9853   (save-excursion
9854     (let ((buffer-read-only nil))
9855       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9856   (gnus-summary-position-point))
9857
9858 (defun gnus-summary-show-thread ()
9859   "Show thread subtrees.
9860 Returns nil if no thread was there to be shown."
9861   (interactive)
9862   (let ((buffer-read-only nil)
9863         (orig (point))
9864         ;; first goto end then to beg, to have point at beg after let
9865         (end (progn (end-of-line) (point)))
9866         (beg (progn (beginning-of-line) (point))))
9867     (prog1
9868         ;; Any hidden lines here?
9869         (search-forward "\r" end t)
9870       (subst-char-in-region beg end ?\^M ?\n t)
9871       (goto-char orig)
9872       (gnus-summary-position-point))))
9873
9874 (defun gnus-summary-maybe-hide-threads ()
9875   "If requested, hide the threads that should be hidden."
9876   (when (and gnus-show-threads
9877              gnus-thread-hide-subtree)
9878     (gnus-summary-hide-all-threads
9879      (if (or (consp gnus-thread-hide-subtree)
9880              (gnus-functionp gnus-thread-hide-subtree))
9881          (gnus-make-predicate gnus-thread-hide-subtree)
9882        nil))))
9883
9884 ;;; Hiding predicates.
9885
9886 (defun gnus-article-unread-p (header)
9887   (memq (mail-header-number header) gnus-newsgroup-unreads))
9888
9889 (defun gnus-article-unseen-p (header)
9890   (memq (mail-header-number header) gnus-newsgroup-unseen))
9891
9892 (defun gnus-map-articles (predicate articles)
9893   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9894   (apply 'gnus-or (mapcar predicate
9895                           (mapcar 'gnus-summary-article-header articles))))
9896
9897 (defun gnus-summary-hide-all-threads (&optional predicate)
9898   "Hide all thread subtrees.
9899 If PREDICATE is supplied, threads that satisfy this predicate
9900 will not be hidden."
9901   (interactive)
9902   (save-excursion
9903     (goto-char (point-min))
9904     (let ((end nil))
9905       (while (not end)
9906         (when (or (not predicate)
9907                   (gnus-map-articles
9908                    predicate (gnus-summary-article-children)))
9909             (gnus-summary-hide-thread))
9910         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9911   (gnus-summary-position-point))
9912
9913 (defun gnus-summary-hide-thread ()
9914   "Hide thread subtrees.
9915 If PREDICATE is supplied, threads that satisfy this predicate
9916 will not be hidden.
9917 Returns nil if no threads were there to be hidden."
9918   (interactive)
9919   (let ((buffer-read-only nil)
9920         (start (point))
9921         (article (gnus-summary-article-number)))
9922     (goto-char start)
9923     ;; Go forward until either the buffer ends or the subthread
9924     ;; ends.
9925     (when (and (not (eobp))
9926                (or (zerop (gnus-summary-next-thread 1 t))
9927                    (goto-char (point-max))))
9928       (prog1
9929           (if (and (> (point) start)
9930                    (search-backward "\n" start t))
9931               (progn
9932                 (subst-char-in-region start (point) ?\n ?\^M)
9933                 (gnus-summary-goto-subject article))
9934             (goto-char start)
9935             nil)))))
9936
9937 (defun gnus-summary-go-to-next-thread (&optional previous)
9938   "Go to the same level (or less) next thread.
9939 If PREVIOUS is non-nil, go to previous thread instead.
9940 Return the article number moved to, or nil if moving was impossible."
9941   (let ((level (gnus-summary-thread-level))
9942         (way (if previous -1 1))
9943         (beg (point)))
9944     (forward-line way)
9945     (while (and (not (eobp))
9946                 (< level (gnus-summary-thread-level)))
9947       (forward-line way))
9948     (if (eobp)
9949         (progn
9950           (goto-char beg)
9951           nil)
9952       (setq beg (point))
9953       (prog1
9954           (gnus-summary-article-number)
9955         (goto-char beg)))))
9956
9957 (defun gnus-summary-next-thread (n &optional silent)
9958   "Go to the same level next N'th thread.
9959 If N is negative, search backward instead.
9960 Returns the difference between N and the number of skips actually
9961 done.
9962
9963 If SILENT, don't output messages."
9964   (interactive "p")
9965   (let ((backward (< n 0))
9966         (n (abs n)))
9967     (while (and (> n 0)
9968                 (gnus-summary-go-to-next-thread backward))
9969       (decf n))
9970     (unless silent
9971       (gnus-summary-position-point))
9972     (when (and (not silent) (/= 0 n))
9973       (gnus-message 7 "No more threads"))
9974     n))
9975
9976 (defun gnus-summary-prev-thread (n)
9977   "Go to the same level previous N'th thread.
9978 Returns the difference between N and the number of skips actually
9979 done."
9980   (interactive "p")
9981   (gnus-summary-next-thread (- n)))
9982
9983 (defun gnus-summary-go-down-thread ()
9984   "Go down one level in the current thread."
9985   (let ((children (gnus-summary-article-children)))
9986     (when children
9987       (gnus-summary-goto-subject (car children)))))
9988
9989 (defun gnus-summary-go-up-thread ()
9990   "Go up one level in the current thread."
9991   (let ((parent (gnus-summary-article-parent)))
9992     (when parent
9993       (gnus-summary-goto-subject parent))))
9994
9995 (defun gnus-summary-down-thread (n)
9996   "Go down thread N steps.
9997 If N is negative, go up instead.
9998 Returns the difference between N and how many steps down that were
9999 taken."
10000   (interactive "p")
10001   (let ((up (< n 0))
10002         (n (abs n)))
10003     (while (and (> n 0)
10004                 (if up (gnus-summary-go-up-thread)
10005                   (gnus-summary-go-down-thread)))
10006       (setq n (1- n)))
10007     (gnus-summary-position-point)
10008     (when (/= 0 n)
10009       (gnus-message 7 "Can't go further"))
10010     n))
10011
10012 (defun gnus-summary-up-thread (n)
10013   "Go up thread N steps.
10014 If N is negative, go down instead.
10015 Returns the difference between N and how many steps down that were
10016 taken."
10017   (interactive "p")
10018   (gnus-summary-down-thread (- n)))
10019
10020 (defun gnus-summary-top-thread ()
10021   "Go to the top of the thread."
10022   (interactive)
10023   (while (gnus-summary-go-up-thread))
10024   (gnus-summary-article-number))
10025
10026 (defun gnus-summary-kill-thread (&optional unmark)
10027   "Mark articles under current thread as read.
10028 If the prefix argument is positive, remove any kinds of marks.
10029 If the prefix argument is negative, tick articles instead."
10030   (interactive "P")
10031   (when unmark
10032     (setq unmark (prefix-numeric-value unmark)))
10033   (let ((articles (gnus-summary-articles-in-thread)))
10034     (save-excursion
10035       ;; Expand the thread.
10036       (gnus-summary-show-thread)
10037       ;; Mark all the articles.
10038       (while articles
10039         (gnus-summary-goto-subject (car articles))
10040         (cond ((null unmark)
10041                (gnus-summary-mark-article-as-read gnus-killed-mark))
10042               ((> unmark 0)
10043                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10044               (t
10045                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10046         (setq articles (cdr articles))))
10047     ;; Hide killed subtrees.
10048     (and (null unmark)
10049          gnus-thread-hide-killed
10050          (gnus-summary-hide-thread))
10051     ;; If marked as read, go to next unread subject.
10052     (when (null unmark)
10053       ;; Go to next unread subject.
10054       (gnus-summary-next-subject 1 t)))
10055   (gnus-set-mode-line 'summary))
10056
10057 ;; Summary sorting commands
10058
10059 (defun gnus-summary-sort-by-number (&optional reverse)
10060   "Sort the summary buffer by article number.
10061 Argument REVERSE means reverse order."
10062   (interactive "P")
10063   (gnus-summary-sort 'number reverse))
10064
10065 (defun gnus-summary-sort-by-author (&optional reverse)
10066   "Sort the summary buffer by author name alphabetically.
10067 If `case-fold-search' is non-nil, case of letters is ignored.
10068 Argument REVERSE means reverse order."
10069   (interactive "P")
10070   (gnus-summary-sort 'author reverse))
10071
10072 (defun gnus-summary-sort-by-subject (&optional reverse)
10073   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10074 If `case-fold-search' is non-nil, case of letters is ignored.
10075 Argument REVERSE means reverse order."
10076   (interactive "P")
10077   (gnus-summary-sort 'subject reverse))
10078
10079 (defun gnus-summary-sort-by-date (&optional reverse)
10080   "Sort the summary buffer by date.
10081 Argument REVERSE means reverse order."
10082   (interactive "P")
10083   (gnus-summary-sort 'date reverse))
10084
10085 (defun gnus-summary-sort-by-score (&optional reverse)
10086   "Sort the summary buffer by score.
10087 Argument REVERSE means reverse order."
10088   (interactive "P")
10089   (gnus-summary-sort 'score reverse))
10090
10091 (defun gnus-summary-sort-by-lines (&optional reverse)
10092   "Sort the summary buffer by the number of lines.
10093 Argument REVERSE means reverse order."
10094   (interactive "P")
10095   (gnus-summary-sort 'lines reverse))
10096
10097 (defun gnus-summary-sort-by-chars (&optional reverse)
10098   "Sort the summary buffer by article length.
10099 Argument REVERSE means reverse order."
10100   (interactive "P")
10101   (gnus-summary-sort 'chars reverse))
10102
10103 (defun gnus-summary-sort-by-original (&optional reverse)
10104   "Sort the summary buffer using the default sorting method.
10105 Argument REVERSE means reverse order."
10106   (interactive "P")
10107   (let* ((buffer-read-only)
10108          (gnus-summary-prepare-hook nil))
10109     ;; We do the sorting by regenerating the threads.
10110     (gnus-summary-prepare)
10111     ;; Hide subthreads if needed.
10112     (gnus-summary-maybe-hide-threads)))
10113
10114 (defun gnus-summary-sort (predicate reverse)
10115   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10116   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10117          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10118          (gnus-thread-sort-functions
10119           (if (not reverse)
10120               thread
10121             `(lambda (t1 t2)
10122                (,thread t2 t1))))
10123          (gnus-sort-gathered-threads-function
10124           gnus-thread-sort-functions)
10125          (gnus-article-sort-functions
10126           (if (not reverse)
10127               article
10128             `(lambda (t1 t2)
10129                (,article t2 t1))))
10130          (buffer-read-only)
10131          (gnus-summary-prepare-hook nil))
10132     ;; We do the sorting by regenerating the threads.
10133     (gnus-summary-prepare)
10134     ;; Hide subthreads if needed.
10135     (gnus-summary-maybe-hide-threads)))
10136
10137 ;; Summary saving commands.
10138
10139 (defun gnus-summary-save-article (&optional n not-saved)
10140   "Save the current article using the default saver function.
10141 If N is a positive number, save the N next articles.
10142 If N is a negative number, save the N previous articles.
10143 If N is nil and any articles have been marked with the process mark,
10144 save those articles instead.
10145 The variable `gnus-default-article-saver' specifies the saver function."
10146   (interactive "P")
10147   (let* ((articles (gnus-summary-work-articles n))
10148          (save-buffer (save-excursion
10149                         (nnheader-set-temp-buffer " *Gnus Save*")))
10150          (num (length articles))
10151          header file)
10152     (dolist (article articles)
10153       (setq header (gnus-summary-article-header article))
10154       (if (not (vectorp header))
10155           ;; This is a pseudo-article.
10156           (if (assq 'name header)
10157               (gnus-copy-file (cdr (assq 'name header)))
10158             (gnus-message 1 "Article %d is unsaveable" article))
10159         ;; This is a real article.
10160         (save-window-excursion
10161           (let ((gnus-display-mime-function nil)
10162                 (gnus-article-prepare-hook nil))
10163             (gnus-summary-select-article t nil nil article)))
10164         (save-excursion
10165           (set-buffer save-buffer)
10166           (erase-buffer)
10167           (insert-buffer-substring gnus-original-article-buffer))
10168         (setq file (gnus-article-save save-buffer file num))
10169         (gnus-summary-remove-process-mark article)
10170         (unless not-saved
10171           (gnus-summary-set-saved-mark article))))
10172     (gnus-kill-buffer save-buffer)
10173     (gnus-summary-position-point)
10174     (gnus-set-mode-line 'summary)
10175     n))
10176
10177 (defun gnus-summary-pipe-output (&optional arg)
10178   "Pipe the current article to a subprocess.
10179 If N is a positive number, pipe the N next articles.
10180 If N is a negative number, pipe the N previous articles.
10181 If N is nil and any articles have been marked with the process mark,
10182 pipe those articles instead."
10183   (interactive "P")
10184   (require 'gnus-art)
10185   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10186     (gnus-summary-save-article arg t))
10187   (let ((buffer (get-buffer "*Shell Command Output*")))
10188     (if (and buffer
10189              (with-current-buffer buffer (> (point-max) (point-min))))
10190         (gnus-configure-windows 'pipe))))
10191
10192 (defun gnus-summary-save-article-mail (&optional arg)
10193   "Append the current article to an mail file.
10194 If N is a positive number, save the N next articles.
10195 If N is a negative number, save the N previous articles.
10196 If N is nil and any articles have been marked with the process mark,
10197 save those articles instead."
10198   (interactive "P")
10199   (require 'gnus-art)
10200   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10201     (gnus-summary-save-article arg)))
10202
10203 (defun gnus-summary-save-article-rmail (&optional arg)
10204   "Append the current article to an rmail file.
10205 If N is a positive number, save the N next articles.
10206 If N is a negative number, save the N previous articles.
10207 If N is nil and any articles have been marked with the process mark,
10208 save those articles instead."
10209   (interactive "P")
10210   (require 'gnus-art)
10211   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10212     (gnus-summary-save-article arg)))
10213
10214 (defun gnus-summary-save-article-file (&optional arg)
10215   "Append the current article to a file.
10216 If N is a positive number, save the N next articles.
10217 If N is a negative number, save the N previous articles.
10218 If N is nil and any articles have been marked with the process mark,
10219 save those articles instead."
10220   (interactive "P")
10221   (require 'gnus-art)
10222   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10223     (gnus-summary-save-article arg)))
10224
10225 (defun gnus-summary-write-article-file (&optional arg)
10226   "Write the current article to a file, deleting the previous file.
10227 If N is a positive number, save the N next articles.
10228 If N is a negative number, save the N previous articles.
10229 If N is nil and any articles have been marked with the process mark,
10230 save those articles instead."
10231   (interactive "P")
10232   (require 'gnus-art)
10233   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10234     (gnus-summary-save-article arg)))
10235
10236 (defun gnus-summary-save-article-body-file (&optional arg)
10237   "Append the current article body to a file.
10238 If N is a positive number, save the N next articles.
10239 If N is a negative number, save the N previous articles.
10240 If N is nil and any articles have been marked with the process mark,
10241 save those articles instead."
10242   (interactive "P")
10243   (require 'gnus-art)
10244   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10245     (gnus-summary-save-article arg)))
10246
10247 (defun gnus-summary-muttprint (&optional arg)
10248   "Print the current article using Muttprint.
10249 If N is a positive number, save the N next articles.
10250 If N is a negative number, save the N previous articles.
10251 If N is nil and any articles have been marked with the process mark,
10252 save those articles instead."
10253   (interactive "P")
10254   (require 'gnus-art)
10255   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10256     (gnus-summary-save-article arg t)))
10257
10258 (defun gnus-summary-pipe-message (program)
10259   "Pipe the current article through PROGRAM."
10260   (interactive "sProgram: ")
10261   (gnus-summary-select-article)
10262   (let ((mail-header-separator ""))
10263     (gnus-eval-in-buffer-window gnus-article-buffer
10264       (save-restriction
10265         (widen)
10266         (let ((start (window-start))
10267               buffer-read-only)
10268           (message-pipe-buffer-body program)
10269           (set-window-start (get-buffer-window (current-buffer)) start))))))
10270
10271 (defun gnus-get-split-value (methods)
10272   "Return a value based on the split METHODS."
10273   (let (split-name method result match)
10274     (when methods
10275       (save-excursion
10276         (set-buffer gnus-original-article-buffer)
10277         (save-restriction
10278           (nnheader-narrow-to-headers)
10279           (while (and methods (not split-name))
10280             (goto-char (point-min))
10281             (setq method (pop methods))
10282             (setq match (car method))
10283             (when (cond
10284                    ((stringp match)
10285                     ;; Regular expression.
10286                     (ignore-errors
10287                       (re-search-forward match nil t)))
10288                    ((gnus-functionp match)
10289                     ;; Function.
10290                     (save-restriction
10291                       (widen)
10292                       (setq result (funcall match gnus-newsgroup-name))))
10293                    ((consp match)
10294                     ;; Form.
10295                     (save-restriction
10296                       (widen)
10297                       (setq result (eval match)))))
10298               (setq split-name (cdr method))
10299               (cond ((stringp result)
10300                      (push (expand-file-name
10301                             result gnus-article-save-directory)
10302                            split-name))
10303                     ((consp result)
10304                      (setq split-name (append result split-name)))))))))
10305     (nreverse split-name)))
10306
10307 (defun gnus-valid-move-group-p (group)
10308   (and (boundp group)
10309        (symbol-name group)
10310        (symbol-value group)
10311        (gnus-get-function (gnus-find-method-for-group
10312                            (symbol-name group)) 'request-accept-article t)))
10313
10314 (defun gnus-read-move-group-name (prompt default articles prefix)
10315   "Read a group name."
10316   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10317          (minibuffer-confirm-incomplete nil) ; XEmacs
10318          (prom
10319           (format "%s %s to:"
10320                   prompt
10321                   (if (> (length articles) 1)
10322                       (format "these %d articles" (length articles))
10323                     "this article")))
10324          (to-newsgroup
10325           (cond
10326            ((null split-name)
10327             (gnus-completing-read-with-default
10328              default prom
10329              gnus-active-hashtb
10330              'gnus-valid-move-group-p
10331              nil prefix
10332              'gnus-group-history))
10333            ((= 1 (length split-name))
10334             (gnus-completing-read-with-default
10335              (car split-name) prom
10336              gnus-active-hashtb
10337              'gnus-valid-move-group-p
10338              nil nil
10339              'gnus-group-history))
10340            (t
10341             (gnus-completing-read-with-default
10342              nil prom
10343              (mapcar (lambda (el) (list el))
10344                      (nreverse split-name))
10345              nil nil nil
10346              'gnus-group-history))))
10347          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10348     (when to-newsgroup
10349       (if (or (string= to-newsgroup "")
10350               (string= to-newsgroup prefix))
10351           (setq to-newsgroup default))
10352       (unless to-newsgroup
10353         (error "No group name entered"))
10354       (or (gnus-active to-newsgroup)
10355           (gnus-activate-group to-newsgroup nil nil to-method)
10356           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10357                                      to-newsgroup))
10358               (or (and (gnus-request-create-group to-newsgroup to-method)
10359                        (gnus-activate-group
10360                         to-newsgroup nil nil to-method)
10361                        (gnus-subscribe-group to-newsgroup))
10362                   (error "Couldn't create group %s" to-newsgroup)))
10363           (error "No such group: %s" to-newsgroup)))
10364     to-newsgroup))
10365
10366 (defun gnus-summary-save-parts (type dir n &optional reverse)
10367   "Save parts matching TYPE to DIR.
10368 If REVERSE, save parts that do not match TYPE."
10369   (interactive
10370    (list (read-string "Save parts of type: "
10371                       (or (car gnus-summary-save-parts-type-history)
10372                           gnus-summary-save-parts-default-mime)
10373                       'gnus-summary-save-parts-type-history)
10374          (setq gnus-summary-save-parts-last-directory
10375                (read-file-name "Save to directory: "
10376                                gnus-summary-save-parts-last-directory
10377                                nil t))
10378          current-prefix-arg))
10379   (gnus-summary-iterate n
10380     (let ((gnus-display-mime-function nil)
10381           (gnus-inhibit-treatment t))
10382       (gnus-summary-select-article))
10383     (save-excursion
10384       (set-buffer gnus-article-buffer)
10385       (let ((handles (or gnus-article-mime-handles
10386                          (mm-dissect-buffer nil gnus-article-loose-mime)
10387                          (mm-uu-dissect))))
10388         (when handles
10389           (gnus-summary-save-parts-1 type dir handles reverse)
10390           (unless gnus-article-mime-handles ;; Don't destroy this case.
10391             (mm-destroy-parts handles)))))))
10392
10393 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10394   (if (stringp (car handle))
10395       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10396               (cdr handle))
10397     (when (if reverse
10398               (not (string-match type (mm-handle-media-type handle)))
10399             (string-match type (mm-handle-media-type handle)))
10400       (let ((file (expand-file-name
10401                    (file-name-nondirectory
10402                     (or
10403                      (mail-content-type-get
10404                       (mm-handle-disposition handle) 'filename)
10405                      (concat gnus-newsgroup-name
10406                              "." (number-to-string
10407                                   (cdr gnus-article-current)))))
10408                    dir)))
10409         (unless (file-exists-p file)
10410           (mm-save-part-to-file handle file))))))
10411
10412 ;; Summary extract commands
10413
10414 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10415   (let ((buffer-read-only nil)
10416         (article (gnus-summary-article-number))
10417         after-article b e)
10418     (unless (gnus-summary-goto-subject article)
10419       (error "No such article: %d" article))
10420     (gnus-summary-position-point)
10421     ;; If all commands are to be bunched up on one line, we collect
10422     ;; them here.
10423     (unless gnus-view-pseudos-separately
10424       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10425             files action)
10426         (while ps
10427           (setq action (cdr (assq 'action (car ps))))
10428           (setq files (list (cdr (assq 'name (car ps)))))
10429           (while (and ps (cdr ps)
10430                       (string= (or action "1")
10431                                (or (cdr (assq 'action (cadr ps))) "2")))
10432             (push (cdr (assq 'name (cadr ps))) files)
10433             (setcdr ps (cddr ps)))
10434           (when files
10435             (when (not (string-match "%s" action))
10436               (push " " files))
10437             (push " " files)
10438             (when (assq 'execute (car ps))
10439               (setcdr (assq 'execute (car ps))
10440                       (funcall (if (string-match "%s" action)
10441                                    'format 'concat)
10442                                action
10443                                (mapconcat
10444                                 (lambda (f)
10445                                   (if (equal f " ")
10446                                       f
10447                                     (mm-quote-arg f)))
10448                                 files " ")))))
10449           (setq ps (cdr ps)))))
10450     (if (and gnus-view-pseudos (not not-view))
10451         (while pslist
10452           (when (assq 'execute (car pslist))
10453             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10454                                   (eq gnus-view-pseudos 'not-confirm)))
10455           (setq pslist (cdr pslist)))
10456       (save-excursion
10457         (while pslist
10458           (setq after-article (or (cdr (assq 'article (car pslist)))
10459                                   (gnus-summary-article-number)))
10460           (gnus-summary-goto-subject after-article)
10461           (forward-line 1)
10462           (setq b (point))
10463           (insert "    " (file-name-nondirectory
10464                           (cdr (assq 'name (car pslist))))
10465                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10466           (setq e (point))
10467           (forward-line -1)             ; back to `b'
10468           (gnus-add-text-properties
10469            b (1- e) (list 'gnus-number gnus-reffed-article-number
10470                           gnus-mouse-face-prop gnus-mouse-face))
10471           (gnus-data-enter
10472            after-article gnus-reffed-article-number
10473            gnus-unread-mark b (car pslist) 0 (- e b))
10474           (setq gnus-newsgroup-unreads
10475                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10476                                          gnus-reffed-article-number))
10477           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10478           (setq pslist (cdr pslist)))))))
10479
10480 (defun gnus-pseudos< (p1 p2)
10481   (let ((c1 (cdr (assq 'action p1)))
10482         (c2 (cdr (assq 'action p2))))
10483     (and c1 c2 (string< c1 c2))))
10484
10485 (defun gnus-request-pseudo-article (props)
10486   (cond ((assq 'execute props)
10487          (gnus-execute-command (cdr (assq 'execute props)))))
10488   (let ((gnus-current-article (gnus-summary-article-number)))
10489     (gnus-run-hooks 'gnus-mark-article-hook)))
10490
10491 (defun gnus-execute-command (command &optional automatic)
10492   (save-excursion
10493     (gnus-article-setup-buffer)
10494     (set-buffer gnus-article-buffer)
10495     (setq buffer-read-only nil)
10496     (let ((command (if automatic command
10497                      (read-string "Command: " (cons command 0)))))
10498       (erase-buffer)
10499       (insert "$ " command "\n\n")
10500       (if gnus-view-pseudo-asynchronously
10501           (start-process "gnus-execute" (current-buffer) shell-file-name
10502                          shell-command-switch command)
10503         (call-process shell-file-name nil t nil
10504                       shell-command-switch command)))))
10505
10506 ;; Summary kill commands.
10507
10508 (defun gnus-summary-edit-global-kill (article)
10509   "Edit the \"global\" kill file."
10510   (interactive (list (gnus-summary-article-number)))
10511   (gnus-group-edit-global-kill article))
10512
10513 (defun gnus-summary-edit-local-kill ()
10514   "Edit a local kill file applied to the current newsgroup."
10515   (interactive)
10516   (setq gnus-current-headers (gnus-summary-article-header))
10517   (gnus-group-edit-local-kill
10518    (gnus-summary-article-number) gnus-newsgroup-name))
10519
10520 ;;; Header reading.
10521
10522 (defun gnus-read-header (id &optional header)
10523   "Read the headers of article ID and enter them into the Gnus system."
10524   (let ((group gnus-newsgroup-name)
10525         (gnus-override-method
10526          (or
10527           gnus-override-method
10528           (and (gnus-news-group-p gnus-newsgroup-name)
10529                (car (gnus-refer-article-methods)))))
10530         where)
10531     ;; First we check to see whether the header in question is already
10532     ;; fetched.
10533     (if (stringp id)
10534         ;; This is a Message-ID.
10535         (setq header (or header (gnus-id-to-header id)))
10536       ;; This is an article number.
10537       (setq header (or header (gnus-summary-article-header id))))
10538     (if (and header
10539              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10540         ;; We have found the header.
10541         header
10542       ;; If this is a sparse article, we have to nix out its
10543       ;; previous entry in the thread hashtb.
10544       (when (and header
10545                  (gnus-summary-article-sparse-p (mail-header-number header)))
10546         (let* ((parent (gnus-parent-id (mail-header-references header)))
10547                (thread (and parent (gnus-id-to-thread parent))))
10548           (when thread
10549             (delq (assq header thread) thread))))
10550       ;; We have to really fetch the header to this article.
10551       (save-excursion
10552         (set-buffer nntp-server-buffer)
10553         (when (setq where (gnus-request-head id group))
10554           (nnheader-fold-continuation-lines)
10555           (goto-char (point-max))
10556           (insert ".\n")
10557           (goto-char (point-min))
10558           (insert "211 ")
10559           (princ (cond
10560                   ((numberp id) id)
10561                   ((cdr where) (cdr where))
10562                   (header (mail-header-number header))
10563                   (t gnus-reffed-article-number))
10564                  (current-buffer))
10565           (insert " Article retrieved.\n"))
10566         (if (or (not where)
10567                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10568             ()                          ; Malformed head.
10569           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10570             (when (and (stringp id)
10571                        (not (string= (gnus-group-real-name group)
10572                                      (car where))))
10573               ;; If we fetched by Message-ID and the article came
10574               ;; from a different group, we fudge some bogus article
10575               ;; numbers for this article.
10576               (mail-header-set-number header gnus-reffed-article-number))
10577             (save-excursion
10578               (set-buffer gnus-summary-buffer)
10579               (decf gnus-reffed-article-number)
10580               (gnus-remove-header (mail-header-number header))
10581               (push header gnus-newsgroup-headers)
10582               (setq gnus-current-headers header)
10583               (push (mail-header-number header) gnus-newsgroup-limit)))
10584           header)))))
10585
10586 (defun gnus-remove-header (number)
10587   "Remove header NUMBER from `gnus-newsgroup-headers'."
10588   (if (and gnus-newsgroup-headers
10589            (= number (mail-header-number (car gnus-newsgroup-headers))))
10590       (pop gnus-newsgroup-headers)
10591     (let ((headers gnus-newsgroup-headers))
10592       (while (and (cdr headers)
10593                   (not (= number (mail-header-number (cadr headers)))))
10594         (pop headers))
10595       (when (cdr headers)
10596         (setcdr headers (cddr headers))))))
10597
10598 ;;;
10599 ;;; summary highlights
10600 ;;;
10601
10602 (defun gnus-highlight-selected-summary ()
10603   "Highlight selected article in summary buffer."
10604   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10605   (when gnus-summary-selected-face
10606     (save-excursion
10607       (let* ((beg (progn (beginning-of-line) (point)))
10608              (end (progn (end-of-line) (point)))
10609              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10610              (from (if (get-text-property beg gnus-mouse-face-prop)
10611                        beg
10612                      (or (next-single-property-change
10613                           beg gnus-mouse-face-prop nil end)
10614                          beg)))
10615              (to
10616               (if (= from end)
10617                   (- from 2)
10618                 (or (next-single-property-change
10619                      from gnus-mouse-face-prop nil end)
10620                     end))))
10621         ;; If no mouse-face prop on line we will have to = from = end,
10622         ;; so we highlight the entire line instead.
10623         (when (= (+ to 2) from)
10624           (setq from beg)
10625           (setq to end))
10626         (if gnus-newsgroup-selected-overlay
10627             ;; Move old overlay.
10628             (gnus-move-overlay
10629              gnus-newsgroup-selected-overlay from to (current-buffer))
10630           ;; Create new overlay.
10631           (gnus-overlay-put
10632            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10633            'face gnus-summary-selected-face))))))
10634
10635 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10636 (defun gnus-summary-highlight-line ()
10637   "Highlight current line according to `gnus-summary-highlight'."
10638   (let* ((list gnus-summary-highlight)
10639          (p (point))
10640          (end (progn (end-of-line) (point)))
10641          ;; now find out where the line starts and leave point there.
10642          (beg (progn (beginning-of-line) (point)))
10643          (article (gnus-summary-article-number))
10644          (score (or (cdr (assq (or article gnus-current-article)
10645                                gnus-newsgroup-scored))
10646                     gnus-summary-default-score 0))
10647          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10648          (inhibit-read-only t))
10649     ;; Eval the cars of the lists until we find a match.
10650     (let ((default gnus-summary-default-score)
10651           (default-high gnus-summary-default-high-score)
10652           (default-low gnus-summary-default-low-score))
10653       (while (and list
10654                   (not (eval (caar list))))
10655         (setq list (cdr list))))
10656     (let ((face (cdar list)))
10657       (unless (eq face (get-text-property beg 'face))
10658         (gnus-put-text-property-excluding-characters-with-faces
10659          beg end 'face
10660          (setq face (if (boundp face) (symbol-value face) face)))
10661         (when gnus-summary-highlight-line-function
10662           (funcall gnus-summary-highlight-line-function article face))))
10663     (goto-char p)))
10664
10665 (defun gnus-update-read-articles (group unread &optional compute)
10666   "Update the list of read articles in GROUP.
10667 UNREAD is a sorted list."
10668   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10669          (entry (gnus-gethash group gnus-newsrc-hashtb))
10670          (info (nth 2 entry))
10671          (prev 1)
10672          read)
10673     (if (or (not info) (not active))
10674         ;; There is no info on this group if it was, in fact,
10675         ;; killed.  Gnus stores no information on killed groups, so
10676         ;; there's nothing to be done.
10677         ;; One could store the information somewhere temporarily,
10678         ;; perhaps...  Hmmm...
10679         ()
10680       ;; Remove any negative articles numbers.
10681       (while (and unread (< (car unread) 0))
10682         (setq unread (cdr unread)))
10683       ;; Remove any expired article numbers
10684       (while (and unread (< (car unread) (car active)))
10685         (setq unread (cdr unread)))
10686       ;; Compute the ranges of read articles by looking at the list of
10687       ;; unread articles.
10688       (while unread
10689         (when (/= (car unread) prev)
10690           (push (if (= prev (1- (car unread))) prev
10691                   (cons prev (1- (car unread))))
10692                 read))
10693         (setq prev (1+ (car unread)))
10694         (setq unread (cdr unread)))
10695       (when (<= prev (cdr active))
10696         (push (cons prev (cdr active)) read))
10697       (setq read (if (> (length read) 1) (nreverse read) read))
10698       (if compute
10699           read
10700         (save-excursion
10701           (let (setmarkundo)
10702             ;; Propagate the read marks to the backend.
10703             (when (gnus-check-backend-function 'request-set-mark group)
10704               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10705                     (add (gnus-remove-from-range read (gnus-info-read info))))
10706                 (when (or add del)
10707                   (unless (gnus-check-group group)
10708                     (error "Can't open server for %s" group))
10709                   (gnus-request-set-mark
10710                    group (delq nil (list (if add (list add 'add '(read)))
10711                                          (if del (list del 'del '(read))))))
10712                   (setq setmarkundo
10713                         `(gnus-request-set-mark
10714                           ,group
10715                           ',(delq nil (list
10716                                        (if del (list del 'add '(read)))
10717                                        (if add (list add 'del '(read))))))))))
10718             (set-buffer gnus-group-buffer)
10719             (gnus-undo-register
10720               `(progn
10721                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10722                  (gnus-info-set-read ',info ',(gnus-info-read info))
10723                  (gnus-get-unread-articles-in-group ',info
10724                                                     (gnus-active ,group))
10725                  (gnus-group-update-group ,group t)
10726                  ,setmarkundo))))
10727         ;; Enter this list into the group info.
10728         (gnus-info-set-read info read)
10729         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10730         (gnus-get-unread-articles-in-group info (gnus-active group))
10731         t))))
10732
10733 (defun gnus-offer-save-summaries ()
10734   "Offer to save all active summary buffers."
10735   (let (buffers)
10736     ;; Go through all buffers and find all summaries.
10737     (dolist (buffer (buffer-list))
10738       (when (and (setq buffer (buffer-name buffer))
10739                  (string-match "Summary" buffer)
10740                  (save-excursion
10741                    (set-buffer buffer)
10742                    ;; We check that this is, indeed, a summary buffer.
10743                    (and (eq major-mode 'gnus-summary-mode)
10744                         ;; Also make sure this isn't bogus.
10745                         gnus-newsgroup-prepared
10746                         ;; Also make sure that this isn't a
10747                         ;; dead summary buffer.
10748                         (not gnus-dead-summary-mode))))
10749         (push buffer buffers)))
10750     ;; Go through all these summary buffers and offer to save them.
10751     (when buffers
10752       (save-excursion
10753         (map-y-or-n-p
10754          "Update summary buffer %s? "
10755          (lambda (buf)
10756            (switch-to-buffer buf)
10757            (gnus-summary-exit))
10758          buffers)))))
10759
10760 (defun gnus-summary-setup-default-charset ()
10761   "Setup newsgroup default charset."
10762   (if (equal gnus-newsgroup-name "nndraft:drafts")
10763       (setq gnus-newsgroup-charset nil)
10764     (let* ((ignored-charsets
10765             (or gnus-newsgroup-ephemeral-ignored-charsets
10766                 (append
10767                  (and gnus-newsgroup-name
10768                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10769                  gnus-newsgroup-ignored-charsets))))
10770       (setq gnus-newsgroup-charset
10771             (or gnus-newsgroup-ephemeral-charset
10772                 (and gnus-newsgroup-name
10773                      (gnus-parameter-charset gnus-newsgroup-name))
10774                 gnus-default-charset))
10775       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10776            ignored-charsets))))
10777
10778 ;;;
10779 ;;; Mime Commands
10780 ;;;
10781
10782 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10783   "Display the current article buffer fully MIME-buttonized.
10784 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10785 treated as multipart/mixed."
10786   (interactive "P")
10787   (require 'gnus-art)
10788   (let ((gnus-unbuttonized-mime-types nil)
10789         (gnus-mime-display-multipart-as-mixed show-all-parts))
10790     (gnus-summary-show-article)))
10791
10792 (defun gnus-summary-repair-multipart (article)
10793   "Add a Content-Type header to a multipart article without one."
10794   (interactive (list (gnus-summary-article-number)))
10795   (gnus-with-article article
10796     (message-narrow-to-head)
10797     (message-remove-header "Mime-Version")
10798     (goto-char (point-max))
10799     (insert "Mime-Version: 1.0\n")
10800     (widen)
10801     (when (search-forward "\n--" nil t)
10802       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10803         (message-narrow-to-head)
10804         (message-remove-header "Content-Type")
10805         (goto-char (point-max))
10806         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10807                         separator))
10808         (widen))))
10809   (let (gnus-mark-article-hook)
10810     (gnus-summary-select-article t t nil article)))
10811
10812 (defun gnus-summary-toggle-display-buttonized ()
10813   "Toggle the buttonizing of the article buffer."
10814   (interactive)
10815   (require 'gnus-art)
10816   (if (setq gnus-inhibit-mime-unbuttonizing
10817             (not gnus-inhibit-mime-unbuttonizing))
10818       (let ((gnus-unbuttonized-mime-types nil))
10819         (gnus-summary-show-article))
10820     (gnus-summary-show-article)))
10821
10822 ;;;
10823 ;;; Generic summary marking commands
10824 ;;;
10825
10826 (defvar gnus-summary-marking-alist
10827   '((read gnus-del-mark "d")
10828     (unread gnus-unread-mark "u")
10829     (ticked gnus-ticked-mark "!")
10830     (dormant gnus-dormant-mark "?")
10831     (expirable gnus-expirable-mark "e"))
10832   "An alist of names/marks/keystrokes.")
10833
10834 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10835 (defvar gnus-summary-mark-map)
10836
10837 (defun gnus-summary-make-all-marking-commands ()
10838   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10839   (dolist (elem gnus-summary-marking-alist)
10840     (apply 'gnus-summary-make-marking-command elem)))
10841
10842 (defun gnus-summary-make-marking-command (name mark keystroke)
10843   (let ((map (make-sparse-keymap)))
10844     (define-key gnus-summary-generic-mark-map keystroke map)
10845     (dolist (lway `((next "next" next nil "n")
10846                     (next-unread "next unread" next t "N")
10847                     (prev "previous" prev nil "p")
10848                     (prev-unread "previous unread" prev t "P")
10849                     (nomove "" nil nil ,keystroke)))
10850       (let ((func (gnus-summary-make-marking-command-1
10851                    mark (car lway) lway name)))
10852         (setq func (eval func))
10853         (define-key map (nth 4 lway) func)))))
10854
10855 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10856   `(defun ,(intern
10857             (format "gnus-summary-put-mark-as-%s%s"
10858                     name (if (eq way 'nomove)
10859                              ""
10860                            (concat "-" (symbol-name way)))))
10861      (n)
10862      ,(format
10863        "Mark the current article as %s%s.
10864 If N, the prefix, then repeat N times.
10865 If N is negative, move in reverse order.
10866 The difference between N and the actual number of articles marked is
10867 returned."
10868        name (cadr lway))
10869      (interactive "p")
10870      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10871
10872 (defun gnus-summary-generic-mark (n mark move unread)
10873   "Mark N articles with MARK."
10874   (unless (eq major-mode 'gnus-summary-mode)
10875     (error "This command can only be used in the summary buffer"))
10876   (gnus-summary-show-thread)
10877   (let ((nummove
10878          (cond
10879           ((eq move 'next) 1)
10880           ((eq move 'prev) -1)
10881           (t 0))))
10882     (if (zerop nummove)
10883         (setq n 1)
10884       (when (< n 0)
10885         (setq n (abs n)
10886               nummove (* -1 nummove))))
10887     (while (and (> n 0)
10888                 (gnus-summary-mark-article nil mark)
10889                 (zerop (gnus-summary-next-subject nummove unread t)))
10890       (setq n (1- n)))
10891     (when (/= 0 n)
10892       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10893     (gnus-summary-recenter)
10894     (gnus-summary-position-point)
10895     (gnus-set-mode-line 'summary)
10896     n))
10897
10898 (defun gnus-summary-insert-articles (articles)
10899   (when (setq articles
10900               (gnus-sorted-difference articles
10901                                       (mapcar (lambda (h)
10902                                                 (mail-header-number h))
10903                                               gnus-newsgroup-headers)))
10904     (setq gnus-newsgroup-headers
10905           (merge 'list
10906                  gnus-newsgroup-headers
10907                  (gnus-fetch-headers articles)
10908                  'gnus-article-sort-by-number))
10909     ;; Suppress duplicates?
10910     (when gnus-suppress-duplicates
10911       (gnus-dup-suppress-articles))
10912
10913     ;; We might want to build some more threads first.
10914     (when (and gnus-fetch-old-headers
10915                (eq gnus-headers-retrieved-by 'nov))
10916       (if (eq gnus-fetch-old-headers 'invisible)
10917           (gnus-build-all-threads)
10918         (gnus-build-old-threads)))
10919     ;; Let the Gnus agent mark articles as read.
10920     (when gnus-agent
10921       (gnus-agent-get-undownloaded-list))
10922     ;; Remove list identifiers from subject
10923     (when gnus-list-identifiers
10924       (gnus-summary-remove-list-identifiers))
10925     ;; First and last article in this newsgroup.
10926     (when gnus-newsgroup-headers
10927       (setq gnus-newsgroup-begin
10928             (mail-header-number (car gnus-newsgroup-headers))
10929             gnus-newsgroup-end
10930             (mail-header-number
10931              (gnus-last-element gnus-newsgroup-headers))))
10932     (when gnus-use-scoring
10933       (gnus-possibly-score-headers))))
10934
10935 (defun gnus-summary-insert-old-articles (&optional all)
10936   "Insert all old articles in this group.
10937 If ALL is non-nil, already read articles become readable.
10938 If ALL is a number, fetch this number of articles."
10939   (interactive "P")
10940   (prog1
10941       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
10942             older len)
10943         (setq older
10944               (gnus-sorted-difference
10945                (gnus-uncompress-range (list gnus-newsgroup-active))
10946                old))
10947         (setq len (length older))
10948         (cond
10949          ((null older) nil)
10950          ((numberp all)
10951           (if (< all len)
10952               (setq older (last older all))))
10953          (all nil)
10954          (t
10955           (if (and (numberp gnus-large-newsgroup)
10956                    (> len gnus-large-newsgroup))
10957               (let* ((cursor-in-echo-area nil)
10958                      (initial (gnus-parameter-large-newsgroup-initial 
10959                                gnus-newsgroup-name))
10960                      (input
10961                       (read-string
10962                        (format
10963                         "How many articles from %s (%s %d): "
10964                         (gnus-limit-string
10965                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
10966                         (if initial "max" "default")
10967                         len)
10968                        (if initial
10969                            (cons (number-to-string initial)
10970                                  0)))))
10971                 (unless (string-match "^[ \t]*$" input)
10972                   (setq all (string-to-number input))
10973                   (if (< all len)
10974                       (setq older (last older all))))))))
10975         (if (not older)
10976             (message "No old news.")
10977           (gnus-summary-insert-articles older)
10978           (gnus-summary-limit (gnus-sorted-nunion old older))))
10979     (gnus-summary-position-point)))
10980
10981 (defun gnus-summary-insert-new-articles ()
10982   "Insert all new articles in this group."
10983   (interactive)
10984   (prog1
10985       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
10986             (old-active gnus-newsgroup-active)
10987             (nnmail-fetched-sources (list t))
10988             i new)
10989         (setq gnus-newsgroup-active
10990               (gnus-activate-group gnus-newsgroup-name 'scan))
10991         (setq i (1+ (cdr old-active)))
10992         (while (<= i (cdr gnus-newsgroup-active))
10993           (push i new)
10994           (incf i))
10995         (if (not new)
10996             (message "No gnus is bad news.")
10997           (setq new (nreverse new))
10998           (gnus-summary-insert-articles new)
10999           (setq gnus-newsgroup-unreads
11000                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11001           (gnus-summary-limit (gnus-sorted-nunion old new))))
11002     (gnus-summary-position-point)))
11003
11004 (gnus-summary-make-all-marking-commands)
11005
11006 (gnus-ems-redefine)
11007
11008 (provide 'gnus-sum)
11009
11010 (run-hooks 'gnus-sum-load-hook)
11011
11012 ;;; gnus-sum.el ends here