* gnus-srvr.el (gnus-browse-make-menu-bar): Add "d".
[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:
140 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
141 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
142   :group 'gnus-thread
143   :type '(repeat function))
144
145 (defcustom gnus-simplify-ignored-prefixes nil
146   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  regexp))
150
151 (defcustom gnus-build-sparse-threads nil
152   "*If non-nil, fill in the gaps in threads.
153 If `some', only fill in the gaps that are needed to tie loose threads
154 together.  If `more', fill in all leaf nodes that Gnus can find.  If
155 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
156   :group 'gnus-thread
157   :type '(choice (const :tag "off" nil)
158                  (const some)
159                  (const more)
160                  (sexp :menu-tag "all" t)))
161
162 (defcustom gnus-summary-thread-gathering-function
163   'gnus-gather-threads-by-subject
164   "*Function used for gathering loose threads.
165 There are two pre-defined functions: `gnus-gather-threads-by-subject',
166 which only takes Subjects into consideration; and
167 `gnus-gather-threads-by-references', which compared the References
168 headers of the articles to find matches."
169   :group 'gnus-thread
170   :type '(radio (function-item gnus-gather-threads-by-subject)
171                 (function-item gnus-gather-threads-by-references)
172                 (function :tag "other")))
173
174 (defcustom gnus-summary-same-subject ""
175   "*String indicating that the current article has the same subject as the previous.
176 This variable will only be used if the value of
177 `gnus-summary-make-false-root' is `empty'."
178   :group 'gnus-summary-format
179   :type 'string)
180
181 (defcustom gnus-summary-goto-unread t
182   "*If t, many commands will go to the next unread article.
183 This applies to marking commands as well as other commands that
184 \"naturally\" select the next article, like, for instance, `SPC' at
185 the end of an article.
186
187 If nil, the marking commands do NOT go to the next unread article
188 (they go to the next article instead).  If `never', commands that
189 usually go to the next unread article, will go to the next article,
190 whether it is read or not."
191   :group 'gnus-summary-marks
192   :link '(custom-manual "(gnus)Setting Marks")
193   :type '(choice (const :tag "off" nil)
194                  (const never)
195                  (sexp :menu-tag "on" t)))
196
197 (defcustom gnus-summary-default-score 0
198   "*Default article score level.
199 All scores generated by the score files will be added to this score.
200 If this variable is nil, scoring will be disabled."
201   :group 'gnus-score-default
202   :type '(choice (const :tag "disable")
203                  integer))
204
205 (defcustom gnus-summary-default-high-score 0
206   "*Default threshold for a high scored article.
207 An article will be highlighted as high scored if its score is greater
208 than this score."
209   :group 'gnus-score-default
210   :type 'integer)
211
212 (defcustom gnus-summary-default-low-score 0
213   "*Default threshold for a low scored article.
214 An article will be highlighted as low scored if its score is smaller
215 than this score."
216   :group 'gnus-score-default
217   :type 'integer)
218
219 (defcustom gnus-summary-zcore-fuzz 0
220   "*Fuzziness factor for the zcore in the summary buffer.
221 Articles with scores closer than this to `gnus-summary-default-score'
222 will not be marked."
223   :group 'gnus-summary-format
224   :type 'integer)
225
226 (defcustom gnus-simplify-subject-fuzzy-regexp nil
227   "*Strings to be removed when doing fuzzy matches.
228 This can either be a regular expression or list of regular expressions
229 that will be removed from subject strings if fuzzy subject
230 simplification is selected."
231   :group 'gnus-thread
232   :type '(repeat regexp))
233
234 (defcustom gnus-show-threads t
235   "*If non-nil, display threads in summary mode."
236   :group 'gnus-thread
237   :type 'boolean)
238
239 (defcustom gnus-thread-hide-subtree nil
240   "*If non-nil, hide all threads initially.
241 This can be a predicate specifier which says which threads to hide.
242 If threads are hidden, you have to run the command
243 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
244 to expose hidden threads."
245   :group 'gnus-thread
246   :type 'boolean)
247
248 (defcustom gnus-thread-hide-killed t
249   "*If non-nil, hide killed threads automatically."
250   :group 'gnus-thread
251   :type 'boolean)
252
253 (defcustom gnus-thread-ignore-subject t
254   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
255 If nil, articles that have different subjects from their parents will
256 start separate threads."
257   :group 'gnus-thread
258   :type 'boolean)
259
260 (defcustom gnus-thread-operation-ignore-subject t
261   "*If non-nil, subjects will be ignored when doing thread commands.
262 This affects commands like `gnus-summary-kill-thread' and
263 `gnus-summary-lower-thread'.
264
265 If this variable is nil, articles in the same thread with different
266 subjects will not be included in the operation in question.  If this
267 variable is `fuzzy', only articles that have subjects that are fuzzily
268 equal will be included."
269   :group 'gnus-thread
270   :type '(choice (const :tag "off" nil)
271                  (const fuzzy)
272                  (sexp :tag "on" t)))
273
274 (defcustom gnus-thread-indent-level 4
275   "*Number that says how much each sub-thread should be indented."
276   :group 'gnus-thread
277   :type 'integer)
278
279 (defcustom gnus-auto-extend-newsgroup t
280   "*If non-nil, extend newsgroup forward and backward when requested."
281   :group 'gnus-summary-choose
282   :type 'boolean)
283
284 (defcustom gnus-auto-select-first t
285   "*If non-nil, select the article under point.
286 Which article this is is controlled by the `gnus-auto-select-subject'
287 variable.
288
289 If you want to prevent automatic selection of articles in some
290 newsgroups, set the variable to nil in `gnus-select-group-hook'."
291   :group 'gnus-group-select
292   :type '(choice (const :tag "none" nil)
293                  (sexp :menu-tag "first" t)))
294
295 (defcustom gnus-auto-select-subject 'unread
296   "*Says what subject to place under point when entering a group.
297
298 This variable can either be the symbols `first' (place point on the
299 first subject), `unread' (place point on the subject line of the first
300 unread article), `best' (place point on the subject line of the
301 higest-scored article), `unseen' (place point on the subject line of
302 the first unseen article), 'unseen-or-unread' (place point on the subject
303 line of the first unseen article or, if all article have been seen, on the
304 subject line of the first unread article), or a function to be called to
305 place point on some subject line."
306   :group 'gnus-group-select
307   :type '(choice (const best)
308                  (const unread)
309                  (const first)
310                  (const unseen)
311                  (const unseen-or-unread)))
312
313 (defcustom gnus-auto-select-next t
314   "*If non-nil, offer to go to the next group from the end of the previous.
315 If the value is t and the next newsgroup is empty, Gnus will exit
316 summary mode and go back to group mode.  If the value is neither nil
317 nor t, Gnus will select the following unread newsgroup.  In
318 particular, if the value is the symbol `quietly', the next unread
319 newsgroup will be selected without any confirmation, and if it is
320 `almost-quietly', the next group will be selected without any
321 confirmation if you are located on the last article in the group.
322 Finally, if this variable is `slightly-quietly', the `Z n' command
323 will go to the next group without confirmation."
324   :group 'gnus-summary-maneuvering
325   :type '(choice (const :tag "off" nil)
326                  (const quietly)
327                  (const almost-quietly)
328                  (const slightly-quietly)
329                  (sexp :menu-tag "on" t)))
330
331 (defcustom gnus-auto-select-same nil
332   "*If non-nil, select the next article with the same subject.
333 If there are no more articles with the same subject, go to
334 the first unread article."
335   :group 'gnus-summary-maneuvering
336   :type 'boolean)
337
338 (defcustom gnus-summary-check-current nil
339   "*If non-nil, consider the current article when moving.
340 The \"unread\" movement commands will stay on the same line if the
341 current article is unread."
342   :group 'gnus-summary-maneuvering
343   :type 'boolean)
344
345 (defcustom gnus-auto-center-summary t
346   "*If non-nil, always center the current summary buffer.
347 In particular, if `vertical' do only vertical recentering.  If non-nil
348 and non-`vertical', do both horizontal and vertical recentering."
349   :group 'gnus-summary-maneuvering
350   :type '(choice (const :tag "none" nil)
351                  (const vertical)
352                  (integer :tag "height")
353                  (sexp :menu-tag "both" t)))
354
355 (defcustom gnus-show-all-headers nil
356   "*If non-nil, don't hide any headers."
357   :group 'gnus-article-hiding
358   :group 'gnus-article-headers
359   :type 'boolean)
360
361 (defcustom gnus-summary-ignore-duplicates nil
362   "*If non-nil, ignore articles with identical Message-ID headers."
363   :group 'gnus-summary
364   :type 'boolean)
365
366 (defcustom gnus-single-article-buffer t
367   "*If non-nil, display all articles in the same buffer.
368 If nil, each group will get its own article buffer."
369   :group 'gnus-article-various
370   :type 'boolean)
371
372 (defcustom gnus-break-pages t
373   "*If non-nil, do page breaking on articles.
374 The page delimiter is specified by the `gnus-page-delimiter'
375 variable."
376   :group 'gnus-article-various
377   :type 'boolean)
378
379 (defcustom gnus-move-split-methods nil
380   "*Variable used to suggest where articles are to be moved to.
381 It uses the same syntax as the `gnus-split-methods' variable.
382 However, whereas `gnus-split-methods' specifies file names as targets,
383 this variable specifies group names."
384   :group 'gnus-summary-mail
385   :type '(repeat (choice (list :value (fun) function)
386                          (cons :value ("" "") regexp (repeat string))
387                          (sexp :value nil))))
388
389 (defcustom gnus-unread-mark ?           ;Whitespace
390   "*Mark used for unread articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-ticked-mark ?!
395   "*Mark used for ticked articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-dormant-mark ??
400   "*Mark used for dormant articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-del-mark ?r
405   "*Mark used for del'd articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-read-mark ?R
410   "*Mark used for read articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-expirable-mark ?E
415   "*Mark used for expirable articles."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-killed-mark ?K
420   "*Mark used for killed articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-spam-mark ?H
425   "*Mark used for spam articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-souped-mark ?F
430   "*Mark used for souped articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-kill-file-mark ?X
435   "*Mark used for articles killed by kill files."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-low-score-mark ?Y
440   "*Mark used for articles with a low score."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-catchup-mark ?C
445   "*Mark used for articles that are caught up."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-replied-mark ?A
450   "*Mark used for articles that have been replied to."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-forwarded-mark ?F
455   "*Mark used for articles that have been forwarded."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-recent-mark ?N
460   "*Mark used for articles that are recent."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-cached-mark ?*
465   "*Mark used for articles that are in the cache."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-saved-mark ?S
470   "*Mark used for articles that have been saved."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-unseen-mark ?.
475   "*Mark used for articles that haven't been seen."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-no-mark ?               ;Whitespace
480   "*Mark used for articles that have no other secondary mark."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-ancient-mark ?O
485   "*Mark used for ancient articles."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-sparse-mark ?Q
490   "*Mark used for sparsely reffed articles."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-canceled-mark ?G
495   "*Mark used for canceled articles."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-duplicate-mark ?M
500   "*Mark used for duplicate articles."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-undownloaded-mark ?@
505   "*Mark used for articles that weren't downloaded."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-downloadable-mark ?%
510   "*Mark used for articles that are to be downloaded."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-unsendable-mark ?=
515   "*Mark used for articles that won't be sent."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-score-over-mark ?+
520   "*Score mark used for articles with high scores."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-score-below-mark ?-
525   "*Score mark used for articles with low scores."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-empty-thread-mark ?     ;Whitespace
530   "*There is no thread under the article."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-not-empty-thread-mark ?=
535   "*There is a thread under the article."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-view-pseudo-asynchronously nil
540   "*If non-nil, Gnus will view pseudo-articles asynchronously."
541   :group 'gnus-extract-view
542   :type 'boolean)
543
544 (defcustom gnus-auto-expirable-marks
545   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
546         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
547         gnus-souped-mark gnus-duplicate-mark)
548   "*The list of marks converted into expiration if a group is auto-expirable."
549   :version "21.1"
550   :group 'gnus-summary
551   :type '(repeat character))
552
553 (defcustom gnus-inhibit-user-auto-expire t
554   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
555   :version "21.1"
556   :group 'gnus-summary
557   :type 'boolean)
558
559 (defcustom gnus-view-pseudos nil
560   "*If `automatic', pseudo-articles will be viewed automatically.
561 If `not-confirm', pseudos will be viewed automatically, and the user
562 will not be asked to confirm the command."
563   :group 'gnus-extract-view
564   :type '(choice (const :tag "off" nil)
565                  (const automatic)
566                  (const not-confirm)))
567
568 (defcustom gnus-view-pseudos-separately t
569   "*If non-nil, one pseudo-article will be created for each file to be viewed.
570 If nil, all files that use the same viewing command will be given as a
571 list of parameters to that command."
572   :group 'gnus-extract-view
573   :type 'boolean)
574
575 (defcustom gnus-insert-pseudo-articles t
576   "*If non-nil, insert pseudo-articles when decoding articles."
577   :group 'gnus-extract-view
578   :type 'boolean)
579
580 (defcustom gnus-summary-dummy-line-format
581   "  %(:                          :%) %S\n"
582   "*The format specification for the dummy roots in the summary buffer.
583 It works along the same lines as a normal formatting string,
584 with some simple extensions.
585
586 %S  The subject
587
588 General format specifiers can also be used.
589 See `(gnus)Formatting Variables'."
590   :link '(custom-manual "(gnus)Formatting Variables")
591   :group 'gnus-threading
592   :type 'string)
593
594 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
595   "*The format specification for the summary mode line.
596 It works along the same lines as a normal formatting string,
597 with some simple extensions:
598
599 %G  Group name
600 %p  Unprefixed group name
601 %A  Current article number
602 %z  Current article score
603 %V  Gnus version
604 %U  Number of unread articles in the group
605 %e  Number of unselected articles in the group
606 %Z  A string with unread/unselected article counts
607 %g  Shortish group name
608 %S  Subject of the current article
609 %u  User-defined spec
610 %s  Current score file name
611 %d  Number of dormant articles
612 %r  Number of articles that have been marked as read in this session
613 %E  Number of articles expunged by the score files"
614   :group 'gnus-summary-format
615   :type 'string)
616
617 (defcustom gnus-list-identifiers nil
618   "Regexp that matches list identifiers to be removed from subject.
619 This can also be a list of regexps."
620   :version "21.1"
621   :group 'gnus-summary-format
622   :group 'gnus-article-hiding
623   :type '(choice (const :tag "none" nil)
624                  (regexp :value ".*")
625                  (repeat :value (".*") regexp)))
626
627 (defcustom gnus-summary-mark-below 0
628   "*Mark all articles with a score below this variable as read.
629 This variable is local to each summary buffer and usually set by the
630 score file."
631   :group 'gnus-score-default
632   :type 'integer)
633
634 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
635   "*List of functions used for sorting articles in the summary buffer.
636
637 Each function takes two articles and returns non-nil if the first
638 article should be sorted before the other.  If you use more than one
639 function, the primary sort function should be the last.  You should
640 probably always include `gnus-article-sort-by-number' in the list of
641 sorting functions -- preferably first.  Also note that sorting by date
642 is often much slower than sorting by number, and the sorting order is
643 very similar.  (Sorting by date means sorting by the time the message
644 was sent, sorting by number means sorting by arrival time.)
645
646 Ready-made functions include `gnus-article-sort-by-number',
647 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
648 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
649 and `gnus-article-sort-by-score'.
650
651 When threading is turned on, the variable `gnus-thread-sort-functions'
652 controls how articles are sorted."
653   :group 'gnus-summary-sort
654   :type '(repeat (choice (function-item gnus-article-sort-by-number)
655                          (function-item gnus-article-sort-by-author)
656                          (function-item gnus-article-sort-by-subject)
657                          (function-item gnus-article-sort-by-date)
658                          (function-item gnus-article-sort-by-score)
659                          (function-item gnus-article-sort-by-random)
660                          (function :tag "other"))))
661
662 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
663   "*List of functions used for sorting threads in the summary buffer.
664 By default, threads are sorted by article number.
665
666 Each function takes two threads and returns non-nil if the first
667 thread should be sorted before the other.  If you use more than one
668 function, the primary sort function should be the last.  You should
669 probably always include `gnus-thread-sort-by-number' in the list of
670 sorting functions -- preferably first.  Also note that sorting by date
671 is often much slower than sorting by number, and the sorting order is
672 very similar.  (Sorting by date means sorting by the time the message
673 was sent, sorting by number means sorting by arrival time.)
674
675 Ready-made functions include `gnus-thread-sort-by-number',
676 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
677 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
678 `gnus-thread-sort-by-most-recent-number',
679 `gnus-thread-sort-by-most-recent-date',
680 `gnus-thread-sort-by-random', and
681 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
682
683 When threading is turned off, the variable
684 `gnus-article-sort-functions' controls how articles are sorted."
685   :group 'gnus-summary-sort
686   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
687                          (function-item gnus-thread-sort-by-author)
688                          (function-item gnus-thread-sort-by-subject)
689                          (function-item gnus-thread-sort-by-date)
690                          (function-item gnus-thread-sort-by-score)
691                          (function-item gnus-thread-sort-by-total-score)
692                          (function-item gnus-thread-sort-by-random)
693                          (function :tag "other"))))
694
695 (defcustom gnus-thread-score-function '+
696   "*Function used for calculating the total score of a thread.
697
698 The function is called with the scores of the article and each
699 subthread and should then return the score of the thread.
700
701 Some functions you can use are `+', `max', or `min'."
702   :group 'gnus-summary-sort
703   :type 'function)
704
705 (defcustom gnus-summary-expunge-below nil
706   "All articles that have a score less than this variable will be expunged.
707 This variable is local to the summary buffers."
708   :group 'gnus-score-default
709   :type '(choice (const :tag "off" nil)
710                  integer))
711
712 (defcustom gnus-thread-expunge-below nil
713   "All threads that have a total score less than this variable will be expunged.
714 See `gnus-thread-score-function' for en explanation of what a
715 \"thread score\" is.
716
717 This variable is local to the summary buffers."
718   :group 'gnus-threading
719   :group 'gnus-score-default
720   :type '(choice (const :tag "off" nil)
721                  integer))
722
723 (defcustom gnus-summary-mode-hook nil
724   "*A hook for Gnus summary mode.
725 This hook is run before any variables are set in the summary buffer."
726   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
727   :group 'gnus-summary-various
728   :type 'hook)
729
730 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
731 (when (featurep 'xemacs)
732   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
733   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
734   (add-hook 'gnus-summary-mode-hook
735             'gnus-xmas-switch-horizontal-scrollbar-off))
736
737 (defcustom gnus-summary-menu-hook nil
738   "*Hook run after the creation of the summary mode menu."
739   :group 'gnus-summary-visual
740   :type 'hook)
741
742 (defcustom gnus-summary-exit-hook nil
743   "*A hook called on exit from the summary buffer.
744 It will be called with point in the group buffer."
745   :group 'gnus-summary-exit
746   :type 'hook)
747
748 (defcustom gnus-summary-prepare-hook nil
749   "*A hook called after the summary buffer has been generated.
750 If you want to modify the summary buffer, you can use this hook."
751   :group 'gnus-summary-various
752   :type 'hook)
753
754 (defcustom gnus-summary-prepared-hook nil
755   "*A hook called as the last thing after the summary buffer has been generated."
756   :group 'gnus-summary-various
757   :type 'hook)
758
759 (defcustom gnus-summary-generate-hook nil
760   "*A hook run just before generating the summary buffer.
761 This hook is commonly used to customize threading variables and the
762 like."
763   :group 'gnus-summary-various
764   :type 'hook)
765
766 (defcustom gnus-select-group-hook nil
767   "*A hook called when a newsgroup is selected.
768
769 If you'd like to simplify subjects like the
770 `gnus-summary-next-same-subject' command does, you can use the
771 following hook:
772
773  (add-hook gnus-select-group-hook
774            (lambda ()
775              (mapcar (lambda (header)
776                        (mail-header-set-subject
777                         header
778                         (gnus-simplify-subject
779                          (mail-header-subject header) 're-only)))
780                      gnus-newsgroup-headers)))"
781   :group 'gnus-group-select
782   :type 'hook)
783
784 (defcustom gnus-select-article-hook nil
785   "*A hook called when an article is selected."
786   :group 'gnus-summary-choose
787   :type 'hook)
788
789 (defcustom gnus-visual-mark-article-hook
790   (list 'gnus-highlight-selected-summary)
791   "*Hook run after selecting an article in the summary buffer.
792 It is meant to be used for highlighting the article in some way.  It
793 is not run if `gnus-visual' is nil."
794   :group 'gnus-summary-visual
795   :type 'hook)
796
797 (defcustom gnus-parse-headers-hook nil
798   "*A hook called before parsing the headers."
799   :group 'gnus-various
800   :type 'hook)
801
802 (defcustom gnus-exit-group-hook nil
803   "*A hook called when exiting summary mode.
804 This hook is not called from the non-updating exit commands like `Q'."
805   :group 'gnus-various
806   :type 'hook)
807
808 (defcustom gnus-summary-update-hook
809   (list 'gnus-summary-highlight-line)
810   "*A hook called when a summary line is changed.
811 The hook will not be called if `gnus-visual' is nil.
812
813 The default function `gnus-summary-highlight-line' will
814 highlight the line according to the `gnus-summary-highlight'
815 variable."
816   :group 'gnus-summary-visual
817   :type 'hook)
818
819 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
820   "*A hook called when an article is selected for the first time.
821 The hook is intended to mark an article as read (or unread)
822 automatically when it is selected."
823   :group 'gnus-summary-choose
824   :type 'hook)
825
826 (defcustom gnus-group-no-more-groups-hook nil
827   "*A hook run when returning to group mode having no more (unread) groups."
828   :group 'gnus-group-select
829   :type 'hook)
830
831 (defcustom gnus-ps-print-hook nil
832   "*A hook run before ps-printing something from Gnus."
833   :group 'gnus-summary
834   :type 'hook)
835
836 (defcustom gnus-summary-display-arrow
837   (and (fboundp 'display-graphic-p)
838        (display-graphic-p))
839   "*If non-nil, display an arrow highlighting the current article."
840   :version "21.1"
841   :group 'gnus-summary
842   :type 'boolean)
843
844 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
845   "Face used for highlighting the current article in the summary buffer."
846   :group 'gnus-summary-visual
847   :type 'face)
848
849 (defcustom gnus-summary-highlight
850   '(((eq mark gnus-canceled-mark)
851      . gnus-summary-cancelled-face)
852     ((and (> score default-high)
853           (or (eq mark gnus-dormant-mark)
854               (eq mark gnus-ticked-mark)))
855      . gnus-summary-high-ticked-face)
856     ((and (< score default-low)
857           (or (eq mark gnus-dormant-mark)
858               (eq mark gnus-ticked-mark)))
859      . gnus-summary-low-ticked-face)
860     ((or (eq mark gnus-dormant-mark)
861          (eq mark gnus-ticked-mark))
862      . gnus-summary-normal-ticked-face)
863     ((and (> score default-high) (eq mark gnus-ancient-mark))
864      . gnus-summary-high-ancient-face)
865     ((and (< score default-low) (eq mark gnus-ancient-mark))
866      . gnus-summary-low-ancient-face)
867     ((eq mark gnus-ancient-mark)
868      . gnus-summary-normal-ancient-face)
869     ((and (> score default-high) (eq mark gnus-unread-mark))
870      . gnus-summary-high-unread-face)
871     ((and (< score default-low) (eq mark gnus-unread-mark))
872      . gnus-summary-low-unread-face)
873     ((eq mark gnus-unread-mark)
874      . gnus-summary-normal-unread-face)
875     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
876                                                   gnus-undownloaded-mark)))
877      . gnus-summary-high-unread-face)
878     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
879                                                  gnus-undownloaded-mark)))
880      . gnus-summary-low-unread-face)
881     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
882           (memq article gnus-newsgroup-unreads))
883      . gnus-summary-normal-unread-face)
884     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
885      . gnus-summary-normal-read-face)
886     ((> score default-high)
887      . gnus-summary-high-read-face)
888     ((< score default-low)
889      . gnus-summary-low-read-face)
890     (t
891      . gnus-summary-normal-read-face))
892   "*Controls the highlighting of summary buffer lines.
893
894 A list of (FORM . FACE) pairs.  When deciding how a a particular
895 summary line should be displayed, each form is evaluated.  The content
896 of the face field after the first true form is used.  You can change
897 how those summary lines are displayed, by editing the face field.
898
899 You can use the following variables in the FORM field.
900
901 score:        The article's score
902 default:      The default article score.
903 default-high: The default score for high scored articles.
904 default-low:  The default score for low scored articles.
905 below:        The score below which articles are automatically marked as read.
906 mark:         The articles mark."
907   :group 'gnus-summary-visual
908   :type '(repeat (cons (sexp :tag "Form" nil)
909                        face)))
910
911 (defcustom gnus-alter-header-function nil
912   "Function called to allow alteration of article header structures.
913 The function is called with one parameter, the article header vector,
914 which it may alter in any way.")
915
916 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
917   "Variable that says which function should be used to decode a string with encoded words.")
918
919 (defcustom gnus-extra-headers '(To Newsgroups)
920   "*Extra headers to parse."
921   :version "21.1"
922   :group 'gnus-summary
923   :type '(repeat symbol))
924
925 (defcustom gnus-ignored-from-addresses
926   (and user-mail-address (regexp-quote user-mail-address))
927   "*Regexp of From headers that may be suppressed in favor of To headers."
928   :version "21.1"
929   :group 'gnus-summary
930   :type 'regexp)
931
932 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
933   "List of charsets that should be ignored.
934 When these charsets are used in the \"charset\" parameter, the
935 default charset will be used instead."
936   :version "21.1"
937   :type '(repeat symbol)
938   :group 'gnus-charset)
939
940 (gnus-define-group-parameter
941  ignored-charsets
942  :type list
943  :function-document
944  "Return the ignored charsets of GROUP."
945  :variable gnus-group-ignored-charsets-alist
946  :variable-default
947  '(("alt\\.chinese\\.text" iso-8859-1))
948  :variable-document
949  "Alist of regexps (to match group names) and charsets that should be ignored.
950 When these charsets are used in the \"charset\" parameter, the
951 default charset will be used instead."
952  :variable-group gnus-charset
953  :variable-type '(repeat (cons (regexp :tag "Group")
954                                (repeat symbol)))
955  :parameter-type '(choice :tag "Ignored charsets"
956                           :value nil
957                           (repeat (symbol)))
958  :parameter-document       "\
959 List of charsets that should be ignored.
960
961 When these charsets are used in the \"charset\" parameter, the
962 default charset will be used instead.")
963
964 (defcustom gnus-group-highlight-words-alist nil
965   "Alist of group regexps and highlight regexps.
966 This variable uses the same syntax as `gnus-emphasis-alist'."
967   :version "21.1"
968   :type '(repeat (cons (regexp :tag "Group")
969                        (repeat (list (regexp :tag "Highlight regexp")
970                                      (number :tag "Group for entire word" 0)
971                                      (number :tag "Group for displayed part" 0)
972                                      (symbol :tag "Face"
973                                              gnus-emphasis-highlight-words)))))
974   :group 'gnus-summary-visual)
975
976 (defcustom gnus-summary-show-article-charset-alist
977   nil
978   "Alist of number and charset.
979 The article will be shown with the charset corresponding to the
980 numbered argument.
981 For example: ((1 . cn-gb-2312) (2 . big5))."
982   :version "21.1"
983   :type '(repeat (cons (number :tag "Argument" 1)
984                        (symbol :tag "Charset")))
985   :group 'gnus-charset)
986
987 (defcustom gnus-preserve-marks t
988   "Whether marks are preserved when moving, copying and respooling messages."
989   :version "21.1"
990   :type 'boolean
991   :group 'gnus-summary-marks)
992
993 (defcustom gnus-alter-articles-to-read-function nil
994   "Function to be called to alter the list of articles to be selected."
995   :type '(choice (const nil) function)
996   :group 'gnus-summary)
997
998 (defcustom gnus-orphan-score nil
999   "*All orphans get this score added.  Set in the score file."
1000   :group 'gnus-score-default
1001   :type '(choice (const nil)
1002                  integer))
1003
1004 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1005   "*A regexp to match MIME parts when saving multiple parts of a message
1006 with gnus-summary-save-parts (X m). This regexp will be used by default
1007 when prompting the user for which type of files to save."
1008   :group 'gnus-summary
1009   :type 'regexp)
1010
1011 (defcustom gnus-read-all-available-headers nil
1012   "Whether Gnus should parse all headers made available to it.
1013 This is mostly relevant for slow backends where the user may
1014 wish to widen the summary buffer to include all headers
1015 that were fetched.  Say, for nnultimate groups."
1016   :group 'gnus-summary
1017   :type '(choice boolean regexp))
1018
1019 (defcustom gnus-summary-muttprint-program "muttprint"
1020   "Command (and optional arguments) used to run Muttprint."
1021   :version "21.3"
1022   :group 'gnus-summary
1023   :type 'string)
1024
1025 (defcustom gnus-article-loose-mime nil
1026   "If non-nil, don't require MIME-Version header.
1027 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1028 supply the MIME-Version header or deliberately strip it From the mail.
1029 Set it to non-nil, Gnus will treat some articles as MIME even if
1030 the MIME-Version header is missed."
1031   :version "21.3"
1032   :type 'boolean
1033   :group 'gnus-article)
1034
1035 ;;; Internal variables
1036
1037 (defvar gnus-summary-display-cache nil)
1038 (defvar gnus-article-mime-handles nil)
1039 (defvar gnus-article-decoded-p nil)
1040 (defvar gnus-article-charset nil)
1041 (defvar gnus-article-ignored-charsets nil)
1042 (defvar gnus-scores-exclude-files nil)
1043 (defvar gnus-page-broken nil)
1044 (defvar gnus-inhibit-mime-unbuttonizing nil)
1045
1046 (defvar gnus-original-article nil)
1047 (defvar gnus-article-internal-prepare-hook nil)
1048 (defvar gnus-newsgroup-process-stack nil)
1049
1050 (defvar gnus-thread-indent-array nil)
1051 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1052 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1053   "Function called to sort the articles within a thread after it has been gathered together.")
1054
1055 (defvar gnus-summary-save-parts-type-history nil)
1056 (defvar gnus-summary-save-parts-last-directory nil)
1057
1058 ;; Avoid highlighting in kill files.
1059 (defvar gnus-summary-inhibit-highlight nil)
1060 (defvar gnus-newsgroup-selected-overlay nil)
1061 (defvar gnus-inhibit-limiting nil)
1062 (defvar gnus-newsgroup-adaptive-score-file nil)
1063 (defvar gnus-current-score-file nil)
1064 (defvar gnus-current-move-group nil)
1065 (defvar gnus-current-copy-group nil)
1066 (defvar gnus-current-crosspost-group nil)
1067 (defvar gnus-newsgroup-display nil)
1068
1069 (defvar gnus-newsgroup-dependencies nil)
1070 (defvar gnus-newsgroup-adaptive nil)
1071 (defvar gnus-summary-display-article-function nil)
1072 (defvar gnus-summary-highlight-line-function nil
1073   "Function called after highlighting a summary line.")
1074
1075 (defvar gnus-summary-line-format-alist
1076   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1077     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1078     (?s gnus-tmp-subject-or-nil ?s)
1079     (?n gnus-tmp-name ?s)
1080     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1081         ?s)
1082     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1083             gnus-tmp-from) ?s)
1084     (?F gnus-tmp-from ?s)
1085     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1086     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1087     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1088     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1089     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1090     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1091     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1092     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1093     (?L gnus-tmp-lines ?s)
1094     (?I gnus-tmp-indentation ?s)
1095     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1096     (?R gnus-tmp-replied ?c)
1097     (?\[ gnus-tmp-opening-bracket ?c)
1098     (?\] gnus-tmp-closing-bracket ?c)
1099     (?\> (make-string gnus-tmp-level ? ) ?s)
1100     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1101     (?i gnus-tmp-score ?d)
1102     (?z gnus-tmp-score-char ?c)
1103     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1104     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1105     (?U gnus-tmp-unread ?c)
1106     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1107         ?s)
1108     (?t (gnus-summary-number-of-articles-in-thread
1109          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1110         ?d)
1111     (?e (gnus-summary-number-of-articles-in-thread
1112          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1113         ?c)
1114     (?u gnus-tmp-user-defined ?s)
1115     (?P (gnus-pick-line-number) ?d)
1116     (?B gnus-tmp-thread-tree-header-string ?s)
1117     (user-date (gnus-user-date
1118                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1119   "An alist of format specifications that can appear in summary lines.
1120 These are paired with what variables they correspond with, along with
1121 the type of the variable (string, integer, character, etc).")
1122
1123 (defvar gnus-summary-dummy-line-format-alist
1124   `((?S gnus-tmp-subject ?s)
1125     (?N gnus-tmp-number ?d)
1126     (?u gnus-tmp-user-defined ?s)))
1127
1128 (defvar gnus-summary-mode-line-format-alist
1129   `((?G gnus-tmp-group-name ?s)
1130     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1131     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1132     (?A gnus-tmp-article-number ?d)
1133     (?Z gnus-tmp-unread-and-unselected ?s)
1134     (?V gnus-version ?s)
1135     (?U gnus-tmp-unread-and-unticked ?d)
1136     (?S gnus-tmp-subject ?s)
1137     (?e gnus-tmp-unselected ?d)
1138     (?u gnus-tmp-user-defined ?s)
1139     (?d (length gnus-newsgroup-dormant) ?d)
1140     (?t (length gnus-newsgroup-marked) ?d)
1141     (?h (length gnus-newsgroup-spam-marked) ?d)
1142     (?r (length gnus-newsgroup-reads) ?d)
1143     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1144     (?E gnus-newsgroup-expunged-tally ?d)
1145     (?s (gnus-current-score-file-nondirectory) ?s)))
1146
1147 (defvar gnus-last-search-regexp nil
1148   "Default regexp for article search command.")
1149
1150 (defvar gnus-last-shell-command nil
1151   "Default shell command on article.")
1152
1153 (defvar gnus-newsgroup-begin nil)
1154 (defvar gnus-newsgroup-end nil)
1155 (defvar gnus-newsgroup-last-rmail nil)
1156 (defvar gnus-newsgroup-last-mail nil)
1157 (defvar gnus-newsgroup-last-folder nil)
1158 (defvar gnus-newsgroup-last-file nil)
1159 (defvar gnus-newsgroup-auto-expire nil)
1160 (defvar gnus-newsgroup-active nil)
1161
1162 (defvar gnus-newsgroup-data nil)
1163 (defvar gnus-newsgroup-data-reverse nil)
1164 (defvar gnus-newsgroup-limit nil)
1165 (defvar gnus-newsgroup-limits nil)
1166
1167 (defvar gnus-newsgroup-unreads nil
1168   "Sorted list of unread articles in the current newsgroup.")
1169
1170 (defvar gnus-newsgroup-unselected nil
1171   "Sorted list of unselected unread articles in the current newsgroup.")
1172
1173 (defvar gnus-newsgroup-reads nil
1174   "Alist of read articles and article marks in the current newsgroup.")
1175
1176 (defvar gnus-newsgroup-expunged-tally nil)
1177
1178 (defvar gnus-newsgroup-marked nil
1179   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1180
1181 (defvar gnus-newsgroup-spam-marked nil
1182   "List of ranges of articles that have been marked as spam.")
1183
1184 (defvar gnus-newsgroup-killed nil
1185   "List of ranges of articles that have been through the scoring process.")
1186
1187 (defvar gnus-newsgroup-cached nil
1188   "Sorted list of articles that come from the article cache.")
1189
1190 (defvar gnus-newsgroup-saved nil
1191   "List of articles that have been saved.")
1192
1193 (defvar gnus-newsgroup-kill-headers nil)
1194
1195 (defvar gnus-newsgroup-replied nil
1196   "List of articles that have been replied to in the current newsgroup.")
1197
1198 (defvar gnus-newsgroup-forwarded nil
1199   "List of articles that have been forwarded in the current newsgroup.")
1200
1201 (defvar gnus-newsgroup-recent nil
1202   "List of articles that have are recent in the current newsgroup.")
1203
1204 (defvar gnus-newsgroup-expirable nil
1205   "Sorted list of articles in the current newsgroup that can be expired.")
1206
1207 (defvar gnus-newsgroup-processable nil
1208   "List of articles in the current newsgroup that can be processed.")
1209
1210 (defvar gnus-newsgroup-downloadable nil
1211   "Sorted list of articles in the current newsgroup that can be processed.")
1212
1213 (defvar gnus-newsgroup-undownloaded nil
1214   "List of articles in the current newsgroup that haven't been downloaded..")
1215
1216 (defvar gnus-newsgroup-unsendable nil
1217   "List of articles in the current newsgroup that won't be sent.")
1218
1219 (defvar gnus-newsgroup-bookmarks nil
1220   "List of articles in the current newsgroup that have bookmarks.")
1221
1222 (defvar gnus-newsgroup-dormant nil
1223   "Sorted list of dormant articles in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-unseen nil
1226   "List of unseen articles in the current newsgroup.")
1227
1228 (defvar gnus-newsgroup-seen nil
1229   "Range of seen articles in the current newsgroup.")
1230
1231 (defvar gnus-newsgroup-articles nil
1232   "List of articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-scored nil
1235   "List of scored articles in the current newsgroup.")
1236
1237 (defvar gnus-newsgroup-headers nil
1238   "List of article headers in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-threads nil)
1241
1242 (defvar gnus-newsgroup-prepared nil
1243   "Whether the current group has been prepared properly.")
1244
1245 (defvar gnus-newsgroup-ancient nil
1246   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1247
1248 (defvar gnus-newsgroup-sparse nil)
1249
1250 (defvar gnus-current-article nil)
1251 (defvar gnus-article-current nil)
1252 (defvar gnus-current-headers nil)
1253 (defvar gnus-have-all-headers nil)
1254 (defvar gnus-last-article nil)
1255 (defvar gnus-newsgroup-history nil)
1256 (defvar gnus-newsgroup-charset nil)
1257 (defvar gnus-newsgroup-ephemeral-charset nil)
1258 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1259
1260 (defvar gnus-article-before-search nil)
1261
1262 (defconst gnus-summary-local-variables
1263   '(gnus-newsgroup-name
1264     gnus-newsgroup-begin gnus-newsgroup-end
1265     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1266     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1267     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1268     gnus-newsgroup-unselected gnus-newsgroup-marked
1269     gnus-newsgroup-spam-marked
1270     gnus-newsgroup-reads gnus-newsgroup-saved
1271     gnus-newsgroup-replied gnus-newsgroup-forwarded
1272     gnus-newsgroup-recent
1273     gnus-newsgroup-expirable
1274     gnus-newsgroup-processable gnus-newsgroup-killed
1275     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1276     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1277     gnus-newsgroup-seen gnus-newsgroup-articles
1278     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1279     gnus-newsgroup-headers gnus-newsgroup-threads
1280     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1281     gnus-current-article gnus-current-headers gnus-have-all-headers
1282     gnus-last-article gnus-article-internal-prepare-hook
1283     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1284     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1285     gnus-thread-expunge-below
1286     gnus-score-alist gnus-current-score-file
1287     (gnus-summary-expunge-below . global)
1288     (gnus-summary-mark-below . global)
1289     (gnus-orphan-score . global)
1290     gnus-newsgroup-active gnus-scores-exclude-files
1291     gnus-newsgroup-history gnus-newsgroup-ancient
1292     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1293     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1294     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1295     (gnus-newsgroup-expunged-tally . 0)
1296     gnus-cache-removable-articles gnus-newsgroup-cached
1297     gnus-newsgroup-data gnus-newsgroup-data-reverse
1298     gnus-newsgroup-limit gnus-newsgroup-limits
1299     gnus-newsgroup-charset gnus-newsgroup-display)
1300   "Variables that are buffer-local to the summary buffers.")
1301
1302 (defvar gnus-newsgroup-variables nil
1303   "A list of variables that have separate values in different newsgroups.
1304 A list of newsgroup (summary buffer) local variables, or cons of
1305 variables and their default values (when the default values are not
1306 nil), that should be made global while the summary buffer is active.
1307 These variables can be used to set variables in the group parameters
1308 while still allowing them to affect operations done in other
1309 buffers. For example:
1310
1311 \(setq gnus-newsgroup-variables
1312      '(message-use-followup-to
1313        (gnus-visible-headers .
1314          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1315 ")
1316
1317 ;; Byte-compiler warning.
1318 (eval-when-compile (defvar gnus-article-mode-map))
1319
1320 ;; MIME stuff.
1321
1322 (defvar gnus-decode-encoded-word-methods
1323   '(mail-decode-encoded-word-string)
1324   "List of methods used to decode encoded words.
1325
1326 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1327 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1328 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1329 whose names match REGEXP.
1330
1331 For example:
1332 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1333  mail-decode-encoded-word-string
1334  (\"chinese\" . rfc1843-decode-string))")
1335
1336 (defvar gnus-decode-encoded-word-methods-cache nil)
1337
1338 (defun gnus-multi-decode-encoded-word-string (string)
1339   "Apply the functions from `gnus-encoded-word-methods' that match."
1340   (unless (and gnus-decode-encoded-word-methods-cache
1341                (eq gnus-newsgroup-name
1342                    (car gnus-decode-encoded-word-methods-cache)))
1343     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1344     (mapcar (lambda (x)
1345               (if (symbolp x)
1346                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1347                 (if (and gnus-newsgroup-name
1348                          (string-match (car x) gnus-newsgroup-name))
1349                     (nconc gnus-decode-encoded-word-methods-cache
1350                            (list (cdr x))))))
1351             gnus-decode-encoded-word-methods))
1352   (let ((xlist gnus-decode-encoded-word-methods-cache))
1353     (pop xlist)
1354     (while xlist
1355       (setq string (funcall (pop xlist) string))))
1356   string)
1357
1358 ;; Subject simplification.
1359
1360 (defun gnus-simplify-whitespace (str)
1361   "Remove excessive whitespace from STR."
1362   (let ((mystr str))
1363     ;; Multiple spaces.
1364     (while (string-match "[ \t][ \t]+" mystr)
1365       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1366                           " "
1367                           (substring mystr (match-end 0)))))
1368     ;; Leading spaces.
1369     (when (string-match "^[ \t]+" mystr)
1370       (setq mystr (substring mystr (match-end 0))))
1371     ;; Trailing spaces.
1372     (when (string-match "[ \t]+$" mystr)
1373       (setq mystr (substring mystr 0 (match-beginning 0))))
1374     mystr))
1375
1376 (defun gnus-simplify-all-whitespace (str)
1377   "Remove all whitespace from STR."
1378   (let ((mystr str))
1379     (while (string-match "[ \t\n]+" mystr)
1380       (setq mystr (replace-match "" nil nil mystr)))
1381     mystr))
1382
1383 (defsubst gnus-simplify-subject-re (subject)
1384   "Remove \"Re:\" from subject lines."
1385   (if (string-match message-subject-re-regexp subject)
1386       (substring subject (match-end 0))
1387     subject))
1388
1389 (defun gnus-simplify-subject (subject &optional re-only)
1390   "Remove `Re:' and words in parentheses.
1391 If RE-ONLY is non-nil, strip leading `Re:'s only."
1392   (let ((case-fold-search t))           ;Ignore case.
1393     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1394     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1395       (setq subject (substring subject (match-end 0))))
1396     ;; Remove uninteresting prefixes.
1397     (when (and (not re-only)
1398                gnus-simplify-ignored-prefixes
1399                (string-match gnus-simplify-ignored-prefixes subject))
1400       (setq subject (substring subject (match-end 0))))
1401     ;; Remove words in parentheses from end.
1402     (unless re-only
1403       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1404         (setq subject (substring subject 0 (match-beginning 0)))))
1405     ;; Return subject string.
1406     subject))
1407
1408 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1409 ;; all whitespace.
1410 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1411   (goto-char (point-min))
1412   (while (re-search-forward regexp nil t)
1413     (replace-match (or newtext ""))))
1414
1415 (defun gnus-simplify-buffer-fuzzy ()
1416   "Simplify string in the buffer fuzzily.
1417 The string in the accessible portion of the current buffer is simplified.
1418 It is assumed to be a single-line subject.
1419 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1420 matter is removed.  Additional things can be deleted by setting
1421 `gnus-simplify-subject-fuzzy-regexp'."
1422   (let ((case-fold-search t)
1423         (modified-tick))
1424     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1425
1426     (while (not (eq modified-tick (buffer-modified-tick)))
1427       (setq modified-tick (buffer-modified-tick))
1428       (cond
1429        ((listp gnus-simplify-subject-fuzzy-regexp)
1430         (mapcar 'gnus-simplify-buffer-fuzzy-step
1431                 gnus-simplify-subject-fuzzy-regexp))
1432        (gnus-simplify-subject-fuzzy-regexp
1433         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1434       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1435       (gnus-simplify-buffer-fuzzy-step
1436        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1437       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1438
1439     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1440     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1441     (gnus-simplify-buffer-fuzzy-step " $")
1442     (gnus-simplify-buffer-fuzzy-step "^ +")))
1443
1444 (defun gnus-simplify-subject-fuzzy (subject)
1445   "Simplify a subject string fuzzily.
1446 See `gnus-simplify-buffer-fuzzy' for details."
1447   (save-excursion
1448     (gnus-set-work-buffer)
1449     (let ((case-fold-search t))
1450       ;; Remove uninteresting prefixes.
1451       (when (and gnus-simplify-ignored-prefixes
1452                  (string-match gnus-simplify-ignored-prefixes subject))
1453         (setq subject (substring subject (match-end 0))))
1454       (insert subject)
1455       (inline (gnus-simplify-buffer-fuzzy))
1456       (buffer-string))))
1457
1458 (defsubst gnus-simplify-subject-fully (subject)
1459   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1460   (cond
1461    (gnus-simplify-subject-functions
1462     (gnus-map-function gnus-simplify-subject-functions subject))
1463    ((null gnus-summary-gather-subject-limit)
1464     (gnus-simplify-subject-re subject))
1465    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1466     (gnus-simplify-subject-fuzzy subject))
1467    ((numberp gnus-summary-gather-subject-limit)
1468     (gnus-limit-string (gnus-simplify-subject-re subject)
1469                        gnus-summary-gather-subject-limit))
1470    (t
1471     subject)))
1472
1473 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1474   "Check whether two subjects are equal.
1475 If optional argument simple-first is t, first argument is already
1476 simplified."
1477   (cond
1478    ((null simple-first)
1479     (equal (gnus-simplify-subject-fully s1)
1480            (gnus-simplify-subject-fully s2)))
1481    (t
1482     (equal s1
1483            (gnus-simplify-subject-fully s2)))))
1484
1485 (defun gnus-summary-bubble-group ()
1486   "Increase the score of the current group.
1487 This is a handy function to add to `gnus-summary-exit-hook' to
1488 increase the score of each group you read."
1489   (gnus-group-add-score gnus-newsgroup-name))
1490
1491 \f
1492 ;;;
1493 ;;; Gnus summary mode
1494 ;;;
1495
1496 (put 'gnus-summary-mode 'mode-class 'special)
1497
1498 (defvar gnus-article-commands-menu)
1499
1500 (when t
1501   ;; Non-orthogonal keys
1502
1503   (gnus-define-keys gnus-summary-mode-map
1504     " " gnus-summary-next-page
1505     "\177" gnus-summary-prev-page
1506     [delete] gnus-summary-prev-page
1507     [backspace] gnus-summary-prev-page
1508     "\r" gnus-summary-scroll-up
1509     "\M-\r" gnus-summary-scroll-down
1510     "n" gnus-summary-next-unread-article
1511     "p" gnus-summary-prev-unread-article
1512     "N" gnus-summary-next-article
1513     "P" gnus-summary-prev-article
1514     "\M-\C-n" gnus-summary-next-same-subject
1515     "\M-\C-p" gnus-summary-prev-same-subject
1516     "\M-n" gnus-summary-next-unread-subject
1517     "\M-p" gnus-summary-prev-unread-subject
1518     "." gnus-summary-first-unread-article
1519     "," gnus-summary-best-unread-article
1520     "\M-s" gnus-summary-search-article-forward
1521     "\M-r" gnus-summary-search-article-backward
1522     "<" gnus-summary-beginning-of-article
1523     ">" gnus-summary-end-of-article
1524     "j" gnus-summary-goto-article
1525     "^" gnus-summary-refer-parent-article
1526     "\M-^" gnus-summary-refer-article
1527     "u" gnus-summary-tick-article-forward
1528     "!" gnus-summary-tick-article-forward
1529     "U" gnus-summary-tick-article-backward
1530     "d" gnus-summary-mark-as-read-forward
1531     "D" gnus-summary-mark-as-read-backward
1532     "E" gnus-summary-mark-as-expirable
1533     "\M-u" gnus-summary-clear-mark-forward
1534     "\M-U" gnus-summary-clear-mark-backward
1535     "k" gnus-summary-kill-same-subject-and-select
1536     "\C-k" gnus-summary-kill-same-subject
1537     "\M-\C-k" gnus-summary-kill-thread
1538     "\M-\C-l" gnus-summary-lower-thread
1539     "e" gnus-summary-edit-article
1540     "#" gnus-summary-mark-as-processable
1541     "\M-#" gnus-summary-unmark-as-processable
1542     "\M-\C-t" gnus-summary-toggle-threads
1543     "\M-\C-s" gnus-summary-show-thread
1544     "\M-\C-h" gnus-summary-hide-thread
1545     "\M-\C-f" gnus-summary-next-thread
1546     "\M-\C-b" gnus-summary-prev-thread
1547     [(meta down)] gnus-summary-next-thread
1548     [(meta up)] gnus-summary-prev-thread
1549     "\M-\C-u" gnus-summary-up-thread
1550     "\M-\C-d" gnus-summary-down-thread
1551     "&" gnus-summary-execute-command
1552     "c" gnus-summary-catchup-and-exit
1553     "\C-w" gnus-summary-mark-region-as-read
1554     "\C-t" gnus-summary-toggle-truncation
1555     "?" gnus-summary-mark-as-dormant
1556     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1557     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1558     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1559     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1560     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1561     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1562     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1563     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1564     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1565     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1566     "=" gnus-summary-expand-window
1567     "\C-x\C-s" gnus-summary-reselect-current-group
1568     "\M-g" gnus-summary-rescan-group
1569     "w" gnus-summary-stop-page-breaking
1570     "\C-c\C-r" gnus-summary-caesar-message
1571     "f" gnus-summary-followup
1572     "F" gnus-summary-followup-with-original
1573     "C" gnus-summary-cancel-article
1574     "r" gnus-summary-reply
1575     "R" gnus-summary-reply-with-original
1576     "\C-c\C-f" gnus-summary-mail-forward
1577     "o" gnus-summary-save-article
1578     "\C-o" gnus-summary-save-article-mail
1579     "|" gnus-summary-pipe-output
1580     "\M-k" gnus-summary-edit-local-kill
1581     "\M-K" gnus-summary-edit-global-kill
1582     ;; "V" gnus-version
1583     "\C-c\C-d" gnus-summary-describe-group
1584     "q" gnus-summary-exit
1585     "Q" gnus-summary-exit-no-update
1586     "\C-c\C-i" gnus-info-find-node
1587     gnus-mouse-2 gnus-mouse-pick-article
1588     "m" gnus-summary-mail-other-window
1589     "a" gnus-summary-post-news
1590     "i" gnus-summary-news-other-window
1591     "x" gnus-summary-limit-to-unread
1592     "s" gnus-summary-isearch-article
1593     "t" gnus-summary-toggle-header
1594     "g" gnus-summary-show-article
1595     "l" gnus-summary-goto-last-article
1596     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1597     "\C-d" gnus-summary-enter-digest-group
1598     "\M-\C-d" gnus-summary-read-document
1599     "\M-\C-e" gnus-summary-edit-parameters
1600     "\M-\C-a" gnus-summary-customize-parameters
1601     "\C-c\C-b" gnus-bug
1602     "*" gnus-cache-enter-article
1603     "\M-*" gnus-cache-remove-article
1604     "\M-&" gnus-summary-universal-argument
1605     "\C-l" gnus-recenter
1606     "I" gnus-summary-increase-score
1607     "L" gnus-summary-lower-score
1608     "\M-i" gnus-symbolic-argument
1609     "h" gnus-summary-select-article-buffer
1610
1611     "b" gnus-article-view-part
1612     "\M-t" gnus-summary-toggle-display-buttonized
1613
1614     "V" gnus-summary-score-map
1615     "X" gnus-uu-extract-map
1616     "S" gnus-summary-send-map)
1617
1618   ;; Sort of orthogonal keymap
1619   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1620     "t" gnus-summary-tick-article-forward
1621     "!" gnus-summary-tick-article-forward
1622     "d" gnus-summary-mark-as-read-forward
1623     "r" gnus-summary-mark-as-read-forward
1624     "c" gnus-summary-clear-mark-forward
1625     " " gnus-summary-clear-mark-forward
1626     "e" gnus-summary-mark-as-expirable
1627     "x" gnus-summary-mark-as-expirable
1628     "?" gnus-summary-mark-as-dormant
1629     "b" gnus-summary-set-bookmark
1630     "B" gnus-summary-remove-bookmark
1631     "#" gnus-summary-mark-as-processable
1632     "\M-#" gnus-summary-unmark-as-processable
1633     "S" gnus-summary-limit-include-expunged
1634     "C" gnus-summary-catchup
1635     "H" gnus-summary-catchup-to-here
1636     "h" gnus-summary-catchup-from-here
1637     "\C-c" gnus-summary-catchup-all
1638     "k" gnus-summary-kill-same-subject-and-select
1639     "K" gnus-summary-kill-same-subject
1640     "P" gnus-uu-mark-map)
1641
1642   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1643     "c" gnus-summary-clear-above
1644     "u" gnus-summary-tick-above
1645     "m" gnus-summary-mark-above
1646     "k" gnus-summary-kill-below)
1647
1648   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1649     "/" gnus-summary-limit-to-subject
1650     "n" gnus-summary-limit-to-articles
1651     "w" gnus-summary-pop-limit
1652     "s" gnus-summary-limit-to-subject
1653     "a" gnus-summary-limit-to-author
1654     "u" gnus-summary-limit-to-unread
1655     "m" gnus-summary-limit-to-marks
1656     "M" gnus-summary-limit-exclude-marks
1657     "v" gnus-summary-limit-to-score
1658     "*" gnus-summary-limit-include-cached
1659     "D" gnus-summary-limit-include-dormant
1660     "T" gnus-summary-limit-include-thread
1661     "d" gnus-summary-limit-exclude-dormant
1662     "t" gnus-summary-limit-to-age
1663     "." gnus-summary-limit-to-unseen
1664     "x" gnus-summary-limit-to-extra
1665     "p" gnus-summary-limit-to-display-predicate
1666     "E" gnus-summary-limit-include-expunged
1667     "c" gnus-summary-limit-exclude-childless-dormant
1668     "C" gnus-summary-limit-mark-excluded-as-read
1669     "o" gnus-summary-insert-old-articles
1670     "N" gnus-summary-insert-new-articles)
1671
1672   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1673     "n" gnus-summary-next-unread-article
1674     "p" gnus-summary-prev-unread-article
1675     "N" gnus-summary-next-article
1676     "P" gnus-summary-prev-article
1677     "\C-n" gnus-summary-next-same-subject
1678     "\C-p" gnus-summary-prev-same-subject
1679     "\M-n" gnus-summary-next-unread-subject
1680     "\M-p" gnus-summary-prev-unread-subject
1681     "f" gnus-summary-first-unread-article
1682     "b" gnus-summary-best-unread-article
1683     "j" gnus-summary-goto-article
1684     "g" gnus-summary-goto-subject
1685     "l" gnus-summary-goto-last-article
1686     "o" gnus-summary-pop-article)
1687
1688   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1689     "k" gnus-summary-kill-thread
1690     "l" gnus-summary-lower-thread
1691     "i" gnus-summary-raise-thread
1692     "T" gnus-summary-toggle-threads
1693     "t" gnus-summary-rethread-current
1694     "^" gnus-summary-reparent-thread
1695     "s" gnus-summary-show-thread
1696     "S" gnus-summary-show-all-threads
1697     "h" gnus-summary-hide-thread
1698     "H" gnus-summary-hide-all-threads
1699     "n" gnus-summary-next-thread
1700     "p" gnus-summary-prev-thread
1701     "u" gnus-summary-up-thread
1702     "o" gnus-summary-top-thread
1703     "d" gnus-summary-down-thread
1704     "#" gnus-uu-mark-thread
1705     "\M-#" gnus-uu-unmark-thread)
1706
1707   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1708     "g" gnus-summary-prepare
1709     "c" gnus-summary-insert-cached-articles)
1710
1711   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1712     "c" gnus-summary-catchup-and-exit
1713     "C" gnus-summary-catchup-all-and-exit
1714     "E" gnus-summary-exit-no-update
1715     "Q" gnus-summary-exit
1716     "Z" gnus-summary-exit
1717     "n" gnus-summary-catchup-and-goto-next-group
1718     "R" gnus-summary-reselect-current-group
1719     "G" gnus-summary-rescan-group
1720     "N" gnus-summary-next-group
1721     "s" gnus-summary-save-newsrc
1722     "P" gnus-summary-prev-group)
1723
1724   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1725     " " gnus-summary-next-page
1726     "n" gnus-summary-next-page
1727     "\177" gnus-summary-prev-page
1728     [delete] gnus-summary-prev-page
1729     "p" gnus-summary-prev-page
1730     "\r" gnus-summary-scroll-up
1731     "\M-\r" gnus-summary-scroll-down
1732     "<" gnus-summary-beginning-of-article
1733     ">" gnus-summary-end-of-article
1734     "b" gnus-summary-beginning-of-article
1735     "e" gnus-summary-end-of-article
1736     "^" gnus-summary-refer-parent-article
1737     "r" gnus-summary-refer-parent-article
1738     "D" gnus-summary-enter-digest-group
1739     "R" gnus-summary-refer-references
1740     "T" gnus-summary-refer-thread
1741     "g" gnus-summary-show-article
1742     "s" gnus-summary-isearch-article
1743     "P" gnus-summary-print-article
1744     "M" gnus-mailing-list-insinuate
1745     "t" gnus-article-babel)
1746
1747   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1748     "b" gnus-article-add-buttons
1749     "B" gnus-article-add-buttons-to-head
1750     "o" gnus-article-treat-overstrike
1751     "e" gnus-article-emphasize
1752     "w" gnus-article-fill-cited-article
1753     "Q" gnus-article-fill-long-lines
1754     "C" gnus-article-capitalize-sentences
1755     "c" gnus-article-remove-cr
1756     "q" gnus-article-de-quoted-unreadable
1757     "6" gnus-article-de-base64-unreadable
1758     "Z" gnus-article-decode-HZ
1759     "h" gnus-article-wash-html
1760     "u" gnus-article-unsplit-urls
1761     "s" gnus-summary-force-verify-and-decrypt
1762     "f" gnus-article-display-x-face
1763     "l" gnus-summary-stop-page-breaking
1764     "r" gnus-summary-caesar-message
1765     "t" gnus-summary-toggle-header
1766     "g" gnus-treat-smiley
1767     "v" gnus-summary-verbose-headers
1768     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1769     "p" gnus-article-verify-x-pgp-sig
1770     "d" gnus-article-treat-dumbquotes
1771     "k" gnus-article-outlook-deuglify-article)
1772
1773   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1774     "a" gnus-article-hide
1775     "h" gnus-article-hide-headers
1776     "b" gnus-article-hide-boring-headers
1777     "s" gnus-article-hide-signature
1778     "c" gnus-article-hide-citation
1779     "C" gnus-article-hide-citation-in-followups
1780     "l" gnus-article-hide-list-identifiers
1781     "p" gnus-article-hide-pgp
1782     "B" gnus-article-strip-banner
1783     "P" gnus-article-hide-pem
1784     "\C-c" gnus-article-hide-citation-maybe)
1785
1786   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1787     "a" gnus-article-highlight
1788     "h" gnus-article-highlight-headers
1789     "c" gnus-article-highlight-citation
1790     "s" gnus-article-highlight-signature)
1791
1792   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1793     "f" gnus-article-treat-fold-headers
1794     "u" gnus-article-treat-unfold-headers
1795     "n" gnus-article-treat-fold-newsgroups)
1796
1797   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1798     "x" gnus-article-display-x-face
1799     "s" gnus-treat-smiley
1800     "D" gnus-article-remove-images
1801     "f" gnus-treat-from-picon
1802     "m" gnus-treat-mail-picon
1803     "n" gnus-treat-newsgroups-picon)
1804
1805   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1806     "w" gnus-article-decode-mime-words
1807     "c" gnus-article-decode-charset
1808     "v" gnus-mime-view-all-parts
1809     "b" gnus-article-view-part)
1810
1811   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1812     "z" gnus-article-date-ut
1813     "u" gnus-article-date-ut
1814     "l" gnus-article-date-local
1815     "p" gnus-article-date-english
1816     "e" gnus-article-date-lapsed
1817     "o" gnus-article-date-original
1818     "i" gnus-article-date-iso8601
1819     "s" gnus-article-date-user)
1820
1821   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1822     "t" gnus-article-remove-trailing-blank-lines
1823     "l" gnus-article-strip-leading-blank-lines
1824     "m" gnus-article-strip-multiple-blank-lines
1825     "a" gnus-article-strip-blank-lines
1826     "A" gnus-article-strip-all-blank-lines
1827     "s" gnus-article-strip-leading-space
1828     "e" gnus-article-strip-trailing-space
1829     "w" gnus-article-remove-leading-whitespace)
1830
1831   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1832     "v" gnus-version
1833     "f" gnus-summary-fetch-faq
1834     "d" gnus-summary-describe-group
1835     "h" gnus-summary-describe-briefly
1836     "i" gnus-info-find-node)
1837
1838   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1839     "e" gnus-summary-expire-articles
1840     "\M-\C-e" gnus-summary-expire-articles-now
1841     "\177" gnus-summary-delete-article
1842     [delete] gnus-summary-delete-article
1843     [backspace] gnus-summary-delete-article
1844     "m" gnus-summary-move-article
1845     "r" gnus-summary-respool-article
1846     "w" gnus-summary-edit-article
1847     "c" gnus-summary-copy-article
1848     "B" gnus-summary-crosspost-article
1849     "q" gnus-summary-respool-query
1850     "t" gnus-summary-respool-trace
1851     "i" gnus-summary-import-article
1852     "I" gnus-summary-create-article
1853     "p" gnus-summary-article-posted-p)
1854
1855   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1856     "o" gnus-summary-save-article
1857     "m" gnus-summary-save-article-mail
1858     "F" gnus-summary-write-article-file
1859     "r" gnus-summary-save-article-rmail
1860     "f" gnus-summary-save-article-file
1861     "b" gnus-summary-save-article-body-file
1862     "h" gnus-summary-save-article-folder
1863     "v" gnus-summary-save-article-vm
1864     "p" gnus-summary-pipe-output
1865     "P" gnus-summary-muttprint
1866     "s" gnus-soup-add-article)
1867
1868   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1869     "b" gnus-summary-display-buttonized
1870     "m" gnus-summary-repair-multipart
1871     "v" gnus-article-view-part
1872     "o" gnus-article-save-part
1873     "c" gnus-article-copy-part
1874     "C" gnus-article-view-part-as-charset
1875     "e" gnus-article-view-part-externally
1876     "E" gnus-article-encrypt-body
1877     "i" gnus-article-inline-part
1878     "|" gnus-article-pipe-part)
1879
1880   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1881     "p" gnus-summary-mark-as-processable
1882     "u" gnus-summary-unmark-as-processable
1883     "U" gnus-summary-unmark-all-processable
1884     "v" gnus-uu-mark-over
1885     "s" gnus-uu-mark-series
1886     "r" gnus-uu-mark-region
1887     "g" gnus-uu-unmark-region
1888     "R" gnus-uu-mark-by-regexp
1889     "G" gnus-uu-unmark-by-regexp
1890     "t" gnus-uu-mark-thread
1891     "T" gnus-uu-unmark-thread
1892     "a" gnus-uu-mark-all
1893     "b" gnus-uu-mark-buffer
1894     "S" gnus-uu-mark-sparse
1895     "k" gnus-summary-kill-process-mark
1896     "y" gnus-summary-yank-process-mark
1897     "w" gnus-summary-save-process-mark
1898     "i" gnus-uu-invert-processable)
1899
1900   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1901     ;;"x" gnus-uu-extract-any
1902     "m" gnus-summary-save-parts
1903     "u" gnus-uu-decode-uu
1904     "U" gnus-uu-decode-uu-and-save
1905     "s" gnus-uu-decode-unshar
1906     "S" gnus-uu-decode-unshar-and-save
1907     "o" gnus-uu-decode-save
1908     "O" gnus-uu-decode-save
1909     "b" gnus-uu-decode-binhex
1910     "B" gnus-uu-decode-binhex
1911     "p" gnus-uu-decode-postscript
1912     "P" gnus-uu-decode-postscript-and-save)
1913
1914   (gnus-define-keys
1915       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1916     "u" gnus-uu-decode-uu-view
1917     "U" gnus-uu-decode-uu-and-save-view
1918     "s" gnus-uu-decode-unshar-view
1919     "S" gnus-uu-decode-unshar-and-save-view
1920     "o" gnus-uu-decode-save-view
1921     "O" gnus-uu-decode-save-view
1922     "b" gnus-uu-decode-binhex-view
1923     "B" gnus-uu-decode-binhex-view
1924     "p" gnus-uu-decode-postscript-view
1925     "P" gnus-uu-decode-postscript-and-save-view))
1926
1927 (defvar gnus-article-post-menu nil)
1928
1929 (defconst gnus-summary-menu-maxlen 20)
1930
1931 (defun gnus-summary-menu-split (menu)
1932   ;; If we have lots of elements, divide them into groups of 20
1933   ;; and make a pane (or submenu) for each one.
1934   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1935       (let ((menu menu) sublists next
1936             (i 1))
1937         (while menu
1938           ;; Pull off the next gnus-summary-menu-maxlen elements
1939           ;; and make them the next element of sublist.
1940           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1941           (if next
1942               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1943                       nil))
1944           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1945                                              (aref (car (last menu)) 0)) menu)
1946                                sublists))
1947           (setq i (1+ i))
1948           (setq menu next))
1949         (nreverse sublists))
1950     ;; Few elements--put them all in one pane.
1951     menu))
1952
1953 (defun gnus-summary-make-menu-bar ()
1954   (gnus-turn-off-edit-menu 'summary)
1955
1956   (unless (boundp 'gnus-summary-misc-menu)
1957
1958     (easy-menu-define
1959       gnus-summary-kill-menu gnus-summary-mode-map ""
1960       (cons
1961        "Score"
1962        (nconc
1963         (list
1964          ["Customize" gnus-score-customize t])
1965         (gnus-make-score-map 'increase)
1966         (gnus-make-score-map 'lower)
1967         '(("Mark"
1968            ["Kill below" gnus-summary-kill-below t]
1969            ["Mark above" gnus-summary-mark-above t]
1970            ["Tick above" gnus-summary-tick-above t]
1971            ["Clear above" gnus-summary-clear-above t])
1972           ["Current score" gnus-summary-current-score t]
1973           ["Set score" gnus-summary-set-score t]
1974           ["Switch current score file..." gnus-score-change-score-file t]
1975           ["Set mark below..." gnus-score-set-mark-below t]
1976           ["Set expunge below..." gnus-score-set-expunge-below t]
1977           ["Edit current score file" gnus-score-edit-current-scores t]
1978           ["Edit score file" gnus-score-edit-file t]
1979           ["Trace score" gnus-score-find-trace t]
1980           ["Find words" gnus-score-find-favourite-words t]
1981           ["Rescore buffer" gnus-summary-rescore t]
1982           ["Increase score..." gnus-summary-increase-score t]
1983           ["Lower score..." gnus-summary-lower-score t]))))
1984
1985     ;; Define both the Article menu in the summary buffer and the
1986     ;; equivalent Commands menu in the article buffer here for
1987     ;; consistency.
1988     (let ((innards
1989            `(("Hide"
1990               ["All" gnus-article-hide t]
1991               ["Headers" gnus-article-hide-headers t]
1992               ["Signature" gnus-article-hide-signature t]
1993               ["Citation" gnus-article-hide-citation t]
1994               ["List identifiers" gnus-article-hide-list-identifiers t]
1995               ["PGP" gnus-article-hide-pgp t]
1996               ["Banner" gnus-article-strip-banner t]
1997               ["Boring headers" gnus-article-hide-boring-headers t])
1998              ("Highlight"
1999               ["All" gnus-article-highlight t]
2000               ["Headers" gnus-article-highlight-headers t]
2001               ["Signature" gnus-article-highlight-signature t]
2002               ["Citation" gnus-article-highlight-citation t])
2003              ("MIME"
2004               ["Words" gnus-article-decode-mime-words t]
2005               ["Charset" gnus-article-decode-charset t]
2006               ["QP" gnus-article-de-quoted-unreadable t]
2007               ["Base64" gnus-article-de-base64-unreadable t]
2008               ["View MIME buttons" gnus-summary-display-buttonized t]
2009               ["View all" gnus-mime-view-all-parts t]
2010               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2011               ["Encrypt body" gnus-article-encrypt-body t]
2012               ["Extract all parts" gnus-summary-save-parts t])
2013              ("Date"
2014               ["Local" gnus-article-date-local t]
2015               ["ISO8601" gnus-article-date-iso8601 t]
2016               ["UT" gnus-article-date-ut t]
2017               ["Original" gnus-article-date-original t]
2018               ["Lapsed" gnus-article-date-lapsed t]
2019               ["User-defined" gnus-article-date-user t])
2020              ("Display"
2021               ["Remove images" gnus-article-remove-images t]
2022               ["Toggle smiley" gnus-treat-smiley t]
2023               ["Show X-Face" gnus-article-display-x-face t]
2024               ["Show picons in From" gnus-treat-from-picon t]
2025               ["Show picons in mail headers" gnus-treat-mail-picon t]
2026               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2027               ("View as different encoding"
2028                ,@(gnus-summary-menu-split
2029                   (mapcar
2030                    (lambda (cs)
2031                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2032                      ;; forms for menu commands, we should provide intern'ed
2033                      ;; function symbols.
2034                      (let ((command (intern (format "\
2035 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2036                        (fset command
2037                              `(lambda ()
2038                                 (interactive)
2039                                 (let ((gnus-summary-show-article-charset-alist
2040                                        '((1 . ,cs))))
2041                                   (gnus-summary-show-article 1))))
2042                        `[,(symbol-name cs) ,command t]))
2043                    (sort (if (fboundp 'coding-system-list)
2044                              (coding-system-list)
2045                            (mapcar 'car mm-mime-mule-charset-alist))
2046                          (lambda (a b)
2047                            (string< (symbol-name a)
2048                                     (symbol-name b))))))))
2049              ("Washing"
2050               ("Remove Blanks"
2051                ["Leading" gnus-article-strip-leading-blank-lines t]
2052                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2053                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2054                ["All of the above" gnus-article-strip-blank-lines t]
2055                ["All" gnus-article-strip-all-blank-lines t]
2056                ["Leading space" gnus-article-strip-leading-space t]
2057                ["Trailing space" gnus-article-strip-trailing-space t]
2058                ["Leading space in headers"
2059                 gnus-article-remove-leading-whitespace t])
2060               ["Overstrike" gnus-article-treat-overstrike t]
2061               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2062               ["Emphasis" gnus-article-emphasize t]
2063               ["Word wrap" gnus-article-fill-cited-article t]
2064               ["Fill long lines" gnus-article-fill-long-lines t]
2065               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2066               ["CR" gnus-article-remove-cr t]
2067               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2068               ["Base64" gnus-article-de-base64-unreadable t]
2069               ["Rot 13" gnus-summary-caesar-message
2070                ,@(if (featurep 'xemacs) '(t)
2071                    '(:help "\"Caesar rotate\" article by 13"))]
2072               ["Unix pipe..." gnus-summary-pipe-message t]
2073               ["Add buttons" gnus-article-add-buttons t]
2074               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2075               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2076               ["Verbose header" gnus-summary-verbose-headers t]
2077               ["Toggle header" gnus-summary-toggle-header t]
2078               ["Unfold headers" gnus-article-treat-unfold-headers t]
2079               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2080               ["Html" gnus-article-wash-html t]
2081               ["URLs" gnus-article-unsplit-urls t]
2082               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2083               ["HZ" gnus-article-decode-HZ t]
2084               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2085               )
2086              ("Output"
2087               ["Save in default format" gnus-summary-save-article
2088                ,@(if (featurep 'xemacs) '(t)
2089                    '(:help "Save article using default method"))]
2090               ["Save in file" gnus-summary-save-article-file
2091                ,@(if (featurep 'xemacs) '(t)
2092                    '(:help "Save article in file"))]
2093               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2094               ["Save in MH folder" gnus-summary-save-article-folder t]
2095               ["Save in VM folder" gnus-summary-save-article-vm t]
2096               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2097               ["Save body in file" gnus-summary-save-article-body-file t]
2098               ["Pipe through a filter" gnus-summary-pipe-output t]
2099               ["Add to SOUP packet" gnus-soup-add-article t]
2100               ["Print with Muttprint" gnus-summary-muttprint t]
2101               ["Print" gnus-summary-print-article t])
2102              ("Backend"
2103               ["Respool article..." gnus-summary-respool-article t]
2104               ["Move article..." gnus-summary-move-article
2105                (gnus-check-backend-function
2106                 'request-move-article gnus-newsgroup-name)]
2107               ["Copy article..." gnus-summary-copy-article t]
2108               ["Crosspost article..." gnus-summary-crosspost-article
2109                (gnus-check-backend-function
2110                 'request-replace-article gnus-newsgroup-name)]
2111               ["Import file..." gnus-summary-import-article t]
2112               ["Create article..." gnus-summary-create-article t]
2113               ["Check if posted" gnus-summary-article-posted-p t]
2114               ["Edit article" gnus-summary-edit-article
2115                (not (gnus-group-read-only-p))]
2116               ["Delete article" gnus-summary-delete-article
2117                (gnus-check-backend-function
2118                 'request-expire-articles gnus-newsgroup-name)]
2119               ["Query respool" gnus-summary-respool-query t]
2120               ["Trace respool" gnus-summary-respool-trace t]
2121               ["Delete expirable articles" gnus-summary-expire-articles-now
2122                (gnus-check-backend-function
2123                 'request-expire-articles gnus-newsgroup-name)])
2124              ("Extract"
2125               ["Uudecode" gnus-uu-decode-uu
2126                ,@(if (featurep 'xemacs) '(t)
2127                    '(:help "Decode uuencoded article(s)"))]
2128               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2129               ["Unshar" gnus-uu-decode-unshar t]
2130               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2131               ["Save" gnus-uu-decode-save t]
2132               ["Binhex" gnus-uu-decode-binhex t]
2133               ["Postscript" gnus-uu-decode-postscript t]
2134               ["All MIME parts" gnus-summary-save-parts t])
2135              ("Cache"
2136               ["Enter article" gnus-cache-enter-article t]
2137               ["Remove article" gnus-cache-remove-article t])
2138              ["Translate" gnus-article-babel t]
2139              ["Select article buffer" gnus-summary-select-article-buffer t]
2140              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2141              ["Isearch article..." gnus-summary-isearch-article t]
2142              ["Beginning of the article" gnus-summary-beginning-of-article t]
2143              ["End of the article" gnus-summary-end-of-article t]
2144              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2145              ["Fetch referenced articles" gnus-summary-refer-references t]
2146              ["Fetch current thread" gnus-summary-refer-thread t]
2147              ["Fetch article with id..." gnus-summary-refer-article t]
2148              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2149              ["Redisplay" gnus-summary-show-article t]
2150              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2151       (easy-menu-define
2152         gnus-summary-article-menu gnus-summary-mode-map ""
2153         (cons "Article" innards))
2154
2155       (if (not (keymapp gnus-summary-article-menu))
2156           (easy-menu-define
2157             gnus-article-commands-menu gnus-article-mode-map ""
2158             (cons "Commands" innards))
2159         ;; in Emacs, don't share menu.
2160         (setq gnus-article-commands-menu
2161               (copy-keymap gnus-summary-article-menu))
2162         (define-key gnus-article-mode-map [menu-bar commands]
2163           (cons "Commands" gnus-article-commands-menu))))
2164
2165     (easy-menu-define
2166       gnus-summary-thread-menu gnus-summary-mode-map ""
2167       '("Threads"
2168         ["Toggle threading" gnus-summary-toggle-threads t]
2169         ["Hide threads" gnus-summary-hide-all-threads t]
2170         ["Show threads" gnus-summary-show-all-threads t]
2171         ["Hide thread" gnus-summary-hide-thread t]
2172         ["Show thread" gnus-summary-show-thread t]
2173         ["Go to next thread" gnus-summary-next-thread t]
2174         ["Go to previous thread" gnus-summary-prev-thread t]
2175         ["Go down thread" gnus-summary-down-thread t]
2176         ["Go up thread" gnus-summary-up-thread t]
2177         ["Top of thread" gnus-summary-top-thread t]
2178         ["Mark thread as read" gnus-summary-kill-thread t]
2179         ["Lower thread score" gnus-summary-lower-thread t]
2180         ["Raise thread score" gnus-summary-raise-thread t]
2181         ["Rethread current" gnus-summary-rethread-current t]))
2182
2183     (easy-menu-define
2184       gnus-summary-post-menu gnus-summary-mode-map ""
2185       `("Post"
2186         ["Send a message (mail or news)" gnus-summary-post-news
2187          ,@(if (featurep 'xemacs) '(t)
2188              '(:help "Post an article"))]
2189         ["Followup" gnus-summary-followup
2190          ,@(if (featurep 'xemacs) '(t)
2191              '(:help "Post followup to this article"))]
2192         ["Followup and yank" gnus-summary-followup-with-original
2193          ,@(if (featurep 'xemacs) '(t)
2194              '(:help "Post followup to this article, quoting its contents"))]
2195         ["Supersede article" gnus-summary-supersede-article t]
2196         ["Cancel article" gnus-summary-cancel-article
2197          ,@(if (featurep 'xemacs) '(t)
2198              '(:help "Cancel an article you posted"))]
2199         ["Reply" gnus-summary-reply t]
2200         ["Reply and yank" gnus-summary-reply-with-original t]
2201         ["Wide reply" gnus-summary-wide-reply t]
2202         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2203          ,@(if (featurep 'xemacs) '(t)
2204              '(:help "Mail a reply, quoting this article"))]
2205         ["Very wide reply" gnus-summary-very-wide-reply t]
2206         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2207          ,@(if (featurep 'xemacs) '(t)
2208              '(:help "Mail a very wide reply, quoting this article"))]
2209         ["Mail forward" gnus-summary-mail-forward t]
2210         ["Post forward" gnus-summary-post-forward t]
2211         ["Digest and mail" gnus-uu-digest-mail-forward t]
2212         ["Digest and post" gnus-uu-digest-post-forward t]
2213         ["Resend message" gnus-summary-resend-message t]
2214         ["Resend message edit" gnus-summary-resend-message-edit t]
2215         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2216         ["Send a mail" gnus-summary-mail-other-window t]
2217         ["Create a local message" gnus-summary-news-other-window t]
2218         ["Uuencode and post" gnus-uu-post-news
2219          ,@(if (featurep 'xemacs) '(t)
2220              '(:help "Post a uuencoded article"))]
2221         ["Followup via news" gnus-summary-followup-to-mail t]
2222         ["Followup via news and yank"
2223          gnus-summary-followup-to-mail-with-original t]
2224         ;;("Draft"
2225         ;;["Send" gnus-summary-send-draft t]
2226         ;;["Send bounced" gnus-resend-bounced-mail t])
2227         ))
2228
2229     (cond
2230      ((not (keymapp gnus-summary-post-menu))
2231       (setq gnus-article-post-menu gnus-summary-post-menu))
2232      ((not gnus-article-post-menu)
2233       ;; Don't share post menu.
2234       (setq gnus-article-post-menu
2235             (copy-keymap gnus-summary-post-menu))))
2236     (define-key gnus-article-mode-map [menu-bar post]
2237       (cons "Post" gnus-article-post-menu))
2238
2239     (easy-menu-define
2240       gnus-summary-misc-menu gnus-summary-mode-map ""
2241       `("Gnus"
2242         ("Mark Read"
2243          ["Mark as read" gnus-summary-mark-as-read-forward t]
2244          ["Mark same subject and select"
2245           gnus-summary-kill-same-subject-and-select t]
2246          ["Mark same subject" gnus-summary-kill-same-subject t]
2247          ["Catchup" gnus-summary-catchup
2248           ,@(if (featurep 'xemacs) '(t)
2249               '(:help "Mark unread articles in this group as read"))]
2250          ["Catchup all" gnus-summary-catchup-all t]
2251          ["Catchup to here" gnus-summary-catchup-to-here t]
2252          ["Catchup from here" gnus-summary-catchup-from-here t]
2253          ["Catchup region" gnus-summary-mark-region-as-read t]
2254          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2255         ("Mark Various"
2256          ["Tick" gnus-summary-tick-article-forward t]
2257          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2258          ["Remove marks" gnus-summary-clear-mark-forward t]
2259          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2260          ["Set bookmark" gnus-summary-set-bookmark t]
2261          ["Remove bookmark" gnus-summary-remove-bookmark t])
2262         ("Limit to"
2263          ["Marks..." gnus-summary-limit-to-marks t]
2264          ["Subject..." gnus-summary-limit-to-subject t]
2265          ["Author..." gnus-summary-limit-to-author t]
2266          ["Age..." gnus-summary-limit-to-age t]
2267          ["Extra..." gnus-summary-limit-to-extra t]
2268          ["Score..." gnus-summary-limit-to-score t]
2269          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2270          ["Unread" gnus-summary-limit-to-unread t]
2271          ["Unseen" gnus-summary-limit-to-unseen t]
2272          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2273          ["Articles" gnus-summary-limit-to-articles t]
2274          ["Pop limit" gnus-summary-pop-limit t]
2275          ["Show dormant" gnus-summary-limit-include-dormant t]
2276          ["Hide childless dormant"
2277           gnus-summary-limit-exclude-childless-dormant t]
2278          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2279          ["Hide marked" gnus-summary-limit-exclude-marks t]
2280          ["Show expunged" gnus-summary-limit-include-expunged t])
2281         ("Process Mark"
2282          ["Set mark" gnus-summary-mark-as-processable t]
2283          ["Remove mark" gnus-summary-unmark-as-processable t]
2284          ["Remove all marks" gnus-summary-unmark-all-processable t]
2285          ["Mark above" gnus-uu-mark-over t]
2286          ["Mark series" gnus-uu-mark-series t]
2287          ["Mark region" gnus-uu-mark-region t]
2288          ["Unmark region" gnus-uu-unmark-region t]
2289          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2290          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2291          ["Mark all" gnus-uu-mark-all t]
2292          ["Mark buffer" gnus-uu-mark-buffer t]
2293          ["Mark sparse" gnus-uu-mark-sparse t]
2294          ["Mark thread" gnus-uu-mark-thread t]
2295          ["Unmark thread" gnus-uu-unmark-thread t]
2296          ("Process Mark Sets"
2297           ["Kill" gnus-summary-kill-process-mark t]
2298           ["Yank" gnus-summary-yank-process-mark
2299            gnus-newsgroup-process-stack]
2300           ["Save" gnus-summary-save-process-mark t]))
2301         ("Scroll article"
2302          ["Page forward" gnus-summary-next-page
2303           ,@(if (featurep 'xemacs) '(t)
2304               '(:help "Show next page of article"))]
2305          ["Page backward" gnus-summary-prev-page
2306           ,@(if (featurep 'xemacs) '(t)
2307               '(:help "Show previous page of article"))]
2308          ["Line forward" gnus-summary-scroll-up t])
2309         ("Move"
2310          ["Next unread article" gnus-summary-next-unread-article t]
2311          ["Previous unread article" gnus-summary-prev-unread-article t]
2312          ["Next article" gnus-summary-next-article t]
2313          ["Previous article" gnus-summary-prev-article t]
2314          ["Next unread subject" gnus-summary-next-unread-subject t]
2315          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2316          ["Next article same subject" gnus-summary-next-same-subject t]
2317          ["Previous article same subject" gnus-summary-prev-same-subject t]
2318          ["First unread article" gnus-summary-first-unread-article t]
2319          ["Best unread article" gnus-summary-best-unread-article t]
2320          ["Go to subject number..." gnus-summary-goto-subject t]
2321          ["Go to article number..." gnus-summary-goto-article t]
2322          ["Go to the last article" gnus-summary-goto-last-article t]
2323          ["Pop article off history" gnus-summary-pop-article t])
2324         ("Sort"
2325          ["Sort by number" gnus-summary-sort-by-number t]
2326          ["Sort by author" gnus-summary-sort-by-author t]
2327          ["Sort by subject" gnus-summary-sort-by-subject t]
2328          ["Sort by date" gnus-summary-sort-by-date t]
2329          ["Sort by score" gnus-summary-sort-by-score t]
2330          ["Sort by lines" gnus-summary-sort-by-lines t]
2331          ["Sort by characters" gnus-summary-sort-by-chars t]
2332          ["Randomize" gnus-summary-sort-by-random t]
2333          ["Original sort" gnus-summary-sort-by-original t])
2334         ("Help"
2335          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2336          ["Describe group" gnus-summary-describe-group t]
2337          ["Read manual" gnus-info-find-node t])
2338         ("Modes"
2339          ["Pick and read" gnus-pick-mode t]
2340          ["Binary" gnus-binary-mode t])
2341         ("Regeneration"
2342          ["Regenerate" gnus-summary-prepare t]
2343          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2344          ["Toggle threading" gnus-summary-toggle-threads t])
2345         ["See old articles" gnus-summary-insert-old-articles t]
2346         ["See new articles" gnus-summary-insert-new-articles t]
2347         ["Filter articles..." gnus-summary-execute-command t]
2348         ["Run command on subjects..." gnus-summary-universal-argument t]
2349         ["Search articles forward..." gnus-summary-search-article-forward t]
2350         ["Search articles backward..." gnus-summary-search-article-backward t]
2351         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2352         ["Expand window" gnus-summary-expand-window t]
2353         ["Expire expirable articles" gnus-summary-expire-articles
2354          (gnus-check-backend-function
2355           'request-expire-articles gnus-newsgroup-name)]
2356         ["Edit local kill file" gnus-summary-edit-local-kill t]
2357         ["Edit main kill file" gnus-summary-edit-global-kill t]
2358         ["Edit group parameters" gnus-summary-edit-parameters t]
2359         ["Customize group parameters" gnus-summary-customize-parameters t]
2360         ["Send a bug report" gnus-bug t]
2361         ("Exit"
2362          ["Catchup and exit" gnus-summary-catchup-and-exit
2363           ,@(if (featurep 'xemacs) '(t)
2364               '(:help "Mark unread articles in this group as read, then exit"))]
2365          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2366          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2367          ["Exit group" gnus-summary-exit
2368           ,@(if (featurep 'xemacs) '(t)
2369               '(:help "Exit current group, return to group selection mode"))]
2370          ["Exit group without updating" gnus-summary-exit-no-update t]
2371          ["Exit and goto next group" gnus-summary-next-group t]
2372          ["Exit and goto prev group" gnus-summary-prev-group t]
2373          ["Reselect group" gnus-summary-reselect-current-group t]
2374          ["Rescan group" gnus-summary-rescan-group t]
2375          ["Update dribble" gnus-summary-save-newsrc t])))
2376
2377     (gnus-run-hooks 'gnus-summary-menu-hook)))
2378
2379 (defvar gnus-summary-tool-bar-map nil)
2380
2381 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2382 (defun gnus-summary-make-tool-bar ()
2383   (if (and (fboundp 'tool-bar-add-item-from-menu)
2384            (default-value 'tool-bar-mode)
2385            (not gnus-summary-tool-bar-map))
2386       (setq gnus-summary-tool-bar-map
2387             (let ((tool-bar-map (make-sparse-keymap))
2388                   (load-path (mm-image-load-path)))
2389               (tool-bar-add-item-from-menu
2390                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2391               (tool-bar-add-item-from-menu
2392                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2393               (tool-bar-add-item-from-menu
2394                'gnus-summary-post-news "post" gnus-summary-mode-map)
2395               (tool-bar-add-item-from-menu
2396                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2397               (tool-bar-add-item-from-menu
2398                'gnus-summary-followup "followup" gnus-summary-mode-map)
2399               (tool-bar-add-item-from-menu
2400                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2401               (tool-bar-add-item-from-menu
2402                'gnus-summary-reply "reply" gnus-summary-mode-map)
2403               (tool-bar-add-item-from-menu
2404                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2405               (tool-bar-add-item-from-menu
2406                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2407               (tool-bar-add-item-from-menu
2408                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2409               (tool-bar-add-item-from-menu
2410                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2411               (tool-bar-add-item-from-menu
2412                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2413               (tool-bar-add-item-from-menu
2414                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2415               (tool-bar-add-item-from-menu
2416                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2417               (tool-bar-add-item-from-menu
2418                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2419               tool-bar-map)))
2420   (if gnus-summary-tool-bar-map
2421       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2422
2423 (defun gnus-score-set-default (var value)
2424   "A version of set that updates the GNU Emacs menu-bar."
2425   (set var value)
2426   ;; It is the message that forces the active status to be updated.
2427   (message ""))
2428
2429 (defun gnus-make-score-map (type)
2430   "Make a summary score map of type TYPE."
2431   (if t
2432       nil
2433     (let ((headers '(("author" "from" string)
2434                      ("subject" "subject" string)
2435                      ("article body" "body" string)
2436                      ("article head" "head" string)
2437                      ("xref" "xref" string)
2438                      ("extra header" "extra" string)
2439                      ("lines" "lines" number)
2440                      ("followups to author" "followup" string)))
2441           (types '((number ("less than" <)
2442                            ("greater than" >)
2443                            ("equal" =))
2444                    (string ("substring" s)
2445                            ("exact string" e)
2446                            ("fuzzy string" f)
2447                            ("regexp" r))))
2448           (perms '(("temporary" (current-time-string))
2449                    ("permanent" nil)
2450                    ("immediate" now)))
2451           header)
2452       (list
2453        (apply
2454         'nconc
2455         (list
2456          (if (eq type 'lower)
2457              "Lower score"
2458            "Increase score"))
2459         (let (outh)
2460           (while headers
2461             (setq header (car headers))
2462             (setq outh
2463                   (cons
2464                    (apply
2465                     'nconc
2466                     (list (car header))
2467                     (let ((ts (cdr (assoc (nth 2 header) types)))
2468                           outt)
2469                       (while ts
2470                         (setq outt
2471                               (cons
2472                                (apply
2473                                 'nconc
2474                                 (list (caar ts))
2475                                 (let ((ps perms)
2476                                       outp)
2477                                   (while ps
2478                                     (setq outp
2479                                           (cons
2480                                            (vector
2481                                             (caar ps)
2482                                             (list
2483                                              'gnus-summary-score-entry
2484                                              (nth 1 header)
2485                                              (if (or (string= (nth 1 header)
2486                                                               "head")
2487                                                      (string= (nth 1 header)
2488                                                               "body"))
2489                                                  ""
2490                                                (list 'gnus-summary-header
2491                                                      (nth 1 header)))
2492                                              (list 'quote (nth 1 (car ts)))
2493                                              (list 'gnus-score-delta-default
2494                                                    nil)
2495                                              (nth 1 (car ps))
2496                                              t)
2497                                             t)
2498                                            outp))
2499                                     (setq ps (cdr ps)))
2500                                   (list (nreverse outp))))
2501                                outt))
2502                         (setq ts (cdr ts)))
2503                       (list (nreverse outt))))
2504                    outh))
2505             (setq headers (cdr headers)))
2506           (list (nreverse outh))))))))
2507
2508 \f
2509
2510 (defun gnus-summary-mode (&optional group)
2511   "Major mode for reading articles.
2512
2513 All normal editing commands are switched off.
2514 \\<gnus-summary-mode-map>
2515 Each line in this buffer represents one article.  To read an
2516 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2517 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2518 respectively.
2519
2520 You can also post articles and send mail from this buffer.  To
2521 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2522 of an article, type `\\[gnus-summary-reply]'.
2523
2524 There are approx. one gazillion commands you can execute in this
2525 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2526
2527 The following commands are available:
2528
2529 \\{gnus-summary-mode-map}"
2530   (interactive)
2531   (kill-all-local-variables)
2532   (when (gnus-visual-p 'summary-menu 'menu)
2533     (gnus-summary-make-menu-bar)
2534     (gnus-summary-make-tool-bar))
2535   (gnus-summary-make-local-variables)
2536   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2537     (gnus-summary-make-local-variables))
2538   (gnus-make-thread-indent-array)
2539   (gnus-simplify-mode-line)
2540   (setq major-mode 'gnus-summary-mode)
2541   (setq mode-name "Summary")
2542   (make-local-variable 'minor-mode-alist)
2543   (use-local-map gnus-summary-mode-map)
2544   (buffer-disable-undo)
2545   (setq buffer-read-only t)             ;Disable modification
2546   (setq truncate-lines t)
2547   (setq selective-display t)
2548   (setq selective-display-ellipses t)   ;Display `...'
2549   (gnus-summary-set-display-table)
2550   (gnus-set-default-directory)
2551   (setq gnus-newsgroup-name group)
2552   (make-local-variable 'gnus-summary-line-format)
2553   (make-local-variable 'gnus-summary-line-format-spec)
2554   (make-local-variable 'gnus-summary-dummy-line-format)
2555   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2556   (make-local-variable 'gnus-summary-mark-positions)
2557   (make-local-hook 'pre-command-hook)
2558   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2559   (gnus-run-hooks 'gnus-summary-mode-hook)
2560   (turn-on-gnus-mailing-list-mode)
2561   (mm-enable-multibyte)
2562   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2563   (gnus-update-summary-mark-positions))
2564
2565 (defun gnus-summary-make-local-variables ()
2566   "Make all the local summary buffer variables."
2567   (let (global)
2568     (dolist (local gnus-summary-local-variables)
2569       (if (consp local)
2570           (progn
2571             (if (eq (cdr local) 'global)
2572                 ;; Copy the global value of the variable.
2573                 (setq global (symbol-value (car local)))
2574               ;; Use the value from the list.
2575               (setq global (eval (cdr local))))
2576             (set (make-local-variable (car local)) global))
2577         ;; Simple nil-valued local variable.
2578         (set (make-local-variable local) nil)))))
2579
2580 (defun gnus-summary-clear-local-variables ()
2581   (let ((locals gnus-summary-local-variables))
2582     (while locals
2583       (if (consp (car locals))
2584           (and (vectorp (caar locals))
2585                (set (caar locals) nil))
2586         (and (vectorp (car locals))
2587              (set (car locals) nil)))
2588       (setq locals (cdr locals)))))
2589
2590 ;; Summary data functions.
2591
2592 (defmacro gnus-data-number (data)
2593   `(car ,data))
2594
2595 (defmacro gnus-data-set-number (data number)
2596   `(setcar ,data ,number))
2597
2598 (defmacro gnus-data-mark (data)
2599   `(nth 1 ,data))
2600
2601 (defmacro gnus-data-set-mark (data mark)
2602   `(setcar (nthcdr 1 ,data) ,mark))
2603
2604 (defmacro gnus-data-pos (data)
2605   `(nth 2 ,data))
2606
2607 (defmacro gnus-data-set-pos (data pos)
2608   `(setcar (nthcdr 2 ,data) ,pos))
2609
2610 (defmacro gnus-data-header (data)
2611   `(nth 3 ,data))
2612
2613 (defmacro gnus-data-set-header (data header)
2614   `(setf (nth 3 ,data) ,header))
2615
2616 (defmacro gnus-data-level (data)
2617   `(nth 4 ,data))
2618
2619 (defmacro gnus-data-unread-p (data)
2620   `(= (nth 1 ,data) gnus-unread-mark))
2621
2622 (defmacro gnus-data-read-p (data)
2623   `(/= (nth 1 ,data) gnus-unread-mark))
2624
2625 (defmacro gnus-data-pseudo-p (data)
2626   `(consp (nth 3 ,data)))
2627
2628 (defmacro gnus-data-find (number)
2629   `(assq ,number gnus-newsgroup-data))
2630
2631 (defmacro gnus-data-find-list (number &optional data)
2632   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2633      (memq (assq ,number bdata)
2634            bdata)))
2635
2636 (defmacro gnus-data-make (number mark pos header level)
2637   `(list ,number ,mark ,pos ,header ,level))
2638
2639 (defun gnus-data-enter (after-article number mark pos header level offset)
2640   (let ((data (gnus-data-find-list after-article)))
2641     (unless data
2642       (error "No such article: %d" after-article))
2643     (setcdr data (cons (gnus-data-make number mark pos header level)
2644                        (cdr data)))
2645     (setq gnus-newsgroup-data-reverse nil)
2646     (gnus-data-update-list (cddr data) offset)))
2647
2648 (defun gnus-data-enter-list (after-article list &optional offset)
2649   (when list
2650     (let ((data (and after-article (gnus-data-find-list after-article)))
2651           (ilist list))
2652       (if (not (or data
2653                    after-article))
2654           (let ((odata gnus-newsgroup-data))
2655             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2656             (when offset
2657               (gnus-data-update-list odata offset)))
2658       ;; Find the last element in the list to be spliced into the main
2659         ;; list.
2660         (while (cdr list)
2661           (setq list (cdr list)))
2662         (if (not data)
2663             (progn
2664               (setcdr list gnus-newsgroup-data)
2665               (setq gnus-newsgroup-data ilist)
2666               (when offset
2667                 (gnus-data-update-list (cdr list) offset)))
2668           (setcdr list (cdr data))
2669           (setcdr data ilist)
2670           (when offset
2671             (gnus-data-update-list (cdr list) offset))))
2672       (setq gnus-newsgroup-data-reverse nil))))
2673
2674 (defun gnus-data-remove (article &optional offset)
2675   (let ((data gnus-newsgroup-data))
2676     (if (= (gnus-data-number (car data)) article)
2677         (progn
2678           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2679                 gnus-newsgroup-data-reverse nil)
2680           (when offset
2681             (gnus-data-update-list gnus-newsgroup-data offset)))
2682       (while (cdr data)
2683         (when (= (gnus-data-number (cadr data)) article)
2684           (setcdr data (cddr data))
2685           (when offset
2686             (gnus-data-update-list (cdr data) offset))
2687           (setq data nil
2688                 gnus-newsgroup-data-reverse nil))
2689         (setq data (cdr data))))))
2690
2691 (defmacro gnus-data-list (backward)
2692   `(if ,backward
2693        (or gnus-newsgroup-data-reverse
2694            (setq gnus-newsgroup-data-reverse
2695                  (reverse gnus-newsgroup-data)))
2696      gnus-newsgroup-data))
2697
2698 (defun gnus-data-update-list (data offset)
2699   "Add OFFSET to the POS of all data entries in DATA."
2700   (setq gnus-newsgroup-data-reverse nil)
2701   (while data
2702     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2703     (setq data (cdr data))))
2704
2705 (defun gnus-summary-article-pseudo-p (article)
2706   "Say whether this article is a pseudo article or not."
2707   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2708
2709 (defmacro gnus-summary-article-sparse-p (article)
2710   "Say whether this article is a sparse article or not."
2711   `(memq ,article gnus-newsgroup-sparse))
2712
2713 (defmacro gnus-summary-article-ancient-p (article)
2714   "Say whether this article is a sparse article or not."
2715   `(memq ,article gnus-newsgroup-ancient))
2716
2717 (defun gnus-article-parent-p (number)
2718   "Say whether this article is a parent or not."
2719   (let ((data (gnus-data-find-list number)))
2720     (and (cdr data)              ; There has to be an article after...
2721          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2722             (gnus-data-level (nth 1 data))))))
2723
2724 (defun gnus-article-children (number)
2725   "Return a list of all children to NUMBER."
2726   (let* ((data (gnus-data-find-list number))
2727          (level (gnus-data-level (car data)))
2728          children)
2729     (setq data (cdr data))
2730     (while (and data
2731                 (= (gnus-data-level (car data)) (1+ level)))
2732       (push (gnus-data-number (car data)) children)
2733       (setq data (cdr data)))
2734     children))
2735
2736 (defmacro gnus-summary-skip-intangible ()
2737   "If the current article is intangible, then jump to a different article."
2738   '(let ((to (get-text-property (point) 'gnus-intangible)))
2739      (and to (gnus-summary-goto-subject to))))
2740
2741 (defmacro gnus-summary-article-intangible-p ()
2742   "Say whether this article is intangible or not."
2743   '(get-text-property (point) 'gnus-intangible))
2744
2745 (defun gnus-article-read-p (article)
2746   "Say whether ARTICLE is read or not."
2747   (not (or (memq article gnus-newsgroup-marked)
2748            (memq article gnus-newsgroup-spam-marked)
2749            (memq article gnus-newsgroup-unreads)
2750            (memq article gnus-newsgroup-unselected)
2751            (memq article gnus-newsgroup-dormant))))
2752
2753 ;; Some summary mode macros.
2754
2755 (defmacro gnus-summary-article-number ()
2756   "The article number of the article on the current line.
2757 If there isn's an article number here, then we return the current
2758 article number."
2759   '(progn
2760      (gnus-summary-skip-intangible)
2761      (or (get-text-property (point) 'gnus-number)
2762          (gnus-summary-last-subject))))
2763
2764 (defmacro gnus-summary-article-header (&optional number)
2765   "Return the header of article NUMBER."
2766   `(gnus-data-header (gnus-data-find
2767                       ,(or number '(gnus-summary-article-number)))))
2768
2769 (defmacro gnus-summary-thread-level (&optional number)
2770   "Return the level of thread that starts with article NUMBER."
2771   `(if (and (eq gnus-summary-make-false-root 'dummy)
2772             (get-text-property (point) 'gnus-intangible))
2773        0
2774      (gnus-data-level (gnus-data-find
2775                        ,(or number '(gnus-summary-article-number))))))
2776
2777 (defmacro gnus-summary-article-mark (&optional number)
2778   "Return the mark of article NUMBER."
2779   `(gnus-data-mark (gnus-data-find
2780                     ,(or number '(gnus-summary-article-number)))))
2781
2782 (defmacro gnus-summary-article-pos (&optional number)
2783   "Return the position of the line of article NUMBER."
2784   `(gnus-data-pos (gnus-data-find
2785                    ,(or number '(gnus-summary-article-number)))))
2786
2787 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2788 (defmacro gnus-summary-article-subject (&optional number)
2789   "Return current subject string or nil if nothing."
2790   `(let ((headers
2791           ,(if number
2792                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2793              '(gnus-data-header (assq (gnus-summary-article-number)
2794                                       gnus-newsgroup-data)))))
2795      (and headers
2796           (vectorp headers)
2797           (mail-header-subject headers))))
2798
2799 (defmacro gnus-summary-article-score (&optional number)
2800   "Return current article score."
2801   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2802                   gnus-newsgroup-scored))
2803        gnus-summary-default-score 0))
2804
2805 (defun gnus-summary-article-children (&optional number)
2806   "Return a list of article numbers that are children of article NUMBER."
2807   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2808          (level (gnus-data-level (car data)))
2809          l children)
2810     (while (and (setq data (cdr data))
2811                 (> (setq l (gnus-data-level (car data))) level))
2812       (and (= (1+ level) l)
2813            (push (gnus-data-number (car data))
2814                  children)))
2815     (nreverse children)))
2816
2817 (defun gnus-summary-article-parent (&optional number)
2818   "Return the article number of the parent of article NUMBER."
2819   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2820                                     (gnus-data-list t)))
2821          (level (gnus-data-level (car data))))
2822     (if (zerop level)
2823         ()                              ; This is a root.
2824       ;; We search until we find an article with a level less than
2825       ;; this one.  That function has to be the parent.
2826       (while (and (setq data (cdr data))
2827                   (not (< (gnus-data-level (car data)) level))))
2828       (and data (gnus-data-number (car data))))))
2829
2830 (defun gnus-unread-mark-p (mark)
2831   "Say whether MARK is the unread mark."
2832   (= mark gnus-unread-mark))
2833
2834 (defun gnus-read-mark-p (mark)
2835   "Say whether MARK is one of the marks that mark as read.
2836 This is all marks except unread, ticked, dormant, and expirable."
2837   (not (or (= mark gnus-unread-mark)
2838            (= mark gnus-ticked-mark)
2839            (= mark gnus-dormant-mark)
2840            (= mark gnus-expirable-mark))))
2841
2842 (defmacro gnus-article-mark (number)
2843   "Return the MARK of article NUMBER.
2844 This macro should only be used when computing the mark the \"first\"
2845 time; i.e., when generating the summary lines.  After that,
2846 `gnus-summary-article-mark' should be used to examine the
2847 marks of articles."
2848   `(cond
2849     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2850     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2851     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2852     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2853     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2854     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2855     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2856     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2857     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2858            gnus-ancient-mark))))
2859
2860 ;; Saving hidden threads.
2861
2862 (defmacro gnus-save-hidden-threads (&rest forms)
2863   "Save hidden threads, eval FORMS, and restore the hidden threads."
2864   (let ((config (make-symbol "config")))
2865     `(let ((,config (gnus-hidden-threads-configuration)))
2866        (unwind-protect
2867            (save-excursion
2868              ,@forms)
2869          (gnus-restore-hidden-threads-configuration ,config)))))
2870 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2871 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2872
2873 (defun gnus-data-compute-positions ()
2874   "Compute the positions of all articles."
2875   (setq gnus-newsgroup-data-reverse nil)
2876   (let ((data gnus-newsgroup-data))
2877     (save-excursion
2878       (gnus-save-hidden-threads
2879         (gnus-summary-show-all-threads)
2880         (goto-char (point-min))
2881         (while data
2882           (while (get-text-property (point) 'gnus-intangible)
2883             (forward-line 1))
2884           (gnus-data-set-pos (car data) (+ (point) 3))
2885           (setq data (cdr data))
2886           (forward-line 1))))))
2887
2888 (defun gnus-hidden-threads-configuration ()
2889   "Return the current hidden threads configuration."
2890   (save-excursion
2891     (let (config)
2892       (goto-char (point-min))
2893       (while (search-forward "\r" nil t)
2894         (push (1- (point)) config))
2895       config)))
2896
2897 (defun gnus-restore-hidden-threads-configuration (config)
2898   "Restore hidden threads configuration from CONFIG."
2899   (save-excursion
2900     (let (point buffer-read-only)
2901       (while (setq point (pop config))
2902         (when (and (< point (point-max))
2903                    (goto-char point)
2904                    (eq (char-after) ?\n))
2905           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2906
2907 ;; Various summary mode internalish functions.
2908
2909 (defun gnus-mouse-pick-article (e)
2910   (interactive "e")
2911   (mouse-set-point e)
2912   (gnus-summary-next-page nil t))
2913
2914 (defun gnus-summary-set-display-table ()
2915   "Change the display table.
2916 Odd characters have a tendency to mess
2917 up nicely formatted displays - we make all possible glyphs
2918 display only a single character."
2919
2920   ;; We start from the standard display table, if any.
2921   (let ((table (or (copy-sequence standard-display-table)
2922                    (make-display-table)))
2923         (i 32))
2924     ;; Nix out all the control chars...
2925     (while (>= (setq i (1- i)) 0)
2926       (aset table i [??]))
2927    ;; ... but not newline and cr, of course.  (cr is necessary for the
2928     ;; selective display).
2929     (aset table ?\n nil)
2930     (aset table ?\r nil)
2931     ;; We keep TAB as well.
2932     (aset table ?\t nil)
2933     ;; We nix out any glyphs over 126 that are not set already.
2934     (let ((i 256))
2935       (while (>= (setq i (1- i)) 127)
2936         ;; Only modify if the entry is nil.
2937         (unless (aref table i)
2938           (aset table i [??]))))
2939     (setq buffer-display-table table)))
2940
2941 (defun gnus-summary-set-article-display-arrow (pos)
2942   "Update the overlay arrow to point to line at position POS."
2943   (when (and gnus-summary-display-arrow
2944              (boundp 'overlay-arrow-position)
2945              (boundp 'overlay-arrow-string))
2946     (save-excursion
2947       (goto-char pos)
2948       (beginning-of-line)
2949       (unless overlay-arrow-position
2950         (setq overlay-arrow-position (make-marker)))
2951       (setq overlay-arrow-string "=>"
2952             overlay-arrow-position (set-marker overlay-arrow-position
2953                                                (point)
2954                                                (current-buffer))))))
2955
2956 (defun gnus-summary-buffer-name (group)
2957   "Return the summary buffer name of GROUP."
2958   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2959
2960 (defun gnus-summary-setup-buffer (group)
2961   "Initialize summary buffer."
2962   (let ((buffer (gnus-summary-buffer-name group))
2963         (dead-name (concat "*Dead Summary "
2964                            (gnus-group-decoded-name group) "*")))
2965     ;; If a dead summary buffer exists, we kill it.
2966     (when (gnus-buffer-live-p dead-name)
2967       (gnus-kill-buffer dead-name))
2968     (if (get-buffer buffer)
2969         (progn
2970           (set-buffer buffer)
2971           (setq gnus-summary-buffer (current-buffer))
2972           (not gnus-newsgroup-prepared))
2973       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2974       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2975       (gnus-summary-mode group)
2976       (when gnus-carpal
2977         (gnus-carpal-setup-buffer 'summary))
2978       (unless gnus-single-article-buffer
2979         (make-local-variable 'gnus-article-buffer)
2980         (make-local-variable 'gnus-article-current)
2981         (make-local-variable 'gnus-original-article-buffer))
2982       (setq gnus-newsgroup-name group)
2983       ;; Set any local variables in the group parameters.
2984       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2985       t)))
2986
2987 (defun gnus-set-global-variables ()
2988   "Set the global equivalents of the buffer-local variables.
2989 They are set to the latest values they had.  These reflect the summary
2990 buffer that was in action when the last article was fetched."
2991   (when (eq major-mode 'gnus-summary-mode)
2992     (setq gnus-summary-buffer (current-buffer))
2993     (let ((name gnus-newsgroup-name)
2994           (marked gnus-newsgroup-marked)
2995           (spam gnus-newsgroup-spam-marked)
2996           (unread gnus-newsgroup-unreads)
2997           (headers gnus-current-headers)
2998           (data gnus-newsgroup-data)
2999           (summary gnus-summary-buffer)
3000           (article-buffer gnus-article-buffer)
3001           (original gnus-original-article-buffer)
3002           (gac gnus-article-current)
3003           (reffed gnus-reffed-article-number)
3004           (score-file gnus-current-score-file)
3005           (default-charset gnus-newsgroup-charset)
3006           vlist)
3007       (let ((locals gnus-newsgroup-variables))
3008         (while locals
3009           (if (consp (car locals))
3010               (push (eval (caar locals)) vlist)
3011             (push (eval (car locals)) vlist))
3012           (setq locals (cdr locals)))
3013         (setq vlist (nreverse vlist)))
3014       (save-excursion
3015         (set-buffer gnus-group-buffer)
3016         (setq gnus-newsgroup-name name
3017               gnus-newsgroup-marked marked
3018               gnus-newsgroup-spam-marked spam
3019               gnus-newsgroup-unreads unread
3020               gnus-current-headers headers
3021               gnus-newsgroup-data data
3022               gnus-article-current gac
3023               gnus-summary-buffer summary
3024               gnus-article-buffer article-buffer
3025               gnus-original-article-buffer original
3026               gnus-reffed-article-number reffed
3027               gnus-current-score-file score-file
3028               gnus-newsgroup-charset default-charset)
3029         (let ((locals gnus-newsgroup-variables))
3030           (while locals
3031             (if (consp (car locals))
3032                 (set (caar locals) (pop vlist))
3033               (set (car locals) (pop vlist)))
3034             (setq locals (cdr locals))))
3035         ;; The article buffer also has local variables.
3036         (when (gnus-buffer-live-p gnus-article-buffer)
3037           (set-buffer gnus-article-buffer)
3038           (setq gnus-summary-buffer summary))))))
3039
3040 (defun gnus-summary-article-unread-p (article)
3041   "Say whether ARTICLE is unread or not."
3042   (memq article gnus-newsgroup-unreads))
3043
3044 (defun gnus-summary-first-article-p (&optional article)
3045   "Return whether ARTICLE is the first article in the buffer."
3046   (if (not (setq article (or article (gnus-summary-article-number))))
3047       nil
3048     (eq article (caar gnus-newsgroup-data))))
3049
3050 (defun gnus-summary-last-article-p (&optional article)
3051   "Return whether ARTICLE is the last article in the buffer."
3052   (if (not (setq article (or article (gnus-summary-article-number))))
3053       ;; All non-existent numbers are the last article.  :-)
3054       t
3055     (not (cdr (gnus-data-find-list article)))))
3056
3057 (defun gnus-make-thread-indent-array ()
3058   (let ((n 200))
3059     (unless (and gnus-thread-indent-array
3060                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3061       (setq gnus-thread-indent-array (make-vector 201 "")
3062             gnus-thread-indent-array-level gnus-thread-indent-level)
3063       (while (>= n 0)
3064         (aset gnus-thread-indent-array n
3065               (make-string (* n gnus-thread-indent-level) ? ))
3066         (setq n (1- n))))))
3067
3068 (defun gnus-update-summary-mark-positions ()
3069   "Compute where the summary marks are to go."
3070   (save-excursion
3071     (when (gnus-buffer-exists-p gnus-summary-buffer)
3072       (set-buffer gnus-summary-buffer))
3073     (let ((gnus-replied-mark 129)
3074           (gnus-score-below-mark 130)
3075           (gnus-score-over-mark 130)
3076           (gnus-download-mark 131)
3077           (spec gnus-summary-line-format-spec)
3078           gnus-visual pos)
3079       (save-excursion
3080         (gnus-set-work-buffer)
3081         (let ((gnus-summary-line-format-spec spec)
3082               (gnus-newsgroup-downloadable '((0 . t))))
3083           (gnus-summary-insert-line
3084            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3085            0 nil 128 t nil "" nil 1)
3086           (goto-char (point-min))
3087           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3088                                              (- (point) (point-min) 1)))))
3089           (goto-char (point-min))
3090           (push (cons 'replied (and (search-forward "\201" nil t)
3091                                     (- (point) (point-min) 1)))
3092                 pos)
3093           (goto-char (point-min))
3094           (push (cons 'score (and (search-forward "\202" nil t)
3095                                   (- (point) (point-min) 1)))
3096                 pos)
3097           (goto-char (point-min))
3098           (push (cons 'download
3099                       (and (search-forward "\203" nil t)
3100                            (- (point) (point-min) 1)))
3101                 pos)))
3102       (setq gnus-summary-mark-positions pos))))
3103
3104 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3105   "Insert a dummy root in the summary buffer."
3106   (beginning-of-line)
3107   (gnus-add-text-properties
3108    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3109    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3110
3111 (defun gnus-summary-extract-address-component (from)
3112   (or (car (funcall gnus-extract-address-components from))
3113       from))
3114
3115 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3116   (let ((mail-parse-charset gnus-newsgroup-charset)
3117         ; Is it really necessary to do this next part for each summary line?
3118         ; Luckily, doesn't seem to slow things down much.
3119         (mail-parse-ignored-charsets
3120          (save-excursion (set-buffer gnus-summary-buffer)
3121                          gnus-newsgroup-ignored-charsets)))
3122     (or
3123      (and gnus-ignored-from-addresses
3124           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3125           (let ((extra-headers (mail-header-extra header))
3126                 to
3127                 newsgroups)
3128             (cond
3129              ((setq to (cdr (assq 'To extra-headers)))
3130               (concat "-> "
3131                       (inline
3132                         (gnus-summary-extract-address-component
3133                          (funcall gnus-decode-encoded-word-function to)))))
3134              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3135               (concat "=> " newsgroups)))))
3136      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3137
3138 (defun gnus-summary-insert-line (gnus-tmp-header
3139                                  gnus-tmp-level gnus-tmp-current
3140                                  gnus-tmp-unread gnus-tmp-replied
3141                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3142                                  &optional gnus-tmp-dummy gnus-tmp-score
3143                                  gnus-tmp-process)
3144   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3145          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3146          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3147          (gnus-tmp-score-char
3148           (if (or (null gnus-summary-default-score)
3149                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3150                       gnus-summary-zcore-fuzz))
3151               ?                         ;Whitespace
3152             (if (< gnus-tmp-score gnus-summary-default-score)
3153                 gnus-score-below-mark gnus-score-over-mark)))
3154          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3155          (gnus-tmp-replied
3156           (cond (gnus-tmp-process gnus-process-mark)
3157                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3158                  gnus-cached-mark)
3159                 (gnus-tmp-replied gnus-replied-mark)
3160                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3161                  gnus-forwarded-mark)
3162                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3163                  gnus-saved-mark)
3164                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3165                  gnus-recent-mark)
3166                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3167                  gnus-unseen-mark)
3168                 (t gnus-no-mark)))
3169          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3170          (gnus-tmp-name
3171           (cond
3172            ((string-match "<[^>]+> *$" gnus-tmp-from)
3173             (let ((beg (match-beginning 0)))
3174               (or (and (string-match "^\".+\"" gnus-tmp-from)
3175                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3176                   (substring gnus-tmp-from 0 beg))))
3177            ((string-match "(.+)" gnus-tmp-from)
3178             (substring gnus-tmp-from
3179                        (1+ (match-beginning 0)) (1- (match-end 0))))
3180            (t gnus-tmp-from)))
3181          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3182          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3183          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3184          (buffer-read-only nil))
3185     (when (string= gnus-tmp-name "")
3186       (setq gnus-tmp-name gnus-tmp-from))
3187     (unless (numberp gnus-tmp-lines)
3188       (setq gnus-tmp-lines -1))
3189     (if (= gnus-tmp-lines -1)
3190         (setq gnus-tmp-lines "?")
3191       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3192     (gnus-put-text-property
3193      (point)
3194      (progn (eval gnus-summary-line-format-spec) (point))
3195      'gnus-number gnus-tmp-number)
3196     (when (gnus-visual-p 'summary-highlight 'highlight)
3197       (forward-line -1)
3198       (gnus-run-hooks 'gnus-summary-update-hook)
3199       (forward-line 1))))
3200
3201 (defun gnus-summary-update-line (&optional dont-update)
3202   "Update summary line after change."
3203   (when (and gnus-summary-default-score
3204              (not gnus-summary-inhibit-highlight))
3205     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3206            (article (gnus-summary-article-number))
3207            (score (gnus-summary-article-score article)))
3208       (unless dont-update
3209         (if (and gnus-summary-mark-below
3210                  (< (gnus-summary-article-score)
3211                     gnus-summary-mark-below))
3212             ;; This article has a low score, so we mark it as read.
3213             (when (memq article gnus-newsgroup-unreads)
3214               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3215           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3216             ;; This article was previously marked as read on account
3217             ;; of a low score, but now it has risen, so we mark it as
3218             ;; unread.
3219             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3220         (gnus-summary-update-mark
3221          (if (or (null gnus-summary-default-score)
3222                  (<= (abs (- score gnus-summary-default-score))
3223                      gnus-summary-zcore-fuzz))
3224              ?                          ;Whitespace
3225            (if (< score gnus-summary-default-score)
3226                gnus-score-below-mark gnus-score-over-mark))
3227          'score))
3228       ;; Do visual highlighting.
3229       (when (gnus-visual-p 'summary-highlight 'highlight)
3230         (gnus-run-hooks 'gnus-summary-update-hook)))))
3231
3232 (defvar gnus-tmp-new-adopts nil)
3233
3234 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3235   "Return the number of articles in THREAD.
3236 This may be 0 in some cases -- if none of the articles in
3237 the thread are to be displayed."
3238   (let* ((number
3239          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3240           (cond
3241            ((not (listp thread))
3242             1)
3243            ((and (consp thread) (cdr thread))
3244             (apply
3245              '+ 1 (mapcar
3246                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3247            ((null thread)
3248             1)
3249            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3250             1)
3251            (t 0))))
3252     (when (and level (zerop level) gnus-tmp-new-adopts)
3253       (incf number
3254             (apply '+ (mapcar
3255                        'gnus-summary-number-of-articles-in-thread
3256                        gnus-tmp-new-adopts))))
3257     (if char
3258         (if (> number 1) gnus-not-empty-thread-mark
3259           gnus-empty-thread-mark)
3260       number)))
3261
3262 (defsubst gnus-summary-line-message-size (head)
3263   "Return pretty-printed version of message size.
3264 This function is intended to be used in
3265 `gnus-summary-line-format-alist', which see."
3266   (let ((c (or (mail-header-chars head) -1)))
3267     (cond ((< c 0) "n/a")               ; chars not available
3268           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3269           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3270           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3271           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3272
3273
3274 (defun gnus-summary-set-local-parameters (group)
3275   "Go through the local params of GROUP and set all variable specs in that list."
3276   (let ((params (gnus-group-find-parameter group))
3277         (vars '(quit-config))           ; Ignore quit-config.
3278         elem)
3279     (while params
3280       (setq elem (car params)
3281             params (cdr params))
3282       (and (consp elem)                 ; Has to be a cons.
3283            (consp (cdr elem))           ; The cdr has to be a list.
3284            (symbolp (car elem))         ; Has to be a symbol in there.
3285            (not (memq (car elem) vars))
3286            (ignore-errors               ; So we set it.
3287              (push (car elem) vars)
3288              (make-local-variable (car elem))
3289              (set (car elem) (eval (nth 1 elem))))))))
3290
3291 (defun gnus-summary-read-group (group &optional show-all no-article
3292                                       kill-buffer no-display backward
3293                                       select-articles)
3294   "Start reading news in newsgroup GROUP.
3295 If SHOW-ALL is non-nil, already read articles are also listed.
3296 If NO-ARTICLE is non-nil, no article is selected initially.
3297 If NO-DISPLAY, don't generate a summary buffer."
3298   (let (result)
3299     (while (and group
3300                 (null (setq result
3301                             (let ((gnus-auto-select-next nil))
3302                               (or (gnus-summary-read-group-1
3303                                    group show-all no-article
3304                                    kill-buffer no-display
3305                                    select-articles)
3306                                   (setq show-all nil
3307                                         select-articles nil)))))
3308                 (eq gnus-auto-select-next 'quietly))
3309       (set-buffer gnus-group-buffer)
3310       ;; The entry function called above goes to the next
3311       ;; group automatically, so we go two groups back
3312       ;; if we are searching for the previous group.
3313       (when backward
3314         (gnus-group-prev-unread-group 2))
3315       (if (not (equal group (gnus-group-group-name)))
3316           (setq group (gnus-group-group-name))
3317         (setq group nil)))
3318     result))
3319
3320 (defun gnus-summary-read-group-1 (group show-all no-article
3321                                         kill-buffer no-display
3322                                         &optional select-articles)
3323   ;; Killed foreign groups can't be entered.
3324   ;;  (when (and (not (gnus-group-native-p group))
3325   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3326   ;;    (error "Dead non-native groups can't be entered"))
3327   (gnus-message 5 "Retrieving newsgroup: %s..."
3328                 (gnus-group-decoded-name group))
3329   (let* ((new-group (gnus-summary-setup-buffer group))
3330          (quit-config (gnus-group-quit-config group))
3331          (did-select (and new-group (gnus-select-newsgroup
3332                                      group show-all select-articles))))
3333     (cond
3334      ;; This summary buffer exists already, so we just select it.
3335      ((not new-group)
3336       (gnus-set-global-variables)
3337       (when kill-buffer
3338         (gnus-kill-or-deaden-summary kill-buffer))
3339       (gnus-configure-windows 'summary 'force)
3340       (gnus-set-mode-line 'summary)
3341       (gnus-summary-position-point)
3342       (message "")
3343       t)
3344      ;; We couldn't select this group.
3345      ((null did-select)
3346       (when (and (eq major-mode 'gnus-summary-mode)
3347                  (not (equal (current-buffer) kill-buffer)))
3348         (kill-buffer (current-buffer))
3349         (if (not quit-config)
3350             (progn
3351               ;; Update the info -- marks might need to be removed,
3352               ;; for instance.
3353               (gnus-summary-update-info)
3354               (set-buffer gnus-group-buffer)
3355               (gnus-group-jump-to-group group)
3356               (gnus-group-next-unread-group 1))
3357           (gnus-handle-ephemeral-exit quit-config)))
3358       (let ((grpinfo (gnus-get-info group)))
3359         (if (null (gnus-info-read grpinfo))
3360             (gnus-message 3 "Group %s contains no messages"
3361                           (gnus-group-decoded-name group))
3362           (gnus-message 3 "Can't select group")))
3363       nil)
3364      ;; The user did a `C-g' while prompting for number of articles,
3365      ;; so we exit this group.
3366      ((eq did-select 'quit)
3367       (and (eq major-mode 'gnus-summary-mode)
3368            (not (equal (current-buffer) kill-buffer))
3369            (kill-buffer (current-buffer)))
3370       (when kill-buffer
3371         (gnus-kill-or-deaden-summary kill-buffer))
3372       (if (not quit-config)
3373           (progn
3374             (set-buffer gnus-group-buffer)
3375             (gnus-group-jump-to-group group)
3376             (gnus-group-next-unread-group 1)
3377             (gnus-configure-windows 'group 'force))
3378         (gnus-handle-ephemeral-exit quit-config))
3379       ;; Finally signal the quit.
3380       (signal 'quit nil))
3381      ;; The group was successfully selected.
3382      (t
3383       (gnus-set-global-variables)
3384       ;; Save the active value in effect when the group was entered.
3385       (setq gnus-newsgroup-active
3386             (gnus-copy-sequence
3387              (gnus-active gnus-newsgroup-name)))
3388       ;; You can change the summary buffer in some way with this hook.
3389       (gnus-run-hooks 'gnus-select-group-hook)
3390       (gnus-update-format-specifications
3391        nil 'summary 'summary-mode 'summary-dummy)
3392       (gnus-update-summary-mark-positions)
3393       ;; Do score processing.
3394       (when gnus-use-scoring
3395         (gnus-possibly-score-headers))
3396       ;; Check whether to fill in the gaps in the threads.
3397       (when gnus-build-sparse-threads
3398         (gnus-build-sparse-threads))
3399       ;; Find the initial limit.
3400       (if gnus-show-threads
3401           (if show-all
3402               (let ((gnus-newsgroup-dormant nil))
3403                 (gnus-summary-initial-limit show-all))
3404             (gnus-summary-initial-limit show-all))
3405         ;; When untreaded, all articles are always shown.
3406         (setq gnus-newsgroup-limit
3407               (mapcar
3408                (lambda (header) (mail-header-number header))
3409                gnus-newsgroup-headers)))
3410       ;; Generate the summary buffer.
3411       (unless no-display
3412         (gnus-summary-prepare))
3413       (when gnus-use-trees
3414         (gnus-tree-open group)
3415         (setq gnus-summary-highlight-line-function
3416               'gnus-tree-highlight-article))
3417       ;; If the summary buffer is empty, but there are some low-scored
3418       ;; articles or some excluded dormants, we include these in the
3419       ;; buffer.
3420       (when (and (zerop (buffer-size))
3421                  (not no-display))
3422         (cond (gnus-newsgroup-dormant
3423                (gnus-summary-limit-include-dormant))
3424               ((and gnus-newsgroup-scored show-all)
3425                (gnus-summary-limit-include-expunged t))))
3426       ;; Function `gnus-apply-kill-file' must be called in this hook.
3427       (gnus-run-hooks 'gnus-apply-kill-hook)
3428       (if (and (zerop (buffer-size))
3429                (not no-display))
3430           (progn
3431             ;; This newsgroup is empty.
3432             (gnus-summary-catchup-and-exit nil t)
3433             (gnus-message 6 "No unread news")
3434             (when kill-buffer
3435               (gnus-kill-or-deaden-summary kill-buffer))
3436             ;; Return nil from this function.
3437             nil)
3438         ;; Hide conversation thread subtrees.  We cannot do this in
3439         ;; gnus-summary-prepare-hook since kill processing may not
3440         ;; work with hidden articles.
3441         (gnus-summary-maybe-hide-threads)
3442         (when kill-buffer
3443           (gnus-kill-or-deaden-summary kill-buffer))
3444         (gnus-summary-auto-select-subject)
3445         ;; Show first unread article if requested.
3446         (if (and (not no-article)
3447                  (not no-display)
3448                  gnus-newsgroup-unreads
3449                  gnus-auto-select-first)
3450             (progn
3451               (gnus-configure-windows 'summary)
3452               (let ((art (gnus-summary-article-number)))
3453                 (unless (or (memq art gnus-newsgroup-undownloaded)
3454                             (memq art gnus-newsgroup-downloadable))
3455                   (gnus-summary-goto-article art))))
3456           ;; Don't select any articles.
3457           (gnus-summary-position-point)
3458           (gnus-configure-windows 'summary 'force)
3459           (gnus-set-mode-line 'summary))
3460         (when (get-buffer-window gnus-group-buffer t)
3461           ;; Gotta use windows, because recenter does weird stuff if
3462           ;; the current buffer ain't the displayed window.
3463           (let ((owin (selected-window)))
3464             (select-window (get-buffer-window gnus-group-buffer t))
3465             (when (gnus-group-goto-group group)
3466               (recenter))
3467             (select-window owin)))
3468         ;; Mark this buffer as "prepared".
3469         (setq gnus-newsgroup-prepared t)
3470         (gnus-run-hooks 'gnus-summary-prepared-hook)
3471         t)))))
3472
3473 (defun gnus-summary-auto-select-subject ()
3474   "Select the subject line on initial group entry."
3475   (goto-char (point-min))
3476   (cond
3477    ((eq gnus-auto-select-subject 'best)
3478     (gnus-summary-best-unread-subject))
3479    ((eq gnus-auto-select-subject 'unread)
3480     (gnus-summary-first-unread-subject))
3481    ((eq gnus-auto-select-subject 'unseen)
3482     (gnus-summary-first-unseen-subject))
3483    ((eq gnus-auto-select-subject 'unseen-or-unread)
3484     (gnus-summary-first-unseen-or-unread-subject))
3485    ((eq gnus-auto-select-subject 'first)
3486     ;; Do nothing.
3487     )
3488    ((gnus-functionp gnus-auto-select-subject)
3489     (funcall gnus-auto-select-subject))))
3490
3491 (defun gnus-summary-prepare ()
3492   "Generate the summary buffer."
3493   (interactive)
3494   (let ((buffer-read-only nil))
3495     (erase-buffer)
3496     (setq gnus-newsgroup-data nil
3497           gnus-newsgroup-data-reverse nil)
3498     (gnus-run-hooks 'gnus-summary-generate-hook)
3499     ;; Generate the buffer, either with threads or without.
3500     (when gnus-newsgroup-headers
3501       (gnus-summary-prepare-threads
3502        (if gnus-show-threads
3503            (gnus-sort-gathered-threads
3504             (funcall gnus-summary-thread-gathering-function
3505                      (gnus-sort-threads
3506                       (gnus-cut-threads (gnus-make-threads)))))
3507          ;; Unthreaded display.
3508          (gnus-sort-articles gnus-newsgroup-headers))))
3509     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3510     ;; Call hooks for modifying summary buffer.
3511     (goto-char (point-min))
3512     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3513
3514 (defsubst gnus-general-simplify-subject (subject)
3515   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3516   (setq subject
3517         (cond
3518          ;; Truncate the subject.
3519          (gnus-simplify-subject-functions
3520           (gnus-map-function gnus-simplify-subject-functions subject))
3521          ((numberp gnus-summary-gather-subject-limit)
3522           (setq subject (gnus-simplify-subject-re subject))
3523           (if (> (length subject) gnus-summary-gather-subject-limit)
3524               (substring subject 0 gnus-summary-gather-subject-limit)
3525             subject))
3526          ;; Fuzzily simplify it.
3527          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3528           (gnus-simplify-subject-fuzzy subject))
3529          ;; Just remove the leading "Re:".
3530          (t
3531           (gnus-simplify-subject-re subject))))
3532
3533   (if (and gnus-summary-gather-exclude-subject
3534            (string-match gnus-summary-gather-exclude-subject subject))
3535       nil                         ; This article shouldn't be gathered
3536     subject))
3537
3538 (defun gnus-summary-simplify-subject-query ()
3539   "Query where the respool algorithm would put this article."
3540   (interactive)
3541   (gnus-summary-select-article)
3542   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3543
3544 (defun gnus-gather-threads-by-subject (threads)
3545   "Gather threads by looking at Subject headers."
3546   (if (not gnus-summary-make-false-root)
3547       threads
3548     (let ((hashtb (gnus-make-hashtable 1024))
3549           (prev threads)
3550           (result threads)
3551           subject hthread whole-subject)
3552       (while threads
3553         (setq subject (gnus-general-simplify-subject
3554                        (setq whole-subject (mail-header-subject
3555                                             (caar threads)))))
3556         (when subject
3557           (if (setq hthread (gnus-gethash subject hashtb))
3558               (progn
3559                 ;; We enter a dummy root into the thread, if we
3560                 ;; haven't done that already.
3561                 (unless (stringp (caar hthread))
3562                   (setcar hthread (list whole-subject (car hthread))))
3563                 ;; We add this new gathered thread to this gathered
3564                 ;; thread.
3565                 (setcdr (car hthread)
3566                         (nconc (cdar hthread) (list (car threads))))
3567                 ;; Remove it from the list of threads.
3568                 (setcdr prev (cdr threads))
3569                 (setq threads prev))
3570             ;; Enter this thread into the hash table.
3571             (gnus-sethash subject threads hashtb)))
3572         (setq prev threads)
3573         (setq threads (cdr threads)))
3574       result)))
3575
3576 (defun gnus-gather-threads-by-references (threads)
3577   "Gather threads by looking at References headers."
3578   (let ((idhashtb (gnus-make-hashtable 1024))
3579         (thhashtb (gnus-make-hashtable 1024))
3580         (prev threads)
3581         (result threads)
3582         ids references id gthread gid entered ref)
3583     (while threads
3584       (when (setq references (mail-header-references (caar threads)))
3585         (setq id (mail-header-id (caar threads))
3586               ids (inline (gnus-split-references references))
3587               entered nil)
3588         (while (setq ref (pop ids))
3589           (setq ids (delete ref ids))
3590           (if (not (setq gid (gnus-gethash ref idhashtb)))
3591               (progn
3592                 (gnus-sethash ref id idhashtb)
3593                 (gnus-sethash id threads thhashtb))
3594             (setq gthread (gnus-gethash gid thhashtb))
3595             (unless entered
3596               ;; We enter a dummy root into the thread, if we
3597               ;; haven't done that already.
3598               (unless (stringp (caar gthread))
3599                 (setcar gthread (list (mail-header-subject (caar gthread))
3600                                       (car gthread))))
3601               ;; We add this new gathered thread to this gathered
3602               ;; thread.
3603               (setcdr (car gthread)
3604                       (nconc (cdar gthread) (list (car threads)))))
3605             ;; Add it into the thread hash table.
3606             (gnus-sethash id gthread thhashtb)
3607             (setq entered t)
3608             ;; Remove it from the list of threads.
3609             (setcdr prev (cdr threads))
3610             (setq threads prev))))
3611       (setq prev threads)
3612       (setq threads (cdr threads)))
3613     result))
3614
3615 (defun gnus-sort-gathered-threads (threads)
3616   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3617   (let ((result threads))
3618     (while threads
3619       (when (stringp (caar threads))
3620         (setcdr (car threads)
3621                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3622       (setq threads (cdr threads)))
3623     result))
3624
3625 (defun gnus-thread-loop-p (root thread)
3626   "Say whether ROOT is in THREAD."
3627   (let ((stack (list thread))
3628         (infloop 0)
3629         th)
3630     (while (setq thread (pop stack))
3631       (setq th (cdr thread))
3632       (while (and th
3633                   (not (eq (caar th) root)))
3634         (pop th))
3635       (if th
3636           ;; We have found a loop.
3637           (let (ref-dep)
3638             (setcdr thread (delq (car th) (cdr thread)))
3639             (if (boundp (setq ref-dep (intern "none"
3640                                               gnus-newsgroup-dependencies)))
3641                 (setcdr (symbol-value ref-dep)
3642                         (nconc (cdr (symbol-value ref-dep))
3643                                (list (car th))))
3644               (set ref-dep (list nil (car th))))
3645             (setq infloop 1
3646                   stack nil))
3647         ;; Push all the subthreads onto the stack.
3648         (push (cdr thread) stack)))
3649     infloop))
3650
3651 (defun gnus-make-threads ()
3652   "Go through the dependency hashtb and find the roots.  Return all threads."
3653   (let (threads)
3654     (while (catch 'infloop
3655              (mapatoms
3656               (lambda (refs)
3657                 ;; Deal with self-referencing References loops.
3658                 (when (and (car (symbol-value refs))
3659                            (not (zerop
3660                                  (apply
3661                                   '+
3662                                   (mapcar
3663                                    (lambda (thread)
3664                                      (gnus-thread-loop-p
3665                                       (car (symbol-value refs)) thread))
3666                                    (cdr (symbol-value refs)))))))
3667                   (setq threads nil)
3668                   (throw 'infloop t))
3669                 (unless (car (symbol-value refs))
3670                   ;; These threads do not refer back to any other
3671                   ;; articles, so they're roots.
3672                   (setq threads (append (cdr (symbol-value refs)) threads))))
3673               gnus-newsgroup-dependencies)))
3674     threads))
3675
3676 ;; Build the thread tree.
3677 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3678   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3679
3680 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3681 if it was already present.
3682
3683 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3684 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3685 Message-IDs will be renamed to a unique Message-ID before being
3686 entered.
3687
3688 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3689   (let* ((id (mail-header-id header))
3690          (id-dep (and id (intern id dependencies)))
3691          parent-id ref ref-dep ref-header replaced)
3692     ;; Enter this `header' in the `dependencies' table.
3693     (cond
3694      ((not id-dep)
3695       (setq header nil))
3696      ;; The first two cases do the normal part: enter a new `header'
3697      ;; in the `dependencies' table.
3698      ((not (boundp id-dep))
3699       (set id-dep (list header)))
3700      ((null (car (symbol-value id-dep)))
3701       (setcar (symbol-value id-dep) header))
3702
3703      ;; From here the `header' was already present in the
3704      ;; `dependencies' table.
3705      (force-new
3706       ;; Overrides an existing entry;
3707       ;; just set the header part of the entry.
3708       (setcar (symbol-value id-dep) header)
3709       (setq replaced t))
3710
3711      ;; Renames the existing `header' to a unique Message-ID.
3712      ((not gnus-summary-ignore-duplicates)
3713       ;; An article with this Message-ID has already been seen.
3714       ;; We rename the Message-ID.
3715       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3716            (list header))
3717       (mail-header-set-id header id))
3718
3719      ;; The last case ignores an existing entry, except it adds any
3720      ;; additional Xrefs (in case the two articles came from different
3721      ;; servers.
3722      ;; Also sets `header' to `nil' meaning that the `dependencies'
3723      ;; table was *not* modified.
3724      (t
3725       (mail-header-set-xref
3726        (car (symbol-value id-dep))
3727        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3728                    "")
3729                (or (mail-header-xref header) "")))
3730       (setq header nil)))
3731
3732     (when (and header (not replaced))
3733       ;; First check that we are not creating a References loop.
3734       (setq parent-id (gnus-parent-id (mail-header-references header)))
3735       (setq ref parent-id)
3736       (while (and ref
3737                   (setq ref-dep (intern-soft ref dependencies))
3738                   (boundp ref-dep)
3739                   (setq ref-header (car (symbol-value ref-dep))))
3740         (if (string= id ref)
3741             ;; Yuk!  This is a reference loop.  Make the article be a
3742             ;; root article.
3743             (progn
3744               (mail-header-set-references (car (symbol-value id-dep)) "none")
3745               (setq ref nil)
3746               (setq parent-id nil))
3747           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3748       (setq ref-dep (intern (or parent-id "none") dependencies))
3749       (if (boundp ref-dep)
3750           (setcdr (symbol-value ref-dep)
3751                   (nconc (cdr (symbol-value ref-dep))
3752                          (list (symbol-value id-dep))))
3753         (set ref-dep (list nil (symbol-value id-dep)))))
3754     header))
3755
3756 (defun gnus-extract-message-id-from-in-reply-to (string)
3757   (if (string-match "<[^>]+>" string)
3758       (substring string (match-beginning 0) (match-end 0))
3759     nil))
3760
3761 (defun gnus-build-sparse-threads ()
3762   (let ((headers gnus-newsgroup-headers)
3763         (mail-parse-charset gnus-newsgroup-charset)
3764         (gnus-summary-ignore-duplicates t)
3765         header references generation relations
3766         subject child end new-child date)
3767     ;; First we create an alist of generations/relations, where
3768     ;; generations is how much we trust the relation, and the relation
3769     ;; is parent/child.
3770     (gnus-message 7 "Making sparse threads...")
3771     (save-excursion
3772       (nnheader-set-temp-buffer " *gnus sparse threads*")
3773       (while (setq header (pop headers))
3774         (when (and (setq references (mail-header-references header))
3775                    (not (string= references "")))
3776           (insert references)
3777           (setq child (mail-header-id header)
3778                 subject (mail-header-subject header)
3779                 date (mail-header-date header)
3780                 generation 0)
3781           (while (search-backward ">" nil t)
3782             (setq end (1+ (point)))
3783             (when (search-backward "<" nil t)
3784               (setq new-child (buffer-substring (point) end))
3785               (push (list (incf generation)
3786                           child (setq child new-child)
3787                           subject date)
3788                     relations)))
3789           (when child
3790             (push (list (1+ generation) child nil subject) relations))
3791           (erase-buffer)))
3792       (kill-buffer (current-buffer)))
3793     ;; Sort over trustworthiness.
3794     (mapcar
3795      (lambda (relation)
3796        (when (gnus-dependencies-add-header
3797               (make-full-mail-header
3798                gnus-reffed-article-number
3799                (nth 3 relation) "" (or (nth 4 relation) "")
3800                (nth 1 relation)
3801                (or (nth 2 relation) "") 0 0 "")
3802               gnus-newsgroup-dependencies nil)
3803          (push gnus-reffed-article-number gnus-newsgroup-limit)
3804          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3805          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3806                gnus-newsgroup-reads)
3807          (decf gnus-reffed-article-number)))
3808      (sort relations 'car-less-than-car))
3809     (gnus-message 7 "Making sparse threads...done")))
3810
3811 (defun gnus-build-old-threads ()
3812   ;; Look at all the articles that refer back to old articles, and
3813   ;; fetch the headers for the articles that aren't there.  This will
3814   ;; build complete threads - if the roots haven't been expired by the
3815   ;; server, that is.
3816   (let ((mail-parse-charset gnus-newsgroup-charset)
3817         id heads)
3818     (mapatoms
3819      (lambda (refs)
3820        (when (not (car (symbol-value refs)))
3821          (setq heads (cdr (symbol-value refs)))
3822          (while heads
3823            (if (memq (mail-header-number (caar heads))
3824                      gnus-newsgroup-dormant)
3825                (setq heads (cdr heads))
3826              (setq id (symbol-name refs))
3827              (while (and (setq id (gnus-build-get-header id))
3828                          (not (car (gnus-id-to-thread id)))))
3829              (setq heads nil)))))
3830      gnus-newsgroup-dependencies)))
3831
3832 ;; This function has to be called with point after the article number
3833 ;; on the beginning of the line.
3834 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3835   (let ((eol (gnus-point-at-eol))
3836         (buffer (current-buffer))
3837         header references in-reply-to)
3838
3839     ;; overview: [num subject from date id refs chars lines misc]
3840     (unwind-protect
3841         (progn
3842           (narrow-to-region (point) eol)
3843           (unless (eobp)
3844             (forward-char))
3845
3846           (setq header
3847                 (make-full-mail-header
3848                  number                 ; number
3849                  (funcall gnus-decode-encoded-word-function
3850                           (nnheader-nov-field)) ; subject
3851                  (funcall gnus-decode-encoded-word-function
3852                           (nnheader-nov-field)) ; from
3853                  (nnheader-nov-field)   ; date
3854                  (nnheader-nov-read-message-id) ; id
3855                  (setq references (nnheader-nov-field)) ; refs
3856                  (nnheader-nov-read-integer) ; chars
3857                  (nnheader-nov-read-integer) ; lines
3858                  (unless (eobp)
3859                    (if (looking-at "Xref: ")
3860                        (goto-char (match-end 0)))
3861                    (nnheader-nov-field)) ; Xref
3862                  (nnheader-nov-parse-extra)))) ; extra
3863
3864       (widen))
3865
3866     (when (and (string= references "")
3867                (setq in-reply-to (mail-header-extra header))
3868                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3869       (mail-header-set-references
3870        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3871
3872     (when gnus-alter-header-function
3873       (funcall gnus-alter-header-function header))
3874     (gnus-dependencies-add-header header dependencies force-new)))
3875
3876 (defun gnus-build-get-header (id)
3877   "Look through the buffer of NOV lines and find the header to ID.
3878 Enter this line into the dependencies hash table, and return
3879 the id of the parent article (if any)."
3880   (let ((deps gnus-newsgroup-dependencies)
3881         found header)
3882     (prog1
3883         (save-excursion
3884           (set-buffer nntp-server-buffer)
3885           (let ((case-fold-search nil))
3886             (goto-char (point-min))
3887             (while (and (not found)
3888                         (search-forward id nil t))
3889               (beginning-of-line)
3890               (setq found (looking-at
3891                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3892                                    (regexp-quote id))))
3893               (or found (beginning-of-line 2)))
3894             (when found
3895               (beginning-of-line)
3896               (and
3897                (setq header (gnus-nov-parse-line
3898                              (read (current-buffer)) deps))
3899                (gnus-parent-id (mail-header-references header))))))
3900       (when header
3901         (let ((number (mail-header-number header)))
3902           (push number gnus-newsgroup-limit)
3903           (push header gnus-newsgroup-headers)
3904           (if (memq number gnus-newsgroup-unselected)
3905               (progn
3906                 (setq gnus-newsgroup-unreads
3907                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3908                                                number))
3909                 (setq gnus-newsgroup-unselected
3910                       (delq number gnus-newsgroup-unselected)))
3911             (push number gnus-newsgroup-ancient)))))))
3912
3913 (defun gnus-build-all-threads ()
3914   "Read all the headers."
3915   (let ((gnus-summary-ignore-duplicates t)
3916         (mail-parse-charset gnus-newsgroup-charset)
3917         (dependencies gnus-newsgroup-dependencies)
3918         header article)
3919     (save-excursion
3920       (set-buffer nntp-server-buffer)
3921       (let ((case-fold-search nil))
3922         (goto-char (point-min))
3923         (while (not (eobp))
3924           (ignore-errors
3925             (setq article (read (current-buffer))
3926                   header (gnus-nov-parse-line article dependencies)))
3927           (when header
3928             (save-excursion
3929               (set-buffer gnus-summary-buffer)
3930               (push header gnus-newsgroup-headers)
3931               (if (memq (setq article (mail-header-number header))
3932                         gnus-newsgroup-unselected)
3933                   (progn
3934                     (setq gnus-newsgroup-unreads
3935                           (gnus-add-to-sorted-list
3936                            gnus-newsgroup-unreads article))
3937                     (setq gnus-newsgroup-unselected
3938                           (delq article gnus-newsgroup-unselected)))
3939                 (push article gnus-newsgroup-ancient)))
3940             (forward-line 1)))))))
3941
3942 (defun gnus-summary-update-article-line (article header)
3943   "Update the line for ARTICLE using HEADERS."
3944   (let* ((id (mail-header-id header))
3945          (thread (gnus-id-to-thread id)))
3946     (unless thread
3947       (error "Article in no thread"))
3948     ;; Update the thread.
3949     (setcar thread header)
3950     (gnus-summary-goto-subject article)
3951     (let* ((datal (gnus-data-find-list article))
3952            (data (car datal))
3953            (length (when (cdr datal)
3954                      (- (gnus-data-pos data)
3955                         (gnus-data-pos (cadr datal)))))
3956            (buffer-read-only nil)
3957            (level (gnus-summary-thread-level)))
3958       (gnus-delete-line)
3959       (gnus-summary-insert-line
3960        header level nil (gnus-article-mark article)
3961        (memq article gnus-newsgroup-replied)
3962        (memq article gnus-newsgroup-expirable)
3963        ;; Only insert the Subject string when it's different
3964        ;; from the previous Subject string.
3965        (if (and
3966             gnus-show-threads
3967             (gnus-subject-equal
3968              (condition-case ()
3969                  (mail-header-subject
3970                   (gnus-data-header
3971                    (cadr
3972                     (gnus-data-find-list
3973                      article
3974                      (gnus-data-list t)))))
3975                ;; Error on the side of excessive subjects.
3976                (error ""))
3977              (mail-header-subject header)))
3978            ""
3979          (mail-header-subject header))
3980        nil (cdr (assq article gnus-newsgroup-scored))
3981        (memq article gnus-newsgroup-processable))
3982       (when length
3983         (gnus-data-update-list
3984          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3985
3986 (defun gnus-summary-update-article (article &optional iheader)
3987   "Update ARTICLE in the summary buffer."
3988   (set-buffer gnus-summary-buffer)
3989   (let* ((header (gnus-summary-article-header article))
3990          (id (mail-header-id header))
3991          (data (gnus-data-find article))
3992          (thread (gnus-id-to-thread id))
3993          (references (mail-header-references header))
3994          (parent
3995           (gnus-id-to-thread
3996            (or (gnus-parent-id
3997                 (when (and references
3998                            (not (equal "" references)))
3999                   references))
4000                "none")))
4001          (buffer-read-only nil)
4002          (old (car thread)))
4003     (when thread
4004       (unless iheader
4005         (setcar thread nil)
4006         (when parent
4007           (delq thread parent)))
4008       (if (gnus-summary-insert-subject id header)
4009        ;; Set the (possibly) new article number in the data structure.
4010           (gnus-data-set-number data (gnus-id-to-article id))
4011         (setcar thread old)
4012         nil))))
4013
4014 (defun gnus-rebuild-thread (id &optional line)
4015   "Rebuild the thread containing ID.
4016 If LINE, insert the rebuilt thread starting on line LINE."
4017   (let ((buffer-read-only nil)
4018         old-pos current thread data)
4019     (if (not gnus-show-threads)
4020         (setq thread (list (car (gnus-id-to-thread id))))
4021       ;; Get the thread this article is part of.
4022       (setq thread (gnus-remove-thread id)))
4023     (setq old-pos (gnus-point-at-bol))
4024     (setq current (save-excursion
4025                     (and (re-search-backward "[\r\n]" nil t)
4026                          (gnus-summary-article-number))))
4027     ;; If this is a gathered thread, we have to go some re-gathering.
4028     (when (stringp (car thread))
4029       (let ((subject (car thread))
4030             roots thr)
4031         (setq thread (cdr thread))
4032         (while thread
4033           (unless (memq (setq thr (gnus-id-to-thread
4034                                    (gnus-root-id
4035                                     (mail-header-id (caar thread)))))
4036                         roots)
4037             (push thr roots))
4038           (setq thread (cdr thread)))
4039         ;; We now have all (unique) roots.
4040         (if (= (length roots) 1)
4041             ;; All the loose roots are now one solid root.
4042             (setq thread (car roots))
4043           (setq thread (cons subject (gnus-sort-threads roots))))))
4044     (let (threads)
4045       ;; We then insert this thread into the summary buffer.
4046       (when line
4047         (goto-char (point-min))
4048         (forward-line (1- line)))
4049       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4050         (if gnus-show-threads
4051             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4052           (gnus-summary-prepare-unthreaded thread))
4053         (setq data (nreverse gnus-newsgroup-data))
4054         (setq threads gnus-newsgroup-threads))
4055       ;; We splice the new data into the data structure.
4056       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4057       ;;!!! then we want to insert at the beginning of the buffer.
4058       ;;!!! That happens to be true with Gnus now, but that may
4059       ;;!!! change in the future.  Perhaps.
4060       (gnus-data-enter-list
4061        (if line nil current) data (- (point) old-pos))
4062       (setq gnus-newsgroup-threads
4063             (nconc threads gnus-newsgroup-threads))
4064       (gnus-data-compute-positions))))
4065
4066 (defun gnus-number-to-header (number)
4067   "Return the header for article NUMBER."
4068   (let ((headers gnus-newsgroup-headers))
4069     (while (and headers
4070                 (not (= number (mail-header-number (car headers)))))
4071       (pop headers))
4072     (when headers
4073       (car headers))))
4074
4075 (defun gnus-parent-headers (in-headers &optional generation)
4076   "Return the headers of the GENERATIONeth parent of HEADERS."
4077   (unless generation
4078     (setq generation 1))
4079   (let ((parent t)
4080         (headers in-headers)
4081         references)
4082     (while (and parent
4083                 (not (zerop generation))
4084                 (setq references (mail-header-references headers)))
4085       (setq headers (if (and references
4086                              (setq parent (gnus-parent-id references)))
4087                         (car (gnus-id-to-thread parent))
4088                       nil))
4089       (decf generation))
4090     (and (not (eq headers in-headers))
4091          headers)))
4092
4093 (defun gnus-id-to-thread (id)
4094   "Return the (sub-)thread where ID appears."
4095   (gnus-gethash id gnus-newsgroup-dependencies))
4096
4097 (defun gnus-id-to-article (id)
4098   "Return the article number of ID."
4099   (let ((thread (gnus-id-to-thread id)))
4100     (when (and thread
4101                (car thread))
4102       (mail-header-number (car thread)))))
4103
4104 (defun gnus-id-to-header (id)
4105   "Return the article headers of ID."
4106   (car (gnus-id-to-thread id)))
4107
4108 (defun gnus-article-displayed-root-p (article)
4109   "Say whether ARTICLE is a root(ish) article."
4110   (let ((level (gnus-summary-thread-level article))
4111         (refs (mail-header-references  (gnus-summary-article-header article)))
4112         particle)
4113     (cond
4114      ((null level) nil)
4115      ((zerop level) t)
4116      ((null refs) t)
4117      ((null (gnus-parent-id refs)) t)
4118      ((and (= 1 level)
4119            (null (setq particle (gnus-id-to-article
4120                                  (gnus-parent-id refs))))
4121            (null (gnus-summary-thread-level particle)))))))
4122
4123 (defun gnus-root-id (id)
4124   "Return the id of the root of the thread where ID appears."
4125   (let (last-id prev)
4126     (while (and id (setq prev (car (gnus-id-to-thread id))))
4127       (setq last-id id
4128             id (gnus-parent-id (mail-header-references prev))))
4129     last-id))
4130
4131 (defun gnus-articles-in-thread (thread)
4132   "Return the list of articles in THREAD."
4133   (cons (mail-header-number (car thread))
4134         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4135
4136 (defun gnus-remove-thread (id &optional dont-remove)
4137   "Remove the thread that has ID in it."
4138   (let (headers thread last-id)
4139     ;; First go up in this thread until we find the root.
4140     (setq last-id (gnus-root-id id)
4141           headers (message-flatten-list (gnus-id-to-thread last-id)))
4142     ;; We have now found the real root of this thread.  It might have
4143     ;; been gathered into some loose thread, so we have to search
4144     ;; through the threads to find the thread we wanted.
4145     (let ((threads gnus-newsgroup-threads)
4146           sub)
4147       (while threads
4148         (setq sub (car threads))
4149         (if (stringp (car sub))
4150             ;; This is a gathered thread, so we look at the roots
4151             ;; below it to find whether this article is in this
4152             ;; gathered root.
4153             (progn
4154               (setq sub (cdr sub))
4155               (while sub
4156                 (when (member (caar sub) headers)
4157                   (setq thread (car threads)
4158                         threads nil
4159                         sub nil))
4160                 (setq sub (cdr sub))))
4161           ;; It's an ordinary thread, so we check it.
4162           (when (eq (car sub) (car headers))
4163             (setq thread sub
4164                   threads nil)))
4165         (setq threads (cdr threads)))
4166       ;; If this article is in no thread, then it's a root.
4167       (if thread
4168           (unless dont-remove
4169             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4170         (setq thread (gnus-id-to-thread last-id)))
4171       (when thread
4172         (prog1
4173             thread                      ; We return this thread.
4174           (unless dont-remove
4175             (if (stringp (car thread))
4176                 (progn
4177                   ;; If we use dummy roots, then we have to remove the
4178                   ;; dummy root as well.
4179                   (when (eq gnus-summary-make-false-root 'dummy)
4180                     ;; We go to the dummy root by going to
4181                     ;; the first sub-"thread", and then one line up.
4182                     (gnus-summary-goto-article
4183                      (mail-header-number (caadr thread)))
4184                     (forward-line -1)
4185                     (gnus-delete-line)
4186                     (gnus-data-compute-positions))
4187                   (setq thread (cdr thread))
4188                   (while thread
4189                     (gnus-remove-thread-1 (car thread))
4190                     (setq thread (cdr thread))))
4191               (gnus-remove-thread-1 thread))))))))
4192
4193 (defun gnus-remove-thread-1 (thread)
4194   "Remove the thread THREAD recursively."
4195   (let ((number (mail-header-number (pop thread)))
4196         d)
4197     (setq thread (reverse thread))
4198     (while thread
4199       (gnus-remove-thread-1 (pop thread)))
4200     (when (setq d (gnus-data-find number))
4201       (goto-char (gnus-data-pos d))
4202       (gnus-summary-show-thread)
4203       (gnus-data-remove
4204        number
4205        (- (gnus-point-at-bol)
4206           (prog1
4207               (1+ (gnus-point-at-eol))
4208             (gnus-delete-line)))))))
4209
4210 (defun gnus-sort-threads-1 (threads func)
4211   (sort (mapcar (lambda (thread)
4212                   (cons (car thread)
4213                         (and (cdr thread)
4214                              (gnus-sort-threads-1 (cdr thread) func))))
4215                 threads) func))
4216
4217 (defun gnus-sort-threads (threads)
4218   "Sort THREADS."
4219   (if (not gnus-thread-sort-functions)
4220       threads
4221     (gnus-message 8 "Sorting threads...")
4222     (prog1
4223         (gnus-sort-threads-1
4224          threads
4225          (gnus-make-sort-function gnus-thread-sort-functions))
4226       (gnus-message 8 "Sorting threads...done"))))
4227
4228 (defun gnus-sort-articles (articles)
4229   "Sort ARTICLES."
4230   (when gnus-article-sort-functions
4231     (gnus-message 7 "Sorting articles...")
4232     (prog1
4233         (setq gnus-newsgroup-headers
4234               (sort articles (gnus-make-sort-function
4235                               gnus-article-sort-functions)))
4236       (gnus-message 7 "Sorting articles...done"))))
4237
4238 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4239 (defmacro gnus-thread-header (thread)
4240   "Return header of first article in THREAD.
4241 Note that THREAD must never, ever be anything else than a variable -
4242 using some other form will lead to serious barfage."
4243   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4244   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4245   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4246         (vector thread) 2))
4247
4248 (defsubst gnus-article-sort-by-number (h1 h2)
4249   "Sort articles by article number."
4250   (< (mail-header-number h1)
4251      (mail-header-number h2)))
4252
4253 (defun gnus-thread-sort-by-number (h1 h2)
4254   "Sort threads by root article number."
4255   (gnus-article-sort-by-number
4256    (gnus-thread-header h1) (gnus-thread-header h2)))
4257
4258 (defsubst gnus-article-sort-by-random (h1 h2)
4259   "Sort articles by article number."
4260   (zerop (random 2)))
4261
4262 (defun gnus-thread-sort-by-random (h1 h2)
4263   "Sort threads by root article number."
4264   (gnus-article-sort-by-random
4265    (gnus-thread-header h1) (gnus-thread-header h2)))
4266
4267 (defsubst gnus-article-sort-by-lines (h1 h2)
4268   "Sort articles by article Lines header."
4269   (< (mail-header-lines h1)
4270      (mail-header-lines h2)))
4271
4272 (defun gnus-thread-sort-by-lines (h1 h2)
4273   "Sort threads by root article Lines header."
4274   (gnus-article-sort-by-lines
4275    (gnus-thread-header h1) (gnus-thread-header h2)))
4276
4277 (defsubst gnus-article-sort-by-chars (h1 h2)
4278   "Sort articles by octet length."
4279   (< (mail-header-chars h1)
4280      (mail-header-chars h2)))
4281
4282 (defun gnus-thread-sort-by-chars (h1 h2)
4283   "Sort threads by root article octet length."
4284   (gnus-article-sort-by-chars
4285    (gnus-thread-header h1) (gnus-thread-header h2)))
4286
4287 (defsubst gnus-article-sort-by-author (h1 h2)
4288   "Sort articles by root author."
4289   (string-lessp
4290    (let ((extract (funcall
4291                    gnus-extract-address-components
4292                    (mail-header-from h1))))
4293      (or (car extract) (cadr extract) ""))
4294    (let ((extract (funcall
4295                    gnus-extract-address-components
4296                    (mail-header-from h2))))
4297      (or (car extract) (cadr extract) ""))))
4298
4299 (defun gnus-thread-sort-by-author (h1 h2)
4300   "Sort threads by root author."
4301   (gnus-article-sort-by-author
4302    (gnus-thread-header h1)  (gnus-thread-header h2)))
4303
4304 (defsubst gnus-article-sort-by-subject (h1 h2)
4305   "Sort articles by root subject."
4306   (string-lessp
4307    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4308    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4309
4310 (defun gnus-thread-sort-by-subject (h1 h2)
4311   "Sort threads by root subject."
4312   (gnus-article-sort-by-subject
4313    (gnus-thread-header h1) (gnus-thread-header h2)))
4314
4315 (defsubst gnus-article-sort-by-date (h1 h2)
4316   "Sort articles by root article date."
4317   (time-less-p
4318    (gnus-date-get-time (mail-header-date h1))
4319    (gnus-date-get-time (mail-header-date h2))))
4320
4321 (defun gnus-thread-sort-by-date (h1 h2)
4322   "Sort threads by root article date."
4323   (gnus-article-sort-by-date
4324    (gnus-thread-header h1) (gnus-thread-header h2)))
4325
4326 (defsubst gnus-article-sort-by-score (h1 h2)
4327   "Sort articles by root article score.
4328 Unscored articles will be counted as having a score of zero."
4329   (> (or (cdr (assq (mail-header-number h1)
4330                     gnus-newsgroup-scored))
4331          gnus-summary-default-score 0)
4332      (or (cdr (assq (mail-header-number h2)
4333                     gnus-newsgroup-scored))
4334          gnus-summary-default-score 0)))
4335
4336 (defun gnus-thread-sort-by-score (h1 h2)
4337   "Sort threads by root article score."
4338   (gnus-article-sort-by-score
4339    (gnus-thread-header h1) (gnus-thread-header h2)))
4340
4341 (defun gnus-thread-sort-by-total-score (h1 h2)
4342   "Sort threads by the sum of all scores in the thread.
4343 Unscored articles will be counted as having a score of zero."
4344   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4345
4346 (defun gnus-thread-total-score (thread)
4347   ;; This function find the total score of THREAD.
4348   (cond
4349    ((null thread)
4350     0)
4351    ((consp thread)
4352     (if (stringp (car thread))
4353         (apply gnus-thread-score-function 0
4354                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4355       (gnus-thread-total-score-1 thread)))
4356    (t
4357     (gnus-thread-total-score-1 (list thread)))))
4358
4359 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4360   "Sort threads such that the thread with the most recently arrived article comes first."
4361   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4362
4363 (defun gnus-thread-highest-number (thread)
4364   "Return the highest article number in THREAD."
4365   (apply 'max (mapcar (lambda (header)
4366                         (mail-header-number header))
4367                       (message-flatten-list thread))))
4368
4369 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4370   "Sort threads such that the thread with the most recently dated article comes first."
4371   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4372
4373 (defun gnus-thread-latest-date (thread)
4374   "Return the highest article date in THREAD."
4375   (let ((previous-time 0))
4376     (apply 'max (mapcar
4377                  (lambda (header)
4378                    (setq previous-time
4379                          (time-to-seconds
4380                           (mail-header-parse-date
4381                            (condition-case ()
4382                                (mail-header-date header)
4383                              (error previous-time))))))
4384                  (sort
4385                   (message-flatten-list thread)
4386                   (lambda (h1 h2)
4387                     (< (mail-header-number h1)
4388                        (mail-header-number h2))))))))
4389
4390 (defun gnus-thread-total-score-1 (root)
4391   ;; This function find the total score of the thread below ROOT.
4392   (setq root (car root))
4393   (apply gnus-thread-score-function
4394          (or (append
4395               (mapcar 'gnus-thread-total-score
4396                       (cdr (gnus-id-to-thread (mail-header-id root))))
4397               (when (> (mail-header-number root) 0)
4398                 (list (or (cdr (assq (mail-header-number root)
4399                                      gnus-newsgroup-scored))
4400                           gnus-summary-default-score 0))))
4401              (list gnus-summary-default-score)
4402              '(0))))
4403
4404 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4405 (defvar gnus-tmp-prev-subject nil)
4406 (defvar gnus-tmp-false-parent nil)
4407 (defvar gnus-tmp-root-expunged nil)
4408 (defvar gnus-tmp-dummy-line nil)
4409
4410 (eval-when-compile (defvar gnus-tmp-header))
4411 (defun gnus-extra-header (type &optional header)
4412   "Return the extra header of TYPE."
4413   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4414       ""))
4415
4416 (defvar gnus-tmp-thread-tree-header-string "")
4417
4418 (defcustom gnus-sum-thread-tree-root "> "
4419   "With %B spec, used for the root of a thread.
4420 If nil, use subject instead."
4421   :type 'string
4422   :group 'gnus-thread)
4423 (defcustom gnus-sum-thread-tree-single-indent ""
4424   "With %B spec, used for a thread with just one message.
4425 If nil, use subject instead."
4426   :type 'string
4427   :group 'gnus-thread)
4428 (defcustom gnus-sum-thread-tree-vertical "| "
4429   "With %B spec, used for drawing a vertical line."
4430   :type 'string
4431   :group 'gnus-thread)
4432 (defcustom gnus-sum-thread-tree-indent "  "
4433   "With %B spec, used for indenting."
4434   :type 'string
4435   :group 'gnus-thread)
4436 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4437   "With %B spec, used for a leaf with brothers."
4438   :type 'string
4439   :group 'gnus-thread)
4440 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4441   "With %B spec, used for a leaf without brothers."
4442   :type 'string
4443   :group 'gnus-thread)
4444
4445 (defun gnus-summary-prepare-threads (threads)
4446   "Prepare summary buffer from THREADS and indentation LEVEL.
4447 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4448 or a straight list of headers."
4449   (gnus-message 7 "Generating summary...")
4450
4451   (setq gnus-newsgroup-threads threads)
4452   (beginning-of-line)
4453
4454   (let ((gnus-tmp-level 0)
4455         (default-score (or gnus-summary-default-score 0))
4456         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4457         thread number subject stack state gnus-tmp-gathered beg-match
4458         new-roots gnus-tmp-new-adopts thread-end simp-subject
4459         gnus-tmp-header gnus-tmp-unread
4460         gnus-tmp-replied gnus-tmp-subject-or-nil
4461         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4462         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4463         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4464         tree-stack)
4465
4466     (setq gnus-tmp-prev-subject nil)
4467
4468     (if (vectorp (car threads))
4469         ;; If this is a straight (sic) list of headers, then a
4470         ;; threaded summary display isn't required, so we just create
4471         ;; an unthreaded one.
4472         (gnus-summary-prepare-unthreaded threads)
4473
4474       ;; Do the threaded display.
4475
4476       (while (or threads stack gnus-tmp-new-adopts new-roots)
4477
4478         (if (and (= gnus-tmp-level 0)
4479                  (or (not stack)
4480                      (= (caar stack) 0))
4481                  (not gnus-tmp-false-parent)
4482                  (or gnus-tmp-new-adopts new-roots))
4483             (if gnus-tmp-new-adopts
4484                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4485                       thread (list (car gnus-tmp-new-adopts))
4486                       gnus-tmp-header (caar thread)
4487                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4488               (when new-roots
4489                 (setq thread (list (car new-roots))
4490                       gnus-tmp-header (caar thread)
4491                       new-roots (cdr new-roots))))
4492
4493           (if threads
4494               ;; If there are some threads, we do them before the
4495               ;; threads on the stack.
4496               (setq thread threads
4497                     gnus-tmp-header (caar thread))
4498             ;; There were no current threads, so we pop something off
4499             ;; the stack.
4500             (setq state (car stack)
4501                   gnus-tmp-level (car state)
4502                   tree-stack (cadr state)
4503                   thread (caddr state)
4504                   stack (cdr stack)
4505                   gnus-tmp-header (caar thread))))
4506
4507         (setq gnus-tmp-false-parent nil)
4508         (setq gnus-tmp-root-expunged nil)
4509         (setq thread-end nil)
4510
4511         (if (stringp gnus-tmp-header)
4512             ;; The header is a dummy root.
4513             (cond
4514              ((eq gnus-summary-make-false-root 'adopt)
4515               ;; We let the first article adopt the rest.
4516               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4517                                                (cddar thread)))
4518               (setq gnus-tmp-gathered
4519                     (nconc (mapcar
4520                             (lambda (h) (mail-header-number (car h)))
4521                             (cddar thread))
4522                            gnus-tmp-gathered))
4523               (setq thread (cons (list (caar thread)
4524                                        (cadar thread))
4525                                  (cdr thread)))
4526               (setq gnus-tmp-level -1
4527                     gnus-tmp-false-parent t))
4528              ((eq gnus-summary-make-false-root 'empty)
4529               ;; We print adopted articles with empty subject fields.
4530               (setq gnus-tmp-gathered
4531                     (nconc (mapcar
4532                             (lambda (h) (mail-header-number (car h)))
4533                             (cddar thread))
4534                            gnus-tmp-gathered))
4535               (setq gnus-tmp-level -1))
4536              ((eq gnus-summary-make-false-root 'dummy)
4537               ;; We remember that we probably want to output a dummy
4538               ;; root.
4539               (setq gnus-tmp-dummy-line gnus-tmp-header)
4540               (setq gnus-tmp-prev-subject gnus-tmp-header))
4541              (t
4542               ;; We do not make a root for the gathered
4543               ;; sub-threads at all.
4544               (setq gnus-tmp-level -1)))
4545
4546           (setq number (mail-header-number gnus-tmp-header)
4547                 subject (mail-header-subject gnus-tmp-header)
4548                 simp-subject (gnus-simplify-subject-fully subject))
4549
4550           (cond
4551            ;; If the thread has changed subject, we might want to make
4552            ;; this subthread into a root.
4553            ((and (null gnus-thread-ignore-subject)
4554                  (not (zerop gnus-tmp-level))
4555                  gnus-tmp-prev-subject
4556                  (not (string= gnus-tmp-prev-subject simp-subject)))
4557             (setq new-roots (nconc new-roots (list (car thread)))
4558                   thread-end t
4559                   gnus-tmp-header nil))
4560            ;; If the article lies outside the current limit,
4561            ;; then we do not display it.
4562            ((not (memq number gnus-newsgroup-limit))
4563             (setq gnus-tmp-gathered
4564                   (nconc (mapcar
4565                           (lambda (h) (mail-header-number (car h)))
4566                           (cdar thread))
4567                          gnus-tmp-gathered))
4568             (setq gnus-tmp-new-adopts (if (cdar thread)
4569                                           (append gnus-tmp-new-adopts
4570                                                   (cdar thread))
4571                                         gnus-tmp-new-adopts)
4572                   thread-end t
4573                   gnus-tmp-header nil)
4574             (when (zerop gnus-tmp-level)
4575               (setq gnus-tmp-root-expunged t)))
4576            ;; Perhaps this article is to be marked as read?
4577            ((and gnus-summary-mark-below
4578                  (< (or (cdr (assq number gnus-newsgroup-scored))
4579                         default-score)
4580                     gnus-summary-mark-below)
4581                  ;; Don't touch sparse articles.
4582                  (not (gnus-summary-article-sparse-p number))
4583                  (not (gnus-summary-article-ancient-p number)))
4584             (setq gnus-newsgroup-unreads
4585                   (delq number gnus-newsgroup-unreads))
4586             (if gnus-newsgroup-auto-expire
4587                 (setq gnus-newsgroup-expirable
4588                       (gnus-add-to-sorted-list
4589                        gnus-newsgroup-expirable number))
4590               (push (cons number gnus-low-score-mark)
4591                     gnus-newsgroup-reads))))
4592
4593           (when gnus-tmp-header
4594             ;; We may have an old dummy line to output before this
4595             ;; article.
4596             (when (and gnus-tmp-dummy-line
4597                        (gnus-subject-equal
4598                         gnus-tmp-dummy-line
4599                         (mail-header-subject gnus-tmp-header)))
4600               (gnus-summary-insert-dummy-line
4601                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4602               (setq gnus-tmp-dummy-line nil))
4603
4604             ;; Compute the mark.
4605             (setq gnus-tmp-unread (gnus-article-mark number))
4606
4607             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4608                                   gnus-tmp-header gnus-tmp-level)
4609                   gnus-newsgroup-data)
4610
4611             ;; Actually insert the line.
4612             (setq
4613              gnus-tmp-subject-or-nil
4614              (cond
4615               ((and gnus-thread-ignore-subject
4616                     gnus-tmp-prev-subject
4617                     (not (string= gnus-tmp-prev-subject simp-subject)))
4618                subject)
4619               ((zerop gnus-tmp-level)
4620                (if (and (eq gnus-summary-make-false-root 'empty)
4621                         (memq number gnus-tmp-gathered)
4622                         gnus-tmp-prev-subject
4623                         (string= gnus-tmp-prev-subject simp-subject))
4624                    gnus-summary-same-subject
4625                  subject))
4626               (t gnus-summary-same-subject)))
4627             (if (and (eq gnus-summary-make-false-root 'adopt)
4628                      (= gnus-tmp-level 1)
4629                      (memq number gnus-tmp-gathered))
4630                 (setq gnus-tmp-opening-bracket ?\<
4631                       gnus-tmp-closing-bracket ?\>)
4632               (setq gnus-tmp-opening-bracket ?\[
4633                     gnus-tmp-closing-bracket ?\]))
4634             (setq
4635              gnus-tmp-indentation
4636              (aref gnus-thread-indent-array gnus-tmp-level)
4637              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4638              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4639                                 gnus-summary-default-score 0)
4640              gnus-tmp-score-char
4641              (if (or (null gnus-summary-default-score)
4642                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4643                          gnus-summary-zcore-fuzz))
4644                  ?                      ;Whitespace
4645                (if (< gnus-tmp-score gnus-summary-default-score)
4646                    gnus-score-below-mark gnus-score-over-mark))
4647              gnus-tmp-replied
4648              (cond ((memq number gnus-newsgroup-processable)
4649                     gnus-process-mark)
4650                    ((memq number gnus-newsgroup-cached)
4651                     gnus-cached-mark)
4652                    ((memq number gnus-newsgroup-replied)
4653                     gnus-replied-mark)
4654                    ((memq number gnus-newsgroup-forwarded)
4655                     gnus-forwarded-mark)
4656                    ((memq number gnus-newsgroup-saved)
4657                     gnus-saved-mark)
4658                    ((memq number gnus-newsgroup-recent)
4659                     gnus-recent-mark)
4660                    ((memq number gnus-newsgroup-unseen)
4661                     gnus-unseen-mark)
4662                    (t gnus-no-mark))
4663              gnus-tmp-from (mail-header-from gnus-tmp-header)
4664              gnus-tmp-name
4665              (cond
4666               ((string-match "<[^>]+> *$" gnus-tmp-from)
4667                (setq beg-match (match-beginning 0))
4668                (or (and (string-match "^\".+\"" gnus-tmp-from)
4669                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4670                    (substring gnus-tmp-from 0 beg-match)))
4671               ((string-match "(.+)" gnus-tmp-from)
4672                (substring gnus-tmp-from
4673                           (1+ (match-beginning 0)) (1- (match-end 0))))
4674               (t gnus-tmp-from))
4675              gnus-tmp-thread-tree-header-string
4676              (cond
4677               ((not gnus-show-threads) "")
4678               ((zerop gnus-tmp-level)
4679                (if (cdar thread)
4680                    (or gnus-sum-thread-tree-root subject)
4681                  (or gnus-sum-thread-tree-single-indent subject)))
4682               (t
4683                (concat (apply 'concat
4684                               (mapcar (lambda (item)
4685                                         (if (= item 1)
4686                                             gnus-sum-thread-tree-vertical
4687                                           gnus-sum-thread-tree-indent))
4688                                       (cdr (reverse tree-stack))))
4689                        (if (nth 1 thread)
4690                            gnus-sum-thread-tree-leaf-with-other
4691                          gnus-sum-thread-tree-single-leaf)))))
4692             (when (string= gnus-tmp-name "")
4693               (setq gnus-tmp-name gnus-tmp-from))
4694             (unless (numberp gnus-tmp-lines)
4695               (setq gnus-tmp-lines -1))
4696             (if (= gnus-tmp-lines -1)
4697                 (setq gnus-tmp-lines "?")
4698               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4699             (gnus-put-text-property
4700              (point)
4701              (progn (eval gnus-summary-line-format-spec) (point))
4702              'gnus-number number)
4703             (when gnus-visual-p
4704               (forward-line -1)
4705               (gnus-run-hooks 'gnus-summary-update-hook)
4706               (forward-line 1))
4707
4708             (setq gnus-tmp-prev-subject simp-subject)))
4709
4710         (when (nth 1 thread)
4711           (push (list (max 0 gnus-tmp-level)
4712                       (copy-list tree-stack)
4713                       (nthcdr 1 thread))
4714                 stack))
4715         (push (if (nth 1 thread) 1 0) tree-stack)
4716         (incf gnus-tmp-level)
4717         (setq threads (if thread-end nil (cdar thread)))
4718         (unless threads
4719           (setq gnus-tmp-level 0)))))
4720   (gnus-message 7 "Generating summary...done"))
4721
4722 (defun gnus-summary-prepare-unthreaded (headers)
4723   "Generate an unthreaded summary buffer based on HEADERS."
4724   (let (header number mark)
4725
4726     (beginning-of-line)
4727
4728     (while headers
4729       ;; We may have to root out some bad articles...
4730       (when (memq (setq number (mail-header-number
4731                                 (setq header (pop headers))))
4732                   gnus-newsgroup-limit)
4733         ;; Mark article as read when it has a low score.
4734         (when (and gnus-summary-mark-below
4735                    (< (or (cdr (assq number gnus-newsgroup-scored))
4736                           gnus-summary-default-score 0)
4737                       gnus-summary-mark-below)
4738                    (not (gnus-summary-article-ancient-p number)))
4739           (setq gnus-newsgroup-unreads
4740                 (delq number gnus-newsgroup-unreads))
4741           (if gnus-newsgroup-auto-expire
4742               (push number gnus-newsgroup-expirable)
4743             (push (cons number gnus-low-score-mark)
4744                   gnus-newsgroup-reads)))
4745
4746         (setq mark (gnus-article-mark number))
4747         (push (gnus-data-make number mark (1+ (point)) header 0)
4748               gnus-newsgroup-data)
4749         (gnus-summary-insert-line
4750          header 0 number
4751          mark (memq number gnus-newsgroup-replied)
4752          (memq number gnus-newsgroup-expirable)
4753          (mail-header-subject header) nil
4754          (cdr (assq number gnus-newsgroup-scored))
4755          (memq number gnus-newsgroup-processable))))))
4756
4757 (defun gnus-summary-remove-list-identifiers ()
4758   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4759   (let ((regexp (if (consp gnus-list-identifiers)
4760                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4761                   gnus-list-identifiers))
4762         changed subject)
4763     (when regexp
4764       (dolist (header gnus-newsgroup-headers)
4765         (setq subject (mail-header-subject header)
4766               changed nil)
4767         (while (string-match
4768                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4769                 subject)
4770           (setq subject
4771                 (concat (substring subject 0 (match-beginning 2))
4772                         (substring subject (match-end 0)))
4773                 changed t))
4774         (when (and changed
4775                    (string-match
4776                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4777           (setq subject
4778                 (concat (substring subject 0 (match-beginning 1))
4779                         (substring subject (match-end 1)))))
4780         (when changed
4781           (mail-header-set-subject header subject))))))
4782
4783 (defun gnus-fetch-headers (articles)
4784   "Fetch headers of ARTICLES."
4785   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4786     (gnus-message 5 "Fetching headers for %s..." name)
4787     (prog1
4788         (if (eq 'nov
4789                 (setq gnus-headers-retrieved-by
4790                       (gnus-retrieve-headers
4791                        articles gnus-newsgroup-name
4792                        ;; We might want to fetch old headers, but
4793                        ;; not if there is only 1 article.
4794                        (and (or (and
4795                                  (not (eq gnus-fetch-old-headers 'some))
4796                                  (not (numberp gnus-fetch-old-headers)))
4797                                 (> (length articles) 1))
4798                             gnus-fetch-old-headers))))
4799             (gnus-get-newsgroup-headers-xover
4800              articles nil nil gnus-newsgroup-name t)
4801           (gnus-get-newsgroup-headers))
4802       (gnus-message 5 "Fetching headers for %s...done" name))))
4803
4804 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4805   "Select newsgroup GROUP.
4806 If READ-ALL is non-nil, all articles in the group are selected.
4807 If SELECT-ARTICLES, only select those articles from GROUP."
4808   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4809          ;;!!! Dirty hack; should be removed.
4810          (gnus-summary-ignore-duplicates
4811           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4812               t
4813             gnus-summary-ignore-duplicates))
4814          (info (nth 2 entry))
4815          articles fetched-articles cached)
4816
4817     (unless (gnus-check-server
4818              (set (make-local-variable 'gnus-current-select-method)
4819                   (gnus-find-method-for-group group)))
4820       (error "Couldn't open server"))
4821
4822     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4823         (gnus-activate-group group)     ; Or we can activate it...
4824         (progn                          ; Or we bug out.
4825           (when (equal major-mode 'gnus-summary-mode)
4826             (kill-buffer (current-buffer)))
4827           (error "Couldn't activate group %s: %s"
4828                  group (gnus-status-message group))))
4829
4830     (unless (gnus-request-group group t)
4831       (when (equal major-mode 'gnus-summary-mode)
4832         (kill-buffer (current-buffer)))
4833       (error "Couldn't request group %s: %s"
4834              group (gnus-status-message group)))
4835
4836     (setq gnus-newsgroup-name group
4837           gnus-newsgroup-unselected nil
4838           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4839
4840     (let ((display (gnus-group-find-parameter group 'display)))
4841       (setq gnus-newsgroup-display
4842             (cond
4843              ((not (zerop (or (car-safe read-all) 0)))
4844               ;; The user entered the group with C-u SPC/RET, let's show
4845               ;; all articles.
4846               'gnus-not-ignore)
4847              ((eq display 'all)
4848               'gnus-not-ignore)
4849              ((arrayp display)
4850               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4851              ((numberp display)
4852               ;; The following is probably the "correct" solution, but
4853               ;; it makes Gnus fetch all headers and then limit the
4854               ;; articles (which is slow), so instead we hack the
4855               ;; select-articles parameter instead. -- Simon Josefsson
4856               ;; <jas@kth.se>
4857               ;;
4858               ;; (gnus-byte-compile
4859               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4860               ;;                         display)))))
4861               (setq select-articles
4862                     (gnus-uncompress-range
4863                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4864                              (if (> tmp 0)
4865                                  tmp
4866                                1))
4867                            (cdr (gnus-active group)))))
4868               nil)
4869              (t
4870               nil))))
4871
4872     (gnus-summary-setup-default-charset)
4873
4874     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4875     (when (gnus-virtual-group-p group)
4876       (setq cached gnus-newsgroup-cached))
4877
4878     (setq gnus-newsgroup-unreads
4879           (gnus-sorted-ndifference
4880            (gnus-sorted-ndifference gnus-newsgroup-unreads
4881                                     gnus-newsgroup-marked)
4882            gnus-newsgroup-dormant))
4883
4884     (setq gnus-newsgroup-processable nil)
4885
4886     (gnus-update-read-articles group gnus-newsgroup-unreads)
4887
4888     ;; Adjust and set lists of article marks.
4889     (when info
4890       (gnus-adjust-marked-articles info))
4891
4892     (if (setq articles select-articles)
4893         (setq gnus-newsgroup-unselected
4894               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4895       (setq articles (gnus-articles-to-read group read-all)))
4896
4897     (cond
4898      ((null articles)
4899       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4900       'quit)
4901      ((eq articles 0) nil)
4902      (t
4903       ;; Init the dependencies hash table.
4904       (setq gnus-newsgroup-dependencies
4905             (gnus-make-hashtable (length articles)))
4906       (gnus-set-global-variables)
4907       ;; Retrieve the headers and read them in.
4908       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4909
4910       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4911       (when cached
4912         (setq gnus-newsgroup-cached cached))
4913
4914       ;; Suppress duplicates?
4915       (when gnus-suppress-duplicates
4916         (gnus-dup-suppress-articles))
4917
4918       ;; Set the initial limit.
4919       (setq gnus-newsgroup-limit (copy-sequence articles))
4920       ;; Remove canceled articles from the list of unread articles.
4921       (setq fetched-articles
4922             (mapcar (lambda (headers) (mail-header-number headers))
4923                     gnus-newsgroup-headers))
4924       (setq gnus-newsgroup-articles fetched-articles)
4925       (setq gnus-newsgroup-unreads
4926             (gnus-sorted-nintersection
4927              gnus-newsgroup-unreads fetched-articles))
4928       (gnus-compute-unseen-list)
4929
4930       ;; Removed marked articles that do not exist.
4931       (gnus-update-missing-marks
4932        (gnus-sorted-difference articles fetched-articles))
4933       ;; We might want to build some more threads first.
4934       (when (and gnus-fetch-old-headers
4935                  (eq gnus-headers-retrieved-by 'nov))
4936         (if (eq gnus-fetch-old-headers 'invisible)
4937             (gnus-build-all-threads)
4938           (gnus-build-old-threads)))
4939       ;; Let the Gnus agent mark articles as read.
4940       (when gnus-agent
4941         (gnus-agent-get-undownloaded-list))
4942       ;; Remove list identifiers from subject
4943       (when gnus-list-identifiers
4944         (gnus-summary-remove-list-identifiers))
4945       ;; Check whether auto-expire is to be done in this group.
4946       (setq gnus-newsgroup-auto-expire
4947             (gnus-group-auto-expirable-p group))
4948       ;; Set up the article buffer now, if necessary.
4949       (unless gnus-single-article-buffer
4950         (gnus-article-setup-buffer))
4951       ;; First and last article in this newsgroup.
4952       (when gnus-newsgroup-headers
4953         (setq gnus-newsgroup-begin
4954               (mail-header-number (car gnus-newsgroup-headers))
4955               gnus-newsgroup-end
4956               (mail-header-number
4957                (gnus-last-element gnus-newsgroup-headers))))
4958       ;; GROUP is successfully selected.
4959       (or gnus-newsgroup-headers t)))))
4960
4961 (defun gnus-compute-unseen-list ()
4962   ;; The `seen' marks are treated specially.
4963   (if (not gnus-newsgroup-seen)
4964       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4965     (setq gnus-newsgroup-unseen
4966           (gnus-inverse-list-range-intersection
4967            gnus-newsgroup-articles gnus-newsgroup-seen))))
4968
4969 (defun gnus-summary-display-make-predicate (display)
4970   (require 'gnus-agent)
4971   (when (= (length display) 1)
4972     (setq display (car display)))
4973   (unless gnus-summary-display-cache
4974     (dolist (elem (append '((unread . unread)
4975                             (read . read)
4976                             (unseen . unseen))
4977                           gnus-article-mark-lists))
4978       (push (cons (cdr elem)
4979                   (gnus-byte-compile
4980                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4981             gnus-summary-display-cache)))
4982   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
4983         (gnus-category-predicate-cache gnus-summary-display-cache))
4984     (gnus-get-predicate display)))
4985
4986 ;; Uses the dynamically bound `number' variable.
4987 (defvar number)
4988 (defun gnus-article-marked-p (type &optional article)
4989   (let ((article (or article number)))
4990     (cond
4991      ((eq type 'tick)
4992       (memq article gnus-newsgroup-marked))
4993      ((eq type 'spam)
4994       (memq article gnus-newsgroup-spam-marked))
4995      ((eq type 'unsend)
4996       (memq article gnus-newsgroup-unsendable))
4997      ((eq type 'undownload)
4998       (memq article gnus-newsgroup-undownloaded))
4999      ((eq type 'download)
5000       (memq article gnus-newsgroup-downloadable))
5001      ((eq type 'unread)
5002       (memq article gnus-newsgroup-unreads))
5003      ((eq type 'read)
5004       (memq article gnus-newsgroup-reads))
5005      ((eq type 'dormant)
5006       (memq article gnus-newsgroup-dormant) )
5007      ((eq type 'expire)
5008       (memq article gnus-newsgroup-expirable))
5009      ((eq type 'reply)
5010       (memq article gnus-newsgroup-replied))
5011      ((eq type 'killed)
5012       (memq article gnus-newsgroup-killed))
5013      ((eq type 'bookmark)
5014       (assq article gnus-newsgroup-bookmarks))
5015      ((eq type 'score)
5016       (assq article gnus-newsgroup-scored))
5017      ((eq type 'save)
5018       (memq article gnus-newsgroup-saved))
5019      ((eq type 'cache)
5020       (memq article gnus-newsgroup-cached))
5021      ((eq type 'forward)
5022       (memq article gnus-newsgroup-forwarded))
5023      ((eq type 'seen)
5024       (not (memq article gnus-newsgroup-unseen)))
5025      ((eq type 'recent)
5026       (memq article gnus-newsgroup-recent))
5027      (t t))))
5028
5029 (defun gnus-articles-to-read (group &optional read-all)
5030   "Find out what articles the user wants to read."
5031   (let* ((articles
5032           ;; Select all articles if `read-all' is non-nil, or if there
5033           ;; are no unread articles.
5034           (if (or read-all
5035                   (and (zerop (length gnus-newsgroup-marked))
5036                        (zerop (length gnus-newsgroup-unreads)))
5037                   ;; Fetch all if the predicate is non-nil.
5038                   gnus-newsgroup-display)
5039               ;; We want to select the headers for all the articles in
5040               ;; the group, so we select either all the active
5041               ;; articles in the group, or (if that's nil), the
5042               ;; articles in the cache.
5043               (or
5044                (gnus-uncompress-range (gnus-active group))
5045                (gnus-cache-articles-in-group group))
5046             ;; Select only the "normal" subset of articles.
5047             (gnus-sorted-nunion
5048              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5049              gnus-newsgroup-unreads)))
5050          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5051          (scored (length scored-list))
5052          (number (length articles))
5053          (marked (+ (length gnus-newsgroup-marked)
5054                     (length gnus-newsgroup-dormant)))
5055          (select
5056           (cond
5057            ((numberp read-all)
5058             read-all)
5059            ((numberp gnus-newsgroup-display)
5060             gnus-newsgroup-display)
5061            (t
5062             (condition-case ()
5063                 (cond
5064                  ((and (or (<= scored marked) (= scored number))
5065                        (numberp gnus-large-newsgroup)
5066                        (> number gnus-large-newsgroup))
5067                   (let* ((cursor-in-echo-area nil)
5068                          (initial (gnus-parameter-large-newsgroup-initial
5069                                    gnus-newsgroup-name))
5070                          (input
5071                           (read-string
5072                            (format
5073                             "How many articles from %s (%s %d): "
5074                             (gnus-limit-string
5075                              (gnus-group-decoded-name gnus-newsgroup-name)
5076                              35)
5077                             (if initial "max" "default")
5078                             number)
5079                            (if initial
5080                                (cons (number-to-string initial)
5081                                      0)))))
5082                     (if (string-match "^[ \t]*$" input) number input)))
5083                  ((and (> scored marked) (< scored number)
5084                        (> (- scored number) 20))
5085                   (let ((input
5086                          (read-string
5087                           (format "%s %s (%d scored, %d total): "
5088                                   "How many articles from"
5089                                   (gnus-group-decoded-name group)
5090                                   scored number))))
5091                     (if (string-match "^[ \t]*$" input)
5092                         number input)))
5093                  (t number))
5094               (quit
5095                (message "Quit getting the articles to read")
5096                nil))))))
5097     (setq select (if (stringp select) (string-to-number select) select))
5098     (if (or (null select) (zerop select))
5099         select
5100       (if (and (not (zerop scored)) (<= (abs select) scored))
5101           (progn
5102             (setq articles (sort scored-list '<))
5103             (setq number (length articles)))
5104         (setq articles (copy-sequence articles)))
5105
5106       (when (< (abs select) number)
5107         (if (< select 0)
5108             ;; Select the N oldest articles.
5109             (setcdr (nthcdr (1- (abs select)) articles) nil)
5110           ;; Select the N most recent articles.
5111           (setq articles (nthcdr (- number select) articles))))
5112       (setq gnus-newsgroup-unselected
5113             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5114       (when gnus-alter-articles-to-read-function
5115         (setq gnus-newsgroup-unreads
5116               (sort
5117                (funcall gnus-alter-articles-to-read-function
5118                         gnus-newsgroup-name gnus-newsgroup-unreads)
5119                '<)))
5120       articles)))
5121
5122 (defun gnus-killed-articles (killed articles)
5123   (let (out)
5124     (while articles
5125       (when (inline (gnus-member-of-range (car articles) killed))
5126         (push (car articles) out))
5127       (setq articles (cdr articles)))
5128     out))
5129
5130 (defun gnus-uncompress-marks (marks)
5131   "Uncompress the mark ranges in MARKS."
5132   (let ((uncompressed '(score bookmark))
5133         out)
5134     (while marks
5135       (if (memq (caar marks) uncompressed)
5136           (push (car marks) out)
5137         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5138       (setq marks (cdr marks)))
5139     out))
5140
5141 (defun gnus-article-mark-to-type (mark)
5142   "Return the type of MARK."
5143   (or (cadr (assq mark gnus-article-special-mark-lists))
5144       'list))
5145
5146 (defun gnus-article-unpropagatable-p (mark)
5147   "Return whether MARK should be propagated to backend."
5148   (memq mark gnus-article-unpropagated-mark-lists))
5149
5150 (defun gnus-adjust-marked-articles (info)
5151   "Set all article lists and remove all marks that are no longer valid."
5152   (let* ((marked-lists (gnus-info-marks info))
5153          (active (gnus-active (gnus-info-group info)))
5154          (min (car active))
5155          (max (cdr active))
5156          (types gnus-article-mark-lists)
5157          marks var articles article mark mark-type)
5158
5159     (dolist (marks marked-lists)
5160       (setq mark (car marks)
5161             mark-type (gnus-article-mark-to-type mark)
5162             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5163
5164       ;; We set the variable according to the type of the marks list,
5165       ;; and then adjust the marks to a subset of the active articles.
5166       (cond
5167        ;; Adjust "simple" lists.
5168        ((eq mark-type 'list)
5169         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5170         (when (memq mark '(tick dormant expire reply save))
5171           (while articles
5172             (when (or (< (setq article (pop articles)) min) (> article max))
5173               (set var (delq article (symbol-value var)))))))
5174        ;; Adjust assocs.
5175        ((eq mark-type 'tuple)
5176         (set var (setq articles (cdr marks)))
5177         (when (not (listp (cdr (symbol-value var))))
5178           (set var (list (symbol-value var))))
5179         (when (not (listp (cdr articles)))
5180           (setq articles (list articles)))
5181         (while articles
5182           (when (or (not (consp (setq article (pop articles))))
5183                     (< (car article) min)
5184                     (> (car article) max))
5185             (set var (delq article (symbol-value var))))))
5186        ;; Adjust ranges (sloppily).
5187        ((eq mark-type 'range)
5188         (cond
5189          ((eq mark 'seen)
5190           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5191           ;; It should be (seen (NUM1 . NUM2)).
5192           (when (numberp (cddr marks))
5193             (setcdr marks (list (cdr marks))))
5194           (setq articles (cdr marks))
5195           (while (and articles
5196                       (or (and (consp (car articles))
5197                                (> min (cdar articles)))
5198                           (and (numberp (car articles))
5199                                (> min (car articles)))))
5200             (pop articles))
5201           (set var articles))))))))
5202
5203 (defun gnus-update-missing-marks (missing)
5204   "Go through the list of MISSING articles and remove them from the mark lists."
5205   (when missing
5206     (let (var m)
5207       ;; Go through all types.
5208       (dolist (elem gnus-article-mark-lists)
5209         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5210           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5211           (when (symbol-value var)
5212             ;; This list has articles.  So we delete all missing
5213             ;; articles from it.
5214             (setq m missing)
5215             (while m
5216               (set var (delq (pop m) (symbol-value var))))))))))
5217
5218 (defun gnus-update-marks ()
5219   "Enter the various lists of marked articles into the newsgroup info list."
5220   (let ((types gnus-article-mark-lists)
5221         (info (gnus-get-info gnus-newsgroup-name))
5222         type list newmarked symbol delta-marks)
5223     (when info
5224       ;; Add all marks lists to the list of marks lists.
5225       (while (setq type (pop types))
5226         (setq list (symbol-value
5227                     (setq symbol
5228                           (intern (format "gnus-newsgroup-%s" (car type))))))
5229
5230         (when list
5231           ;; Get rid of the entries of the articles that have the
5232           ;; default score.
5233           (when (and (eq (cdr type) 'score)
5234                      gnus-save-score
5235                      list)
5236             (let* ((arts list)
5237                    (prev (cons nil list))
5238                    (all prev))
5239               (while arts
5240                 (if (or (not (consp (car arts)))
5241                         (= (cdar arts) gnus-summary-default-score))
5242                     (setcdr prev (cdr arts))
5243                   (setq prev arts))
5244                 (setq arts (cdr arts)))
5245               (setq list (cdr all)))))
5246
5247         (when (eq (cdr type) 'seen)
5248           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5249
5250         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5251           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5252
5253         (when (and (gnus-check-backend-function
5254                     'request-set-mark gnus-newsgroup-name)
5255                    (not (gnus-article-unpropagatable-p (cdr type))))
5256           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5257                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5258                  (add (gnus-remove-from-range
5259                        (gnus-copy-sequence list) old)))
5260             (when add
5261               (push (list add 'add (list (cdr type))) delta-marks))
5262             (when del
5263               (push (list del 'del (list (cdr type))) delta-marks))))
5264
5265         (when list
5266           (push (cons (cdr type) list) newmarked)))
5267
5268       (when delta-marks
5269         (unless (gnus-check-group gnus-newsgroup-name)
5270           (error "Can't open server for %s" gnus-newsgroup-name))
5271         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5272
5273       ;; Enter these new marks into the info of the group.
5274       (if (nthcdr 3 info)
5275           (setcar (nthcdr 3 info) newmarked)
5276         ;; Add the marks lists to the end of the info.
5277         (when newmarked
5278           (setcdr (nthcdr 2 info) (list newmarked))))
5279
5280       ;; Cut off the end of the info if there's nothing else there.
5281       (let ((i 5))
5282         (while (and (> i 2)
5283                     (not (nth i info)))
5284           (when (nthcdr (decf i) info)
5285             (setcdr (nthcdr i info) nil)))))))
5286
5287 (defun gnus-set-mode-line (where)
5288   "Set the mode line of the article or summary buffers.
5289 If WHERE is `summary', the summary mode line format will be used."
5290   ;; Is this mode line one we keep updated?
5291   (when (and (memq where gnus-updated-mode-lines)
5292              (symbol-value
5293               (intern (format "gnus-%s-mode-line-format-spec" where))))
5294     (let (mode-string)
5295       (save-excursion
5296         ;; We evaluate this in the summary buffer since these
5297         ;; variables are buffer-local to that buffer.
5298         (set-buffer gnus-summary-buffer)
5299        ;; We bind all these variables that are used in the `eval' form
5300         ;; below.
5301         (let* ((mformat (symbol-value
5302                          (intern
5303                           (format "gnus-%s-mode-line-format-spec" where))))
5304                (gnus-tmp-group-name (gnus-group-decoded-name
5305                                      gnus-newsgroup-name))
5306                (gnus-tmp-article-number (or gnus-current-article 0))
5307                (gnus-tmp-unread gnus-newsgroup-unreads)
5308                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5309                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5310                (gnus-tmp-unread-and-unselected
5311                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5312                             (zerop gnus-tmp-unselected))
5313                        "")
5314                       ((zerop gnus-tmp-unselected)
5315                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5316                       (t (format "{%d(+%d) more}"
5317                                  gnus-tmp-unread-and-unticked
5318                                  gnus-tmp-unselected))))
5319                (gnus-tmp-subject
5320                 (if (and gnus-current-headers
5321                          (vectorp gnus-current-headers))
5322                     (gnus-mode-string-quote
5323                      (mail-header-subject gnus-current-headers))
5324                   ""))
5325                bufname-length max-len
5326                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5327           (setq mode-string (eval mformat))
5328           (setq bufname-length (if (string-match "%b" mode-string)
5329                                    (- (length
5330                                        (buffer-name
5331                                         (if (eq where 'summary)
5332                                             nil
5333                                           (get-buffer gnus-article-buffer))))
5334                                       2)
5335                                  0))
5336           (setq max-len (max 4 (if gnus-mode-non-string-length
5337                                    (- (window-width)
5338                                       gnus-mode-non-string-length
5339                                       bufname-length)
5340                                  (length mode-string))))
5341           ;; We might have to chop a bit of the string off...
5342           (when (> (length mode-string) max-len)
5343             (setq mode-string
5344                   (concat (truncate-string-to-width mode-string (- max-len 3))
5345                           "...")))
5346           ;; Pad the mode string a bit.
5347           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5348       ;; Update the mode line.
5349       (setq mode-line-buffer-identification
5350             (gnus-mode-line-buffer-identification (list mode-string)))
5351       (set-buffer-modified-p t))))
5352
5353 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5354   "Go through the HEADERS list and add all Xrefs to a hash table.
5355 The resulting hash table is returned, or nil if no Xrefs were found."
5356   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5357          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5358          (xref-hashtb (gnus-make-hashtable))
5359          start group entry number xrefs header)
5360     (while headers
5361       (setq header (pop headers))
5362       (when (and (setq xrefs (mail-header-xref header))
5363                  (not (memq (setq number (mail-header-number header))
5364                             unreads)))
5365         (setq start 0)
5366         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5367           (setq start (match-end 0))
5368           (setq group (if prefix
5369                           (concat prefix (substring xrefs (match-beginning 1)
5370                                                     (match-end 1)))
5371                         (substring xrefs (match-beginning 1) (match-end 1))))
5372           (setq number
5373                 (string-to-int (substring xrefs (match-beginning 2)
5374                                           (match-end 2))))
5375           (if (setq entry (gnus-gethash group xref-hashtb))
5376               (setcdr entry (cons number (cdr entry)))
5377             (gnus-sethash group (cons number nil) xref-hashtb)))))
5378     (and start xref-hashtb)))
5379
5380 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5381   "Look through all the headers and mark the Xrefs as read."
5382   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5383         name entry info xref-hashtb idlist method nth4)
5384     (save-excursion
5385       (set-buffer gnus-group-buffer)
5386       (when (setq xref-hashtb
5387                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5388         (mapatoms
5389          (lambda (group)
5390            (unless (string= from-newsgroup (setq name (symbol-name group)))
5391              (setq idlist (symbol-value group))
5392              ;; Dead groups are not updated.
5393              (and (prog1
5394                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5395                             info (nth 2 entry))
5396                     (when (stringp (setq nth4 (gnus-info-method info)))
5397                       (setq nth4 (gnus-server-to-method nth4))))
5398                   ;; Only do the xrefs if the group has the same
5399                   ;; select method as the group we have just read.
5400                   (or (gnus-methods-equal-p
5401                        nth4 (gnus-find-method-for-group from-newsgroup))
5402                       virtual
5403                       (equal nth4 (setq method (gnus-find-method-for-group
5404                                                 from-newsgroup)))
5405                       (and (equal (car nth4) (car method))
5406                            (equal (nth 1 nth4) (nth 1 method))))
5407                   gnus-use-cross-reference
5408                   (or (not (eq gnus-use-cross-reference t))
5409                       virtual
5410                       ;; Only do cross-references on subscribed
5411                       ;; groups, if that is what is wanted.
5412                       (<= (gnus-info-level info) gnus-level-subscribed))
5413                   (gnus-group-make-articles-read name idlist))))
5414          xref-hashtb)))))
5415
5416 (defun gnus-compute-read-articles (group articles)
5417   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5418          (info (nth 2 entry))
5419          (active (gnus-active group))
5420          ninfo)
5421     (when entry
5422       ;; First peel off all invalid article numbers.
5423       (when active
5424         (let ((ids articles)
5425               id first)
5426           (while (setq id (pop ids))
5427             (when (and first (> id (cdr active)))
5428               ;; We'll end up in this situation in one particular
5429               ;; obscure situation.  If you re-scan a group and get
5430               ;; a new article that is cross-posted to a different
5431               ;; group that has not been re-scanned, you might get
5432               ;; crossposted article that has a higher number than
5433               ;; Gnus believes possible.  So we re-activate this
5434               ;; group as well.  This might mean doing the
5435               ;; crossposting thingy will *increase* the number
5436               ;; of articles in some groups.  Tsk, tsk.
5437               (setq active (or (gnus-activate-group group) active)))
5438             (when (or (> id (cdr active))
5439                       (< id (car active)))
5440               (setq articles (delq id articles))))))
5441       ;; If the read list is nil, we init it.
5442       (if (and active
5443                (null (gnus-info-read info))
5444                (> (car active) 1))
5445           (setq ninfo (cons 1 (1- (car active))))
5446         (setq ninfo (gnus-info-read info)))
5447       ;; Then we add the read articles to the range.
5448       (gnus-add-to-range
5449        ninfo (setq articles (sort articles '<))))))
5450
5451 (defun gnus-group-make-articles-read (group articles)
5452   "Update the info of GROUP to say that ARTICLES are read."
5453   (let* ((num 0)
5454          (entry (gnus-gethash group gnus-newsrc-hashtb))
5455          (info (nth 2 entry))
5456          (active (gnus-active group))
5457          range)
5458     (when entry
5459       (setq range (gnus-compute-read-articles group articles))
5460       (save-excursion
5461         (set-buffer gnus-group-buffer)
5462         (gnus-undo-register
5463           `(progn
5464              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5465              (gnus-info-set-read ',info ',(gnus-info-read info))
5466              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5467              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5468              (gnus-group-update-group ,group t))))
5469       ;; Add the read articles to the range.
5470       (gnus-info-set-read info range)
5471       (gnus-request-set-mark group (list (list range 'add '(read))))
5472       ;; Then we have to re-compute how many unread
5473       ;; articles there are in this group.
5474       (when active
5475         (cond
5476          ((not range)
5477           (setq num (- (1+ (cdr active)) (car active))))
5478          ((not (listp (cdr range)))
5479           (setq num (- (cdr active) (- (1+ (cdr range))
5480                                        (car range)))))
5481          (t
5482           (while range
5483             (if (numberp (car range))
5484                 (setq num (1+ num))
5485               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5486             (setq range (cdr range)))
5487           (setq num (- (cdr active) num))))
5488         ;; Update the number of unread articles.
5489         (setcar entry num)
5490         ;; Update the group buffer.
5491         (gnus-group-update-group group t)))))
5492
5493 (defvar gnus-newsgroup-none-id 0)
5494
5495 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5496   (let ((cur nntp-server-buffer)
5497         (dependencies
5498          (or dependencies
5499              (save-excursion (set-buffer gnus-summary-buffer)
5500                              gnus-newsgroup-dependencies)))
5501         headers id end ref
5502         (mail-parse-charset gnus-newsgroup-charset)
5503         (mail-parse-ignored-charsets
5504          (save-excursion (condition-case nil
5505                              (set-buffer gnus-summary-buffer)
5506                            (error))
5507                          gnus-newsgroup-ignored-charsets)))
5508     (save-excursion
5509       (set-buffer nntp-server-buffer)
5510       ;; Translate all TAB characters into SPACE characters.
5511       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5512       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5513       (gnus-run-hooks 'gnus-parse-headers-hook)
5514       (let ((case-fold-search t)
5515             in-reply-to header p lines chars)
5516         (goto-char (point-min))
5517         ;; Search to the beginning of the next header.  Error messages
5518         ;; do not begin with 2 or 3.
5519         (while (re-search-forward "^[23][0-9]+ " nil t)
5520           (setq id nil
5521                 ref nil)
5522           ;; This implementation of this function, with nine
5523           ;; search-forwards instead of the one re-search-forward and
5524           ;; a case (which basically was the old function) is actually
5525           ;; about twice as fast, even though it looks messier.  You
5526           ;; can't have everything, I guess.  Speed and elegance
5527           ;; doesn't always go hand in hand.
5528           (setq
5529            header
5530            (vector
5531             ;; Number.
5532             (prog1
5533                 (read cur)
5534               (end-of-line)
5535               (setq p (point))
5536               (narrow-to-region (point)
5537                                 (or (and (search-forward "\n.\n" nil t)
5538                                          (- (point) 2))
5539                                     (point))))
5540             ;; Subject.
5541             (progn
5542               (goto-char p)
5543               (if (search-forward "\nsubject:" nil t)
5544                   (funcall gnus-decode-encoded-word-function
5545                            (nnheader-header-value))
5546                 "(none)"))
5547             ;; From.
5548             (progn
5549               (goto-char p)
5550               (if (search-forward "\nfrom:" nil t)
5551                   (funcall gnus-decode-encoded-word-function
5552                            (nnheader-header-value))
5553                 "(nobody)"))
5554             ;; Date.
5555             (progn
5556               (goto-char p)
5557               (if (search-forward "\ndate:" nil t)
5558                   (nnheader-header-value) ""))
5559             ;; Message-ID.
5560             (progn
5561               (goto-char p)
5562               (setq id (if (re-search-forward
5563                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5564                            ;; We do it this way to make sure the Message-ID
5565                            ;; is (somewhat) syntactically valid.
5566                            (buffer-substring (match-beginning 1)
5567                                              (match-end 1))
5568                          ;; If there was no message-id, we just fake one
5569                          ;; to make subsequent routines simpler.
5570                          (nnheader-generate-fake-message-id))))
5571             ;; References.
5572             (progn
5573               (goto-char p)
5574               (if (search-forward "\nreferences:" nil t)
5575                   (progn
5576                     (setq end (point))
5577                     (prog1
5578                         (nnheader-header-value)
5579                       (setq ref
5580                             (buffer-substring
5581                              (progn
5582                                (end-of-line)
5583                                (search-backward ">" end t)
5584                                (1+ (point)))
5585                              (progn
5586                                (search-backward "<" end t)
5587                                (point))))))
5588                 ;; Get the references from the in-reply-to header if there
5589                 ;; were no references and the in-reply-to header looks
5590                 ;; promising.
5591                 (if (and (search-forward "\nin-reply-to:" nil t)
5592                          (setq in-reply-to (nnheader-header-value))
5593                          (string-match "<[^>]+>" in-reply-to))
5594                     (let (ref2)
5595                       (setq ref (substring in-reply-to (match-beginning 0)
5596                                            (match-end 0)))
5597                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5598                         (setq ref2 (substring in-reply-to (match-beginning 0)
5599                                               (match-end 0)))
5600                         (when (> (length ref2) (length ref))
5601                           (setq ref ref2)))
5602                       ref)
5603                   (setq ref nil))))
5604             ;; Chars.
5605             (progn
5606               (goto-char p)
5607               (if (search-forward "\nchars: " nil t)
5608                   (if (numberp (setq chars (ignore-errors (read cur))))
5609                       chars -1)
5610                 -1))
5611             ;; Lines.
5612             (progn
5613               (goto-char p)
5614               (if (search-forward "\nlines: " nil t)
5615                   (if (numberp (setq lines (ignore-errors (read cur))))
5616                       lines -1)
5617                 -1))
5618             ;; Xref.
5619             (progn
5620               (goto-char p)
5621               (and (search-forward "\nxref:" nil t)
5622                    (nnheader-header-value)))
5623             ;; Extra.
5624             (when gnus-extra-headers
5625               (let ((extra gnus-extra-headers)
5626                     out)
5627                 (while extra
5628                   (goto-char p)
5629                   (when (search-forward
5630                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5631                     (push (cons (car extra) (nnheader-header-value))
5632                           out))
5633                   (pop extra))
5634                 out))))
5635           (when (equal id ref)
5636             (setq ref nil))
5637
5638           (when gnus-alter-header-function
5639             (funcall gnus-alter-header-function header)
5640             (setq id (mail-header-id header)
5641                   ref (gnus-parent-id (mail-header-references header))))
5642
5643           (when (setq header
5644                       (gnus-dependencies-add-header
5645                        header dependencies force-new))
5646             (push header headers))
5647           (goto-char (point-max))
5648           (widen))
5649         (nreverse headers)))))
5650
5651 ;; Goes through the xover lines and returns a list of vectors
5652 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5653                                                   force-new dependencies
5654                                                   group also-fetch-heads)
5655   "Parse the news overview data in the server buffer.
5656 Return a list of headers that match SEQUENCE (see
5657 `nntp-retrieve-headers')."
5658   ;; Get the Xref when the users reads the articles since most/some
5659   ;; NNTP servers do not include Xrefs when using XOVER.
5660   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5661   (let ((mail-parse-charset gnus-newsgroup-charset)
5662         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5663         (cur nntp-server-buffer)
5664         (dependencies (or dependencies gnus-newsgroup-dependencies))
5665         (allp (cond
5666                ((eq gnus-read-all-available-headers t)
5667                 t)
5668                ((stringp gnus-read-all-available-headers)
5669                 (string-match gnus-read-all-available-headers group))
5670                (t
5671                 nil)))
5672         number headers header)
5673     (save-excursion
5674       (set-buffer nntp-server-buffer)
5675       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5676       ;; Allow the user to mangle the headers before parsing them.
5677       (gnus-run-hooks 'gnus-parse-headers-hook)
5678       (goto-char (point-min))
5679       (gnus-parse-without-error
5680         (while (and (or sequence allp)
5681                     (not (eobp)))
5682           (setq number (read cur))
5683           (when (not allp)
5684             (while (and sequence
5685                         (< (car sequence) number))
5686               (setq sequence (cdr sequence))))
5687           (when (and (or allp
5688                          (and sequence
5689                               (eq number (car sequence))))
5690                      (progn
5691                        (setq sequence (cdr sequence))
5692                        (setq header (inline
5693                                       (gnus-nov-parse-line
5694                                        number dependencies force-new)))))
5695             (push header headers))
5696           (forward-line 1)))
5697       ;; A common bug in inn is that if you have posted an article and
5698       ;; then retrieves the active file, it will answer correctly --
5699       ;; the new article is included.  However, a NOV entry for the
5700       ;; article may not have been generated yet, so this may fail.
5701       ;; We work around this problem by retrieving the last few
5702       ;; headers using HEAD.
5703       (if (or (not also-fetch-heads)
5704               (not sequence))
5705           ;; We (probably) got all the headers.
5706           (nreverse headers)
5707         (let ((gnus-nov-is-evil t))
5708           (nconc
5709            (nreverse headers)
5710            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5711              (gnus-get-newsgroup-headers))))))))
5712
5713 (defun gnus-article-get-xrefs ()
5714   "Fill in the Xref value in `gnus-current-headers', if necessary.
5715 This is meant to be called in `gnus-article-internal-prepare-hook'."
5716   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5717                                  gnus-current-headers)))
5718     (or (not gnus-use-cross-reference)
5719         (not headers)
5720         (and (mail-header-xref headers)
5721              (not (string= (mail-header-xref headers) "")))
5722         (let ((case-fold-search t)
5723               xref)
5724           (save-restriction
5725             (nnheader-narrow-to-headers)
5726             (goto-char (point-min))
5727             (when (or (and (not (eobp))
5728                            (eq (downcase (char-after)) ?x)
5729                            (looking-at "Xref:"))
5730                       (search-forward "\nXref:" nil t))
5731               (goto-char (1+ (match-end 0)))
5732               (setq xref (buffer-substring (point)
5733                                            (progn (end-of-line) (point))))
5734               (mail-header-set-xref headers xref)))))))
5735
5736 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5737   "Find article ID and insert the summary line for that article.
5738 OLD-HEADER can either be a header or a line number to insert
5739 the subject line on."
5740   (let* ((line (and (numberp old-header) old-header))
5741          (old-header (and (vectorp old-header) old-header))
5742          (header (cond ((and old-header use-old-header)
5743                         old-header)
5744                        ((and (numberp id)
5745                              (gnus-number-to-header id))
5746                         (gnus-number-to-header id))
5747                        (t
5748                         (gnus-read-header id))))
5749          (number (and (numberp id) id))
5750          d)
5751     (when header
5752       ;; Rebuild the thread that this article is part of and go to the
5753       ;; article we have fetched.
5754       (when (and (not gnus-show-threads)
5755                  old-header)
5756         (when (and number
5757                    (setq d (gnus-data-find (mail-header-number old-header))))
5758           (goto-char (gnus-data-pos d))
5759           (gnus-data-remove
5760            number
5761            (- (gnus-point-at-bol)
5762               (prog1
5763                   (1+ (gnus-point-at-eol))
5764                 (gnus-delete-line))))))
5765       (when old-header
5766         (mail-header-set-number header (mail-header-number old-header)))
5767       (setq gnus-newsgroup-sparse
5768             (delq (setq number (mail-header-number header))
5769                   gnus-newsgroup-sparse))
5770       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5771       (push number gnus-newsgroup-limit)
5772       (gnus-rebuild-thread (mail-header-id header) line)
5773       (gnus-summary-goto-subject number nil t))
5774     (when (and (numberp number)
5775                (> number 0))
5776       ;; We have to update the boundaries even if we can't fetch the
5777       ;; article if ID is a number -- so that the next `P' or `N'
5778       ;; command will fetch the previous (or next) article even
5779       ;; if the one we tried to fetch this time has been canceled.
5780       (when (> number gnus-newsgroup-end)
5781         (setq gnus-newsgroup-end number))
5782       (when (< number gnus-newsgroup-begin)
5783         (setq gnus-newsgroup-begin number))
5784       (setq gnus-newsgroup-unselected
5785             (delq number gnus-newsgroup-unselected)))
5786     ;; Report back a success?
5787     (and header (mail-header-number header))))
5788
5789 ;;; Process/prefix in the summary buffer
5790
5791 (defun gnus-summary-work-articles (n)
5792   "Return a list of articles to be worked upon.
5793 The prefix argument, the list of process marked articles, and the
5794 current article will be taken into consideration."
5795   (save-excursion
5796     (set-buffer gnus-summary-buffer)
5797     (cond
5798      (n
5799       ;; A numerical prefix has been given.
5800       (setq n (prefix-numeric-value n))
5801       (let ((backward (< n 0))
5802             (n (abs (prefix-numeric-value n)))
5803             articles article)
5804         (save-excursion
5805           (while
5806               (and (> n 0)
5807                    (push (setq article (gnus-summary-article-number))
5808                          articles)
5809                    (if backward
5810                        (gnus-summary-find-prev nil article)
5811                      (gnus-summary-find-next nil article)))
5812             (decf n)))
5813         (nreverse articles)))
5814      ((and (gnus-region-active-p) (mark))
5815       (message "region active")
5816       ;; Work on the region between point and mark.
5817       (let ((max (max (point) (mark)))
5818             articles article)
5819         (save-excursion
5820           (goto-char (min (min (point) (mark))))
5821           (while
5822               (and
5823                (push (setq article (gnus-summary-article-number)) articles)
5824                (gnus-summary-find-next nil article)
5825                (< (point) max)))
5826           (nreverse articles))))
5827      (gnus-newsgroup-processable
5828       ;; There are process-marked articles present.
5829       ;; Save current state.
5830       (gnus-summary-save-process-mark)
5831       ;; Return the list.
5832       (reverse gnus-newsgroup-processable))
5833      (t
5834       ;; Just return the current article.
5835       (list (gnus-summary-article-number))))))
5836
5837 (defmacro gnus-summary-iterate (arg &rest forms)
5838   "Iterate over the process/prefixed articles and do FORMS.
5839 ARG is the interactive prefix given to the command.  FORMS will be
5840 executed with point over the summary line of the articles."
5841   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5842     `(let ((,articles (gnus-summary-work-articles ,arg)))
5843        (while ,articles
5844          (gnus-summary-goto-subject (car ,articles))
5845          ,@forms
5846          (pop ,articles)))))
5847
5848 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5849 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5850
5851 (defun gnus-summary-save-process-mark ()
5852   "Push the current set of process marked articles on the stack."
5853   (interactive)
5854   (push (copy-sequence gnus-newsgroup-processable)
5855         gnus-newsgroup-process-stack))
5856
5857 (defun gnus-summary-kill-process-mark ()
5858   "Push the current set of process marked articles on the stack and unmark."
5859   (interactive)
5860   (gnus-summary-save-process-mark)
5861   (gnus-summary-unmark-all-processable))
5862
5863 (defun gnus-summary-yank-process-mark ()
5864   "Pop the last process mark state off the stack and restore it."
5865   (interactive)
5866   (unless gnus-newsgroup-process-stack
5867     (error "Empty mark stack"))
5868   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5869
5870 (defun gnus-summary-process-mark-set (set)
5871   "Make SET into the current process marked articles."
5872   (gnus-summary-unmark-all-processable)
5873   (while set
5874     (gnus-summary-set-process-mark (pop set))))
5875
5876 ;;; Searching and stuff
5877
5878 (defun gnus-summary-search-group (&optional backward use-level)
5879   "Search for next unread newsgroup.
5880 If optional argument BACKWARD is non-nil, search backward instead."
5881   (save-excursion
5882     (set-buffer gnus-group-buffer)
5883     (when (gnus-group-search-forward
5884            backward nil (if use-level (gnus-group-group-level) nil))
5885       (gnus-group-group-name))))
5886
5887 (defun gnus-summary-best-group (&optional exclude-group)
5888   "Find the name of the best unread group.
5889 If EXCLUDE-GROUP, do not go to this group."
5890   (save-excursion
5891     (set-buffer gnus-group-buffer)
5892     (save-excursion
5893       (gnus-group-best-unread-group exclude-group))))
5894
5895 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5896   (if backward (gnus-summary-find-prev)
5897     (let* ((dummy (gnus-summary-article-intangible-p))
5898            (article (or article (gnus-summary-article-number)))
5899            (arts (gnus-data-find-list article))
5900            result)
5901       (when (and (not dummy)
5902                  (or (not gnus-summary-check-current)
5903                      (not unread)
5904                      (not (gnus-data-unread-p (car arts)))))
5905         (setq arts (cdr arts)))
5906       (when (setq result
5907                   (if unread
5908                       (progn
5909                         (while arts
5910                           (when (or (and undownloaded
5911                                          (eq gnus-undownloaded-mark
5912                                              (gnus-data-mark (car arts))))
5913                                     (gnus-data-unread-p (car arts)))
5914                             (setq result (car arts)
5915                                   arts nil))
5916                           (setq arts (cdr arts)))
5917                         result)
5918                     (car arts)))
5919         (goto-char (gnus-data-pos result))
5920         (gnus-data-number result)))))
5921
5922 (defun gnus-summary-find-prev (&optional unread article)
5923   (let* ((eobp (eobp))
5924          (article (or article (gnus-summary-article-number)))
5925          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5926          result)
5927     (when (and (not eobp)
5928                (or (not gnus-summary-check-current)
5929                    (not unread)
5930                    (not (gnus-data-unread-p (car arts)))))
5931       (setq arts (cdr arts)))
5932     (when (setq result
5933                 (if unread
5934                     (progn
5935                       (while arts
5936                         (when (gnus-data-unread-p (car arts))
5937                           (setq result (car arts)
5938                                 arts nil))
5939                         (setq arts (cdr arts)))
5940                       result)
5941                   (car arts)))
5942       (goto-char (gnus-data-pos result))
5943       (gnus-data-number result))))
5944
5945 (defun gnus-summary-find-subject (subject &optional unread backward article)
5946   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5947          (article (or article (gnus-summary-article-number)))
5948          (articles (gnus-data-list backward))
5949          (arts (gnus-data-find-list article articles))
5950          result)
5951     (when (or (not gnus-summary-check-current)
5952               (not unread)
5953               (not (gnus-data-unread-p (car arts))))
5954       (setq arts (cdr arts)))
5955     (while arts
5956       (and (or (not unread)
5957                (gnus-data-unread-p (car arts)))
5958            (vectorp (gnus-data-header (car arts)))
5959            (gnus-subject-equal
5960             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5961            (setq result (car arts)
5962                  arts nil))
5963       (setq arts (cdr arts)))
5964     (and result
5965          (goto-char (gnus-data-pos result))
5966          (gnus-data-number result))))
5967
5968 (defun gnus-summary-search-forward (&optional unread subject backward)
5969   "Search forward for an article.
5970 If UNREAD, look for unread articles.  If SUBJECT, look for
5971 articles with that subject.  If BACKWARD, search backward instead."
5972   (cond (subject (gnus-summary-find-subject subject unread backward))
5973         (backward (gnus-summary-find-prev unread))
5974         (t (gnus-summary-find-next unread))))
5975
5976 (defun gnus-recenter (&optional n)
5977   "Center point in window and redisplay frame.
5978 Also do horizontal recentering."
5979   (interactive "P")
5980   (when (and gnus-auto-center-summary
5981              (not (eq gnus-auto-center-summary 'vertical)))
5982     (gnus-horizontal-recenter))
5983   (recenter n))
5984
5985 (defun gnus-summary-recenter ()
5986   "Center point in the summary window.
5987 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5988 displayed, no centering will be performed."
5989   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5990 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5991   (interactive)
5992   (let* ((top (cond ((< (window-height) 4) 0)
5993                     ((< (window-height) 7) 1)
5994                     (t (if (numberp gnus-auto-center-summary)
5995                            gnus-auto-center-summary
5996                          2))))
5997          (height (1- (window-height)))
5998          (bottom (save-excursion (goto-char (point-max))
5999                                  (forward-line (- height))
6000                                  (point)))
6001          (window (get-buffer-window (current-buffer))))
6002     ;; The user has to want it.
6003     (when gnus-auto-center-summary
6004       (when (get-buffer-window gnus-article-buffer)
6005         ;; Only do recentering when the article buffer is displayed,
6006       ;; Set the window start to either `bottom', which is the biggest
6007         ;; possible valid number, or the second line from the top,
6008         ;; whichever is the least.
6009         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6010           (if (> bottom top-pos)
6011               ;; Keep the second line from the top visible
6012               (set-window-start window top-pos t)
6013             ;; Try to keep the bottom line visible; if it's partially
6014             ;; obscured, either scroll one more line to make it fully
6015             ;; visible, or revert to using TOP-POS.
6016             (save-excursion
6017               (goto-char (point-max))
6018               (forward-line -1)
6019               (let ((last-line-start (point)))
6020                 (goto-char bottom)
6021                 (set-window-start window (point) t)
6022                 (when (not (pos-visible-in-window-p last-line-start window))
6023                   (forward-line 1)
6024                   (set-window-start window (min (point) top-pos) t)))))))
6025       ;; Do horizontal recentering while we're at it.
6026       (when (and (get-buffer-window (current-buffer) t)
6027                  (not (eq gnus-auto-center-summary 'vertical)))
6028         (let ((selected (selected-window)))
6029           (select-window (get-buffer-window (current-buffer) t))
6030           (gnus-summary-position-point)
6031           (gnus-horizontal-recenter)
6032           (select-window selected))))))
6033
6034 (defun gnus-summary-jump-to-group (newsgroup)
6035   "Move point to NEWSGROUP in group mode buffer."
6036   ;; Keep update point of group mode buffer if visible.
6037   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6038       (save-window-excursion
6039         ;; Take care of tree window mode.
6040         (when (get-buffer-window gnus-group-buffer)
6041           (pop-to-buffer gnus-group-buffer))
6042         (gnus-group-jump-to-group newsgroup))
6043     (save-excursion
6044       ;; Take care of tree window mode.
6045       (if (get-buffer-window gnus-group-buffer)
6046           (pop-to-buffer gnus-group-buffer)
6047         (set-buffer gnus-group-buffer))
6048       (gnus-group-jump-to-group newsgroup))))
6049
6050 ;; This function returns a list of article numbers based on the
6051 ;; difference between the ranges of read articles in this group and
6052 ;; the range of active articles.
6053 (defun gnus-list-of-unread-articles (group)
6054   (let* ((read (gnus-info-read (gnus-get-info group)))
6055          (active (or (gnus-active group) (gnus-activate-group group)))
6056          (last (cdr active))
6057          first nlast unread)
6058     ;; If none are read, then all are unread.
6059     (if (not read)
6060         (setq first (car active))
6061       ;; If the range of read articles is a single range, then the
6062       ;; first unread article is the article after the last read
6063       ;; article.  Sounds logical, doesn't it?
6064       (if (and (not (listp (cdr read)))
6065                (or (< (car read) (car active))
6066                    (progn (setq read (list read))
6067                           nil)))
6068           (setq first (max (car active) (1+ (cdr read))))
6069         ;; `read' is a list of ranges.
6070         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6071                                   (caar read)))
6072                   1)
6073           (setq first (car active)))
6074         (while read
6075           (when first
6076             (while (< first nlast)
6077               (push first unread)
6078               (setq first (1+ first))))
6079           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6080           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6081           (setq read (cdr read)))))
6082     ;; And add the last unread articles.
6083     (while (<= first last)
6084       (push first unread)
6085       (setq first (1+ first)))
6086     ;; Return the list of unread articles.
6087     (delq 0 (nreverse unread))))
6088
6089 (defun gnus-list-of-read-articles (group)
6090   "Return a list of unread, unticked and non-dormant articles."
6091   (let* ((info (gnus-get-info group))
6092          (marked (gnus-info-marks info))
6093          (active (gnus-active group)))
6094     (and info active
6095          (gnus-list-range-difference
6096           (gnus-list-range-difference
6097            (gnus-sorted-complement
6098             (gnus-uncompress-range active)
6099             (gnus-list-of-unread-articles group))
6100            (cdr (assq 'dormant marked)))
6101           (cdr (assq 'tick marked))))))
6102
6103 ;; Various summary commands
6104
6105 (defun gnus-summary-select-article-buffer ()
6106   "Reconfigure windows to show article buffer."
6107   (interactive)
6108   (if (not (gnus-buffer-live-p gnus-article-buffer))
6109       (error "There is no article buffer for this summary buffer")
6110     (gnus-configure-windows 'article)
6111     (select-window (get-buffer-window gnus-article-buffer))))
6112
6113 (defun gnus-summary-universal-argument (arg)
6114   "Perform any operation on all articles that are process/prefixed."
6115   (interactive "P")
6116   (let ((articles (gnus-summary-work-articles arg))
6117         func article)
6118     (if (eq
6119          (setq
6120           func
6121           (key-binding
6122            (read-key-sequence
6123             (substitute-command-keys
6124              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6125          'undefined)
6126         (gnus-error 1 "Undefined key")
6127       (save-excursion
6128         (while articles
6129           (gnus-summary-goto-subject (setq article (pop articles)))
6130           (let (gnus-newsgroup-processable)
6131             (command-execute func))
6132           (gnus-summary-remove-process-mark article)))))
6133   (gnus-summary-position-point))
6134
6135 (defun gnus-summary-toggle-truncation (&optional arg)
6136   "Toggle truncation of summary lines.
6137 With arg, turn line truncation on iff arg is positive."
6138   (interactive "P")
6139   (setq truncate-lines
6140         (if (null arg) (not truncate-lines)
6141           (> (prefix-numeric-value arg) 0)))
6142   (redraw-display))
6143
6144 (defun gnus-summary-reselect-current-group (&optional all rescan)
6145   "Exit and then reselect the current newsgroup.
6146 The prefix argument ALL means to select all articles."
6147   (interactive "P")
6148   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6149     (error "Ephemeral groups can't be reselected"))
6150   (let ((current-subject (gnus-summary-article-number))
6151         (group gnus-newsgroup-name))
6152     (setq gnus-newsgroup-begin nil)
6153     (gnus-summary-exit)
6154     ;; We have to adjust the point of group mode buffer because
6155     ;; point was moved to the next unread newsgroup by exiting.
6156     (gnus-summary-jump-to-group group)
6157     (when rescan
6158       (save-excursion
6159         (gnus-group-get-new-news-this-group 1)))
6160     (gnus-group-read-group all t)
6161     (gnus-summary-goto-subject current-subject nil t)))
6162
6163 (defun gnus-summary-rescan-group (&optional all)
6164   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6165   (interactive "P")
6166   (gnus-summary-reselect-current-group all t))
6167
6168 (defun gnus-summary-update-info (&optional non-destructive)
6169   (save-excursion
6170     (let ((group gnus-newsgroup-name))
6171       (when group
6172         (when gnus-newsgroup-kill-headers
6173           (setq gnus-newsgroup-killed
6174                 (gnus-compress-sequence
6175                  (gnus-sorted-union
6176                   (gnus-list-range-intersection
6177                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6178                   gnus-newsgroup-unreads)
6179                  t)))
6180         (unless (listp (cdr gnus-newsgroup-killed))
6181           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6182         (let ((headers gnus-newsgroup-headers))
6183           ;; Set the new ranges of read articles.
6184           (save-excursion
6185             (set-buffer gnus-group-buffer)
6186             (gnus-undo-force-boundary))
6187           (gnus-update-read-articles
6188            group (gnus-sorted-union
6189                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6190           ;; Set the current article marks.
6191           (let ((gnus-newsgroup-scored
6192                  (if (and (not gnus-save-score)
6193                           (not non-destructive))
6194                      nil
6195                    gnus-newsgroup-scored)))
6196             (save-excursion
6197               (gnus-update-marks)))
6198           ;; Do the cross-ref thing.
6199           (when gnus-use-cross-reference
6200             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6201           ;; Do not switch windows but change the buffer to work.
6202           (set-buffer gnus-group-buffer)
6203           (unless (gnus-ephemeral-group-p group)
6204             (gnus-group-update-group group)))))))
6205
6206 (defun gnus-summary-save-newsrc (&optional force)
6207   "Save the current number of read/marked articles in the dribble buffer.
6208 The dribble buffer will then be saved.
6209 If FORCE (the prefix), also save the .newsrc file(s)."
6210   (interactive "P")
6211   (gnus-summary-update-info t)
6212   (if force
6213       (gnus-save-newsrc-file)
6214     (gnus-dribble-save)))
6215
6216 (defun gnus-summary-exit (&optional temporary)
6217   "Exit reading current newsgroup, and then return to group selection mode.
6218 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6219   (interactive)
6220   (gnus-set-global-variables)
6221   (when (gnus-buffer-live-p gnus-article-buffer)
6222     (save-excursion
6223       (set-buffer gnus-article-buffer)
6224       (mm-destroy-parts gnus-article-mime-handles)
6225       ;; Set it to nil for safety reason.
6226       (setq gnus-article-mime-handle-alist nil)
6227       (setq gnus-article-mime-handles nil)))
6228   (gnus-kill-save-kill-buffer)
6229   (gnus-async-halt-prefetch)
6230   (let* ((group gnus-newsgroup-name)
6231          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6232          (mode major-mode)
6233          (group-point nil)
6234          (buf (current-buffer)))
6235     (unless quit-config
6236       ;; Do adaptive scoring, and possibly save score files.
6237       (when gnus-newsgroup-adaptive
6238         (gnus-score-adaptive))
6239       (when gnus-use-scoring
6240         (gnus-score-save)))
6241     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6242     ;; If we have several article buffers, we kill them at exit.
6243     (unless gnus-single-article-buffer
6244       (gnus-kill-buffer gnus-original-article-buffer)
6245       (setq gnus-article-current nil))
6246     (when gnus-use-cache
6247       (gnus-cache-possibly-remove-articles)
6248       (gnus-cache-save-buffers))
6249     (gnus-async-prefetch-remove-group group)
6250     (when gnus-suppress-duplicates
6251       (gnus-dup-enter-articles))
6252     (when gnus-use-trees
6253       (gnus-tree-close group))
6254     (when gnus-use-cache
6255       (gnus-cache-write-active))
6256     ;; Remove entries for this group.
6257     (nnmail-purge-split-history (gnus-group-real-name group))
6258     ;; Make all changes in this group permanent.
6259     (unless quit-config
6260       (gnus-run-hooks 'gnus-exit-group-hook)
6261       (gnus-summary-update-info))
6262     (gnus-close-group group)
6263     ;; Make sure where we were, and go to next newsgroup.
6264     (set-buffer gnus-group-buffer)
6265     (unless quit-config
6266       (gnus-group-jump-to-group group))
6267     (gnus-run-hooks 'gnus-summary-exit-hook)
6268     (unless (or quit-config
6269                 ;; If this group has disappeared from the summary
6270                 ;; buffer, don't skip forwards.
6271                 (not (string= group (gnus-group-group-name))))
6272       (gnus-group-next-unread-group 1))
6273     (setq group-point (point))
6274     (if temporary
6275         nil                             ;Nothing to do.
6276       ;; If we have several article buffers, we kill them at exit.
6277       (unless gnus-single-article-buffer
6278         (gnus-kill-buffer gnus-article-buffer)
6279         (gnus-kill-buffer gnus-original-article-buffer)
6280         (setq gnus-article-current nil))
6281       (set-buffer buf)
6282       (if (not gnus-kill-summary-on-exit)
6283           (progn
6284             (gnus-deaden-summary)
6285             (setq mode nil))
6286        ;; We set all buffer-local variables to nil.  It is unclear why
6287         ;; this is needed, but if we don't, buffer-local variables are
6288         ;; not garbage-collected, it seems.  This would the lead to en
6289         ;; ever-growing Emacs.
6290         (gnus-summary-clear-local-variables)
6291         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6292           (gnus-summary-clear-local-variables))
6293         (when (get-buffer gnus-article-buffer)
6294           (bury-buffer gnus-article-buffer))
6295         ;; We clear the global counterparts of the buffer-local
6296         ;; variables as well, just to be on the safe side.
6297         (set-buffer gnus-group-buffer)
6298         (gnus-summary-clear-local-variables)
6299         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6300           (gnus-summary-clear-local-variables)))
6301       (setq gnus-current-select-method gnus-select-method)
6302       (pop-to-buffer gnus-group-buffer)
6303       (if (not quit-config)
6304           (progn
6305             (goto-char group-point)
6306             (gnus-configure-windows 'group 'force))
6307         (gnus-handle-ephemeral-exit quit-config))
6308       ;; Return to group mode buffer.
6309       (when (eq mode 'gnus-summary-mode)
6310         (gnus-kill-buffer buf))
6311       ;; Clear the current group name.
6312       (unless quit-config
6313         (setq gnus-newsgroup-name nil)))))
6314
6315 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6316 (defun gnus-summary-exit-no-update (&optional no-questions)
6317   "Quit reading current newsgroup without updating read article info."
6318   (interactive)
6319   (let* ((group gnus-newsgroup-name)
6320          (quit-config (gnus-group-quit-config group)))
6321     (when (or no-questions
6322               gnus-expert-user
6323               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6324       (gnus-async-halt-prefetch)
6325       (mapcar 'funcall
6326               (delq 'gnus-summary-expire-articles
6327                     (copy-sequence gnus-summary-prepare-exit-hook)))
6328       (when (gnus-buffer-live-p gnus-article-buffer)
6329         (save-excursion
6330           (set-buffer gnus-article-buffer)
6331           (mm-destroy-parts gnus-article-mime-handles)
6332           ;; Set it to nil for safety reason.
6333           (setq gnus-article-mime-handle-alist nil)
6334           (setq gnus-article-mime-handles nil)))
6335       ;; If we have several article buffers, we kill them at exit.
6336       (unless gnus-single-article-buffer
6337         (gnus-kill-buffer gnus-article-buffer)
6338         (gnus-kill-buffer gnus-original-article-buffer)
6339         (setq gnus-article-current nil))
6340       (if (not gnus-kill-summary-on-exit)
6341           (gnus-deaden-summary)
6342         (gnus-close-group group)
6343         (gnus-summary-clear-local-variables)
6344         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6345           (gnus-summary-clear-local-variables))
6346         (set-buffer gnus-group-buffer)
6347         (gnus-summary-clear-local-variables)
6348         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6349           (gnus-summary-clear-local-variables))
6350         (when (get-buffer gnus-summary-buffer)
6351           (kill-buffer gnus-summary-buffer)))
6352       (unless gnus-single-article-buffer
6353         (setq gnus-article-current nil))
6354       (when gnus-use-trees
6355         (gnus-tree-close group))
6356       (gnus-async-prefetch-remove-group group)
6357       (when (get-buffer gnus-article-buffer)
6358         (bury-buffer gnus-article-buffer))
6359       ;; Return to the group buffer.
6360       (gnus-configure-windows 'group 'force)
6361       ;; Clear the current group name.
6362       (setq gnus-newsgroup-name nil)
6363       (when (equal (gnus-group-group-name) group)
6364         (gnus-group-next-unread-group 1))
6365       (when quit-config
6366         (gnus-handle-ephemeral-exit quit-config)))))
6367
6368 (defun gnus-handle-ephemeral-exit (quit-config)
6369   "Handle movement when leaving an ephemeral group.
6370 The state which existed when entering the ephemeral is reset."
6371   (if (not (buffer-name (car quit-config)))
6372       (gnus-configure-windows 'group 'force)
6373     (set-buffer (car quit-config))
6374     (cond ((eq major-mode 'gnus-summary-mode)
6375            (gnus-set-global-variables))
6376           ((eq major-mode 'gnus-article-mode)
6377            (save-excursion
6378              ;; The `gnus-summary-buffer' variable may point
6379              ;; to the old summary buffer when using a single
6380              ;; article buffer.
6381              (unless (gnus-buffer-live-p gnus-summary-buffer)
6382                (set-buffer gnus-group-buffer))
6383              (set-buffer gnus-summary-buffer)
6384              (gnus-set-global-variables))))
6385     (if (or (eq (cdr quit-config) 'article)
6386             (eq (cdr quit-config) 'pick))
6387         (progn
6388           ;; The current article may be from the ephemeral group
6389           ;; thus it is best that we reload this article
6390           (gnus-summary-show-article)
6391           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6392               (gnus-configure-windows 'pick 'force)
6393             (gnus-configure-windows (cdr quit-config) 'force)))
6394       (gnus-configure-windows (cdr quit-config) 'force))
6395     (when (eq major-mode 'gnus-summary-mode)
6396       (gnus-summary-next-subject 1 nil t)
6397       (gnus-summary-recenter)
6398       (gnus-summary-position-point))))
6399
6400 ;;; Dead summaries.
6401
6402 (defvar gnus-dead-summary-mode-map nil)
6403
6404 (unless gnus-dead-summary-mode-map
6405   (setq gnus-dead-summary-mode-map (make-keymap))
6406   (suppress-keymap gnus-dead-summary-mode-map)
6407   (substitute-key-definition
6408    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6409   (dolist (key '("\C-d" "\r" "\177" [delete]))
6410     (define-key gnus-dead-summary-mode-map
6411       key 'gnus-summary-wake-up-the-dead))
6412   (dolist (key '("q" "Q"))
6413     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6414
6415 (defvar gnus-dead-summary-mode nil
6416   "Minor mode for Gnus summary buffers.")
6417
6418 (defun gnus-dead-summary-mode (&optional arg)
6419   "Minor mode for Gnus summary buffers."
6420   (interactive "P")
6421   (when (eq major-mode 'gnus-summary-mode)
6422     (make-local-variable 'gnus-dead-summary-mode)
6423     (setq gnus-dead-summary-mode
6424           (if (null arg) (not gnus-dead-summary-mode)
6425             (> (prefix-numeric-value arg) 0)))
6426     (when gnus-dead-summary-mode
6427       (gnus-add-minor-mode
6428        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6429
6430 (defun gnus-deaden-summary ()
6431   "Make the current summary buffer into a dead summary buffer."
6432   ;; Kill any previous dead summary buffer.
6433   (when (and gnus-dead-summary
6434              (buffer-name gnus-dead-summary))
6435     (save-excursion
6436       (set-buffer gnus-dead-summary)
6437       (when gnus-dead-summary-mode
6438         (kill-buffer (current-buffer)))))
6439   ;; Make this the current dead summary.
6440   (setq gnus-dead-summary (current-buffer))
6441   (gnus-dead-summary-mode 1)
6442   (let ((name (buffer-name)))
6443     (when (string-match "Summary" name)
6444       (rename-buffer
6445        (concat (substring name 0 (match-beginning 0)) "Dead "
6446                (substring name (match-beginning 0)))
6447        t)
6448       (bury-buffer))))
6449
6450 (defun gnus-kill-or-deaden-summary (buffer)
6451   "Kill or deaden the summary BUFFER."
6452   (save-excursion
6453     (when (and (buffer-name buffer)
6454                (not gnus-single-article-buffer))
6455       (save-excursion
6456         (set-buffer buffer)
6457         (gnus-kill-buffer gnus-article-buffer)
6458         (gnus-kill-buffer gnus-original-article-buffer)))
6459     (cond
6460      ;; Kill the buffer.
6461      (gnus-kill-summary-on-exit
6462       (when (and gnus-use-trees
6463                  (gnus-buffer-exists-p buffer))
6464         (save-excursion
6465           (set-buffer buffer)
6466           (gnus-tree-close gnus-newsgroup-name)))
6467       (gnus-kill-buffer buffer))
6468      ;; Deaden the buffer.
6469      ((gnus-buffer-exists-p buffer)
6470       (save-excursion
6471         (set-buffer buffer)
6472         (gnus-deaden-summary))))))
6473
6474 (defun gnus-summary-wake-up-the-dead (&rest args)
6475   "Wake up the dead summary buffer."
6476   (interactive)
6477   (gnus-dead-summary-mode -1)
6478   (let ((name (buffer-name)))
6479     (when (string-match "Dead " name)
6480       (rename-buffer
6481        (concat (substring name 0 (match-beginning 0))
6482                (substring name (match-end 0)))
6483        t)))
6484   (gnus-message 3 "This dead summary is now alive again"))
6485
6486 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6487 (defun gnus-summary-fetch-faq (&optional faq-dir)
6488   "Fetch the FAQ for the current group.
6489 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6490 in."
6491   (interactive
6492    (list
6493     (when current-prefix-arg
6494       (completing-read
6495        "Faq dir: " (and (listp gnus-group-faq-directory)
6496                         (mapcar (lambda (file) (list file))
6497                                 gnus-group-faq-directory))))))
6498   (let (gnus-faq-buffer)
6499     (when (setq gnus-faq-buffer
6500                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6501       (gnus-configure-windows 'summary-faq))))
6502
6503 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6504 (defun gnus-summary-describe-group (&optional force)
6505   "Describe the current newsgroup."
6506   (interactive "P")
6507   (gnus-group-describe-group force gnus-newsgroup-name))
6508
6509 (defun gnus-summary-describe-briefly ()
6510   "Describe summary mode commands briefly."
6511   (interactive)
6512   (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")))
6513
6514 ;; Walking around group mode buffer from summary mode.
6515
6516 (defun gnus-summary-next-group (&optional no-article target-group backward)
6517   "Exit current newsgroup and then select next unread newsgroup.
6518 If prefix argument NO-ARTICLE is non-nil, no article is selected
6519 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6520 previous group instead."
6521   (interactive "P")
6522   ;; Stop pre-fetching.
6523   (gnus-async-halt-prefetch)
6524   (let ((current-group gnus-newsgroup-name)
6525         (current-buffer (current-buffer))
6526         entered)
6527    ;; First we semi-exit this group to update Xrefs and all variables.
6528     ;; We can't do a real exit, because the window conf must remain
6529     ;; the same in case the user is prompted for info, and we don't
6530     ;; want the window conf to change before that...
6531     (gnus-summary-exit t)
6532     (while (not entered)
6533       ;; Then we find what group we are supposed to enter.
6534       (set-buffer gnus-group-buffer)
6535       (gnus-group-jump-to-group current-group)
6536       (setq target-group
6537             (or target-group
6538                 (if (eq gnus-keep-same-level 'best)
6539                     (gnus-summary-best-group gnus-newsgroup-name)
6540                   (gnus-summary-search-group backward gnus-keep-same-level))))
6541       (if (not target-group)
6542           ;; There are no further groups, so we return to the group
6543           ;; buffer.
6544           (progn
6545             (gnus-message 5 "Returning to the group buffer")
6546             (setq entered t)
6547             (when (gnus-buffer-live-p current-buffer)
6548               (set-buffer current-buffer)
6549               (gnus-summary-exit))
6550             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6551         ;; We try to enter the target group.
6552         (gnus-group-jump-to-group target-group)
6553         (let ((unreads (gnus-group-group-unread)))
6554           (if (and (or (eq t unreads)
6555                        (and unreads (not (zerop unreads))))
6556                    (gnus-summary-read-group
6557                     target-group nil no-article
6558                     (and (buffer-name current-buffer) current-buffer)
6559                     nil backward))
6560               (setq entered t)
6561             (setq current-group target-group
6562                   target-group nil)))))))
6563
6564 (defun gnus-summary-prev-group (&optional no-article)
6565   "Exit current newsgroup and then select previous unread newsgroup.
6566 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6567   (interactive "P")
6568   (gnus-summary-next-group no-article nil t))
6569
6570 ;; Walking around summary lines.
6571
6572 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6573   "Go to the first unread subject.
6574 If UNREAD is non-nil, go to the first unread article.
6575 Returns the article selected or nil if there are no unread articles."
6576   (interactive "P")
6577   (prog1
6578       (cond
6579        ;; Empty summary.
6580        ((null gnus-newsgroup-data)
6581         (gnus-message 3 "No articles in the group")
6582         nil)
6583        ;; Pick the first article.
6584        ((not unread)
6585         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6586         (gnus-data-number (car gnus-newsgroup-data)))
6587        ;; No unread articles.
6588        ((null gnus-newsgroup-unreads)
6589         (gnus-message 3 "No more unread articles")
6590         nil)
6591        ;; Find the first unread article.
6592        (t
6593         (let ((data gnus-newsgroup-data))
6594           (while (and data
6595                       (and (not (and undownloaded
6596                                      (eq gnus-undownloaded-mark
6597                                          (gnus-data-mark (car data)))))
6598                            (if unseen
6599                                (or (not (memq
6600                                          (gnus-data-number (car data))
6601                                          gnus-newsgroup-unseen))
6602                                    (not (gnus-data-unread-p (car data))))
6603                              (not (gnus-data-unread-p (car data))))))
6604             (setq data (cdr data)))
6605           (when data
6606             (goto-char (gnus-data-pos (car data)))
6607             (gnus-data-number (car data))))))
6608     (gnus-summary-position-point)))
6609
6610 (defun gnus-summary-next-subject (n &optional unread dont-display)
6611   "Go to next N'th summary line.
6612 If N is negative, go to the previous N'th subject line.
6613 If UNREAD is non-nil, only unread articles are selected.
6614 The difference between N and the actual number of steps taken is
6615 returned."
6616   (interactive "p")
6617   (let ((backward (< n 0))
6618         (n (abs n)))
6619     (while (and (> n 0)
6620                 (if backward
6621                     (gnus-summary-find-prev unread)
6622                   (gnus-summary-find-next unread)))
6623       (unless (zerop (setq n (1- n)))
6624         (gnus-summary-show-thread)))
6625     (when (/= 0 n)
6626       (gnus-message 7 "No more%s articles"
6627                     (if unread " unread" "")))
6628     (unless dont-display
6629       (gnus-summary-recenter)
6630       (gnus-summary-position-point))
6631     n))
6632
6633 (defun gnus-summary-next-unread-subject (n)
6634   "Go to next N'th unread summary line."
6635   (interactive "p")
6636   (gnus-summary-next-subject n t))
6637
6638 (defun gnus-summary-prev-subject (n &optional unread)
6639   "Go to previous N'th summary line.
6640 If optional argument UNREAD is non-nil, only unread article is selected."
6641   (interactive "p")
6642   (gnus-summary-next-subject (- n) unread))
6643
6644 (defun gnus-summary-prev-unread-subject (n)
6645   "Go to previous N'th unread summary line."
6646   (interactive "p")
6647   (gnus-summary-next-subject (- n) t))
6648
6649 (defun gnus-summary-goto-subject (article &optional force silent)
6650   "Go the subject line of ARTICLE.
6651 If FORCE, also allow jumping to articles not currently shown."
6652   (interactive "nArticle number: ")
6653   (unless (numberp article)
6654     (error "Article %s is not a number" article))
6655   (let ((b (point))
6656         (data (gnus-data-find article)))
6657     ;; We read in the article if we have to.
6658     (and (not data)
6659          force
6660          (gnus-summary-insert-subject
6661           article
6662           (if (or (numberp force) (vectorp force)) force)
6663           t)
6664          (setq data (gnus-data-find article)))
6665     (goto-char b)
6666     (if (not data)
6667         (progn
6668           (unless silent
6669             (gnus-message 3 "Can't find article %d" article))
6670           nil)
6671       (let ((pt (gnus-data-pos data)))
6672         (goto-char pt)
6673         (gnus-summary-set-article-display-arrow pt))
6674       (gnus-summary-position-point)
6675       article)))
6676
6677 ;; Walking around summary lines with displaying articles.
6678
6679 (defun gnus-summary-expand-window (&optional arg)
6680   "Make the summary buffer take up the entire Emacs frame.
6681 Given a prefix, will force an `article' buffer configuration."
6682   (interactive "P")
6683   (if arg
6684       (gnus-configure-windows 'article 'force)
6685     (gnus-configure-windows 'summary 'force)))
6686
6687 (defun gnus-summary-display-article (article &optional all-header)
6688   "Display ARTICLE in article buffer."
6689   (when (gnus-buffer-live-p gnus-article-buffer)
6690     (with-current-buffer gnus-article-buffer
6691       (mm-enable-multibyte)))
6692   (gnus-set-global-variables)
6693   (when (gnus-buffer-live-p gnus-article-buffer)
6694     (with-current-buffer gnus-article-buffer
6695       (setq gnus-article-charset gnus-newsgroup-charset)
6696       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6697       (mm-enable-multibyte)))
6698   (if (null article)
6699       nil
6700     (prog1
6701         (if gnus-summary-display-article-function
6702             (funcall gnus-summary-display-article-function article all-header)
6703           (gnus-article-prepare article all-header))
6704       (gnus-run-hooks 'gnus-select-article-hook)
6705       (when (and gnus-current-article
6706                  (not (zerop gnus-current-article)))
6707         (gnus-summary-goto-subject gnus-current-article))
6708       (gnus-summary-recenter)
6709       (when (and gnus-use-trees gnus-show-threads)
6710         (gnus-possibly-generate-tree article)
6711         (gnus-highlight-selected-tree article))
6712       ;; Successfully display article.
6713       (gnus-article-set-window-start
6714        (cdr (assq article gnus-newsgroup-bookmarks))))))
6715
6716 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6717   "Select the current article.
6718 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6719 non-nil, the article will be re-fetched even if it already present in
6720 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6721 be displayed."
6722   ;; Make sure we are in the summary buffer to work around bbdb bug.
6723   (unless (eq major-mode 'gnus-summary-mode)
6724     (set-buffer gnus-summary-buffer))
6725   (let ((article (or article (gnus-summary-article-number)))
6726         (all-headers (not (not all-headers))) ;Must be t or nil.
6727         gnus-summary-display-article-function)
6728     (and (not pseudo)
6729          (gnus-summary-article-pseudo-p article)
6730          (error "This is a pseudo-article"))
6731     (save-excursion
6732       (set-buffer gnus-summary-buffer)
6733       (if (or (and gnus-single-article-buffer
6734                    (or (null gnus-current-article)
6735                        (null gnus-article-current)
6736                        (null (get-buffer gnus-article-buffer))
6737                        (not (eq article (cdr gnus-article-current)))
6738                        (not (equal (car gnus-article-current)
6739                                    gnus-newsgroup-name))))
6740               (and (not gnus-single-article-buffer)
6741                    (or (null gnus-current-article)
6742                        (not (eq gnus-current-article article))))
6743               force)
6744           ;; The requested article is different from the current article.
6745           (progn
6746             (gnus-summary-display-article article all-headers)
6747             (when (gnus-buffer-live-p gnus-article-buffer)
6748               (with-current-buffer gnus-article-buffer
6749                 (if (not gnus-article-decoded-p) ;; a local variable
6750                     (mm-disable-multibyte))))
6751             (gnus-article-set-window-start
6752              (cdr (assq article gnus-newsgroup-bookmarks)))
6753             article)
6754         'old))))
6755
6756 (defun gnus-summary-force-verify-and-decrypt ()
6757   (interactive)
6758   (let ((mm-verify-option 'known)
6759         (mm-decrypt-option 'known)
6760         (gnus-buttonized-mime-types (append (list "multipart/signed"
6761                                                   "multipart/encrypted")
6762                                             gnus-buttonized-mime-types)))
6763     (gnus-summary-select-article nil 'force)))
6764
6765 (defun gnus-summary-set-current-mark (&optional current-mark)
6766   "Obsolete function."
6767   nil)
6768
6769 (defun gnus-summary-next-article (&optional unread subject backward push)
6770   "Select the next article.
6771 If UNREAD, only unread articles are selected.
6772 If SUBJECT, only articles with SUBJECT are selected.
6773 If BACKWARD, the previous article is selected instead of the next."
6774   (interactive "P")
6775   (cond
6776    ;; Is there such an article?
6777    ((and (gnus-summary-search-forward unread subject backward)
6778          (or (gnus-summary-display-article (gnus-summary-article-number))
6779              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6780     (gnus-summary-position-point))
6781    ;; If not, we try the first unread, if that is wanted.
6782    ((and subject
6783          gnus-auto-select-same
6784          (gnus-summary-first-unread-article))
6785     (gnus-summary-position-point)
6786     (gnus-message 6 "Wrapped"))
6787    ;; Try to get next/previous article not displayed in this group.
6788    ((and gnus-auto-extend-newsgroup
6789          (not unread) (not subject))
6790     (gnus-summary-goto-article
6791      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6792      nil (count-lines (point-min) (point))))
6793    ;; Go to next/previous group.
6794    (t
6795     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6796       (gnus-summary-jump-to-group gnus-newsgroup-name))
6797     (let ((cmd last-command-char)
6798           (point
6799            (save-excursion
6800              (set-buffer gnus-group-buffer)
6801              (point)))
6802           (group
6803            (if (eq gnus-keep-same-level 'best)
6804                (gnus-summary-best-group gnus-newsgroup-name)
6805              (gnus-summary-search-group backward gnus-keep-same-level))))
6806       ;; For some reason, the group window gets selected.  We change
6807       ;; it back.
6808       (select-window (get-buffer-window (current-buffer)))
6809       ;; Select next unread newsgroup automagically.
6810       (cond
6811        ((or (not gnus-auto-select-next)
6812             (not cmd))
6813         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6814        ((or (eq gnus-auto-select-next 'quietly)
6815             (and (eq gnus-auto-select-next 'slightly-quietly)
6816                  push)
6817             (and (eq gnus-auto-select-next 'almost-quietly)
6818                  (gnus-summary-last-article-p)))
6819         ;; Select quietly.
6820         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6821             (gnus-summary-exit)
6822           (gnus-message 7 "No more%s articles (%s)..."
6823                         (if unread " unread" "")
6824                         (if group (concat "selecting " group)
6825                           "exiting"))
6826           (gnus-summary-next-group nil group backward)))
6827        (t
6828         (when (gnus-key-press-event-p last-input-event)
6829           (gnus-summary-walk-group-buffer
6830            gnus-newsgroup-name cmd unread backward point))))))))
6831
6832 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6833   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6834                       (?\C-p (gnus-group-prev-unread-group 1))))
6835         (cursor-in-echo-area t)
6836         keve key group ended)
6837     (save-excursion
6838       (set-buffer gnus-group-buffer)
6839       (goto-char start)
6840       (setq group
6841             (if (eq gnus-keep-same-level 'best)
6842                 (gnus-summary-best-group gnus-newsgroup-name)
6843               (gnus-summary-search-group backward gnus-keep-same-level))))
6844     (while (not ended)
6845       (gnus-message
6846        5 "No more%s articles%s" (if unread " unread" "")
6847        (if (and group
6848                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6849            (format " (Type %s for %s [%s])"
6850                    (single-key-description cmd) group
6851                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6852          (format " (Type %s to exit %s)"
6853                  (single-key-description cmd)
6854                  gnus-newsgroup-name)))
6855       ;; Confirm auto selection.
6856       (setq key (car (setq keve (gnus-read-event-char))))
6857       (setq ended t)
6858       (cond
6859        ((assq key keystrokes)
6860         (let ((obuf (current-buffer)))
6861           (switch-to-buffer gnus-group-buffer)
6862           (when group
6863             (gnus-group-jump-to-group group))
6864           (eval (cadr (assq key keystrokes)))
6865           (setq group (gnus-group-group-name))
6866           (switch-to-buffer obuf))
6867         (setq ended nil))
6868        ((equal key cmd)
6869         (if (or (not group)
6870                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6871             (gnus-summary-exit)
6872           (gnus-summary-next-group nil group backward)))
6873        (t
6874         (push (cdr keve) unread-command-events))))))
6875
6876 (defun gnus-summary-next-unread-article ()
6877   "Select unread article after current one."
6878   (interactive)
6879   (gnus-summary-next-article
6880    (or (not (eq gnus-summary-goto-unread 'never))
6881        (gnus-summary-last-article-p (gnus-summary-article-number)))
6882    (and gnus-auto-select-same
6883         (gnus-summary-article-subject))))
6884
6885 (defun gnus-summary-prev-article (&optional unread subject)
6886   "Select the article after the current one.
6887 If UNREAD is non-nil, only unread articles are selected."
6888   (interactive "P")
6889   (gnus-summary-next-article unread subject t))
6890
6891 (defun gnus-summary-prev-unread-article ()
6892   "Select unread article before current one."
6893   (interactive)
6894   (gnus-summary-prev-article
6895    (or (not (eq gnus-summary-goto-unread 'never))
6896        (gnus-summary-first-article-p (gnus-summary-article-number)))
6897    (and gnus-auto-select-same
6898         (gnus-summary-article-subject))))
6899
6900 (defun gnus-summary-next-page (&optional lines circular)
6901   "Show next page of the selected article.
6902 If at the end of the current article, select the next article.
6903 LINES says how many lines should be scrolled up.
6904
6905 If CIRCULAR is non-nil, go to the start of the article instead of
6906 selecting the next article when reaching the end of the current
6907 article."
6908   (interactive "P")
6909   (setq gnus-summary-buffer (current-buffer))
6910   (gnus-set-global-variables)
6911   (let ((article (gnus-summary-article-number))
6912         (article-window (get-buffer-window gnus-article-buffer t))
6913         endp)
6914     ;; If the buffer is empty, we have no article.
6915     (unless article
6916       (error "No article to select"))
6917     (gnus-configure-windows 'article)
6918     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6919         (if (and (eq gnus-summary-goto-unread 'never)
6920                  (not (gnus-summary-last-article-p article)))
6921             (gnus-summary-next-article)
6922           (gnus-summary-next-unread-article))
6923       (if (or (null gnus-current-article)
6924               (null gnus-article-current)
6925               (/= article (cdr gnus-article-current))
6926               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6927           ;; Selected subject is different from current article's.
6928           (gnus-summary-display-article article)
6929         (when article-window
6930           (gnus-eval-in-buffer-window gnus-article-buffer
6931             (setq endp (gnus-article-next-page lines)))
6932           (when endp
6933             (cond (circular
6934                    (gnus-summary-beginning-of-article))
6935                   (lines
6936                    (gnus-message 3 "End of message"))
6937                   ((null lines)
6938                    (if (and (eq gnus-summary-goto-unread 'never)
6939                             (not (gnus-summary-last-article-p article)))
6940                        (gnus-summary-next-article)
6941                      (gnus-summary-next-unread-article))))))))
6942     (gnus-summary-recenter)
6943     (gnus-summary-position-point)))
6944
6945 (defun gnus-summary-prev-page (&optional lines move)
6946   "Show previous page of selected article.
6947 Argument LINES specifies lines to be scrolled down.
6948 If MOVE, move to the previous unread article if point is at
6949 the beginning of the buffer."
6950   (interactive "P")
6951   (let ((article (gnus-summary-article-number))
6952         (article-window (get-buffer-window gnus-article-buffer t))
6953         endp)
6954     (gnus-configure-windows 'article)
6955     (if (or (null gnus-current-article)
6956             (null gnus-article-current)
6957             (/= article (cdr gnus-article-current))
6958             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6959         ;; Selected subject is different from current article's.
6960         (gnus-summary-display-article article)
6961       (gnus-summary-recenter)
6962       (when article-window
6963         (gnus-eval-in-buffer-window gnus-article-buffer
6964           (setq endp (gnus-article-prev-page lines)))
6965         (when (and move endp)
6966           (cond (lines
6967                  (gnus-message 3 "Beginning of message"))
6968                 ((null lines)
6969                  (if (and (eq gnus-summary-goto-unread 'never)
6970                           (not (gnus-summary-first-article-p article)))
6971                      (gnus-summary-prev-article)
6972                    (gnus-summary-prev-unread-article))))))))
6973   (gnus-summary-position-point))
6974
6975 (defun gnus-summary-prev-page-or-article (&optional lines)
6976   "Show previous page of selected article.
6977 Argument LINES specifies lines to be scrolled down.
6978 If at the beginning of the article, go to the next article."
6979   (interactive "P")
6980   (gnus-summary-prev-page lines t))
6981
6982 (defun gnus-summary-scroll-up (lines)
6983   "Scroll up (or down) one line current article.
6984 Argument LINES specifies lines to be scrolled up (or down if negative)."
6985   (interactive "p")
6986   (gnus-configure-windows 'article)
6987   (gnus-summary-show-thread)
6988   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6989     (gnus-eval-in-buffer-window gnus-article-buffer
6990       (cond ((> lines 0)
6991              (when (gnus-article-next-page lines)
6992                (gnus-message 3 "End of message")))
6993             ((< lines 0)
6994              (gnus-article-prev-page (- lines))))))
6995   (gnus-summary-recenter)
6996   (gnus-summary-position-point))
6997
6998 (defun gnus-summary-scroll-down (lines)
6999   "Scroll down (or up) one line current article.
7000 Argument LINES specifies lines to be scrolled down (or up if negative)."
7001   (interactive "p")
7002   (gnus-summary-scroll-up (- lines)))
7003
7004 (defun gnus-summary-next-same-subject ()
7005   "Select next article which has the same subject as current one."
7006   (interactive)
7007   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7008
7009 (defun gnus-summary-prev-same-subject ()
7010   "Select previous article which has the same subject as current one."
7011   (interactive)
7012   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7013
7014 (defun gnus-summary-next-unread-same-subject ()
7015   "Select next unread article which has the same subject as current one."
7016   (interactive)
7017   (gnus-summary-next-article t (gnus-summary-article-subject)))
7018
7019 (defun gnus-summary-prev-unread-same-subject ()
7020   "Select previous unread article which has the same subject as current one."
7021   (interactive)
7022   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7023
7024 (defun gnus-summary-first-unread-article ()
7025   "Select the first unread article.
7026 Return nil if there are no unread articles."
7027   (interactive)
7028   (prog1
7029       (when (gnus-summary-first-subject t)
7030         (gnus-summary-show-thread)
7031         (gnus-summary-first-subject t)
7032         (gnus-summary-display-article (gnus-summary-article-number)))
7033     (gnus-summary-position-point)))
7034
7035 (defun gnus-summary-first-unread-subject ()
7036   "Place the point on the subject line of the first unread article.
7037 Return nil if there are no unread articles."
7038   (interactive)
7039   (prog1
7040       (when (gnus-summary-first-subject t)
7041         (gnus-summary-show-thread)
7042         (gnus-summary-first-subject t))
7043     (gnus-summary-position-point)))
7044
7045 (defun gnus-summary-first-unseen-subject ()
7046   "Place the point on the subject line of the first unseen article.
7047 Return nil if there are no unseen articles."
7048   (interactive)
7049   (prog1
7050       (when (gnus-summary-first-subject t t t)
7051         (gnus-summary-show-thread)
7052         (gnus-summary-first-subject t t t))
7053     (gnus-summary-position-point)))
7054
7055 (defun gnus-summary-first-unseen-or-unread-subject ()
7056   "Place the point on the subject line of the first unseen article.
7057 Return nil if there are no unseen articles."
7058   (interactive)
7059   (prog1
7060       (unless (when (gnus-summary-first-subject t t t)
7061                 (gnus-summary-show-thread)
7062                 (gnus-summary-first-subject t t t))
7063         (when (gnus-summary-first-subject t)
7064           (gnus-summary-show-thread)
7065           (gnus-summary-first-subject t)))
7066     (gnus-summary-position-point)))
7067
7068 (defun gnus-summary-first-article ()
7069   "Select the first article.
7070 Return nil if there are no articles."
7071   (interactive)
7072   (prog1
7073       (when (gnus-summary-first-subject)
7074         (gnus-summary-show-thread)
7075         (gnus-summary-first-subject)
7076         (gnus-summary-display-article (gnus-summary-article-number)))
7077     (gnus-summary-position-point)))
7078
7079 (defun gnus-summary-best-unread-article (&optional arg)
7080   "Select the unread article with the highest score.
7081 If given a prefix argument, select the next unread article that has a
7082 score higher than the default score."
7083   (interactive "P")
7084   (let ((article (if arg
7085                      (gnus-summary-better-unread-subject)
7086                    (gnus-summary-best-unread-subject))))
7087     (if article
7088         (gnus-summary-goto-article article)
7089       (error "No unread articles"))))
7090
7091 (defun gnus-summary-best-unread-subject ()
7092   "Select the unread subject with the highest score."
7093   (interactive)
7094   (let ((best -1000000)
7095         (data gnus-newsgroup-data)
7096         article score)
7097     (while data
7098       (and (gnus-data-unread-p (car data))
7099            (> (setq score
7100                     (gnus-summary-article-score (gnus-data-number (car data))))
7101               best)
7102            (setq best score
7103                  article (gnus-data-number (car data))))
7104       (setq data (cdr data)))
7105     (when article
7106       (gnus-summary-goto-subject article))
7107     (gnus-summary-position-point)
7108     article))
7109
7110 (defun gnus-summary-better-unread-subject ()
7111   "Select the first unread subject that has a score over the default score."
7112   (interactive)
7113   (let ((data gnus-newsgroup-data)
7114         article score)
7115     (while (and (setq article (gnus-data-number (car data)))
7116                 (or (gnus-data-read-p (car data))
7117                     (not (> (gnus-summary-article-score article)
7118                             gnus-summary-default-score))))
7119       (setq data (cdr data)))
7120     (when article
7121       (gnus-summary-goto-subject article))
7122     (gnus-summary-position-point)
7123     article))
7124
7125 (defun gnus-summary-last-subject ()
7126   "Go to the last displayed subject line in the group."
7127   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7128     (when article
7129       (gnus-summary-goto-subject article))))
7130
7131 (defun gnus-summary-goto-article (article &optional all-headers force)
7132   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7133 If ALL-HEADERS is non-nil, no header lines are hidden.
7134 If FORCE, go to the article even if it isn't displayed.  If FORCE
7135 is a number, it is the line the article is to be displayed on."
7136   (interactive
7137    (list
7138     (completing-read
7139      "Article number or Message-ID: "
7140      (mapcar (lambda (number) (list (int-to-string number)))
7141              gnus-newsgroup-limit))
7142     current-prefix-arg
7143     t))
7144   (prog1
7145       (if (and (stringp article)
7146                (string-match "@" article))
7147           (gnus-summary-refer-article article)
7148         (when (stringp article)
7149           (setq article (string-to-number article)))
7150         (if (gnus-summary-goto-subject article force)
7151             (gnus-summary-display-article article all-headers)
7152           (gnus-message 4 "Couldn't go to article %s" article) nil))
7153     (gnus-summary-position-point)))
7154
7155 (defun gnus-summary-goto-last-article ()
7156   "Go to the previously read article."
7157   (interactive)
7158   (prog1
7159       (when gnus-last-article
7160         (gnus-summary-goto-article gnus-last-article nil t))
7161     (gnus-summary-position-point)))
7162
7163 (defun gnus-summary-pop-article (number)
7164   "Pop one article off the history and go to the previous.
7165 NUMBER articles will be popped off."
7166   (interactive "p")
7167   (let (to)
7168     (setq gnus-newsgroup-history
7169           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7170     (if to
7171         (gnus-summary-goto-article (car to) nil t)
7172       (error "Article history empty")))
7173   (gnus-summary-position-point))
7174
7175 ;; Summary commands and functions for limiting the summary buffer.
7176
7177 (defun gnus-summary-limit-to-articles (n)
7178   "Limit the summary buffer to the next N articles.
7179 If not given a prefix, use the process marked articles instead."
7180   (interactive "P")
7181   (prog1
7182       (let ((articles (gnus-summary-work-articles n)))
7183         (setq gnus-newsgroup-processable nil)
7184         (gnus-summary-limit articles))
7185     (gnus-summary-position-point)))
7186
7187 (defun gnus-summary-pop-limit (&optional total)
7188   "Restore the previous limit.
7189 If given a prefix, remove all limits."
7190   (interactive "P")
7191   (when total
7192     (setq gnus-newsgroup-limits
7193           (list (mapcar (lambda (h) (mail-header-number h))
7194                         gnus-newsgroup-headers))))
7195   (unless gnus-newsgroup-limits
7196     (error "No limit to pop"))
7197   (prog1
7198       (gnus-summary-limit nil 'pop)
7199     (gnus-summary-position-point)))
7200
7201 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7202   "Limit the summary buffer to articles that have subjects that match a regexp.
7203 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7204   (interactive
7205    (list (read-string (if current-prefix-arg
7206                           "Exclude subject (regexp): "
7207                         "Limit to subject (regexp): "))
7208          nil current-prefix-arg))
7209   (unless header
7210     (setq header "subject"))
7211   (when (not (equal "" subject))
7212     (prog1
7213         (let ((articles (gnus-summary-find-matching
7214                          (or header "subject") subject 'all nil nil
7215                          not-matching)))
7216           (unless articles
7217             (error "Found no matches for \"%s\"" subject))
7218           (gnus-summary-limit articles))
7219       (gnus-summary-position-point))))
7220
7221 (defun gnus-summary-limit-to-author (from &optional not-matching)
7222   "Limit the summary buffer to articles that have authors that match a regexp.
7223 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7224   (interactive
7225    (list (read-string (if current-prefix-arg
7226                           "Exclude author (regexp): "
7227                         "Limit to author (regexp): "))
7228          current-prefix-arg))
7229   (gnus-summary-limit-to-subject from "from" not-matching))
7230
7231 (defun gnus-summary-limit-to-age (age &optional younger-p)
7232   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7233 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7234 articles that are younger than AGE days."
7235   (interactive
7236    (let ((younger current-prefix-arg)
7237          (days-got nil)
7238          days)
7239      (while (not days-got)
7240        (setq days (if younger
7241                       (read-string "Limit to articles within (in days): ")
7242                     (read-string "Limit to articles older than (in days): ")))
7243        (when (> (length days) 0)
7244          (setq days (read days)))
7245        (if (numberp days)
7246            (progn
7247              (setq days-got t)
7248              (if (< days 0)
7249                  (progn
7250                    (setq younger (not younger))
7251                    (setq days (* days -1)))))
7252          (message "Please enter a number.")
7253          (sleep-for 1)))
7254      (list days younger)))
7255   (prog1
7256       (let ((data gnus-newsgroup-data)
7257             (cutoff (days-to-time age))
7258             articles d date is-younger)
7259         (while (setq d (pop data))
7260           (when (and (vectorp (gnus-data-header d))
7261                      (setq date (mail-header-date (gnus-data-header d))))
7262             (setq is-younger (time-less-p
7263                               (time-since (condition-case ()
7264                                               (date-to-time date)
7265                                             (error '(0 0))))
7266                               cutoff))
7267             (when (if younger-p
7268                       is-younger
7269                     (not is-younger))
7270               (push (gnus-data-number d) articles))))
7271         (gnus-summary-limit (nreverse articles)))
7272     (gnus-summary-position-point)))
7273
7274 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7275   "Limit the summary buffer to articles that match an 'extra' header."
7276   (interactive
7277    (let ((header
7278           (intern
7279            (gnus-completing-read-with-default
7280             (symbol-name (car gnus-extra-headers))
7281             (if current-prefix-arg
7282                 "Exclude extra header:"
7283               "Limit extra header:")
7284             (mapcar (lambda (x)
7285                       (cons (symbol-name x) x))
7286                     gnus-extra-headers)
7287             nil
7288             t))))
7289      (list header
7290            (read-string (format "%s header %s (regexp): "
7291                                 (if current-prefix-arg "Exclude" "Limit to")
7292                                 header))
7293            current-prefix-arg)))
7294   (when (not (equal "" regexp))
7295     (prog1
7296         (let ((articles (gnus-summary-find-matching
7297                          (cons 'extra header) regexp 'all nil nil
7298                          not-matching)))
7299           (unless articles
7300             (error "Found no matches for \"%s\"" regexp))
7301           (gnus-summary-limit articles))
7302       (gnus-summary-position-point))))
7303
7304 (defun gnus-summary-limit-to-display-predicate ()
7305   "Limit the summary buffer to the predicated in the `display' group parameter."
7306   (interactive)
7307   (unless gnus-newsgroup-display
7308     (error "There is no `display' group parameter"))
7309   (let (articles)
7310     (dolist (number gnus-newsgroup-articles)
7311       (when (funcall gnus-newsgroup-display)
7312         (push number articles)))
7313     (gnus-summary-limit articles))
7314   (gnus-summary-position-point))
7315
7316 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7317 (make-obsolete
7318  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7319
7320 (defun gnus-summary-limit-to-unread (&optional all)
7321   "Limit the summary buffer to articles that are not marked as read.
7322 If ALL is non-nil, limit strictly to unread articles."
7323   (interactive "P")
7324   (if all
7325       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7326     (gnus-summary-limit-to-marks
7327      ;; Concat all the marks that say that an article is read and have
7328      ;; those removed.
7329      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7330            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7331            gnus-low-score-mark gnus-expirable-mark
7332            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7333            gnus-duplicate-mark gnus-souped-mark)
7334      'reverse)))
7335
7336 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7337 (make-obsolete 'gnus-summary-delete-marked-with
7338                'gnus-summary-limit-exclude-marks)
7339
7340 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7341   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7342 If REVERSE, limit the summary buffer to articles that are marked
7343 with MARKS.  MARKS can either be a string of marks or a list of marks.
7344 Returns how many articles were removed."
7345   (interactive "sMarks: ")
7346   (gnus-summary-limit-to-marks marks t))
7347
7348 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7349   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7350 If REVERSE (the prefix), limit the summary buffer to articles that are
7351 not marked with MARKS.  MARKS can either be a string of marks or a
7352 list of marks.
7353 Returns how many articles were removed."
7354   (interactive "sMarks: \nP")
7355   (prog1
7356       (let ((data gnus-newsgroup-data)
7357             (marks (if (listp marks) marks
7358                      (append marks nil))) ; Transform to list.
7359             articles)
7360         (while data
7361           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7362                   (memq (gnus-data-mark (car data)) marks))
7363             (push (gnus-data-number (car data)) articles))
7364           (setq data (cdr data)))
7365         (gnus-summary-limit articles))
7366     (gnus-summary-position-point)))
7367
7368 (defun gnus-summary-limit-to-score (score)
7369   "Limit to articles with score at or above SCORE."
7370   (interactive "NLimit to articles with score of at least: ")
7371   (let ((data gnus-newsgroup-data)
7372         articles)
7373     (while data
7374       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7375                 score)
7376         (push (gnus-data-number (car data)) articles))
7377       (setq data (cdr data)))
7378     (prog1
7379         (gnus-summary-limit articles)
7380       (gnus-summary-position-point))))
7381
7382 (defun gnus-summary-limit-to-unseen ()
7383   "Limit to unseen articles."
7384   (interactive)
7385   (prog1
7386       (gnus-summary-limit gnus-newsgroup-unseen)
7387     (gnus-summary-position-point)))
7388
7389 (defun gnus-summary-limit-include-thread (id)
7390   "Display all the hidden articles that is in the thread with ID in it.
7391 When called interactively, ID is the Message-ID of the current
7392 article."
7393   (interactive (list (mail-header-id (gnus-summary-article-header))))
7394   (let ((articles (gnus-articles-in-thread
7395                    (gnus-id-to-thread (gnus-root-id id)))))
7396     (prog1
7397         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7398       (gnus-summary-limit-include-matching-articles
7399        "subject"
7400        (regexp-quote (gnus-simplify-subject-re
7401                       (mail-header-subject (gnus-id-to-header id)))))
7402       (gnus-summary-position-point))))
7403
7404 (defun gnus-summary-limit-include-matching-articles (header regexp)
7405   "Display all the hidden articles that have HEADERs that match REGEXP."
7406   (interactive (list (read-string "Match on header: ")
7407                      (read-string "Regexp: ")))
7408   (let ((articles (gnus-find-matching-articles header regexp)))
7409     (prog1
7410         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7411       (gnus-summary-position-point))))
7412
7413 (defun gnus-summary-limit-include-dormant ()
7414   "Display all the hidden articles that are marked as dormant.
7415 Note that this command only works on a subset of the articles currently
7416 fetched for this group."
7417   (interactive)
7418   (unless gnus-newsgroup-dormant
7419     (error "There are no dormant articles in this group"))
7420   (prog1
7421       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7422     (gnus-summary-position-point)))
7423
7424 (defun gnus-summary-limit-exclude-dormant ()
7425   "Hide all dormant articles."
7426   (interactive)
7427   (prog1
7428       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7429     (gnus-summary-position-point)))
7430
7431 (defun gnus-summary-limit-exclude-childless-dormant ()
7432   "Hide all dormant articles that have no children."
7433   (interactive)
7434   (let ((data (gnus-data-list t))
7435         articles d children)
7436     ;; Find all articles that are either not dormant or have
7437     ;; children.
7438     (while (setq d (pop data))
7439       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7440                 (and (setq children
7441                            (gnus-article-children (gnus-data-number d)))
7442                      (let (found)
7443                        (while children
7444                          (when (memq (car children) articles)
7445                            (setq children nil
7446                                  found t))
7447                          (pop children))
7448                        found)))
7449         (push (gnus-data-number d) articles)))
7450     ;; Do the limiting.
7451     (prog1
7452         (gnus-summary-limit articles)
7453       (gnus-summary-position-point))))
7454
7455 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7456   "Mark all unread excluded articles as read.
7457 If ALL, mark even excluded ticked and dormants as read."
7458   (interactive "P")
7459   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7460   (let ((articles (gnus-sorted-ndifference
7461                    (sort
7462                     (mapcar (lambda (h) (mail-header-number h))
7463                             gnus-newsgroup-headers)
7464                     '<)
7465                    gnus-newsgroup-limit))
7466         article)
7467     (setq gnus-newsgroup-unreads
7468           (gnus-sorted-intersection gnus-newsgroup-unreads
7469                                     gnus-newsgroup-limit))
7470     (if all
7471         (setq gnus-newsgroup-dormant nil
7472               gnus-newsgroup-marked nil
7473               gnus-newsgroup-reads
7474               (nconc
7475                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7476                gnus-newsgroup-reads))
7477       (while (setq article (pop articles))
7478         (unless (or (memq article gnus-newsgroup-dormant)
7479                     (memq article gnus-newsgroup-marked))
7480           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7481
7482 (defun gnus-summary-limit (articles &optional pop)
7483   (if pop
7484       ;; We pop the previous limit off the stack and use that.
7485       (setq articles (car gnus-newsgroup-limits)
7486             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7487     ;; We use the new limit, so we push the old limit on the stack.
7488     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7489   ;; Set the limit.
7490   (setq gnus-newsgroup-limit articles)
7491   (let ((total (length gnus-newsgroup-data))
7492         (data (gnus-data-find-list (gnus-summary-article-number)))
7493         (gnus-summary-mark-below nil)   ; Inhibit this.
7494         found)
7495     ;; This will do all the work of generating the new summary buffer
7496     ;; according to the new limit.
7497     (gnus-summary-prepare)
7498     ;; Hide any threads, possibly.
7499     (gnus-summary-maybe-hide-threads)
7500     ;; Try to return to the article you were at, or one in the
7501     ;; neighborhood.
7502     (when data
7503       ;; We try to find some article after the current one.
7504       (while data
7505         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7506           (setq data nil
7507                 found t))
7508         (setq data (cdr data))))
7509     (unless found
7510       ;; If there is no data, that means that we were after the last
7511       ;; article.  The same goes when we can't find any articles
7512       ;; after the current one.
7513       (goto-char (point-max))
7514       (gnus-summary-find-prev))
7515     (gnus-set-mode-line 'summary)
7516     ;; We return how many articles were removed from the summary
7517     ;; buffer as a result of the new limit.
7518     (- total (length gnus-newsgroup-data))))
7519
7520 (defsubst gnus-invisible-cut-children (threads)
7521   (let ((num 0))
7522     (while threads
7523       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7524         (incf num))
7525       (pop threads))
7526     (< num 2)))
7527
7528 (defsubst gnus-cut-thread (thread)
7529   "Go forwards in the thread until we find an article that we want to display."
7530   (when (or (eq gnus-fetch-old-headers 'some)
7531             (eq gnus-fetch-old-headers 'invisible)
7532             (numberp gnus-fetch-old-headers)
7533             (eq gnus-build-sparse-threads 'some)
7534             (eq gnus-build-sparse-threads 'more))
7535     ;; Deal with old-fetched headers and sparse threads.
7536     (while (and
7537             thread
7538             (or
7539              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7540              (gnus-summary-article-ancient-p
7541               (mail-header-number (car thread))))
7542             (if (or (<= (length (cdr thread)) 1)
7543                     (eq gnus-fetch-old-headers 'invisible))
7544                 (setq gnus-newsgroup-limit
7545                       (delq (mail-header-number (car thread))
7546                             gnus-newsgroup-limit)
7547                       thread (cadr thread))
7548               (when (gnus-invisible-cut-children (cdr thread))
7549                 (let ((th (cdr thread)))
7550                   (while th
7551                     (if (memq (mail-header-number (caar th))
7552                               gnus-newsgroup-limit)
7553                         (setq thread (car th)
7554                               th nil)
7555                       (setq th (cdr th))))))))))
7556   thread)
7557
7558 (defun gnus-cut-threads (threads)
7559   "Cut off all uninteresting articles from the beginning of threads."
7560   (when (or (eq gnus-fetch-old-headers 'some)
7561             (eq gnus-fetch-old-headers 'invisible)
7562             (numberp gnus-fetch-old-headers)
7563             (eq gnus-build-sparse-threads 'some)
7564             (eq gnus-build-sparse-threads 'more))
7565     (let ((th threads))
7566       (while th
7567         (setcar th (gnus-cut-thread (car th)))
7568         (setq th (cdr th)))))
7569   ;; Remove nixed out threads.
7570   (delq nil threads))
7571
7572 (defun gnus-summary-initial-limit (&optional show-if-empty)
7573   "Figure out what the initial limit is supposed to be on group entry.
7574 This entails weeding out unwanted dormants, low-scored articles,
7575 fetch-old-headers verbiage, and so on."
7576   ;; Most groups have nothing to remove.
7577   (if (or gnus-inhibit-limiting
7578           (and (null gnus-newsgroup-dormant)
7579                (eq gnus-newsgroup-display 'gnus-not-ignore)
7580                (not (eq gnus-fetch-old-headers 'some))
7581                (not (numberp gnus-fetch-old-headers))
7582                (not (eq gnus-fetch-old-headers 'invisible))
7583                (null gnus-summary-expunge-below)
7584                (not (eq gnus-build-sparse-threads 'some))
7585                (not (eq gnus-build-sparse-threads 'more))
7586                (null gnus-thread-expunge-below)
7587                (not gnus-use-nocem)))
7588       ()                                ; Do nothing.
7589     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7590     (setq gnus-newsgroup-limit nil)
7591     (mapatoms
7592      (lambda (node)
7593        (unless (car (symbol-value node))
7594          ;; These threads have no parents -- they are roots.
7595          (let ((nodes (cdr (symbol-value node)))
7596                thread)
7597            (while nodes
7598              (if (and gnus-thread-expunge-below
7599                       (< (gnus-thread-total-score (car nodes))
7600                          gnus-thread-expunge-below))
7601                  (gnus-expunge-thread (pop nodes))
7602                (setq thread (pop nodes))
7603                (gnus-summary-limit-children thread))))))
7604      gnus-newsgroup-dependencies)
7605     ;; If this limitation resulted in an empty group, we might
7606     ;; pop the previous limit and use it instead.
7607     (when (and (not gnus-newsgroup-limit)
7608                show-if-empty)
7609       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7610     gnus-newsgroup-limit))
7611
7612 (defun gnus-summary-limit-children (thread)
7613   "Return 1 if this subthread is visible and 0 if it is not."
7614   ;; First we get the number of visible children to this thread.  This
7615   ;; is done by recursing down the thread using this function, so this
7616   ;; will really go down to a leaf article first, before slowly
7617   ;; working its way up towards the root.
7618   (when thread
7619     (let ((children
7620            (if (cdr thread)
7621                (apply '+ (mapcar 'gnus-summary-limit-children
7622                                  (cdr thread)))
7623              0))
7624           (number (mail-header-number (car thread)))
7625           score)
7626       (if (and
7627            (not (memq number gnus-newsgroup-marked))
7628            (or
7629             ;; If this article is dormant and has absolutely no visible
7630             ;; children, then this article isn't visible.
7631             (and (memq number gnus-newsgroup-dormant)
7632                  (zerop children))
7633             ;; If this is "fetch-old-headered" and there is no
7634             ;; visible children, then we don't want this article.
7635             (and (or (eq gnus-fetch-old-headers 'some)
7636                      (numberp gnus-fetch-old-headers))
7637                  (gnus-summary-article-ancient-p number)
7638                  (zerop children))
7639             ;; If this is "fetch-old-headered" and `invisible', then
7640             ;; we don't want this article.
7641             (and (eq gnus-fetch-old-headers 'invisible)
7642                  (gnus-summary-article-ancient-p number))
7643             ;; If this is a sparsely inserted article with no children,
7644             ;; we don't want it.
7645             (and (eq gnus-build-sparse-threads 'some)
7646                  (gnus-summary-article-sparse-p number)
7647                  (zerop children))
7648             ;; If we use expunging, and this article is really
7649             ;; low-scored, then we don't want this article.
7650             (when (and gnus-summary-expunge-below
7651                        (< (setq score
7652                                 (or (cdr (assq number gnus-newsgroup-scored))
7653                                     gnus-summary-default-score))
7654                           gnus-summary-expunge-below))
7655               ;; We increase the expunge-tally here, but that has
7656               ;; nothing to do with the limits, really.
7657               (incf gnus-newsgroup-expunged-tally)
7658               ;; We also mark as read here, if that's wanted.
7659               (when (and gnus-summary-mark-below
7660                          (< score gnus-summary-mark-below))
7661                 (setq gnus-newsgroup-unreads
7662                       (delq number gnus-newsgroup-unreads))
7663                 (if gnus-newsgroup-auto-expire
7664                     (push number gnus-newsgroup-expirable)
7665                   (push (cons number gnus-low-score-mark)
7666                         gnus-newsgroup-reads)))
7667               t)
7668             ;; Do the `display' group parameter.
7669             (and gnus-newsgroup-display
7670                  (not (funcall gnus-newsgroup-display)))
7671             ;; Check NoCeM things.
7672             (if (and gnus-use-nocem
7673                      (gnus-nocem-unwanted-article-p
7674                       (mail-header-id (car thread))))
7675                 (progn
7676                   (setq gnus-newsgroup-unreads
7677                         (delq number gnus-newsgroup-unreads))
7678                   t))))
7679           ;; Nope, invisible article.
7680           0
7681         ;; Ok, this article is to be visible, so we add it to the limit
7682         ;; and return 1.
7683         (push number gnus-newsgroup-limit)
7684         1))))
7685
7686 (defun gnus-expunge-thread (thread)
7687   "Mark all articles in THREAD as read."
7688   (let* ((number (mail-header-number (car thread))))
7689     (incf gnus-newsgroup-expunged-tally)
7690     ;; We also mark as read here, if that's wanted.
7691     (setq gnus-newsgroup-unreads
7692           (delq number gnus-newsgroup-unreads))
7693     (if gnus-newsgroup-auto-expire
7694         (push number gnus-newsgroup-expirable)
7695       (push (cons number gnus-low-score-mark)
7696             gnus-newsgroup-reads)))
7697   ;; Go recursively through all subthreads.
7698   (mapcar 'gnus-expunge-thread (cdr thread)))
7699
7700 ;; Summary article oriented commands
7701
7702 (defun gnus-summary-refer-parent-article (n)
7703   "Refer parent article N times.
7704 If N is negative, go to ancestor -N instead.
7705 The difference between N and the number of articles fetched is returned."
7706   (interactive "p")
7707   (let ((skip 1)
7708         error header ref)
7709     (when (not (natnump n))
7710       (setq skip (abs n)
7711             n 1))
7712     (while (and (> n 0)
7713                 (not error))
7714       (setq header (gnus-summary-article-header))
7715       (if (and (eq (mail-header-number header)
7716                    (cdr gnus-article-current))
7717                (equal gnus-newsgroup-name
7718                       (car gnus-article-current)))
7719           ;; If we try to find the parent of the currently
7720           ;; displayed article, then we take a look at the actual
7721           ;; References header, since this is slightly more
7722           ;; reliable than the References field we got from the
7723           ;; server.
7724           (save-excursion
7725             (set-buffer gnus-original-article-buffer)
7726             (nnheader-narrow-to-headers)
7727             (unless (setq ref (message-fetch-field "references"))
7728               (setq ref (message-fetch-field "in-reply-to")))
7729             (widen))
7730         (setq ref
7731               ;; It's not the current article, so we take a bet on
7732               ;; the value we got from the server.
7733               (mail-header-references header)))
7734       (if (and ref
7735                (not (equal ref "")))
7736           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7737             (gnus-message 1 "Couldn't find parent"))
7738         (gnus-message 1 "No references in article %d"
7739                       (gnus-summary-article-number))
7740         (setq error t))
7741       (decf n))
7742     (gnus-summary-position-point)
7743     n))
7744
7745 (defun gnus-summary-refer-references ()
7746   "Fetch all articles mentioned in the References header.
7747 Return the number of articles fetched."
7748   (interactive)
7749   (let ((ref (mail-header-references (gnus-summary-article-header)))
7750         (current (gnus-summary-article-number))
7751         (n 0))
7752     (if (or (not ref)
7753             (equal ref ""))
7754         (error "No References in the current article")
7755       ;; For each Message-ID in the References header...
7756       (while (string-match "<[^>]*>" ref)
7757         (incf n)
7758         ;; ... fetch that article.
7759         (gnus-summary-refer-article
7760          (prog1 (match-string 0 ref)
7761            (setq ref (substring ref (match-end 0))))))
7762       (gnus-summary-goto-subject current)
7763       (gnus-summary-position-point)
7764       n)))
7765
7766 (defun gnus-summary-refer-thread (&optional limit)
7767   "Fetch all articles in the current thread.
7768 If LIMIT (the numerical prefix), fetch that many old headers instead
7769 of what's specified by the `gnus-refer-thread-limit' variable."
7770   (interactive "P")
7771   (let ((id (mail-header-id (gnus-summary-article-header)))
7772         (limit (if limit (prefix-numeric-value limit)
7773                  gnus-refer-thread-limit)))
7774     ;; We want to fetch LIMIT *old* headers, but we also have to
7775     ;; re-fetch all the headers in the current buffer, because many of
7776     ;; them may be undisplayed.  So we adjust LIMIT.
7777     (when (numberp limit)
7778       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7779     (unless (eq gnus-fetch-old-headers 'invisible)
7780       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7781       ;; Retrieve the headers and read them in.
7782       (if (eq (gnus-retrieve-headers
7783                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7784               'nov)
7785           (gnus-build-all-threads)
7786         (error "Can't fetch thread from backends that don't support NOV"))
7787       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7788     (gnus-summary-limit-include-thread id)))
7789
7790 (defun gnus-summary-refer-article (message-id)
7791   "Fetch an article specified by MESSAGE-ID."
7792   (interactive "sMessage-ID: ")
7793   (when (and (stringp message-id)
7794              (not (zerop (length message-id))))
7795     ;; Construct the correct Message-ID if necessary.
7796     ;; Suggested by tale@pawl.rpi.edu.
7797     (unless (string-match "^<" message-id)
7798       (setq message-id (concat "<" message-id)))
7799     (unless (string-match ">$" message-id)
7800       (setq message-id (concat message-id ">")))
7801     (let* ((header (gnus-id-to-header message-id))
7802            (sparse (and header
7803                         (gnus-summary-article-sparse-p
7804                          (mail-header-number header))
7805                         (memq (mail-header-number header)
7806                               gnus-newsgroup-limit)))
7807            number)
7808       (cond
7809        ;; If the article is present in the buffer we just go to it.
7810        ((and header
7811              (or (not (gnus-summary-article-sparse-p
7812                        (mail-header-number header)))
7813                  sparse))
7814         (prog1
7815             (gnus-summary-goto-article
7816              (mail-header-number header) nil t)
7817           (when sparse
7818             (gnus-summary-update-article (mail-header-number header)))))
7819        (t
7820         ;; We fetch the article.
7821         (catch 'found
7822           (dolist (gnus-override-method (gnus-refer-article-methods))
7823             (gnus-check-server gnus-override-method)
7824             ;; Fetch the header, and display the article.
7825             (when (setq number (gnus-summary-insert-subject message-id))
7826               (gnus-summary-select-article nil nil nil number)
7827               (throw 'found t)))
7828           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7829
7830 (defun gnus-refer-article-methods ()
7831   "Return a list of referrable methods."
7832   (cond
7833    ;; No method, so we default to current and native.
7834    ((null gnus-refer-article-method)
7835     (list gnus-current-select-method gnus-select-method))
7836    ;; Current.
7837    ((eq 'current gnus-refer-article-method)
7838     (list gnus-current-select-method))
7839    ;; List of select methods.
7840    ((not (and (symbolp (car gnus-refer-article-method))
7841               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7842     (let (out)
7843       (dolist (method gnus-refer-article-method)
7844         (push (if (eq 'current method)
7845                   gnus-current-select-method
7846                 method)
7847               out))
7848       (nreverse out)))
7849    ;; One single select method.
7850    (t
7851     (list gnus-refer-article-method))))
7852
7853 (defun gnus-summary-edit-parameters ()
7854   "Edit the group parameters of the current group."
7855   (interactive)
7856   (gnus-group-edit-group gnus-newsgroup-name 'params))
7857
7858 (defun gnus-summary-customize-parameters ()
7859   "Customize the group parameters of the current group."
7860   (interactive)
7861   (gnus-group-customize gnus-newsgroup-name))
7862
7863 (defun gnus-summary-enter-digest-group (&optional force)
7864   "Enter an nndoc group based on the current article.
7865 If FORCE, force a digest interpretation.  If not, try
7866 to guess what the document format is."
7867   (interactive "P")
7868   (let ((conf gnus-current-window-configuration))
7869     (save-excursion
7870       (gnus-summary-select-article))
7871     (setq gnus-current-window-configuration conf)
7872     (let* ((name (format "%s-%d"
7873                          (gnus-group-prefixed-name
7874                           gnus-newsgroup-name (list 'nndoc ""))
7875                          (save-excursion
7876                            (set-buffer gnus-summary-buffer)
7877                            gnus-current-article)))
7878            (ogroup gnus-newsgroup-name)
7879            (params (append (gnus-info-params (gnus-get-info ogroup))
7880                            (list (cons 'to-group ogroup))
7881                            (list (cons 'save-article-group ogroup))))
7882            (case-fold-search t)
7883            (buf (current-buffer))
7884            dig to-address)
7885       (save-excursion
7886         (set-buffer gnus-original-article-buffer)
7887         ;; Have the digest group inherit the main mail address of
7888         ;; the parent article.
7889         (when (setq to-address (or (gnus-fetch-field "reply-to")
7890                                    (gnus-fetch-field "from")))
7891           (setq params (append
7892                         (list (cons 'to-address
7893                                     (funcall gnus-decode-encoded-word-function
7894                                              to-address))))))
7895         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7896         (insert-buffer-substring gnus-original-article-buffer)
7897         ;; Remove lines that may lead nndoc to misinterpret the
7898         ;; document type.
7899         (narrow-to-region
7900          (goto-char (point-min))
7901          (or (search-forward "\n\n" nil t) (point)))
7902         (goto-char (point-min))
7903         (delete-matching-lines "^Path:\\|^From ")
7904         (widen))
7905       (unwind-protect
7906           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7907                     (gnus-newsgroup-ephemeral-ignored-charsets
7908                      gnus-newsgroup-ignored-charsets))
7909                 (gnus-group-read-ephemeral-group
7910                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7911                               (nndoc-article-type
7912                                ,(if force 'mbox 'guess)))
7913                  t nil nil nil
7914                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7915                                                         "ADAPT")))))
7916               ;; Make all postings to this group go to the parent group.
7917               (nconc (gnus-info-params (gnus-get-info name))
7918                      params)
7919             ;; Couldn't select this doc group.
7920             (switch-to-buffer buf)
7921             (gnus-set-global-variables)
7922             (gnus-configure-windows 'summary)
7923             (gnus-message 3 "Article couldn't be entered?"))
7924         (kill-buffer dig)))))
7925
7926 (defun gnus-summary-read-document (n)
7927   "Open a new group based on the current article(s).
7928 This will allow you to read digests and other similar
7929 documents as newsgroups.
7930 Obeys the standard process/prefix convention."
7931   (interactive "P")
7932   (let* ((articles (gnus-summary-work-articles n))
7933          (ogroup gnus-newsgroup-name)
7934          (params (append (gnus-info-params (gnus-get-info ogroup))
7935                          (list (cons 'to-group ogroup))))
7936          article group egroup groups vgroup)
7937     (while (setq article (pop articles))
7938       (setq group (format "%s-%d" gnus-newsgroup-name article))
7939       (gnus-summary-remove-process-mark article)
7940       (when (gnus-summary-display-article article)
7941         (save-excursion
7942           (with-temp-buffer
7943             (insert-buffer-substring gnus-original-article-buffer)
7944             ;; Remove some headers that may lead nndoc to make
7945             ;; the wrong guess.
7946             (message-narrow-to-head)
7947             (goto-char (point-min))
7948             (delete-matching-lines "^\\(Path\\):\\|^From ")
7949             (widen)
7950             (if (setq egroup
7951                       (gnus-group-read-ephemeral-group
7952                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7953                                      (nndoc-article-type guess))
7954                        t nil t))
7955                 (progn
7956             ;; Make all postings to this group go to the parent group.
7957                   (nconc (gnus-info-params (gnus-get-info egroup))
7958                          params)
7959                   (push egroup groups))
7960               ;; Couldn't select this doc group.
7961               (gnus-error 3 "Article couldn't be entered"))))))
7962     ;; Now we have selected all the documents.
7963     (cond
7964      ((not groups)
7965       (error "None of the articles could be interpreted as documents"))
7966      ((gnus-group-read-ephemeral-group
7967        (setq vgroup (format
7968                      "nnvirtual:%s-%s" gnus-newsgroup-name
7969                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7970        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7971        t
7972        (cons (current-buffer) 'summary)))
7973      (t
7974       (error "Couldn't select virtual nndoc group")))))
7975
7976 (defun gnus-summary-isearch-article (&optional regexp-p)
7977   "Do incremental search forward on the current article.
7978 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7979   (interactive "P")
7980   (gnus-summary-select-article)
7981   (gnus-configure-windows 'article)
7982   (gnus-eval-in-buffer-window gnus-article-buffer
7983     (save-restriction
7984       (widen)
7985       (isearch-forward regexp-p))))
7986
7987 (defun gnus-summary-search-article-forward (regexp &optional backward)
7988   "Search for an article containing REGEXP forward.
7989 If BACKWARD, search backward instead."
7990   (interactive
7991    (list (read-string
7992           (format "Search article %s (regexp%s): "
7993                   (if current-prefix-arg "backward" "forward")
7994                   (if gnus-last-search-regexp
7995                       (concat ", default " gnus-last-search-regexp)
7996                     "")))
7997          current-prefix-arg))
7998   (if (string-equal regexp "")
7999       (setq regexp (or gnus-last-search-regexp ""))
8000     (setq gnus-last-search-regexp regexp)
8001     (setq gnus-article-before-search gnus-current-article))
8002   ;; Intentionally set gnus-last-article.
8003   (setq gnus-last-article gnus-article-before-search)
8004   (let ((gnus-last-article gnus-last-article))
8005     (if (gnus-summary-search-article regexp backward)
8006         (gnus-summary-show-thread)
8007       (error "Search failed: \"%s\"" regexp))))
8008
8009 (defun gnus-summary-search-article-backward (regexp)
8010   "Search for an article containing REGEXP backward."
8011   (interactive
8012    (list (read-string
8013           (format "Search article backward (regexp%s): "
8014                   (if gnus-last-search-regexp
8015                       (concat ", default " gnus-last-search-regexp)
8016                     "")))))
8017   (gnus-summary-search-article-forward regexp 'backward))
8018
8019 (defun gnus-summary-search-article (regexp &optional backward)
8020   "Search for an article containing REGEXP.
8021 Optional argument BACKWARD means do search for backward.
8022 `gnus-select-article-hook' is not called during the search."
8023   ;; We have to require this here to make sure that the following
8024   ;; dynamic binding isn't shadowed by autoloading.
8025   (require 'gnus-async)
8026   (require 'gnus-art)
8027   (let ((gnus-select-article-hook nil)  ;Disable hook.
8028         (gnus-article-prepare-hook nil)
8029         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8030         (gnus-use-article-prefetch nil)
8031         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8032         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8033         (sum (current-buffer))
8034         (gnus-display-mime-function nil)
8035         (found nil)
8036         point)
8037     (gnus-save-hidden-threads
8038       (gnus-summary-select-article)
8039       (set-buffer gnus-article-buffer)
8040       (goto-char (window-point (get-buffer-window (current-buffer))))
8041       (when backward
8042         (forward-line -1))
8043       (while (not found)
8044         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8045         (if (if backward
8046                 (re-search-backward regexp nil t)
8047               (re-search-forward regexp nil t))
8048             ;; We found the regexp.
8049             (progn
8050               (setq found 'found)
8051               (beginning-of-line)
8052               (set-window-start
8053                (get-buffer-window (current-buffer))
8054                (point))
8055               (forward-line 1)
8056               (set-window-point
8057                (get-buffer-window (current-buffer))
8058                (point))
8059               (set-buffer sum)
8060               (setq point (point)))
8061           ;; We didn't find it, so we go to the next article.
8062           (set-buffer sum)
8063           (setq found 'not)
8064           (while (eq found 'not)
8065             (if (not (if backward (gnus-summary-find-prev)
8066                        (gnus-summary-find-next)))
8067                 ;; No more articles.
8068                 (setq found t)
8069               ;; Select the next article and adjust point.
8070               (unless (gnus-summary-article-sparse-p
8071                        (gnus-summary-article-number))
8072                 (setq found nil)
8073                 (gnus-summary-select-article)
8074                 (set-buffer gnus-article-buffer)
8075                 (widen)
8076                 (goto-char (if backward (point-max) (point-min))))))))
8077       (gnus-message 7 ""))
8078     ;; Return whether we found the regexp.
8079     (when (eq found 'found)
8080       (goto-char point)
8081       (gnus-summary-show-thread)
8082       (gnus-summary-goto-subject gnus-current-article)
8083       (gnus-summary-position-point)
8084       t)))
8085
8086 (defun gnus-find-matching-articles (header regexp)
8087   "Return a list of all articles that match REGEXP on HEADER.
8088 This search includes all articles in the current group that Gnus has
8089 fetched headers for, whether they are displayed or not."
8090   (let ((articles nil)
8091         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8092         (case-fold-search t))
8093     (dolist (header gnus-newsgroup-headers)
8094       (when (string-match regexp (funcall func header))
8095         (push (mail-header-number header) articles)))
8096     (nreverse articles)))
8097
8098 (defun gnus-summary-find-matching (header regexp &optional backward unread
8099                                           not-case-fold not-matching)
8100   "Return a list of all articles that match REGEXP on HEADER.
8101 The search stars on the current article and goes forwards unless
8102 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8103 If UNREAD is non-nil, only unread articles will
8104 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8105 in the comparisons. If NOT-MATCHING, return a list of all articles that
8106 not match REGEXP on HEADER."
8107   (let ((case-fold-search (not not-case-fold))
8108         articles d func)
8109     (if (consp header)
8110         (if (eq (car header) 'extra)
8111             (setq func
8112                   `(lambda (h)
8113                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8114                          "")))
8115           (error "%s is an invalid header" header))
8116       (unless (fboundp (intern (concat "mail-header-" header)))
8117         (error "%s is not a valid header" header))
8118       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8119     (dolist (d (if (eq backward 'all)
8120                    gnus-newsgroup-data
8121                  (gnus-data-find-list
8122                   (gnus-summary-article-number)
8123                   (gnus-data-list backward))))
8124       (when (and (or (not unread)       ; We want all articles...
8125                      (gnus-data-unread-p d)) ; Or just unreads.
8126                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8127                  (if not-matching
8128                      (not (string-match
8129                            regexp
8130                            (funcall func (gnus-data-header d))))
8131                    (string-match regexp
8132                                  (funcall func (gnus-data-header d)))))
8133         (push (gnus-data-number d) articles))) ; Success!
8134     (nreverse articles)))
8135
8136 (defun gnus-summary-execute-command (header regexp command &optional backward)
8137   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8138 If HEADER is an empty string (or nil), the match is done on the entire
8139 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8140   (interactive
8141    (list (let ((completion-ignore-case t))
8142            (completing-read
8143             "Header name: "
8144             (mapcar (lambda (header) (list (format "%s" header)))
8145                     (append
8146                      '("Number" "Subject" "From" "Lines" "Date"
8147                        "Message-ID" "Xref" "References" "Body")
8148                      gnus-extra-headers))
8149             nil 'require-match))
8150          (read-string "Regexp: ")
8151          (read-key-sequence "Command: ")
8152          current-prefix-arg))
8153   (when (equal header "Body")
8154     (setq header ""))
8155   ;; Hidden thread subtrees must be searched as well.
8156   (gnus-summary-show-all-threads)
8157   ;; We don't want to change current point nor window configuration.
8158   (save-excursion
8159     (save-window-excursion
8160       (gnus-message 6 "Executing %s..." (key-description command))
8161 ;; We'd like to execute COMMAND interactively so as to give arguments.
8162       (gnus-execute header regexp
8163                     `(call-interactively ',(key-binding command))
8164                     backward)
8165       (gnus-message 6 "Executing %s...done" (key-description command)))))
8166
8167 (defun gnus-summary-beginning-of-article ()
8168   "Scroll the article back to the beginning."
8169   (interactive)
8170   (gnus-summary-select-article)
8171   (gnus-configure-windows 'article)
8172   (gnus-eval-in-buffer-window gnus-article-buffer
8173     (widen)
8174     (goto-char (point-min))
8175     (when gnus-page-broken
8176       (gnus-narrow-to-page))))
8177
8178 (defun gnus-summary-end-of-article ()
8179   "Scroll to the end of the article."
8180   (interactive)
8181   (gnus-summary-select-article)
8182   (gnus-configure-windows 'article)
8183   (gnus-eval-in-buffer-window gnus-article-buffer
8184     (widen)
8185     (goto-char (point-max))
8186     (recenter -3)
8187     (when gnus-page-broken
8188       (gnus-narrow-to-page))))
8189
8190 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8191   "Truncate to LEN and quote all \"(\"'s in STRING."
8192   (gnus-replace-in-string (if (and len (> (length string) len))
8193                               (substring string 0 len)
8194                             string)
8195                           "[()]" "\\\\\\&"))
8196
8197 (defun gnus-summary-print-article (&optional filename n)
8198   "Generate and print a PostScript image of the N next (mail) articles.
8199
8200 If N is negative, print the N previous articles.  If N is nil and articles
8201 have been marked with the process mark, print these instead.
8202
8203 If the optional first argument FILENAME is nil, send the image to the
8204 printer.  If FILENAME is a string, save the PostScript image in a file with
8205 that name.  If FILENAME is a number, prompt the user for the name of the file
8206 to save in."
8207   (interactive (list (ps-print-preprint current-prefix-arg)))
8208   (dolist (article (gnus-summary-work-articles n))
8209     (gnus-summary-select-article nil nil 'pseudo article)
8210     (gnus-eval-in-buffer-window gnus-article-buffer
8211       (gnus-print-buffer))
8212     (gnus-summary-remove-process-mark article))
8213   (ps-despool filename))
8214
8215 (defun gnus-print-buffer ()
8216   (let ((buffer (generate-new-buffer " *print*")))
8217     (unwind-protect
8218         (progn
8219           (copy-to-buffer buffer (point-min) (point-max))
8220           (set-buffer buffer)
8221           (gnus-article-delete-invisible-text)
8222           (gnus-remove-text-with-property 'gnus-decoration)
8223           (when (gnus-visual-p 'article-highlight 'highlight)
8224             ;; Copy-to-buffer doesn't copy overlay.  So redo
8225             ;; highlight.
8226             (let ((gnus-article-buffer buffer))
8227               (gnus-article-highlight-citation t)
8228               (gnus-article-highlight-signature)))
8229           (let ((ps-left-header
8230                  (list
8231                   (concat "("
8232                           (gnus-summary-print-truncate-and-quote
8233                            (mail-header-subject gnus-current-headers)
8234                            66) ")")
8235                   (concat "("
8236                           (gnus-summary-print-truncate-and-quote
8237                            (mail-header-from gnus-current-headers)
8238                            45) ")")))
8239                 (ps-right-header
8240                  (list
8241                   "/pagenumberstring load"
8242                   (concat "("
8243                           (mail-header-date gnus-current-headers) ")"))))
8244             (gnus-run-hooks 'gnus-ps-print-hook)
8245             (save-excursion
8246               (if window-system
8247                   (ps-spool-buffer-with-faces)
8248                 (ps-spool-buffer)))))
8249       (kill-buffer buffer))))
8250
8251 (defun gnus-summary-show-article (&optional arg)
8252   "Force redisplaying of the current article.
8253 If ARG (the prefix) is a number, show the article with the charset
8254 defined in `gnus-summary-show-article-charset-alist', or the charset
8255 input.
8256 If ARG (the prefix) is non-nil and not a number, show the raw article
8257 without any article massaging functions being run.  Normally, the key strokes
8258 are `C-u g'."
8259   (interactive "P")
8260   (cond
8261    ((numberp arg)
8262     (gnus-summary-show-article t)
8263     (let ((gnus-newsgroup-charset
8264            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8265                (mm-read-coding-system
8266                 "View as charset: " ;; actually it is coding system.
8267                 (save-excursion
8268                   (set-buffer gnus-article-buffer)
8269                   (mm-detect-coding-region (point) (point-max))))))
8270           (gnus-newsgroup-ignored-charsets 'gnus-all))
8271       (gnus-summary-select-article nil 'force)
8272       (let ((deps gnus-newsgroup-dependencies)
8273             head header lines)
8274         (save-excursion
8275           (set-buffer gnus-original-article-buffer)
8276           (save-restriction
8277             (message-narrow-to-head)
8278             (setq head (buffer-string))
8279             (goto-char (point-min))
8280             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8281               (goto-char (point-max))
8282               (widen)
8283               (setq lines (1- (count-lines (point) (point-max))))))
8284           (with-temp-buffer
8285             (insert (format "211 %d Article retrieved.\n"
8286                             (cdr gnus-article-current)))
8287             (insert head)
8288             (if lines (insert (format "Lines: %d\n" lines)))
8289             (insert ".\n")
8290             (let ((nntp-server-buffer (current-buffer)))
8291               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8292         (gnus-data-set-header
8293          (gnus-data-find (cdr gnus-article-current))
8294          header)
8295         (gnus-summary-update-article-line
8296          (cdr gnus-article-current) header)
8297         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8298           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8299    ((not arg)
8300     ;; Select the article the normal way.
8301     (gnus-summary-select-article nil 'force))
8302    (t
8303     ;; We have to require this here to make sure that the following
8304     ;; dynamic binding isn't shadowed by autoloading.
8305     (require 'gnus-async)
8306     (require 'gnus-art)
8307     ;; Bind the article treatment functions to nil.
8308     (let ((gnus-have-all-headers t)
8309           gnus-article-prepare-hook
8310           gnus-article-decode-hook
8311           gnus-display-mime-function
8312           gnus-break-pages)
8313       ;; Destroy any MIME parts.
8314       (when (gnus-buffer-live-p gnus-article-buffer)
8315         (save-excursion
8316           (set-buffer gnus-article-buffer)
8317           (mm-destroy-parts gnus-article-mime-handles)
8318           ;; Set it to nil for safety reason.
8319           (setq gnus-article-mime-handle-alist nil)
8320           (setq gnus-article-mime-handles nil)))
8321       (gnus-summary-select-article nil 'force))))
8322   (gnus-summary-goto-subject gnus-current-article)
8323   (gnus-summary-position-point))
8324
8325 (defun gnus-summary-show-raw-article ()
8326   "Show the raw article without any article massaging functions being run."
8327   (interactive)
8328   (gnus-summary-show-article t))
8329
8330 (defun gnus-summary-verbose-headers (&optional arg)
8331   "Toggle permanent full header display.
8332 If ARG is a positive number, turn header display on.
8333 If ARG is a negative number, turn header display off."
8334   (interactive "P")
8335   (setq gnus-show-all-headers
8336         (cond ((or (not (numberp arg))
8337                    (zerop arg))
8338                (not gnus-show-all-headers))
8339               ((natnump arg)
8340                t)))
8341   (gnus-summary-show-article))
8342
8343 (defun gnus-summary-toggle-header (&optional arg)
8344   "Show the headers if they are hidden, or hide them if they are shown.
8345 If ARG is a positive number, show the entire header.
8346 If ARG is a negative number, hide the unwanted header lines."
8347   (interactive "P")
8348   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8349                      (get-buffer-window gnus-article-buffer t))))
8350     (when window
8351       (with-current-buffer gnus-article-buffer
8352         (widen)
8353         (article-narrow-to-head)
8354         (let* ((buffer-read-only nil)
8355                (inhibit-point-motion-hooks t)
8356                (hidden (if (numberp arg)
8357                            (>= arg 0)
8358                          (gnus-article-hidden-text-p 'headers)))
8359                s e)
8360           (delete-region (point-min) (point-max))
8361           (with-current-buffer gnus-original-article-buffer
8362             (goto-char (setq s (point-min)))
8363             (setq e (if (search-forward "\n\n" nil t)
8364                         (1- (point))
8365                       (point-max))))
8366           (insert-buffer-substring gnus-original-article-buffer s e)
8367           (article-decode-encoded-words)
8368           (if hidden
8369               (let ((gnus-treat-hide-headers nil)
8370                     (gnus-treat-hide-boring-headers nil))
8371                 (gnus-delete-wash-type 'headers)
8372                 (gnus-treat-article 'head))
8373             (gnus-treat-article 'head))
8374           (widen)
8375           (set-window-start window (goto-char (point-min)))
8376           (setq gnus-page-broken
8377                 (when gnus-break-pages
8378                   (gnus-narrow-to-page)
8379                   t))
8380           (gnus-set-mode-line 'article))))))
8381
8382 (defun gnus-summary-show-all-headers ()
8383   "Make all header lines visible."
8384   (interactive)
8385   (gnus-summary-toggle-header 1))
8386
8387 (defun gnus-summary-caesar-message (&optional arg)
8388   "Caesar rotate the current article by 13.
8389 The numerical prefix specifies how many places to rotate each letter
8390 forward."
8391   (interactive "P")
8392   (gnus-summary-select-article)
8393   (let ((mail-header-separator ""))
8394     (gnus-eval-in-buffer-window gnus-article-buffer
8395       (save-restriction
8396         (widen)
8397         (let ((start (window-start))
8398               buffer-read-only)
8399           (message-caesar-buffer-body arg)
8400           (set-window-start (get-buffer-window (current-buffer)) start))))))
8401
8402 (defun gnus-summary-stop-page-breaking ()
8403   "Stop page breaking in the current article."
8404   (interactive)
8405   (gnus-summary-select-article)
8406   (gnus-eval-in-buffer-window gnus-article-buffer
8407     (widen)
8408     (when (gnus-visual-p 'page-marker)
8409       (let ((buffer-read-only nil))
8410         (gnus-remove-text-with-property 'gnus-prev)
8411         (gnus-remove-text-with-property 'gnus-next))
8412       (setq gnus-page-broken nil))))
8413
8414 (defun gnus-summary-move-article (&optional n to-newsgroup
8415                                             select-method action)
8416   "Move the current article to a different newsgroup.
8417 If N is a positive number, move the N next articles.
8418 If N is a negative number, move the N previous articles.
8419 If N is nil and any articles have been marked with the process mark,
8420 move those articles instead.
8421 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8422 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8423 re-spool using this method.
8424
8425 When called interactively with TO-NEWSGROUP being nil, the value of
8426 the variable `gnus-move-split-methods' is used for finding a default
8427 for the target newsgroup.
8428
8429 For this function to work, both the current newsgroup and the
8430 newsgroup that you want to move to have to support the `request-move'
8431 and `request-accept' functions.
8432
8433 ACTION can be either `move' (the default), `crosspost' or `copy'."
8434   (interactive "P")
8435   (unless action
8436     (setq action 'move))
8437   ;; Check whether the source group supports the required functions.
8438   (cond ((and (eq action 'move)
8439               (not (gnus-check-backend-function
8440                     'request-move-article gnus-newsgroup-name)))
8441          (error "The current group does not support article moving"))
8442         ((and (eq action 'crosspost)
8443               (not (gnus-check-backend-function
8444                     'request-replace-article gnus-newsgroup-name)))
8445          (error "The current group does not support article editing")))
8446   (let ((articles (gnus-summary-work-articles n))
8447         (prefix (if (gnus-check-backend-function
8448                      'request-move-article gnus-newsgroup-name)
8449                     (gnus-group-real-prefix gnus-newsgroup-name)
8450                   ""))
8451         (names '((move "Move" "Moving")
8452                  (copy "Copy" "Copying")
8453                  (crosspost "Crosspost" "Crossposting")))
8454         (copy-buf (save-excursion
8455                     (nnheader-set-temp-buffer " *copy article*")))
8456         art-group to-method new-xref article to-groups)
8457     (unless (assq action names)
8458       (error "Unknown action %s" action))
8459     ;; Read the newsgroup name.
8460     (when (and (not to-newsgroup)
8461                (not select-method))
8462       (if (and gnus-move-split-methods
8463                (not
8464                 (and (memq gnus-current-article articles)
8465                      (gnus-buffer-live-p gnus-original-article-buffer))))
8466           ;; When `gnus-move-split-methods' is non-nil, we have to
8467           ;; select an article to give `gnus-read-move-group-name' an
8468           ;; opportunity to suggest an appropriate default.  However,
8469           ;; we needn't render or mark the article.
8470           (let ((gnus-display-mime-function nil)
8471                 (gnus-article-prepare-hook nil)
8472                 (gnus-mark-article-hook nil))
8473             (gnus-summary-select-article nil nil nil (car articles))))
8474       (setq to-newsgroup
8475             (gnus-read-move-group-name
8476              (cadr (assq action names))
8477              (symbol-value (intern (format "gnus-current-%s-group" action)))
8478              articles prefix))
8479       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8480     (setq to-method (or select-method
8481                         (gnus-server-to-method
8482                          (gnus-group-method to-newsgroup))))
8483     ;; Check the method we are to move this article to...
8484     (unless (gnus-check-backend-function
8485              'request-accept-article (car to-method))
8486       (error "%s does not support article copying" (car to-method)))
8487     (unless (gnus-check-server to-method)
8488       (error "Can't open server %s" (car to-method)))
8489     (gnus-message 6 "%s to %s: %s..."
8490                   (caddr (assq action names))
8491                   (or (car select-method) to-newsgroup) articles)
8492     (while articles
8493       (setq article (pop articles))
8494       (setq
8495        art-group
8496        (cond
8497         ;; Move the article.
8498         ((eq action 'move)
8499          ;; Remove this article from future suppression.
8500          (gnus-dup-unsuppress-article article)
8501          (gnus-request-move-article
8502           article                       ; Article to move
8503           gnus-newsgroup-name           ; From newsgroup
8504           (nth 1 (gnus-find-method-for-group
8505                   gnus-newsgroup-name)) ; Server
8506           (list 'gnus-request-accept-article
8507                 to-newsgroup (list 'quote select-method)
8508                 (not articles) t)       ; Accept form
8509           (not articles)))              ; Only save nov last time
8510         ;; Copy the article.
8511         ((eq action 'copy)
8512          (save-excursion
8513            (set-buffer copy-buf)
8514            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8515              (gnus-request-accept-article
8516               to-newsgroup select-method (not articles) t))))
8517         ;; Crosspost the article.
8518         ((eq action 'crosspost)
8519          (let ((xref (message-tokenize-header
8520                       (mail-header-xref (gnus-summary-article-header article))
8521                       " ")))
8522            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8523                                   ":" (number-to-string article)))
8524            (unless xref
8525              (setq xref (list (system-name))))
8526            (setq new-xref
8527                  (concat
8528                   (mapconcat 'identity
8529                              (delete "Xref:" (delete new-xref xref))
8530                              " ")
8531                   " " new-xref))
8532            (save-excursion
8533              (set-buffer copy-buf)
8534              ;; First put the article in the destination group.
8535              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8536              (when (consp (setq art-group
8537                                 (gnus-request-accept-article
8538                                  to-newsgroup select-method (not articles))))
8539                (setq new-xref (concat new-xref " " (car art-group)
8540                                       ":"
8541                                       (number-to-string (cdr art-group))))
8542                ;; Now we have the new Xrefs header, so we insert
8543                ;; it and replace the new article.
8544                (nnheader-replace-header "Xref" new-xref)
8545                (gnus-request-replace-article
8546                 (cdr art-group) to-newsgroup (current-buffer))
8547                art-group))))))
8548       (cond
8549        ((not art-group)
8550         (gnus-message 1 "Couldn't %s article %s: %s"
8551                       (cadr (assq action names)) article
8552                       (nnheader-get-report (car to-method))))
8553        ((eq art-group 'junk)
8554         (when (eq action 'move)
8555           (gnus-summary-mark-article article gnus-canceled-mark)
8556           (gnus-message 4 "Deleted article %s" article)))
8557        (t
8558         (let* ((pto-group (gnus-group-prefixed-name
8559                            (car art-group) to-method))
8560                (entry
8561                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8562                (info (nth 2 entry))
8563                (to-group (gnus-info-group info))
8564                to-marks)
8565           ;; Update the group that has been moved to.
8566           (when (and info
8567                      (memq action '(move copy)))
8568             (unless (member to-group to-groups)
8569               (push to-group to-groups))
8570
8571             (unless (memq article gnus-newsgroup-unreads)
8572               (push 'read to-marks)
8573               (gnus-info-set-read
8574                info (gnus-add-to-range (gnus-info-read info)
8575                                        (list (cdr art-group)))))
8576
8577             ;; See whether the article is to be put in the cache.
8578             (let ((marks gnus-article-mark-lists)
8579                   (to-article (cdr art-group)))
8580
8581               ;; Enter the article into the cache in the new group,
8582               ;; if that is required.
8583               (when gnus-use-cache
8584                 (gnus-cache-possibly-enter-article
8585                  to-group to-article
8586                  (memq article gnus-newsgroup-marked)
8587                  (memq article gnus-newsgroup-dormant)
8588                  (memq article gnus-newsgroup-unreads)))
8589
8590               (when gnus-preserve-marks
8591                 ;; Copy any marks over to the new group.
8592                 (when (and (equal to-group gnus-newsgroup-name)
8593                            (not (memq article gnus-newsgroup-unreads)))
8594                   ;; Mark this article as read in this group.
8595                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8596                   (setcdr (gnus-active to-group) to-article)
8597                   (setcdr gnus-newsgroup-active to-article))
8598
8599                 (while marks
8600                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8601                     (when (memq article (symbol-value
8602                                          (intern (format "gnus-newsgroup-%s"
8603                                                          (caar marks)))))
8604                       (push (cdar marks) to-marks)
8605                       ;; If the other group is the same as this group,
8606                       ;; then we have to add the mark to the list.
8607                       (when (equal to-group gnus-newsgroup-name)
8608                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8609                              (cons to-article
8610                                    (symbol-value
8611                                     (intern (format "gnus-newsgroup-%s"
8612                                                     (caar marks)))))))
8613                       ;; Copy the marks to other group.
8614                       (gnus-add-marked-articles
8615                        to-group (cdar marks) (list to-article) info)))
8616                   (setq marks (cdr marks)))
8617
8618                 (gnus-request-set-mark to-group (list (list (list to-article)
8619                                                             'add
8620                                                             to-marks))))
8621
8622               (gnus-dribble-enter
8623                (concat "(gnus-group-set-info '"
8624                        (gnus-prin1-to-string (gnus-get-info to-group))
8625                        ")"))))
8626
8627           ;; Update the Xref header in this article to point to
8628           ;; the new crossposted article we have just created.
8629           (when (eq action 'crosspost)
8630             (save-excursion
8631               (set-buffer copy-buf)
8632               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8633               (nnheader-replace-header "Xref" new-xref)
8634               (gnus-request-replace-article
8635                article gnus-newsgroup-name (current-buffer)))))
8636
8637         ;;;!!!Why is this necessary?
8638         (set-buffer gnus-summary-buffer)
8639
8640         (gnus-summary-goto-subject article)
8641         (when (eq action 'move)
8642           (gnus-summary-mark-article article gnus-canceled-mark))))
8643       (gnus-summary-remove-process-mark article))
8644     ;; Re-activate all groups that have been moved to.
8645     (save-excursion
8646       (set-buffer gnus-group-buffer)
8647       (let ((gnus-group-marked to-groups))
8648         (gnus-group-get-new-news-this-group nil t)))
8649
8650     (gnus-kill-buffer copy-buf)
8651     (gnus-summary-position-point)
8652     (gnus-set-mode-line 'summary)))
8653
8654 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8655   "Move the current article to a different newsgroup.
8656 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8657 When called interactively, if TO-NEWSGROUP is nil, use the value of
8658 the variable `gnus-move-split-methods' for finding a default target
8659 newsgroup.
8660 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8661 re-spool using this method."
8662   (interactive "P")
8663   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8664
8665 (defun gnus-summary-crosspost-article (&optional n)
8666   "Crosspost the current article to some other group."
8667   (interactive "P")
8668   (gnus-summary-move-article n nil nil 'crosspost))
8669
8670 (defcustom gnus-summary-respool-default-method nil
8671   "Default method type for respooling an article.
8672 If nil, use to the current newsgroup method."
8673   :type 'symbol
8674   :group 'gnus-summary-mail)
8675
8676 (defun gnus-summary-respool-article (&optional n method)
8677   "Respool the current article.
8678 The article will be squeezed through the mail spooling process again,
8679 which means that it will be put in some mail newsgroup or other
8680 depending on `nnmail-split-methods'.
8681 If N is a positive number, respool the N next articles.
8682 If N is a negative number, respool the N previous articles.
8683 If N is nil and any articles have been marked with the process mark,
8684 respool those articles instead.
8685
8686 Respooling can be done both from mail groups and \"real\" newsgroups.
8687 In the former case, the articles in question will be moved from the
8688 current group into whatever groups they are destined to.  In the
8689 latter case, they will be copied into the relevant groups."
8690   (interactive
8691    (list current-prefix-arg
8692          (let* ((methods (gnus-methods-using 'respool))
8693                 (methname
8694                  (symbol-name (or gnus-summary-respool-default-method
8695                                   (car (gnus-find-method-for-group
8696                                         gnus-newsgroup-name)))))
8697                 (method
8698                  (gnus-completing-read-with-default
8699                   methname "What backend do you want to use when respooling?"
8700                   methods nil t nil 'gnus-mail-method-history))
8701                 ms)
8702            (cond
8703             ((zerop (length (setq ms (gnus-servers-using-backend
8704                                       (intern method)))))
8705              (list (intern method) ""))
8706             ((= 1 (length ms))
8707              (car ms))
8708             (t
8709              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8710                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8711                            ms-alist))))))))
8712   (unless method
8713     (error "No method given for respooling"))
8714   (if (assoc (symbol-name
8715               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8716              (gnus-methods-using 'respool))
8717       (gnus-summary-move-article n nil method)
8718     (gnus-summary-copy-article n nil method)))
8719
8720 (defun gnus-summary-import-article (file &optional edit)
8721   "Import an arbitrary file into a mail newsgroup."
8722   (interactive "fImport file: \nP")
8723   (let ((group gnus-newsgroup-name)
8724         (now (current-time))
8725         atts lines group-art)
8726     (unless (gnus-check-backend-function 'request-accept-article group)
8727       (error "%s does not support article importing" group))
8728     (or (file-readable-p file)
8729         (not (file-regular-p file))
8730         (error "Can't read %s" file))
8731     (save-excursion
8732       (set-buffer (gnus-get-buffer-create " *import file*"))
8733       (erase-buffer)
8734       (nnheader-insert-file-contents file)
8735       (goto-char (point-min))
8736       (if (nnheader-article-p)
8737           (save-restriction
8738             (goto-char (point-min))
8739             (search-forward "\n\n" nil t)
8740             (narrow-to-region (point-min) (1- (point)))
8741             (goto-char (point-min))
8742             (unless (re-search-forward "^date:" nil t)
8743               (goto-char (point-max))
8744               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8745        ;; This doesn't look like an article, so we fudge some headers.
8746         (setq atts (file-attributes file)
8747               lines (count-lines (point-min) (point-max)))
8748         (insert "From: " (read-string "From: ") "\n"
8749                 "Subject: " (read-string "Subject: ") "\n"
8750                 "Date: " (message-make-date (nth 5 atts)) "\n"
8751                 "Message-ID: " (message-make-message-id) "\n"
8752                 "Lines: " (int-to-string lines) "\n"
8753                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8754       (setq group-art (gnus-request-accept-article group nil t))
8755       (kill-buffer (current-buffer)))
8756     (setq gnus-newsgroup-active (gnus-activate-group group))
8757     (forward-line 1)
8758     (gnus-summary-goto-article (cdr group-art) nil t)
8759     (when edit
8760       (gnus-summary-edit-article))))
8761
8762 (defun gnus-summary-create-article ()
8763   "Create an article in a mail newsgroup."
8764   (interactive)
8765   (let ((group gnus-newsgroup-name)
8766         (now (current-time))
8767         group-art)
8768     (unless (gnus-check-backend-function 'request-accept-article group)
8769       (error "%s does not support article importing" group))
8770     (save-excursion
8771       (set-buffer (gnus-get-buffer-create " *import file*"))
8772       (erase-buffer)
8773       (goto-char (point-min))
8774       ;; This doesn't look like an article, so we fudge some headers.
8775       (insert "From: " (read-string "From: ") "\n"
8776               "Subject: " (read-string "Subject: ") "\n"
8777               "Date: " (message-make-date now) "\n"
8778               "Message-ID: " (message-make-message-id) "\n")
8779       (setq group-art (gnus-request-accept-article group nil t))
8780       (kill-buffer (current-buffer)))
8781     (setq gnus-newsgroup-active (gnus-activate-group group))
8782     (forward-line 1)
8783     (gnus-summary-goto-article (cdr group-art) nil t)
8784     (gnus-summary-edit-article)))
8785
8786 (defun gnus-summary-article-posted-p ()
8787   "Say whether the current (mail) article is available from news as well.
8788 This will be the case if the article has both been mailed and posted."
8789   (interactive)
8790   (let ((id (mail-header-references (gnus-summary-article-header)))
8791         (gnus-override-method (car (gnus-refer-article-methods))))
8792     (if (gnus-request-head id "")
8793         (gnus-message 2 "The current message was found on %s"
8794                       gnus-override-method)
8795       (gnus-message 2 "The current message couldn't be found on %s"
8796                     gnus-override-method)
8797       nil)))
8798
8799 (defun gnus-summary-expire-articles (&optional now)
8800   "Expire all articles that are marked as expirable in the current group."
8801   (interactive)
8802   (when (gnus-check-backend-function
8803          'request-expire-articles gnus-newsgroup-name)
8804     ;; This backend supports expiry.
8805     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8806            (expirable (if total
8807                           (progn
8808                             ;; We need to update the info for
8809                             ;; this group for `gnus-list-of-read-articles'
8810                             ;; to give us the right answer.
8811                             (gnus-run-hooks 'gnus-exit-group-hook)
8812                             (gnus-summary-update-info)
8813                             (gnus-list-of-read-articles gnus-newsgroup-name))
8814                         (setq gnus-newsgroup-expirable
8815                               (sort gnus-newsgroup-expirable '<))))
8816            (expiry-wait (if now 'immediate
8817                           (gnus-group-find-parameter
8818                            gnus-newsgroup-name 'expiry-wait)))
8819            (nnmail-expiry-target
8820             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8821                 nnmail-expiry-target))
8822            es)
8823       (when expirable
8824         ;; There are expirable articles in this group, so we run them
8825         ;; through the expiry process.
8826         (gnus-message 6 "Expiring articles...")
8827         (unless (gnus-check-group gnus-newsgroup-name)
8828           (error "Can't open server for %s" gnus-newsgroup-name))
8829         ;; The list of articles that weren't expired is returned.
8830         (save-excursion
8831           (if expiry-wait
8832               (let ((nnmail-expiry-wait-function nil)
8833                     (nnmail-expiry-wait expiry-wait))
8834                 (setq es (gnus-request-expire-articles
8835                           expirable gnus-newsgroup-name)))
8836             (setq es (gnus-request-expire-articles
8837                       expirable gnus-newsgroup-name)))
8838           (unless total
8839             (setq gnus-newsgroup-expirable es))
8840           ;; We go through the old list of expirable, and mark all
8841           ;; really expired articles as nonexistent.
8842           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8843             (let ((gnus-use-cache nil))
8844               (dolist (article expirable)
8845                 (when (and (not (memq article es))
8846                            (gnus-data-find article))
8847                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8848         (gnus-message 6 "Expiring articles...done")))))
8849
8850 (defun gnus-summary-expire-articles-now ()
8851   "Expunge all expirable articles in the current group.
8852 This means that *all* articles that are marked as expirable will be
8853 deleted forever, right now."
8854   (interactive)
8855   (or gnus-expert-user
8856       (gnus-yes-or-no-p
8857        "Are you really, really, really sure you want to delete all these messages? ")
8858       (error "Phew!"))
8859   (gnus-summary-expire-articles t))
8860
8861 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8862 (defun gnus-summary-delete-article (&optional n)
8863   "Delete the N next (mail) articles.
8864 This command actually deletes articles.  This is not a marking
8865 command.  The article will disappear forever from your life, never to
8866 return.
8867 If N is negative, delete backwards.
8868 If N is nil and articles have been marked with the process mark,
8869 delete these instead."
8870   (interactive "P")
8871   (unless (gnus-check-backend-function 'request-expire-articles
8872                                        gnus-newsgroup-name)
8873     (error "The current newsgroup does not support article deletion"))
8874   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8875     (error "Couldn't open server"))
8876   ;; Compute the list of articles to delete.
8877   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8878         (nnmail-expiry-target 'delete)
8879         not-deleted)
8880     (if (and gnus-novice-user
8881              (not (gnus-yes-or-no-p
8882                    (format "Do you really want to delete %s forever? "
8883                            (if (> (length articles) 1)
8884                                (format "these %s articles" (length articles))
8885                              "this article")))))
8886         ()
8887       ;; Delete the articles.
8888       (setq not-deleted (gnus-request-expire-articles
8889                          articles gnus-newsgroup-name 'force))
8890       (while articles
8891         (gnus-summary-remove-process-mark (car articles))
8892         ;; The backend might not have been able to delete the article
8893         ;; after all.
8894         (unless (memq (car articles) not-deleted)
8895           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8896         (setq articles (cdr articles)))
8897       (when not-deleted
8898         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8899     (gnus-summary-position-point)
8900     (gnus-set-mode-line 'summary)
8901     not-deleted))
8902
8903 (defun gnus-summary-edit-article (&optional arg)
8904   "Edit the current article.
8905 This will have permanent effect only in mail groups.
8906 If ARG is nil, edit the decoded articles.
8907 If ARG is 1, edit the raw articles.
8908 If ARG is 2, edit the raw articles even in read-only groups.
8909 If ARG is 3, edit the articles with the current handles.
8910 Otherwise, allow editing of articles even in read-only
8911 groups."
8912   (interactive "P")
8913   (let (force raw current-handles)
8914     (cond
8915      ((null arg))
8916      ((eq arg 1)
8917       (setq raw t))
8918      ((eq arg 2)
8919       (setq raw t
8920             force t))
8921      ((eq arg 3)
8922       (setq current-handles
8923             (and (gnus-buffer-live-p gnus-article-buffer)
8924                  (with-current-buffer gnus-article-buffer
8925                    (prog1
8926                        gnus-article-mime-handles
8927                      (setq gnus-article-mime-handles nil))))))
8928      (t
8929       (setq force t)))
8930     (when (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8931       (error "Can't edit the raw article in group nndraft:drafts"))
8932     (save-excursion
8933       (set-buffer gnus-summary-buffer)
8934       (let ((mail-parse-charset gnus-newsgroup-charset)
8935             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8936         (gnus-set-global-variables)
8937         (when (and (not force)
8938                    (gnus-group-read-only-p))
8939           (error "The current newsgroup does not support article editing"))
8940         (gnus-summary-show-article t)
8941         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8942           (with-current-buffer gnus-article-buffer
8943             (mm-enable-multibyte)))
8944         (if (equal gnus-newsgroup-name "nndraft:drafts")
8945             (setq raw t))
8946         (gnus-article-edit-article
8947          (if raw 'ignore
8948            `(lambda ()
8949               (let ((mbl mml-buffer-list))
8950                 (setq mml-buffer-list nil)
8951                 (mime-to-mml ,'current-handles)
8952                 (let ((mbl1 mml-buffer-list))
8953                   (setq mml-buffer-list mbl)
8954                   (set (make-local-variable 'mml-buffer-list) mbl1))
8955                 (make-local-hook 'kill-buffer-hook)
8956                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8957          `(lambda (no-highlight)
8958             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8959                   (message-options message-options)
8960                   (message-options-set-recipient)
8961                   (mail-parse-ignored-charsets
8962                    ',gnus-newsgroup-ignored-charsets))
8963               ,(if (not raw) '(progn
8964                                 (mml-to-mime)
8965                                 (mml-destroy-buffers)
8966                                 (remove-hook 'kill-buffer-hook
8967                                              'mml-destroy-buffers t)
8968                                 (kill-local-variable 'mml-buffer-list)))
8969               (gnus-summary-edit-article-done
8970                ,(or (mail-header-references gnus-current-headers) "")
8971                ,(gnus-group-read-only-p)
8972                ,gnus-summary-buffer no-highlight))))))))
8973
8974 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8975
8976 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8977                                                  no-highlight)
8978   "Make edits to the current article permanent."
8979   (interactive)
8980   (save-excursion
8981    ;; The buffer restriction contains the entire article if it exists.
8982     (when (article-goto-body)
8983       (let ((lines (count-lines (point) (point-max)))
8984             (length (- (point-max) (point)))
8985             (case-fold-search t)
8986             (body (copy-marker (point))))
8987         (goto-char (point-min))
8988         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8989           (delete-region (match-beginning 1) (match-end 1))
8990           (insert (number-to-string length)))
8991         (goto-char (point-min))
8992         (when (re-search-forward
8993                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8994           (delete-region (match-beginning 1) (match-end 1))
8995           (insert (number-to-string length)))
8996         (goto-char (point-min))
8997         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8998           (delete-region (match-beginning 1) (match-end 1))
8999           (insert (number-to-string lines))))))
9000   ;; Replace the article.
9001   (let ((buf (current-buffer)))
9002     (with-temp-buffer
9003       (insert-buffer-substring buf)
9004
9005       (if (and (not read-only)
9006                (not (gnus-request-replace-article
9007                      (cdr gnus-article-current) (car gnus-article-current)
9008                      (current-buffer) t)))
9009           (error "Couldn't replace article")
9010         ;; Update the summary buffer.
9011         (if (and references
9012                  (equal (message-tokenize-header references " ")
9013                         (message-tokenize-header
9014                          (or (message-fetch-field "references") "") " ")))
9015             ;; We only have to update this line.
9016             (save-excursion
9017               (save-restriction
9018                 (message-narrow-to-head)
9019                 (let ((head (buffer-string))
9020                       header)
9021                   (with-temp-buffer
9022                     (insert (format "211 %d Article retrieved.\n"
9023                                     (cdr gnus-article-current)))
9024                     (insert head)
9025                     (insert ".\n")
9026                     (let ((nntp-server-buffer (current-buffer)))
9027                       (setq header (car (gnus-get-newsgroup-headers
9028                                          nil t))))
9029                     (save-excursion
9030                       (set-buffer gnus-summary-buffer)
9031                       (gnus-data-set-header
9032                        (gnus-data-find (cdr gnus-article-current))
9033                        header)
9034                       (gnus-summary-update-article-line
9035                        (cdr gnus-article-current) header)
9036                       (if (gnus-summary-goto-subject
9037                            (cdr gnus-article-current) nil t)
9038                           (gnus-summary-update-secondary-mark
9039                            (cdr gnus-article-current))))))))
9040           ;; Update threads.
9041           (set-buffer (or buffer gnus-summary-buffer))
9042           (gnus-summary-update-article (cdr gnus-article-current))
9043           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9044               (gnus-summary-update-secondary-mark
9045                (cdr gnus-article-current))))
9046         ;; Prettify the article buffer again.
9047         (unless no-highlight
9048           (save-excursion
9049             (set-buffer gnus-article-buffer)
9050             ;;;!!! Fix this -- article should be rehighlighted.
9051             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9052             (set-buffer gnus-original-article-buffer)
9053             (gnus-request-article
9054              (cdr gnus-article-current)
9055              (car gnus-article-current) (current-buffer))))
9056         ;; Prettify the summary buffer line.
9057         (when (gnus-visual-p 'summary-highlight 'highlight)
9058           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9059
9060 (defun gnus-summary-edit-wash (key)
9061   "Perform editing command KEY in the article buffer."
9062   (interactive
9063    (list
9064     (progn
9065       (message "%s" (concat (this-command-keys) "- "))
9066       (read-char))))
9067   (message "")
9068   (gnus-summary-edit-article)
9069   (execute-kbd-macro (concat (this-command-keys) key))
9070   (gnus-article-edit-done))
9071
9072 ;;; Respooling
9073
9074 (defun gnus-summary-respool-query (&optional silent trace)
9075   "Query where the respool algorithm would put this article."
9076   (interactive)
9077   (let (gnus-mark-article-hook)
9078     (gnus-summary-select-article)
9079     (save-excursion
9080       (set-buffer gnus-original-article-buffer)
9081       (save-restriction
9082         (message-narrow-to-head)
9083         (let ((groups (nnmail-article-group 'identity trace)))
9084           (unless silent
9085             (if groups
9086                 (message "This message would go to %s"
9087                          (mapconcat 'car groups ", "))
9088               (message "This message would go to no groups"))
9089             groups))))))
9090
9091 (defun gnus-summary-respool-trace ()
9092   "Trace where the respool algorithm would put this article.
9093 Display a buffer showing all fancy splitting patterns which matched."
9094   (interactive)
9095   (gnus-summary-respool-query nil t))
9096
9097 ;; Summary marking commands.
9098
9099 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9100   "Mark articles which has the same subject as read, and then select the next.
9101 If UNMARK is positive, remove any kind of mark.
9102 If UNMARK is negative, tick articles."
9103   (interactive "P")
9104   (when unmark
9105     (setq unmark (prefix-numeric-value unmark)))
9106   (let ((count
9107          (gnus-summary-mark-same-subject
9108           (gnus-summary-article-subject) unmark)))
9109     ;; Select next unread article.  If auto-select-same mode, should
9110     ;; select the first unread article.
9111     (gnus-summary-next-article t (and gnus-auto-select-same
9112                                       (gnus-summary-article-subject)))
9113     (gnus-message 7 "%d article%s marked as %s"
9114                   count (if (= count 1) " is" "s are")
9115                   (if unmark "unread" "read"))))
9116
9117 (defun gnus-summary-kill-same-subject (&optional unmark)
9118   "Mark articles which has the same subject as read.
9119 If UNMARK is positive, remove any kind of mark.
9120 If UNMARK is negative, tick articles."
9121   (interactive "P")
9122   (when unmark
9123     (setq unmark (prefix-numeric-value unmark)))
9124   (let ((count
9125          (gnus-summary-mark-same-subject
9126           (gnus-summary-article-subject) unmark)))
9127     ;; If marked as read, go to next unread subject.
9128     (when (null unmark)
9129       ;; Go to next unread subject.
9130       (gnus-summary-next-subject 1 t))
9131     (gnus-message 7 "%d articles are marked as %s"
9132                   count (if unmark "unread" "read"))))
9133
9134 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9135   "Mark articles with same SUBJECT as read, and return marked number.
9136 If optional argument UNMARK is positive, remove any kinds of marks.
9137 If optional argument UNMARK is negative, mark articles as unread instead."
9138   (let ((count 1))
9139     (save-excursion
9140       (cond
9141        ((null unmark)                   ; Mark as read.
9142         (while (and
9143                 (progn
9144                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9145                   (gnus-summary-show-thread) t)
9146                 (gnus-summary-find-subject subject))
9147           (setq count (1+ count))))
9148        ((> unmark 0)                    ; Tick.
9149         (while (and
9150                 (progn
9151                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9152                   (gnus-summary-show-thread) t)
9153                 (gnus-summary-find-subject subject))
9154           (setq count (1+ count))))
9155        (t                               ; Mark as unread.
9156         (while (and
9157                 (progn
9158                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9159                   (gnus-summary-show-thread) t)
9160                 (gnus-summary-find-subject subject))
9161           (setq count (1+ count)))))
9162       (gnus-set-mode-line 'summary)
9163       ;; Return the number of marked articles.
9164       count)))
9165
9166 (defun gnus-summary-mark-as-processable (n &optional unmark)
9167   "Set the process mark on the next N articles.
9168 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9169 the process mark instead.  The difference between N and the actual
9170 number of articles marked is returned."
9171   (interactive "P")
9172   (if (and (null n) (gnus-region-active-p))
9173       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9174     (setq n (prefix-numeric-value n))
9175     (let ((backward (< n 0))
9176           (n (abs n)))
9177       (while (and
9178               (> n 0)
9179               (if unmark
9180                   (gnus-summary-remove-process-mark
9181                    (gnus-summary-article-number))
9182                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9183               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9184         (setq n (1- n)))
9185       (when (/= 0 n)
9186         (gnus-message 7 "No more articles"))
9187       (gnus-summary-recenter)
9188       (gnus-summary-position-point)
9189       n)))
9190
9191 (defun gnus-summary-unmark-as-processable (n)
9192   "Remove the process mark from the next N articles.
9193 If N is negative, unmark backward instead.  The difference between N and
9194 the actual number of articles unmarked is returned."
9195   (interactive "P")
9196   (gnus-summary-mark-as-processable n t))
9197
9198 (defun gnus-summary-unmark-all-processable ()
9199   "Remove the process mark from all articles."
9200   (interactive)
9201   (save-excursion
9202     (while gnus-newsgroup-processable
9203       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9204   (gnus-summary-position-point))
9205
9206 (defun gnus-summary-add-mark (article type)
9207   "Mark ARTICLE with a mark of TYPE."
9208   (let ((vtype (car (assq type gnus-article-mark-lists)))
9209         var)
9210     (if (not vtype)
9211         (error "No such mark type: %s" type)
9212       (setq var (intern (format "gnus-newsgroup-%s" type)))
9213       (set var (cons article (symbol-value var)))
9214       (if (memq type '(processable cached replied forwarded recent saved))
9215           (gnus-summary-update-secondary-mark article)
9216         ;;; !!! This is bogus.  We should find out what primary
9217         ;;; !!! mark we want to set.
9218         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9219
9220 (defun gnus-summary-mark-as-expirable (n)
9221   "Mark N articles forward as expirable.
9222 If N is negative, mark backward instead.  The difference between N and
9223 the actual number of articles marked is returned."
9224   (interactive "p")
9225   (gnus-summary-mark-forward n gnus-expirable-mark))
9226
9227 (defun gnus-summary-mark-as-spam (n)
9228   "Mark N articles forward as spam.
9229 If N is negative, mark backward instead.  The difference between N and
9230 the actual number of articles marked is returned."
9231   (interactive "p")
9232   (gnus-summary-mark-forward n gnus-spam-mark))
9233
9234 (defun gnus-summary-mark-article-as-replied (article)
9235   "Mark ARTICLE as replied to and update the summary line.
9236 ARTICLE can also be a list of articles."
9237   (interactive (list (gnus-summary-article-number)))
9238   (let ((articles (if (listp article) article (list article))))
9239     (dolist (article articles)
9240       (push article gnus-newsgroup-replied)
9241       (let ((buffer-read-only nil))
9242         (when (gnus-summary-goto-subject article nil t)
9243           (gnus-summary-update-secondary-mark article))))))
9244
9245 (defun gnus-summary-mark-article-as-forwarded (article)
9246   "Mark ARTICLE as forwarded and update the summary line.
9247 ARTICLE can also be a list of articles."
9248   (let ((articles (if (listp article) article (list article))))
9249     (dolist (article articles)
9250       (push article gnus-newsgroup-forwarded)
9251       (let ((buffer-read-only nil))
9252         (when (gnus-summary-goto-subject article nil t)
9253           (gnus-summary-update-secondary-mark article))))))
9254
9255 (defun gnus-summary-set-bookmark (article)
9256   "Set a bookmark in current article."
9257   (interactive (list (gnus-summary-article-number)))
9258   (when (or (not (get-buffer gnus-article-buffer))
9259             (not gnus-current-article)
9260             (not gnus-article-current)
9261             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9262     (error "No current article selected"))
9263   ;; Remove old bookmark, if one exists.
9264   (let ((old (assq article gnus-newsgroup-bookmarks)))
9265     (when old
9266       (setq gnus-newsgroup-bookmarks
9267             (delq old gnus-newsgroup-bookmarks))))
9268   ;; Set the new bookmark, which is on the form
9269   ;; (article-number . line-number-in-body).
9270   (push
9271    (cons article
9272          (save-excursion
9273            (set-buffer gnus-article-buffer)
9274            (count-lines
9275             (min (point)
9276                  (save-excursion
9277                    (goto-char (point-min))
9278                    (search-forward "\n\n" nil t)
9279                    (point)))
9280             (point))))
9281    gnus-newsgroup-bookmarks)
9282   (gnus-message 6 "A bookmark has been added to the current article."))
9283
9284 (defun gnus-summary-remove-bookmark (article)
9285   "Remove the bookmark from the current article."
9286   (interactive (list (gnus-summary-article-number)))
9287   ;; Remove old bookmark, if one exists.
9288   (let ((old (assq article gnus-newsgroup-bookmarks)))
9289     (if old
9290         (progn
9291           (setq gnus-newsgroup-bookmarks
9292                 (delq old gnus-newsgroup-bookmarks))
9293           (gnus-message 6 "Removed bookmark."))
9294       (gnus-message 6 "No bookmark in current article."))))
9295
9296 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9297 (defun gnus-summary-mark-as-dormant (n)
9298   "Mark N articles forward as dormant.
9299 If N is negative, mark backward instead.  The difference between N and
9300 the actual number of articles marked is returned."
9301   (interactive "p")
9302   (gnus-summary-mark-forward n gnus-dormant-mark))
9303
9304 (defun gnus-summary-set-process-mark (article)
9305   "Set the process mark on ARTICLE and update the summary line."
9306   (setq gnus-newsgroup-processable
9307         (cons article
9308               (delq article gnus-newsgroup-processable)))
9309   (when (gnus-summary-goto-subject article)
9310     (gnus-summary-show-thread)
9311     (gnus-summary-goto-subject article)
9312     (gnus-summary-update-secondary-mark article)))
9313
9314 (defun gnus-summary-remove-process-mark (article)
9315   "Remove the process mark from ARTICLE and update the summary line."
9316   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9317   (when (gnus-summary-goto-subject article)
9318     (gnus-summary-show-thread)
9319     (gnus-summary-goto-subject article)
9320     (gnus-summary-update-secondary-mark article)))
9321
9322 (defun gnus-summary-set-saved-mark (article)
9323   "Set the process mark on ARTICLE and update the summary line."
9324   (push article gnus-newsgroup-saved)
9325   (when (gnus-summary-goto-subject article)
9326     (gnus-summary-update-secondary-mark article)))
9327
9328 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9329   "Mark N articles as read forwards.
9330 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9331 The difference between N and the actual number of articles marked is
9332 returned.
9333 Iff NO-EXPIRE, auto-expiry will be inhibited."
9334   (interactive "p")
9335   (gnus-summary-show-thread)
9336   (let ((backward (< n 0))
9337         (gnus-summary-goto-unread
9338          (and gnus-summary-goto-unread
9339               (not (eq gnus-summary-goto-unread 'never))
9340               (not (memq mark (list gnus-unread-mark
9341                                     gnus-ticked-mark gnus-dormant-mark)))))
9342         (n (abs n))
9343         (mark (or mark gnus-del-mark)))
9344     (while (and (> n 0)
9345                 (gnus-summary-mark-article nil mark no-expire)
9346                 (zerop (gnus-summary-next-subject
9347                         (if backward -1 1)
9348                         (and gnus-summary-goto-unread
9349                              (not (eq gnus-summary-goto-unread 'never)))
9350                         t)))
9351       (setq n (1- n)))
9352     (when (/= 0 n)
9353       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9354     (gnus-summary-recenter)
9355     (gnus-summary-position-point)
9356     (gnus-set-mode-line 'summary)
9357     n))
9358
9359 (defun gnus-summary-mark-article-as-read (mark)
9360   "Mark the current article quickly as read with MARK."
9361   (let ((article (gnus-summary-article-number)))
9362     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9363     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9364     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9365     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9366     (push (cons article mark) gnus-newsgroup-reads)
9367     ;; Possibly remove from cache, if that is used.
9368     (when gnus-use-cache
9369       (gnus-cache-enter-remove-article article))
9370     ;; Allow the backend to change the mark.
9371     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9372     ;; Check for auto-expiry.
9373     (when (and gnus-newsgroup-auto-expire
9374                (memq mark gnus-auto-expirable-marks))
9375       (setq mark gnus-expirable-mark)
9376       ;; Let the backend know about the mark change.
9377       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9378       (push article gnus-newsgroup-expirable))
9379     ;; Set the mark in the buffer.
9380     (gnus-summary-update-mark mark 'unread)
9381     t))
9382
9383 (defun gnus-summary-mark-article-as-unread (mark)
9384   "Mark the current article quickly as unread with MARK."
9385   (let* ((article (gnus-summary-article-number))
9386          (old-mark (gnus-summary-article-mark article)))
9387     ;; Allow the backend to change the mark.
9388     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9389     (if (eq mark old-mark)
9390         t
9391       (if (<= article 0)
9392           (progn
9393             (gnus-error 1 "Can't mark negative article numbers")
9394             nil)
9395         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9396         (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9397         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9398         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9399         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9400         (cond ((= mark gnus-ticked-mark)
9401                (setq gnus-newsgroup-marked
9402                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9403                                               article)))
9404               ((= mark gnus-spam-mark)
9405                (setq gnus-newsgroup-spam-marked
9406                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9407                                               article)))
9408               ((= mark gnus-dormant-mark)
9409                (setq gnus-newsgroup-dormant
9410                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9411                                               article)))
9412               (t
9413                (setq gnus-newsgroup-unreads
9414                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9415                                               article))))
9416         (gnus-pull article gnus-newsgroup-reads)
9417
9418         ;; See whether the article is to be put in the cache.
9419         (and gnus-use-cache
9420              (vectorp (gnus-summary-article-header article))
9421              (save-excursion
9422                (gnus-cache-possibly-enter-article
9423                 gnus-newsgroup-name article
9424                 (= mark gnus-ticked-mark)
9425                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9426
9427         ;; Fix the mark.
9428         (gnus-summary-update-mark mark 'unread)
9429         t))))
9430
9431 (defun gnus-summary-mark-article (&optional article mark no-expire)
9432   "Mark ARTICLE with MARK.  MARK can be any character.
9433 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9434 `??' (dormant) and `?E' (expirable).
9435 If MARK is nil, then the default character `?r' is used.
9436 If ARTICLE is nil, then the article on the current line will be
9437 marked.
9438 Iff NO-EXPIRE, auto-expiry will be inhibited."
9439   ;; The mark might be a string.
9440   (when (stringp mark)
9441     (setq mark (aref mark 0)))
9442   ;; If no mark is given, then we check auto-expiring.
9443   (when (null mark)
9444     (setq mark gnus-del-mark))
9445   (when (and (not no-expire)
9446              gnus-newsgroup-auto-expire
9447              (memq mark gnus-auto-expirable-marks))
9448     (setq mark gnus-expirable-mark))
9449   (let ((article (or article (gnus-summary-article-number)))
9450         (old-mark (gnus-summary-article-mark article)))
9451     ;; Allow the backend to change the mark.
9452     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9453     (if (eq mark old-mark)
9454         t
9455       (unless article
9456         (error "No article on current line"))
9457       (if (not (if (or (= mark gnus-unread-mark)
9458                        (= mark gnus-ticked-mark)
9459                        (= mark gnus-spam-mark)
9460                        (= mark gnus-dormant-mark))
9461                    (gnus-mark-article-as-unread article mark)
9462                  (gnus-mark-article-as-read article mark)))
9463           t
9464         ;; See whether the article is to be put in the cache.
9465         (and gnus-use-cache
9466              (not (= mark gnus-canceled-mark))
9467              (vectorp (gnus-summary-article-header article))
9468              (save-excursion
9469                (gnus-cache-possibly-enter-article
9470                 gnus-newsgroup-name article
9471                 (= mark gnus-ticked-mark)
9472                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9473
9474         (when (gnus-summary-goto-subject article nil t)
9475           (let ((buffer-read-only nil))
9476             (gnus-summary-show-thread)
9477             ;; Fix the mark.
9478             (gnus-summary-update-mark mark 'unread)
9479             t))))))
9480
9481 (defun gnus-summary-update-secondary-mark (article)
9482   "Update the secondary (read, process, cache) mark."
9483   (gnus-summary-update-mark
9484    (cond ((memq article gnus-newsgroup-processable)
9485           gnus-process-mark)
9486          ((memq article gnus-newsgroup-cached)
9487           gnus-cached-mark)
9488          ((memq article gnus-newsgroup-replied)
9489           gnus-replied-mark)
9490          ((memq article gnus-newsgroup-forwarded)
9491           gnus-forwarded-mark)
9492          ((memq article gnus-newsgroup-saved)
9493           gnus-saved-mark)
9494          ((memq article gnus-newsgroup-recent)
9495           gnus-recent-mark)
9496          ((memq article gnus-newsgroup-unseen)
9497           gnus-unseen-mark)
9498          (t gnus-no-mark))
9499    'replied)
9500   (when (gnus-visual-p 'summary-highlight 'highlight)
9501     (gnus-run-hooks 'gnus-summary-update-hook))
9502   t)
9503
9504 (defun gnus-summary-update-mark (mark type)
9505   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9506         (buffer-read-only nil))
9507     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9508     (when forward
9509       (when (looking-at "\r")
9510         (incf forward))
9511       (when (<= (+ forward (point)) (point-max))
9512         ;; Go to the right position on the line.
9513         (goto-char (+ forward (point)))
9514         ;; Replace the old mark with the new mark.
9515         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9516         ;; Optionally update the marks by some user rule.
9517         (when (eq type 'unread)
9518           (gnus-data-set-mark
9519            (gnus-data-find (gnus-summary-article-number)) mark)
9520           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9521
9522 (defun gnus-mark-article-as-read (article &optional mark)
9523   "Enter ARTICLE in the pertinent lists and remove it from others."
9524   ;; Make the article expirable.
9525   (let ((mark (or mark gnus-del-mark)))
9526     (setq gnus-newsgroup-expirable
9527           (if (= mark gnus-expirable-mark)
9528               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9529             (delq article gnus-newsgroup-expirable)))
9530     ;; Remove from unread and marked lists.
9531     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9532     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9533     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9534     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9535     (push (cons article mark) gnus-newsgroup-reads)
9536     ;; Possibly remove from cache, if that is used.
9537     (when gnus-use-cache
9538       (gnus-cache-enter-remove-article article))
9539     t))
9540
9541 (defun gnus-mark-article-as-unread (article &optional mark)
9542   "Enter ARTICLE in the pertinent lists and remove it from others."
9543   (let ((mark (or mark gnus-ticked-mark)))
9544     (if (<= article 0)
9545         (progn
9546           (gnus-error 1 "Can't mark negative article numbers")
9547           nil)
9548       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9549             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9550             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9551             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9552             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9553
9554       ;; Unsuppress duplicates?
9555       (when gnus-suppress-duplicates
9556         (gnus-dup-unsuppress-article article))
9557
9558       (cond ((= mark gnus-ticked-mark)
9559              (setq gnus-newsgroup-marked
9560                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9561             ((= mark gnus-spam-mark)
9562              (setq gnus-newsgroup-spam-marked
9563                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked article)))
9564             ((= mark gnus-dormant-mark)
9565              (setq gnus-newsgroup-dormant
9566                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9567             (t
9568              (setq gnus-newsgroup-unreads
9569                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9570       (gnus-pull article gnus-newsgroup-reads)
9571       t)))
9572
9573 (defalias 'gnus-summary-mark-as-unread-forward
9574   'gnus-summary-tick-article-forward)
9575 (make-obsolete 'gnus-summary-mark-as-unread-forward
9576                'gnus-summary-tick-article-forward)
9577 (defun gnus-summary-tick-article-forward (n)
9578   "Tick N articles forwards.
9579 If N is negative, tick backwards instead.
9580 The difference between N and the number of articles ticked is returned."
9581   (interactive "p")
9582   (gnus-summary-mark-forward n gnus-ticked-mark))
9583
9584 (defalias 'gnus-summary-mark-as-unread-backward
9585   'gnus-summary-tick-article-backward)
9586 (make-obsolete 'gnus-summary-mark-as-unread-backward
9587                'gnus-summary-tick-article-backward)
9588 (defun gnus-summary-tick-article-backward (n)
9589   "Tick N articles backwards.
9590 The difference between N and the number of articles ticked is returned."
9591   (interactive "p")
9592   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9593
9594 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9595 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9596 (defun gnus-summary-tick-article (&optional article clear-mark)
9597   "Mark current article as unread.
9598 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9599 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9600   (interactive)
9601   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9602                                        gnus-ticked-mark)))
9603
9604 (defun gnus-summary-mark-as-read-forward (n)
9605   "Mark N articles as read forwards.
9606 If N is negative, mark backwards instead.
9607 The difference between N and the actual number of articles marked is
9608 returned."
9609   (interactive "p")
9610   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9611
9612 (defun gnus-summary-mark-as-read-backward (n)
9613   "Mark the N articles as read backwards.
9614 The difference between N and the actual number of articles marked is
9615 returned."
9616   (interactive "p")
9617   (gnus-summary-mark-forward
9618    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9619
9620 (defun gnus-summary-mark-as-read (&optional article mark)
9621   "Mark current article as read.
9622 ARTICLE specifies the article to be marked as read.
9623 MARK specifies a string to be inserted at the beginning of the line."
9624   (gnus-summary-mark-article article mark))
9625
9626 (defun gnus-summary-clear-mark-forward (n)
9627   "Clear marks from N articles forward.
9628 If N is negative, clear backward instead.
9629 The difference between N and the number of marks cleared is returned."
9630   (interactive "p")
9631   (gnus-summary-mark-forward n gnus-unread-mark))
9632
9633 (defun gnus-summary-clear-mark-backward (n)
9634   "Clear marks from N articles backward.
9635 The difference between N and the number of marks cleared is returned."
9636   (interactive "p")
9637   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9638
9639 (defun gnus-summary-mark-unread-as-read ()
9640   "Intended to be used by `gnus-summary-mark-article-hook'."
9641   (when (memq gnus-current-article gnus-newsgroup-unreads)
9642     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9643
9644 (defun gnus-summary-mark-read-and-unread-as-read ()
9645   "Intended to be used by `gnus-summary-mark-article-hook'."
9646   (let ((mark (gnus-summary-article-mark)))
9647     (when (or (gnus-unread-mark-p mark)
9648               (gnus-read-mark-p mark))
9649       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9650
9651 (defun gnus-summary-mark-unread-as-ticked ()
9652   "Intended to be used by `gnus-summary-mark-article-hook'."
9653   (when (memq gnus-current-article gnus-newsgroup-unreads)
9654     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9655
9656 (defun gnus-summary-mark-region-as-read (point mark all)
9657   "Mark all unread articles between point and mark as read.
9658 If given a prefix, mark all articles between point and mark as read,
9659 even ticked and dormant ones."
9660   (interactive "r\nP")
9661   (save-excursion
9662     (let (article)
9663       (goto-char point)
9664       (beginning-of-line)
9665       (while (and
9666               (< (point) mark)
9667               (progn
9668                 (when (or all
9669                           (memq (setq article (gnus-summary-article-number))
9670                                 gnus-newsgroup-unreads))
9671                   (gnus-summary-mark-article article gnus-del-mark))
9672                 t)
9673               (gnus-summary-find-next))))))
9674
9675 (defun gnus-summary-mark-below (score mark)
9676   "Mark articles with score less than SCORE with MARK."
9677   (interactive "P\ncMark: ")
9678   (setq score (if score
9679                   (prefix-numeric-value score)
9680                 (or gnus-summary-default-score 0)))
9681   (save-excursion
9682     (set-buffer gnus-summary-buffer)
9683     (goto-char (point-min))
9684     (while
9685         (progn
9686           (and (< (gnus-summary-article-score) score)
9687                (gnus-summary-mark-article nil mark))
9688           (gnus-summary-find-next)))))
9689
9690 (defun gnus-summary-kill-below (&optional score)
9691   "Mark articles with score below SCORE as read."
9692   (interactive "P")
9693   (gnus-summary-mark-below score gnus-killed-mark))
9694
9695 (defun gnus-summary-clear-above (&optional score)
9696   "Clear all marks from articles with score above SCORE."
9697   (interactive "P")
9698   (gnus-summary-mark-above score gnus-unread-mark))
9699
9700 (defun gnus-summary-tick-above (&optional score)
9701   "Tick all articles with score above SCORE."
9702   (interactive "P")
9703   (gnus-summary-mark-above score gnus-ticked-mark))
9704
9705 (defun gnus-summary-mark-above (score mark)
9706   "Mark articles with score over SCORE with MARK."
9707   (interactive "P\ncMark: ")
9708   (setq score (if score
9709                   (prefix-numeric-value score)
9710                 (or gnus-summary-default-score 0)))
9711   (save-excursion
9712     (set-buffer gnus-summary-buffer)
9713     (goto-char (point-min))
9714     (while (and (progn
9715                   (when (> (gnus-summary-article-score) score)
9716                     (gnus-summary-mark-article nil mark))
9717                   t)
9718                 (gnus-summary-find-next)))))
9719
9720 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9721 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9722 (defun gnus-summary-limit-include-expunged (&optional no-error)
9723   "Display all the hidden articles that were expunged for low scores."
9724   (interactive)
9725   (let ((buffer-read-only nil))
9726     (let ((scored gnus-newsgroup-scored)
9727           headers h)
9728       (while scored
9729         (unless (gnus-summary-article-header (caar scored))
9730           (and (setq h (gnus-number-to-header (caar scored)))
9731                (< (cdar scored) gnus-summary-expunge-below)
9732                (push h headers)))
9733         (setq scored (cdr scored)))
9734       (if (not headers)
9735           (when (not no-error)
9736             (error "No expunged articles hidden"))
9737         (goto-char (point-min))
9738         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9739         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9740         (mapcar (lambda (x) (push (mail-header-number x)
9741                                   gnus-newsgroup-limit))
9742                 headers)
9743         (gnus-summary-prepare-unthreaded (nreverse headers))
9744         (goto-char (point-min))
9745         (gnus-summary-position-point)
9746         t))))
9747
9748 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9749   "Mark all unread articles in this newsgroup as read.
9750 If prefix argument ALL is non-nil, ticked and dormant articles will
9751 also be marked as read.
9752 If QUIETLY is non-nil, no questions will be asked.
9753 If TO-HERE is non-nil, it should be a point in the buffer.  All
9754 articles before (after, if REVERSE is set) this point will be marked as read.
9755 Note that this function will only catch up the unread article
9756 in the current summary buffer limitation.
9757 The number of articles marked as read is returned."
9758   (interactive "P")
9759   (prog1
9760       (save-excursion
9761         (when (or quietly
9762                   (not gnus-interactive-catchup) ;Without confirmation?
9763                   gnus-expert-user
9764                   (gnus-y-or-n-p
9765                    (if all
9766                        "Mark absolutely all articles as read? "
9767                      "Mark all unread articles as read? ")))
9768           (if (and not-mark
9769                    (not gnus-newsgroup-adaptive)
9770                    (not gnus-newsgroup-auto-expire)
9771                    (not gnus-suppress-duplicates)
9772                    (or (not gnus-use-cache)
9773                        (eq gnus-use-cache 'passive)))
9774               (progn
9775                 (when all
9776                   (setq gnus-newsgroup-marked nil
9777                         gnus-newsgroup-spam-marked nil
9778                         gnus-newsgroup-dormant nil))
9779                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9780             ;; We actually mark all articles as canceled, which we
9781             ;; have to do when using auto-expiry or adaptive scoring.
9782             (gnus-summary-show-all-threads)
9783             (if (and to-here reverse)
9784                 (progn
9785                   (goto-char to-here)
9786                   (while (and
9787                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9788                           (gnus-summary-find-next (not all) nil nil t))))
9789               (when (gnus-summary-first-subject (not all) t)
9790                 (while (and
9791                         (if to-here (< (point) to-here) t)
9792                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9793                         (gnus-summary-find-next (not all) nil nil t)))))
9794             (gnus-set-mode-line 'summary))
9795           t))
9796     (gnus-summary-position-point)))
9797
9798 (defun gnus-summary-catchup-to-here (&optional all)
9799   "Mark all unticked articles before the current one as read.
9800 If ALL is non-nil, also mark ticked and dormant articles as read."
9801   (interactive "P")
9802   (save-excursion
9803     (gnus-save-hidden-threads
9804       (let ((beg (point)))
9805         ;; We check that there are unread articles.
9806         (when (or all (gnus-summary-find-prev))
9807           (gnus-summary-catchup all t beg)))))
9808   (gnus-summary-position-point))
9809
9810 (defun gnus-summary-catchup-from-here (&optional all)
9811   "Mark all unticked articles after the current one as read.
9812 If ALL is non-nil, also mark ticked and dormant articles as read."
9813   (interactive "P")
9814   (save-excursion
9815     (gnus-save-hidden-threads
9816       (let ((beg (point)))
9817         ;; We check that there are unread articles.
9818         (when (or all (gnus-summary-find-next))
9819           (gnus-summary-catchup all t beg nil t)))))
9820
9821   (gnus-summary-position-point))
9822 (defun gnus-summary-catchup-all (&optional quietly)
9823   "Mark all articles in this newsgroup as read."
9824   (interactive "P")
9825   (gnus-summary-catchup t quietly))
9826
9827 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9828   "Mark all unread articles in this group as read, then exit.
9829 If prefix argument ALL is non-nil, all articles are marked as read.
9830 If QUIETLY is non-nil, no questions will be asked."
9831   (interactive "P")
9832   (when (gnus-summary-catchup all quietly nil 'fast)
9833     ;; Select next newsgroup or exit.
9834     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9835              (eq gnus-auto-select-next 'quietly))
9836         (gnus-summary-next-group nil)
9837       (gnus-summary-exit))))
9838
9839 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9840   "Mark all articles in this newsgroup as read, and then exit."
9841   (interactive "P")
9842   (gnus-summary-catchup-and-exit t quietly))
9843
9844 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9845   "Mark all articles in this group as read and select the next group.
9846 If given a prefix, mark all articles, unread as well as ticked, as
9847 read."
9848   (interactive "P")
9849   (save-excursion
9850     (gnus-summary-catchup all))
9851   (gnus-summary-next-group))
9852
9853 ;;;
9854 ;;; with article
9855 ;;;
9856
9857 (defmacro gnus-with-article (article &rest forms)
9858   "Select ARTICLE and perform FORMS in the original article buffer.
9859 Then replace the article with the result."
9860   `(progn
9861      ;; We don't want the article to be marked as read.
9862      (let (gnus-mark-article-hook)
9863        (gnus-summary-select-article t t nil ,article))
9864      (set-buffer gnus-original-article-buffer)
9865      ,@forms
9866      (if (not (gnus-check-backend-function
9867                'request-replace-article (car gnus-article-current)))
9868          (gnus-message 5 "Read-only group; not replacing")
9869        (unless (gnus-request-replace-article
9870                 ,article (car gnus-article-current)
9871                 (current-buffer) t)
9872          (error "Couldn't replace article")))
9873      ;; The cache and backlog have to be flushed somewhat.
9874      (when gnus-keep-backlog
9875        (gnus-backlog-remove-article
9876         (car gnus-article-current) (cdr gnus-article-current)))
9877      (when gnus-use-cache
9878        (gnus-cache-update-article
9879         (car gnus-article-current) (cdr gnus-article-current)))))
9880
9881 (put 'gnus-with-article 'lisp-indent-function 1)
9882 (put 'gnus-with-article 'edebug-form-spec '(form body))
9883
9884 ;; Thread-based commands.
9885
9886 (defun gnus-summary-articles-in-thread (&optional article)
9887   "Return a list of all articles in the current thread.
9888 If ARTICLE is non-nil, return all articles in the thread that starts
9889 with that article."
9890   (let* ((article (or article (gnus-summary-article-number)))
9891          (data (gnus-data-find-list article))
9892          (top-level (gnus-data-level (car data)))
9893          (top-subject
9894           (cond ((null gnus-thread-operation-ignore-subject)
9895                  (gnus-simplify-subject-re
9896                   (mail-header-subject (gnus-data-header (car data)))))
9897                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9898                  (gnus-simplify-subject-fuzzy
9899                   (mail-header-subject (gnus-data-header (car data)))))
9900                 (t nil)))
9901          (end-point (save-excursion
9902                       (if (gnus-summary-go-to-next-thread)
9903                           (point) (point-max))))
9904          articles)
9905     (while (and data
9906                 (< (gnus-data-pos (car data)) end-point))
9907       (when (or (not top-subject)
9908                 (string= top-subject
9909                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9910                              (gnus-simplify-subject-fuzzy
9911                               (mail-header-subject
9912                                (gnus-data-header (car data))))
9913                            (gnus-simplify-subject-re
9914                             (mail-header-subject
9915                              (gnus-data-header (car data)))))))
9916         (push (gnus-data-number (car data)) articles))
9917       (unless (and (setq data (cdr data))
9918                    (> (gnus-data-level (car data)) top-level))
9919         (setq data nil)))
9920     ;; Return the list of articles.
9921     (nreverse articles)))
9922
9923 (defun gnus-summary-rethread-current ()
9924   "Rethread the thread the current article is part of."
9925   (interactive)
9926   (let* ((gnus-show-threads t)
9927          (article (gnus-summary-article-number))
9928          (id (mail-header-id (gnus-summary-article-header)))
9929          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9930     (unless id
9931       (error "No article on the current line"))
9932     (gnus-rebuild-thread id)
9933     (gnus-summary-goto-subject article)))
9934
9935 (defun gnus-summary-reparent-thread ()
9936   "Make the current article child of the marked (or previous) article.
9937
9938 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9939 is non-nil or the Subject: of both articles are the same."
9940   (interactive)
9941   (unless (not (gnus-group-read-only-p))
9942     (error "The current newsgroup does not support article editing"))
9943   (unless (<= (length gnus-newsgroup-processable) 1)
9944     (error "No more than one article may be marked"))
9945   (save-window-excursion
9946     (let ((gnus-article-buffer " *reparent*")
9947           (current-article (gnus-summary-article-number))
9948           ;; First grab the marked article, otherwise one line up.
9949           (parent-article (if (not (null gnus-newsgroup-processable))
9950                               (car gnus-newsgroup-processable)
9951                             (save-excursion
9952                               (if (eq (forward-line -1) 0)
9953                                   (gnus-summary-article-number)
9954                                 (error "Beginning of summary buffer"))))))
9955       (unless (not (eq current-article parent-article))
9956         (error "An article may not be self-referential"))
9957       (let ((message-id (mail-header-id
9958                          (gnus-summary-article-header parent-article))))
9959         (unless (and message-id (not (equal message-id "")))
9960           (error "No message-id in desired parent"))
9961         (gnus-with-article current-article
9962           (save-restriction
9963             (goto-char (point-min))
9964             (message-narrow-to-head)
9965             (if (re-search-forward "^References: " nil t)
9966                 (progn
9967                   (re-search-forward "^[^ \t]" nil t)
9968                   (forward-line -1)
9969                   (end-of-line)
9970                   (insert " " message-id))
9971               (insert "References: " message-id "\n"))))
9972         (set-buffer gnus-summary-buffer)
9973         (gnus-summary-unmark-all-processable)
9974         (gnus-summary-update-article current-article)
9975         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9976             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9977         (gnus-summary-rethread-current)
9978         (gnus-message 3 "Article %d is now the child of article %d"
9979                       current-article parent-article)))))
9980
9981 (defun gnus-summary-toggle-threads (&optional arg)
9982   "Toggle showing conversation threads.
9983 If ARG is positive number, turn showing conversation threads on."
9984   (interactive "P")
9985   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9986     (setq gnus-show-threads
9987           (if (null arg) (not gnus-show-threads)
9988             (> (prefix-numeric-value arg) 0)))
9989     (gnus-summary-prepare)
9990     (gnus-summary-goto-subject current)
9991     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9992     (gnus-summary-position-point)))
9993
9994 (defun gnus-summary-show-all-threads ()
9995   "Show all threads."
9996   (interactive)
9997   (save-excursion
9998     (let ((buffer-read-only nil))
9999       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10000   (gnus-summary-position-point))
10001
10002 (defun gnus-summary-show-thread ()
10003   "Show thread subtrees.
10004 Returns nil if no thread was there to be shown."
10005   (interactive)
10006   (let ((buffer-read-only nil)
10007         (orig (point))
10008         ;; first goto end then to beg, to have point at beg after let
10009         (end (progn (end-of-line) (point)))
10010         (beg (progn (beginning-of-line) (point))))
10011     (prog1
10012         ;; Any hidden lines here?
10013         (search-forward "\r" end t)
10014       (subst-char-in-region beg end ?\^M ?\n t)
10015       (goto-char orig)
10016       (gnus-summary-position-point))))
10017
10018 (defun gnus-summary-maybe-hide-threads ()
10019   "If requested, hide the threads that should be hidden."
10020   (when (and gnus-show-threads
10021              gnus-thread-hide-subtree)
10022     (gnus-summary-hide-all-threads
10023      (if (or (consp gnus-thread-hide-subtree)
10024              (gnus-functionp gnus-thread-hide-subtree))
10025          (gnus-make-predicate gnus-thread-hide-subtree)
10026        nil))))
10027
10028 ;;; Hiding predicates.
10029
10030 (defun gnus-article-unread-p (header)
10031   (memq (mail-header-number header) gnus-newsgroup-unreads))
10032
10033 (defun gnus-article-unseen-p (header)
10034   (memq (mail-header-number header) gnus-newsgroup-unseen))
10035
10036 (defun gnus-map-articles (predicate articles)
10037   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10038   (apply 'gnus-or (mapcar predicate
10039                           (mapcar 'gnus-summary-article-header articles))))
10040
10041 (defun gnus-summary-hide-all-threads (&optional predicate)
10042   "Hide all thread subtrees.
10043 If PREDICATE is supplied, threads that satisfy this predicate
10044 will not be hidden."
10045   (interactive)
10046   (save-excursion
10047     (goto-char (point-min))
10048     (let ((end nil))
10049       (while (not end)
10050         (when (or (not predicate)
10051                   (gnus-map-articles
10052                    predicate (gnus-summary-article-children)))
10053             (gnus-summary-hide-thread))
10054         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10055   (gnus-summary-position-point))
10056
10057 (defun gnus-summary-hide-thread ()
10058   "Hide thread subtrees.
10059 If PREDICATE is supplied, threads that satisfy this predicate
10060 will not be hidden.
10061 Returns nil if no threads were there to be hidden."
10062   (interactive)
10063   (let ((buffer-read-only nil)
10064         (start (point))
10065         (article (gnus-summary-article-number)))
10066     (goto-char start)
10067     ;; Go forward until either the buffer ends or the subthread
10068     ;; ends.
10069     (when (and (not (eobp))
10070                (or (zerop (gnus-summary-next-thread 1 t))
10071                    (goto-char (point-max))))
10072       (prog1
10073           (if (and (> (point) start)
10074                    (search-backward "\n" start t))
10075               (progn
10076                 (subst-char-in-region start (point) ?\n ?\^M)
10077                 (gnus-summary-goto-subject article))
10078             (goto-char start)
10079             nil)))))
10080
10081 (defun gnus-summary-go-to-next-thread (&optional previous)
10082   "Go to the same level (or less) next thread.
10083 If PREVIOUS is non-nil, go to previous thread instead.
10084 Return the article number moved to, or nil if moving was impossible."
10085   (let ((level (gnus-summary-thread-level))
10086         (way (if previous -1 1))
10087         (beg (point)))
10088     (forward-line way)
10089     (while (and (not (eobp))
10090                 (< level (gnus-summary-thread-level)))
10091       (forward-line way))
10092     (if (eobp)
10093         (progn
10094           (goto-char beg)
10095           nil)
10096       (setq beg (point))
10097       (prog1
10098           (gnus-summary-article-number)
10099         (goto-char beg)))))
10100
10101 (defun gnus-summary-next-thread (n &optional silent)
10102   "Go to the same level next N'th thread.
10103 If N is negative, search backward instead.
10104 Returns the difference between N and the number of skips actually
10105 done.
10106
10107 If SILENT, don't output messages."
10108   (interactive "p")
10109   (let ((backward (< n 0))
10110         (n (abs n)))
10111     (while (and (> n 0)
10112                 (gnus-summary-go-to-next-thread backward))
10113       (decf n))
10114     (unless silent
10115       (gnus-summary-position-point))
10116     (when (and (not silent) (/= 0 n))
10117       (gnus-message 7 "No more threads"))
10118     n))
10119
10120 (defun gnus-summary-prev-thread (n)
10121   "Go to the same level previous N'th thread.
10122 Returns the difference between N and the number of skips actually
10123 done."
10124   (interactive "p")
10125   (gnus-summary-next-thread (- n)))
10126
10127 (defun gnus-summary-go-down-thread ()
10128   "Go down one level in the current thread."
10129   (let ((children (gnus-summary-article-children)))
10130     (when children
10131       (gnus-summary-goto-subject (car children)))))
10132
10133 (defun gnus-summary-go-up-thread ()
10134   "Go up one level in the current thread."
10135   (let ((parent (gnus-summary-article-parent)))
10136     (when parent
10137       (gnus-summary-goto-subject parent))))
10138
10139 (defun gnus-summary-down-thread (n)
10140   "Go down thread N steps.
10141 If N is negative, go up instead.
10142 Returns the difference between N and how many steps down that were
10143 taken."
10144   (interactive "p")
10145   (let ((up (< n 0))
10146         (n (abs n)))
10147     (while (and (> n 0)
10148                 (if up (gnus-summary-go-up-thread)
10149                   (gnus-summary-go-down-thread)))
10150       (setq n (1- n)))
10151     (gnus-summary-position-point)
10152     (when (/= 0 n)
10153       (gnus-message 7 "Can't go further"))
10154     n))
10155
10156 (defun gnus-summary-up-thread (n)
10157   "Go up thread N steps.
10158 If N is negative, go down instead.
10159 Returns the difference between N and how many steps down that were
10160 taken."
10161   (interactive "p")
10162   (gnus-summary-down-thread (- n)))
10163
10164 (defun gnus-summary-top-thread ()
10165   "Go to the top of the thread."
10166   (interactive)
10167   (while (gnus-summary-go-up-thread))
10168   (gnus-summary-article-number))
10169
10170 (defun gnus-summary-kill-thread (&optional unmark)
10171   "Mark articles under current thread as read.
10172 If the prefix argument is positive, remove any kinds of marks.
10173 If the prefix argument is negative, tick articles instead."
10174   (interactive "P")
10175   (when unmark
10176     (setq unmark (prefix-numeric-value unmark)))
10177   (let ((articles (gnus-summary-articles-in-thread)))
10178     (save-excursion
10179       ;; Expand the thread.
10180       (gnus-summary-show-thread)
10181       ;; Mark all the articles.
10182       (while articles
10183         (gnus-summary-goto-subject (car articles))
10184         (cond ((null unmark)
10185                (gnus-summary-mark-article-as-read gnus-killed-mark))
10186               ((> unmark 0)
10187                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10188               (t
10189                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10190         (setq articles (cdr articles))))
10191     ;; Hide killed subtrees.
10192     (and (null unmark)
10193          gnus-thread-hide-killed
10194          (gnus-summary-hide-thread))
10195     ;; If marked as read, go to next unread subject.
10196     (when (null unmark)
10197       ;; Go to next unread subject.
10198       (gnus-summary-next-subject 1 t)))
10199   (gnus-set-mode-line 'summary))
10200
10201 ;; Summary sorting commands
10202
10203 (defun gnus-summary-sort-by-number (&optional reverse)
10204   "Sort the summary buffer by article number.
10205 Argument REVERSE means reverse order."
10206   (interactive "P")
10207   (gnus-summary-sort 'number reverse))
10208
10209 (defun gnus-summary-sort-by-random (&optional reverse)
10210   "Randomize the order in the summary buffer.
10211 Argument REVERSE means to randomize in reverse order."
10212   (interactive "P")
10213   (gnus-summary-sort 'random reverse))
10214
10215 (defun gnus-summary-sort-by-author (&optional reverse)
10216   "Sort the summary buffer by author name alphabetically.
10217 If `case-fold-search' is non-nil, case of letters is ignored.
10218 Argument REVERSE means reverse order."
10219   (interactive "P")
10220   (gnus-summary-sort 'author reverse))
10221
10222 (defun gnus-summary-sort-by-subject (&optional reverse)
10223   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10224 If `case-fold-search' is non-nil, case of letters is ignored.
10225 Argument REVERSE means reverse order."
10226   (interactive "P")
10227   (gnus-summary-sort 'subject reverse))
10228
10229 (defun gnus-summary-sort-by-date (&optional reverse)
10230   "Sort the summary buffer by date.
10231 Argument REVERSE means reverse order."
10232   (interactive "P")
10233   (gnus-summary-sort 'date reverse))
10234
10235 (defun gnus-summary-sort-by-score (&optional reverse)
10236   "Sort the summary buffer by score.
10237 Argument REVERSE means reverse order."
10238   (interactive "P")
10239   (gnus-summary-sort 'score reverse))
10240
10241 (defun gnus-summary-sort-by-lines (&optional reverse)
10242   "Sort the summary buffer by the number of lines.
10243 Argument REVERSE means reverse order."
10244   (interactive "P")
10245   (gnus-summary-sort 'lines reverse))
10246
10247 (defun gnus-summary-sort-by-chars (&optional reverse)
10248   "Sort the summary buffer by article length.
10249 Argument REVERSE means reverse order."
10250   (interactive "P")
10251   (gnus-summary-sort 'chars reverse))
10252
10253 (defun gnus-summary-sort-by-original (&optional reverse)
10254   "Sort the summary buffer using the default sorting method.
10255 Argument REVERSE means reverse order."
10256   (interactive "P")
10257   (let* ((buffer-read-only)
10258          (gnus-summary-prepare-hook nil))
10259     ;; We do the sorting by regenerating the threads.
10260     (gnus-summary-prepare)
10261     ;; Hide subthreads if needed.
10262     (gnus-summary-maybe-hide-threads)))
10263
10264 (defun gnus-summary-sort (predicate reverse)
10265   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10266   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10267          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10268          (gnus-thread-sort-functions
10269           (if (not reverse)
10270               thread
10271             `(lambda (t1 t2)
10272                (,thread t2 t1))))
10273          (gnus-sort-gathered-threads-function
10274           gnus-thread-sort-functions)
10275          (gnus-article-sort-functions
10276           (if (not reverse)
10277               article
10278             `(lambda (t1 t2)
10279                (,article t2 t1))))
10280          (buffer-read-only)
10281          (gnus-summary-prepare-hook nil))
10282     ;; We do the sorting by regenerating the threads.
10283     (gnus-summary-prepare)
10284     ;; Hide subthreads if needed.
10285     (gnus-summary-maybe-hide-threads)))
10286
10287 ;; Summary saving commands.
10288
10289 (defun gnus-summary-save-article (&optional n not-saved)
10290   "Save the current article using the default saver function.
10291 If N is a positive number, save the N next articles.
10292 If N is a negative number, save the N previous articles.
10293 If N is nil and any articles have been marked with the process mark,
10294 save those articles instead.
10295 The variable `gnus-default-article-saver' specifies the saver function."
10296   (interactive "P")
10297   (let* ((articles (gnus-summary-work-articles n))
10298          (save-buffer (save-excursion
10299                         (nnheader-set-temp-buffer " *Gnus Save*")))
10300          (num (length articles))
10301          header file)
10302     (dolist (article articles)
10303       (setq header (gnus-summary-article-header article))
10304       (if (not (vectorp header))
10305           ;; This is a pseudo-article.
10306           (if (assq 'name header)
10307               (gnus-copy-file (cdr (assq 'name header)))
10308             (gnus-message 1 "Article %d is unsaveable" article))
10309         ;; This is a real article.
10310         (save-window-excursion
10311           (let ((gnus-display-mime-function nil)
10312                 (gnus-article-prepare-hook nil))
10313             (gnus-summary-select-article t nil nil article)))
10314         (save-excursion
10315           (set-buffer save-buffer)
10316           (erase-buffer)
10317           (insert-buffer-substring gnus-original-article-buffer))
10318         (setq file (gnus-article-save save-buffer file num))
10319         (gnus-summary-remove-process-mark article)
10320         (unless not-saved
10321           (gnus-summary-set-saved-mark article))))
10322     (gnus-kill-buffer save-buffer)
10323     (gnus-summary-position-point)
10324     (gnus-set-mode-line 'summary)
10325     n))
10326
10327 (defun gnus-summary-pipe-output (&optional arg)
10328   "Pipe the current article to a subprocess.
10329 If N is a positive number, pipe the N next articles.
10330 If N is a negative number, pipe the N previous articles.
10331 If N is nil and any articles have been marked with the process mark,
10332 pipe those articles instead."
10333   (interactive "P")
10334   (require 'gnus-art)
10335   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10336     (gnus-summary-save-article arg t))
10337   (let ((buffer (get-buffer "*Shell Command Output*")))
10338     (if (and buffer
10339              (with-current-buffer buffer (> (point-max) (point-min))))
10340         (gnus-configure-windows 'pipe))))
10341
10342 (defun gnus-summary-save-article-mail (&optional arg)
10343   "Append the current article to an mail file.
10344 If N is a positive number, save the N next articles.
10345 If N is a negative number, save the N previous articles.
10346 If N is nil and any articles have been marked with the process mark,
10347 save those articles instead."
10348   (interactive "P")
10349   (require 'gnus-art)
10350   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10351     (gnus-summary-save-article arg)))
10352
10353 (defun gnus-summary-save-article-rmail (&optional arg)
10354   "Append the current article to an rmail file.
10355 If N is a positive number, save the N next articles.
10356 If N is a negative number, save the N previous articles.
10357 If N is nil and any articles have been marked with the process mark,
10358 save those articles instead."
10359   (interactive "P")
10360   (require 'gnus-art)
10361   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10362     (gnus-summary-save-article arg)))
10363
10364 (defun gnus-summary-save-article-file (&optional arg)
10365   "Append the current article to a file.
10366 If N is a positive number, save the N next articles.
10367 If N is a negative number, save the N previous articles.
10368 If N is nil and any articles have been marked with the process mark,
10369 save those articles instead."
10370   (interactive "P")
10371   (require 'gnus-art)
10372   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10373     (gnus-summary-save-article arg)))
10374
10375 (defun gnus-summary-write-article-file (&optional arg)
10376   "Write the current article to a file, deleting the previous file.
10377 If N is a positive number, save the N next articles.
10378 If N is a negative number, save the N previous articles.
10379 If N is nil and any articles have been marked with the process mark,
10380 save those articles instead."
10381   (interactive "P")
10382   (require 'gnus-art)
10383   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10384     (gnus-summary-save-article arg)))
10385
10386 (defun gnus-summary-save-article-body-file (&optional arg)
10387   "Append the current article body to a file.
10388 If N is a positive number, save the N next articles.
10389 If N is a negative number, save the N previous articles.
10390 If N is nil and any articles have been marked with the process mark,
10391 save those articles instead."
10392   (interactive "P")
10393   (require 'gnus-art)
10394   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10395     (gnus-summary-save-article arg)))
10396
10397 (defun gnus-summary-muttprint (&optional arg)
10398   "Print the current article using Muttprint.
10399 If N is a positive number, save the N next articles.
10400 If N is a negative number, save the N previous articles.
10401 If N is nil and any articles have been marked with the process mark,
10402 save those articles instead."
10403   (interactive "P")
10404   (require 'gnus-art)
10405   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10406     (gnus-summary-save-article arg t)))
10407
10408 (defun gnus-summary-pipe-message (program)
10409   "Pipe the current article through PROGRAM."
10410   (interactive "sProgram: ")
10411   (gnus-summary-select-article)
10412   (let ((mail-header-separator ""))
10413     (gnus-eval-in-buffer-window gnus-article-buffer
10414       (save-restriction
10415         (widen)
10416         (let ((start (window-start))
10417               buffer-read-only)
10418           (message-pipe-buffer-body program)
10419           (set-window-start (get-buffer-window (current-buffer)) start))))))
10420
10421 (defun gnus-get-split-value (methods)
10422   "Return a value based on the split METHODS."
10423   (let (split-name method result match)
10424     (when methods
10425       (save-excursion
10426         (set-buffer gnus-original-article-buffer)
10427         (save-restriction
10428           (nnheader-narrow-to-headers)
10429           (while (and methods (not split-name))
10430             (goto-char (point-min))
10431             (setq method (pop methods))
10432             (setq match (car method))
10433             (when (cond
10434                    ((stringp match)
10435                     ;; Regular expression.
10436                     (ignore-errors
10437                       (re-search-forward match nil t)))
10438                    ((gnus-functionp match)
10439                     ;; Function.
10440                     (save-restriction
10441                       (widen)
10442                       (setq result (funcall match gnus-newsgroup-name))))
10443                    ((consp match)
10444                     ;; Form.
10445                     (save-restriction
10446                       (widen)
10447                       (setq result (eval match)))))
10448               (setq split-name (cdr method))
10449               (cond ((stringp result)
10450                      (push (expand-file-name
10451                             result gnus-article-save-directory)
10452                            split-name))
10453                     ((consp result)
10454                      (setq split-name (append result split-name)))))))))
10455     (nreverse split-name)))
10456
10457 (defun gnus-valid-move-group-p (group)
10458   (and (boundp group)
10459        (symbol-name group)
10460        (symbol-value group)
10461        (gnus-get-function (gnus-find-method-for-group
10462                            (symbol-name group)) 'request-accept-article t)))
10463
10464 (defun gnus-read-move-group-name (prompt default articles prefix)
10465   "Read a group name."
10466   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10467          (minibuffer-confirm-incomplete nil) ; XEmacs
10468          (prom
10469           (format "%s %s to:"
10470                   prompt
10471                   (if (> (length articles) 1)
10472                       (format "these %d articles" (length articles))
10473                     "this article")))
10474          (to-newsgroup
10475           (cond
10476            ((null split-name)
10477             (gnus-completing-read-with-default
10478              default prom
10479              gnus-active-hashtb
10480              'gnus-valid-move-group-p
10481              nil prefix
10482              'gnus-group-history))
10483            ((= 1 (length split-name))
10484             (gnus-completing-read-with-default
10485              (car split-name) prom
10486              gnus-active-hashtb
10487              'gnus-valid-move-group-p
10488              nil nil
10489              'gnus-group-history))
10490            (t
10491             (gnus-completing-read-with-default
10492              nil prom
10493              (mapcar (lambda (el) (list el))
10494                      (nreverse split-name))
10495              nil nil nil
10496              'gnus-group-history))))
10497          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10498     (when to-newsgroup
10499       (if (or (string= to-newsgroup "")
10500               (string= to-newsgroup prefix))
10501           (setq to-newsgroup default))
10502       (unless to-newsgroup
10503         (error "No group name entered"))
10504       (or (gnus-active to-newsgroup)
10505           (gnus-activate-group to-newsgroup nil nil to-method)
10506           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10507                                      to-newsgroup))
10508               (or (and (gnus-request-create-group to-newsgroup to-method)
10509                        (gnus-activate-group
10510                         to-newsgroup nil nil to-method)
10511                        (gnus-subscribe-group to-newsgroup))
10512                   (error "Couldn't create group %s" to-newsgroup)))
10513           (error "No such group: %s" to-newsgroup)))
10514     to-newsgroup))
10515
10516 (defun gnus-summary-save-parts (type dir n &optional reverse)
10517   "Save parts matching TYPE to DIR.
10518 If REVERSE, save parts that do not match TYPE."
10519   (interactive
10520    (list (read-string "Save parts of type: "
10521                       (or (car gnus-summary-save-parts-type-history)
10522                           gnus-summary-save-parts-default-mime)
10523                       'gnus-summary-save-parts-type-history)
10524          (setq gnus-summary-save-parts-last-directory
10525                (read-file-name "Save to directory: "
10526                                gnus-summary-save-parts-last-directory
10527                                nil t))
10528          current-prefix-arg))
10529   (gnus-summary-iterate n
10530     (let ((gnus-display-mime-function nil)
10531           (gnus-inhibit-treatment t))
10532       (gnus-summary-select-article))
10533     (save-excursion
10534       (set-buffer gnus-article-buffer)
10535       (let ((handles (or gnus-article-mime-handles
10536                          (mm-dissect-buffer nil gnus-article-loose-mime)
10537                          (mm-uu-dissect))))
10538         (when handles
10539           (gnus-summary-save-parts-1 type dir handles reverse)
10540           (unless gnus-article-mime-handles ;; Don't destroy this case.
10541             (mm-destroy-parts handles)))))))
10542
10543 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10544   (if (stringp (car handle))
10545       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10546               (cdr handle))
10547     (when (if reverse
10548               (not (string-match type (mm-handle-media-type handle)))
10549             (string-match type (mm-handle-media-type handle)))
10550       (let ((file (expand-file-name
10551                    (file-name-nondirectory
10552                     (or
10553                      (mail-content-type-get
10554                       (mm-handle-disposition handle) 'filename)
10555                      (concat gnus-newsgroup-name
10556                              "." (number-to-string
10557                                   (cdr gnus-article-current)))))
10558                    dir)))
10559         (unless (file-exists-p file)
10560           (mm-save-part-to-file handle file))))))
10561
10562 ;; Summary extract commands
10563
10564 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10565   (let ((buffer-read-only nil)
10566         (article (gnus-summary-article-number))
10567         after-article b e)
10568     (unless (gnus-summary-goto-subject article)
10569       (error "No such article: %d" article))
10570     (gnus-summary-position-point)
10571     ;; If all commands are to be bunched up on one line, we collect
10572     ;; them here.
10573     (unless gnus-view-pseudos-separately
10574       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10575             files action)
10576         (while ps
10577           (setq action (cdr (assq 'action (car ps))))
10578           (setq files (list (cdr (assq 'name (car ps)))))
10579           (while (and ps (cdr ps)
10580                       (string= (or action "1")
10581                                (or (cdr (assq 'action (cadr ps))) "2")))
10582             (push (cdr (assq 'name (cadr ps))) files)
10583             (setcdr ps (cddr ps)))
10584           (when files
10585             (when (not (string-match "%s" action))
10586               (push " " files))
10587             (push " " files)
10588             (when (assq 'execute (car ps))
10589               (setcdr (assq 'execute (car ps))
10590                       (funcall (if (string-match "%s" action)
10591                                    'format 'concat)
10592                                action
10593                                (mapconcat
10594                                 (lambda (f)
10595                                   (if (equal f " ")
10596                                       f
10597                                     (mm-quote-arg f)))
10598                                 files " ")))))
10599           (setq ps (cdr ps)))))
10600     (if (and gnus-view-pseudos (not not-view))
10601         (while pslist
10602           (when (assq 'execute (car pslist))
10603             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10604                                   (eq gnus-view-pseudos 'not-confirm)))
10605           (setq pslist (cdr pslist)))
10606       (save-excursion
10607         (while pslist
10608           (setq after-article (or (cdr (assq 'article (car pslist)))
10609                                   (gnus-summary-article-number)))
10610           (gnus-summary-goto-subject after-article)
10611           (forward-line 1)
10612           (setq b (point))
10613           (insert "    " (file-name-nondirectory
10614                           (cdr (assq 'name (car pslist))))
10615                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10616           (setq e (point))
10617           (forward-line -1)             ; back to `b'
10618           (gnus-add-text-properties
10619            b (1- e) (list 'gnus-number gnus-reffed-article-number
10620                           gnus-mouse-face-prop gnus-mouse-face))
10621           (gnus-data-enter
10622            after-article gnus-reffed-article-number
10623            gnus-unread-mark b (car pslist) 0 (- e b))
10624           (setq gnus-newsgroup-unreads
10625                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10626                                          gnus-reffed-article-number))
10627           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10628           (setq pslist (cdr pslist)))))))
10629
10630 (defun gnus-pseudos< (p1 p2)
10631   (let ((c1 (cdr (assq 'action p1)))
10632         (c2 (cdr (assq 'action p2))))
10633     (and c1 c2 (string< c1 c2))))
10634
10635 (defun gnus-request-pseudo-article (props)
10636   (cond ((assq 'execute props)
10637          (gnus-execute-command (cdr (assq 'execute props)))))
10638   (let ((gnus-current-article (gnus-summary-article-number)))
10639     (gnus-run-hooks 'gnus-mark-article-hook)))
10640
10641 (defun gnus-execute-command (command &optional automatic)
10642   (save-excursion
10643     (gnus-article-setup-buffer)
10644     (set-buffer gnus-article-buffer)
10645     (setq buffer-read-only nil)
10646     (let ((command (if automatic command
10647                      (read-string "Command: " (cons command 0)))))
10648       (erase-buffer)
10649       (insert "$ " command "\n\n")
10650       (if gnus-view-pseudo-asynchronously
10651           (start-process "gnus-execute" (current-buffer) shell-file-name
10652                          shell-command-switch command)
10653         (call-process shell-file-name nil t nil
10654                       shell-command-switch command)))))
10655
10656 ;; Summary kill commands.
10657
10658 (defun gnus-summary-edit-global-kill (article)
10659   "Edit the \"global\" kill file."
10660   (interactive (list (gnus-summary-article-number)))
10661   (gnus-group-edit-global-kill article))
10662
10663 (defun gnus-summary-edit-local-kill ()
10664   "Edit a local kill file applied to the current newsgroup."
10665   (interactive)
10666   (setq gnus-current-headers (gnus-summary-article-header))
10667   (gnus-group-edit-local-kill
10668    (gnus-summary-article-number) gnus-newsgroup-name))
10669
10670 ;;; Header reading.
10671
10672 (defun gnus-read-header (id &optional header)
10673   "Read the headers of article ID and enter them into the Gnus system."
10674   (let ((group gnus-newsgroup-name)
10675         (gnus-override-method
10676          (or
10677           gnus-override-method
10678           (and (gnus-news-group-p gnus-newsgroup-name)
10679                (car (gnus-refer-article-methods)))))
10680         where)
10681     ;; First we check to see whether the header in question is already
10682     ;; fetched.
10683     (if (stringp id)
10684         ;; This is a Message-ID.
10685         (setq header (or header (gnus-id-to-header id)))
10686       ;; This is an article number.
10687       (setq header (or header (gnus-summary-article-header id))))
10688     (if (and header
10689              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10690         ;; We have found the header.
10691         header
10692       ;; If this is a sparse article, we have to nix out its
10693       ;; previous entry in the thread hashtb.
10694       (when (and header
10695                  (gnus-summary-article-sparse-p (mail-header-number header)))
10696         (let* ((parent (gnus-parent-id (mail-header-references header)))
10697                (thread (and parent (gnus-id-to-thread parent))))
10698           (when thread
10699             (delq (assq header thread) thread))))
10700       ;; We have to really fetch the header to this article.
10701       (save-excursion
10702         (set-buffer nntp-server-buffer)
10703         (when (setq where (gnus-request-head id group))
10704           (nnheader-fold-continuation-lines)
10705           (goto-char (point-max))
10706           (insert ".\n")
10707           (goto-char (point-min))
10708           (insert "211 ")
10709           (princ (cond
10710                   ((numberp id) id)
10711                   ((cdr where) (cdr where))
10712                   (header (mail-header-number header))
10713                   (t gnus-reffed-article-number))
10714                  (current-buffer))
10715           (insert " Article retrieved.\n"))
10716         (if (or (not where)
10717                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10718             ()                          ; Malformed head.
10719           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10720             (when (and (stringp id)
10721                        (not (string= (gnus-group-real-name group)
10722                                      (car where))))
10723               ;; If we fetched by Message-ID and the article came
10724               ;; from a different group, we fudge some bogus article
10725               ;; numbers for this article.
10726               (mail-header-set-number header gnus-reffed-article-number))
10727             (save-excursion
10728               (set-buffer gnus-summary-buffer)
10729               (decf gnus-reffed-article-number)
10730               (gnus-remove-header (mail-header-number header))
10731               (push header gnus-newsgroup-headers)
10732               (setq gnus-current-headers header)
10733               (push (mail-header-number header) gnus-newsgroup-limit)))
10734           header)))))
10735
10736 (defun gnus-remove-header (number)
10737   "Remove header NUMBER from `gnus-newsgroup-headers'."
10738   (if (and gnus-newsgroup-headers
10739            (= number (mail-header-number (car gnus-newsgroup-headers))))
10740       (pop gnus-newsgroup-headers)
10741     (let ((headers gnus-newsgroup-headers))
10742       (while (and (cdr headers)
10743                   (not (= number (mail-header-number (cadr headers)))))
10744         (pop headers))
10745       (when (cdr headers)
10746         (setcdr headers (cddr headers))))))
10747
10748 ;;;
10749 ;;; summary highlights
10750 ;;;
10751
10752 (defun gnus-highlight-selected-summary ()
10753   "Highlight selected article in summary buffer."
10754   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10755   (when gnus-summary-selected-face
10756     (save-excursion
10757       (let* ((beg (progn (beginning-of-line) (point)))
10758              (end (progn (end-of-line) (point)))
10759              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10760              (from (if (get-text-property beg gnus-mouse-face-prop)
10761                        beg
10762                      (or (next-single-property-change
10763                           beg gnus-mouse-face-prop nil end)
10764                          beg)))
10765              (to
10766               (if (= from end)
10767                   (- from 2)
10768                 (or (next-single-property-change
10769                      from gnus-mouse-face-prop nil end)
10770                     end))))
10771         ;; If no mouse-face prop on line we will have to = from = end,
10772         ;; so we highlight the entire line instead.
10773         (when (= (+ to 2) from)
10774           (setq from beg)
10775           (setq to end))
10776         (if gnus-newsgroup-selected-overlay
10777             ;; Move old overlay.
10778             (gnus-move-overlay
10779              gnus-newsgroup-selected-overlay from to (current-buffer))
10780           ;; Create new overlay.
10781           (gnus-overlay-put
10782            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10783            'face gnus-summary-selected-face))))))
10784
10785 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10786 (defun gnus-summary-highlight-line ()
10787   "Highlight current line according to `gnus-summary-highlight'."
10788   (let* ((list gnus-summary-highlight)
10789          (beg (gnus-point-at-bol))
10790          (article (gnus-summary-article-number))
10791          (score (or (cdr (assq (or article gnus-current-article)
10792                                gnus-newsgroup-scored))
10793                     gnus-summary-default-score 0))
10794          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10795          (inhibit-read-only t)
10796          (default gnus-summary-default-score)
10797          (default-high gnus-summary-default-high-score)
10798          (default-low gnus-summary-default-low-score))
10799     ;; Eval the cars of the lists until we find a match.
10800     (while (and list
10801                 (not (eval (caar list))))
10802       (setq list (cdr list)))
10803     (let ((face (cdar list)))
10804       (unless (eq face (get-text-property beg 'face))
10805         (gnus-put-text-property-excluding-characters-with-faces
10806          beg (gnus-point-at-eol) 'face
10807          (setq face (if (boundp face) (symbol-value face) face)))
10808         (when gnus-summary-highlight-line-function
10809           (funcall gnus-summary-highlight-line-function article face))))))
10810
10811 (defun gnus-update-read-articles (group unread &optional compute)
10812   "Update the list of read articles in GROUP.
10813 UNREAD is a sorted list."
10814   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10815          (entry (gnus-gethash group gnus-newsrc-hashtb))
10816          (info (nth 2 entry))
10817          (prev 1)
10818          read)
10819     (if (or (not info) (not active))
10820         ;; There is no info on this group if it was, in fact,
10821         ;; killed.  Gnus stores no information on killed groups, so
10822         ;; there's nothing to be done.
10823         ;; One could store the information somewhere temporarily,
10824         ;; perhaps...  Hmmm...
10825         ()
10826       ;; Remove any negative articles numbers.
10827       (while (and unread (< (car unread) 0))
10828         (setq unread (cdr unread)))
10829       ;; Remove any expired article numbers
10830       (while (and unread (< (car unread) (car active)))
10831         (setq unread (cdr unread)))
10832       ;; Compute the ranges of read articles by looking at the list of
10833       ;; unread articles.
10834       (while unread
10835         (when (/= (car unread) prev)
10836           (push (if (= prev (1- (car unread))) prev
10837                   (cons prev (1- (car unread))))
10838                 read))
10839         (setq prev (1+ (car unread)))
10840         (setq unread (cdr unread)))
10841       (when (<= prev (cdr active))
10842         (push (cons prev (cdr active)) read))
10843       (setq read (if (> (length read) 1) (nreverse read) read))
10844       (if compute
10845           read
10846         (save-excursion
10847           (let (setmarkundo)
10848             ;; Propagate the read marks to the backend.
10849             (when (gnus-check-backend-function 'request-set-mark group)
10850               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10851                     (add (gnus-remove-from-range read (gnus-info-read info))))
10852                 (when (or add del)
10853                   (unless (gnus-check-group group)
10854                     (error "Can't open server for %s" group))
10855                   (gnus-request-set-mark
10856                    group (delq nil (list (if add (list add 'add '(read)))
10857                                          (if del (list del 'del '(read))))))
10858                   (setq setmarkundo
10859                         `(gnus-request-set-mark
10860                           ,group
10861                           ',(delq nil (list
10862                                        (if del (list del 'add '(read)))
10863                                        (if add (list add 'del '(read))))))))))
10864             (set-buffer gnus-group-buffer)
10865             (gnus-undo-register
10866               `(progn
10867                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10868                  (gnus-info-set-read ',info ',(gnus-info-read info))
10869                  (gnus-get-unread-articles-in-group ',info
10870                                                     (gnus-active ,group))
10871                  (gnus-group-update-group ,group t)
10872                  ,setmarkundo))))
10873         ;; Enter this list into the group info.
10874         (gnus-info-set-read info read)
10875         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10876         (gnus-get-unread-articles-in-group info (gnus-active group))
10877         t))))
10878
10879 (defun gnus-offer-save-summaries ()
10880   "Offer to save all active summary buffers."
10881   (let (buffers)
10882     ;; Go through all buffers and find all summaries.
10883     (dolist (buffer (buffer-list))
10884       (when (and (setq buffer (buffer-name buffer))
10885                  (string-match "Summary" buffer)
10886                  (save-excursion
10887                    (set-buffer buffer)
10888                    ;; We check that this is, indeed, a summary buffer.
10889                    (and (eq major-mode 'gnus-summary-mode)
10890                         ;; Also make sure this isn't bogus.
10891                         gnus-newsgroup-prepared
10892                         ;; Also make sure that this isn't a
10893                         ;; dead summary buffer.
10894                         (not gnus-dead-summary-mode))))
10895         (push buffer buffers)))
10896     ;; Go through all these summary buffers and offer to save them.
10897     (when buffers
10898       (save-excursion
10899         (map-y-or-n-p
10900          "Update summary buffer %s? "
10901          (lambda (buf)
10902            (switch-to-buffer buf)
10903            (gnus-summary-exit))
10904          buffers)))))
10905
10906 (defun gnus-summary-setup-default-charset ()
10907   "Setup newsgroup default charset."
10908   (if (equal gnus-newsgroup-name "nndraft:drafts")
10909       (setq gnus-newsgroup-charset nil)
10910     (let* ((ignored-charsets
10911             (or gnus-newsgroup-ephemeral-ignored-charsets
10912                 (append
10913                  (and gnus-newsgroup-name
10914                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10915                  gnus-newsgroup-ignored-charsets))))
10916       (setq gnus-newsgroup-charset
10917             (or gnus-newsgroup-ephemeral-charset
10918                 (and gnus-newsgroup-name
10919                      (gnus-parameter-charset gnus-newsgroup-name))
10920                 gnus-default-charset))
10921       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10922            ignored-charsets))))
10923
10924 ;;;
10925 ;;; Mime Commands
10926 ;;;
10927
10928 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10929   "Display the current article buffer fully MIME-buttonized.
10930 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10931 treated as multipart/mixed."
10932   (interactive "P")
10933   (require 'gnus-art)
10934   (let ((gnus-unbuttonized-mime-types nil)
10935         (gnus-mime-display-multipart-as-mixed show-all-parts))
10936     (gnus-summary-show-article)))
10937
10938 (defun gnus-summary-repair-multipart (article)
10939   "Add a Content-Type header to a multipart article without one."
10940   (interactive (list (gnus-summary-article-number)))
10941   (gnus-with-article article
10942     (message-narrow-to-head)
10943     (message-remove-header "Mime-Version")
10944     (goto-char (point-max))
10945     (insert "Mime-Version: 1.0\n")
10946     (widen)
10947     (when (search-forward "\n--" nil t)
10948       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10949         (message-narrow-to-head)
10950         (message-remove-header "Content-Type")
10951         (goto-char (point-max))
10952         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10953                         separator))
10954         (widen))))
10955   (let (gnus-mark-article-hook)
10956     (gnus-summary-select-article t t nil article)))
10957
10958 (defun gnus-summary-toggle-display-buttonized ()
10959   "Toggle the buttonizing of the article buffer."
10960   (interactive)
10961   (require 'gnus-art)
10962   (if (setq gnus-inhibit-mime-unbuttonizing
10963             (not gnus-inhibit-mime-unbuttonizing))
10964       (let ((gnus-unbuttonized-mime-types nil))
10965         (gnus-summary-show-article))
10966     (gnus-summary-show-article)))
10967
10968 ;;;
10969 ;;; Generic summary marking commands
10970 ;;;
10971
10972 (defvar gnus-summary-marking-alist
10973   '((read gnus-del-mark "d")
10974     (unread gnus-unread-mark "u")
10975     (ticked gnus-ticked-mark "!")
10976     (dormant gnus-dormant-mark "?")
10977     (expirable gnus-expirable-mark "e"))
10978   "An alist of names/marks/keystrokes.")
10979
10980 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10981 (defvar gnus-summary-mark-map)
10982
10983 (defun gnus-summary-make-all-marking-commands ()
10984   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10985   (dolist (elem gnus-summary-marking-alist)
10986     (apply 'gnus-summary-make-marking-command elem)))
10987
10988 (defun gnus-summary-make-marking-command (name mark keystroke)
10989   (let ((map (make-sparse-keymap)))
10990     (define-key gnus-summary-generic-mark-map keystroke map)
10991     (dolist (lway `((next "next" next nil "n")
10992                     (next-unread "next unread" next t "N")
10993                     (prev "previous" prev nil "p")
10994                     (prev-unread "previous unread" prev t "P")
10995                     (nomove "" nil nil ,keystroke)))
10996       (let ((func (gnus-summary-make-marking-command-1
10997                    mark (car lway) lway name)))
10998         (setq func (eval func))
10999         (define-key map (nth 4 lway) func)))))
11000
11001 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11002   `(defun ,(intern
11003             (format "gnus-summary-put-mark-as-%s%s"
11004                     name (if (eq way 'nomove)
11005                              ""
11006                            (concat "-" (symbol-name way)))))
11007      (n)
11008      ,(format
11009        "Mark the current article as %s%s.
11010 If N, the prefix, then repeat N times.
11011 If N is negative, move in reverse order.
11012 The difference between N and the actual number of articles marked is
11013 returned."
11014        name (cadr lway))
11015      (interactive "p")
11016      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11017
11018 (defun gnus-summary-generic-mark (n mark move unread)
11019   "Mark N articles with MARK."
11020   (unless (eq major-mode 'gnus-summary-mode)
11021     (error "This command can only be used in the summary buffer"))
11022   (gnus-summary-show-thread)
11023   (let ((nummove
11024          (cond
11025           ((eq move 'next) 1)
11026           ((eq move 'prev) -1)
11027           (t 0))))
11028     (if (zerop nummove)
11029         (setq n 1)
11030       (when (< n 0)
11031         (setq n (abs n)
11032               nummove (* -1 nummove))))
11033     (while (and (> n 0)
11034                 (gnus-summary-mark-article nil mark)
11035                 (zerop (gnus-summary-next-subject nummove unread t)))
11036       (setq n (1- n)))
11037     (when (/= 0 n)
11038       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11039     (gnus-summary-recenter)
11040     (gnus-summary-position-point)
11041     (gnus-set-mode-line 'summary)
11042     n))
11043
11044 (defun gnus-summary-insert-articles (articles)
11045   (when (setq articles
11046               (gnus-sorted-difference articles
11047                                       (mapcar (lambda (h)
11048                                                 (mail-header-number h))
11049                                               gnus-newsgroup-headers)))
11050     (setq gnus-newsgroup-headers
11051           (merge 'list
11052                  gnus-newsgroup-headers
11053                  (gnus-fetch-headers articles)
11054                  'gnus-article-sort-by-number))
11055     ;; Suppress duplicates?
11056     (when gnus-suppress-duplicates
11057       (gnus-dup-suppress-articles))
11058
11059     ;; We might want to build some more threads first.
11060     (when (and gnus-fetch-old-headers
11061                (eq gnus-headers-retrieved-by 'nov))
11062       (if (eq gnus-fetch-old-headers 'invisible)
11063           (gnus-build-all-threads)
11064         (gnus-build-old-threads)))
11065     ;; Let the Gnus agent mark articles as read.
11066     (when gnus-agent
11067       (gnus-agent-get-undownloaded-list))
11068     ;; Remove list identifiers from subject
11069     (when gnus-list-identifiers
11070       (gnus-summary-remove-list-identifiers))
11071     ;; First and last article in this newsgroup.
11072     (when gnus-newsgroup-headers
11073       (setq gnus-newsgroup-begin
11074             (mail-header-number (car gnus-newsgroup-headers))
11075             gnus-newsgroup-end
11076             (mail-header-number
11077              (gnus-last-element gnus-newsgroup-headers))))
11078     (when gnus-use-scoring
11079       (gnus-possibly-score-headers))))
11080
11081 (defun gnus-summary-insert-old-articles (&optional all)
11082   "Insert all old articles in this group.
11083 If ALL is non-nil, already read articles become readable.
11084 If ALL is a number, fetch this number of articles."
11085   (interactive "P")
11086   (prog1
11087       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11088             older len)
11089         (setq older
11090               (gnus-sorted-difference
11091                (gnus-uncompress-range (list gnus-newsgroup-active))
11092                old))
11093         (setq len (length older))
11094         (cond
11095          ((null older) nil)
11096          ((numberp all)
11097           (if (< all len)
11098               (setq older (last older all))))
11099          (all nil)
11100          (t
11101           (if (and (numberp gnus-large-newsgroup)
11102                    (> len gnus-large-newsgroup))
11103               (let* ((cursor-in-echo-area nil)
11104                      (initial (gnus-parameter-large-newsgroup-initial
11105                                gnus-newsgroup-name))
11106                      (input
11107                       (read-string
11108                        (format
11109                         "How many articles from %s (%s %d): "
11110                         (gnus-limit-string
11111                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11112                         (if initial "max" "default")
11113                         len)
11114                        (if initial
11115                            (cons (number-to-string initial)
11116                                  0)))))
11117                 (unless (string-match "^[ \t]*$" input)
11118                   (setq all (string-to-number input))
11119                   (if (< all len)
11120                       (setq older (last older all))))))))
11121         (if (not older)
11122             (message "No old news.")
11123           (gnus-summary-insert-articles older)
11124           (gnus-summary-limit (gnus-sorted-nunion old older))))
11125     (gnus-summary-position-point)))
11126
11127 (defun gnus-summary-insert-new-articles ()
11128   "Insert all new articles in this group."
11129   (interactive)
11130   (prog1
11131       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11132             (old-active gnus-newsgroup-active)
11133             (nnmail-fetched-sources (list t))
11134             i new)
11135         (setq gnus-newsgroup-active
11136               (gnus-activate-group gnus-newsgroup-name 'scan))
11137         (setq i (cdr gnus-newsgroup-active))
11138         (while (> i (cdr old-active))
11139           (push i new)
11140           (decf i))
11141         (if (not new)
11142             (message "No gnus is bad news.")
11143           (gnus-summary-insert-articles new)
11144           (setq gnus-newsgroup-unreads
11145                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11146           (gnus-summary-limit (gnus-sorted-nunion old new))))
11147     (gnus-summary-position-point)))
11148
11149 (gnus-summary-make-all-marking-commands)
11150
11151 (gnus-ems-redefine)
11152
11153 (provide 'gnus-sum)
11154
11155 (run-hooks 'gnus-sum-load-hook)
11156
11157 ;;; gnus-sum.el ends here