Importing Oort Gnus v0.07.
[elisp/gnus.git-] / 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     (?L gnus-tmp-lines ?s)
1093     (?I gnus-tmp-indentation ?s)
1094     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1095     (?R gnus-tmp-replied ?c)
1096     (?\[ gnus-tmp-opening-bracket ?c)
1097     (?\] gnus-tmp-closing-bracket ?c)
1098     (?\> (make-string gnus-tmp-level ? ) ?s)
1099     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1100     (?i gnus-tmp-score ?d)
1101     (?z gnus-tmp-score-char ?c)
1102     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1103     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1104     (?U gnus-tmp-unread ?c)
1105     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1106         ?s)
1107     (?t (gnus-summary-number-of-articles-in-thread
1108          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1109         ?d)
1110     (?e (gnus-summary-number-of-articles-in-thread
1111          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1112         ?c)
1113     (?u gnus-tmp-user-defined ?s)
1114     (?P (gnus-pick-line-number) ?d)
1115     (?B gnus-tmp-thread-tree-header-string ?s)
1116     (user-date (gnus-user-date
1117                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1118   "An alist of format specifications that can appear in summary lines.
1119 These are paired with what variables they correspond with, along with
1120 the type of the variable (string, integer, character, etc).")
1121
1122 (defvar gnus-summary-dummy-line-format-alist
1123   `((?S gnus-tmp-subject ?s)
1124     (?N gnus-tmp-number ?d)
1125     (?u gnus-tmp-user-defined ?s)))
1126
1127 (defvar gnus-summary-mode-line-format-alist
1128   `((?G gnus-tmp-group-name ?s)
1129     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1130     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1131     (?A gnus-tmp-article-number ?d)
1132     (?Z gnus-tmp-unread-and-unselected ?s)
1133     (?V gnus-version ?s)
1134     (?U gnus-tmp-unread-and-unticked ?d)
1135     (?S gnus-tmp-subject ?s)
1136     (?e gnus-tmp-unselected ?d)
1137     (?u gnus-tmp-user-defined ?s)
1138     (?d (length gnus-newsgroup-dormant) ?d)
1139     (?t (length gnus-newsgroup-marked) ?d)
1140     (?h (length gnus-newsgroup-spam-marked) ?d)
1141     (?r (length gnus-newsgroup-reads) ?d)
1142     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1143     (?E gnus-newsgroup-expunged-tally ?d)
1144     (?s (gnus-current-score-file-nondirectory) ?s)))
1145
1146 (defvar gnus-last-search-regexp nil
1147   "Default regexp for article search command.")
1148
1149 (defvar gnus-last-shell-command nil
1150   "Default shell command on article.")
1151
1152 (defvar gnus-newsgroup-begin nil)
1153 (defvar gnus-newsgroup-end nil)
1154 (defvar gnus-newsgroup-last-rmail nil)
1155 (defvar gnus-newsgroup-last-mail nil)
1156 (defvar gnus-newsgroup-last-folder nil)
1157 (defvar gnus-newsgroup-last-file nil)
1158 (defvar gnus-newsgroup-auto-expire nil)
1159 (defvar gnus-newsgroup-active nil)
1160
1161 (defvar gnus-newsgroup-data nil)
1162 (defvar gnus-newsgroup-data-reverse nil)
1163 (defvar gnus-newsgroup-limit nil)
1164 (defvar gnus-newsgroup-limits nil)
1165
1166 (defvar gnus-newsgroup-unreads nil
1167   "Sorted list of unread articles in the current newsgroup.")
1168
1169 (defvar gnus-newsgroup-unselected nil
1170   "Sorted list of unselected unread articles in the current newsgroup.")
1171
1172 (defvar gnus-newsgroup-reads nil
1173   "Alist of read articles and article marks in the current newsgroup.")
1174
1175 (defvar gnus-newsgroup-expunged-tally nil)
1176
1177 (defvar gnus-newsgroup-marked nil
1178   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1179
1180 (defvar gnus-newsgroup-spam-marked nil
1181   "List of ranges of articles that have been marked as spam.")
1182
1183 (defvar gnus-newsgroup-killed nil
1184   "List of ranges of articles that have been through the scoring process.")
1185
1186 (defvar gnus-newsgroup-cached nil
1187   "Sorted list of articles that come from the article cache.")
1188
1189 (defvar gnus-newsgroup-saved nil
1190   "List of articles that have been saved.")
1191
1192 (defvar gnus-newsgroup-kill-headers nil)
1193
1194 (defvar gnus-newsgroup-replied nil
1195   "List of articles that have been replied to in the current newsgroup.")
1196
1197 (defvar gnus-newsgroup-forwarded nil
1198   "List of articles that have been forwarded in the current newsgroup.")
1199
1200 (defvar gnus-newsgroup-recent nil
1201   "List of articles that have are recent in the current newsgroup.")
1202
1203 (defvar gnus-newsgroup-expirable nil
1204   "Sorted list of articles in the current newsgroup that can be expired.")
1205
1206 (defvar gnus-newsgroup-processable nil
1207   "List of articles in the current newsgroup that can be processed.")
1208
1209 (defvar gnus-newsgroup-downloadable nil
1210   "Sorted list of articles in the current newsgroup that can be processed.")
1211
1212 (defvar gnus-newsgroup-undownloaded nil
1213   "List of articles in the current newsgroup that haven't been downloaded..")
1214
1215 (defvar gnus-newsgroup-unsendable nil
1216   "List of articles in the current newsgroup that won't be sent.")
1217
1218 (defvar gnus-newsgroup-bookmarks nil
1219   "List of articles in the current newsgroup that have bookmarks.")
1220
1221 (defvar gnus-newsgroup-dormant nil
1222   "Sorted list of dormant articles in the current newsgroup.")
1223
1224 (defvar gnus-newsgroup-unseen nil
1225   "List of unseen articles in the current newsgroup.")
1226
1227 (defvar gnus-newsgroup-seen nil
1228   "Range of seen articles in the current newsgroup.")
1229
1230 (defvar gnus-newsgroup-articles nil
1231   "List of articles in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-scored nil
1234   "List of scored articles in the current newsgroup.")
1235
1236 (defvar gnus-newsgroup-headers nil
1237   "List of article headers in the current newsgroup.")
1238
1239 (defvar gnus-newsgroup-threads nil)
1240
1241 (defvar gnus-newsgroup-prepared nil
1242   "Whether the current group has been prepared properly.")
1243
1244 (defvar gnus-newsgroup-ancient nil
1245   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1246
1247 (defvar gnus-newsgroup-sparse nil)
1248
1249 (defvar gnus-current-article nil)
1250 (defvar gnus-article-current nil)
1251 (defvar gnus-current-headers nil)
1252 (defvar gnus-have-all-headers nil)
1253 (defvar gnus-last-article nil)
1254 (defvar gnus-newsgroup-history nil)
1255 (defvar gnus-newsgroup-charset nil)
1256 (defvar gnus-newsgroup-ephemeral-charset nil)
1257 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1258
1259 (defvar gnus-article-before-search nil)
1260
1261 (defconst gnus-summary-local-variables
1262   '(gnus-newsgroup-name
1263     gnus-newsgroup-begin gnus-newsgroup-end
1264     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1265     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1266     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1267     gnus-newsgroup-unselected gnus-newsgroup-marked
1268     gnus-newsgroup-spam-marked
1269     gnus-newsgroup-reads gnus-newsgroup-saved
1270     gnus-newsgroup-replied gnus-newsgroup-forwarded
1271     gnus-newsgroup-recent
1272     gnus-newsgroup-expirable
1273     gnus-newsgroup-processable gnus-newsgroup-killed
1274     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1275     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1276     gnus-newsgroup-seen gnus-newsgroup-articles
1277     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1278     gnus-newsgroup-headers gnus-newsgroup-threads
1279     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1280     gnus-current-article gnus-current-headers gnus-have-all-headers
1281     gnus-last-article gnus-article-internal-prepare-hook
1282     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1283     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1284     gnus-thread-expunge-below
1285     gnus-score-alist gnus-current-score-file
1286     (gnus-summary-expunge-below . global)
1287     (gnus-summary-mark-below . global)
1288     (gnus-orphan-score . global)
1289     gnus-newsgroup-active gnus-scores-exclude-files
1290     gnus-newsgroup-history gnus-newsgroup-ancient
1291     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1292     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1293     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1294     (gnus-newsgroup-expunged-tally . 0)
1295     gnus-cache-removable-articles gnus-newsgroup-cached
1296     gnus-newsgroup-data gnus-newsgroup-data-reverse
1297     gnus-newsgroup-limit gnus-newsgroup-limits
1298     gnus-newsgroup-charset gnus-newsgroup-display)
1299   "Variables that are buffer-local to the summary buffers.")
1300
1301 (defvar gnus-newsgroup-variables nil
1302   "A list of variables that have separate values in different newsgroups.
1303 A list of newsgroup (summary buffer) local variables, or cons of
1304 variables and their default values (when the default values are not
1305 nil), that should be made global while the summary buffer is active.
1306 These variables can be used to set variables in the group parameters
1307 while still allowing them to affect operations done in other
1308 buffers. For example:
1309
1310 \(setq gnus-newsgroup-variables
1311      '(message-use-followup-to
1312        (gnus-visible-headers .
1313          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1314 ")
1315
1316 ;; Byte-compiler warning.
1317 (eval-when-compile (defvar gnus-article-mode-map))
1318
1319 ;; MIME stuff.
1320
1321 (defvar gnus-decode-encoded-word-methods
1322   '(mail-decode-encoded-word-string)
1323   "List of methods used to decode encoded words.
1324
1325 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1326 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1327 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1328 whose names match REGEXP.
1329
1330 For example:
1331 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1332  mail-decode-encoded-word-string
1333  (\"chinese\" . rfc1843-decode-string))")
1334
1335 (defvar gnus-decode-encoded-word-methods-cache nil)
1336
1337 (defun gnus-multi-decode-encoded-word-string (string)
1338   "Apply the functions from `gnus-encoded-word-methods' that match."
1339   (unless (and gnus-decode-encoded-word-methods-cache
1340                (eq gnus-newsgroup-name
1341                    (car gnus-decode-encoded-word-methods-cache)))
1342     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1343     (mapcar (lambda (x)
1344               (if (symbolp x)
1345                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1346                 (if (and gnus-newsgroup-name
1347                          (string-match (car x) gnus-newsgroup-name))
1348                     (nconc gnus-decode-encoded-word-methods-cache
1349                            (list (cdr x))))))
1350             gnus-decode-encoded-word-methods))
1351   (let ((xlist gnus-decode-encoded-word-methods-cache))
1352     (pop xlist)
1353     (while xlist
1354       (setq string (funcall (pop xlist) string))))
1355   string)
1356
1357 ;; Subject simplification.
1358
1359 (defun gnus-simplify-whitespace (str)
1360   "Remove excessive whitespace from STR."
1361   (let ((mystr str))
1362     ;; Multiple spaces.
1363     (while (string-match "[ \t][ \t]+" mystr)
1364       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1365                           " "
1366                           (substring mystr (match-end 0)))))
1367     ;; Leading spaces.
1368     (when (string-match "^[ \t]+" mystr)
1369       (setq mystr (substring mystr (match-end 0))))
1370     ;; Trailing spaces.
1371     (when (string-match "[ \t]+$" mystr)
1372       (setq mystr (substring mystr 0 (match-beginning 0))))
1373     mystr))
1374
1375 (defun gnus-simplify-all-whitespace (str)
1376   "Remove all whitespace from STR."
1377   (let ((mystr str))
1378     (while (string-match "[ \t\n]+" mystr)
1379       (setq mystr (replace-match "" nil nil mystr)))
1380     mystr))
1381
1382 (defsubst gnus-simplify-subject-re (subject)
1383   "Remove \"Re:\" from subject lines."
1384   (if (string-match message-subject-re-regexp subject)
1385       (substring subject (match-end 0))
1386     subject))
1387
1388 (defun gnus-simplify-subject (subject &optional re-only)
1389   "Remove `Re:' and words in parentheses.
1390 If RE-ONLY is non-nil, strip leading `Re:'s only."
1391   (let ((case-fold-search t))           ;Ignore case.
1392     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1393     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1394       (setq subject (substring subject (match-end 0))))
1395     ;; Remove uninteresting prefixes.
1396     (when (and (not re-only)
1397                gnus-simplify-ignored-prefixes
1398                (string-match gnus-simplify-ignored-prefixes subject))
1399       (setq subject (substring subject (match-end 0))))
1400     ;; Remove words in parentheses from end.
1401     (unless re-only
1402       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1403         (setq subject (substring subject 0 (match-beginning 0)))))
1404     ;; Return subject string.
1405     subject))
1406
1407 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1408 ;; all whitespace.
1409 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1410   (goto-char (point-min))
1411   (while (re-search-forward regexp nil t)
1412     (replace-match (or newtext ""))))
1413
1414 (defun gnus-simplify-buffer-fuzzy ()
1415   "Simplify string in the buffer fuzzily.
1416 The string in the accessible portion of the current buffer is simplified.
1417 It is assumed to be a single-line subject.
1418 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1419 matter is removed.  Additional things can be deleted by setting
1420 `gnus-simplify-subject-fuzzy-regexp'."
1421   (let ((case-fold-search t)
1422         (modified-tick))
1423     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1424
1425     (while (not (eq modified-tick (buffer-modified-tick)))
1426       (setq modified-tick (buffer-modified-tick))
1427       (cond
1428        ((listp gnus-simplify-subject-fuzzy-regexp)
1429         (mapcar 'gnus-simplify-buffer-fuzzy-step
1430                 gnus-simplify-subject-fuzzy-regexp))
1431        (gnus-simplify-subject-fuzzy-regexp
1432         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1433       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1434       (gnus-simplify-buffer-fuzzy-step
1435        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1436       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1437
1438     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1439     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1440     (gnus-simplify-buffer-fuzzy-step " $")
1441     (gnus-simplify-buffer-fuzzy-step "^ +")))
1442
1443 (defun gnus-simplify-subject-fuzzy (subject)
1444   "Simplify a subject string fuzzily.
1445 See `gnus-simplify-buffer-fuzzy' for details."
1446   (save-excursion
1447     (gnus-set-work-buffer)
1448     (let ((case-fold-search t))
1449       ;; Remove uninteresting prefixes.
1450       (when (and gnus-simplify-ignored-prefixes
1451                  (string-match gnus-simplify-ignored-prefixes subject))
1452         (setq subject (substring subject (match-end 0))))
1453       (insert subject)
1454       (inline (gnus-simplify-buffer-fuzzy))
1455       (buffer-string))))
1456
1457 (defsubst gnus-simplify-subject-fully (subject)
1458   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1459   (cond
1460    (gnus-simplify-subject-functions
1461     (gnus-map-function gnus-simplify-subject-functions subject))
1462    ((null gnus-summary-gather-subject-limit)
1463     (gnus-simplify-subject-re subject))
1464    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1465     (gnus-simplify-subject-fuzzy subject))
1466    ((numberp gnus-summary-gather-subject-limit)
1467     (gnus-limit-string (gnus-simplify-subject-re subject)
1468                        gnus-summary-gather-subject-limit))
1469    (t
1470     subject)))
1471
1472 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1473   "Check whether two subjects are equal.
1474 If optional argument simple-first is t, first argument is already
1475 simplified."
1476   (cond
1477    ((null simple-first)
1478     (equal (gnus-simplify-subject-fully s1)
1479            (gnus-simplify-subject-fully s2)))
1480    (t
1481     (equal s1
1482            (gnus-simplify-subject-fully s2)))))
1483
1484 (defun gnus-summary-bubble-group ()
1485   "Increase the score of the current group.
1486 This is a handy function to add to `gnus-summary-exit-hook' to
1487 increase the score of each group you read."
1488   (gnus-group-add-score gnus-newsgroup-name))
1489
1490 \f
1491 ;;;
1492 ;;; Gnus summary mode
1493 ;;;
1494
1495 (put 'gnus-summary-mode 'mode-class 'special)
1496
1497 (defvar gnus-article-commands-menu)
1498
1499 (when t
1500   ;; Non-orthogonal keys
1501
1502   (gnus-define-keys gnus-summary-mode-map
1503     " " gnus-summary-next-page
1504     "\177" gnus-summary-prev-page
1505     [delete] gnus-summary-prev-page
1506     [backspace] gnus-summary-prev-page
1507     "\r" gnus-summary-scroll-up
1508     "\M-\r" gnus-summary-scroll-down
1509     "n" gnus-summary-next-unread-article
1510     "p" gnus-summary-prev-unread-article
1511     "N" gnus-summary-next-article
1512     "P" gnus-summary-prev-article
1513     "\M-\C-n" gnus-summary-next-same-subject
1514     "\M-\C-p" gnus-summary-prev-same-subject
1515     "\M-n" gnus-summary-next-unread-subject
1516     "\M-p" gnus-summary-prev-unread-subject
1517     "." gnus-summary-first-unread-article
1518     "," gnus-summary-best-unread-article
1519     "\M-s" gnus-summary-search-article-forward
1520     "\M-r" gnus-summary-search-article-backward
1521     "<" gnus-summary-beginning-of-article
1522     ">" gnus-summary-end-of-article
1523     "j" gnus-summary-goto-article
1524     "^" gnus-summary-refer-parent-article
1525     "\M-^" gnus-summary-refer-article
1526     "u" gnus-summary-tick-article-forward
1527     "!" gnus-summary-tick-article-forward
1528     "U" gnus-summary-tick-article-backward
1529     "d" gnus-summary-mark-as-read-forward
1530     "D" gnus-summary-mark-as-read-backward
1531     "E" gnus-summary-mark-as-expirable
1532     "\M-u" gnus-summary-clear-mark-forward
1533     "\M-U" gnus-summary-clear-mark-backward
1534     "k" gnus-summary-kill-same-subject-and-select
1535     "\C-k" gnus-summary-kill-same-subject
1536     "\M-\C-k" gnus-summary-kill-thread
1537     "\M-\C-l" gnus-summary-lower-thread
1538     "e" gnus-summary-edit-article
1539     "#" gnus-summary-mark-as-processable
1540     "\M-#" gnus-summary-unmark-as-processable
1541     "\M-\C-t" gnus-summary-toggle-threads
1542     "\M-\C-s" gnus-summary-show-thread
1543     "\M-\C-h" gnus-summary-hide-thread
1544     "\M-\C-f" gnus-summary-next-thread
1545     "\M-\C-b" gnus-summary-prev-thread
1546     [(meta down)] gnus-summary-next-thread
1547     [(meta up)] gnus-summary-prev-thread
1548     "\M-\C-u" gnus-summary-up-thread
1549     "\M-\C-d" gnus-summary-down-thread
1550     "&" gnus-summary-execute-command
1551     "c" gnus-summary-catchup-and-exit
1552     "\C-w" gnus-summary-mark-region-as-read
1553     "\C-t" gnus-summary-toggle-truncation
1554     "?" gnus-summary-mark-as-dormant
1555     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1556     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1557     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1558     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1559     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1560     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1561     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1562     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1563     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1564     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1565     "=" gnus-summary-expand-window
1566     "\C-x\C-s" gnus-summary-reselect-current-group
1567     "\M-g" gnus-summary-rescan-group
1568     "w" gnus-summary-stop-page-breaking
1569     "\C-c\C-r" gnus-summary-caesar-message
1570     "f" gnus-summary-followup
1571     "F" gnus-summary-followup-with-original
1572     "C" gnus-summary-cancel-article
1573     "r" gnus-summary-reply
1574     "R" gnus-summary-reply-with-original
1575     "\C-c\C-f" gnus-summary-mail-forward
1576     "o" gnus-summary-save-article
1577     "\C-o" gnus-summary-save-article-mail
1578     "|" gnus-summary-pipe-output
1579     "\M-k" gnus-summary-edit-local-kill
1580     "\M-K" gnus-summary-edit-global-kill
1581     ;; "V" gnus-version
1582     "\C-c\C-d" gnus-summary-describe-group
1583     "q" gnus-summary-exit
1584     "Q" gnus-summary-exit-no-update
1585     "\C-c\C-i" gnus-info-find-node
1586     gnus-mouse-2 gnus-mouse-pick-article
1587     "m" gnus-summary-mail-other-window
1588     "a" gnus-summary-post-news
1589     "i" gnus-summary-news-other-window
1590     "x" gnus-summary-limit-to-unread
1591     "s" gnus-summary-isearch-article
1592     "t" gnus-summary-toggle-header
1593     "g" gnus-summary-show-article
1594     "l" gnus-summary-goto-last-article
1595     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1596     "\C-d" gnus-summary-enter-digest-group
1597     "\M-\C-d" gnus-summary-read-document
1598     "\M-\C-e" gnus-summary-edit-parameters
1599     "\M-\C-a" gnus-summary-customize-parameters
1600     "\C-c\C-b" gnus-bug
1601     "*" gnus-cache-enter-article
1602     "\M-*" gnus-cache-remove-article
1603     "\M-&" gnus-summary-universal-argument
1604     "\C-l" gnus-recenter
1605     "I" gnus-summary-increase-score
1606     "L" gnus-summary-lower-score
1607     "\M-i" gnus-symbolic-argument
1608     "h" gnus-summary-select-article-buffer
1609
1610     "b" gnus-article-view-part
1611     "\M-t" gnus-summary-toggle-display-buttonized
1612
1613     "V" gnus-summary-score-map
1614     "X" gnus-uu-extract-map
1615     "S" gnus-summary-send-map)
1616
1617   ;; Sort of orthogonal keymap
1618   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1619     "t" gnus-summary-tick-article-forward
1620     "!" gnus-summary-tick-article-forward
1621     "d" gnus-summary-mark-as-read-forward
1622     "r" gnus-summary-mark-as-read-forward
1623     "c" gnus-summary-clear-mark-forward
1624     " " gnus-summary-clear-mark-forward
1625     "e" gnus-summary-mark-as-expirable
1626     "x" gnus-summary-mark-as-expirable
1627     "?" gnus-summary-mark-as-dormant
1628     "b" gnus-summary-set-bookmark
1629     "B" gnus-summary-remove-bookmark
1630     "#" gnus-summary-mark-as-processable
1631     "\M-#" gnus-summary-unmark-as-processable
1632     "S" gnus-summary-limit-include-expunged
1633     "C" gnus-summary-catchup
1634     "H" gnus-summary-catchup-to-here
1635     "h" gnus-summary-catchup-from-here
1636     "\C-c" gnus-summary-catchup-all
1637     "k" gnus-summary-kill-same-subject-and-select
1638     "K" gnus-summary-kill-same-subject
1639     "P" gnus-uu-mark-map)
1640
1641   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1642     "c" gnus-summary-clear-above
1643     "u" gnus-summary-tick-above
1644     "m" gnus-summary-mark-above
1645     "k" gnus-summary-kill-below)
1646
1647   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1648     "/" gnus-summary-limit-to-subject
1649     "n" gnus-summary-limit-to-articles
1650     "w" gnus-summary-pop-limit
1651     "s" gnus-summary-limit-to-subject
1652     "a" gnus-summary-limit-to-author
1653     "u" gnus-summary-limit-to-unread
1654     "m" gnus-summary-limit-to-marks
1655     "M" gnus-summary-limit-exclude-marks
1656     "v" gnus-summary-limit-to-score
1657     "*" gnus-summary-limit-include-cached
1658     "D" gnus-summary-limit-include-dormant
1659     "T" gnus-summary-limit-include-thread
1660     "d" gnus-summary-limit-exclude-dormant
1661     "t" gnus-summary-limit-to-age
1662     "x" gnus-summary-limit-to-extra
1663     "p" gnus-summary-limit-to-display-predicate
1664     "E" gnus-summary-limit-include-expunged
1665     "c" gnus-summary-limit-exclude-childless-dormant
1666     "C" gnus-summary-limit-mark-excluded-as-read
1667     "o" gnus-summary-insert-old-articles
1668     "N" gnus-summary-insert-new-articles)
1669
1670   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1671     "n" gnus-summary-next-unread-article
1672     "p" gnus-summary-prev-unread-article
1673     "N" gnus-summary-next-article
1674     "P" gnus-summary-prev-article
1675     "\C-n" gnus-summary-next-same-subject
1676     "\C-p" gnus-summary-prev-same-subject
1677     "\M-n" gnus-summary-next-unread-subject
1678     "\M-p" gnus-summary-prev-unread-subject
1679     "f" gnus-summary-first-unread-article
1680     "b" gnus-summary-best-unread-article
1681     "j" gnus-summary-goto-article
1682     "g" gnus-summary-goto-subject
1683     "l" gnus-summary-goto-last-article
1684     "o" gnus-summary-pop-article)
1685
1686   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1687     "k" gnus-summary-kill-thread
1688     "l" gnus-summary-lower-thread
1689     "i" gnus-summary-raise-thread
1690     "T" gnus-summary-toggle-threads
1691     "t" gnus-summary-rethread-current
1692     "^" gnus-summary-reparent-thread
1693     "s" gnus-summary-show-thread
1694     "S" gnus-summary-show-all-threads
1695     "h" gnus-summary-hide-thread
1696     "H" gnus-summary-hide-all-threads
1697     "n" gnus-summary-next-thread
1698     "p" gnus-summary-prev-thread
1699     "u" gnus-summary-up-thread
1700     "o" gnus-summary-top-thread
1701     "d" gnus-summary-down-thread
1702     "#" gnus-uu-mark-thread
1703     "\M-#" gnus-uu-unmark-thread)
1704
1705   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1706     "g" gnus-summary-prepare
1707     "c" gnus-summary-insert-cached-articles)
1708
1709   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1710     "c" gnus-summary-catchup-and-exit
1711     "C" gnus-summary-catchup-all-and-exit
1712     "E" gnus-summary-exit-no-update
1713     "Q" gnus-summary-exit
1714     "Z" gnus-summary-exit
1715     "n" gnus-summary-catchup-and-goto-next-group
1716     "R" gnus-summary-reselect-current-group
1717     "G" gnus-summary-rescan-group
1718     "N" gnus-summary-next-group
1719     "s" gnus-summary-save-newsrc
1720     "P" gnus-summary-prev-group)
1721
1722   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1723     " " gnus-summary-next-page
1724     "n" gnus-summary-next-page
1725     "\177" gnus-summary-prev-page
1726     [delete] gnus-summary-prev-page
1727     "p" gnus-summary-prev-page
1728     "\r" gnus-summary-scroll-up
1729     "\M-\r" gnus-summary-scroll-down
1730     "<" gnus-summary-beginning-of-article
1731     ">" gnus-summary-end-of-article
1732     "b" gnus-summary-beginning-of-article
1733     "e" gnus-summary-end-of-article
1734     "^" gnus-summary-refer-parent-article
1735     "r" gnus-summary-refer-parent-article
1736     "D" gnus-summary-enter-digest-group
1737     "R" gnus-summary-refer-references
1738     "T" gnus-summary-refer-thread
1739     "g" gnus-summary-show-article
1740     "s" gnus-summary-isearch-article
1741     "P" gnus-summary-print-article
1742     "M" gnus-mailing-list-insinuate
1743     "t" gnus-article-babel)
1744
1745   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1746     "b" gnus-article-add-buttons
1747     "B" gnus-article-add-buttons-to-head
1748     "o" gnus-article-treat-overstrike
1749     "e" gnus-article-emphasize
1750     "w" gnus-article-fill-cited-article
1751     "Q" gnus-article-fill-long-lines
1752     "C" gnus-article-capitalize-sentences
1753     "c" gnus-article-remove-cr
1754     "q" gnus-article-de-quoted-unreadable
1755     "6" gnus-article-de-base64-unreadable
1756     "Z" gnus-article-decode-HZ
1757     "h" gnus-article-wash-html
1758     "u" gnus-article-unsplit-urls
1759     "s" gnus-summary-force-verify-and-decrypt
1760     "f" gnus-article-display-x-face
1761     "l" gnus-summary-stop-page-breaking
1762     "r" gnus-summary-caesar-message
1763     "t" gnus-summary-toggle-header
1764     "g" gnus-treat-smiley
1765     "v" gnus-summary-verbose-headers
1766     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1767     "p" gnus-article-verify-x-pgp-sig
1768     "d" gnus-article-treat-dumbquotes
1769     "k" gnus-article-outlook-deuglify-article)
1770
1771   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1772     "a" gnus-article-hide
1773     "h" gnus-article-hide-headers
1774     "b" gnus-article-hide-boring-headers
1775     "s" gnus-article-hide-signature
1776     "c" gnus-article-hide-citation
1777     "C" gnus-article-hide-citation-in-followups
1778     "l" gnus-article-hide-list-identifiers
1779     "p" gnus-article-hide-pgp
1780     "B" gnus-article-strip-banner
1781     "P" gnus-article-hide-pem
1782     "\C-c" gnus-article-hide-citation-maybe)
1783
1784   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1785     "a" gnus-article-highlight
1786     "h" gnus-article-highlight-headers
1787     "c" gnus-article-highlight-citation
1788     "s" gnus-article-highlight-signature)
1789
1790   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1791     "f" gnus-article-treat-fold-headers
1792     "u" gnus-article-treat-unfold-headers
1793     "n" gnus-article-treat-fold-newsgroups)
1794
1795   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1796     "x" gnus-article-display-x-face
1797     "s" gnus-treat-smiley
1798     "D" gnus-article-remove-images
1799     "f" gnus-treat-from-picon
1800     "m" gnus-treat-mail-picon
1801     "n" gnus-treat-newsgroups-picon)
1802
1803   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1804     "w" gnus-article-decode-mime-words
1805     "c" gnus-article-decode-charset
1806     "v" gnus-mime-view-all-parts
1807     "b" gnus-article-view-part)
1808
1809   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1810     "z" gnus-article-date-ut
1811     "u" gnus-article-date-ut
1812     "l" gnus-article-date-local
1813     "p" gnus-article-date-english
1814     "e" gnus-article-date-lapsed
1815     "o" gnus-article-date-original
1816     "i" gnus-article-date-iso8601
1817     "s" gnus-article-date-user)
1818
1819   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1820     "t" gnus-article-remove-trailing-blank-lines
1821     "l" gnus-article-strip-leading-blank-lines
1822     "m" gnus-article-strip-multiple-blank-lines
1823     "a" gnus-article-strip-blank-lines
1824     "A" gnus-article-strip-all-blank-lines
1825     "s" gnus-article-strip-leading-space
1826     "e" gnus-article-strip-trailing-space
1827     "w" gnus-article-remove-leading-whitespace)
1828
1829   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1830     "v" gnus-version
1831     "f" gnus-summary-fetch-faq
1832     "d" gnus-summary-describe-group
1833     "h" gnus-summary-describe-briefly
1834     "i" gnus-info-find-node)
1835
1836   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1837     "e" gnus-summary-expire-articles
1838     "\M-\C-e" gnus-summary-expire-articles-now
1839     "\177" gnus-summary-delete-article
1840     [delete] gnus-summary-delete-article
1841     [backspace] gnus-summary-delete-article
1842     "m" gnus-summary-move-article
1843     "r" gnus-summary-respool-article
1844     "w" gnus-summary-edit-article
1845     "c" gnus-summary-copy-article
1846     "B" gnus-summary-crosspost-article
1847     "q" gnus-summary-respool-query
1848     "t" gnus-summary-respool-trace
1849     "i" gnus-summary-import-article
1850     "I" gnus-summary-create-article
1851     "p" gnus-summary-article-posted-p)
1852
1853   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1854     "o" gnus-summary-save-article
1855     "m" gnus-summary-save-article-mail
1856     "F" gnus-summary-write-article-file
1857     "r" gnus-summary-save-article-rmail
1858     "f" gnus-summary-save-article-file
1859     "b" gnus-summary-save-article-body-file
1860     "h" gnus-summary-save-article-folder
1861     "v" gnus-summary-save-article-vm
1862     "p" gnus-summary-pipe-output
1863     "P" gnus-summary-muttprint
1864     "s" gnus-soup-add-article)
1865
1866   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1867     "b" gnus-summary-display-buttonized
1868     "m" gnus-summary-repair-multipart
1869     "v" gnus-article-view-part
1870     "o" gnus-article-save-part
1871     "c" gnus-article-copy-part
1872     "C" gnus-article-view-part-as-charset
1873     "e" gnus-article-view-part-externally
1874     "E" gnus-article-encrypt-body
1875     "i" gnus-article-inline-part
1876     "|" gnus-article-pipe-part)
1877
1878   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1879     "p" gnus-summary-mark-as-processable
1880     "u" gnus-summary-unmark-as-processable
1881     "U" gnus-summary-unmark-all-processable
1882     "v" gnus-uu-mark-over
1883     "s" gnus-uu-mark-series
1884     "r" gnus-uu-mark-region
1885     "g" gnus-uu-unmark-region
1886     "R" gnus-uu-mark-by-regexp
1887     "G" gnus-uu-unmark-by-regexp
1888     "t" gnus-uu-mark-thread
1889     "T" gnus-uu-unmark-thread
1890     "a" gnus-uu-mark-all
1891     "b" gnus-uu-mark-buffer
1892     "S" gnus-uu-mark-sparse
1893     "k" gnus-summary-kill-process-mark
1894     "y" gnus-summary-yank-process-mark
1895     "w" gnus-summary-save-process-mark
1896     "i" gnus-uu-invert-processable)
1897
1898   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1899     ;;"x" gnus-uu-extract-any
1900     "m" gnus-summary-save-parts
1901     "u" gnus-uu-decode-uu
1902     "U" gnus-uu-decode-uu-and-save
1903     "s" gnus-uu-decode-unshar
1904     "S" gnus-uu-decode-unshar-and-save
1905     "o" gnus-uu-decode-save
1906     "O" gnus-uu-decode-save
1907     "b" gnus-uu-decode-binhex
1908     "B" gnus-uu-decode-binhex
1909     "p" gnus-uu-decode-postscript
1910     "P" gnus-uu-decode-postscript-and-save)
1911
1912   (gnus-define-keys
1913       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1914     "u" gnus-uu-decode-uu-view
1915     "U" gnus-uu-decode-uu-and-save-view
1916     "s" gnus-uu-decode-unshar-view
1917     "S" gnus-uu-decode-unshar-and-save-view
1918     "o" gnus-uu-decode-save-view
1919     "O" gnus-uu-decode-save-view
1920     "b" gnus-uu-decode-binhex-view
1921     "B" gnus-uu-decode-binhex-view
1922     "p" gnus-uu-decode-postscript-view
1923     "P" gnus-uu-decode-postscript-and-save-view))
1924
1925 (defvar gnus-article-post-menu nil)
1926
1927 (defconst gnus-summary-menu-maxlen 20)
1928
1929 (defun gnus-summary-menu-split (menu)
1930   ;; If we have lots of elements, divide them into groups of 20
1931   ;; and make a pane (or submenu) for each one.
1932   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1933       (let ((menu menu) sublists next
1934             (i 1))
1935         (while menu
1936           ;; Pull off the next gnus-summary-menu-maxlen elements
1937           ;; and make them the next element of sublist.
1938           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1939           (if next
1940               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1941                       nil))
1942           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1943                                              (aref (car (last menu)) 0)) menu)
1944                                sublists))
1945           (setq i (1+ i))
1946           (setq menu next))
1947         (nreverse sublists))
1948     ;; Few elements--put them all in one pane.
1949     menu))
1950
1951 (defun gnus-summary-make-menu-bar ()
1952   (gnus-turn-off-edit-menu 'summary)
1953
1954   (unless (boundp 'gnus-summary-misc-menu)
1955
1956     (easy-menu-define
1957       gnus-summary-kill-menu gnus-summary-mode-map ""
1958       (cons
1959        "Score"
1960        (nconc
1961         (list
1962          ["Customize" gnus-score-customize t])
1963         (gnus-make-score-map 'increase)
1964         (gnus-make-score-map 'lower)
1965         '(("Mark"
1966            ["Kill below" gnus-summary-kill-below t]
1967            ["Mark above" gnus-summary-mark-above t]
1968            ["Tick above" gnus-summary-tick-above t]
1969            ["Clear above" gnus-summary-clear-above t])
1970           ["Current score" gnus-summary-current-score t]
1971           ["Set score" gnus-summary-set-score t]
1972           ["Switch current score file..." gnus-score-change-score-file t]
1973           ["Set mark below..." gnus-score-set-mark-below t]
1974           ["Set expunge below..." gnus-score-set-expunge-below t]
1975           ["Edit current score file" gnus-score-edit-current-scores t]
1976           ["Edit score file" gnus-score-edit-file t]
1977           ["Trace score" gnus-score-find-trace t]
1978           ["Find words" gnus-score-find-favourite-words t]
1979           ["Rescore buffer" gnus-summary-rescore t]
1980           ["Increase score..." gnus-summary-increase-score t]
1981           ["Lower score..." gnus-summary-lower-score t]))))
1982
1983     ;; Define both the Article menu in the summary buffer and the
1984     ;; equivalent Commands menu in the article buffer here for
1985     ;; consistency.
1986     (let ((innards
1987            `(("Hide"
1988               ["All" gnus-article-hide t]
1989               ["Headers" gnus-article-hide-headers t]
1990               ["Signature" gnus-article-hide-signature t]
1991               ["Citation" gnus-article-hide-citation t]
1992               ["List identifiers" gnus-article-hide-list-identifiers t]
1993               ["PGP" gnus-article-hide-pgp t]
1994               ["Banner" gnus-article-strip-banner t]
1995               ["Boring headers" gnus-article-hide-boring-headers t])
1996              ("Highlight"
1997               ["All" gnus-article-highlight t]
1998               ["Headers" gnus-article-highlight-headers t]
1999               ["Signature" gnus-article-highlight-signature t]
2000               ["Citation" gnus-article-highlight-citation t])
2001              ("MIME"
2002               ["Words" gnus-article-decode-mime-words t]
2003               ["Charset" gnus-article-decode-charset t]
2004               ["QP" gnus-article-de-quoted-unreadable t]
2005               ["Base64" gnus-article-de-base64-unreadable t]
2006               ["View all" gnus-mime-view-all-parts t]
2007               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2008               ["Encrypt body" gnus-article-encrypt-body t]
2009               ["Extract all parts" gnus-summary-save-parts t])
2010              ("Date"
2011               ["Local" gnus-article-date-local t]
2012               ["ISO8601" gnus-article-date-iso8601 t]
2013               ["UT" gnus-article-date-ut t]
2014               ["Original" gnus-article-date-original t]
2015               ["Lapsed" gnus-article-date-lapsed t]
2016               ["User-defined" gnus-article-date-user t])
2017              ("Display"
2018               ["Remove images" gnus-article-remove-images t]
2019               ["Toggle smiley" gnus-treat-smiley t]
2020               ["Show X-Face" gnus-article-display-x-face t]
2021               ["Show picons in From" gnus-treat-from-picon t]
2022               ["Show picons in mail headers" gnus-treat-mail-picon t]
2023               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2024               ("View as different encoding"
2025                ,@(gnus-summary-menu-split
2026                   (mapcar
2027                    (lambda (cs)
2028                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2029                      ;; forms for menu commands, we should provide intern'ed
2030                      ;; function symbols.
2031                      (let ((command (intern (format "\
2032 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2033                        (fset command
2034                              `(lambda ()
2035                                 (interactive)
2036                                 (let ((gnus-summary-show-article-charset-alist
2037                                        '((1 . ,cs))))
2038                                   (gnus-summary-show-article 1))))
2039                        `[,(symbol-name cs) ,command t]))
2040                    (sort (if (fboundp 'coding-system-list)
2041                              (coding-system-list)
2042                            (mapcar 'car mm-mime-mule-charset-alist))
2043                          (lambda (a b)
2044                            (string< (symbol-name a)
2045                                     (symbol-name b))))))))
2046              ("Washing"
2047               ("Remove Blanks"
2048                ["Leading" gnus-article-strip-leading-blank-lines t]
2049                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2050                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2051                ["All of the above" gnus-article-strip-blank-lines t]
2052                ["All" gnus-article-strip-all-blank-lines t]
2053                ["Leading space" gnus-article-strip-leading-space t]
2054                ["Trailing space" gnus-article-strip-trailing-space t]
2055                ["Leading space in headers"
2056                 gnus-article-remove-leading-whitespace t])
2057               ["Overstrike" gnus-article-treat-overstrike t]
2058               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2059               ["Emphasis" gnus-article-emphasize t]
2060               ["Word wrap" gnus-article-fill-cited-article t]
2061               ["Fill long lines" gnus-article-fill-long-lines t]
2062               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2063               ["CR" gnus-article-remove-cr t]
2064               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2065               ["Base64" gnus-article-de-base64-unreadable t]
2066               ["Rot 13" gnus-summary-caesar-message
2067                ,@(if (featurep 'xemacs) '(t)
2068                    '(:help "\"Caesar rotate\" article by 13"))]
2069               ["Unix pipe" gnus-summary-pipe-message t]
2070               ["Add buttons" gnus-article-add-buttons t]
2071               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2072               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2073               ["Verbose header" gnus-summary-verbose-headers t]
2074               ["Toggle header" gnus-summary-toggle-header t]
2075               ["Unfold headers" gnus-article-treat-unfold-headers t]
2076               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2077               ["Html" gnus-article-wash-html t]
2078               ["URLs" gnus-article-unsplit-urls t]
2079               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2080               ["HZ" gnus-article-decode-HZ t]
2081               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2082               )
2083              ("Output"
2084               ["Save in default format" gnus-summary-save-article
2085                ,@(if (featurep 'xemacs) '(t)
2086                    '(:help "Save article using default method"))]
2087               ["Save in file" gnus-summary-save-article-file
2088                ,@(if (featurep 'xemacs) '(t)
2089                    '(:help "Save article in file"))]
2090               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2091               ["Save in MH folder" gnus-summary-save-article-folder t]
2092               ["Save in VM folder" gnus-summary-save-article-vm t]
2093               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2094               ["Save body in file" gnus-summary-save-article-body-file t]
2095               ["Pipe through a filter" gnus-summary-pipe-output t]
2096               ["Add to SOUP packet" gnus-soup-add-article t]
2097               ["Print with Muttprint" gnus-summary-muttprint t]
2098               ["Print" gnus-summary-print-article t])
2099              ("Backend"
2100               ["Respool article..." gnus-summary-respool-article t]
2101               ["Move article..." gnus-summary-move-article
2102                (gnus-check-backend-function
2103                 'request-move-article gnus-newsgroup-name)]
2104               ["Copy article..." gnus-summary-copy-article t]
2105               ["Crosspost article..." gnus-summary-crosspost-article
2106                (gnus-check-backend-function
2107                 'request-replace-article gnus-newsgroup-name)]
2108               ["Import file..." gnus-summary-import-article t]
2109               ["Create article..." gnus-summary-create-article t]
2110               ["Check if posted" gnus-summary-article-posted-p t]
2111               ["Edit article" gnus-summary-edit-article
2112                (not (gnus-group-read-only-p))]
2113               ["Delete article" gnus-summary-delete-article
2114                (gnus-check-backend-function
2115                 'request-expire-articles gnus-newsgroup-name)]
2116               ["Query respool" gnus-summary-respool-query t]
2117               ["Trace respool" gnus-summary-respool-trace t]
2118               ["Delete expirable articles" gnus-summary-expire-articles-now
2119                (gnus-check-backend-function
2120                 'request-expire-articles gnus-newsgroup-name)])
2121              ("Extract"
2122               ["Uudecode" gnus-uu-decode-uu
2123                ,@(if (featurep 'xemacs) '(t)
2124                    '(:help "Decode uuencoded article(s)"))]
2125               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2126               ["Unshar" gnus-uu-decode-unshar t]
2127               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2128               ["Save" gnus-uu-decode-save t]
2129               ["Binhex" gnus-uu-decode-binhex t]
2130               ["Postscript" gnus-uu-decode-postscript t]
2131               ["All MIME parts" gnus-summary-save-parts t])
2132              ("Cache"
2133               ["Enter article" gnus-cache-enter-article t]
2134               ["Remove article" gnus-cache-remove-article t])
2135              ["Translate" gnus-article-babel t]
2136              ["Select article buffer" gnus-summary-select-article-buffer t]
2137              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2138              ["Isearch article..." gnus-summary-isearch-article t]
2139              ["Beginning of the article" gnus-summary-beginning-of-article t]
2140              ["End of the article" gnus-summary-end-of-article t]
2141              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2142              ["Fetch referenced articles" gnus-summary-refer-references t]
2143              ["Fetch current thread" gnus-summary-refer-thread t]
2144              ["Fetch article with id..." gnus-summary-refer-article t]
2145              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2146              ["Redisplay" gnus-summary-show-article t]
2147              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2148       (easy-menu-define
2149         gnus-summary-article-menu gnus-summary-mode-map ""
2150         (cons "Article" innards))
2151
2152       (if (not (keymapp gnus-summary-article-menu))
2153           (easy-menu-define
2154             gnus-article-commands-menu gnus-article-mode-map ""
2155             (cons "Commands" innards))
2156         ;; in Emacs, don't share menu.
2157         (setq gnus-article-commands-menu
2158               (copy-keymap gnus-summary-article-menu))
2159         (define-key gnus-article-mode-map [menu-bar commands]
2160           (cons "Commands" gnus-article-commands-menu))))
2161
2162     (easy-menu-define
2163       gnus-summary-thread-menu gnus-summary-mode-map ""
2164       '("Threads"
2165         ["Toggle threading" gnus-summary-toggle-threads t]
2166         ["Hide threads" gnus-summary-hide-all-threads t]
2167         ["Show threads" gnus-summary-show-all-threads t]
2168         ["Hide thread" gnus-summary-hide-thread t]
2169         ["Show thread" gnus-summary-show-thread t]
2170         ["Go to next thread" gnus-summary-next-thread t]
2171         ["Go to previous thread" gnus-summary-prev-thread t]
2172         ["Go down thread" gnus-summary-down-thread t]
2173         ["Go up thread" gnus-summary-up-thread t]
2174         ["Top of thread" gnus-summary-top-thread t]
2175         ["Mark thread as read" gnus-summary-kill-thread t]
2176         ["Lower thread score" gnus-summary-lower-thread t]
2177         ["Raise thread score" gnus-summary-raise-thread t]
2178         ["Rethread current" gnus-summary-rethread-current t]))
2179
2180     (easy-menu-define
2181       gnus-summary-post-menu gnus-summary-mode-map ""
2182       `("Post"
2183         ["Send a message (mail or news)" gnus-summary-post-news
2184          ,@(if (featurep 'xemacs) '(t)
2185              '(:help "Post an article"))]
2186         ["Followup" gnus-summary-followup
2187          ,@(if (featurep 'xemacs) '(t)
2188              '(:help "Post followup to this article"))]
2189         ["Followup and yank" gnus-summary-followup-with-original
2190          ,@(if (featurep 'xemacs) '(t)
2191              '(:help "Post followup to this article, quoting its contents"))]
2192         ["Supersede article" gnus-summary-supersede-article t]
2193         ["Cancel article" gnus-summary-cancel-article
2194          ,@(if (featurep 'xemacs) '(t)
2195              '(:help "Cancel an article you posted"))]
2196         ["Reply" gnus-summary-reply t]
2197         ["Reply and yank" gnus-summary-reply-with-original t]
2198         ["Wide reply" gnus-summary-wide-reply t]
2199         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2200          ,@(if (featurep 'xemacs) '(t)
2201              '(:help "Mail a reply, quoting this article"))]
2202         ["Very wide reply" gnus-summary-very-wide-reply t]
2203         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2204          ,@(if (featurep 'xemacs) '(t)
2205              '(:help "Mail a very wide reply, quoting this article"))]
2206         ["Mail forward" gnus-summary-mail-forward t]
2207         ["Post forward" gnus-summary-post-forward t]
2208         ["Digest and mail" gnus-uu-digest-mail-forward t]
2209         ["Digest and post" gnus-uu-digest-post-forward t]
2210         ["Resend message" gnus-summary-resend-message t]
2211         ["Resend message edit" gnus-summary-resend-message-edit t]
2212         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2213         ["Send a mail" gnus-summary-mail-other-window t]
2214         ["Create a local message" gnus-summary-news-other-window t]
2215         ["Uuencode and post" gnus-uu-post-news
2216          ,@(if (featurep 'xemacs) '(t)
2217              '(:help "Post a uuencoded article"))]
2218         ["Followup via news" gnus-summary-followup-to-mail t]
2219         ["Followup via news and yank"
2220          gnus-summary-followup-to-mail-with-original t]
2221         ;;("Draft"
2222         ;;["Send" gnus-summary-send-draft t]
2223         ;;["Send bounced" gnus-resend-bounced-mail t])
2224         ))
2225
2226     (cond
2227      ((not (keymapp gnus-summary-post-menu))
2228       (setq gnus-article-post-menu gnus-summary-post-menu))
2229      ((not gnus-article-post-menu)
2230       ;; Don't share post menu.
2231       (setq gnus-article-post-menu
2232             (copy-keymap gnus-summary-post-menu))))
2233     (define-key gnus-article-mode-map [menu-bar post]
2234       (cons "Post" gnus-article-post-menu))
2235
2236     (easy-menu-define
2237       gnus-summary-misc-menu gnus-summary-mode-map ""
2238       `("Gnus"
2239         ("Mark Read"
2240          ["Mark as read" gnus-summary-mark-as-read-forward t]
2241          ["Mark same subject and select"
2242           gnus-summary-kill-same-subject-and-select t]
2243          ["Mark same subject" gnus-summary-kill-same-subject t]
2244          ["Catchup" gnus-summary-catchup
2245           ,@(if (featurep 'xemacs) '(t)
2246               '(:help "Mark unread articles in this group as read"))]
2247          ["Catchup all" gnus-summary-catchup-all t]
2248          ["Catchup to here" gnus-summary-catchup-to-here t]
2249          ["Catchup from here" gnus-summary-catchup-from-here t]
2250          ["Catchup region" gnus-summary-mark-region-as-read t]
2251          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2252         ("Mark Various"
2253          ["Tick" gnus-summary-tick-article-forward t]
2254          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2255          ["Remove marks" gnus-summary-clear-mark-forward t]
2256          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2257          ["Set bookmark" gnus-summary-set-bookmark t]
2258          ["Remove bookmark" gnus-summary-remove-bookmark t])
2259         ("Limit to"
2260          ["Marks..." gnus-summary-limit-to-marks t]
2261          ["Subject..." gnus-summary-limit-to-subject t]
2262          ["Author..." gnus-summary-limit-to-author t]
2263          ["Age..." gnus-summary-limit-to-age t]
2264          ["Extra..." gnus-summary-limit-to-extra t]
2265          ["Score" gnus-summary-limit-to-score t]
2266          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2267          ["Unread" gnus-summary-limit-to-unread t]
2268          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2269          ["Articles" gnus-summary-limit-to-articles t]
2270          ["Pop limit" gnus-summary-pop-limit t]
2271          ["Show dormant" gnus-summary-limit-include-dormant t]
2272          ["Hide childless dormant"
2273           gnus-summary-limit-exclude-childless-dormant t]
2274          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2275          ["Hide marked" gnus-summary-limit-exclude-marks t]
2276          ["Show expunged" gnus-summary-limit-include-expunged t])
2277         ("Process Mark"
2278          ["Set mark" gnus-summary-mark-as-processable t]
2279          ["Remove mark" gnus-summary-unmark-as-processable t]
2280          ["Remove all marks" gnus-summary-unmark-all-processable t]
2281          ["Mark above" gnus-uu-mark-over t]
2282          ["Mark series" gnus-uu-mark-series t]
2283          ["Mark region" gnus-uu-mark-region t]
2284          ["Unmark region" gnus-uu-unmark-region t]
2285          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2286          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2287          ["Mark all" gnus-uu-mark-all t]
2288          ["Mark buffer" gnus-uu-mark-buffer t]
2289          ["Mark sparse" gnus-uu-mark-sparse t]
2290          ["Mark thread" gnus-uu-mark-thread t]
2291          ["Unmark thread" gnus-uu-unmark-thread t]
2292          ("Process Mark Sets"
2293           ["Kill" gnus-summary-kill-process-mark t]
2294           ["Yank" gnus-summary-yank-process-mark
2295            gnus-newsgroup-process-stack]
2296           ["Save" gnus-summary-save-process-mark t]))
2297         ("Scroll article"
2298          ["Page forward" gnus-summary-next-page
2299           ,@(if (featurep 'xemacs) '(t)
2300               '(:help "Show next page of article"))]
2301          ["Page backward" gnus-summary-prev-page
2302           ,@(if (featurep 'xemacs) '(t)
2303               '(:help "Show previous page of article"))]
2304          ["Line forward" gnus-summary-scroll-up t])
2305         ("Move"
2306          ["Next unread article" gnus-summary-next-unread-article t]
2307          ["Previous unread article" gnus-summary-prev-unread-article t]
2308          ["Next article" gnus-summary-next-article t]
2309          ["Previous article" gnus-summary-prev-article t]
2310          ["Next unread subject" gnus-summary-next-unread-subject t]
2311          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2312          ["Next article same subject" gnus-summary-next-same-subject t]
2313          ["Previous article same subject" gnus-summary-prev-same-subject t]
2314          ["First unread article" gnus-summary-first-unread-article t]
2315          ["Best unread article" gnus-summary-best-unread-article t]
2316          ["Go to subject number..." gnus-summary-goto-subject t]
2317          ["Go to article number..." gnus-summary-goto-article t]
2318          ["Go to the last article" gnus-summary-goto-last-article t]
2319          ["Pop article off history" gnus-summary-pop-article t])
2320         ("Sort"
2321          ["Sort by number" gnus-summary-sort-by-number t]
2322          ["Sort by author" gnus-summary-sort-by-author t]
2323          ["Sort by subject" gnus-summary-sort-by-subject t]
2324          ["Sort by date" gnus-summary-sort-by-date t]
2325          ["Sort by score" gnus-summary-sort-by-score t]
2326          ["Sort by lines" gnus-summary-sort-by-lines t]
2327          ["Sort by characters" gnus-summary-sort-by-chars t]
2328          ["Randomize" gnus-summary-sort-by-random t]
2329          ["Original sort" gnus-summary-sort-by-original t])
2330         ("Help"
2331          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2332          ["Describe group" gnus-summary-describe-group t]
2333          ["Read manual" gnus-info-find-node t])
2334         ("Modes"
2335          ["Pick and read" gnus-pick-mode t]
2336          ["Binary" gnus-binary-mode t])
2337         ("Regeneration"
2338          ["Regenerate" gnus-summary-prepare t]
2339          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2340          ["Toggle threading" gnus-summary-toggle-threads t])
2341         ["See old articles" gnus-summary-insert-old-articles t]
2342         ["See new articles" gnus-summary-insert-new-articles t]
2343         ["Filter articles..." gnus-summary-execute-command t]
2344         ["Run command on subjects..." gnus-summary-universal-argument t]
2345         ["Search articles forward..." gnus-summary-search-article-forward t]
2346         ["Search articles backward..." gnus-summary-search-article-backward t]
2347         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2348         ["Expand window" gnus-summary-expand-window t]
2349         ["Expire expirable articles" gnus-summary-expire-articles
2350          (gnus-check-backend-function
2351           'request-expire-articles gnus-newsgroup-name)]
2352         ["Edit local kill file" gnus-summary-edit-local-kill t]
2353         ["Edit main kill file" gnus-summary-edit-global-kill t]
2354         ["Edit group parameters" gnus-summary-edit-parameters t]
2355         ["Customize group parameters" gnus-summary-customize-parameters t]
2356         ["Send a bug report" gnus-bug t]
2357         ("Exit"
2358          ["Catchup and exit" gnus-summary-catchup-and-exit
2359           ,@(if (featurep 'xemacs) '(t)
2360               '(:help "Mark unread articles in this group as read, then exit"))]
2361          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2362          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2363          ["Exit group" gnus-summary-exit
2364           ,@(if (featurep 'xemacs) '(t)
2365               '(:help "Exit current group, return to group selection mode"))]
2366          ["Exit group without updating" gnus-summary-exit-no-update t]
2367          ["Exit and goto next group" gnus-summary-next-group t]
2368          ["Exit and goto prev group" gnus-summary-prev-group t]
2369          ["Reselect group" gnus-summary-reselect-current-group t]
2370          ["Rescan group" gnus-summary-rescan-group t]
2371          ["Update dribble" gnus-summary-save-newsrc t])))
2372
2373     (gnus-run-hooks 'gnus-summary-menu-hook)))
2374
2375 (defvar gnus-summary-tool-bar-map nil)
2376
2377 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2378 (defun gnus-summary-make-tool-bar ()
2379   (if (and (fboundp 'tool-bar-add-item-from-menu)
2380            (default-value 'tool-bar-mode)
2381            (not gnus-summary-tool-bar-map))
2382       (setq gnus-summary-tool-bar-map
2383             (let ((tool-bar-map (make-sparse-keymap))
2384                   (load-path (mm-image-load-path)))
2385               (tool-bar-add-item-from-menu
2386                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2387               (tool-bar-add-item-from-menu
2388                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2389               (tool-bar-add-item-from-menu
2390                'gnus-summary-post-news "post" gnus-summary-mode-map)
2391               (tool-bar-add-item-from-menu
2392                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2393               (tool-bar-add-item-from-menu
2394                'gnus-summary-followup "followup" gnus-summary-mode-map)
2395               (tool-bar-add-item-from-menu
2396                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2397               (tool-bar-add-item-from-menu
2398                'gnus-summary-reply "reply" gnus-summary-mode-map)
2399               (tool-bar-add-item-from-menu
2400                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2401               (tool-bar-add-item-from-menu
2402                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2403               (tool-bar-add-item-from-menu
2404                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2405               (tool-bar-add-item-from-menu
2406                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2407               (tool-bar-add-item-from-menu
2408                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2409               (tool-bar-add-item-from-menu
2410                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2411               (tool-bar-add-item-from-menu
2412                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2413               (tool-bar-add-item-from-menu
2414                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2415               tool-bar-map)))
2416   (if gnus-summary-tool-bar-map
2417       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2418
2419 (defun gnus-score-set-default (var value)
2420   "A version of set that updates the GNU Emacs menu-bar."
2421   (set var value)
2422   ;; It is the message that forces the active status to be updated.
2423   (message ""))
2424
2425 (defun gnus-make-score-map (type)
2426   "Make a summary score map of type TYPE."
2427   (if t
2428       nil
2429     (let ((headers '(("author" "from" string)
2430                      ("subject" "subject" string)
2431                      ("article body" "body" string)
2432                      ("article head" "head" string)
2433                      ("xref" "xref" string)
2434                      ("extra header" "extra" string)
2435                      ("lines" "lines" number)
2436                      ("followups to author" "followup" string)))
2437           (types '((number ("less than" <)
2438                            ("greater than" >)
2439                            ("equal" =))
2440                    (string ("substring" s)
2441                            ("exact string" e)
2442                            ("fuzzy string" f)
2443                            ("regexp" r))))
2444           (perms '(("temporary" (current-time-string))
2445                    ("permanent" nil)
2446                    ("immediate" now)))
2447           header)
2448       (list
2449        (apply
2450         'nconc
2451         (list
2452          (if (eq type 'lower)
2453              "Lower score"
2454            "Increase score"))
2455         (let (outh)
2456           (while headers
2457             (setq header (car headers))
2458             (setq outh
2459                   (cons
2460                    (apply
2461                     'nconc
2462                     (list (car header))
2463                     (let ((ts (cdr (assoc (nth 2 header) types)))
2464                           outt)
2465                       (while ts
2466                         (setq outt
2467                               (cons
2468                                (apply
2469                                 'nconc
2470                                 (list (caar ts))
2471                                 (let ((ps perms)
2472                                       outp)
2473                                   (while ps
2474                                     (setq outp
2475                                           (cons
2476                                            (vector
2477                                             (caar ps)
2478                                             (list
2479                                              'gnus-summary-score-entry
2480                                              (nth 1 header)
2481                                              (if (or (string= (nth 1 header)
2482                                                               "head")
2483                                                      (string= (nth 1 header)
2484                                                               "body"))
2485                                                  ""
2486                                                (list 'gnus-summary-header
2487                                                      (nth 1 header)))
2488                                              (list 'quote (nth 1 (car ts)))
2489                                              (list 'gnus-score-delta-default
2490                                                    nil)
2491                                              (nth 1 (car ps))
2492                                              t)
2493                                             t)
2494                                            outp))
2495                                     (setq ps (cdr ps)))
2496                                   (list (nreverse outp))))
2497                                outt))
2498                         (setq ts (cdr ts)))
2499                       (list (nreverse outt))))
2500                    outh))
2501             (setq headers (cdr headers)))
2502           (list (nreverse outh))))))))
2503
2504 \f
2505
2506 (defun gnus-summary-mode (&optional group)
2507   "Major mode for reading articles.
2508
2509 All normal editing commands are switched off.
2510 \\<gnus-summary-mode-map>
2511 Each line in this buffer represents one article.  To read an
2512 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2513 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2514 respectively.
2515
2516 You can also post articles and send mail from this buffer.  To
2517 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2518 of an article, type `\\[gnus-summary-reply]'.
2519
2520 There are approx. one gazillion commands you can execute in this
2521 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2522
2523 The following commands are available:
2524
2525 \\{gnus-summary-mode-map}"
2526   (interactive)
2527   (kill-all-local-variables)
2528   (when (gnus-visual-p 'summary-menu 'menu)
2529     (gnus-summary-make-menu-bar)
2530     (gnus-summary-make-tool-bar))
2531   (gnus-summary-make-local-variables)
2532   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2533     (gnus-summary-make-local-variables))
2534   (gnus-make-thread-indent-array)
2535   (gnus-simplify-mode-line)
2536   (setq major-mode 'gnus-summary-mode)
2537   (setq mode-name "Summary")
2538   (make-local-variable 'minor-mode-alist)
2539   (use-local-map gnus-summary-mode-map)
2540   (buffer-disable-undo)
2541   (setq buffer-read-only t)             ;Disable modification
2542   (setq truncate-lines t)
2543   (setq selective-display t)
2544   (setq selective-display-ellipses t)   ;Display `...'
2545   (gnus-summary-set-display-table)
2546   (gnus-set-default-directory)
2547   (setq gnus-newsgroup-name group)
2548   (make-local-variable 'gnus-summary-line-format)
2549   (make-local-variable 'gnus-summary-line-format-spec)
2550   (make-local-variable 'gnus-summary-dummy-line-format)
2551   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2552   (make-local-variable 'gnus-summary-mark-positions)
2553   (make-local-hook 'pre-command-hook)
2554   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2555   (gnus-run-hooks 'gnus-summary-mode-hook)
2556   (turn-on-gnus-mailing-list-mode)
2557   (mm-enable-multibyte)
2558   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2559   (gnus-update-summary-mark-positions))
2560
2561 (defun gnus-summary-make-local-variables ()
2562   "Make all the local summary buffer variables."
2563   (let (global)
2564     (dolist (local gnus-summary-local-variables)
2565       (if (consp local)
2566           (progn
2567             (if (eq (cdr local) 'global)
2568                 ;; Copy the global value of the variable.
2569                 (setq global (symbol-value (car local)))
2570               ;; Use the value from the list.
2571               (setq global (eval (cdr local))))
2572             (set (make-local-variable (car local)) global))
2573         ;; Simple nil-valued local variable.
2574         (set (make-local-variable local) nil)))))
2575
2576 (defun gnus-summary-clear-local-variables ()
2577   (let ((locals gnus-summary-local-variables))
2578     (while locals
2579       (if (consp (car locals))
2580           (and (vectorp (caar locals))
2581                (set (caar locals) nil))
2582         (and (vectorp (car locals))
2583              (set (car locals) nil)))
2584       (setq locals (cdr locals)))))
2585
2586 ;; Summary data functions.
2587
2588 (defmacro gnus-data-number (data)
2589   `(car ,data))
2590
2591 (defmacro gnus-data-set-number (data number)
2592   `(setcar ,data ,number))
2593
2594 (defmacro gnus-data-mark (data)
2595   `(nth 1 ,data))
2596
2597 (defmacro gnus-data-set-mark (data mark)
2598   `(setcar (nthcdr 1 ,data) ,mark))
2599
2600 (defmacro gnus-data-pos (data)
2601   `(nth 2 ,data))
2602
2603 (defmacro gnus-data-set-pos (data pos)
2604   `(setcar (nthcdr 2 ,data) ,pos))
2605
2606 (defmacro gnus-data-header (data)
2607   `(nth 3 ,data))
2608
2609 (defmacro gnus-data-set-header (data header)
2610   `(setf (nth 3 ,data) ,header))
2611
2612 (defmacro gnus-data-level (data)
2613   `(nth 4 ,data))
2614
2615 (defmacro gnus-data-unread-p (data)
2616   `(= (nth 1 ,data) gnus-unread-mark))
2617
2618 (defmacro gnus-data-read-p (data)
2619   `(/= (nth 1 ,data) gnus-unread-mark))
2620
2621 (defmacro gnus-data-pseudo-p (data)
2622   `(consp (nth 3 ,data)))
2623
2624 (defmacro gnus-data-find (number)
2625   `(assq ,number gnus-newsgroup-data))
2626
2627 (defmacro gnus-data-find-list (number &optional data)
2628   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2629      (memq (assq ,number bdata)
2630            bdata)))
2631
2632 (defmacro gnus-data-make (number mark pos header level)
2633   `(list ,number ,mark ,pos ,header ,level))
2634
2635 (defun gnus-data-enter (after-article number mark pos header level offset)
2636   (let ((data (gnus-data-find-list after-article)))
2637     (unless data
2638       (error "No such article: %d" after-article))
2639     (setcdr data (cons (gnus-data-make number mark pos header level)
2640                        (cdr data)))
2641     (setq gnus-newsgroup-data-reverse nil)
2642     (gnus-data-update-list (cddr data) offset)))
2643
2644 (defun gnus-data-enter-list (after-article list &optional offset)
2645   (when list
2646     (let ((data (and after-article (gnus-data-find-list after-article)))
2647           (ilist list))
2648       (if (not (or data
2649                    after-article))
2650           (let ((odata gnus-newsgroup-data))
2651             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2652             (when offset
2653               (gnus-data-update-list odata offset)))
2654       ;; Find the last element in the list to be spliced into the main
2655         ;; list.
2656         (while (cdr list)
2657           (setq list (cdr list)))
2658         (if (not data)
2659             (progn
2660               (setcdr list gnus-newsgroup-data)
2661               (setq gnus-newsgroup-data ilist)
2662               (when offset
2663                 (gnus-data-update-list (cdr list) offset)))
2664           (setcdr list (cdr data))
2665           (setcdr data ilist)
2666           (when offset
2667             (gnus-data-update-list (cdr list) offset))))
2668       (setq gnus-newsgroup-data-reverse nil))))
2669
2670 (defun gnus-data-remove (article &optional offset)
2671   (let ((data gnus-newsgroup-data))
2672     (if (= (gnus-data-number (car data)) article)
2673         (progn
2674           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2675                 gnus-newsgroup-data-reverse nil)
2676           (when offset
2677             (gnus-data-update-list gnus-newsgroup-data offset)))
2678       (while (cdr data)
2679         (when (= (gnus-data-number (cadr data)) article)
2680           (setcdr data (cddr data))
2681           (when offset
2682             (gnus-data-update-list (cdr data) offset))
2683           (setq data nil
2684                 gnus-newsgroup-data-reverse nil))
2685         (setq data (cdr data))))))
2686
2687 (defmacro gnus-data-list (backward)
2688   `(if ,backward
2689        (or gnus-newsgroup-data-reverse
2690            (setq gnus-newsgroup-data-reverse
2691                  (reverse gnus-newsgroup-data)))
2692      gnus-newsgroup-data))
2693
2694 (defun gnus-data-update-list (data offset)
2695   "Add OFFSET to the POS of all data entries in DATA."
2696   (setq gnus-newsgroup-data-reverse nil)
2697   (while data
2698     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2699     (setq data (cdr data))))
2700
2701 (defun gnus-summary-article-pseudo-p (article)
2702   "Say whether this article is a pseudo article or not."
2703   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2704
2705 (defmacro gnus-summary-article-sparse-p (article)
2706   "Say whether this article is a sparse article or not."
2707   `(memq ,article gnus-newsgroup-sparse))
2708
2709 (defmacro gnus-summary-article-ancient-p (article)
2710   "Say whether this article is a sparse article or not."
2711   `(memq ,article gnus-newsgroup-ancient))
2712
2713 (defun gnus-article-parent-p (number)
2714   "Say whether this article is a parent or not."
2715   (let ((data (gnus-data-find-list number)))
2716     (and (cdr data)              ; There has to be an article after...
2717          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2718             (gnus-data-level (nth 1 data))))))
2719
2720 (defun gnus-article-children (number)
2721   "Return a list of all children to NUMBER."
2722   (let* ((data (gnus-data-find-list number))
2723          (level (gnus-data-level (car data)))
2724          children)
2725     (setq data (cdr data))
2726     (while (and data
2727                 (= (gnus-data-level (car data)) (1+ level)))
2728       (push (gnus-data-number (car data)) children)
2729       (setq data (cdr data)))
2730     children))
2731
2732 (defmacro gnus-summary-skip-intangible ()
2733   "If the current article is intangible, then jump to a different article."
2734   '(let ((to (get-text-property (point) 'gnus-intangible)))
2735      (and to (gnus-summary-goto-subject to))))
2736
2737 (defmacro gnus-summary-article-intangible-p ()
2738   "Say whether this article is intangible or not."
2739   '(get-text-property (point) 'gnus-intangible))
2740
2741 (defun gnus-article-read-p (article)
2742   "Say whether ARTICLE is read or not."
2743   (not (or (memq article gnus-newsgroup-marked)
2744            (memq article gnus-newsgroup-spam-marked)
2745            (memq article gnus-newsgroup-unreads)
2746            (memq article gnus-newsgroup-unselected)
2747            (memq article gnus-newsgroup-dormant))))
2748
2749 ;; Some summary mode macros.
2750
2751 (defmacro gnus-summary-article-number ()
2752   "The article number of the article on the current line.
2753 If there isn's an article number here, then we return the current
2754 article number."
2755   '(progn
2756      (gnus-summary-skip-intangible)
2757      (or (get-text-property (point) 'gnus-number)
2758          (gnus-summary-last-subject))))
2759
2760 (defmacro gnus-summary-article-header (&optional number)
2761   "Return the header of article NUMBER."
2762   `(gnus-data-header (gnus-data-find
2763                       ,(or number '(gnus-summary-article-number)))))
2764
2765 (defmacro gnus-summary-thread-level (&optional number)
2766   "Return the level of thread that starts with article NUMBER."
2767   `(if (and (eq gnus-summary-make-false-root 'dummy)
2768             (get-text-property (point) 'gnus-intangible))
2769        0
2770      (gnus-data-level (gnus-data-find
2771                        ,(or number '(gnus-summary-article-number))))))
2772
2773 (defmacro gnus-summary-article-mark (&optional number)
2774   "Return the mark of article NUMBER."
2775   `(gnus-data-mark (gnus-data-find
2776                     ,(or number '(gnus-summary-article-number)))))
2777
2778 (defmacro gnus-summary-article-pos (&optional number)
2779   "Return the position of the line of article NUMBER."
2780   `(gnus-data-pos (gnus-data-find
2781                    ,(or number '(gnus-summary-article-number)))))
2782
2783 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2784 (defmacro gnus-summary-article-subject (&optional number)
2785   "Return current subject string or nil if nothing."
2786   `(let ((headers
2787           ,(if number
2788                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2789              '(gnus-data-header (assq (gnus-summary-article-number)
2790                                       gnus-newsgroup-data)))))
2791      (and headers
2792           (vectorp headers)
2793           (mail-header-subject headers))))
2794
2795 (defmacro gnus-summary-article-score (&optional number)
2796   "Return current article score."
2797   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2798                   gnus-newsgroup-scored))
2799        gnus-summary-default-score 0))
2800
2801 (defun gnus-summary-article-children (&optional number)
2802   "Return a list of article numbers that are children of article NUMBER."
2803   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2804          (level (gnus-data-level (car data)))
2805          l children)
2806     (while (and (setq data (cdr data))
2807                 (> (setq l (gnus-data-level (car data))) level))
2808       (and (= (1+ level) l)
2809            (push (gnus-data-number (car data))
2810                  children)))
2811     (nreverse children)))
2812
2813 (defun gnus-summary-article-parent (&optional number)
2814   "Return the article number of the parent of article NUMBER."
2815   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2816                                     (gnus-data-list t)))
2817          (level (gnus-data-level (car data))))
2818     (if (zerop level)
2819         ()                              ; This is a root.
2820       ;; We search until we find an article with a level less than
2821       ;; this one.  That function has to be the parent.
2822       (while (and (setq data (cdr data))
2823                   (not (< (gnus-data-level (car data)) level))))
2824       (and data (gnus-data-number (car data))))))
2825
2826 (defun gnus-unread-mark-p (mark)
2827   "Say whether MARK is the unread mark."
2828   (= mark gnus-unread-mark))
2829
2830 (defun gnus-read-mark-p (mark)
2831   "Say whether MARK is one of the marks that mark as read.
2832 This is all marks except unread, ticked, dormant, and expirable."
2833   (not (or (= mark gnus-unread-mark)
2834            (= mark gnus-ticked-mark)
2835            (= mark gnus-dormant-mark)
2836            (= mark gnus-expirable-mark))))
2837
2838 (defmacro gnus-article-mark (number)
2839   "Return the MARK of article NUMBER.
2840 This macro should only be used when computing the mark the \"first\"
2841 time; i.e., when generating the summary lines.  After that,
2842 `gnus-summary-article-mark' should be used to examine the
2843 marks of articles."
2844   `(cond
2845     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2846     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2847     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2848     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2849     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2850     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2851     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2852     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2853     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2854            gnus-ancient-mark))))
2855
2856 ;; Saving hidden threads.
2857
2858 (defmacro gnus-save-hidden-threads (&rest forms)
2859   "Save hidden threads, eval FORMS, and restore the hidden threads."
2860   (let ((config (make-symbol "config")))
2861     `(let ((,config (gnus-hidden-threads-configuration)))
2862        (unwind-protect
2863            (save-excursion
2864              ,@forms)
2865          (gnus-restore-hidden-threads-configuration ,config)))))
2866 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2867 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2868
2869 (defun gnus-data-compute-positions ()
2870   "Compute the positions of all articles."
2871   (setq gnus-newsgroup-data-reverse nil)
2872   (let ((data gnus-newsgroup-data))
2873     (save-excursion
2874       (gnus-save-hidden-threads
2875         (gnus-summary-show-all-threads)
2876         (goto-char (point-min))
2877         (while data
2878           (while (get-text-property (point) 'gnus-intangible)
2879             (forward-line 1))
2880           (gnus-data-set-pos (car data) (+ (point) 3))
2881           (setq data (cdr data))
2882           (forward-line 1))))))
2883
2884 (defun gnus-hidden-threads-configuration ()
2885   "Return the current hidden threads configuration."
2886   (save-excursion
2887     (let (config)
2888       (goto-char (point-min))
2889       (while (search-forward "\r" nil t)
2890         (push (1- (point)) config))
2891       config)))
2892
2893 (defun gnus-restore-hidden-threads-configuration (config)
2894   "Restore hidden threads configuration from CONFIG."
2895   (save-excursion
2896     (let (point buffer-read-only)
2897       (while (setq point (pop config))
2898         (when (and (< point (point-max))
2899                    (goto-char point)
2900                    (eq (char-after) ?\n))
2901           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2902
2903 ;; Various summary mode internalish functions.
2904
2905 (defun gnus-mouse-pick-article (e)
2906   (interactive "e")
2907   (mouse-set-point e)
2908   (gnus-summary-next-page nil t))
2909
2910 (defun gnus-summary-set-display-table ()
2911   "Change the display table.
2912 Odd characters have a tendency to mess
2913 up nicely formatted displays - we make all possible glyphs
2914 display only a single character."
2915
2916   ;; We start from the standard display table, if any.
2917   (let ((table (or (copy-sequence standard-display-table)
2918                    (make-display-table)))
2919         (i 32))
2920     ;; Nix out all the control chars...
2921     (while (>= (setq i (1- i)) 0)
2922       (aset table i [??]))
2923    ;; ... but not newline and cr, of course.  (cr is necessary for the
2924     ;; selective display).
2925     (aset table ?\n nil)
2926     (aset table ?\r nil)
2927     ;; We keep TAB as well.
2928     (aset table ?\t nil)
2929     ;; We nix out any glyphs over 126 that are not set already.
2930     (let ((i 256))
2931       (while (>= (setq i (1- i)) 127)
2932         ;; Only modify if the entry is nil.
2933         (unless (aref table i)
2934           (aset table i [??]))))
2935     (setq buffer-display-table table)))
2936
2937 (defun gnus-summary-set-article-display-arrow (pos)
2938   "Update the overlay arrow to point to line at position POS."
2939   (when (and gnus-summary-display-arrow
2940              (boundp 'overlay-arrow-position)
2941              (boundp 'overlay-arrow-string))
2942     (save-excursion
2943       (goto-char pos)
2944       (beginning-of-line)
2945       (unless overlay-arrow-position
2946         (setq overlay-arrow-position (make-marker)))
2947       (setq overlay-arrow-string "=>"
2948             overlay-arrow-position (set-marker overlay-arrow-position
2949                                                (point)
2950                                                (current-buffer))))))
2951
2952 (defun gnus-summary-buffer-name (group)
2953   "Return the summary buffer name of GROUP."
2954   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2955
2956 (defun gnus-summary-setup-buffer (group)
2957   "Initialize summary buffer."
2958   (let ((buffer (gnus-summary-buffer-name group))
2959         (dead-name (concat "*Dead Summary "
2960                            (gnus-group-decoded-name group) "*")))
2961     ;; If a dead summary buffer exists, we kill it.
2962     (when (gnus-buffer-live-p dead-name)
2963       (gnus-kill-buffer dead-name))
2964     (if (get-buffer buffer)
2965         (progn
2966           (set-buffer buffer)
2967           (setq gnus-summary-buffer (current-buffer))
2968           (not gnus-newsgroup-prepared))
2969       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2970       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2971       (gnus-summary-mode group)
2972       (when gnus-carpal
2973         (gnus-carpal-setup-buffer 'summary))
2974       (unless gnus-single-article-buffer
2975         (make-local-variable 'gnus-article-buffer)
2976         (make-local-variable 'gnus-article-current)
2977         (make-local-variable 'gnus-original-article-buffer))
2978       (setq gnus-newsgroup-name group)
2979       ;; Set any local variables in the group parameters.
2980       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2981       t)))
2982
2983 (defun gnus-set-global-variables ()
2984   "Set the global equivalents of the buffer-local variables.
2985 They are set to the latest values they had.  These reflect the summary
2986 buffer that was in action when the last article was fetched."
2987   (when (eq major-mode 'gnus-summary-mode)
2988     (setq gnus-summary-buffer (current-buffer))
2989     (let ((name gnus-newsgroup-name)
2990           (marked gnus-newsgroup-marked)
2991           (spam gnus-newsgroup-spam-marked)
2992           (unread gnus-newsgroup-unreads)
2993           (headers gnus-current-headers)
2994           (data gnus-newsgroup-data)
2995           (summary gnus-summary-buffer)
2996           (article-buffer gnus-article-buffer)
2997           (original gnus-original-article-buffer)
2998           (gac gnus-article-current)
2999           (reffed gnus-reffed-article-number)
3000           (score-file gnus-current-score-file)
3001           (default-charset gnus-newsgroup-charset)
3002           vlist)
3003       (let ((locals gnus-newsgroup-variables))
3004         (while locals
3005           (if (consp (car locals))
3006               (push (eval (caar locals)) vlist)
3007             (push (eval (car locals)) vlist))
3008           (setq locals (cdr locals)))
3009         (setq vlist (nreverse vlist)))
3010       (save-excursion
3011         (set-buffer gnus-group-buffer)
3012         (setq gnus-newsgroup-name name
3013               gnus-newsgroup-marked marked
3014               gnus-newsgroup-spam-marked spam
3015               gnus-newsgroup-unreads unread
3016               gnus-current-headers headers
3017               gnus-newsgroup-data data
3018               gnus-article-current gac
3019               gnus-summary-buffer summary
3020               gnus-article-buffer article-buffer
3021               gnus-original-article-buffer original
3022               gnus-reffed-article-number reffed
3023               gnus-current-score-file score-file
3024               gnus-newsgroup-charset default-charset)
3025         (let ((locals gnus-newsgroup-variables))
3026           (while locals
3027             (if (consp (car locals))
3028                 (set (caar locals) (pop vlist))
3029               (set (car locals) (pop vlist)))
3030             (setq locals (cdr locals))))
3031         ;; The article buffer also has local variables.
3032         (when (gnus-buffer-live-p gnus-article-buffer)
3033           (set-buffer gnus-article-buffer)
3034           (setq gnus-summary-buffer summary))))))
3035
3036 (defun gnus-summary-article-unread-p (article)
3037   "Say whether ARTICLE is unread or not."
3038   (memq article gnus-newsgroup-unreads))
3039
3040 (defun gnus-summary-first-article-p (&optional article)
3041   "Return whether ARTICLE is the first article in the buffer."
3042   (if (not (setq article (or article (gnus-summary-article-number))))
3043       nil
3044     (eq article (caar gnus-newsgroup-data))))
3045
3046 (defun gnus-summary-last-article-p (&optional article)
3047   "Return whether ARTICLE is the last article in the buffer."
3048   (if (not (setq article (or article (gnus-summary-article-number))))
3049       ;; All non-existent numbers are the last article.  :-)
3050       t
3051     (not (cdr (gnus-data-find-list article)))))
3052
3053 (defun gnus-make-thread-indent-array ()
3054   (let ((n 200))
3055     (unless (and gnus-thread-indent-array
3056                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3057       (setq gnus-thread-indent-array (make-vector 201 "")
3058             gnus-thread-indent-array-level gnus-thread-indent-level)
3059       (while (>= n 0)
3060         (aset gnus-thread-indent-array n
3061               (make-string (* n gnus-thread-indent-level) ? ))
3062         (setq n (1- n))))))
3063
3064 (defun gnus-update-summary-mark-positions ()
3065   "Compute where the summary marks are to go."
3066   (save-excursion
3067     (when (gnus-buffer-exists-p gnus-summary-buffer)
3068       (set-buffer gnus-summary-buffer))
3069     (let ((gnus-replied-mark 129)
3070           (gnus-score-below-mark 130)
3071           (gnus-score-over-mark 130)
3072           (gnus-download-mark 131)
3073           (spec gnus-summary-line-format-spec)
3074           gnus-visual pos)
3075       (save-excursion
3076         (gnus-set-work-buffer)
3077         (let ((gnus-summary-line-format-spec spec)
3078               (gnus-newsgroup-downloadable '((0 . t))))
3079           (gnus-summary-insert-line
3080            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3081            0 nil 128 t nil "" nil 1)
3082           (goto-char (point-min))
3083           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3084                                              (- (point) (point-min) 1)))))
3085           (goto-char (point-min))
3086           (push (cons 'replied (and (search-forward "\201" nil t)
3087                                     (- (point) (point-min) 1)))
3088                 pos)
3089           (goto-char (point-min))
3090           (push (cons 'score (and (search-forward "\202" nil t)
3091                                   (- (point) (point-min) 1)))
3092                 pos)
3093           (goto-char (point-min))
3094           (push (cons 'download
3095                       (and (search-forward "\203" nil t)
3096                            (- (point) (point-min) 1)))
3097                 pos)))
3098       (setq gnus-summary-mark-positions pos))))
3099
3100 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3101   "Insert a dummy root in the summary buffer."
3102   (beginning-of-line)
3103   (gnus-add-text-properties
3104    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3105    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3106
3107 (defun gnus-summary-extract-address-component (from)
3108   (or (car (funcall gnus-extract-address-components from))
3109       from))
3110
3111 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3112   (let ((mail-parse-charset gnus-newsgroup-charset)
3113         ; Is it really necessary to do this next part for each summary line?
3114         ; Luckily, doesn't seem to slow things down much.
3115         (mail-parse-ignored-charsets
3116          (save-excursion (set-buffer gnus-summary-buffer)
3117                          gnus-newsgroup-ignored-charsets)))
3118     (or
3119      (and gnus-ignored-from-addresses
3120           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3121           (let ((extra-headers (mail-header-extra header))
3122                 to
3123                 newsgroups)
3124             (cond
3125              ((setq to (cdr (assq 'To extra-headers)))
3126               (concat "-> "
3127                       (inline
3128                         (gnus-summary-extract-address-component
3129                          (funcall gnus-decode-encoded-word-function to)))))
3130              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3131               (concat "=> " newsgroups)))))
3132      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3133
3134 (defun gnus-summary-insert-line (gnus-tmp-header
3135                                  gnus-tmp-level gnus-tmp-current
3136                                  gnus-tmp-unread gnus-tmp-replied
3137                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3138                                  &optional gnus-tmp-dummy gnus-tmp-score
3139                                  gnus-tmp-process)
3140   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3141          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3142          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3143          (gnus-tmp-score-char
3144           (if (or (null gnus-summary-default-score)
3145                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3146                       gnus-summary-zcore-fuzz))
3147               ?                         ;Whitespace
3148             (if (< gnus-tmp-score gnus-summary-default-score)
3149                 gnus-score-below-mark gnus-score-over-mark)))
3150          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3151          (gnus-tmp-replied
3152           (cond (gnus-tmp-process gnus-process-mark)
3153                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3154                  gnus-cached-mark)
3155                 (gnus-tmp-replied gnus-replied-mark)
3156                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3157                  gnus-forwarded-mark)
3158                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3159                  gnus-saved-mark)
3160                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3161                  gnus-recent-mark)
3162                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3163                  gnus-unseen-mark)
3164                 (t gnus-no-mark)))
3165          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3166          (gnus-tmp-name
3167           (cond
3168            ((string-match "<[^>]+> *$" gnus-tmp-from)
3169             (let ((beg (match-beginning 0)))
3170               (or (and (string-match "^\".+\"" gnus-tmp-from)
3171                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3172                   (substring gnus-tmp-from 0 beg))))
3173            ((string-match "(.+)" gnus-tmp-from)
3174             (substring gnus-tmp-from
3175                        (1+ (match-beginning 0)) (1- (match-end 0))))
3176            (t gnus-tmp-from)))
3177          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3178          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3179          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3180          (buffer-read-only nil))
3181     (when (string= gnus-tmp-name "")
3182       (setq gnus-tmp-name gnus-tmp-from))
3183     (unless (numberp gnus-tmp-lines)
3184       (setq gnus-tmp-lines -1))
3185     (if (= gnus-tmp-lines -1)
3186         (setq gnus-tmp-lines "?")
3187       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3188     (gnus-put-text-property
3189      (point)
3190      (progn (eval gnus-summary-line-format-spec) (point))
3191      'gnus-number gnus-tmp-number)
3192     (when (gnus-visual-p 'summary-highlight 'highlight)
3193       (forward-line -1)
3194       (gnus-run-hooks 'gnus-summary-update-hook)
3195       (forward-line 1))))
3196
3197 (defun gnus-summary-update-line (&optional dont-update)
3198   "Update summary line after change."
3199   (when (and gnus-summary-default-score
3200              (not gnus-summary-inhibit-highlight))
3201     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3202            (article (gnus-summary-article-number))
3203            (score (gnus-summary-article-score article)))
3204       (unless dont-update
3205         (if (and gnus-summary-mark-below
3206                  (< (gnus-summary-article-score)
3207                     gnus-summary-mark-below))
3208             ;; This article has a low score, so we mark it as read.
3209             (when (memq article gnus-newsgroup-unreads)
3210               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3211           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3212             ;; This article was previously marked as read on account
3213             ;; of a low score, but now it has risen, so we mark it as
3214             ;; unread.
3215             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3216         (gnus-summary-update-mark
3217          (if (or (null gnus-summary-default-score)
3218                  (<= (abs (- score gnus-summary-default-score))
3219                      gnus-summary-zcore-fuzz))
3220              ?                          ;Whitespace
3221            (if (< score gnus-summary-default-score)
3222                gnus-score-below-mark gnus-score-over-mark))
3223          'score))
3224       ;; Do visual highlighting.
3225       (when (gnus-visual-p 'summary-highlight 'highlight)
3226         (gnus-run-hooks 'gnus-summary-update-hook)))))
3227
3228 (defvar gnus-tmp-new-adopts nil)
3229
3230 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3231   "Return the number of articles in THREAD.
3232 This may be 0 in some cases -- if none of the articles in
3233 the thread are to be displayed."
3234   (let* ((number
3235          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3236           (cond
3237            ((not (listp thread))
3238             1)
3239            ((and (consp thread) (cdr thread))
3240             (apply
3241              '+ 1 (mapcar
3242                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3243            ((null thread)
3244             1)
3245            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3246             1)
3247            (t 0))))
3248     (when (and level (zerop level) gnus-tmp-new-adopts)
3249       (incf number
3250             (apply '+ (mapcar
3251                        'gnus-summary-number-of-articles-in-thread
3252                        gnus-tmp-new-adopts))))
3253     (if char
3254         (if (> number 1) gnus-not-empty-thread-mark
3255           gnus-empty-thread-mark)
3256       number)))
3257
3258 (defun gnus-summary-set-local-parameters (group)
3259   "Go through the local params of GROUP and set all variable specs in that list."
3260   (let ((params (gnus-group-find-parameter group))
3261         (vars '(quit-config))           ; Ignore quit-config.
3262         elem)
3263     (while params
3264       (setq elem (car params)
3265             params (cdr params))
3266       (and (consp elem)                 ; Has to be a cons.
3267            (consp (cdr elem))           ; The cdr has to be a list.
3268            (symbolp (car elem))         ; Has to be a symbol in there.
3269            (not (memq (car elem) vars))
3270            (ignore-errors               ; So we set it.
3271              (push (car elem) vars)
3272              (make-local-variable (car elem))
3273              (set (car elem) (eval (nth 1 elem))))))))
3274
3275 (defun gnus-summary-read-group (group &optional show-all no-article
3276                                       kill-buffer no-display backward
3277                                       select-articles)
3278   "Start reading news in newsgroup GROUP.
3279 If SHOW-ALL is non-nil, already read articles are also listed.
3280 If NO-ARTICLE is non-nil, no article is selected initially.
3281 If NO-DISPLAY, don't generate a summary buffer."
3282   (let (result)
3283     (while (and group
3284                 (null (setq result
3285                             (let ((gnus-auto-select-next nil))
3286                               (or (gnus-summary-read-group-1
3287                                    group show-all no-article
3288                                    kill-buffer no-display
3289                                    select-articles)
3290                                   (setq show-all nil
3291                                         select-articles nil)))))
3292                 (eq gnus-auto-select-next 'quietly))
3293       (set-buffer gnus-group-buffer)
3294       ;; The entry function called above goes to the next
3295       ;; group automatically, so we go two groups back
3296       ;; if we are searching for the previous group.
3297       (when backward
3298         (gnus-group-prev-unread-group 2))
3299       (if (not (equal group (gnus-group-group-name)))
3300           (setq group (gnus-group-group-name))
3301         (setq group nil)))
3302     result))
3303
3304 (defun gnus-summary-read-group-1 (group show-all no-article
3305                                         kill-buffer no-display
3306                                         &optional select-articles)
3307   ;; Killed foreign groups can't be entered.
3308   ;;  (when (and (not (gnus-group-native-p group))
3309   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3310   ;;    (error "Dead non-native groups can't be entered"))
3311   (gnus-message 5 "Retrieving newsgroup: %s..."
3312                 (gnus-group-decoded-name group))
3313   (let* ((new-group (gnus-summary-setup-buffer group))
3314          (quit-config (gnus-group-quit-config group))
3315          (did-select (and new-group (gnus-select-newsgroup
3316                                      group show-all select-articles))))
3317     (cond
3318      ;; This summary buffer exists already, so we just select it.
3319      ((not new-group)
3320       (gnus-set-global-variables)
3321       (when kill-buffer
3322         (gnus-kill-or-deaden-summary kill-buffer))
3323       (gnus-configure-windows 'summary 'force)
3324       (gnus-set-mode-line 'summary)
3325       (gnus-summary-position-point)
3326       (message "")
3327       t)
3328      ;; We couldn't select this group.
3329      ((null did-select)
3330       (when (and (eq major-mode 'gnus-summary-mode)
3331                  (not (equal (current-buffer) kill-buffer)))
3332         (kill-buffer (current-buffer))
3333         (if (not quit-config)
3334             (progn
3335               ;; Update the info -- marks might need to be removed,
3336               ;; for instance.
3337               (gnus-summary-update-info)
3338               (set-buffer gnus-group-buffer)
3339               (gnus-group-jump-to-group group)
3340               (gnus-group-next-unread-group 1))
3341           (gnus-handle-ephemeral-exit quit-config)))
3342       (let ((grpinfo (gnus-get-info group)))
3343         (if (null (gnus-info-read grpinfo))
3344             (gnus-message 3 "Group %s contains no messages"
3345                           (gnus-group-decoded-name group))
3346           (gnus-message 3 "Can't select group")))
3347       nil)
3348      ;; The user did a `C-g' while prompting for number of articles,
3349      ;; so we exit this group.
3350      ((eq did-select 'quit)
3351       (and (eq major-mode 'gnus-summary-mode)
3352            (not (equal (current-buffer) kill-buffer))
3353            (kill-buffer (current-buffer)))
3354       (when kill-buffer
3355         (gnus-kill-or-deaden-summary kill-buffer))
3356       (if (not quit-config)
3357           (progn
3358             (set-buffer gnus-group-buffer)
3359             (gnus-group-jump-to-group group)
3360             (gnus-group-next-unread-group 1)
3361             (gnus-configure-windows 'group 'force))
3362         (gnus-handle-ephemeral-exit quit-config))
3363       ;; Finally signal the quit.
3364       (signal 'quit nil))
3365      ;; The group was successfully selected.
3366      (t
3367       (gnus-set-global-variables)
3368       ;; Save the active value in effect when the group was entered.
3369       (setq gnus-newsgroup-active
3370             (gnus-copy-sequence
3371              (gnus-active gnus-newsgroup-name)))
3372       ;; You can change the summary buffer in some way with this hook.
3373       (gnus-run-hooks 'gnus-select-group-hook)
3374       (gnus-update-format-specifications
3375        nil 'summary 'summary-mode 'summary-dummy)
3376       (gnus-update-summary-mark-positions)
3377       ;; Do score processing.
3378       (when gnus-use-scoring
3379         (gnus-possibly-score-headers))
3380       ;; Check whether to fill in the gaps in the threads.
3381       (when gnus-build-sparse-threads
3382         (gnus-build-sparse-threads))
3383       ;; Find the initial limit.
3384       (if gnus-show-threads
3385           (if show-all
3386               (let ((gnus-newsgroup-dormant nil))
3387                 (gnus-summary-initial-limit show-all))
3388             (gnus-summary-initial-limit show-all))
3389         ;; When untreaded, all articles are always shown.
3390         (setq gnus-newsgroup-limit
3391               (mapcar
3392                (lambda (header) (mail-header-number header))
3393                gnus-newsgroup-headers)))
3394       ;; Generate the summary buffer.
3395       (unless no-display
3396         (gnus-summary-prepare))
3397       (when gnus-use-trees
3398         (gnus-tree-open group)
3399         (setq gnus-summary-highlight-line-function
3400               'gnus-tree-highlight-article))
3401       ;; If the summary buffer is empty, but there are some low-scored
3402       ;; articles or some excluded dormants, we include these in the
3403       ;; buffer.
3404       (when (and (zerop (buffer-size))
3405                  (not no-display))
3406         (cond (gnus-newsgroup-dormant
3407                (gnus-summary-limit-include-dormant))
3408               ((and gnus-newsgroup-scored show-all)
3409                (gnus-summary-limit-include-expunged t))))
3410       ;; Function `gnus-apply-kill-file' must be called in this hook.
3411       (gnus-run-hooks 'gnus-apply-kill-hook)
3412       (if (and (zerop (buffer-size))
3413                (not no-display))
3414           (progn
3415             ;; This newsgroup is empty.
3416             (gnus-summary-catchup-and-exit nil t)
3417             (gnus-message 6 "No unread news")
3418             (when kill-buffer
3419               (gnus-kill-or-deaden-summary kill-buffer))
3420             ;; Return nil from this function.
3421             nil)
3422         ;; Hide conversation thread subtrees.  We cannot do this in
3423         ;; gnus-summary-prepare-hook since kill processing may not
3424         ;; work with hidden articles.
3425         (gnus-summary-maybe-hide-threads)
3426         (when kill-buffer
3427           (gnus-kill-or-deaden-summary kill-buffer))
3428         (gnus-summary-auto-select-subject)
3429         ;; Show first unread article if requested.
3430         (if (and (not no-article)
3431                  (not no-display)
3432                  gnus-newsgroup-unreads
3433                  gnus-auto-select-first)
3434             (progn
3435               (gnus-configure-windows 'summary)
3436               (let ((art (gnus-summary-article-number)))
3437                 (unless (or (memq art gnus-newsgroup-undownloaded)
3438                             (memq art gnus-newsgroup-downloadable))
3439                   (gnus-summary-goto-article art))))
3440           ;; Don't select any articles.
3441           (gnus-summary-position-point)
3442           (gnus-configure-windows 'summary 'force)
3443           (gnus-set-mode-line 'summary))
3444         (when (get-buffer-window gnus-group-buffer t)
3445           ;; Gotta use windows, because recenter does weird stuff if
3446           ;; the current buffer ain't the displayed window.
3447           (let ((owin (selected-window)))
3448             (select-window (get-buffer-window gnus-group-buffer t))
3449             (when (gnus-group-goto-group group)
3450               (recenter))
3451             (select-window owin)))
3452         ;; Mark this buffer as "prepared".
3453         (setq gnus-newsgroup-prepared t)
3454         (gnus-run-hooks 'gnus-summary-prepared-hook)
3455         t)))))
3456
3457 (defun gnus-summary-auto-select-subject ()
3458   "Select the subject line on initial group entry."
3459   (goto-char (point-min))
3460   (cond
3461    ((eq gnus-auto-select-subject 'best)
3462     (gnus-summary-best-unread-subject))
3463    ((eq gnus-auto-select-subject 'unread)
3464     (gnus-summary-first-unread-subject))
3465    ((eq gnus-auto-select-subject 'unseen)
3466     (gnus-summary-first-unseen-subject))
3467    ((eq gnus-auto-select-subject 'unseen-or-unread)
3468     (gnus-summary-first-unseen-or-unread-subject))
3469    ((eq gnus-auto-select-subject 'first)
3470     ;; Do nothing.
3471     )
3472    ((gnus-functionp gnus-auto-select-subject)
3473     (funcall gnus-auto-select-subject))))
3474
3475 (defun gnus-summary-prepare ()
3476   "Generate the summary buffer."
3477   (interactive)
3478   (let ((buffer-read-only nil))
3479     (erase-buffer)
3480     (setq gnus-newsgroup-data nil
3481           gnus-newsgroup-data-reverse nil)
3482     (gnus-run-hooks 'gnus-summary-generate-hook)
3483     ;; Generate the buffer, either with threads or without.
3484     (when gnus-newsgroup-headers
3485       (gnus-summary-prepare-threads
3486        (if gnus-show-threads
3487            (gnus-sort-gathered-threads
3488             (funcall gnus-summary-thread-gathering-function
3489                      (gnus-sort-threads
3490                       (gnus-cut-threads (gnus-make-threads)))))
3491          ;; Unthreaded display.
3492          (gnus-sort-articles gnus-newsgroup-headers))))
3493     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3494     ;; Call hooks for modifying summary buffer.
3495     (goto-char (point-min))
3496     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3497
3498 (defsubst gnus-general-simplify-subject (subject)
3499   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3500   (setq subject
3501         (cond
3502          ;; Truncate the subject.
3503          (gnus-simplify-subject-functions
3504           (gnus-map-function gnus-simplify-subject-functions subject))
3505          ((numberp gnus-summary-gather-subject-limit)
3506           (setq subject (gnus-simplify-subject-re subject))
3507           (if (> (length subject) gnus-summary-gather-subject-limit)
3508               (substring subject 0 gnus-summary-gather-subject-limit)
3509             subject))
3510          ;; Fuzzily simplify it.
3511          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3512           (gnus-simplify-subject-fuzzy subject))
3513          ;; Just remove the leading "Re:".
3514          (t
3515           (gnus-simplify-subject-re subject))))
3516
3517   (if (and gnus-summary-gather-exclude-subject
3518            (string-match gnus-summary-gather-exclude-subject subject))
3519       nil                         ; This article shouldn't be gathered
3520     subject))
3521
3522 (defun gnus-summary-simplify-subject-query ()
3523   "Query where the respool algorithm would put this article."
3524   (interactive)
3525   (gnus-summary-select-article)
3526   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3527
3528 (defun gnus-gather-threads-by-subject (threads)
3529   "Gather threads by looking at Subject headers."
3530   (if (not gnus-summary-make-false-root)
3531       threads
3532     (let ((hashtb (gnus-make-hashtable 1024))
3533           (prev threads)
3534           (result threads)
3535           subject hthread whole-subject)
3536       (while threads
3537         (setq subject (gnus-general-simplify-subject
3538                        (setq whole-subject (mail-header-subject
3539                                             (caar threads)))))
3540         (when subject
3541           (if (setq hthread (gnus-gethash subject hashtb))
3542               (progn
3543                 ;; We enter a dummy root into the thread, if we
3544                 ;; haven't done that already.
3545                 (unless (stringp (caar hthread))
3546                   (setcar hthread (list whole-subject (car hthread))))
3547                 ;; We add this new gathered thread to this gathered
3548                 ;; thread.
3549                 (setcdr (car hthread)
3550                         (nconc (cdar hthread) (list (car threads))))
3551                 ;; Remove it from the list of threads.
3552                 (setcdr prev (cdr threads))
3553                 (setq threads prev))
3554             ;; Enter this thread into the hash table.
3555             (gnus-sethash subject threads hashtb)))
3556         (setq prev threads)
3557         (setq threads (cdr threads)))
3558       result)))
3559
3560 (defun gnus-gather-threads-by-references (threads)
3561   "Gather threads by looking at References headers."
3562   (let ((idhashtb (gnus-make-hashtable 1024))
3563         (thhashtb (gnus-make-hashtable 1024))
3564         (prev threads)
3565         (result threads)
3566         ids references id gthread gid entered ref)
3567     (while threads
3568       (when (setq references (mail-header-references (caar threads)))
3569         (setq id (mail-header-id (caar threads))
3570               ids (inline (gnus-split-references references))
3571               entered nil)
3572         (while (setq ref (pop ids))
3573           (setq ids (delete ref ids))
3574           (if (not (setq gid (gnus-gethash ref idhashtb)))
3575               (progn
3576                 (gnus-sethash ref id idhashtb)
3577                 (gnus-sethash id threads thhashtb))
3578             (setq gthread (gnus-gethash gid thhashtb))
3579             (unless entered
3580               ;; We enter a dummy root into the thread, if we
3581               ;; haven't done that already.
3582               (unless (stringp (caar gthread))
3583                 (setcar gthread (list (mail-header-subject (caar gthread))
3584                                       (car gthread))))
3585               ;; We add this new gathered thread to this gathered
3586               ;; thread.
3587               (setcdr (car gthread)
3588                       (nconc (cdar gthread) (list (car threads)))))
3589             ;; Add it into the thread hash table.
3590             (gnus-sethash id gthread thhashtb)
3591             (setq entered t)
3592             ;; Remove it from the list of threads.
3593             (setcdr prev (cdr threads))
3594             (setq threads prev))))
3595       (setq prev threads)
3596       (setq threads (cdr threads)))
3597     result))
3598
3599 (defun gnus-sort-gathered-threads (threads)
3600   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3601   (let ((result threads))
3602     (while threads
3603       (when (stringp (caar threads))
3604         (setcdr (car threads)
3605                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3606       (setq threads (cdr threads)))
3607     result))
3608
3609 (defun gnus-thread-loop-p (root thread)
3610   "Say whether ROOT is in THREAD."
3611   (let ((stack (list thread))
3612         (infloop 0)
3613         th)
3614     (while (setq thread (pop stack))
3615       (setq th (cdr thread))
3616       (while (and th
3617                   (not (eq (caar th) root)))
3618         (pop th))
3619       (if th
3620           ;; We have found a loop.
3621           (let (ref-dep)
3622             (setcdr thread (delq (car th) (cdr thread)))
3623             (if (boundp (setq ref-dep (intern "none"
3624                                               gnus-newsgroup-dependencies)))
3625                 (setcdr (symbol-value ref-dep)
3626                         (nconc (cdr (symbol-value ref-dep))
3627                                (list (car th))))
3628               (set ref-dep (list nil (car th))))
3629             (setq infloop 1
3630                   stack nil))
3631         ;; Push all the subthreads onto the stack.
3632         (push (cdr thread) stack)))
3633     infloop))
3634
3635 (defun gnus-make-threads ()
3636   "Go through the dependency hashtb and find the roots.  Return all threads."
3637   (let (threads)
3638     (while (catch 'infloop
3639              (mapatoms
3640               (lambda (refs)
3641                 ;; Deal with self-referencing References loops.
3642                 (when (and (car (symbol-value refs))
3643                            (not (zerop
3644                                  (apply
3645                                   '+
3646                                   (mapcar
3647                                    (lambda (thread)
3648                                      (gnus-thread-loop-p
3649                                       (car (symbol-value refs)) thread))
3650                                    (cdr (symbol-value refs)))))))
3651                   (setq threads nil)
3652                   (throw 'infloop t))
3653                 (unless (car (symbol-value refs))
3654                   ;; These threads do not refer back to any other
3655                   ;; articles, so they're roots.
3656                   (setq threads (append (cdr (symbol-value refs)) threads))))
3657               gnus-newsgroup-dependencies)))
3658     threads))
3659
3660 ;; Build the thread tree.
3661 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3662   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3663
3664 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3665 if it was already present.
3666
3667 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3668 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3669 Message-IDs will be renamed to a unique Message-ID before being
3670 entered.
3671
3672 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3673   (let* ((id (mail-header-id header))
3674          (id-dep (and id (intern id dependencies)))
3675          parent-id ref ref-dep ref-header replaced)
3676     ;; Enter this `header' in the `dependencies' table.
3677     (cond
3678      ((not id-dep)
3679       (setq header nil))
3680      ;; The first two cases do the normal part: enter a new `header'
3681      ;; in the `dependencies' table.
3682      ((not (boundp id-dep))
3683       (set id-dep (list header)))
3684      ((null (car (symbol-value id-dep)))
3685       (setcar (symbol-value id-dep) header))
3686
3687      ;; From here the `header' was already present in the
3688      ;; `dependencies' table.
3689      (force-new
3690       ;; Overrides an existing entry;
3691       ;; just set the header part of the entry.
3692       (setcar (symbol-value id-dep) header)
3693       (setq replaced t))
3694
3695      ;; Renames the existing `header' to a unique Message-ID.
3696      ((not gnus-summary-ignore-duplicates)
3697       ;; An article with this Message-ID has already been seen.
3698       ;; We rename the Message-ID.
3699       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3700            (list header))
3701       (mail-header-set-id header id))
3702
3703      ;; The last case ignores an existing entry, except it adds any
3704      ;; additional Xrefs (in case the two articles came from different
3705      ;; servers.
3706      ;; Also sets `header' to `nil' meaning that the `dependencies'
3707      ;; table was *not* modified.
3708      (t
3709       (mail-header-set-xref
3710        (car (symbol-value id-dep))
3711        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3712                    "")
3713                (or (mail-header-xref header) "")))
3714       (setq header nil)))
3715
3716     (when (and header (not replaced))
3717       ;; First check that we are not creating a References loop.
3718       (setq parent-id (gnus-parent-id (mail-header-references header)))
3719       (setq ref parent-id)
3720       (while (and ref
3721                   (setq ref-dep (intern-soft ref dependencies))
3722                   (boundp ref-dep)
3723                   (setq ref-header (car (symbol-value ref-dep))))
3724         (if (string= id ref)
3725             ;; Yuk!  This is a reference loop.  Make the article be a
3726             ;; root article.
3727             (progn
3728               (mail-header-set-references (car (symbol-value id-dep)) "none")
3729               (setq ref nil)
3730               (setq parent-id nil))
3731           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3732       (setq ref-dep (intern (or parent-id "none") dependencies))
3733       (if (boundp ref-dep)
3734           (setcdr (symbol-value ref-dep)
3735                   (nconc (cdr (symbol-value ref-dep))
3736                          (list (symbol-value id-dep))))
3737         (set ref-dep (list nil (symbol-value id-dep)))))
3738     header))
3739
3740 (defun gnus-extract-message-id-from-in-reply-to (string)
3741   (if (string-match "<[^>]+>" string)
3742       (substring string (match-beginning 0) (match-end 0))
3743     nil))
3744
3745 (defun gnus-build-sparse-threads ()
3746   (let ((headers gnus-newsgroup-headers)
3747         (mail-parse-charset gnus-newsgroup-charset)
3748         (gnus-summary-ignore-duplicates t)
3749         header references generation relations
3750         subject child end new-child date)
3751     ;; First we create an alist of generations/relations, where
3752     ;; generations is how much we trust the relation, and the relation
3753     ;; is parent/child.
3754     (gnus-message 7 "Making sparse threads...")
3755     (save-excursion
3756       (nnheader-set-temp-buffer " *gnus sparse threads*")
3757       (while (setq header (pop headers))
3758         (when (and (setq references (mail-header-references header))
3759                    (not (string= references "")))
3760           (insert references)
3761           (setq child (mail-header-id header)
3762                 subject (mail-header-subject header)
3763                 date (mail-header-date header)
3764                 generation 0)
3765           (while (search-backward ">" nil t)
3766             (setq end (1+ (point)))
3767             (when (search-backward "<" nil t)
3768               (setq new-child (buffer-substring (point) end))
3769               (push (list (incf generation)
3770                           child (setq child new-child)
3771                           subject date)
3772                     relations)))
3773           (when child
3774             (push (list (1+ generation) child nil subject) relations))
3775           (erase-buffer)))
3776       (kill-buffer (current-buffer)))
3777     ;; Sort over trustworthiness.
3778     (mapcar
3779      (lambda (relation)
3780        (when (gnus-dependencies-add-header
3781               (make-full-mail-header
3782                gnus-reffed-article-number
3783                (nth 3 relation) "" (or (nth 4 relation) "")
3784                (nth 1 relation)
3785                (or (nth 2 relation) "") 0 0 "")
3786               gnus-newsgroup-dependencies nil)
3787          (push gnus-reffed-article-number gnus-newsgroup-limit)
3788          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3789          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3790                gnus-newsgroup-reads)
3791          (decf gnus-reffed-article-number)))
3792      (sort relations 'car-less-than-car))
3793     (gnus-message 7 "Making sparse threads...done")))
3794
3795 (defun gnus-build-old-threads ()
3796   ;; Look at all the articles that refer back to old articles, and
3797   ;; fetch the headers for the articles that aren't there.  This will
3798   ;; build complete threads - if the roots haven't been expired by the
3799   ;; server, that is.
3800   (let ((mail-parse-charset gnus-newsgroup-charset)
3801         id heads)
3802     (mapatoms
3803      (lambda (refs)
3804        (when (not (car (symbol-value refs)))
3805          (setq heads (cdr (symbol-value refs)))
3806          (while heads
3807            (if (memq (mail-header-number (caar heads))
3808                      gnus-newsgroup-dormant)
3809                (setq heads (cdr heads))
3810              (setq id (symbol-name refs))
3811              (while (and (setq id (gnus-build-get-header id))
3812                          (not (car (gnus-id-to-thread id)))))
3813              (setq heads nil)))))
3814      gnus-newsgroup-dependencies)))
3815
3816 ;; This function has to be called with point after the article number
3817 ;; on the beginning of the line.
3818 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3819   (let ((eol (gnus-point-at-eol))
3820         (buffer (current-buffer))
3821         header references in-reply-to)
3822
3823     ;; overview: [num subject from date id refs chars lines misc]
3824     (unwind-protect
3825         (progn
3826           (narrow-to-region (point) eol)
3827           (unless (eobp)
3828             (forward-char))
3829
3830           (setq header
3831                 (make-full-mail-header
3832                  number                 ; number
3833                  (funcall gnus-decode-encoded-word-function
3834                           (nnheader-nov-field)) ; subject
3835                  (funcall gnus-decode-encoded-word-function
3836                           (nnheader-nov-field)) ; from
3837                  (nnheader-nov-field)   ; date
3838                  (nnheader-nov-read-message-id) ; id
3839                  (setq references (nnheader-nov-field)) ; refs
3840                  (nnheader-nov-read-integer) ; chars
3841                  (nnheader-nov-read-integer) ; lines
3842                  (unless (eobp)
3843                    (if (looking-at "Xref: ")
3844                        (goto-char (match-end 0)))
3845                    (nnheader-nov-field)) ; Xref
3846                  (nnheader-nov-parse-extra)))) ; extra
3847
3848       (widen))
3849
3850     (when (and (string= references "")
3851                (setq in-reply-to (mail-header-extra header))
3852                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3853       (mail-header-set-references
3854        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3855
3856     (when gnus-alter-header-function
3857       (funcall gnus-alter-header-function header))
3858     (gnus-dependencies-add-header header dependencies force-new)))
3859
3860 (defun gnus-build-get-header (id)
3861   "Look through the buffer of NOV lines and find the header to ID.
3862 Enter this line into the dependencies hash table, and return
3863 the id of the parent article (if any)."
3864   (let ((deps gnus-newsgroup-dependencies)
3865         found header)
3866     (prog1
3867         (save-excursion
3868           (set-buffer nntp-server-buffer)
3869           (let ((case-fold-search nil))
3870             (goto-char (point-min))
3871             (while (and (not found)
3872                         (search-forward id nil t))
3873               (beginning-of-line)
3874               (setq found (looking-at
3875                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3876                                    (regexp-quote id))))
3877               (or found (beginning-of-line 2)))
3878             (when found
3879               (beginning-of-line)
3880               (and
3881                (setq header (gnus-nov-parse-line
3882                              (read (current-buffer)) deps))
3883                (gnus-parent-id (mail-header-references header))))))
3884       (when header
3885         (let ((number (mail-header-number header)))
3886           (push number gnus-newsgroup-limit)
3887           (push header gnus-newsgroup-headers)
3888           (if (memq number gnus-newsgroup-unselected)
3889               (progn
3890                 (setq gnus-newsgroup-unreads
3891                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3892                                                number))
3893                 (setq gnus-newsgroup-unselected
3894                       (delq number gnus-newsgroup-unselected)))
3895             (push number gnus-newsgroup-ancient)))))))
3896
3897 (defun gnus-build-all-threads ()
3898   "Read all the headers."
3899   (let ((gnus-summary-ignore-duplicates t)
3900         (mail-parse-charset gnus-newsgroup-charset)
3901         (dependencies gnus-newsgroup-dependencies)
3902         header article)
3903     (save-excursion
3904       (set-buffer nntp-server-buffer)
3905       (let ((case-fold-search nil))
3906         (goto-char (point-min))
3907         (while (not (eobp))
3908           (ignore-errors
3909             (setq article (read (current-buffer))
3910                   header (gnus-nov-parse-line article dependencies)))
3911           (when header
3912             (save-excursion
3913               (set-buffer gnus-summary-buffer)
3914               (push header gnus-newsgroup-headers)
3915               (if (memq (setq article (mail-header-number header))
3916                         gnus-newsgroup-unselected)
3917                   (progn
3918                     (setq gnus-newsgroup-unreads
3919                           (gnus-add-to-sorted-list
3920                            gnus-newsgroup-unreads article))
3921                     (setq gnus-newsgroup-unselected
3922                           (delq article gnus-newsgroup-unselected)))
3923                 (push article gnus-newsgroup-ancient)))
3924             (forward-line 1)))))))
3925
3926 (defun gnus-summary-update-article-line (article header)
3927   "Update the line for ARTICLE using HEADERS."
3928   (let* ((id (mail-header-id header))
3929          (thread (gnus-id-to-thread id)))
3930     (unless thread
3931       (error "Article in no thread"))
3932     ;; Update the thread.
3933     (setcar thread header)
3934     (gnus-summary-goto-subject article)
3935     (let* ((datal (gnus-data-find-list article))
3936            (data (car datal))
3937            (length (when (cdr datal)
3938                      (- (gnus-data-pos data)
3939                         (gnus-data-pos (cadr datal)))))
3940            (buffer-read-only nil)
3941            (level (gnus-summary-thread-level)))
3942       (gnus-delete-line)
3943       (gnus-summary-insert-line
3944        header level nil (gnus-article-mark article)
3945        (memq article gnus-newsgroup-replied)
3946        (memq article gnus-newsgroup-expirable)
3947        ;; Only insert the Subject string when it's different
3948        ;; from the previous Subject string.
3949        (if (and
3950             gnus-show-threads
3951             (gnus-subject-equal
3952              (condition-case ()
3953                  (mail-header-subject
3954                   (gnus-data-header
3955                    (cadr
3956                     (gnus-data-find-list
3957                      article
3958                      (gnus-data-list t)))))
3959                ;; Error on the side of excessive subjects.
3960                (error ""))
3961              (mail-header-subject header)))
3962            ""
3963          (mail-header-subject header))
3964        nil (cdr (assq article gnus-newsgroup-scored))
3965        (memq article gnus-newsgroup-processable))
3966       (when length
3967         (gnus-data-update-list
3968          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3969
3970 (defun gnus-summary-update-article (article &optional iheader)
3971   "Update ARTICLE in the summary buffer."
3972   (set-buffer gnus-summary-buffer)
3973   (let* ((header (gnus-summary-article-header article))
3974          (id (mail-header-id header))
3975          (data (gnus-data-find article))
3976          (thread (gnus-id-to-thread id))
3977          (references (mail-header-references header))
3978          (parent
3979           (gnus-id-to-thread
3980            (or (gnus-parent-id
3981                 (when (and references
3982                            (not (equal "" references)))
3983                   references))
3984                "none")))
3985          (buffer-read-only nil)
3986          (old (car thread)))
3987     (when thread
3988       (unless iheader
3989         (setcar thread nil)
3990         (when parent
3991           (delq thread parent)))
3992       (if (gnus-summary-insert-subject id header)
3993        ;; Set the (possibly) new article number in the data structure.
3994           (gnus-data-set-number data (gnus-id-to-article id))
3995         (setcar thread old)
3996         nil))))
3997
3998 (defun gnus-rebuild-thread (id &optional line)
3999   "Rebuild the thread containing ID.
4000 If LINE, insert the rebuilt thread starting on line LINE."
4001   (let ((buffer-read-only nil)
4002         old-pos current thread data)
4003     (if (not gnus-show-threads)
4004         (setq thread (list (car (gnus-id-to-thread id))))
4005       ;; Get the thread this article is part of.
4006       (setq thread (gnus-remove-thread id)))
4007     (setq old-pos (gnus-point-at-bol))
4008     (setq current (save-excursion
4009                     (and (re-search-backward "[\r\n]" nil t)
4010                          (gnus-summary-article-number))))
4011     ;; If this is a gathered thread, we have to go some re-gathering.
4012     (when (stringp (car thread))
4013       (let ((subject (car thread))
4014             roots thr)
4015         (setq thread (cdr thread))
4016         (while thread
4017           (unless (memq (setq thr (gnus-id-to-thread
4018                                    (gnus-root-id
4019                                     (mail-header-id (caar thread)))))
4020                         roots)
4021             (push thr roots))
4022           (setq thread (cdr thread)))
4023         ;; We now have all (unique) roots.
4024         (if (= (length roots) 1)
4025             ;; All the loose roots are now one solid root.
4026             (setq thread (car roots))
4027           (setq thread (cons subject (gnus-sort-threads roots))))))
4028     (let (threads)
4029       ;; We then insert this thread into the summary buffer.
4030       (when line
4031         (goto-char (point-min))
4032         (forward-line (1- line)))
4033       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4034         (if gnus-show-threads
4035             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4036           (gnus-summary-prepare-unthreaded thread))
4037         (setq data (nreverse gnus-newsgroup-data))
4038         (setq threads gnus-newsgroup-threads))
4039       ;; We splice the new data into the data structure.
4040       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4041       ;;!!! then we want to insert at the beginning of the buffer.
4042       ;;!!! That happens to be true with Gnus now, but that may
4043       ;;!!! change in the future.  Perhaps.
4044       (gnus-data-enter-list
4045        (if line nil current) data (- (point) old-pos))
4046       (setq gnus-newsgroup-threads
4047             (nconc threads gnus-newsgroup-threads))
4048       (gnus-data-compute-positions))))
4049
4050 (defun gnus-number-to-header (number)
4051   "Return the header for article NUMBER."
4052   (let ((headers gnus-newsgroup-headers))
4053     (while (and headers
4054                 (not (= number (mail-header-number (car headers)))))
4055       (pop headers))
4056     (when headers
4057       (car headers))))
4058
4059 (defun gnus-parent-headers (in-headers &optional generation)
4060   "Return the headers of the GENERATIONeth parent of HEADERS."
4061   (unless generation
4062     (setq generation 1))
4063   (let ((parent t)
4064         (headers in-headers)
4065         references)
4066     (while (and parent
4067                 (not (zerop generation))
4068                 (setq references (mail-header-references headers)))
4069       (setq headers (if (and references
4070                              (setq parent (gnus-parent-id references)))
4071                         (car (gnus-id-to-thread parent))
4072                       nil))
4073       (decf generation))
4074     (and (not (eq headers in-headers))
4075          headers)))
4076
4077 (defun gnus-id-to-thread (id)
4078   "Return the (sub-)thread where ID appears."
4079   (gnus-gethash id gnus-newsgroup-dependencies))
4080
4081 (defun gnus-id-to-article (id)
4082   "Return the article number of ID."
4083   (let ((thread (gnus-id-to-thread id)))
4084     (when (and thread
4085                (car thread))
4086       (mail-header-number (car thread)))))
4087
4088 (defun gnus-id-to-header (id)
4089   "Return the article headers of ID."
4090   (car (gnus-id-to-thread id)))
4091
4092 (defun gnus-article-displayed-root-p (article)
4093   "Say whether ARTICLE is a root(ish) article."
4094   (let ((level (gnus-summary-thread-level article))
4095         (refs (mail-header-references  (gnus-summary-article-header article)))
4096         particle)
4097     (cond
4098      ((null level) nil)
4099      ((zerop level) t)
4100      ((null refs) t)
4101      ((null (gnus-parent-id refs)) t)
4102      ((and (= 1 level)
4103            (null (setq particle (gnus-id-to-article
4104                                  (gnus-parent-id refs))))
4105            (null (gnus-summary-thread-level particle)))))))
4106
4107 (defun gnus-root-id (id)
4108   "Return the id of the root of the thread where ID appears."
4109   (let (last-id prev)
4110     (while (and id (setq prev (car (gnus-id-to-thread id))))
4111       (setq last-id id
4112             id (gnus-parent-id (mail-header-references prev))))
4113     last-id))
4114
4115 (defun gnus-articles-in-thread (thread)
4116   "Return the list of articles in THREAD."
4117   (cons (mail-header-number (car thread))
4118         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4119
4120 (defun gnus-remove-thread (id &optional dont-remove)
4121   "Remove the thread that has ID in it."
4122   (let (headers thread last-id)
4123     ;; First go up in this thread until we find the root.
4124     (setq last-id (gnus-root-id id)
4125           headers (message-flatten-list (gnus-id-to-thread last-id)))
4126     ;; We have now found the real root of this thread.  It might have
4127     ;; been gathered into some loose thread, so we have to search
4128     ;; through the threads to find the thread we wanted.
4129     (let ((threads gnus-newsgroup-threads)
4130           sub)
4131       (while threads
4132         (setq sub (car threads))
4133         (if (stringp (car sub))
4134             ;; This is a gathered thread, so we look at the roots
4135             ;; below it to find whether this article is in this
4136             ;; gathered root.
4137             (progn
4138               (setq sub (cdr sub))
4139               (while sub
4140                 (when (member (caar sub) headers)
4141                   (setq thread (car threads)
4142                         threads nil
4143                         sub nil))
4144                 (setq sub (cdr sub))))
4145           ;; It's an ordinary thread, so we check it.
4146           (when (eq (car sub) (car headers))
4147             (setq thread sub
4148                   threads nil)))
4149         (setq threads (cdr threads)))
4150       ;; If this article is in no thread, then it's a root.
4151       (if thread
4152           (unless dont-remove
4153             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4154         (setq thread (gnus-id-to-thread last-id)))
4155       (when thread
4156         (prog1
4157             thread                      ; We return this thread.
4158           (unless dont-remove
4159             (if (stringp (car thread))
4160                 (progn
4161                   ;; If we use dummy roots, then we have to remove the
4162                   ;; dummy root as well.
4163                   (when (eq gnus-summary-make-false-root 'dummy)
4164                     ;; We go to the dummy root by going to
4165                     ;; the first sub-"thread", and then one line up.
4166                     (gnus-summary-goto-article
4167                      (mail-header-number (caadr thread)))
4168                     (forward-line -1)
4169                     (gnus-delete-line)
4170                     (gnus-data-compute-positions))
4171                   (setq thread (cdr thread))
4172                   (while thread
4173                     (gnus-remove-thread-1 (car thread))
4174                     (setq thread (cdr thread))))
4175               (gnus-remove-thread-1 thread))))))))
4176
4177 (defun gnus-remove-thread-1 (thread)
4178   "Remove the thread THREAD recursively."
4179   (let ((number (mail-header-number (pop thread)))
4180         d)
4181     (setq thread (reverse thread))
4182     (while thread
4183       (gnus-remove-thread-1 (pop thread)))
4184     (when (setq d (gnus-data-find number))
4185       (goto-char (gnus-data-pos d))
4186       (gnus-summary-show-thread)
4187       (gnus-data-remove
4188        number
4189        (- (gnus-point-at-bol)
4190           (prog1
4191               (1+ (gnus-point-at-eol))
4192             (gnus-delete-line)))))))
4193
4194 (defun gnus-sort-threads-1 (threads func)
4195   (sort (mapcar (lambda (thread)
4196                   (cons (car thread)
4197                         (and (cdr thread)
4198                              (gnus-sort-threads-1 (cdr thread) func))))
4199                 threads) func))
4200
4201 (defun gnus-sort-threads (threads)
4202   "Sort THREADS."
4203   (if (not gnus-thread-sort-functions)
4204       threads
4205     (gnus-message 8 "Sorting threads...")
4206     (prog1
4207         (gnus-sort-threads-1
4208          threads
4209          (gnus-make-sort-function gnus-thread-sort-functions))
4210       (gnus-message 8 "Sorting threads...done"))))
4211
4212 (defun gnus-sort-articles (articles)
4213   "Sort ARTICLES."
4214   (when gnus-article-sort-functions
4215     (gnus-message 7 "Sorting articles...")
4216     (prog1
4217         (setq gnus-newsgroup-headers
4218               (sort articles (gnus-make-sort-function
4219                               gnus-article-sort-functions)))
4220       (gnus-message 7 "Sorting articles...done"))))
4221
4222 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4223 (defmacro gnus-thread-header (thread)
4224   "Return header of first article in THREAD.
4225 Note that THREAD must never, ever be anything else than a variable -
4226 using some other form will lead to serious barfage."
4227   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4228   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4229   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4230         (vector thread) 2))
4231
4232 (defsubst gnus-article-sort-by-number (h1 h2)
4233   "Sort articles by article number."
4234   (< (mail-header-number h1)
4235      (mail-header-number h2)))
4236
4237 (defun gnus-thread-sort-by-number (h1 h2)
4238   "Sort threads by root article number."
4239   (gnus-article-sort-by-number
4240    (gnus-thread-header h1) (gnus-thread-header h2)))
4241
4242 (defsubst gnus-article-sort-by-random (h1 h2)
4243   "Sort articles by article number."
4244   (zerop (random 2)))
4245
4246 (defun gnus-thread-sort-by-random (h1 h2)
4247   "Sort threads by root article number."
4248   (gnus-article-sort-by-random
4249    (gnus-thread-header h1) (gnus-thread-header h2)))
4250
4251 (defsubst gnus-article-sort-by-lines (h1 h2)
4252   "Sort articles by article Lines header."
4253   (< (mail-header-lines h1)
4254      (mail-header-lines h2)))
4255
4256 (defun gnus-thread-sort-by-lines (h1 h2)
4257   "Sort threads by root article Lines header."
4258   (gnus-article-sort-by-lines
4259    (gnus-thread-header h1) (gnus-thread-header h2)))
4260
4261 (defsubst gnus-article-sort-by-chars (h1 h2)
4262   "Sort articles by octet length."
4263   (< (mail-header-chars h1)
4264      (mail-header-chars h2)))
4265
4266 (defun gnus-thread-sort-by-chars (h1 h2)
4267   "Sort threads by root article octet length."
4268   (gnus-article-sort-by-chars
4269    (gnus-thread-header h1) (gnus-thread-header h2)))
4270
4271 (defsubst gnus-article-sort-by-author (h1 h2)
4272   "Sort articles by root author."
4273   (string-lessp
4274    (let ((extract (funcall
4275                    gnus-extract-address-components
4276                    (mail-header-from h1))))
4277      (or (car extract) (cadr extract) ""))
4278    (let ((extract (funcall
4279                    gnus-extract-address-components
4280                    (mail-header-from h2))))
4281      (or (car extract) (cadr extract) ""))))
4282
4283 (defun gnus-thread-sort-by-author (h1 h2)
4284   "Sort threads by root author."
4285   (gnus-article-sort-by-author
4286    (gnus-thread-header h1)  (gnus-thread-header h2)))
4287
4288 (defsubst gnus-article-sort-by-subject (h1 h2)
4289   "Sort articles by root subject."
4290   (string-lessp
4291    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4292    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4293
4294 (defun gnus-thread-sort-by-subject (h1 h2)
4295   "Sort threads by root subject."
4296   (gnus-article-sort-by-subject
4297    (gnus-thread-header h1) (gnus-thread-header h2)))
4298
4299 (defsubst gnus-article-sort-by-date (h1 h2)
4300   "Sort articles by root article date."
4301   (time-less-p
4302    (gnus-date-get-time (mail-header-date h1))
4303    (gnus-date-get-time (mail-header-date h2))))
4304
4305 (defun gnus-thread-sort-by-date (h1 h2)
4306   "Sort threads by root article date."
4307   (gnus-article-sort-by-date
4308    (gnus-thread-header h1) (gnus-thread-header h2)))
4309
4310 (defsubst gnus-article-sort-by-score (h1 h2)
4311   "Sort articles by root article score.
4312 Unscored articles will be counted as having a score of zero."
4313   (> (or (cdr (assq (mail-header-number h1)
4314                     gnus-newsgroup-scored))
4315          gnus-summary-default-score 0)
4316      (or (cdr (assq (mail-header-number h2)
4317                     gnus-newsgroup-scored))
4318          gnus-summary-default-score 0)))
4319
4320 (defun gnus-thread-sort-by-score (h1 h2)
4321   "Sort threads by root article score."
4322   (gnus-article-sort-by-score
4323    (gnus-thread-header h1) (gnus-thread-header h2)))
4324
4325 (defun gnus-thread-sort-by-total-score (h1 h2)
4326   "Sort threads by the sum of all scores in the thread.
4327 Unscored articles will be counted as having a score of zero."
4328   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4329
4330 (defun gnus-thread-total-score (thread)
4331   ;; This function find the total score of THREAD.
4332   (cond
4333    ((null thread)
4334     0)
4335    ((consp thread)
4336     (if (stringp (car thread))
4337         (apply gnus-thread-score-function 0
4338                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4339       (gnus-thread-total-score-1 thread)))
4340    (t
4341     (gnus-thread-total-score-1 (list thread)))))
4342
4343 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4344   "Sort threads such that the thread with the most recently arrived article comes first."
4345   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4346
4347 (defun gnus-thread-highest-number (thread)
4348   "Return the highest article number in THREAD."
4349   (apply 'max (mapcar (lambda (header)
4350                         (mail-header-number header))
4351                       (message-flatten-list thread))))
4352
4353 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4354   "Sort threads such that the thread with the most recently dated article comes first."
4355   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4356
4357 (defun gnus-thread-latest-date (thread)
4358   "Return the highest article date in THREAD."
4359   (let ((previous-time 0))
4360     (apply 'max (mapcar
4361                  (lambda (header)
4362                    (setq previous-time
4363                          (time-to-seconds
4364                           (mail-header-parse-date
4365                            (condition-case ()
4366                                (mail-header-date header)
4367                              (error previous-time))))))
4368                  (sort
4369                   (message-flatten-list thread)
4370                   (lambda (h1 h2)
4371                     (< (mail-header-number h1)
4372                        (mail-header-number h2))))))))
4373
4374 (defun gnus-thread-total-score-1 (root)
4375   ;; This function find the total score of the thread below ROOT.
4376   (setq root (car root))
4377   (apply gnus-thread-score-function
4378          (or (append
4379               (mapcar 'gnus-thread-total-score
4380                       (cdr (gnus-id-to-thread (mail-header-id root))))
4381               (when (> (mail-header-number root) 0)
4382                 (list (or (cdr (assq (mail-header-number root)
4383                                      gnus-newsgroup-scored))
4384                           gnus-summary-default-score 0))))
4385              (list gnus-summary-default-score)
4386              '(0))))
4387
4388 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4389 (defvar gnus-tmp-prev-subject nil)
4390 (defvar gnus-tmp-false-parent nil)
4391 (defvar gnus-tmp-root-expunged nil)
4392 (defvar gnus-tmp-dummy-line nil)
4393
4394 (eval-when-compile (defvar gnus-tmp-header))
4395 (defun gnus-extra-header (type &optional header)
4396   "Return the extra header of TYPE."
4397   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4398       ""))
4399
4400 (defvar gnus-tmp-thread-tree-header-string "")
4401
4402 (defcustom gnus-sum-thread-tree-root "> "
4403   "With %B spec, used for the root of a thread.
4404 If nil, use subject instead."
4405   :type 'string
4406   :group 'gnus-thread)
4407 (defcustom gnus-sum-thread-tree-single-indent ""
4408   "With %B spec, used for a thread with just one message.
4409 If nil, use subject instead."
4410   :type 'string
4411   :group 'gnus-thread)
4412 (defcustom gnus-sum-thread-tree-vertical "| "
4413   "With %B spec, used for drawing a vertical line."
4414   :type 'string
4415   :group 'gnus-thread)
4416 (defcustom gnus-sum-thread-tree-indent "  "
4417   "With %B spec, used for indenting."
4418   :type 'string
4419   :group 'gnus-thread)
4420 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4421   "With %B spec, used for a leaf with brothers."
4422   :type 'string
4423   :group 'gnus-thread)
4424 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4425   "With %B spec, used for a leaf without brothers."
4426   :type 'string
4427   :group 'gnus-thread)
4428
4429 (defun gnus-summary-prepare-threads (threads)
4430   "Prepare summary buffer from THREADS and indentation LEVEL.
4431 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4432 or a straight list of headers."
4433   (gnus-message 7 "Generating summary...")
4434
4435   (setq gnus-newsgroup-threads threads)
4436   (beginning-of-line)
4437
4438   (let ((gnus-tmp-level 0)
4439         (default-score (or gnus-summary-default-score 0))
4440         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4441         thread number subject stack state gnus-tmp-gathered beg-match
4442         new-roots gnus-tmp-new-adopts thread-end simp-subject
4443         gnus-tmp-header gnus-tmp-unread
4444         gnus-tmp-replied gnus-tmp-subject-or-nil
4445         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4446         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4447         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4448         tree-stack)
4449
4450     (setq gnus-tmp-prev-subject nil)
4451
4452     (if (vectorp (car threads))
4453         ;; If this is a straight (sic) list of headers, then a
4454         ;; threaded summary display isn't required, so we just create
4455         ;; an unthreaded one.
4456         (gnus-summary-prepare-unthreaded threads)
4457
4458       ;; Do the threaded display.
4459
4460       (while (or threads stack gnus-tmp-new-adopts new-roots)
4461
4462         (if (and (= gnus-tmp-level 0)
4463                  (or (not stack)
4464                      (= (caar stack) 0))
4465                  (not gnus-tmp-false-parent)
4466                  (or gnus-tmp-new-adopts new-roots))
4467             (if gnus-tmp-new-adopts
4468                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4469                       thread (list (car gnus-tmp-new-adopts))
4470                       gnus-tmp-header (caar thread)
4471                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4472               (when new-roots
4473                 (setq thread (list (car new-roots))
4474                       gnus-tmp-header (caar thread)
4475                       new-roots (cdr new-roots))))
4476
4477           (if threads
4478               ;; If there are some threads, we do them before the
4479               ;; threads on the stack.
4480               (setq thread threads
4481                     gnus-tmp-header (caar thread))
4482             ;; There were no current threads, so we pop something off
4483             ;; the stack.
4484             (setq state (car stack)
4485                   gnus-tmp-level (car state)
4486                   tree-stack (cadr state)
4487                   thread (caddr state)
4488                   stack (cdr stack)
4489                   gnus-tmp-header (caar thread))))
4490
4491         (setq gnus-tmp-false-parent nil)
4492         (setq gnus-tmp-root-expunged nil)
4493         (setq thread-end nil)
4494
4495         (if (stringp gnus-tmp-header)
4496             ;; The header is a dummy root.
4497             (cond
4498              ((eq gnus-summary-make-false-root 'adopt)
4499               ;; We let the first article adopt the rest.
4500               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4501                                                (cddar thread)))
4502               (setq gnus-tmp-gathered
4503                     (nconc (mapcar
4504                             (lambda (h) (mail-header-number (car h)))
4505                             (cddar thread))
4506                            gnus-tmp-gathered))
4507               (setq thread (cons (list (caar thread)
4508                                        (cadar thread))
4509                                  (cdr thread)))
4510               (setq gnus-tmp-level -1
4511                     gnus-tmp-false-parent t))
4512              ((eq gnus-summary-make-false-root 'empty)
4513               ;; We print adopted articles with empty subject fields.
4514               (setq gnus-tmp-gathered
4515                     (nconc (mapcar
4516                             (lambda (h) (mail-header-number (car h)))
4517                             (cddar thread))
4518                            gnus-tmp-gathered))
4519               (setq gnus-tmp-level -1))
4520              ((eq gnus-summary-make-false-root 'dummy)
4521               ;; We remember that we probably want to output a dummy
4522               ;; root.
4523               (setq gnus-tmp-dummy-line gnus-tmp-header)
4524               (setq gnus-tmp-prev-subject gnus-tmp-header))
4525              (t
4526               ;; We do not make a root for the gathered
4527               ;; sub-threads at all.
4528               (setq gnus-tmp-level -1)))
4529
4530           (setq number (mail-header-number gnus-tmp-header)
4531                 subject (mail-header-subject gnus-tmp-header)
4532                 simp-subject (gnus-simplify-subject-fully subject))
4533
4534           (cond
4535            ;; If the thread has changed subject, we might want to make
4536            ;; this subthread into a root.
4537            ((and (null gnus-thread-ignore-subject)
4538                  (not (zerop gnus-tmp-level))
4539                  gnus-tmp-prev-subject
4540                  (not (string= gnus-tmp-prev-subject simp-subject)))
4541             (setq new-roots (nconc new-roots (list (car thread)))
4542                   thread-end t
4543                   gnus-tmp-header nil))
4544            ;; If the article lies outside the current limit,
4545            ;; then we do not display it.
4546            ((not (memq number gnus-newsgroup-limit))
4547             (setq gnus-tmp-gathered
4548                   (nconc (mapcar
4549                           (lambda (h) (mail-header-number (car h)))
4550                           (cdar thread))
4551                          gnus-tmp-gathered))
4552             (setq gnus-tmp-new-adopts (if (cdar thread)
4553                                           (append gnus-tmp-new-adopts
4554                                                   (cdar thread))
4555                                         gnus-tmp-new-adopts)
4556                   thread-end t
4557                   gnus-tmp-header nil)
4558             (when (zerop gnus-tmp-level)
4559               (setq gnus-tmp-root-expunged t)))
4560            ;; Perhaps this article is to be marked as read?
4561            ((and gnus-summary-mark-below
4562                  (< (or (cdr (assq number gnus-newsgroup-scored))
4563                         default-score)
4564                     gnus-summary-mark-below)
4565                  ;; Don't touch sparse articles.
4566                  (not (gnus-summary-article-sparse-p number))
4567                  (not (gnus-summary-article-ancient-p number)))
4568             (setq gnus-newsgroup-unreads
4569                   (delq number gnus-newsgroup-unreads))
4570             (if gnus-newsgroup-auto-expire
4571                 (setq gnus-newsgroup-expirable
4572                       (gnus-add-to-sorted-list
4573                        gnus-newsgroup-expirable number))
4574               (push (cons number gnus-low-score-mark)
4575                     gnus-newsgroup-reads))))
4576
4577           (when gnus-tmp-header
4578             ;; We may have an old dummy line to output before this
4579             ;; article.
4580             (when (and gnus-tmp-dummy-line
4581                        (gnus-subject-equal
4582                         gnus-tmp-dummy-line
4583                         (mail-header-subject gnus-tmp-header)))
4584               (gnus-summary-insert-dummy-line
4585                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4586               (setq gnus-tmp-dummy-line nil))
4587
4588             ;; Compute the mark.
4589             (setq gnus-tmp-unread (gnus-article-mark number))
4590
4591             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4592                                   gnus-tmp-header gnus-tmp-level)
4593                   gnus-newsgroup-data)
4594
4595             ;; Actually insert the line.
4596             (setq
4597              gnus-tmp-subject-or-nil
4598              (cond
4599               ((and gnus-thread-ignore-subject
4600                     gnus-tmp-prev-subject
4601                     (not (string= gnus-tmp-prev-subject simp-subject)))
4602                subject)
4603               ((zerop gnus-tmp-level)
4604                (if (and (eq gnus-summary-make-false-root 'empty)
4605                         (memq number gnus-tmp-gathered)
4606                         gnus-tmp-prev-subject
4607                         (string= gnus-tmp-prev-subject simp-subject))
4608                    gnus-summary-same-subject
4609                  subject))
4610               (t gnus-summary-same-subject)))
4611             (if (and (eq gnus-summary-make-false-root 'adopt)
4612                      (= gnus-tmp-level 1)
4613                      (memq number gnus-tmp-gathered))
4614                 (setq gnus-tmp-opening-bracket ?\<
4615                       gnus-tmp-closing-bracket ?\>)
4616               (setq gnus-tmp-opening-bracket ?\[
4617                     gnus-tmp-closing-bracket ?\]))
4618             (setq
4619              gnus-tmp-indentation
4620              (aref gnus-thread-indent-array gnus-tmp-level)
4621              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4622              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4623                                 gnus-summary-default-score 0)
4624              gnus-tmp-score-char
4625              (if (or (null gnus-summary-default-score)
4626                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4627                          gnus-summary-zcore-fuzz))
4628                  ?                      ;Whitespace
4629                (if (< gnus-tmp-score gnus-summary-default-score)
4630                    gnus-score-below-mark gnus-score-over-mark))
4631              gnus-tmp-replied
4632              (cond ((memq number gnus-newsgroup-processable)
4633                     gnus-process-mark)
4634                    ((memq number gnus-newsgroup-cached)
4635                     gnus-cached-mark)
4636                    ((memq number gnus-newsgroup-replied)
4637                     gnus-replied-mark)
4638                    ((memq number gnus-newsgroup-forwarded)
4639                     gnus-forwarded-mark)
4640                    ((memq number gnus-newsgroup-saved)
4641                     gnus-saved-mark)
4642                    ((memq number gnus-newsgroup-recent)
4643                     gnus-recent-mark)
4644                    ((memq number gnus-newsgroup-unseen)
4645                     gnus-unseen-mark)
4646                    (t gnus-no-mark))
4647              gnus-tmp-from (mail-header-from gnus-tmp-header)
4648              gnus-tmp-name
4649              (cond
4650               ((string-match "<[^>]+> *$" gnus-tmp-from)
4651                (setq beg-match (match-beginning 0))
4652                (or (and (string-match "^\".+\"" gnus-tmp-from)
4653                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4654                    (substring gnus-tmp-from 0 beg-match)))
4655               ((string-match "(.+)" gnus-tmp-from)
4656                (substring gnus-tmp-from
4657                           (1+ (match-beginning 0)) (1- (match-end 0))))
4658               (t gnus-tmp-from))
4659              gnus-tmp-thread-tree-header-string
4660              (cond
4661               ((not gnus-show-threads) "")
4662               ((zerop gnus-tmp-level)
4663                (if (cdar thread)
4664                    (or gnus-sum-thread-tree-root subject)
4665                  (or gnus-sum-thread-tree-single-indent subject)))
4666               (t
4667                (concat (apply 'concat
4668                               (mapcar (lambda (item)
4669                                         (if (= item 1)
4670                                             gnus-sum-thread-tree-vertical
4671                                           gnus-sum-thread-tree-indent))
4672                                       (cdr (reverse tree-stack))))
4673                        (if (nth 1 thread)
4674                            gnus-sum-thread-tree-leaf-with-other
4675                          gnus-sum-thread-tree-single-leaf)))))
4676             (when (string= gnus-tmp-name "")
4677               (setq gnus-tmp-name gnus-tmp-from))
4678             (unless (numberp gnus-tmp-lines)
4679               (setq gnus-tmp-lines -1))
4680             (if (= gnus-tmp-lines -1)
4681                 (setq gnus-tmp-lines "?")
4682               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4683             (gnus-put-text-property
4684              (point)
4685              (progn (eval gnus-summary-line-format-spec) (point))
4686              'gnus-number number)
4687             (when gnus-visual-p
4688               (forward-line -1)
4689               (gnus-run-hooks 'gnus-summary-update-hook)
4690               (forward-line 1))
4691
4692             (setq gnus-tmp-prev-subject simp-subject)))
4693
4694         (when (nth 1 thread)
4695           (push (list (max 0 gnus-tmp-level)
4696                       (copy-list tree-stack)
4697                       (nthcdr 1 thread))
4698                 stack))
4699         (push (if (nth 1 thread) 1 0) tree-stack)
4700         (incf gnus-tmp-level)
4701         (setq threads (if thread-end nil (cdar thread)))
4702         (unless threads
4703           (setq gnus-tmp-level 0)))))
4704   (gnus-message 7 "Generating summary...done"))
4705
4706 (defun gnus-summary-prepare-unthreaded (headers)
4707   "Generate an unthreaded summary buffer based on HEADERS."
4708   (let (header number mark)
4709
4710     (beginning-of-line)
4711
4712     (while headers
4713       ;; We may have to root out some bad articles...
4714       (when (memq (setq number (mail-header-number
4715                                 (setq header (pop headers))))
4716                   gnus-newsgroup-limit)
4717         ;; Mark article as read when it has a low score.
4718         (when (and gnus-summary-mark-below
4719                    (< (or (cdr (assq number gnus-newsgroup-scored))
4720                           gnus-summary-default-score 0)
4721                       gnus-summary-mark-below)
4722                    (not (gnus-summary-article-ancient-p number)))
4723           (setq gnus-newsgroup-unreads
4724                 (delq number gnus-newsgroup-unreads))
4725           (if gnus-newsgroup-auto-expire
4726               (push number gnus-newsgroup-expirable)
4727             (push (cons number gnus-low-score-mark)
4728                   gnus-newsgroup-reads)))
4729
4730         (setq mark (gnus-article-mark number))
4731         (push (gnus-data-make number mark (1+ (point)) header 0)
4732               gnus-newsgroup-data)
4733         (gnus-summary-insert-line
4734          header 0 number
4735          mark (memq number gnus-newsgroup-replied)
4736          (memq number gnus-newsgroup-expirable)
4737          (mail-header-subject header) nil
4738          (cdr (assq number gnus-newsgroup-scored))
4739          (memq number gnus-newsgroup-processable))))))
4740
4741 (defun gnus-summary-remove-list-identifiers ()
4742   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4743   (let ((regexp (if (consp gnus-list-identifiers)
4744                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4745                   gnus-list-identifiers))
4746         changed subject)
4747     (when regexp
4748       (dolist (header gnus-newsgroup-headers)
4749         (setq subject (mail-header-subject header)
4750               changed nil)
4751         (while (string-match
4752                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4753                 subject)
4754           (setq subject
4755                 (concat (substring subject 0 (match-beginning 2))
4756                         (substring subject (match-end 0)))
4757                 changed t))
4758         (when (and changed
4759                    (string-match
4760                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4761           (setq subject
4762                 (concat (substring subject 0 (match-beginning 1))
4763                         (substring subject (match-end 1)))))
4764         (when changed
4765           (mail-header-set-subject header subject))))))
4766
4767 (defun gnus-fetch-headers (articles)
4768   "Fetch headers of ARTICLES."
4769   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4770     (gnus-message 5 "Fetching headers for %s..." name)
4771     (prog1
4772         (if (eq 'nov
4773                 (setq gnus-headers-retrieved-by
4774                       (gnus-retrieve-headers
4775                        articles gnus-newsgroup-name
4776                        ;; We might want to fetch old headers, but
4777                        ;; not if there is only 1 article.
4778                        (and (or (and
4779                                  (not (eq gnus-fetch-old-headers 'some))
4780                                  (not (numberp gnus-fetch-old-headers)))
4781                                 (> (length articles) 1))
4782                             gnus-fetch-old-headers))))
4783             (gnus-get-newsgroup-headers-xover
4784              articles nil nil gnus-newsgroup-name t)
4785           (gnus-get-newsgroup-headers))
4786       (gnus-message 5 "Fetching headers for %s...done" name))))
4787
4788 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4789   "Select newsgroup GROUP.
4790 If READ-ALL is non-nil, all articles in the group are selected.
4791 If SELECT-ARTICLES, only select those articles from GROUP."
4792   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4793          ;;!!! Dirty hack; should be removed.
4794          (gnus-summary-ignore-duplicates
4795           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4796               t
4797             gnus-summary-ignore-duplicates))
4798          (info (nth 2 entry))
4799          articles fetched-articles cached)
4800
4801     (unless (gnus-check-server
4802              (set (make-local-variable 'gnus-current-select-method)
4803                   (gnus-find-method-for-group group)))
4804       (error "Couldn't open server"))
4805
4806     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4807         (gnus-activate-group group)     ; Or we can activate it...
4808         (progn                          ; Or we bug out.
4809           (when (equal major-mode 'gnus-summary-mode)
4810             (kill-buffer (current-buffer)))
4811           (error "Couldn't activate group %s: %s"
4812                  group (gnus-status-message group))))
4813
4814     (unless (gnus-request-group group t)
4815       (when (equal major-mode 'gnus-summary-mode)
4816         (kill-buffer (current-buffer)))
4817       (error "Couldn't request group %s: %s"
4818              group (gnus-status-message group)))
4819
4820     (setq gnus-newsgroup-name group
4821           gnus-newsgroup-unselected nil
4822           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4823
4824     (let ((display (gnus-group-find-parameter group 'display)))
4825       (setq gnus-newsgroup-display
4826             (cond
4827              ((not (zerop (or (car-safe read-all) 0)))
4828               ;; The user entered the group with C-u SPC/RET, let's show
4829               ;; all articles.
4830               'gnus-not-ignore)
4831              ((eq display 'all)
4832               'gnus-not-ignore)
4833              ((arrayp display)
4834               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4835              ((numberp display)
4836               ;; The following is probably the "correct" solution, but
4837               ;; it makes Gnus fetch all headers and then limit the
4838               ;; articles (which is slow), so instead we hack the
4839               ;; select-articles parameter instead. -- Simon Josefsson
4840               ;; <jas@kth.se>
4841               ;;
4842               ;; (gnus-byte-compile
4843               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4844               ;;                         display)))))
4845               (setq select-articles
4846                     (gnus-uncompress-range
4847                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4848                              (if (> tmp 0)
4849                                  tmp
4850                                1))
4851                            (cdr (gnus-active group)))))
4852               nil)
4853              (t
4854               nil))))
4855
4856     (gnus-summary-setup-default-charset)
4857
4858     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4859     (when (gnus-virtual-group-p group)
4860       (setq cached gnus-newsgroup-cached))
4861
4862     (setq gnus-newsgroup-unreads
4863           (gnus-sorted-ndifference
4864            (gnus-sorted-ndifference gnus-newsgroup-unreads
4865                                     gnus-newsgroup-marked)
4866            gnus-newsgroup-dormant))
4867
4868     (setq gnus-newsgroup-processable nil)
4869
4870     (gnus-update-read-articles group gnus-newsgroup-unreads)
4871
4872     ;; Adjust and set lists of article marks.
4873     (when info
4874       (gnus-adjust-marked-articles info))
4875
4876     (if (setq articles select-articles)
4877         (setq gnus-newsgroup-unselected
4878               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4879       (setq articles (gnus-articles-to-read group read-all)))
4880
4881     (cond
4882      ((null articles)
4883       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4884       'quit)
4885      ((eq articles 0) nil)
4886      (t
4887       ;; Init the dependencies hash table.
4888       (setq gnus-newsgroup-dependencies
4889             (gnus-make-hashtable (length articles)))
4890       (gnus-set-global-variables)
4891       ;; Retrieve the headers and read them in.
4892       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4893
4894       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4895       (when cached
4896         (setq gnus-newsgroup-cached cached))
4897
4898       ;; Suppress duplicates?
4899       (when gnus-suppress-duplicates
4900         (gnus-dup-suppress-articles))
4901
4902       ;; Set the initial limit.
4903       (setq gnus-newsgroup-limit (copy-sequence articles))
4904       ;; Remove canceled articles from the list of unread articles.
4905       (setq fetched-articles
4906             (mapcar (lambda (headers) (mail-header-number headers))
4907                     gnus-newsgroup-headers))
4908       (setq gnus-newsgroup-articles fetched-articles)
4909       (setq gnus-newsgroup-unreads
4910             (gnus-sorted-nintersection
4911              gnus-newsgroup-unreads fetched-articles))
4912       (gnus-compute-unseen-list)
4913
4914       ;; Removed marked articles that do not exist.
4915       (gnus-update-missing-marks
4916        (gnus-sorted-difference articles fetched-articles))
4917       ;; We might want to build some more threads first.
4918       (when (and gnus-fetch-old-headers
4919                  (eq gnus-headers-retrieved-by 'nov))
4920         (if (eq gnus-fetch-old-headers 'invisible)
4921             (gnus-build-all-threads)
4922           (gnus-build-old-threads)))
4923       ;; Let the Gnus agent mark articles as read.
4924       (when gnus-agent
4925         (gnus-agent-get-undownloaded-list))
4926       ;; Remove list identifiers from subject
4927       (when gnus-list-identifiers
4928         (gnus-summary-remove-list-identifiers))
4929       ;; Check whether auto-expire is to be done in this group.
4930       (setq gnus-newsgroup-auto-expire
4931             (gnus-group-auto-expirable-p group))
4932       ;; Set up the article buffer now, if necessary.
4933       (unless gnus-single-article-buffer
4934         (gnus-article-setup-buffer))
4935       ;; First and last article in this newsgroup.
4936       (when gnus-newsgroup-headers
4937         (setq gnus-newsgroup-begin
4938               (mail-header-number (car gnus-newsgroup-headers))
4939               gnus-newsgroup-end
4940               (mail-header-number
4941                (gnus-last-element gnus-newsgroup-headers))))
4942       ;; GROUP is successfully selected.
4943       (or gnus-newsgroup-headers t)))))
4944
4945 (defun gnus-compute-unseen-list ()
4946   ;; The `seen' marks are treated specially.
4947   (if (not gnus-newsgroup-seen)
4948       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4949     (setq gnus-newsgroup-unseen
4950           (gnus-inverse-list-range-intersection
4951            gnus-newsgroup-articles gnus-newsgroup-seen))))
4952
4953 (defun gnus-summary-display-make-predicate (display)
4954   (require 'gnus-agent)
4955   (when (= (length display) 1)
4956     (setq display (car display)))
4957   (unless gnus-summary-display-cache
4958     (dolist (elem (append '((unread . unread)
4959                             (read . read)
4960                             (unseen . unseen))
4961                           gnus-article-mark-lists))
4962       (push (cons (cdr elem)
4963                   (gnus-byte-compile
4964                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4965             gnus-summary-display-cache)))
4966   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
4967         (gnus-category-predicate-cache gnus-summary-display-cache))
4968     (gnus-get-predicate display)))
4969
4970 ;; Uses the dynamically bound `number' variable.
4971 (defvar number)
4972 (defun gnus-article-marked-p (type &optional article)
4973   (let ((article (or article number)))
4974     (cond
4975      ((eq type 'tick)
4976       (memq article gnus-newsgroup-marked))
4977      ((eq type 'spam)
4978       (memq article gnus-newsgroup-spam-marked))
4979      ((eq type 'unsend)
4980       (memq article gnus-newsgroup-unsendable))
4981      ((eq type 'undownload)
4982       (memq article gnus-newsgroup-undownloaded))
4983      ((eq type 'download)
4984       (memq article gnus-newsgroup-downloadable))
4985      ((eq type 'unread)
4986       (memq article gnus-newsgroup-unreads))
4987      ((eq type 'read)
4988       (memq article gnus-newsgroup-reads))
4989      ((eq type 'dormant)
4990       (memq article gnus-newsgroup-dormant) )
4991      ((eq type 'expire)
4992       (memq article gnus-newsgroup-expirable))
4993      ((eq type 'reply)
4994       (memq article gnus-newsgroup-replied))
4995      ((eq type 'killed)
4996       (memq article gnus-newsgroup-killed))
4997      ((eq type 'bookmark)
4998       (assq article gnus-newsgroup-bookmarks))
4999      ((eq type 'score)
5000       (assq article gnus-newsgroup-scored))
5001      ((eq type 'save)
5002       (memq article gnus-newsgroup-saved))
5003      ((eq type 'cache)
5004       (memq article gnus-newsgroup-cached))
5005      ((eq type 'forward)
5006       (memq article gnus-newsgroup-forwarded))
5007      ((eq type 'seen)
5008       (not (memq article gnus-newsgroup-unseen)))
5009      ((eq type 'recent)
5010       (memq article gnus-newsgroup-recent))
5011      (t t))))
5012
5013 (defun gnus-articles-to-read (group &optional read-all)
5014   "Find out what articles the user wants to read."
5015   (let* ((articles
5016           ;; Select all articles if `read-all' is non-nil, or if there
5017           ;; are no unread articles.
5018           (if (or read-all
5019                   (and (zerop (length gnus-newsgroup-marked))
5020                        (zerop (length gnus-newsgroup-unreads)))
5021                   ;; Fetch all if the predicate is non-nil.
5022                   gnus-newsgroup-display)
5023               ;; We want to select the headers for all the articles in
5024               ;; the group, so we select either all the active
5025               ;; articles in the group, or (if that's nil), the
5026               ;; articles in the cache.
5027               (or
5028                (gnus-uncompress-range (gnus-active group))
5029                (gnus-cache-articles-in-group group))
5030             ;; Select only the "normal" subset of articles.
5031             (gnus-sorted-nunion
5032              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5033              gnus-newsgroup-unreads)))
5034          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5035          (scored (length scored-list))
5036          (number (length articles))
5037          (marked (+ (length gnus-newsgroup-marked)
5038                     (length gnus-newsgroup-dormant)))
5039          (select
5040           (cond
5041            ((numberp read-all)
5042             read-all)
5043            ((numberp gnus-newsgroup-display)
5044             gnus-newsgroup-display)
5045            (t
5046             (condition-case ()
5047                 (cond
5048                  ((and (or (<= scored marked) (= scored number))
5049                        (numberp gnus-large-newsgroup)
5050                        (> number gnus-large-newsgroup))
5051                   (let* ((cursor-in-echo-area nil)
5052                          (initial (gnus-parameter-large-newsgroup-initial
5053                                    gnus-newsgroup-name))
5054                          (input
5055                           (read-string
5056                            (format
5057                             "How many articles from %s (%s %d): "
5058                             (gnus-limit-string
5059                              (gnus-group-decoded-name gnus-newsgroup-name)
5060                              35)
5061                             (if initial "max" "default")
5062                             number)
5063                            (if initial
5064                                (cons (number-to-string initial)
5065                                      0)))))
5066                     (if (string-match "^[ \t]*$" input) number input)))
5067                  ((and (> scored marked) (< scored number)
5068                        (> (- scored number) 20))
5069                   (let ((input
5070                          (read-string
5071                           (format "%s %s (%d scored, %d total): "
5072                                   "How many articles from"
5073                                   (gnus-group-decoded-name group)
5074                                   scored number))))
5075                     (if (string-match "^[ \t]*$" input)
5076                         number input)))
5077                  (t number))
5078               (quit
5079                (message "Quit getting the articles to read")
5080                nil))))))
5081     (setq select (if (stringp select) (string-to-number select) select))
5082     (if (or (null select) (zerop select))
5083         select
5084       (if (and (not (zerop scored)) (<= (abs select) scored))
5085           (progn
5086             (setq articles (sort scored-list '<))
5087             (setq number (length articles)))
5088         (setq articles (copy-sequence articles)))
5089
5090       (when (< (abs select) number)
5091         (if (< select 0)
5092             ;; Select the N oldest articles.
5093             (setcdr (nthcdr (1- (abs select)) articles) nil)
5094           ;; Select the N most recent articles.
5095           (setq articles (nthcdr (- number select) articles))))
5096       (setq gnus-newsgroup-unselected
5097             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5098       (when gnus-alter-articles-to-read-function
5099         (setq gnus-newsgroup-unreads
5100               (sort
5101                (funcall gnus-alter-articles-to-read-function
5102                         gnus-newsgroup-name gnus-newsgroup-unreads)
5103                '<)))
5104       articles)))
5105
5106 (defun gnus-killed-articles (killed articles)
5107   (let (out)
5108     (while articles
5109       (when (inline (gnus-member-of-range (car articles) killed))
5110         (push (car articles) out))
5111       (setq articles (cdr articles)))
5112     out))
5113
5114 (defun gnus-uncompress-marks (marks)
5115   "Uncompress the mark ranges in MARKS."
5116   (let ((uncompressed '(score bookmark))
5117         out)
5118     (while marks
5119       (if (memq (caar marks) uncompressed)
5120           (push (car marks) out)
5121         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5122       (setq marks (cdr marks)))
5123     out))
5124
5125 (defun gnus-article-mark-to-type (mark)
5126   "Return the type of MARK."
5127   (or (cadr (assq mark gnus-article-special-mark-lists))
5128       'list))
5129
5130 (defun gnus-article-unpropagatable-p (mark)
5131   "Return whether MARK should be propagated to backend."
5132   (memq mark gnus-article-unpropagated-mark-lists))
5133
5134 (defun gnus-adjust-marked-articles (info)
5135   "Set all article lists and remove all marks that are no longer valid."
5136   (let* ((marked-lists (gnus-info-marks info))
5137          (active (gnus-active (gnus-info-group info)))
5138          (min (car active))
5139          (max (cdr active))
5140          (types gnus-article-mark-lists)
5141          marks var articles article mark mark-type)
5142
5143     (dolist (marks marked-lists)
5144       (setq mark (car marks)
5145             mark-type (gnus-article-mark-to-type mark)
5146             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5147
5148       ;; We set the variable according to the type of the marks list,
5149       ;; and then adjust the marks to a subset of the active articles.
5150       (cond
5151        ;; Adjust "simple" lists.
5152        ((eq mark-type 'list)
5153         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5154         (when (memq mark '(tick dormant expire reply save))
5155           (while articles
5156             (when (or (< (setq article (pop articles)) min) (> article max))
5157               (set var (delq article (symbol-value var)))))))
5158        ;; Adjust assocs.
5159        ((eq mark-type 'tuple)
5160         (set var (setq articles (cdr marks)))
5161         (when (not (listp (cdr (symbol-value var))))
5162           (set var (list (symbol-value var))))
5163         (when (not (listp (cdr articles)))
5164           (setq articles (list articles)))
5165         (while articles
5166           (when (or (not (consp (setq article (pop articles))))
5167                     (< (car article) min)
5168                     (> (car article) max))
5169             (set var (delq article (symbol-value var))))))
5170        ;; Adjust ranges (sloppily).
5171        ((eq mark-type 'range)
5172         (cond
5173          ((eq mark 'seen)
5174           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5175           ;; It should be (seen (NUM1 . NUM2)).
5176           (when (numberp (cddr marks))
5177             (setcdr marks (list (cdr marks))))
5178           (setq articles (cdr marks))
5179           (while (and articles
5180                       (or (and (consp (car articles))
5181                                (> min (cdar articles)))
5182                           (and (numberp (car articles))
5183                                (> min (car articles)))))
5184             (pop articles))
5185           (set var articles))))))))
5186
5187 (defun gnus-update-missing-marks (missing)
5188   "Go through the list of MISSING articles and remove them from the mark lists."
5189   (when missing
5190     (let (var m)
5191       ;; Go through all types.
5192       (dolist (elem gnus-article-mark-lists)
5193         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5194           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5195           (when (symbol-value var)
5196             ;; This list has articles.  So we delete all missing
5197             ;; articles from it.
5198             (setq m missing)
5199             (while m
5200               (set var (delq (pop m) (symbol-value var))))))))))
5201
5202 (defun gnus-update-marks ()
5203   "Enter the various lists of marked articles into the newsgroup info list."
5204   (let ((types gnus-article-mark-lists)
5205         (info (gnus-get-info gnus-newsgroup-name))
5206         type list newmarked symbol delta-marks)
5207     (when info
5208       ;; Add all marks lists to the list of marks lists.
5209       (while (setq type (pop types))
5210         (setq list (symbol-value
5211                     (setq symbol
5212                           (intern (format "gnus-newsgroup-%s" (car type))))))
5213
5214         (when list
5215           ;; Get rid of the entries of the articles that have the
5216           ;; default score.
5217           (when (and (eq (cdr type) 'score)
5218                      gnus-save-score
5219                      list)
5220             (let* ((arts list)
5221                    (prev (cons nil list))
5222                    (all prev))
5223               (while arts
5224                 (if (or (not (consp (car arts)))
5225                         (= (cdar arts) gnus-summary-default-score))
5226                     (setcdr prev (cdr arts))
5227                   (setq prev arts))
5228                 (setq arts (cdr arts)))
5229               (setq list (cdr all)))))
5230
5231         (when (eq (cdr type) 'seen)
5232           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5233
5234         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5235           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5236
5237         (when (and (gnus-check-backend-function
5238                     'request-set-mark gnus-newsgroup-name)
5239                    (not (gnus-article-unpropagatable-p (cdr type))))
5240           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5241                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5242                  (add (gnus-remove-from-range
5243                        (gnus-copy-sequence list) old)))
5244             (when add
5245               (push (list add 'add (list (cdr type))) delta-marks))
5246             (when del
5247               (push (list del 'del (list (cdr type))) delta-marks))))
5248
5249         (when list
5250           (push (cons (cdr type) list) newmarked)))
5251
5252       (when delta-marks
5253         (unless (gnus-check-group gnus-newsgroup-name)
5254           (error "Can't open server for %s" gnus-newsgroup-name))
5255         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5256
5257       ;; Enter these new marks into the info of the group.
5258       (if (nthcdr 3 info)
5259           (setcar (nthcdr 3 info) newmarked)
5260         ;; Add the marks lists to the end of the info.
5261         (when newmarked
5262           (setcdr (nthcdr 2 info) (list newmarked))))
5263
5264       ;; Cut off the end of the info if there's nothing else there.
5265       (let ((i 5))
5266         (while (and (> i 2)
5267                     (not (nth i info)))
5268           (when (nthcdr (decf i) info)
5269             (setcdr (nthcdr i info) nil)))))))
5270
5271 (defun gnus-set-mode-line (where)
5272   "Set the mode line of the article or summary buffers.
5273 If WHERE is `summary', the summary mode line format will be used."
5274   ;; Is this mode line one we keep updated?
5275   (when (and (memq where gnus-updated-mode-lines)
5276              (symbol-value
5277               (intern (format "gnus-%s-mode-line-format-spec" where))))
5278     (let (mode-string)
5279       (save-excursion
5280         ;; We evaluate this in the summary buffer since these
5281         ;; variables are buffer-local to that buffer.
5282         (set-buffer gnus-summary-buffer)
5283        ;; We bind all these variables that are used in the `eval' form
5284         ;; below.
5285         (let* ((mformat (symbol-value
5286                          (intern
5287                           (format "gnus-%s-mode-line-format-spec" where))))
5288                (gnus-tmp-group-name (gnus-group-decoded-name
5289                                      gnus-newsgroup-name))
5290                (gnus-tmp-article-number (or gnus-current-article 0))
5291                (gnus-tmp-unread gnus-newsgroup-unreads)
5292                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5293                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5294                (gnus-tmp-unread-and-unselected
5295                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5296                             (zerop gnus-tmp-unselected))
5297                        "")
5298                       ((zerop gnus-tmp-unselected)
5299                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5300                       (t (format "{%d(+%d) more}"
5301                                  gnus-tmp-unread-and-unticked
5302                                  gnus-tmp-unselected))))
5303                (gnus-tmp-subject
5304                 (if (and gnus-current-headers
5305                          (vectorp gnus-current-headers))
5306                     (gnus-mode-string-quote
5307                      (mail-header-subject gnus-current-headers))
5308                   ""))
5309                bufname-length max-len
5310                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5311           (setq mode-string (eval mformat))
5312           (setq bufname-length (if (string-match "%b" mode-string)
5313                                    (- (length
5314                                        (buffer-name
5315                                         (if (eq where 'summary)
5316                                             nil
5317                                           (get-buffer gnus-article-buffer))))
5318                                       2)
5319                                  0))
5320           (setq max-len (max 4 (if gnus-mode-non-string-length
5321                                    (- (window-width)
5322                                       gnus-mode-non-string-length
5323                                       bufname-length)
5324                                  (length mode-string))))
5325           ;; We might have to chop a bit of the string off...
5326           (when (> (length mode-string) max-len)
5327             (setq mode-string
5328                   (concat (truncate-string-to-width mode-string (- max-len 3))
5329                           "...")))
5330           ;; Pad the mode string a bit.
5331           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5332       ;; Update the mode line.
5333       (setq mode-line-buffer-identification
5334             (gnus-mode-line-buffer-identification (list mode-string)))
5335       (set-buffer-modified-p t))))
5336
5337 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5338   "Go through the HEADERS list and add all Xrefs to a hash table.
5339 The resulting hash table is returned, or nil if no Xrefs were found."
5340   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5341          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5342          (xref-hashtb (gnus-make-hashtable))
5343          start group entry number xrefs header)
5344     (while headers
5345       (setq header (pop headers))
5346       (when (and (setq xrefs (mail-header-xref header))
5347                  (not (memq (setq number (mail-header-number header))
5348                             unreads)))
5349         (setq start 0)
5350         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5351           (setq start (match-end 0))
5352           (setq group (if prefix
5353                           (concat prefix (substring xrefs (match-beginning 1)
5354                                                     (match-end 1)))
5355                         (substring xrefs (match-beginning 1) (match-end 1))))
5356           (setq number
5357                 (string-to-int (substring xrefs (match-beginning 2)
5358                                           (match-end 2))))
5359           (if (setq entry (gnus-gethash group xref-hashtb))
5360               (setcdr entry (cons number (cdr entry)))
5361             (gnus-sethash group (cons number nil) xref-hashtb)))))
5362     (and start xref-hashtb)))
5363
5364 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5365   "Look through all the headers and mark the Xrefs as read."
5366   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5367         name entry info xref-hashtb idlist method nth4)
5368     (save-excursion
5369       (set-buffer gnus-group-buffer)
5370       (when (setq xref-hashtb
5371                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5372         (mapatoms
5373          (lambda (group)
5374            (unless (string= from-newsgroup (setq name (symbol-name group)))
5375              (setq idlist (symbol-value group))
5376              ;; Dead groups are not updated.
5377              (and (prog1
5378                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5379                             info (nth 2 entry))
5380                     (when (stringp (setq nth4 (gnus-info-method info)))
5381                       (setq nth4 (gnus-server-to-method nth4))))
5382                   ;; Only do the xrefs if the group has the same
5383                   ;; select method as the group we have just read.
5384                   (or (gnus-methods-equal-p
5385                        nth4 (gnus-find-method-for-group from-newsgroup))
5386                       virtual
5387                       (equal nth4 (setq method (gnus-find-method-for-group
5388                                                 from-newsgroup)))
5389                       (and (equal (car nth4) (car method))
5390                            (equal (nth 1 nth4) (nth 1 method))))
5391                   gnus-use-cross-reference
5392                   (or (not (eq gnus-use-cross-reference t))
5393                       virtual
5394                       ;; Only do cross-references on subscribed
5395                       ;; groups, if that is what is wanted.
5396                       (<= (gnus-info-level info) gnus-level-subscribed))
5397                   (gnus-group-make-articles-read name idlist))))
5398          xref-hashtb)))))
5399
5400 (defun gnus-compute-read-articles (group articles)
5401   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5402          (info (nth 2 entry))
5403          (active (gnus-active group))
5404          ninfo)
5405     (when entry
5406       ;; First peel off all invalid article numbers.
5407       (when active
5408         (let ((ids articles)
5409               id first)
5410           (while (setq id (pop ids))
5411             (when (and first (> id (cdr active)))
5412               ;; We'll end up in this situation in one particular
5413               ;; obscure situation.  If you re-scan a group and get
5414               ;; a new article that is cross-posted to a different
5415               ;; group that has not been re-scanned, you might get
5416               ;; crossposted article that has a higher number than
5417               ;; Gnus believes possible.  So we re-activate this
5418               ;; group as well.  This might mean doing the
5419               ;; crossposting thingy will *increase* the number
5420               ;; of articles in some groups.  Tsk, tsk.
5421               (setq active (or (gnus-activate-group group) active)))
5422             (when (or (> id (cdr active))
5423                       (< id (car active)))
5424               (setq articles (delq id articles))))))
5425       ;; If the read list is nil, we init it.
5426       (if (and active
5427                (null (gnus-info-read info))
5428                (> (car active) 1))
5429           (setq ninfo (cons 1 (1- (car active))))
5430         (setq ninfo (gnus-info-read info)))
5431       ;; Then we add the read articles to the range.
5432       (gnus-add-to-range
5433        ninfo (setq articles (sort articles '<))))))
5434
5435 (defun gnus-group-make-articles-read (group articles)
5436   "Update the info of GROUP to say that ARTICLES are read."
5437   (let* ((num 0)
5438          (entry (gnus-gethash group gnus-newsrc-hashtb))
5439          (info (nth 2 entry))
5440          (active (gnus-active group))
5441          range)
5442     (when entry
5443       (setq range (gnus-compute-read-articles group articles))
5444       (save-excursion
5445         (set-buffer gnus-group-buffer)
5446         (gnus-undo-register
5447           `(progn
5448              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5449              (gnus-info-set-read ',info ',(gnus-info-read info))
5450              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5451              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5452              (gnus-group-update-group ,group t))))
5453       ;; Add the read articles to the range.
5454       (gnus-info-set-read info range)
5455       (gnus-request-set-mark group (list (list range 'add '(read))))
5456       ;; Then we have to re-compute how many unread
5457       ;; articles there are in this group.
5458       (when active
5459         (cond
5460          ((not range)
5461           (setq num (- (1+ (cdr active)) (car active))))
5462          ((not (listp (cdr range)))
5463           (setq num (- (cdr active) (- (1+ (cdr range))
5464                                        (car range)))))
5465          (t
5466           (while range
5467             (if (numberp (car range))
5468                 (setq num (1+ num))
5469               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5470             (setq range (cdr range)))
5471           (setq num (- (cdr active) num))))
5472         ;; Update the number of unread articles.
5473         (setcar entry num)
5474         ;; Update the group buffer.
5475         (gnus-group-update-group group t)))))
5476
5477 (defvar gnus-newsgroup-none-id 0)
5478
5479 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5480   (let ((cur nntp-server-buffer)
5481         (dependencies
5482          (or dependencies
5483              (save-excursion (set-buffer gnus-summary-buffer)
5484                              gnus-newsgroup-dependencies)))
5485         headers id end ref
5486         (mail-parse-charset gnus-newsgroup-charset)
5487         (mail-parse-ignored-charsets
5488          (save-excursion (condition-case nil
5489                              (set-buffer gnus-summary-buffer)
5490                            (error))
5491                          gnus-newsgroup-ignored-charsets)))
5492     (save-excursion
5493       (set-buffer nntp-server-buffer)
5494       ;; Translate all TAB characters into SPACE characters.
5495       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5496       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5497       (gnus-run-hooks 'gnus-parse-headers-hook)
5498       (let ((case-fold-search t)
5499             in-reply-to header p lines chars)
5500         (goto-char (point-min))
5501         ;; Search to the beginning of the next header.  Error messages
5502         ;; do not begin with 2 or 3.
5503         (while (re-search-forward "^[23][0-9]+ " nil t)
5504           (setq id nil
5505                 ref nil)
5506           ;; This implementation of this function, with nine
5507           ;; search-forwards instead of the one re-search-forward and
5508           ;; a case (which basically was the old function) is actually
5509           ;; about twice as fast, even though it looks messier.  You
5510           ;; can't have everything, I guess.  Speed and elegance
5511           ;; doesn't always go hand in hand.
5512           (setq
5513            header
5514            (vector
5515             ;; Number.
5516             (prog1
5517                 (read cur)
5518               (end-of-line)
5519               (setq p (point))
5520               (narrow-to-region (point)
5521                                 (or (and (search-forward "\n.\n" nil t)
5522                                          (- (point) 2))
5523                                     (point))))
5524             ;; Subject.
5525             (progn
5526               (goto-char p)
5527               (if (search-forward "\nsubject:" nil t)
5528                   (funcall gnus-decode-encoded-word-function
5529                            (nnheader-header-value))
5530                 "(none)"))
5531             ;; From.
5532             (progn
5533               (goto-char p)
5534               (if (search-forward "\nfrom:" nil t)
5535                   (funcall gnus-decode-encoded-word-function
5536                            (nnheader-header-value))
5537                 "(nobody)"))
5538             ;; Date.
5539             (progn
5540               (goto-char p)
5541               (if (search-forward "\ndate:" nil t)
5542                   (nnheader-header-value) ""))
5543             ;; Message-ID.
5544             (progn
5545               (goto-char p)
5546               (setq id (if (re-search-forward
5547                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5548                            ;; We do it this way to make sure the Message-ID
5549                            ;; is (somewhat) syntactically valid.
5550                            (buffer-substring (match-beginning 1)
5551                                              (match-end 1))
5552                          ;; If there was no message-id, we just fake one
5553                          ;; to make subsequent routines simpler.
5554                          (nnheader-generate-fake-message-id))))
5555             ;; References.
5556             (progn
5557               (goto-char p)
5558               (if (search-forward "\nreferences:" nil t)
5559                   (progn
5560                     (setq end (point))
5561                     (prog1
5562                         (nnheader-header-value)
5563                       (setq ref
5564                             (buffer-substring
5565                              (progn
5566                                (end-of-line)
5567                                (search-backward ">" end t)
5568                                (1+ (point)))
5569                              (progn
5570                                (search-backward "<" end t)
5571                                (point))))))
5572                 ;; Get the references from the in-reply-to header if there
5573                 ;; were no references and the in-reply-to header looks
5574                 ;; promising.
5575                 (if (and (search-forward "\nin-reply-to:" nil t)
5576                          (setq in-reply-to (nnheader-header-value))
5577                          (string-match "<[^>]+>" in-reply-to))
5578                     (let (ref2)
5579                       (setq ref (substring in-reply-to (match-beginning 0)
5580                                            (match-end 0)))
5581                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5582                         (setq ref2 (substring in-reply-to (match-beginning 0)
5583                                               (match-end 0)))
5584                         (when (> (length ref2) (length ref))
5585                           (setq ref ref2)))
5586                       ref)
5587                   (setq ref nil))))
5588             ;; Chars.
5589             (progn
5590               (goto-char p)
5591               (if (search-forward "\nchars: " nil t)
5592                   (if (numberp (setq chars (ignore-errors (read cur))))
5593                       chars -1)
5594                 -1))
5595             ;; Lines.
5596             (progn
5597               (goto-char p)
5598               (if (search-forward "\nlines: " nil t)
5599                   (if (numberp (setq lines (ignore-errors (read cur))))
5600                       lines -1)
5601                 -1))
5602             ;; Xref.
5603             (progn
5604               (goto-char p)
5605               (and (search-forward "\nxref:" nil t)
5606                    (nnheader-header-value)))
5607             ;; Extra.
5608             (when gnus-extra-headers
5609               (let ((extra gnus-extra-headers)
5610                     out)
5611                 (while extra
5612                   (goto-char p)
5613                   (when (search-forward
5614                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5615                     (push (cons (car extra) (nnheader-header-value))
5616                           out))
5617                   (pop extra))
5618                 out))))
5619           (when (equal id ref)
5620             (setq ref nil))
5621
5622           (when gnus-alter-header-function
5623             (funcall gnus-alter-header-function header)
5624             (setq id (mail-header-id header)
5625                   ref (gnus-parent-id (mail-header-references header))))
5626
5627           (when (setq header
5628                       (gnus-dependencies-add-header
5629                        header dependencies force-new))
5630             (push header headers))
5631           (goto-char (point-max))
5632           (widen))
5633         (nreverse headers)))))
5634
5635 ;; Goes through the xover lines and returns a list of vectors
5636 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5637                                                   force-new dependencies
5638                                                   group also-fetch-heads)
5639   "Parse the news overview data in the server buffer.
5640 Return a list of headers that match SEQUENCE (see
5641 `nntp-retrieve-headers')."
5642   ;; Get the Xref when the users reads the articles since most/some
5643   ;; NNTP servers do not include Xrefs when using XOVER.
5644   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5645   (let ((mail-parse-charset gnus-newsgroup-charset)
5646         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5647         (cur nntp-server-buffer)
5648         (dependencies (or dependencies gnus-newsgroup-dependencies))
5649         (allp (cond
5650                ((eq gnus-read-all-available-headers t)
5651                 t)
5652                ((stringp gnus-read-all-available-headers)
5653                 (string-match gnus-read-all-available-headers group))
5654                (t
5655                 nil)))
5656         number headers header)
5657     (save-excursion
5658       (set-buffer nntp-server-buffer)
5659       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5660       ;; Allow the user to mangle the headers before parsing them.
5661       (gnus-run-hooks 'gnus-parse-headers-hook)
5662       (goto-char (point-min))
5663       (gnus-parse-without-error
5664         (while (and (or sequence allp)
5665                     (not (eobp)))
5666           (setq number (read cur))
5667           (when (not allp)
5668             (while (and sequence
5669                         (< (car sequence) number))
5670               (setq sequence (cdr sequence))))
5671           (when (and (or allp
5672                          (and sequence
5673                               (eq number (car sequence))))
5674                      (progn
5675                        (setq sequence (cdr sequence))
5676                        (setq header (inline
5677                                       (gnus-nov-parse-line
5678                                        number dependencies force-new)))))
5679             (push header headers))
5680           (forward-line 1)))
5681       ;; A common bug in inn is that if you have posted an article and
5682       ;; then retrieves the active file, it will answer correctly --
5683       ;; the new article is included.  However, a NOV entry for the
5684       ;; article may not have been generated yet, so this may fail.
5685       ;; We work around this problem by retrieving the last few
5686       ;; headers using HEAD.
5687       (if (or (not also-fetch-heads)
5688               (not sequence))
5689           ;; We (probably) got all the headers.
5690           (nreverse headers)
5691         (let ((gnus-nov-is-evil t))
5692           (nconc
5693            (nreverse headers)
5694            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5695              (gnus-get-newsgroup-headers))))))))
5696
5697 (defun gnus-article-get-xrefs ()
5698   "Fill in the Xref value in `gnus-current-headers', if necessary.
5699 This is meant to be called in `gnus-article-internal-prepare-hook'."
5700   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5701                                  gnus-current-headers)))
5702     (or (not gnus-use-cross-reference)
5703         (not headers)
5704         (and (mail-header-xref headers)
5705              (not (string= (mail-header-xref headers) "")))
5706         (let ((case-fold-search t)
5707               xref)
5708           (save-restriction
5709             (nnheader-narrow-to-headers)
5710             (goto-char (point-min))
5711             (when (or (and (not (eobp))
5712                            (eq (downcase (char-after)) ?x)
5713                            (looking-at "Xref:"))
5714                       (search-forward "\nXref:" nil t))
5715               (goto-char (1+ (match-end 0)))
5716               (setq xref (buffer-substring (point)
5717                                            (progn (end-of-line) (point))))
5718               (mail-header-set-xref headers xref)))))))
5719
5720 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5721   "Find article ID and insert the summary line for that article.
5722 OLD-HEADER can either be a header or a line number to insert
5723 the subject line on."
5724   (let* ((line (and (numberp old-header) old-header))
5725          (old-header (and (vectorp old-header) old-header))
5726          (header (cond ((and old-header use-old-header)
5727                         old-header)
5728                        ((and (numberp id)
5729                              (gnus-number-to-header id))
5730                         (gnus-number-to-header id))
5731                        (t
5732                         (gnus-read-header id))))
5733          (number (and (numberp id) id))
5734          d)
5735     (when header
5736       ;; Rebuild the thread that this article is part of and go to the
5737       ;; article we have fetched.
5738       (when (and (not gnus-show-threads)
5739                  old-header)
5740         (when (and number
5741                    (setq d (gnus-data-find (mail-header-number old-header))))
5742           (goto-char (gnus-data-pos d))
5743           (gnus-data-remove
5744            number
5745            (- (gnus-point-at-bol)
5746               (prog1
5747                   (1+ (gnus-point-at-eol))
5748                 (gnus-delete-line))))))
5749       (when old-header
5750         (mail-header-set-number header (mail-header-number old-header)))
5751       (setq gnus-newsgroup-sparse
5752             (delq (setq number (mail-header-number header))
5753                   gnus-newsgroup-sparse))
5754       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5755       (push number gnus-newsgroup-limit)
5756       (gnus-rebuild-thread (mail-header-id header) line)
5757       (gnus-summary-goto-subject number nil t))
5758     (when (and (numberp number)
5759                (> number 0))
5760       ;; We have to update the boundaries even if we can't fetch the
5761       ;; article if ID is a number -- so that the next `P' or `N'
5762       ;; command will fetch the previous (or next) article even
5763       ;; if the one we tried to fetch this time has been canceled.
5764       (when (> number gnus-newsgroup-end)
5765         (setq gnus-newsgroup-end number))
5766       (when (< number gnus-newsgroup-begin)
5767         (setq gnus-newsgroup-begin number))
5768       (setq gnus-newsgroup-unselected
5769             (delq number gnus-newsgroup-unselected)))
5770     ;; Report back a success?
5771     (and header (mail-header-number header))))
5772
5773 ;;; Process/prefix in the summary buffer
5774
5775 (defun gnus-summary-work-articles (n)
5776   "Return a list of articles to be worked upon.
5777 The prefix argument, the list of process marked articles, and the
5778 current article will be taken into consideration."
5779   (save-excursion
5780     (set-buffer gnus-summary-buffer)
5781     (cond
5782      (n
5783       ;; A numerical prefix has been given.
5784       (setq n (prefix-numeric-value n))
5785       (let ((backward (< n 0))
5786             (n (abs (prefix-numeric-value n)))
5787             articles article)
5788         (save-excursion
5789           (while
5790               (and (> n 0)
5791                    (push (setq article (gnus-summary-article-number))
5792                          articles)
5793                    (if backward
5794                        (gnus-summary-find-prev nil article)
5795                      (gnus-summary-find-next nil article)))
5796             (decf n)))
5797         (nreverse articles)))
5798      ((and (gnus-region-active-p) (mark))
5799       (message "region active")
5800       ;; Work on the region between point and mark.
5801       (let ((max (max (point) (mark)))
5802             articles article)
5803         (save-excursion
5804           (goto-char (min (min (point) (mark))))
5805           (while
5806               (and
5807                (push (setq article (gnus-summary-article-number)) articles)
5808                (gnus-summary-find-next nil article)
5809                (< (point) max)))
5810           (nreverse articles))))
5811      (gnus-newsgroup-processable
5812       ;; There are process-marked articles present.
5813       ;; Save current state.
5814       (gnus-summary-save-process-mark)
5815       ;; Return the list.
5816       (reverse gnus-newsgroup-processable))
5817      (t
5818       ;; Just return the current article.
5819       (list (gnus-summary-article-number))))))
5820
5821 (defmacro gnus-summary-iterate (arg &rest forms)
5822   "Iterate over the process/prefixed articles and do FORMS.
5823 ARG is the interactive prefix given to the command.  FORMS will be
5824 executed with point over the summary line of the articles."
5825   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5826     `(let ((,articles (gnus-summary-work-articles ,arg)))
5827        (while ,articles
5828          (gnus-summary-goto-subject (car ,articles))
5829          ,@forms
5830          (pop ,articles)))))
5831
5832 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5833 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5834
5835 (defun gnus-summary-save-process-mark ()
5836   "Push the current set of process marked articles on the stack."
5837   (interactive)
5838   (push (copy-sequence gnus-newsgroup-processable)
5839         gnus-newsgroup-process-stack))
5840
5841 (defun gnus-summary-kill-process-mark ()
5842   "Push the current set of process marked articles on the stack and unmark."
5843   (interactive)
5844   (gnus-summary-save-process-mark)
5845   (gnus-summary-unmark-all-processable))
5846
5847 (defun gnus-summary-yank-process-mark ()
5848   "Pop the last process mark state off the stack and restore it."
5849   (interactive)
5850   (unless gnus-newsgroup-process-stack
5851     (error "Empty mark stack"))
5852   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5853
5854 (defun gnus-summary-process-mark-set (set)
5855   "Make SET into the current process marked articles."
5856   (gnus-summary-unmark-all-processable)
5857   (while set
5858     (gnus-summary-set-process-mark (pop set))))
5859
5860 ;;; Searching and stuff
5861
5862 (defun gnus-summary-search-group (&optional backward use-level)
5863   "Search for next unread newsgroup.
5864 If optional argument BACKWARD is non-nil, search backward instead."
5865   (save-excursion
5866     (set-buffer gnus-group-buffer)
5867     (when (gnus-group-search-forward
5868            backward nil (if use-level (gnus-group-group-level) nil))
5869       (gnus-group-group-name))))
5870
5871 (defun gnus-summary-best-group (&optional exclude-group)
5872   "Find the name of the best unread group.
5873 If EXCLUDE-GROUP, do not go to this group."
5874   (save-excursion
5875     (set-buffer gnus-group-buffer)
5876     (save-excursion
5877       (gnus-group-best-unread-group exclude-group))))
5878
5879 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5880   (if backward (gnus-summary-find-prev)
5881     (let* ((dummy (gnus-summary-article-intangible-p))
5882            (article (or article (gnus-summary-article-number)))
5883            (arts (gnus-data-find-list article))
5884            result)
5885       (when (and (not dummy)
5886                  (or (not gnus-summary-check-current)
5887                      (not unread)
5888                      (not (gnus-data-unread-p (car arts)))))
5889         (setq arts (cdr arts)))
5890       (when (setq result
5891                   (if unread
5892                       (progn
5893                         (while arts
5894                           (when (or (and undownloaded
5895                                          (eq gnus-undownloaded-mark
5896                                              (gnus-data-mark (car arts))))
5897                                     (gnus-data-unread-p (car arts)))
5898                             (setq result (car arts)
5899                                   arts nil))
5900                           (setq arts (cdr arts)))
5901                         result)
5902                     (car arts)))
5903         (goto-char (gnus-data-pos result))
5904         (gnus-data-number result)))))
5905
5906 (defun gnus-summary-find-prev (&optional unread article)
5907   (let* ((eobp (eobp))
5908          (article (or article (gnus-summary-article-number)))
5909          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5910          result)
5911     (when (and (not eobp)
5912                (or (not gnus-summary-check-current)
5913                    (not unread)
5914                    (not (gnus-data-unread-p (car arts)))))
5915       (setq arts (cdr arts)))
5916     (when (setq result
5917                 (if unread
5918                     (progn
5919                       (while arts
5920                         (when (gnus-data-unread-p (car arts))
5921                           (setq result (car arts)
5922                                 arts nil))
5923                         (setq arts (cdr arts)))
5924                       result)
5925                   (car arts)))
5926       (goto-char (gnus-data-pos result))
5927       (gnus-data-number result))))
5928
5929 (defun gnus-summary-find-subject (subject &optional unread backward article)
5930   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5931          (article (or article (gnus-summary-article-number)))
5932          (articles (gnus-data-list backward))
5933          (arts (gnus-data-find-list article articles))
5934          result)
5935     (when (or (not gnus-summary-check-current)
5936               (not unread)
5937               (not (gnus-data-unread-p (car arts))))
5938       (setq arts (cdr arts)))
5939     (while arts
5940       (and (or (not unread)
5941                (gnus-data-unread-p (car arts)))
5942            (vectorp (gnus-data-header (car arts)))
5943            (gnus-subject-equal
5944             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5945            (setq result (car arts)
5946                  arts nil))
5947       (setq arts (cdr arts)))
5948     (and result
5949          (goto-char (gnus-data-pos result))
5950          (gnus-data-number result))))
5951
5952 (defun gnus-summary-search-forward (&optional unread subject backward)
5953   "Search forward for an article.
5954 If UNREAD, look for unread articles.  If SUBJECT, look for
5955 articles with that subject.  If BACKWARD, search backward instead."
5956   (cond (subject (gnus-summary-find-subject subject unread backward))
5957         (backward (gnus-summary-find-prev unread))
5958         (t (gnus-summary-find-next unread))))
5959
5960 (defun gnus-recenter (&optional n)
5961   "Center point in window and redisplay frame.
5962 Also do horizontal recentering."
5963   (interactive "P")
5964   (when (and gnus-auto-center-summary
5965              (not (eq gnus-auto-center-summary 'vertical)))
5966     (gnus-horizontal-recenter))
5967   (recenter n))
5968
5969 (defun gnus-summary-recenter ()
5970   "Center point in the summary window.
5971 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5972 displayed, no centering will be performed."
5973   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5974 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5975   (interactive)
5976   (let* ((top (cond ((< (window-height) 4) 0)
5977                     ((< (window-height) 7) 1)
5978                     (t (if (numberp gnus-auto-center-summary)
5979                            gnus-auto-center-summary
5980                          2))))
5981          (height (1- (window-height)))
5982          (bottom (save-excursion (goto-char (point-max))
5983                                  (forward-line (- height))
5984                                  (point)))
5985          (window (get-buffer-window (current-buffer))))
5986     ;; The user has to want it.
5987     (when gnus-auto-center-summary
5988       (when (get-buffer-window gnus-article-buffer)
5989         ;; Only do recentering when the article buffer is displayed,
5990       ;; Set the window start to either `bottom', which is the biggest
5991         ;; possible valid number, or the second line from the top,
5992         ;; whichever is the least.
5993         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5994           (if (> bottom top-pos)
5995               ;; Keep the second line from the top visible
5996               (set-window-start window top-pos t)
5997             ;; Try to keep the bottom line visible; if it's partially
5998             ;; obscured, either scroll one more line to make it fully
5999             ;; visible, or revert to using TOP-POS.
6000             (save-excursion
6001               (goto-char (point-max))
6002               (forward-line -1)
6003               (let ((last-line-start (point)))
6004                 (goto-char bottom)
6005                 (set-window-start window (point) t)
6006                 (when (not (pos-visible-in-window-p last-line-start window))
6007                   (forward-line 1)
6008                   (set-window-start window (min (point) top-pos) t)))))))
6009       ;; Do horizontal recentering while we're at it.
6010       (when (and (get-buffer-window (current-buffer) t)
6011                  (not (eq gnus-auto-center-summary 'vertical)))
6012         (let ((selected (selected-window)))
6013           (select-window (get-buffer-window (current-buffer) t))
6014           (gnus-summary-position-point)
6015           (gnus-horizontal-recenter)
6016           (select-window selected))))))
6017
6018 (defun gnus-summary-jump-to-group (newsgroup)
6019   "Move point to NEWSGROUP in group mode buffer."
6020   ;; Keep update point of group mode buffer if visible.
6021   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6022       (save-window-excursion
6023         ;; Take care of tree window mode.
6024         (when (get-buffer-window gnus-group-buffer)
6025           (pop-to-buffer gnus-group-buffer))
6026         (gnus-group-jump-to-group newsgroup))
6027     (save-excursion
6028       ;; Take care of tree window mode.
6029       (if (get-buffer-window gnus-group-buffer)
6030           (pop-to-buffer gnus-group-buffer)
6031         (set-buffer gnus-group-buffer))
6032       (gnus-group-jump-to-group newsgroup))))
6033
6034 ;; This function returns a list of article numbers based on the
6035 ;; difference between the ranges of read articles in this group and
6036 ;; the range of active articles.
6037 (defun gnus-list-of-unread-articles (group)
6038   (let* ((read (gnus-info-read (gnus-get-info group)))
6039          (active (or (gnus-active group) (gnus-activate-group group)))
6040          (last (cdr active))
6041          first nlast unread)
6042     ;; If none are read, then all are unread.
6043     (if (not read)
6044         (setq first (car active))
6045       ;; If the range of read articles is a single range, then the
6046       ;; first unread article is the article after the last read
6047       ;; article.  Sounds logical, doesn't it?
6048       (if (and (not (listp (cdr read)))
6049                (or (< (car read) (car active))
6050                    (progn (setq read (list read))
6051                           nil)))
6052           (setq first (max (car active) (1+ (cdr read))))
6053         ;; `read' is a list of ranges.
6054         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6055                                   (caar read)))
6056                   1)
6057           (setq first (car active)))
6058         (while read
6059           (when first
6060             (while (< first nlast)
6061               (push first unread)
6062               (setq first (1+ first))))
6063           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6064           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6065           (setq read (cdr read)))))
6066     ;; And add the last unread articles.
6067     (while (<= first last)
6068       (push first unread)
6069       (setq first (1+ first)))
6070     ;; Return the list of unread articles.
6071     (delq 0 (nreverse unread))))
6072
6073 (defun gnus-list-of-read-articles (group)
6074   "Return a list of unread, unticked and non-dormant articles."
6075   (let* ((info (gnus-get-info group))
6076          (marked (gnus-info-marks info))
6077          (active (gnus-active group)))
6078     (and info active
6079          (gnus-list-range-difference
6080           (gnus-list-range-difference
6081            (gnus-sorted-complement
6082             (gnus-uncompress-range active)
6083             (gnus-list-of-unread-articles group))
6084            (cdr (assq 'dormant marked)))
6085           (cdr (assq 'tick marked))))))
6086
6087 ;; Various summary commands
6088
6089 (defun gnus-summary-select-article-buffer ()
6090   "Reconfigure windows to show article buffer."
6091   (interactive)
6092   (if (not (gnus-buffer-live-p gnus-article-buffer))
6093       (error "There is no article buffer for this summary buffer")
6094     (gnus-configure-windows 'article)
6095     (select-window (get-buffer-window gnus-article-buffer))))
6096
6097 (defun gnus-summary-universal-argument (arg)
6098   "Perform any operation on all articles that are process/prefixed."
6099   (interactive "P")
6100   (let ((articles (gnus-summary-work-articles arg))
6101         func article)
6102     (if (eq
6103          (setq
6104           func
6105           (key-binding
6106            (read-key-sequence
6107             (substitute-command-keys
6108              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6109          'undefined)
6110         (gnus-error 1 "Undefined key")
6111       (save-excursion
6112         (while articles
6113           (gnus-summary-goto-subject (setq article (pop articles)))
6114           (let (gnus-newsgroup-processable)
6115             (command-execute func))
6116           (gnus-summary-remove-process-mark article)))))
6117   (gnus-summary-position-point))
6118
6119 (defun gnus-summary-toggle-truncation (&optional arg)
6120   "Toggle truncation of summary lines.
6121 With arg, turn line truncation on iff arg is positive."
6122   (interactive "P")
6123   (setq truncate-lines
6124         (if (null arg) (not truncate-lines)
6125           (> (prefix-numeric-value arg) 0)))
6126   (redraw-display))
6127
6128 (defun gnus-summary-reselect-current-group (&optional all rescan)
6129   "Exit and then reselect the current newsgroup.
6130 The prefix argument ALL means to select all articles."
6131   (interactive "P")
6132   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6133     (error "Ephemeral groups can't be reselected"))
6134   (let ((current-subject (gnus-summary-article-number))
6135         (group gnus-newsgroup-name))
6136     (setq gnus-newsgroup-begin nil)
6137     (gnus-summary-exit)
6138     ;; We have to adjust the point of group mode buffer because
6139     ;; point was moved to the next unread newsgroup by exiting.
6140     (gnus-summary-jump-to-group group)
6141     (when rescan
6142       (save-excursion
6143         (gnus-group-get-new-news-this-group 1)))
6144     (gnus-group-read-group all t)
6145     (gnus-summary-goto-subject current-subject nil t)))
6146
6147 (defun gnus-summary-rescan-group (&optional all)
6148   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6149   (interactive "P")
6150   (gnus-summary-reselect-current-group all t))
6151
6152 (defun gnus-summary-update-info (&optional non-destructive)
6153   (save-excursion
6154     (let ((group gnus-newsgroup-name))
6155       (when group
6156         (when gnus-newsgroup-kill-headers
6157           (setq gnus-newsgroup-killed
6158                 (gnus-compress-sequence
6159                  (gnus-sorted-union
6160                   (gnus-list-range-intersection
6161                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6162                   gnus-newsgroup-unreads)
6163                  t)))
6164         (unless (listp (cdr gnus-newsgroup-killed))
6165           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6166         (let ((headers gnus-newsgroup-headers))
6167           ;; Set the new ranges of read articles.
6168           (save-excursion
6169             (set-buffer gnus-group-buffer)
6170             (gnus-undo-force-boundary))
6171           (gnus-update-read-articles
6172            group (gnus-sorted-union
6173                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6174           ;; Set the current article marks.
6175           (let ((gnus-newsgroup-scored
6176                  (if (and (not gnus-save-score)
6177                           (not non-destructive))
6178                      nil
6179                    gnus-newsgroup-scored)))
6180             (save-excursion
6181               (gnus-update-marks)))
6182           ;; Do the cross-ref thing.
6183           (when gnus-use-cross-reference
6184             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6185           ;; Do not switch windows but change the buffer to work.
6186           (set-buffer gnus-group-buffer)
6187           (unless (gnus-ephemeral-group-p group)
6188             (gnus-group-update-group group)))))))
6189
6190 (defun gnus-summary-save-newsrc (&optional force)
6191   "Save the current number of read/marked articles in the dribble buffer.
6192 The dribble buffer will then be saved.
6193 If FORCE (the prefix), also save the .newsrc file(s)."
6194   (interactive "P")
6195   (gnus-summary-update-info t)
6196   (if force
6197       (gnus-save-newsrc-file)
6198     (gnus-dribble-save)))
6199
6200 (defun gnus-summary-exit (&optional temporary)
6201   "Exit reading current newsgroup, and then return to group selection mode.
6202 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6203   (interactive)
6204   (gnus-set-global-variables)
6205   (when (gnus-buffer-live-p gnus-article-buffer)
6206     (save-excursion
6207       (set-buffer gnus-article-buffer)
6208       (mm-destroy-parts gnus-article-mime-handles)
6209       ;; Set it to nil for safety reason.
6210       (setq gnus-article-mime-handle-alist nil)
6211       (setq gnus-article-mime-handles nil)))
6212   (gnus-kill-save-kill-buffer)
6213   (gnus-async-halt-prefetch)
6214   (let* ((group gnus-newsgroup-name)
6215          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6216          (mode major-mode)
6217          (group-point nil)
6218          (buf (current-buffer)))
6219     (unless quit-config
6220       ;; Do adaptive scoring, and possibly save score files.
6221       (when gnus-newsgroup-adaptive
6222         (gnus-score-adaptive))
6223       (when gnus-use-scoring
6224         (gnus-score-save)))
6225     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6226     ;; If we have several article buffers, we kill them at exit.
6227     (unless gnus-single-article-buffer
6228       (gnus-kill-buffer gnus-original-article-buffer)
6229       (setq gnus-article-current nil))
6230     (when gnus-use-cache
6231       (gnus-cache-possibly-remove-articles)
6232       (gnus-cache-save-buffers))
6233     (gnus-async-prefetch-remove-group group)
6234     (when gnus-suppress-duplicates
6235       (gnus-dup-enter-articles))
6236     (when gnus-use-trees
6237       (gnus-tree-close group))
6238     (when gnus-use-cache
6239       (gnus-cache-write-active))
6240     ;; Remove entries for this group.
6241     (nnmail-purge-split-history (gnus-group-real-name group))
6242     ;; Make all changes in this group permanent.
6243     (unless quit-config
6244       (gnus-run-hooks 'gnus-exit-group-hook)
6245       (gnus-summary-update-info))
6246     (gnus-close-group group)
6247     ;; Make sure where we were, and go to next newsgroup.
6248     (set-buffer gnus-group-buffer)
6249     (unless quit-config
6250       (gnus-group-jump-to-group group))
6251     (gnus-run-hooks 'gnus-summary-exit-hook)
6252     (unless (or quit-config
6253                 ;; If this group has disappeared from the summary
6254                 ;; buffer, don't skip forwards.
6255                 (not (string= group (gnus-group-group-name))))
6256       (gnus-group-next-unread-group 1))
6257     (setq group-point (point))
6258     (if temporary
6259         nil                             ;Nothing to do.
6260       ;; If we have several article buffers, we kill them at exit.
6261       (unless gnus-single-article-buffer
6262         (gnus-kill-buffer gnus-article-buffer)
6263         (gnus-kill-buffer gnus-original-article-buffer)
6264         (setq gnus-article-current nil))
6265       (set-buffer buf)
6266       (if (not gnus-kill-summary-on-exit)
6267           (progn
6268             (gnus-deaden-summary)
6269             (setq mode nil))
6270        ;; We set all buffer-local variables to nil.  It is unclear why
6271         ;; this is needed, but if we don't, buffer-local variables are
6272         ;; not garbage-collected, it seems.  This would the lead to en
6273         ;; ever-growing Emacs.
6274         (gnus-summary-clear-local-variables)
6275         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6276           (gnus-summary-clear-local-variables))
6277         (when (get-buffer gnus-article-buffer)
6278           (bury-buffer gnus-article-buffer))
6279         ;; We clear the global counterparts of the buffer-local
6280         ;; variables as well, just to be on the safe side.
6281         (set-buffer gnus-group-buffer)
6282         (gnus-summary-clear-local-variables)
6283         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6284           (gnus-summary-clear-local-variables)))
6285       (setq gnus-current-select-method gnus-select-method)
6286       (pop-to-buffer gnus-group-buffer)
6287       (if (not quit-config)
6288           (progn
6289             (goto-char group-point)
6290             (gnus-configure-windows 'group 'force))
6291         (gnus-handle-ephemeral-exit quit-config))
6292       ;; Return to group mode buffer.
6293       (when (eq mode 'gnus-summary-mode)
6294         (gnus-kill-buffer buf))
6295       ;; Clear the current group name.
6296       (unless quit-config
6297         (setq gnus-newsgroup-name nil)))))
6298
6299 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6300 (defun gnus-summary-exit-no-update (&optional no-questions)
6301   "Quit reading current newsgroup without updating read article info."
6302   (interactive)
6303   (let* ((group gnus-newsgroup-name)
6304          (quit-config (gnus-group-quit-config group)))
6305     (when (or no-questions
6306               gnus-expert-user
6307               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6308       (gnus-async-halt-prefetch)
6309       (mapcar 'funcall
6310               (delq 'gnus-summary-expire-articles
6311                     (copy-sequence gnus-summary-prepare-exit-hook)))
6312       (when (gnus-buffer-live-p gnus-article-buffer)
6313         (save-excursion
6314           (set-buffer gnus-article-buffer)
6315           (mm-destroy-parts gnus-article-mime-handles)
6316           ;; Set it to nil for safety reason.
6317           (setq gnus-article-mime-handle-alist nil)
6318           (setq gnus-article-mime-handles nil)))
6319       ;; If we have several article buffers, we kill them at exit.
6320       (unless gnus-single-article-buffer
6321         (gnus-kill-buffer gnus-article-buffer)
6322         (gnus-kill-buffer gnus-original-article-buffer)
6323         (setq gnus-article-current nil))
6324       (if (not gnus-kill-summary-on-exit)
6325           (gnus-deaden-summary)
6326         (gnus-close-group group)
6327         (gnus-summary-clear-local-variables)
6328         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6329           (gnus-summary-clear-local-variables))
6330         (set-buffer gnus-group-buffer)
6331         (gnus-summary-clear-local-variables)
6332         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6333           (gnus-summary-clear-local-variables))
6334         (when (get-buffer gnus-summary-buffer)
6335           (kill-buffer gnus-summary-buffer)))
6336       (unless gnus-single-article-buffer
6337         (setq gnus-article-current nil))
6338       (when gnus-use-trees
6339         (gnus-tree-close group))
6340       (gnus-async-prefetch-remove-group group)
6341       (when (get-buffer gnus-article-buffer)
6342         (bury-buffer gnus-article-buffer))
6343       ;; Return to the group buffer.
6344       (gnus-configure-windows 'group 'force)
6345       ;; Clear the current group name.
6346       (setq gnus-newsgroup-name nil)
6347       (when (equal (gnus-group-group-name) group)
6348         (gnus-group-next-unread-group 1))
6349       (when quit-config
6350         (gnus-handle-ephemeral-exit quit-config)))))
6351
6352 (defun gnus-handle-ephemeral-exit (quit-config)
6353   "Handle movement when leaving an ephemeral group.
6354 The state which existed when entering the ephemeral is reset."
6355   (if (not (buffer-name (car quit-config)))
6356       (gnus-configure-windows 'group 'force)
6357     (set-buffer (car quit-config))
6358     (cond ((eq major-mode 'gnus-summary-mode)
6359            (gnus-set-global-variables))
6360           ((eq major-mode 'gnus-article-mode)
6361            (save-excursion
6362              ;; The `gnus-summary-buffer' variable may point
6363              ;; to the old summary buffer when using a single
6364              ;; article buffer.
6365              (unless (gnus-buffer-live-p gnus-summary-buffer)
6366                (set-buffer gnus-group-buffer))
6367              (set-buffer gnus-summary-buffer)
6368              (gnus-set-global-variables))))
6369     (if (or (eq (cdr quit-config) 'article)
6370             (eq (cdr quit-config) 'pick))
6371         (progn
6372           ;; The current article may be from the ephemeral group
6373           ;; thus it is best that we reload this article
6374           (gnus-summary-show-article)
6375           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6376               (gnus-configure-windows 'pick 'force)
6377             (gnus-configure-windows (cdr quit-config) 'force)))
6378       (gnus-configure-windows (cdr quit-config) 'force))
6379     (when (eq major-mode 'gnus-summary-mode)
6380       (gnus-summary-next-subject 1 nil t)
6381       (gnus-summary-recenter)
6382       (gnus-summary-position-point))))
6383
6384 ;;; Dead summaries.
6385
6386 (defvar gnus-dead-summary-mode-map nil)
6387
6388 (unless gnus-dead-summary-mode-map
6389   (setq gnus-dead-summary-mode-map (make-keymap))
6390   (suppress-keymap gnus-dead-summary-mode-map)
6391   (substitute-key-definition
6392    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6393   (dolist (key '("\C-d" "\r" "\177" [delete]))
6394     (define-key gnus-dead-summary-mode-map
6395       key 'gnus-summary-wake-up-the-dead))
6396   (dolist (key '("q" "Q"))
6397     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6398
6399 (defvar gnus-dead-summary-mode nil
6400   "Minor mode for Gnus summary buffers.")
6401
6402 (defun gnus-dead-summary-mode (&optional arg)
6403   "Minor mode for Gnus summary buffers."
6404   (interactive "P")
6405   (when (eq major-mode 'gnus-summary-mode)
6406     (make-local-variable 'gnus-dead-summary-mode)
6407     (setq gnus-dead-summary-mode
6408           (if (null arg) (not gnus-dead-summary-mode)
6409             (> (prefix-numeric-value arg) 0)))
6410     (when gnus-dead-summary-mode
6411       (gnus-add-minor-mode
6412        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6413
6414 (defun gnus-deaden-summary ()
6415   "Make the current summary buffer into a dead summary buffer."
6416   ;; Kill any previous dead summary buffer.
6417   (when (and gnus-dead-summary
6418              (buffer-name gnus-dead-summary))
6419     (save-excursion
6420       (set-buffer gnus-dead-summary)
6421       (when gnus-dead-summary-mode
6422         (kill-buffer (current-buffer)))))
6423   ;; Make this the current dead summary.
6424   (setq gnus-dead-summary (current-buffer))
6425   (gnus-dead-summary-mode 1)
6426   (let ((name (buffer-name)))
6427     (when (string-match "Summary" name)
6428       (rename-buffer
6429        (concat (substring name 0 (match-beginning 0)) "Dead "
6430                (substring name (match-beginning 0)))
6431        t)
6432       (bury-buffer))))
6433
6434 (defun gnus-kill-or-deaden-summary (buffer)
6435   "Kill or deaden the summary BUFFER."
6436   (save-excursion
6437     (when (and (buffer-name buffer)
6438                (not gnus-single-article-buffer))
6439       (save-excursion
6440         (set-buffer buffer)
6441         (gnus-kill-buffer gnus-article-buffer)
6442         (gnus-kill-buffer gnus-original-article-buffer)))
6443     (cond
6444      ;; Kill the buffer.
6445      (gnus-kill-summary-on-exit
6446       (when (and gnus-use-trees
6447                  (gnus-buffer-exists-p buffer))
6448         (save-excursion
6449           (set-buffer buffer)
6450           (gnus-tree-close gnus-newsgroup-name)))
6451       (gnus-kill-buffer buffer))
6452      ;; Deaden the buffer.
6453      ((gnus-buffer-exists-p buffer)
6454       (save-excursion
6455         (set-buffer buffer)
6456         (gnus-deaden-summary))))))
6457
6458 (defun gnus-summary-wake-up-the-dead (&rest args)
6459   "Wake up the dead summary buffer."
6460   (interactive)
6461   (gnus-dead-summary-mode -1)
6462   (let ((name (buffer-name)))
6463     (when (string-match "Dead " name)
6464       (rename-buffer
6465        (concat (substring name 0 (match-beginning 0))
6466                (substring name (match-end 0)))
6467        t)))
6468   (gnus-message 3 "This dead summary is now alive again"))
6469
6470 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6471 (defun gnus-summary-fetch-faq (&optional faq-dir)
6472   "Fetch the FAQ for the current group.
6473 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6474 in."
6475   (interactive
6476    (list
6477     (when current-prefix-arg
6478       (completing-read
6479        "Faq dir: " (and (listp gnus-group-faq-directory)
6480                         (mapcar (lambda (file) (list file))
6481                                 gnus-group-faq-directory))))))
6482   (let (gnus-faq-buffer)
6483     (when (setq gnus-faq-buffer
6484                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6485       (gnus-configure-windows 'summary-faq))))
6486
6487 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6488 (defun gnus-summary-describe-group (&optional force)
6489   "Describe the current newsgroup."
6490   (interactive "P")
6491   (gnus-group-describe-group force gnus-newsgroup-name))
6492
6493 (defun gnus-summary-describe-briefly ()
6494   "Describe summary mode commands briefly."
6495   (interactive)
6496   (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")))
6497
6498 ;; Walking around group mode buffer from summary mode.
6499
6500 (defun gnus-summary-next-group (&optional no-article target-group backward)
6501   "Exit current newsgroup and then select next unread newsgroup.
6502 If prefix argument NO-ARTICLE is non-nil, no article is selected
6503 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6504 previous group instead."
6505   (interactive "P")
6506   ;; Stop pre-fetching.
6507   (gnus-async-halt-prefetch)
6508   (let ((current-group gnus-newsgroup-name)
6509         (current-buffer (current-buffer))
6510         entered)
6511    ;; First we semi-exit this group to update Xrefs and all variables.
6512     ;; We can't do a real exit, because the window conf must remain
6513     ;; the same in case the user is prompted for info, and we don't
6514     ;; want the window conf to change before that...
6515     (gnus-summary-exit t)
6516     (while (not entered)
6517       ;; Then we find what group we are supposed to enter.
6518       (set-buffer gnus-group-buffer)
6519       (gnus-group-jump-to-group current-group)
6520       (setq target-group
6521             (or target-group
6522                 (if (eq gnus-keep-same-level 'best)
6523                     (gnus-summary-best-group gnus-newsgroup-name)
6524                   (gnus-summary-search-group backward gnus-keep-same-level))))
6525       (if (not target-group)
6526           ;; There are no further groups, so we return to the group
6527           ;; buffer.
6528           (progn
6529             (gnus-message 5 "Returning to the group buffer")
6530             (setq entered t)
6531             (when (gnus-buffer-live-p current-buffer)
6532               (set-buffer current-buffer)
6533               (gnus-summary-exit))
6534             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6535         ;; We try to enter the target group.
6536         (gnus-group-jump-to-group target-group)
6537         (let ((unreads (gnus-group-group-unread)))
6538           (if (and (or (eq t unreads)
6539                        (and unreads (not (zerop unreads))))
6540                    (gnus-summary-read-group
6541                     target-group nil no-article
6542                     (and (buffer-name current-buffer) current-buffer)
6543                     nil backward))
6544               (setq entered t)
6545             (setq current-group target-group
6546                   target-group nil)))))))
6547
6548 (defun gnus-summary-prev-group (&optional no-article)
6549   "Exit current newsgroup and then select previous unread newsgroup.
6550 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6551   (interactive "P")
6552   (gnus-summary-next-group no-article nil t))
6553
6554 ;; Walking around summary lines.
6555
6556 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6557   "Go to the first unread subject.
6558 If UNREAD is non-nil, go to the first unread article.
6559 Returns the article selected or nil if there are no unread articles."
6560   (interactive "P")
6561   (prog1
6562       (cond
6563        ;; Empty summary.
6564        ((null gnus-newsgroup-data)
6565         (gnus-message 3 "No articles in the group")
6566         nil)
6567        ;; Pick the first article.
6568        ((not unread)
6569         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6570         (gnus-data-number (car gnus-newsgroup-data)))
6571        ;; No unread articles.
6572        ((null gnus-newsgroup-unreads)
6573         (gnus-message 3 "No more unread articles")
6574         nil)
6575        ;; Find the first unread article.
6576        (t
6577         (let ((data gnus-newsgroup-data))
6578           (while (and data
6579                       (and (not (and undownloaded
6580                                      (eq gnus-undownloaded-mark
6581                                          (gnus-data-mark (car data)))))
6582                            (if unseen
6583                                (or (not (memq
6584                                          (gnus-data-number (car data))
6585                                          gnus-newsgroup-unseen))
6586                                    (not (gnus-data-unread-p (car data))))
6587                              (not (gnus-data-unread-p (car data))))))
6588             (setq data (cdr data)))
6589           (when data
6590             (goto-char (gnus-data-pos (car data)))
6591             (gnus-data-number (car data))))))
6592     (gnus-summary-position-point)))
6593
6594 (defun gnus-summary-next-subject (n &optional unread dont-display)
6595   "Go to next N'th summary line.
6596 If N is negative, go to the previous N'th subject line.
6597 If UNREAD is non-nil, only unread articles are selected.
6598 The difference between N and the actual number of steps taken is
6599 returned."
6600   (interactive "p")
6601   (let ((backward (< n 0))
6602         (n (abs n)))
6603     (while (and (> n 0)
6604                 (if backward
6605                     (gnus-summary-find-prev unread)
6606                   (gnus-summary-find-next unread)))
6607       (unless (zerop (setq n (1- n)))
6608         (gnus-summary-show-thread)))
6609     (when (/= 0 n)
6610       (gnus-message 7 "No more%s articles"
6611                     (if unread " unread" "")))
6612     (unless dont-display
6613       (gnus-summary-recenter)
6614       (gnus-summary-position-point))
6615     n))
6616
6617 (defun gnus-summary-next-unread-subject (n)
6618   "Go to next N'th unread summary line."
6619   (interactive "p")
6620   (gnus-summary-next-subject n t))
6621
6622 (defun gnus-summary-prev-subject (n &optional unread)
6623   "Go to previous N'th summary line.
6624 If optional argument UNREAD is non-nil, only unread article is selected."
6625   (interactive "p")
6626   (gnus-summary-next-subject (- n) unread))
6627
6628 (defun gnus-summary-prev-unread-subject (n)
6629   "Go to previous N'th unread summary line."
6630   (interactive "p")
6631   (gnus-summary-next-subject (- n) t))
6632
6633 (defun gnus-summary-goto-subject (article &optional force silent)
6634   "Go the subject line of ARTICLE.
6635 If FORCE, also allow jumping to articles not currently shown."
6636   (interactive "nArticle number: ")
6637   (unless (numberp article)
6638     (error "Article %s is not a number" article))
6639   (let ((b (point))
6640         (data (gnus-data-find article)))
6641     ;; We read in the article if we have to.
6642     (and (not data)
6643          force
6644          (gnus-summary-insert-subject
6645           article
6646           (if (or (numberp force) (vectorp force)) force)
6647           t)
6648          (setq data (gnus-data-find article)))
6649     (goto-char b)
6650     (if (not data)
6651         (progn
6652           (unless silent
6653             (gnus-message 3 "Can't find article %d" article))
6654           nil)
6655       (let ((pt (gnus-data-pos data)))
6656         (goto-char pt)
6657         (gnus-summary-set-article-display-arrow pt))
6658       (gnus-summary-position-point)
6659       article)))
6660
6661 ;; Walking around summary lines with displaying articles.
6662
6663 (defun gnus-summary-expand-window (&optional arg)
6664   "Make the summary buffer take up the entire Emacs frame.
6665 Given a prefix, will force an `article' buffer configuration."
6666   (interactive "P")
6667   (if arg
6668       (gnus-configure-windows 'article 'force)
6669     (gnus-configure-windows 'summary 'force)))
6670
6671 (defun gnus-summary-display-article (article &optional all-header)
6672   "Display ARTICLE in article buffer."
6673   (when (gnus-buffer-live-p gnus-article-buffer)
6674     (with-current-buffer gnus-article-buffer
6675       (mm-enable-multibyte)))
6676   (gnus-set-global-variables)
6677   (when (gnus-buffer-live-p gnus-article-buffer)
6678     (with-current-buffer gnus-article-buffer
6679       (setq gnus-article-charset gnus-newsgroup-charset)
6680       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6681       (mm-enable-multibyte)))
6682   (if (null article)
6683       nil
6684     (prog1
6685         (if gnus-summary-display-article-function
6686             (funcall gnus-summary-display-article-function article all-header)
6687           (gnus-article-prepare article all-header))
6688       (gnus-run-hooks 'gnus-select-article-hook)
6689       (when (and gnus-current-article
6690                  (not (zerop gnus-current-article)))
6691         (gnus-summary-goto-subject gnus-current-article))
6692       (gnus-summary-recenter)
6693       (when (and gnus-use-trees gnus-show-threads)
6694         (gnus-possibly-generate-tree article)
6695         (gnus-highlight-selected-tree article))
6696       ;; Successfully display article.
6697       (gnus-article-set-window-start
6698        (cdr (assq article gnus-newsgroup-bookmarks))))))
6699
6700 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6701   "Select the current article.
6702 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6703 non-nil, the article will be re-fetched even if it already present in
6704 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6705 be displayed."
6706   ;; Make sure we are in the summary buffer to work around bbdb bug.
6707   (unless (eq major-mode 'gnus-summary-mode)
6708     (set-buffer gnus-summary-buffer))
6709   (let ((article (or article (gnus-summary-article-number)))
6710         (all-headers (not (not all-headers))) ;Must be t or nil.
6711         gnus-summary-display-article-function)
6712     (and (not pseudo)
6713          (gnus-summary-article-pseudo-p article)
6714          (error "This is a pseudo-article"))
6715     (save-excursion
6716       (set-buffer gnus-summary-buffer)
6717       (if (or (and gnus-single-article-buffer
6718                    (or (null gnus-current-article)
6719                        (null gnus-article-current)
6720                        (null (get-buffer gnus-article-buffer))
6721                        (not (eq article (cdr gnus-article-current)))
6722                        (not (equal (car gnus-article-current)
6723                                    gnus-newsgroup-name))))
6724               (and (not gnus-single-article-buffer)
6725                    (or (null gnus-current-article)
6726                        (not (eq gnus-current-article article))))
6727               force)
6728           ;; The requested article is different from the current article.
6729           (progn
6730             (gnus-summary-display-article article all-headers)
6731             (when (gnus-buffer-live-p gnus-article-buffer)
6732               (with-current-buffer gnus-article-buffer
6733                 (if (not gnus-article-decoded-p) ;; a local variable
6734                     (mm-disable-multibyte))))
6735             (gnus-article-set-window-start
6736              (cdr (assq article gnus-newsgroup-bookmarks)))
6737             article)
6738         'old))))
6739
6740 (defun gnus-summary-force-verify-and-decrypt ()
6741   (interactive)
6742   (let ((mm-verify-option 'known)
6743         (mm-decrypt-option 'known)
6744         (gnus-buttonized-mime-types (append (list "multipart/signed"
6745                                                   "multipart/encrypted")
6746                                             gnus-buttonized-mime-types)))
6747     (gnus-summary-select-article nil 'force)))
6748
6749 (defun gnus-summary-set-current-mark (&optional current-mark)
6750   "Obsolete function."
6751   nil)
6752
6753 (defun gnus-summary-next-article (&optional unread subject backward push)
6754   "Select the next article.
6755 If UNREAD, only unread articles are selected.
6756 If SUBJECT, only articles with SUBJECT are selected.
6757 If BACKWARD, the previous article is selected instead of the next."
6758   (interactive "P")
6759   (cond
6760    ;; Is there such an article?
6761    ((and (gnus-summary-search-forward unread subject backward)
6762          (or (gnus-summary-display-article (gnus-summary-article-number))
6763              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6764     (gnus-summary-position-point))
6765    ;; If not, we try the first unread, if that is wanted.
6766    ((and subject
6767          gnus-auto-select-same
6768          (gnus-summary-first-unread-article))
6769     (gnus-summary-position-point)
6770     (gnus-message 6 "Wrapped"))
6771    ;; Try to get next/previous article not displayed in this group.
6772    ((and gnus-auto-extend-newsgroup
6773          (not unread) (not subject))
6774     (gnus-summary-goto-article
6775      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6776      nil (count-lines (point-min) (point))))
6777    ;; Go to next/previous group.
6778    (t
6779     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6780       (gnus-summary-jump-to-group gnus-newsgroup-name))
6781     (let ((cmd last-command-char)
6782           (point
6783            (save-excursion
6784              (set-buffer gnus-group-buffer)
6785              (point)))
6786           (group
6787            (if (eq gnus-keep-same-level 'best)
6788                (gnus-summary-best-group gnus-newsgroup-name)
6789              (gnus-summary-search-group backward gnus-keep-same-level))))
6790       ;; For some reason, the group window gets selected.  We change
6791       ;; it back.
6792       (select-window (get-buffer-window (current-buffer)))
6793       ;; Select next unread newsgroup automagically.
6794       (cond
6795        ((or (not gnus-auto-select-next)
6796             (not cmd))
6797         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6798        ((or (eq gnus-auto-select-next 'quietly)
6799             (and (eq gnus-auto-select-next 'slightly-quietly)
6800                  push)
6801             (and (eq gnus-auto-select-next 'almost-quietly)
6802                  (gnus-summary-last-article-p)))
6803         ;; Select quietly.
6804         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6805             (gnus-summary-exit)
6806           (gnus-message 7 "No more%s articles (%s)..."
6807                         (if unread " unread" "")
6808                         (if group (concat "selecting " group)
6809                           "exiting"))
6810           (gnus-summary-next-group nil group backward)))
6811        (t
6812         (when (gnus-key-press-event-p last-input-event)
6813           (gnus-summary-walk-group-buffer
6814            gnus-newsgroup-name cmd unread backward point))))))))
6815
6816 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6817   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6818                       (?\C-p (gnus-group-prev-unread-group 1))))
6819         (cursor-in-echo-area t)
6820         keve key group ended)
6821     (save-excursion
6822       (set-buffer gnus-group-buffer)
6823       (goto-char start)
6824       (setq group
6825             (if (eq gnus-keep-same-level 'best)
6826                 (gnus-summary-best-group gnus-newsgroup-name)
6827               (gnus-summary-search-group backward gnus-keep-same-level))))
6828     (while (not ended)
6829       (gnus-message
6830        5 "No more%s articles%s" (if unread " unread" "")
6831        (if (and group
6832                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6833            (format " (Type %s for %s [%s])"
6834                    (single-key-description cmd) group
6835                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6836          (format " (Type %s to exit %s)"
6837                  (single-key-description cmd)
6838                  gnus-newsgroup-name)))
6839       ;; Confirm auto selection.
6840       (setq key (car (setq keve (gnus-read-event-char))))
6841       (setq ended t)
6842       (cond
6843        ((assq key keystrokes)
6844         (let ((obuf (current-buffer)))
6845           (switch-to-buffer gnus-group-buffer)
6846           (when group
6847             (gnus-group-jump-to-group group))
6848           (eval (cadr (assq key keystrokes)))
6849           (setq group (gnus-group-group-name))
6850           (switch-to-buffer obuf))
6851         (setq ended nil))
6852        ((equal key cmd)
6853         (if (or (not group)
6854                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6855             (gnus-summary-exit)
6856           (gnus-summary-next-group nil group backward)))
6857        (t
6858         (push (cdr keve) unread-command-events))))))
6859
6860 (defun gnus-summary-next-unread-article ()
6861   "Select unread article after current one."
6862   (interactive)
6863   (gnus-summary-next-article
6864    (or (not (eq gnus-summary-goto-unread 'never))
6865        (gnus-summary-last-article-p (gnus-summary-article-number)))
6866    (and gnus-auto-select-same
6867         (gnus-summary-article-subject))))
6868
6869 (defun gnus-summary-prev-article (&optional unread subject)
6870   "Select the article after the current one.
6871 If UNREAD is non-nil, only unread articles are selected."
6872   (interactive "P")
6873   (gnus-summary-next-article unread subject t))
6874
6875 (defun gnus-summary-prev-unread-article ()
6876   "Select unread article before current one."
6877   (interactive)
6878   (gnus-summary-prev-article
6879    (or (not (eq gnus-summary-goto-unread 'never))
6880        (gnus-summary-first-article-p (gnus-summary-article-number)))
6881    (and gnus-auto-select-same
6882         (gnus-summary-article-subject))))
6883
6884 (defun gnus-summary-next-page (&optional lines circular)
6885   "Show next page of the selected article.
6886 If at the end of the current article, select the next article.
6887 LINES says how many lines should be scrolled up.
6888
6889 If CIRCULAR is non-nil, go to the start of the article instead of
6890 selecting the next article when reaching the end of the current
6891 article."
6892   (interactive "P")
6893   (setq gnus-summary-buffer (current-buffer))
6894   (gnus-set-global-variables)
6895   (let ((article (gnus-summary-article-number))
6896         (article-window (get-buffer-window gnus-article-buffer t))
6897         endp)
6898     ;; If the buffer is empty, we have no article.
6899     (unless article
6900       (error "No article to select"))
6901     (gnus-configure-windows 'article)
6902     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6903         (if (and (eq gnus-summary-goto-unread 'never)
6904                  (not (gnus-summary-last-article-p article)))
6905             (gnus-summary-next-article)
6906           (gnus-summary-next-unread-article))
6907       (if (or (null gnus-current-article)
6908               (null gnus-article-current)
6909               (/= article (cdr gnus-article-current))
6910               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6911           ;; Selected subject is different from current article's.
6912           (gnus-summary-display-article article)
6913         (when article-window
6914           (gnus-eval-in-buffer-window gnus-article-buffer
6915             (setq endp (gnus-article-next-page lines)))
6916           (when endp
6917             (cond (circular
6918                    (gnus-summary-beginning-of-article))
6919                   (lines
6920                    (gnus-message 3 "End of message"))
6921                   ((null lines)
6922                    (if (and (eq gnus-summary-goto-unread 'never)
6923                             (not (gnus-summary-last-article-p article)))
6924                        (gnus-summary-next-article)
6925                      (gnus-summary-next-unread-article))))))))
6926     (gnus-summary-recenter)
6927     (gnus-summary-position-point)))
6928
6929 (defun gnus-summary-prev-page (&optional lines move)
6930   "Show previous page of selected article.
6931 Argument LINES specifies lines to be scrolled down.
6932 If MOVE, move to the previous unread article if point is at
6933 the beginning of the buffer."
6934   (interactive "P")
6935   (let ((article (gnus-summary-article-number))
6936         (article-window (get-buffer-window gnus-article-buffer t))
6937         endp)
6938     (gnus-configure-windows 'article)
6939     (if (or (null gnus-current-article)
6940             (null gnus-article-current)
6941             (/= article (cdr gnus-article-current))
6942             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6943         ;; Selected subject is different from current article's.
6944         (gnus-summary-display-article article)
6945       (gnus-summary-recenter)
6946       (when article-window
6947         (gnus-eval-in-buffer-window gnus-article-buffer
6948           (setq endp (gnus-article-prev-page lines)))
6949         (when (and move endp)
6950           (cond (lines
6951                  (gnus-message 3 "Beginning of message"))
6952                 ((null lines)
6953                  (if (and (eq gnus-summary-goto-unread 'never)
6954                           (not (gnus-summary-first-article-p article)))
6955                      (gnus-summary-prev-article)
6956                    (gnus-summary-prev-unread-article))))))))
6957   (gnus-summary-position-point))
6958
6959 (defun gnus-summary-prev-page-or-article (&optional lines)
6960   "Show previous page of selected article.
6961 Argument LINES specifies lines to be scrolled down.
6962 If at the beginning of the article, go to the next article."
6963   (interactive "P")
6964   (gnus-summary-prev-page lines t))
6965
6966 (defun gnus-summary-scroll-up (lines)
6967   "Scroll up (or down) one line current article.
6968 Argument LINES specifies lines to be scrolled up (or down if negative)."
6969   (interactive "p")
6970   (gnus-configure-windows 'article)
6971   (gnus-summary-show-thread)
6972   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6973     (gnus-eval-in-buffer-window gnus-article-buffer
6974       (cond ((> lines 0)
6975              (when (gnus-article-next-page lines)
6976                (gnus-message 3 "End of message")))
6977             ((< lines 0)
6978              (gnus-article-prev-page (- lines))))))
6979   (gnus-summary-recenter)
6980   (gnus-summary-position-point))
6981
6982 (defun gnus-summary-scroll-down (lines)
6983   "Scroll down (or up) one line current article.
6984 Argument LINES specifies lines to be scrolled down (or up if negative)."
6985   (interactive "p")
6986   (gnus-summary-scroll-up (- lines)))
6987
6988 (defun gnus-summary-next-same-subject ()
6989   "Select next article which has the same subject as current one."
6990   (interactive)
6991   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6992
6993 (defun gnus-summary-prev-same-subject ()
6994   "Select previous article which has the same subject as current one."
6995   (interactive)
6996   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6997
6998 (defun gnus-summary-next-unread-same-subject ()
6999   "Select next unread article which has the same subject as current one."
7000   (interactive)
7001   (gnus-summary-next-article t (gnus-summary-article-subject)))
7002
7003 (defun gnus-summary-prev-unread-same-subject ()
7004   "Select previous unread article which has the same subject as current one."
7005   (interactive)
7006   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7007
7008 (defun gnus-summary-first-unread-article ()
7009   "Select the first unread article.
7010 Return nil if there are no unread articles."
7011   (interactive)
7012   (prog1
7013       (when (gnus-summary-first-subject t)
7014         (gnus-summary-show-thread)
7015         (gnus-summary-first-subject t)
7016         (gnus-summary-display-article (gnus-summary-article-number)))
7017     (gnus-summary-position-point)))
7018
7019 (defun gnus-summary-first-unread-subject ()
7020   "Place the point on the subject line of the first unread article.
7021 Return nil if there are no unread articles."
7022   (interactive)
7023   (prog1
7024       (when (gnus-summary-first-subject t)
7025         (gnus-summary-show-thread)
7026         (gnus-summary-first-subject t))
7027     (gnus-summary-position-point)))
7028
7029 (defun gnus-summary-first-unseen-subject ()
7030   "Place the point on the subject line of the first unseen article.
7031 Return nil if there are no unseen articles."
7032   (interactive)
7033   (prog1
7034       (when (gnus-summary-first-subject t t t)
7035         (gnus-summary-show-thread)
7036         (gnus-summary-first-subject t t t))
7037     (gnus-summary-position-point)))
7038
7039 (defun gnus-summary-first-unseen-or-unread-subject ()
7040   "Place the point on the subject line of the first unseen article.
7041 Return nil if there are no unseen articles."
7042   (interactive)
7043   (prog1
7044       (unless (when (gnus-summary-first-subject t t t)
7045                 (gnus-summary-show-thread)
7046                 (gnus-summary-first-subject t t t))
7047         (when (gnus-summary-first-subject t)
7048           (gnus-summary-show-thread)
7049           (gnus-summary-first-subject t)))
7050     (gnus-summary-position-point)))
7051
7052 (defun gnus-summary-first-article ()
7053   "Select the first article.
7054 Return nil if there are no articles."
7055   (interactive)
7056   (prog1
7057       (when (gnus-summary-first-subject)
7058         (gnus-summary-show-thread)
7059         (gnus-summary-first-subject)
7060         (gnus-summary-display-article (gnus-summary-article-number)))
7061     (gnus-summary-position-point)))
7062
7063 (defun gnus-summary-best-unread-article (&optional arg)
7064   "Select the unread article with the highest score.
7065 If given a prefix argument, select the next unread article that has a
7066 score higher than the default score."
7067   (interactive "P")
7068   (let ((article (if arg
7069                      (gnus-summary-better-unread-subject)
7070                    (gnus-summary-best-unread-subject))))
7071     (if article
7072         (gnus-summary-goto-article article)
7073       (error "No unread articles"))))
7074
7075 (defun gnus-summary-best-unread-subject ()
7076   "Select the unread subject with the highest score."
7077   (interactive)
7078   (let ((best -1000000)
7079         (data gnus-newsgroup-data)
7080         article score)
7081     (while data
7082       (and (gnus-data-unread-p (car data))
7083            (> (setq score
7084                     (gnus-summary-article-score (gnus-data-number (car data))))
7085               best)
7086            (setq best score
7087                  article (gnus-data-number (car data))))
7088       (setq data (cdr data)))
7089     (when article
7090       (gnus-summary-goto-subject article))
7091     (gnus-summary-position-point)
7092     article))
7093
7094 (defun gnus-summary-better-unread-subject ()
7095   "Select the first unread subject that has a score over the default score."
7096   (interactive)
7097   (let ((data gnus-newsgroup-data)
7098         article score)
7099     (while (and (setq article (gnus-data-number (car data)))
7100                 (or (gnus-data-read-p (car data))
7101                     (not (> (gnus-summary-article-score article)
7102                             gnus-summary-default-score))))
7103       (setq data (cdr data)))
7104     (when article
7105       (gnus-summary-goto-subject article))
7106     (gnus-summary-position-point)
7107     article))
7108
7109 (defun gnus-summary-last-subject ()
7110   "Go to the last displayed subject line in the group."
7111   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7112     (when article
7113       (gnus-summary-goto-subject article))))
7114
7115 (defun gnus-summary-goto-article (article &optional all-headers force)
7116   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7117 If ALL-HEADERS is non-nil, no header lines are hidden.
7118 If FORCE, go to the article even if it isn't displayed.  If FORCE
7119 is a number, it is the line the article is to be displayed on."
7120   (interactive
7121    (list
7122     (completing-read
7123      "Article number or Message-ID: "
7124      (mapcar (lambda (number) (list (int-to-string number)))
7125              gnus-newsgroup-limit))
7126     current-prefix-arg
7127     t))
7128   (prog1
7129       (if (and (stringp article)
7130                (string-match "@" article))
7131           (gnus-summary-refer-article article)
7132         (when (stringp article)
7133           (setq article (string-to-number article)))
7134         (if (gnus-summary-goto-subject article force)
7135             (gnus-summary-display-article article all-headers)
7136           (gnus-message 4 "Couldn't go to article %s" article) nil))
7137     (gnus-summary-position-point)))
7138
7139 (defun gnus-summary-goto-last-article ()
7140   "Go to the previously read article."
7141   (interactive)
7142   (prog1
7143       (when gnus-last-article
7144         (gnus-summary-goto-article gnus-last-article nil t))
7145     (gnus-summary-position-point)))
7146
7147 (defun gnus-summary-pop-article (number)
7148   "Pop one article off the history and go to the previous.
7149 NUMBER articles will be popped off."
7150   (interactive "p")
7151   (let (to)
7152     (setq gnus-newsgroup-history
7153           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7154     (if to
7155         (gnus-summary-goto-article (car to) nil t)
7156       (error "Article history empty")))
7157   (gnus-summary-position-point))
7158
7159 ;; Summary commands and functions for limiting the summary buffer.
7160
7161 (defun gnus-summary-limit-to-articles (n)
7162   "Limit the summary buffer to the next N articles.
7163 If not given a prefix, use the process marked articles instead."
7164   (interactive "P")
7165   (prog1
7166       (let ((articles (gnus-summary-work-articles n)))
7167         (setq gnus-newsgroup-processable nil)
7168         (gnus-summary-limit articles))
7169     (gnus-summary-position-point)))
7170
7171 (defun gnus-summary-pop-limit (&optional total)
7172   "Restore the previous limit.
7173 If given a prefix, remove all limits."
7174   (interactive "P")
7175   (when total
7176     (setq gnus-newsgroup-limits
7177           (list (mapcar (lambda (h) (mail-header-number h))
7178                         gnus-newsgroup-headers))))
7179   (unless gnus-newsgroup-limits
7180     (error "No limit to pop"))
7181   (prog1
7182       (gnus-summary-limit nil 'pop)
7183     (gnus-summary-position-point)))
7184
7185 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7186   "Limit the summary buffer to articles that have subjects that match a regexp.
7187 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7188   (interactive
7189    (list (read-string (if current-prefix-arg
7190                           "Exclude subject (regexp): "
7191                         "Limit to subject (regexp): "))
7192          nil current-prefix-arg))
7193   (unless header
7194     (setq header "subject"))
7195   (when (not (equal "" subject))
7196     (prog1
7197         (let ((articles (gnus-summary-find-matching
7198                          (or header "subject") subject 'all nil nil
7199                          not-matching)))
7200           (unless articles
7201             (error "Found no matches for \"%s\"" subject))
7202           (gnus-summary-limit articles))
7203       (gnus-summary-position-point))))
7204
7205 (defun gnus-summary-limit-to-author (from &optional not-matching)
7206   "Limit the summary buffer to articles that have authors that match a regexp.
7207 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7208   (interactive
7209    (list (read-string (if current-prefix-arg
7210                           "Exclude author (regexp): "
7211                         "Limit to author (regexp): "))
7212          current-prefix-arg))
7213   (gnus-summary-limit-to-subject from "from" not-matching))
7214
7215 (defun gnus-summary-limit-to-age (age &optional younger-p)
7216   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7217 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7218 articles that are younger than AGE days."
7219   (interactive
7220    (let ((younger current-prefix-arg)
7221          (days-got nil)
7222          days)
7223      (while (not days-got)
7224        (setq days (if younger
7225                       (read-string "Limit to articles within (in days): ")
7226                     (read-string "Limit to articles older than (in days): ")))
7227        (when (> (length days) 0)
7228          (setq days (read days)))
7229        (if (numberp days)
7230            (progn
7231              (setq days-got t)
7232              (if (< days 0)
7233                  (progn
7234                    (setq younger (not younger))
7235                    (setq days (* days -1)))))
7236          (message "Please enter a number.")
7237          (sleep-for 1)))
7238      (list days younger)))
7239   (prog1
7240       (let ((data gnus-newsgroup-data)
7241             (cutoff (days-to-time age))
7242             articles d date is-younger)
7243         (while (setq d (pop data))
7244           (when (and (vectorp (gnus-data-header d))
7245                      (setq date (mail-header-date (gnus-data-header d))))
7246             (setq is-younger (time-less-p
7247                               (time-since (condition-case ()
7248                                               (date-to-time date)
7249                                             (error '(0 0))))
7250                               cutoff))
7251             (when (if younger-p
7252                       is-younger
7253                     (not is-younger))
7254               (push (gnus-data-number d) articles))))
7255         (gnus-summary-limit (nreverse articles)))
7256     (gnus-summary-position-point)))
7257
7258 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7259   "Limit the summary buffer to articles that match an 'extra' header."
7260   (interactive
7261    (let ((header
7262           (intern
7263            (gnus-completing-read-with-default
7264             (symbol-name (car gnus-extra-headers))
7265             (if current-prefix-arg
7266                 "Exclude extra header:"
7267               "Limit extra header:")
7268             (mapcar (lambda (x)
7269                       (cons (symbol-name x) x))
7270                     gnus-extra-headers)
7271             nil
7272             t))))
7273      (list header
7274            (read-string (format "%s header %s (regexp): "
7275                                 (if current-prefix-arg "Exclude" "Limit to")
7276                                 header))
7277            current-prefix-arg)))
7278   (when (not (equal "" regexp))
7279     (prog1
7280         (let ((articles (gnus-summary-find-matching
7281                          (cons 'extra header) regexp 'all nil nil
7282                          not-matching)))
7283           (unless articles
7284             (error "Found no matches for \"%s\"" regexp))
7285           (gnus-summary-limit articles))
7286       (gnus-summary-position-point))))
7287
7288 (defun gnus-summary-limit-to-display-predicate ()
7289   "Limit the summary buffer to the predicated in the `display' group parameter."
7290   (interactive)
7291   (unless gnus-newsgroup-display
7292     (error "There is no `display' group parameter"))
7293   (let (articles)
7294     (dolist (number gnus-newsgroup-articles)
7295       (when (funcall gnus-newsgroup-display)
7296         (push number articles)))
7297     (gnus-summary-limit articles))
7298   (gnus-summary-position-point))
7299
7300 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7301 (make-obsolete
7302  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7303
7304 (defun gnus-summary-limit-to-unread (&optional all)
7305   "Limit the summary buffer to articles that are not marked as read.
7306 If ALL is non-nil, limit strictly to unread articles."
7307   (interactive "P")
7308   (if all
7309       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7310     (gnus-summary-limit-to-marks
7311      ;; Concat all the marks that say that an article is read and have
7312      ;; those removed.
7313      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7314            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7315            gnus-low-score-mark gnus-expirable-mark
7316            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7317            gnus-duplicate-mark gnus-souped-mark)
7318      'reverse)))
7319
7320 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7321 (make-obsolete 'gnus-summary-delete-marked-with
7322                'gnus-summary-limit-exclude-marks)
7323
7324 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7325   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7326 If REVERSE, limit the summary buffer to articles that are marked
7327 with MARKS.  MARKS can either be a string of marks or a list of marks.
7328 Returns how many articles were removed."
7329   (interactive "sMarks: ")
7330   (gnus-summary-limit-to-marks marks t))
7331
7332 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7333   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7334 If REVERSE (the prefix), limit the summary buffer to articles that are
7335 not marked with MARKS.  MARKS can either be a string of marks or a
7336 list of marks.
7337 Returns how many articles were removed."
7338   (interactive "sMarks: \nP")
7339   (prog1
7340       (let ((data gnus-newsgroup-data)
7341             (marks (if (listp marks) marks
7342                      (append marks nil))) ; Transform to list.
7343             articles)
7344         (while data
7345           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7346                   (memq (gnus-data-mark (car data)) marks))
7347             (push (gnus-data-number (car data)) articles))
7348           (setq data (cdr data)))
7349         (gnus-summary-limit articles))
7350     (gnus-summary-position-point)))
7351
7352 (defun gnus-summary-limit-to-score (score)
7353   "Limit to articles with score at or above SCORE."
7354   (interactive "NLimit to articles with score of at least: ")
7355   (let ((data gnus-newsgroup-data)
7356         articles)
7357     (while data
7358       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7359                 score)
7360         (push (gnus-data-number (car data)) articles))
7361       (setq data (cdr data)))
7362     (prog1
7363         (gnus-summary-limit articles)
7364       (gnus-summary-position-point))))
7365
7366 (defun gnus-summary-limit-include-thread (id)
7367   "Display all the hidden articles that is in the thread with ID in it.
7368 When called interactively, ID is the Message-ID of the current
7369 article."
7370   (interactive (list (mail-header-id (gnus-summary-article-header))))
7371   (let ((articles (gnus-articles-in-thread
7372                    (gnus-id-to-thread (gnus-root-id id)))))
7373     (prog1
7374         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7375       (gnus-summary-limit-include-matching-articles
7376        "subject"
7377        (regexp-quote (gnus-simplify-subject-re
7378                       (mail-header-subject (gnus-id-to-header id)))))
7379       (gnus-summary-position-point))))
7380
7381 (defun gnus-summary-limit-include-matching-articles (header regexp)
7382   "Display all the hidden articles that have HEADERs that match REGEXP."
7383   (interactive (list (read-string "Match on header: ")
7384                      (read-string "Regexp: ")))
7385   (let ((articles (gnus-find-matching-articles header regexp)))
7386     (prog1
7387         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7388       (gnus-summary-position-point))))
7389
7390 (defun gnus-summary-limit-include-dormant ()
7391   "Display all the hidden articles that are marked as dormant.
7392 Note that this command only works on a subset of the articles currently
7393 fetched for this group."
7394   (interactive)
7395   (unless gnus-newsgroup-dormant
7396     (error "There are no dormant articles in this group"))
7397   (prog1
7398       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7399     (gnus-summary-position-point)))
7400
7401 (defun gnus-summary-limit-exclude-dormant ()
7402   "Hide all dormant articles."
7403   (interactive)
7404   (prog1
7405       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7406     (gnus-summary-position-point)))
7407
7408 (defun gnus-summary-limit-exclude-childless-dormant ()
7409   "Hide all dormant articles that have no children."
7410   (interactive)
7411   (let ((data (gnus-data-list t))
7412         articles d children)
7413     ;; Find all articles that are either not dormant or have
7414     ;; children.
7415     (while (setq d (pop data))
7416       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7417                 (and (setq children
7418                            (gnus-article-children (gnus-data-number d)))
7419                      (let (found)
7420                        (while children
7421                          (when (memq (car children) articles)
7422                            (setq children nil
7423                                  found t))
7424                          (pop children))
7425                        found)))
7426         (push (gnus-data-number d) articles)))
7427     ;; Do the limiting.
7428     (prog1
7429         (gnus-summary-limit articles)
7430       (gnus-summary-position-point))))
7431
7432 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7433   "Mark all unread excluded articles as read.
7434 If ALL, mark even excluded ticked and dormants as read."
7435   (interactive "P")
7436   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7437   (let ((articles (gnus-sorted-ndifference
7438                    (sort
7439                     (mapcar (lambda (h) (mail-header-number h))
7440                             gnus-newsgroup-headers)
7441                     '<)
7442                    gnus-newsgroup-limit))
7443         article)
7444     (setq gnus-newsgroup-unreads
7445           (gnus-sorted-intersection gnus-newsgroup-unreads
7446                                     gnus-newsgroup-limit))
7447     (if all
7448         (setq gnus-newsgroup-dormant nil
7449               gnus-newsgroup-marked nil
7450               gnus-newsgroup-reads
7451               (nconc
7452                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7453                gnus-newsgroup-reads))
7454       (while (setq article (pop articles))
7455         (unless (or (memq article gnus-newsgroup-dormant)
7456                     (memq article gnus-newsgroup-marked))
7457           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7458
7459 (defun gnus-summary-limit (articles &optional pop)
7460   (if pop
7461       ;; We pop the previous limit off the stack and use that.
7462       (setq articles (car gnus-newsgroup-limits)
7463             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7464     ;; We use the new limit, so we push the old limit on the stack.
7465     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7466   ;; Set the limit.
7467   (setq gnus-newsgroup-limit articles)
7468   (let ((total (length gnus-newsgroup-data))
7469         (data (gnus-data-find-list (gnus-summary-article-number)))
7470         (gnus-summary-mark-below nil)   ; Inhibit this.
7471         found)
7472     ;; This will do all the work of generating the new summary buffer
7473     ;; according to the new limit.
7474     (gnus-summary-prepare)
7475     ;; Hide any threads, possibly.
7476     (gnus-summary-maybe-hide-threads)
7477     ;; Try to return to the article you were at, or one in the
7478     ;; neighborhood.
7479     (when data
7480       ;; We try to find some article after the current one.
7481       (while data
7482         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7483           (setq data nil
7484                 found t))
7485         (setq data (cdr data))))
7486     (unless found
7487       ;; If there is no data, that means that we were after the last
7488       ;; article.  The same goes when we can't find any articles
7489       ;; after the current one.
7490       (goto-char (point-max))
7491       (gnus-summary-find-prev))
7492     (gnus-set-mode-line 'summary)
7493     ;; We return how many articles were removed from the summary
7494     ;; buffer as a result of the new limit.
7495     (- total (length gnus-newsgroup-data))))
7496
7497 (defsubst gnus-invisible-cut-children (threads)
7498   (let ((num 0))
7499     (while threads
7500       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7501         (incf num))
7502       (pop threads))
7503     (< num 2)))
7504
7505 (defsubst gnus-cut-thread (thread)
7506   "Go forwards in the thread until we find an article that we want to display."
7507   (when (or (eq gnus-fetch-old-headers 'some)
7508             (eq gnus-fetch-old-headers 'invisible)
7509             (numberp gnus-fetch-old-headers)
7510             (eq gnus-build-sparse-threads 'some)
7511             (eq gnus-build-sparse-threads 'more))
7512     ;; Deal with old-fetched headers and sparse threads.
7513     (while (and
7514             thread
7515             (or
7516              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7517              (gnus-summary-article-ancient-p
7518               (mail-header-number (car thread))))
7519             (if (or (<= (length (cdr thread)) 1)
7520                     (eq gnus-fetch-old-headers 'invisible))
7521                 (setq gnus-newsgroup-limit
7522                       (delq (mail-header-number (car thread))
7523                             gnus-newsgroup-limit)
7524                       thread (cadr thread))
7525               (when (gnus-invisible-cut-children (cdr thread))
7526                 (let ((th (cdr thread)))
7527                   (while th
7528                     (if (memq (mail-header-number (caar th))
7529                               gnus-newsgroup-limit)
7530                         (setq thread (car th)
7531                               th nil)
7532                       (setq th (cdr th))))))))))
7533   thread)
7534
7535 (defun gnus-cut-threads (threads)
7536   "Cut off all uninteresting articles from the beginning of threads."
7537   (when (or (eq gnus-fetch-old-headers 'some)
7538             (eq gnus-fetch-old-headers 'invisible)
7539             (numberp gnus-fetch-old-headers)
7540             (eq gnus-build-sparse-threads 'some)
7541             (eq gnus-build-sparse-threads 'more))
7542     (let ((th threads))
7543       (while th
7544         (setcar th (gnus-cut-thread (car th)))
7545         (setq th (cdr th)))))
7546   ;; Remove nixed out threads.
7547   (delq nil threads))
7548
7549 (defun gnus-summary-initial-limit (&optional show-if-empty)
7550   "Figure out what the initial limit is supposed to be on group entry.
7551 This entails weeding out unwanted dormants, low-scored articles,
7552 fetch-old-headers verbiage, and so on."
7553   ;; Most groups have nothing to remove.
7554   (if (or gnus-inhibit-limiting
7555           (and (null gnus-newsgroup-dormant)
7556                (eq gnus-newsgroup-display 'gnus-not-ignore)
7557                (not (eq gnus-fetch-old-headers 'some))
7558                (not (numberp gnus-fetch-old-headers))
7559                (not (eq gnus-fetch-old-headers 'invisible))
7560                (null gnus-summary-expunge-below)
7561                (not (eq gnus-build-sparse-threads 'some))
7562                (not (eq gnus-build-sparse-threads 'more))
7563                (null gnus-thread-expunge-below)
7564                (not gnus-use-nocem)))
7565       ()                                ; Do nothing.
7566     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7567     (setq gnus-newsgroup-limit nil)
7568     (mapatoms
7569      (lambda (node)
7570        (unless (car (symbol-value node))
7571          ;; These threads have no parents -- they are roots.
7572          (let ((nodes (cdr (symbol-value node)))
7573                thread)
7574            (while nodes
7575              (if (and gnus-thread-expunge-below
7576                       (< (gnus-thread-total-score (car nodes))
7577                          gnus-thread-expunge-below))
7578                  (gnus-expunge-thread (pop nodes))
7579                (setq thread (pop nodes))
7580                (gnus-summary-limit-children thread))))))
7581      gnus-newsgroup-dependencies)
7582     ;; If this limitation resulted in an empty group, we might
7583     ;; pop the previous limit and use it instead.
7584     (when (and (not gnus-newsgroup-limit)
7585                show-if-empty)
7586       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7587     gnus-newsgroup-limit))
7588
7589 (defun gnus-summary-limit-children (thread)
7590   "Return 1 if this subthread is visible and 0 if it is not."
7591   ;; First we get the number of visible children to this thread.  This
7592   ;; is done by recursing down the thread using this function, so this
7593   ;; will really go down to a leaf article first, before slowly
7594   ;; working its way up towards the root.
7595   (when thread
7596     (let ((children
7597            (if (cdr thread)
7598                (apply '+ (mapcar 'gnus-summary-limit-children
7599                                  (cdr thread)))
7600              0))
7601           (number (mail-header-number (car thread)))
7602           score)
7603       (if (and
7604            (not (memq number gnus-newsgroup-marked))
7605            (or
7606             ;; If this article is dormant and has absolutely no visible
7607             ;; children, then this article isn't visible.
7608             (and (memq number gnus-newsgroup-dormant)
7609                  (zerop children))
7610             ;; If this is "fetch-old-headered" and there is no
7611             ;; visible children, then we don't want this article.
7612             (and (or (eq gnus-fetch-old-headers 'some)
7613                      (numberp gnus-fetch-old-headers))
7614                  (gnus-summary-article-ancient-p number)
7615                  (zerop children))
7616             ;; If this is "fetch-old-headered" and `invisible', then
7617             ;; we don't want this article.
7618             (and (eq gnus-fetch-old-headers 'invisible)
7619                  (gnus-summary-article-ancient-p number))
7620             ;; If this is a sparsely inserted article with no children,
7621             ;; we don't want it.
7622             (and (eq gnus-build-sparse-threads 'some)
7623                  (gnus-summary-article-sparse-p number)
7624                  (zerop children))
7625             ;; If we use expunging, and this article is really
7626             ;; low-scored, then we don't want this article.
7627             (when (and gnus-summary-expunge-below
7628                        (< (setq score
7629                                 (or (cdr (assq number gnus-newsgroup-scored))
7630                                     gnus-summary-default-score))
7631                           gnus-summary-expunge-below))
7632               ;; We increase the expunge-tally here, but that has
7633               ;; nothing to do with the limits, really.
7634               (incf gnus-newsgroup-expunged-tally)
7635               ;; We also mark as read here, if that's wanted.
7636               (when (and gnus-summary-mark-below
7637                          (< score gnus-summary-mark-below))
7638                 (setq gnus-newsgroup-unreads
7639                       (delq number gnus-newsgroup-unreads))
7640                 (if gnus-newsgroup-auto-expire
7641                     (push number gnus-newsgroup-expirable)
7642                   (push (cons number gnus-low-score-mark)
7643                         gnus-newsgroup-reads)))
7644               t)
7645             ;; Do the `display' group parameter.
7646             (and gnus-newsgroup-display
7647                  (not (funcall gnus-newsgroup-display)))
7648             ;; Check NoCeM things.
7649             (if (and gnus-use-nocem
7650                      (gnus-nocem-unwanted-article-p
7651                       (mail-header-id (car thread))))
7652                 (progn
7653                   (setq gnus-newsgroup-unreads
7654                         (delq number gnus-newsgroup-unreads))
7655                   t))))
7656           ;; Nope, invisible article.
7657           0
7658         ;; Ok, this article is to be visible, so we add it to the limit
7659         ;; and return 1.
7660         (push number gnus-newsgroup-limit)
7661         1))))
7662
7663 (defun gnus-expunge-thread (thread)
7664   "Mark all articles in THREAD as read."
7665   (let* ((number (mail-header-number (car thread))))
7666     (incf gnus-newsgroup-expunged-tally)
7667     ;; We also mark as read here, if that's wanted.
7668     (setq gnus-newsgroup-unreads
7669           (delq number gnus-newsgroup-unreads))
7670     (if gnus-newsgroup-auto-expire
7671         (push number gnus-newsgroup-expirable)
7672       (push (cons number gnus-low-score-mark)
7673             gnus-newsgroup-reads)))
7674   ;; Go recursively through all subthreads.
7675   (mapcar 'gnus-expunge-thread (cdr thread)))
7676
7677 ;; Summary article oriented commands
7678
7679 (defun gnus-summary-refer-parent-article (n)
7680   "Refer parent article N times.
7681 If N is negative, go to ancestor -N instead.
7682 The difference between N and the number of articles fetched is returned."
7683   (interactive "p")
7684   (let ((skip 1)
7685         error header ref)
7686     (when (not (natnump n))
7687       (setq skip (abs n)
7688             n 1))
7689     (while (and (> n 0)
7690                 (not error))
7691       (setq header (gnus-summary-article-header))
7692       (if (and (eq (mail-header-number header)
7693                    (cdr gnus-article-current))
7694                (equal gnus-newsgroup-name
7695                       (car gnus-article-current)))
7696           ;; If we try to find the parent of the currently
7697           ;; displayed article, then we take a look at the actual
7698           ;; References header, since this is slightly more
7699           ;; reliable than the References field we got from the
7700           ;; server.
7701           (save-excursion
7702             (set-buffer gnus-original-article-buffer)
7703             (nnheader-narrow-to-headers)
7704             (unless (setq ref (message-fetch-field "references"))
7705               (setq ref (message-fetch-field "in-reply-to")))
7706             (widen))
7707         (setq ref
7708               ;; It's not the current article, so we take a bet on
7709               ;; the value we got from the server.
7710               (mail-header-references header)))
7711       (if (and ref
7712                (not (equal ref "")))
7713           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7714             (gnus-message 1 "Couldn't find parent"))
7715         (gnus-message 1 "No references in article %d"
7716                       (gnus-summary-article-number))
7717         (setq error t))
7718       (decf n))
7719     (gnus-summary-position-point)
7720     n))
7721
7722 (defun gnus-summary-refer-references ()
7723   "Fetch all articles mentioned in the References header.
7724 Return the number of articles fetched."
7725   (interactive)
7726   (let ((ref (mail-header-references (gnus-summary-article-header)))
7727         (current (gnus-summary-article-number))
7728         (n 0))
7729     (if (or (not ref)
7730             (equal ref ""))
7731         (error "No References in the current article")
7732       ;; For each Message-ID in the References header...
7733       (while (string-match "<[^>]*>" ref)
7734         (incf n)
7735         ;; ... fetch that article.
7736         (gnus-summary-refer-article
7737          (prog1 (match-string 0 ref)
7738            (setq ref (substring ref (match-end 0))))))
7739       (gnus-summary-goto-subject current)
7740       (gnus-summary-position-point)
7741       n)))
7742
7743 (defun gnus-summary-refer-thread (&optional limit)
7744   "Fetch all articles in the current thread.
7745 If LIMIT (the numerical prefix), fetch that many old headers instead
7746 of what's specified by the `gnus-refer-thread-limit' variable."
7747   (interactive "P")
7748   (let ((id (mail-header-id (gnus-summary-article-header)))
7749         (limit (if limit (prefix-numeric-value limit)
7750                  gnus-refer-thread-limit)))
7751     ;; We want to fetch LIMIT *old* headers, but we also have to
7752     ;; re-fetch all the headers in the current buffer, because many of
7753     ;; them may be undisplayed.  So we adjust LIMIT.
7754     (when (numberp limit)
7755       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7756     (unless (eq gnus-fetch-old-headers 'invisible)
7757       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7758       ;; Retrieve the headers and read them in.
7759       (if (eq (gnus-retrieve-headers
7760                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7761               'nov)
7762           (gnus-build-all-threads)
7763         (error "Can't fetch thread from backends that don't support NOV"))
7764       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7765     (gnus-summary-limit-include-thread id)))
7766
7767 (defun gnus-summary-refer-article (message-id)
7768   "Fetch an article specified by MESSAGE-ID."
7769   (interactive "sMessage-ID: ")
7770   (when (and (stringp message-id)
7771              (not (zerop (length message-id))))
7772     ;; Construct the correct Message-ID if necessary.
7773     ;; Suggested by tale@pawl.rpi.edu.
7774     (unless (string-match "^<" message-id)
7775       (setq message-id (concat "<" message-id)))
7776     (unless (string-match ">$" message-id)
7777       (setq message-id (concat message-id ">")))
7778     (let* ((header (gnus-id-to-header message-id))
7779            (sparse (and header
7780                         (gnus-summary-article-sparse-p
7781                          (mail-header-number header))
7782                         (memq (mail-header-number header)
7783                               gnus-newsgroup-limit)))
7784            number)
7785       (cond
7786        ;; If the article is present in the buffer we just go to it.
7787        ((and header
7788              (or (not (gnus-summary-article-sparse-p
7789                        (mail-header-number header)))
7790                  sparse))
7791         (prog1
7792             (gnus-summary-goto-article
7793              (mail-header-number header) nil t)
7794           (when sparse
7795             (gnus-summary-update-article (mail-header-number header)))))
7796        (t
7797         ;; We fetch the article.
7798         (catch 'found
7799           (dolist (gnus-override-method (gnus-refer-article-methods))
7800             (gnus-check-server gnus-override-method)
7801             ;; Fetch the header, and display the article.
7802             (when (setq number (gnus-summary-insert-subject message-id))
7803               (gnus-summary-select-article nil nil nil number)
7804               (throw 'found t)))
7805           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7806
7807 (defun gnus-refer-article-methods ()
7808   "Return a list of referrable methods."
7809   (cond
7810    ;; No method, so we default to current and native.
7811    ((null gnus-refer-article-method)
7812     (list gnus-current-select-method gnus-select-method))
7813    ;; Current.
7814    ((eq 'current gnus-refer-article-method)
7815     (list gnus-current-select-method))
7816    ;; List of select methods.
7817    ((not (and (symbolp (car gnus-refer-article-method))
7818               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7819     (let (out)
7820       (dolist (method gnus-refer-article-method)
7821         (push (if (eq 'current method)
7822                   gnus-current-select-method
7823                 method)
7824               out))
7825       (nreverse out)))
7826    ;; One single select method.
7827    (t
7828     (list gnus-refer-article-method))))
7829
7830 (defun gnus-summary-edit-parameters ()
7831   "Edit the group parameters of the current group."
7832   (interactive)
7833   (gnus-group-edit-group gnus-newsgroup-name 'params))
7834
7835 (defun gnus-summary-customize-parameters ()
7836   "Customize the group parameters of the current group."
7837   (interactive)
7838   (gnus-group-customize gnus-newsgroup-name))
7839
7840 (defun gnus-summary-enter-digest-group (&optional force)
7841   "Enter an nndoc group based on the current article.
7842 If FORCE, force a digest interpretation.  If not, try
7843 to guess what the document format is."
7844   (interactive "P")
7845   (let ((conf gnus-current-window-configuration))
7846     (save-excursion
7847       (gnus-summary-select-article))
7848     (setq gnus-current-window-configuration conf)
7849     (let* ((name (format "%s-%d"
7850                          (gnus-group-prefixed-name
7851                           gnus-newsgroup-name (list 'nndoc ""))
7852                          (save-excursion
7853                            (set-buffer gnus-summary-buffer)
7854                            gnus-current-article)))
7855            (ogroup gnus-newsgroup-name)
7856            (params (append (gnus-info-params (gnus-get-info ogroup))
7857                            (list (cons 'to-group ogroup))
7858                            (list (cons 'save-article-group ogroup))))
7859            (case-fold-search t)
7860            (buf (current-buffer))
7861            dig to-address)
7862       (save-excursion
7863         (set-buffer gnus-original-article-buffer)
7864         ;; Have the digest group inherit the main mail address of
7865         ;; the parent article.
7866         (when (setq to-address (or (gnus-fetch-field "reply-to")
7867                                    (gnus-fetch-field "from")))
7868           (setq params (append
7869                         (list (cons 'to-address
7870                                     (funcall gnus-decode-encoded-word-function
7871                                              to-address))))))
7872         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7873         (insert-buffer-substring gnus-original-article-buffer)
7874         ;; Remove lines that may lead nndoc to misinterpret the
7875         ;; document type.
7876         (narrow-to-region
7877          (goto-char (point-min))
7878          (or (search-forward "\n\n" nil t) (point)))
7879         (goto-char (point-min))
7880         (delete-matching-lines "^Path:\\|^From ")
7881         (widen))
7882       (unwind-protect
7883           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7884                     (gnus-newsgroup-ephemeral-ignored-charsets
7885                      gnus-newsgroup-ignored-charsets))
7886                 (gnus-group-read-ephemeral-group
7887                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7888                               (nndoc-article-type
7889                                ,(if force 'mbox 'guess)))
7890                  t nil nil nil
7891                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7892                                                         "ADAPT")))))
7893               ;; Make all postings to this group go to the parent group.
7894               (nconc (gnus-info-params (gnus-get-info name))
7895                      params)
7896             ;; Couldn't select this doc group.
7897             (switch-to-buffer buf)
7898             (gnus-set-global-variables)
7899             (gnus-configure-windows 'summary)
7900             (gnus-message 3 "Article couldn't be entered?"))
7901         (kill-buffer dig)))))
7902
7903 (defun gnus-summary-read-document (n)
7904   "Open a new group based on the current article(s).
7905 This will allow you to read digests and other similar
7906 documents as newsgroups.
7907 Obeys the standard process/prefix convention."
7908   (interactive "P")
7909   (let* ((articles (gnus-summary-work-articles n))
7910          (ogroup gnus-newsgroup-name)
7911          (params (append (gnus-info-params (gnus-get-info ogroup))
7912                          (list (cons 'to-group ogroup))))
7913          article group egroup groups vgroup)
7914     (while (setq article (pop articles))
7915       (setq group (format "%s-%d" gnus-newsgroup-name article))
7916       (gnus-summary-remove-process-mark article)
7917       (when (gnus-summary-display-article article)
7918         (save-excursion
7919           (with-temp-buffer
7920             (insert-buffer-substring gnus-original-article-buffer)
7921             ;; Remove some headers that may lead nndoc to make
7922             ;; the wrong guess.
7923             (message-narrow-to-head)
7924             (goto-char (point-min))
7925             (delete-matching-lines "^\\(Path\\):\\|^From ")
7926             (widen)
7927             (if (setq egroup
7928                       (gnus-group-read-ephemeral-group
7929                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7930                                      (nndoc-article-type guess))
7931                        t nil t))
7932                 (progn
7933             ;; Make all postings to this group go to the parent group.
7934                   (nconc (gnus-info-params (gnus-get-info egroup))
7935                          params)
7936                   (push egroup groups))
7937               ;; Couldn't select this doc group.
7938               (gnus-error 3 "Article couldn't be entered"))))))
7939     ;; Now we have selected all the documents.
7940     (cond
7941      ((not groups)
7942       (error "None of the articles could be interpreted as documents"))
7943      ((gnus-group-read-ephemeral-group
7944        (setq vgroup (format
7945                      "nnvirtual:%s-%s" gnus-newsgroup-name
7946                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7947        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7948        t
7949        (cons (current-buffer) 'summary)))
7950      (t
7951       (error "Couldn't select virtual nndoc group")))))
7952
7953 (defun gnus-summary-isearch-article (&optional regexp-p)
7954   "Do incremental search forward on the current article.
7955 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7956   (interactive "P")
7957   (gnus-summary-select-article)
7958   (gnus-configure-windows 'article)
7959   (gnus-eval-in-buffer-window gnus-article-buffer
7960     (save-restriction
7961       (widen)
7962       (isearch-forward regexp-p))))
7963
7964 (defun gnus-summary-search-article-forward (regexp &optional backward)
7965   "Search for an article containing REGEXP forward.
7966 If BACKWARD, search backward instead."
7967   (interactive
7968    (list (read-string
7969           (format "Search article %s (regexp%s): "
7970                   (if current-prefix-arg "backward" "forward")
7971                   (if gnus-last-search-regexp
7972                       (concat ", default " gnus-last-search-regexp)
7973                     "")))
7974          current-prefix-arg))
7975   (if (string-equal regexp "")
7976       (setq regexp (or gnus-last-search-regexp ""))
7977     (setq gnus-last-search-regexp regexp)
7978     (setq gnus-article-before-search gnus-current-article))
7979   ;; Intentionally set gnus-last-article.
7980   (setq gnus-last-article gnus-article-before-search)
7981   (let ((gnus-last-article gnus-last-article))
7982     (if (gnus-summary-search-article regexp backward)
7983         (gnus-summary-show-thread)
7984       (error "Search failed: \"%s\"" regexp))))
7985
7986 (defun gnus-summary-search-article-backward (regexp)
7987   "Search for an article containing REGEXP backward."
7988   (interactive
7989    (list (read-string
7990           (format "Search article backward (regexp%s): "
7991                   (if gnus-last-search-regexp
7992                       (concat ", default " gnus-last-search-regexp)
7993                     "")))))
7994   (gnus-summary-search-article-forward regexp 'backward))
7995
7996 (defun gnus-summary-search-article (regexp &optional backward)
7997   "Search for an article containing REGEXP.
7998 Optional argument BACKWARD means do search for backward.
7999 `gnus-select-article-hook' is not called during the search."
8000   ;; We have to require this here to make sure that the following
8001   ;; dynamic binding isn't shadowed by autoloading.
8002   (require 'gnus-async)
8003   (require 'gnus-art)
8004   (let ((gnus-select-article-hook nil)  ;Disable hook.
8005         (gnus-article-prepare-hook nil)
8006         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8007         (gnus-use-article-prefetch nil)
8008         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8009         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8010         (sum (current-buffer))
8011         (gnus-display-mime-function nil)
8012         (found nil)
8013         point)
8014     (gnus-save-hidden-threads
8015       (gnus-summary-select-article)
8016       (set-buffer gnus-article-buffer)
8017       (goto-char (window-point (get-buffer-window (current-buffer))))
8018       (when backward
8019         (forward-line -1))
8020       (while (not found)
8021         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8022         (if (if backward
8023                 (re-search-backward regexp nil t)
8024               (re-search-forward regexp nil t))
8025             ;; We found the regexp.
8026             (progn
8027               (setq found 'found)
8028               (beginning-of-line)
8029               (set-window-start
8030                (get-buffer-window (current-buffer))
8031                (point))
8032               (forward-line 1)
8033               (set-window-point
8034                (get-buffer-window (current-buffer))
8035                (point))
8036               (set-buffer sum)
8037               (setq point (point)))
8038           ;; We didn't find it, so we go to the next article.
8039           (set-buffer sum)
8040           (setq found 'not)
8041           (while (eq found 'not)
8042             (if (not (if backward (gnus-summary-find-prev)
8043                        (gnus-summary-find-next)))
8044                 ;; No more articles.
8045                 (setq found t)
8046               ;; Select the next article and adjust point.
8047               (unless (gnus-summary-article-sparse-p
8048                        (gnus-summary-article-number))
8049                 (setq found nil)
8050                 (gnus-summary-select-article)
8051                 (set-buffer gnus-article-buffer)
8052                 (widen)
8053                 (goto-char (if backward (point-max) (point-min))))))))
8054       (gnus-message 7 ""))
8055     ;; Return whether we found the regexp.
8056     (when (eq found 'found)
8057       (goto-char point)
8058       (gnus-summary-show-thread)
8059       (gnus-summary-goto-subject gnus-current-article)
8060       (gnus-summary-position-point)
8061       t)))
8062
8063 (defun gnus-find-matching-articles (header regexp)
8064   "Return a list of all articles that match REGEXP on HEADER.
8065 This search includes all articles in the current group that Gnus has
8066 fetched headers for, whether they are displayed or not."
8067   (let ((articles nil)
8068         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8069         (case-fold-search t))
8070     (dolist (header gnus-newsgroup-headers)
8071       (when (string-match regexp (funcall func header))
8072         (push (mail-header-number header) articles)))
8073     (nreverse articles)))
8074
8075 (defun gnus-summary-find-matching (header regexp &optional backward unread
8076                                           not-case-fold not-matching)
8077   "Return a list of all articles that match REGEXP on HEADER.
8078 The search stars on the current article and goes forwards unless
8079 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8080 If UNREAD is non-nil, only unread articles will
8081 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8082 in the comparisons. If NOT-MATCHING, return a list of all articles that
8083 not match REGEXP on HEADER."
8084   (let ((case-fold-search (not not-case-fold))
8085         articles d func)
8086     (if (consp header)
8087         (if (eq (car header) 'extra)
8088             (setq func
8089                   `(lambda (h)
8090                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8091                          "")))
8092           (error "%s is an invalid header" header))
8093       (unless (fboundp (intern (concat "mail-header-" header)))
8094         (error "%s is not a valid header" header))
8095       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8096     (dolist (d (if (eq backward 'all)
8097                    gnus-newsgroup-data
8098                  (gnus-data-find-list
8099                   (gnus-summary-article-number)
8100                   (gnus-data-list backward))))
8101       (when (and (or (not unread)       ; We want all articles...
8102                      (gnus-data-unread-p d)) ; Or just unreads.
8103                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8104                  (if not-matching
8105                      (not (string-match
8106                            regexp
8107                            (funcall func (gnus-data-header d))))
8108                    (string-match regexp
8109                                  (funcall func (gnus-data-header d)))))
8110         (push (gnus-data-number d) articles))) ; Success!
8111     (nreverse articles)))
8112
8113 (defun gnus-summary-execute-command (header regexp command &optional backward)
8114   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8115 If HEADER is an empty string (or nil), the match is done on the entire
8116 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8117   (interactive
8118    (list (let ((completion-ignore-case t))
8119            (completing-read
8120             "Header name: "
8121             (mapcar (lambda (header) (list (format "%s" header)))
8122                     (append
8123                      '("Number" "Subject" "From" "Lines" "Date"
8124                        "Message-ID" "Xref" "References" "Body")
8125                      gnus-extra-headers))
8126             nil 'require-match))
8127          (read-string "Regexp: ")
8128          (read-key-sequence "Command: ")
8129          current-prefix-arg))
8130   (when (equal header "Body")
8131     (setq header ""))
8132   ;; Hidden thread subtrees must be searched as well.
8133   (gnus-summary-show-all-threads)
8134   ;; We don't want to change current point nor window configuration.
8135   (save-excursion
8136     (save-window-excursion
8137       (gnus-message 6 "Executing %s..." (key-description command))
8138 ;; We'd like to execute COMMAND interactively so as to give arguments.
8139       (gnus-execute header regexp
8140                     `(call-interactively ',(key-binding command))
8141                     backward)
8142       (gnus-message 6 "Executing %s...done" (key-description command)))))
8143
8144 (defun gnus-summary-beginning-of-article ()
8145   "Scroll the article back to the beginning."
8146   (interactive)
8147   (gnus-summary-select-article)
8148   (gnus-configure-windows 'article)
8149   (gnus-eval-in-buffer-window gnus-article-buffer
8150     (widen)
8151     (goto-char (point-min))
8152     (when gnus-page-broken
8153       (gnus-narrow-to-page))))
8154
8155 (defun gnus-summary-end-of-article ()
8156   "Scroll to the end of the article."
8157   (interactive)
8158   (gnus-summary-select-article)
8159   (gnus-configure-windows 'article)
8160   (gnus-eval-in-buffer-window gnus-article-buffer
8161     (widen)
8162     (goto-char (point-max))
8163     (recenter -3)
8164     (when gnus-page-broken
8165       (gnus-narrow-to-page))))
8166
8167 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8168   "Truncate to LEN and quote all \"(\"'s in STRING."
8169   (gnus-replace-in-string (if (and len (> (length string) len))
8170                               (substring string 0 len)
8171                             string)
8172                           "[()]" "\\\\\\&"))
8173
8174 (defun gnus-summary-print-article (&optional filename n)
8175   "Generate and print a PostScript image of the N next (mail) articles.
8176
8177 If N is negative, print the N previous articles.  If N is nil and articles
8178 have been marked with the process mark, print these instead.
8179
8180 If the optional first argument FILENAME is nil, send the image to the
8181 printer.  If FILENAME is a string, save the PostScript image in a file with
8182 that name.  If FILENAME is a number, prompt the user for the name of the file
8183 to save in."
8184   (interactive (list (ps-print-preprint current-prefix-arg)))
8185   (dolist (article (gnus-summary-work-articles n))
8186     (gnus-summary-select-article nil nil 'pseudo article)
8187     (gnus-eval-in-buffer-window gnus-article-buffer
8188       (gnus-print-buffer))
8189     (gnus-summary-remove-process-mark article))
8190   (ps-despool filename))
8191
8192 (defun gnus-print-buffer ()
8193   (let ((buffer (generate-new-buffer " *print*")))
8194     (unwind-protect
8195         (progn
8196           (copy-to-buffer buffer (point-min) (point-max))
8197           (set-buffer buffer)
8198           (gnus-article-delete-invisible-text)
8199           (gnus-remove-text-with-property 'gnus-decoration)
8200           (when (gnus-visual-p 'article-highlight 'highlight)
8201             ;; Copy-to-buffer doesn't copy overlay.  So redo
8202             ;; highlight.
8203             (let ((gnus-article-buffer buffer))
8204               (gnus-article-highlight-citation t)
8205               (gnus-article-highlight-signature)))
8206           (let ((ps-left-header
8207                  (list
8208                   (concat "("
8209                           (gnus-summary-print-truncate-and-quote
8210                            (mail-header-subject gnus-current-headers)
8211                            66) ")")
8212                   (concat "("
8213                           (gnus-summary-print-truncate-and-quote
8214                            (mail-header-from gnus-current-headers)
8215                            45) ")")))
8216                 (ps-right-header
8217                  (list
8218                   "/pagenumberstring load"
8219                   (concat "("
8220                           (mail-header-date gnus-current-headers) ")"))))
8221             (gnus-run-hooks 'gnus-ps-print-hook)
8222             (save-excursion
8223               (if window-system
8224                   (ps-spool-buffer-with-faces)
8225                 (ps-spool-buffer)))))
8226       (kill-buffer buffer))))
8227
8228 (defun gnus-summary-show-article (&optional arg)
8229   "Force redisplaying of the current article.
8230 If ARG (the prefix) is a number, show the article with the charset
8231 defined in `gnus-summary-show-article-charset-alist', or the charset
8232 input.
8233 If ARG (the prefix) is non-nil and not a number, show the raw article
8234 without any article massaging functions being run.  Normally, the key strokes
8235 are `C-u g'."
8236   (interactive "P")
8237   (cond
8238    ((numberp arg)
8239     (gnus-summary-show-article t)
8240     (let ((gnus-newsgroup-charset
8241            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8242                (mm-read-coding-system
8243                 "View as charset: " ;; actually it is coding system.
8244                 (save-excursion
8245                   (set-buffer gnus-article-buffer)
8246                   (mm-detect-coding-region (point) (point-max))))))
8247           (gnus-newsgroup-ignored-charsets 'gnus-all))
8248       (gnus-summary-select-article nil 'force)
8249       (let ((deps gnus-newsgroup-dependencies)
8250             head header lines)
8251         (save-excursion
8252           (set-buffer gnus-original-article-buffer)
8253           (save-restriction
8254             (message-narrow-to-head)
8255             (setq head (buffer-string))
8256             (goto-char (point-min))
8257             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8258               (goto-char (point-max))
8259               (widen)
8260               (setq lines (1- (count-lines (point) (point-max))))))
8261           (with-temp-buffer
8262             (insert (format "211 %d Article retrieved.\n"
8263                             (cdr gnus-article-current)))
8264             (insert head)
8265             (if lines (insert (format "Lines: %d\n" lines)))
8266             (insert ".\n")
8267             (let ((nntp-server-buffer (current-buffer)))
8268               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8269         (gnus-data-set-header
8270          (gnus-data-find (cdr gnus-article-current))
8271          header)
8272         (gnus-summary-update-article-line
8273          (cdr gnus-article-current) header)
8274         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8275           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8276    ((not arg)
8277     ;; Select the article the normal way.
8278     (gnus-summary-select-article nil 'force))
8279    (t
8280     ;; We have to require this here to make sure that the following
8281     ;; dynamic binding isn't shadowed by autoloading.
8282     (require 'gnus-async)
8283     (require 'gnus-art)
8284     ;; Bind the article treatment functions to nil.
8285     (let ((gnus-have-all-headers t)
8286           gnus-article-prepare-hook
8287           gnus-article-decode-hook
8288           gnus-display-mime-function
8289           gnus-break-pages)
8290       ;; Destroy any MIME parts.
8291       (when (gnus-buffer-live-p gnus-article-buffer)
8292         (save-excursion
8293           (set-buffer gnus-article-buffer)
8294           (mm-destroy-parts gnus-article-mime-handles)
8295           ;; Set it to nil for safety reason.
8296           (setq gnus-article-mime-handle-alist nil)
8297           (setq gnus-article-mime-handles nil)))
8298       (gnus-summary-select-article nil 'force))))
8299   (gnus-summary-goto-subject gnus-current-article)
8300   (gnus-summary-position-point))
8301
8302 (defun gnus-summary-show-raw-article ()
8303   "Show the raw article without any article massaging functions being run."
8304   (interactive)
8305   (gnus-summary-show-article t))
8306
8307 (defun gnus-summary-verbose-headers (&optional arg)
8308   "Toggle permanent full header display.
8309 If ARG is a positive number, turn header display on.
8310 If ARG is a negative number, turn header display off."
8311   (interactive "P")
8312   (setq gnus-show-all-headers
8313         (cond ((or (not (numberp arg))
8314                    (zerop arg))
8315                (not gnus-show-all-headers))
8316               ((natnump arg)
8317                t)))
8318   (gnus-summary-show-article))
8319
8320 (defun gnus-summary-toggle-header (&optional arg)
8321   "Show the headers if they are hidden, or hide them if they are shown.
8322 If ARG is a positive number, show the entire header.
8323 If ARG is a negative number, hide the unwanted header lines."
8324   (interactive "P")
8325   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8326                      (get-buffer-window gnus-article-buffer t))))
8327     (when window
8328       (with-current-buffer gnus-article-buffer
8329         (widen)
8330         (article-narrow-to-head)
8331         (let* ((buffer-read-only nil)
8332                (inhibit-point-motion-hooks t)
8333                (hidden (if (numberp arg)
8334                            (>= arg 0)
8335                          (gnus-article-hidden-text-p 'headers)))
8336                s e)
8337           (delete-region (point-min) (point-max))
8338           (with-current-buffer gnus-original-article-buffer
8339             (goto-char (setq s (point-min)))
8340             (setq e (if (search-forward "\n\n" nil t)
8341                         (1- (point))
8342                       (point-max))))
8343           (insert-buffer-substring gnus-original-article-buffer s e)
8344           (article-decode-encoded-words)
8345           (if hidden
8346               (let ((gnus-treat-hide-headers nil)
8347                     (gnus-treat-hide-boring-headers nil))
8348                 (gnus-delete-wash-type 'headers)
8349                 (gnus-treat-article 'head))
8350             (gnus-treat-article 'head))
8351           (widen)
8352           (set-window-start window (goto-char (point-min)))
8353           (setq gnus-page-broken
8354                 (when gnus-break-pages
8355                   (gnus-narrow-to-page)
8356                   t))
8357           (gnus-set-mode-line 'article))))))
8358
8359 (defun gnus-summary-show-all-headers ()
8360   "Make all header lines visible."
8361   (interactive)
8362   (gnus-summary-toggle-header 1))
8363
8364 (defun gnus-summary-caesar-message (&optional arg)
8365   "Caesar rotate the current article by 13.
8366 The numerical prefix specifies how many places to rotate each letter
8367 forward."
8368   (interactive "P")
8369   (gnus-summary-select-article)
8370   (let ((mail-header-separator ""))
8371     (gnus-eval-in-buffer-window gnus-article-buffer
8372       (save-restriction
8373         (widen)
8374         (let ((start (window-start))
8375               buffer-read-only)
8376           (message-caesar-buffer-body arg)
8377           (set-window-start (get-buffer-window (current-buffer)) start))))))
8378
8379 (defun gnus-summary-stop-page-breaking ()
8380   "Stop page breaking in the current article."
8381   (interactive)
8382   (gnus-summary-select-article)
8383   (gnus-eval-in-buffer-window gnus-article-buffer
8384     (widen)
8385     (when (gnus-visual-p 'page-marker)
8386       (let ((buffer-read-only nil))
8387         (gnus-remove-text-with-property 'gnus-prev)
8388         (gnus-remove-text-with-property 'gnus-next))
8389       (setq gnus-page-broken nil))))
8390
8391 (defun gnus-summary-move-article (&optional n to-newsgroup
8392                                             select-method action)
8393   "Move the current article to a different newsgroup.
8394 If N is a positive number, move the N next articles.
8395 If N is a negative number, move the N previous articles.
8396 If N is nil and any articles have been marked with the process mark,
8397 move those articles instead.
8398 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8399 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8400 re-spool using this method.
8401
8402 When called interactively with TO-NEWSGROUP being nil, the value of
8403 the variable `gnus-move-split-methods' is used for finding a default
8404 for the target newsgroup.
8405
8406 For this function to work, both the current newsgroup and the
8407 newsgroup that you want to move to have to support the `request-move'
8408 and `request-accept' functions.
8409
8410 ACTION can be either `move' (the default), `crosspost' or `copy'."
8411   (interactive "P")
8412   (unless action
8413     (setq action 'move))
8414   ;; Check whether the source group supports the required functions.
8415   (cond ((and (eq action 'move)
8416               (not (gnus-check-backend-function
8417                     'request-move-article gnus-newsgroup-name)))
8418          (error "The current group does not support article moving"))
8419         ((and (eq action 'crosspost)
8420               (not (gnus-check-backend-function
8421                     'request-replace-article gnus-newsgroup-name)))
8422          (error "The current group does not support article editing")))
8423   (let ((articles (gnus-summary-work-articles n))
8424         (prefix (if (gnus-check-backend-function
8425                      'request-move-article gnus-newsgroup-name)
8426                     (gnus-group-real-prefix gnus-newsgroup-name)
8427                   ""))
8428         (names '((move "Move" "Moving")
8429                  (copy "Copy" "Copying")
8430                  (crosspost "Crosspost" "Crossposting")))
8431         (copy-buf (save-excursion
8432                     (nnheader-set-temp-buffer " *copy article*")))
8433         art-group to-method new-xref article to-groups)
8434     (unless (assq action names)
8435       (error "Unknown action %s" action))
8436     ;; Read the newsgroup name.
8437     (when (and (not to-newsgroup)
8438                (not select-method))
8439       (if (and gnus-move-split-methods
8440                (not
8441                 (and (memq gnus-current-article articles)
8442                      (gnus-buffer-live-p gnus-original-article-buffer))))
8443           ;; When `gnus-move-split-methods' is non-nil, we have to
8444           ;; select an article to give `gnus-read-move-group-name' an
8445           ;; opportunity to suggest an appropriate default.  However,
8446           ;; we needn't render or mark the article.
8447           (let ((gnus-display-mime-function nil)
8448                 (gnus-article-prepare-hook nil)
8449                 (gnus-mark-article-hook nil))
8450             (gnus-summary-select-article nil nil nil (car articles))))
8451       (setq to-newsgroup
8452             (gnus-read-move-group-name
8453              (cadr (assq action names))
8454              (symbol-value (intern (format "gnus-current-%s-group" action)))
8455              articles prefix))
8456       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8457     (setq to-method (or select-method
8458                         (gnus-server-to-method
8459                          (gnus-group-method to-newsgroup))))
8460     ;; Check the method we are to move this article to...
8461     (unless (gnus-check-backend-function
8462              'request-accept-article (car to-method))
8463       (error "%s does not support article copying" (car to-method)))
8464     (unless (gnus-check-server to-method)
8465       (error "Can't open server %s" (car to-method)))
8466     (gnus-message 6 "%s to %s: %s..."
8467                   (caddr (assq action names))
8468                   (or (car select-method) to-newsgroup) articles)
8469     (while articles
8470       (setq article (pop articles))
8471       (setq
8472        art-group
8473        (cond
8474         ;; Move the article.
8475         ((eq action 'move)
8476          ;; Remove this article from future suppression.
8477          (gnus-dup-unsuppress-article article)
8478          (gnus-request-move-article
8479           article                       ; Article to move
8480           gnus-newsgroup-name           ; From newsgroup
8481           (nth 1 (gnus-find-method-for-group
8482                   gnus-newsgroup-name)) ; Server
8483           (list 'gnus-request-accept-article
8484                 to-newsgroup (list 'quote select-method)
8485                 (not articles) t)       ; Accept form
8486           (not articles)))              ; Only save nov last time
8487         ;; Copy the article.
8488         ((eq action 'copy)
8489          (save-excursion
8490            (set-buffer copy-buf)
8491            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8492              (gnus-request-accept-article
8493               to-newsgroup select-method (not articles) t))))
8494         ;; Crosspost the article.
8495         ((eq action 'crosspost)
8496          (let ((xref (message-tokenize-header
8497                       (mail-header-xref (gnus-summary-article-header article))
8498                       " ")))
8499            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8500                                   ":" (number-to-string article)))
8501            (unless xref
8502              (setq xref (list (system-name))))
8503            (setq new-xref
8504                  (concat
8505                   (mapconcat 'identity
8506                              (delete "Xref:" (delete new-xref xref))
8507                              " ")
8508                   " " new-xref))
8509            (save-excursion
8510              (set-buffer copy-buf)
8511              ;; First put the article in the destination group.
8512              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8513              (when (consp (setq art-group
8514                                 (gnus-request-accept-article
8515                                  to-newsgroup select-method (not articles))))
8516                (setq new-xref (concat new-xref " " (car art-group)
8517                                       ":"
8518                                       (number-to-string (cdr art-group))))
8519                ;; Now we have the new Xrefs header, so we insert
8520                ;; it and replace the new article.
8521                (nnheader-replace-header "Xref" new-xref)
8522                (gnus-request-replace-article
8523                 (cdr art-group) to-newsgroup (current-buffer))
8524                art-group))))))
8525       (cond
8526        ((not art-group)
8527         (gnus-message 1 "Couldn't %s article %s: %s"
8528                       (cadr (assq action names)) article
8529                       (nnheader-get-report (car to-method))))
8530        ((eq art-group 'junk)
8531         (when (eq action 'move)
8532           (gnus-summary-mark-article article gnus-canceled-mark)
8533           (gnus-message 4 "Deleted article %s" article)))
8534        (t
8535         (let* ((pto-group (gnus-group-prefixed-name
8536                            (car art-group) to-method))
8537                (entry
8538                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8539                (info (nth 2 entry))
8540                (to-group (gnus-info-group info))
8541                to-marks)
8542           ;; Update the group that has been moved to.
8543           (when (and info
8544                      (memq action '(move copy)))
8545             (unless (member to-group to-groups)
8546               (push to-group to-groups))
8547
8548             (unless (memq article gnus-newsgroup-unreads)
8549               (push 'read to-marks)
8550               (gnus-info-set-read
8551                info (gnus-add-to-range (gnus-info-read info)
8552                                        (list (cdr art-group)))))
8553
8554             ;; See whether the article is to be put in the cache.
8555             (let ((marks gnus-article-mark-lists)
8556                   (to-article (cdr art-group)))
8557
8558               ;; Enter the article into the cache in the new group,
8559               ;; if that is required.
8560               (when gnus-use-cache
8561                 (gnus-cache-possibly-enter-article
8562                  to-group to-article
8563                  (memq article gnus-newsgroup-marked)
8564                  (memq article gnus-newsgroup-dormant)
8565                  (memq article gnus-newsgroup-unreads)))
8566
8567               (when gnus-preserve-marks
8568                 ;; Copy any marks over to the new group.
8569                 (when (and (equal to-group gnus-newsgroup-name)
8570                            (not (memq article gnus-newsgroup-unreads)))
8571                   ;; Mark this article as read in this group.
8572                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8573                   (setcdr (gnus-active to-group) to-article)
8574                   (setcdr gnus-newsgroup-active to-article))
8575
8576                 (while marks
8577                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8578                     (when (memq article (symbol-value
8579                                          (intern (format "gnus-newsgroup-%s"
8580                                                          (caar marks)))))
8581                       (push (cdar marks) to-marks)
8582                       ;; If the other group is the same as this group,
8583                       ;; then we have to add the mark to the list.
8584                       (when (equal to-group gnus-newsgroup-name)
8585                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8586                              (cons to-article
8587                                    (symbol-value
8588                                     (intern (format "gnus-newsgroup-%s"
8589                                                     (caar marks)))))))
8590                       ;; Copy the marks to other group.
8591                       (gnus-add-marked-articles
8592                        to-group (cdar marks) (list to-article) info)))
8593                   (setq marks (cdr marks)))
8594
8595                 (gnus-request-set-mark to-group (list (list (list to-article)
8596                                                             'add
8597                                                             to-marks))))
8598
8599               (gnus-dribble-enter
8600                (concat "(gnus-group-set-info '"
8601                        (gnus-prin1-to-string (gnus-get-info to-group))
8602                        ")"))))
8603
8604           ;; Update the Xref header in this article to point to
8605           ;; the new crossposted article we have just created.
8606           (when (eq action 'crosspost)
8607             (save-excursion
8608               (set-buffer copy-buf)
8609               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8610               (nnheader-replace-header "Xref" new-xref)
8611               (gnus-request-replace-article
8612                article gnus-newsgroup-name (current-buffer)))))
8613
8614         ;;;!!!Why is this necessary?
8615         (set-buffer gnus-summary-buffer)
8616
8617         (gnus-summary-goto-subject article)
8618         (when (eq action 'move)
8619           (gnus-summary-mark-article article gnus-canceled-mark))))
8620       (gnus-summary-remove-process-mark article))
8621     ;; Re-activate all groups that have been moved to.
8622     (save-excursion
8623       (set-buffer gnus-group-buffer)
8624       (let ((gnus-group-marked to-groups))
8625         (gnus-group-get-new-news-this-group nil t)))
8626
8627     (gnus-kill-buffer copy-buf)
8628     (gnus-summary-position-point)
8629     (gnus-set-mode-line 'summary)))
8630
8631 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8632   "Move the current article to a different newsgroup.
8633 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8634 When called interactively, if TO-NEWSGROUP is nil, use the value of
8635 the variable `gnus-move-split-methods' for finding a default target
8636 newsgroup.
8637 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8638 re-spool using this method."
8639   (interactive "P")
8640   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8641
8642 (defun gnus-summary-crosspost-article (&optional n)
8643   "Crosspost the current article to some other group."
8644   (interactive "P")
8645   (gnus-summary-move-article n nil nil 'crosspost))
8646
8647 (defcustom gnus-summary-respool-default-method nil
8648   "Default method type for respooling an article.
8649 If nil, use to the current newsgroup method."
8650   :type 'symbol
8651   :group 'gnus-summary-mail)
8652
8653 (defun gnus-summary-respool-article (&optional n method)
8654   "Respool the current article.
8655 The article will be squeezed through the mail spooling process again,
8656 which means that it will be put in some mail newsgroup or other
8657 depending on `nnmail-split-methods'.
8658 If N is a positive number, respool the N next articles.
8659 If N is a negative number, respool the N previous articles.
8660 If N is nil and any articles have been marked with the process mark,
8661 respool those articles instead.
8662
8663 Respooling can be done both from mail groups and \"real\" newsgroups.
8664 In the former case, the articles in question will be moved from the
8665 current group into whatever groups they are destined to.  In the
8666 latter case, they will be copied into the relevant groups."
8667   (interactive
8668    (list current-prefix-arg
8669          (let* ((methods (gnus-methods-using 'respool))
8670                 (methname
8671                  (symbol-name (or gnus-summary-respool-default-method
8672                                   (car (gnus-find-method-for-group
8673                                         gnus-newsgroup-name)))))
8674                 (method
8675                  (gnus-completing-read-with-default
8676                   methname "What backend do you want to use when respooling?"
8677                   methods nil t nil 'gnus-mail-method-history))
8678                 ms)
8679            (cond
8680             ((zerop (length (setq ms (gnus-servers-using-backend
8681                                       (intern method)))))
8682              (list (intern method) ""))
8683             ((= 1 (length ms))
8684              (car ms))
8685             (t
8686              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8687                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8688                            ms-alist))))))))
8689   (unless method
8690     (error "No method given for respooling"))
8691   (if (assoc (symbol-name
8692               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8693              (gnus-methods-using 'respool))
8694       (gnus-summary-move-article n nil method)
8695     (gnus-summary-copy-article n nil method)))
8696
8697 (defun gnus-summary-import-article (file &optional edit)
8698   "Import an arbitrary file into a mail newsgroup."
8699   (interactive "fImport file: \nP")
8700   (let ((group gnus-newsgroup-name)
8701         (now (current-time))
8702         atts lines group-art)
8703     (unless (gnus-check-backend-function 'request-accept-article group)
8704       (error "%s does not support article importing" group))
8705     (or (file-readable-p file)
8706         (not (file-regular-p file))
8707         (error "Can't read %s" file))
8708     (save-excursion
8709       (set-buffer (gnus-get-buffer-create " *import file*"))
8710       (erase-buffer)
8711       (nnheader-insert-file-contents file)
8712       (goto-char (point-min))
8713       (if (nnheader-article-p)
8714           (save-restriction
8715             (goto-char (point-min))
8716             (search-forward "\n\n" nil t)
8717             (narrow-to-region (point-min) (1- (point)))
8718             (goto-char (point-min))
8719             (unless (re-search-forward "^date:" nil t)
8720               (goto-char (point-max))
8721               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8722        ;; This doesn't look like an article, so we fudge some headers.
8723         (setq atts (file-attributes file)
8724               lines (count-lines (point-min) (point-max)))
8725         (insert "From: " (read-string "From: ") "\n"
8726                 "Subject: " (read-string "Subject: ") "\n"
8727                 "Date: " (message-make-date (nth 5 atts)) "\n"
8728                 "Message-ID: " (message-make-message-id) "\n"
8729                 "Lines: " (int-to-string lines) "\n"
8730                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8731       (setq group-art (gnus-request-accept-article group nil t))
8732       (kill-buffer (current-buffer)))
8733     (setq gnus-newsgroup-active (gnus-activate-group group))
8734     (forward-line 1)
8735     (gnus-summary-goto-article (cdr group-art) nil t)
8736     (when edit
8737       (gnus-summary-edit-article))))
8738
8739 (defun gnus-summary-create-article ()
8740   "Create an article in a mail newsgroup."
8741   (interactive)
8742   (let ((group gnus-newsgroup-name)
8743         (now (current-time))
8744         group-art)
8745     (unless (gnus-check-backend-function 'request-accept-article group)
8746       (error "%s does not support article importing" group))
8747     (save-excursion
8748       (set-buffer (gnus-get-buffer-create " *import file*"))
8749       (erase-buffer)
8750       (goto-char (point-min))
8751       ;; This doesn't look like an article, so we fudge some headers.
8752       (insert "From: " (read-string "From: ") "\n"
8753               "Subject: " (read-string "Subject: ") "\n"
8754               "Date: " (message-make-date now) "\n"
8755               "Message-ID: " (message-make-message-id) "\n")
8756       (setq group-art (gnus-request-accept-article group nil t))
8757       (kill-buffer (current-buffer)))
8758     (setq gnus-newsgroup-active (gnus-activate-group group))
8759     (forward-line 1)
8760     (gnus-summary-goto-article (cdr group-art) nil t)
8761     (gnus-summary-edit-article)))
8762
8763 (defun gnus-summary-article-posted-p ()
8764   "Say whether the current (mail) article is available from news as well.
8765 This will be the case if the article has both been mailed and posted."
8766   (interactive)
8767   (let ((id (mail-header-references (gnus-summary-article-header)))
8768         (gnus-override-method (car (gnus-refer-article-methods))))
8769     (if (gnus-request-head id "")
8770         (gnus-message 2 "The current message was found on %s"
8771                       gnus-override-method)
8772       (gnus-message 2 "The current message couldn't be found on %s"
8773                     gnus-override-method)
8774       nil)))
8775
8776 (defun gnus-summary-expire-articles (&optional now)
8777   "Expire all articles that are marked as expirable in the current group."
8778   (interactive)
8779   (when (gnus-check-backend-function
8780          'request-expire-articles gnus-newsgroup-name)
8781     ;; This backend supports expiry.
8782     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8783            (expirable (if total
8784                           (progn
8785                             ;; We need to update the info for
8786                             ;; this group for `gnus-list-of-read-articles'
8787                             ;; to give us the right answer.
8788                             (gnus-run-hooks 'gnus-exit-group-hook)
8789                             (gnus-summary-update-info)
8790                             (gnus-list-of-read-articles gnus-newsgroup-name))
8791                         (setq gnus-newsgroup-expirable
8792                               (sort gnus-newsgroup-expirable '<))))
8793            (expiry-wait (if now 'immediate
8794                           (gnus-group-find-parameter
8795                            gnus-newsgroup-name 'expiry-wait)))
8796            (nnmail-expiry-target
8797             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8798                 nnmail-expiry-target))
8799            es)
8800       (when expirable
8801         ;; There are expirable articles in this group, so we run them
8802         ;; through the expiry process.
8803         (gnus-message 6 "Expiring articles...")
8804         (unless (gnus-check-group gnus-newsgroup-name)
8805           (error "Can't open server for %s" gnus-newsgroup-name))
8806         ;; The list of articles that weren't expired is returned.
8807         (save-excursion
8808           (if expiry-wait
8809               (let ((nnmail-expiry-wait-function nil)
8810                     (nnmail-expiry-wait expiry-wait))
8811                 (setq es (gnus-request-expire-articles
8812                           expirable gnus-newsgroup-name)))
8813             (setq es (gnus-request-expire-articles
8814                       expirable gnus-newsgroup-name)))
8815           (unless total
8816             (setq gnus-newsgroup-expirable es))
8817           ;; We go through the old list of expirable, and mark all
8818           ;; really expired articles as nonexistent.
8819           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8820             (let ((gnus-use-cache nil))
8821               (dolist (article expirable)
8822                 (when (and (not (memq article es))
8823                            (gnus-data-find article))
8824                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8825         (gnus-message 6 "Expiring articles...done")))))
8826
8827 (defun gnus-summary-expire-articles-now ()
8828   "Expunge all expirable articles in the current group.
8829 This means that *all* articles that are marked as expirable will be
8830 deleted forever, right now."
8831   (interactive)
8832   (or gnus-expert-user
8833       (gnus-yes-or-no-p
8834        "Are you really, really, really sure you want to delete all these messages? ")
8835       (error "Phew!"))
8836   (gnus-summary-expire-articles t))
8837
8838 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8839 (defun gnus-summary-delete-article (&optional n)
8840   "Delete the N next (mail) articles.
8841 This command actually deletes articles.  This is not a marking
8842 command.  The article will disappear forever from your life, never to
8843 return.
8844 If N is negative, delete backwards.
8845 If N is nil and articles have been marked with the process mark,
8846 delete these instead."
8847   (interactive "P")
8848   (unless (gnus-check-backend-function 'request-expire-articles
8849                                        gnus-newsgroup-name)
8850     (error "The current newsgroup does not support article deletion"))
8851   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8852     (error "Couldn't open server"))
8853   ;; Compute the list of articles to delete.
8854   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8855         (nnmail-expiry-target 'delete)
8856         not-deleted)
8857     (if (and gnus-novice-user
8858              (not (gnus-yes-or-no-p
8859                    (format "Do you really want to delete %s forever? "
8860                            (if (> (length articles) 1)
8861                                (format "these %s articles" (length articles))
8862                              "this article")))))
8863         ()
8864       ;; Delete the articles.
8865       (setq not-deleted (gnus-request-expire-articles
8866                          articles gnus-newsgroup-name 'force))
8867       (while articles
8868         (gnus-summary-remove-process-mark (car articles))
8869         ;; The backend might not have been able to delete the article
8870         ;; after all.
8871         (unless (memq (car articles) not-deleted)
8872           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8873         (setq articles (cdr articles)))
8874       (when not-deleted
8875         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8876     (gnus-summary-position-point)
8877     (gnus-set-mode-line 'summary)
8878     not-deleted))
8879
8880 (defun gnus-summary-edit-article (&optional arg)
8881   "Edit the current article.
8882 This will have permanent effect only in mail groups.
8883 If ARG is nil, edit the decoded articles.
8884 If ARG is 1, edit the raw articles.
8885 If ARG is 2, edit the raw articles even in read-only groups.
8886 If ARG is 3, edit the articles with the current handles.
8887 Otherwise, allow editing of articles even in read-only
8888 groups."
8889   (interactive "P")
8890   (let (force raw current-handles)
8891     (cond
8892      ((null arg))
8893      ((eq arg 1)
8894       (setq raw t))
8895      ((eq arg 2)
8896       (setq raw t
8897             force t))
8898      ((eq arg 3)
8899       (setq current-handles
8900             (and (gnus-buffer-live-p gnus-article-buffer)
8901                  (with-current-buffer gnus-article-buffer
8902                    (prog1
8903                        gnus-article-mime-handles
8904                      (setq gnus-article-mime-handles nil))))))
8905      (t
8906       (setq force t)))
8907     (when (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8908       (error "Can't edit the raw article in group nndraft:drafts"))
8909     (save-excursion
8910       (set-buffer gnus-summary-buffer)
8911       (let ((mail-parse-charset gnus-newsgroup-charset)
8912             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8913         (gnus-set-global-variables)
8914         (when (and (not force)
8915                    (gnus-group-read-only-p))
8916           (error "The current newsgroup does not support article editing"))
8917         (gnus-summary-show-article t)
8918         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8919           (with-current-buffer gnus-article-buffer
8920             (mm-enable-multibyte)))
8921         (if (equal gnus-newsgroup-name "nndraft:drafts")
8922             (setq raw t))
8923         (gnus-article-edit-article
8924          (if raw 'ignore
8925            `(lambda ()
8926               (let ((mbl mml-buffer-list))
8927                 (setq mml-buffer-list nil)
8928                 (mime-to-mml ,'current-handles)
8929                 (let ((mbl1 mml-buffer-list))
8930                   (setq mml-buffer-list mbl)
8931                   (set (make-local-variable 'mml-buffer-list) mbl1))
8932                 (make-local-hook 'kill-buffer-hook)
8933                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8934          `(lambda (no-highlight)
8935             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8936                   (message-options message-options)
8937                   (message-options-set-recipient)
8938                   (mail-parse-ignored-charsets
8939                    ',gnus-newsgroup-ignored-charsets))
8940               ,(if (not raw) '(progn
8941                                 (mml-to-mime)
8942                                 (mml-destroy-buffers)
8943                                 (remove-hook 'kill-buffer-hook
8944                                              'mml-destroy-buffers t)
8945                                 (kill-local-variable 'mml-buffer-list)))
8946               (gnus-summary-edit-article-done
8947                ,(or (mail-header-references gnus-current-headers) "")
8948                ,(gnus-group-read-only-p)
8949                ,gnus-summary-buffer no-highlight))))))))
8950
8951 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8952
8953 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8954                                                  no-highlight)
8955   "Make edits to the current article permanent."
8956   (interactive)
8957   (save-excursion
8958    ;; The buffer restriction contains the entire article if it exists.
8959     (when (article-goto-body)
8960       (let ((lines (count-lines (point) (point-max)))
8961             (length (- (point-max) (point)))
8962             (case-fold-search t)
8963             (body (copy-marker (point))))
8964         (goto-char (point-min))
8965         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8966           (delete-region (match-beginning 1) (match-end 1))
8967           (insert (number-to-string length)))
8968         (goto-char (point-min))
8969         (when (re-search-forward
8970                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8971           (delete-region (match-beginning 1) (match-end 1))
8972           (insert (number-to-string length)))
8973         (goto-char (point-min))
8974         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8975           (delete-region (match-beginning 1) (match-end 1))
8976           (insert (number-to-string lines))))))
8977   ;; Replace the article.
8978   (let ((buf (current-buffer)))
8979     (with-temp-buffer
8980       (insert-buffer-substring buf)
8981
8982       (if (and (not read-only)
8983                (not (gnus-request-replace-article
8984                      (cdr gnus-article-current) (car gnus-article-current)
8985                      (current-buffer) t)))
8986           (error "Couldn't replace article")
8987         ;; Update the summary buffer.
8988         (if (and references
8989                  (equal (message-tokenize-header references " ")
8990                         (message-tokenize-header
8991                          (or (message-fetch-field "references") "") " ")))
8992             ;; We only have to update this line.
8993             (save-excursion
8994               (save-restriction
8995                 (message-narrow-to-head)
8996                 (let ((head (buffer-string))
8997                       header)
8998                   (with-temp-buffer
8999                     (insert (format "211 %d Article retrieved.\n"
9000                                     (cdr gnus-article-current)))
9001                     (insert head)
9002                     (insert ".\n")
9003                     (let ((nntp-server-buffer (current-buffer)))
9004                       (setq header (car (gnus-get-newsgroup-headers
9005                                          nil t))))
9006                     (save-excursion
9007                       (set-buffer gnus-summary-buffer)
9008                       (gnus-data-set-header
9009                        (gnus-data-find (cdr gnus-article-current))
9010                        header)
9011                       (gnus-summary-update-article-line
9012                        (cdr gnus-article-current) header)
9013                       (if (gnus-summary-goto-subject
9014                            (cdr gnus-article-current) nil t)
9015                           (gnus-summary-update-secondary-mark
9016                            (cdr gnus-article-current))))))))
9017           ;; Update threads.
9018           (set-buffer (or buffer gnus-summary-buffer))
9019           (gnus-summary-update-article (cdr gnus-article-current))
9020           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9021               (gnus-summary-update-secondary-mark
9022                (cdr gnus-article-current))))
9023         ;; Prettify the article buffer again.
9024         (unless no-highlight
9025           (save-excursion
9026             (set-buffer gnus-article-buffer)
9027             ;;;!!! Fix this -- article should be rehighlighted.
9028             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9029             (set-buffer gnus-original-article-buffer)
9030             (gnus-request-article
9031              (cdr gnus-article-current)
9032              (car gnus-article-current) (current-buffer))))
9033         ;; Prettify the summary buffer line.
9034         (when (gnus-visual-p 'summary-highlight 'highlight)
9035           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9036
9037 (defun gnus-summary-edit-wash (key)
9038   "Perform editing command KEY in the article buffer."
9039   (interactive
9040    (list
9041     (progn
9042       (message "%s" (concat (this-command-keys) "- "))
9043       (read-char))))
9044   (message "")
9045   (gnus-summary-edit-article)
9046   (execute-kbd-macro (concat (this-command-keys) key))
9047   (gnus-article-edit-done))
9048
9049 ;;; Respooling
9050
9051 (defun gnus-summary-respool-query (&optional silent trace)
9052   "Query where the respool algorithm would put this article."
9053   (interactive)
9054   (let (gnus-mark-article-hook)
9055     (gnus-summary-select-article)
9056     (save-excursion
9057       (set-buffer gnus-original-article-buffer)
9058       (save-restriction
9059         (message-narrow-to-head)
9060         (let ((groups (nnmail-article-group 'identity trace)))
9061           (unless silent
9062             (if groups
9063                 (message "This message would go to %s"
9064                          (mapconcat 'car groups ", "))
9065               (message "This message would go to no groups"))
9066             groups))))))
9067
9068 (defun gnus-summary-respool-trace ()
9069   "Trace where the respool algorithm would put this article.
9070 Display a buffer showing all fancy splitting patterns which matched."
9071   (interactive)
9072   (gnus-summary-respool-query nil t))
9073
9074 ;; Summary marking commands.
9075
9076 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9077   "Mark articles which has the same subject as read, and then select the next.
9078 If UNMARK is positive, remove any kind of mark.
9079 If UNMARK is negative, tick articles."
9080   (interactive "P")
9081   (when unmark
9082     (setq unmark (prefix-numeric-value unmark)))
9083   (let ((count
9084          (gnus-summary-mark-same-subject
9085           (gnus-summary-article-subject) unmark)))
9086     ;; Select next unread article.  If auto-select-same mode, should
9087     ;; select the first unread article.
9088     (gnus-summary-next-article t (and gnus-auto-select-same
9089                                       (gnus-summary-article-subject)))
9090     (gnus-message 7 "%d article%s marked as %s"
9091                   count (if (= count 1) " is" "s are")
9092                   (if unmark "unread" "read"))))
9093
9094 (defun gnus-summary-kill-same-subject (&optional unmark)
9095   "Mark articles which has the same subject as read.
9096 If UNMARK is positive, remove any kind of mark.
9097 If UNMARK is negative, tick articles."
9098   (interactive "P")
9099   (when unmark
9100     (setq unmark (prefix-numeric-value unmark)))
9101   (let ((count
9102          (gnus-summary-mark-same-subject
9103           (gnus-summary-article-subject) unmark)))
9104     ;; If marked as read, go to next unread subject.
9105     (when (null unmark)
9106       ;; Go to next unread subject.
9107       (gnus-summary-next-subject 1 t))
9108     (gnus-message 7 "%d articles are marked as %s"
9109                   count (if unmark "unread" "read"))))
9110
9111 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9112   "Mark articles with same SUBJECT as read, and return marked number.
9113 If optional argument UNMARK is positive, remove any kinds of marks.
9114 If optional argument UNMARK is negative, mark articles as unread instead."
9115   (let ((count 1))
9116     (save-excursion
9117       (cond
9118        ((null unmark)                   ; Mark as read.
9119         (while (and
9120                 (progn
9121                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9122                   (gnus-summary-show-thread) t)
9123                 (gnus-summary-find-subject subject))
9124           (setq count (1+ count))))
9125        ((> unmark 0)                    ; Tick.
9126         (while (and
9127                 (progn
9128                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9129                   (gnus-summary-show-thread) t)
9130                 (gnus-summary-find-subject subject))
9131           (setq count (1+ count))))
9132        (t                               ; Mark as unread.
9133         (while (and
9134                 (progn
9135                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9136                   (gnus-summary-show-thread) t)
9137                 (gnus-summary-find-subject subject))
9138           (setq count (1+ count)))))
9139       (gnus-set-mode-line 'summary)
9140       ;; Return the number of marked articles.
9141       count)))
9142
9143 (defun gnus-summary-mark-as-processable (n &optional unmark)
9144   "Set the process mark on the next N articles.
9145 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9146 the process mark instead.  The difference between N and the actual
9147 number of articles marked is returned."
9148   (interactive "P")
9149   (if (and (null n) (gnus-region-active-p))
9150       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9151     (setq n (prefix-numeric-value n))
9152     (let ((backward (< n 0))
9153           (n (abs n)))
9154       (while (and
9155               (> n 0)
9156               (if unmark
9157                   (gnus-summary-remove-process-mark
9158                    (gnus-summary-article-number))
9159                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9160               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9161         (setq n (1- n)))
9162       (when (/= 0 n)
9163         (gnus-message 7 "No more articles"))
9164       (gnus-summary-recenter)
9165       (gnus-summary-position-point)
9166       n)))
9167
9168 (defun gnus-summary-unmark-as-processable (n)
9169   "Remove the process mark from the next N articles.
9170 If N is negative, unmark backward instead.  The difference between N and
9171 the actual number of articles unmarked is returned."
9172   (interactive "P")
9173   (gnus-summary-mark-as-processable n t))
9174
9175 (defun gnus-summary-unmark-all-processable ()
9176   "Remove the process mark from all articles."
9177   (interactive)
9178   (save-excursion
9179     (while gnus-newsgroup-processable
9180       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9181   (gnus-summary-position-point))
9182
9183 (defun gnus-summary-add-mark (article type)
9184   "Mark ARTICLE with a mark of TYPE."
9185   (let ((vtype (car (assq type gnus-article-mark-lists)))
9186         var)
9187     (if (not vtype)
9188         (error "No such mark type: %s" type)
9189       (setq var (intern (format "gnus-newsgroup-%s" type)))
9190       (set var (cons article (symbol-value var)))
9191       (if (memq type '(processable cached replied forwarded recent saved))
9192           (gnus-summary-update-secondary-mark article)
9193         ;;; !!! This is bogus.  We should find out what primary
9194         ;;; !!! mark we want to set.
9195         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9196
9197 (defun gnus-summary-mark-as-expirable (n)
9198   "Mark N articles forward as expirable.
9199 If N is negative, mark backward instead.  The difference between N and
9200 the actual number of articles marked is returned."
9201   (interactive "p")
9202   (gnus-summary-mark-forward n gnus-expirable-mark))
9203
9204 (defun gnus-summary-mark-as-spam (n)
9205   "Mark N articles forward as spam.
9206 If N is negative, mark backward instead.  The difference between N and
9207 the actual number of articles marked is returned."
9208   (interactive "p")
9209   (gnus-summary-mark-forward n gnus-spam-mark))
9210
9211 (defun gnus-summary-mark-article-as-replied (article)
9212   "Mark ARTICLE as replied to and update the summary line.
9213 ARTICLE can also be a list of articles."
9214   (interactive (list (gnus-summary-article-number)))
9215   (let ((articles (if (listp article) article (list article))))
9216     (dolist (article articles)
9217       (push article gnus-newsgroup-replied)
9218       (let ((buffer-read-only nil))
9219         (when (gnus-summary-goto-subject article nil t)
9220           (gnus-summary-update-secondary-mark article))))))
9221
9222 (defun gnus-summary-mark-article-as-forwarded (article)
9223   "Mark ARTICLE as forwarded and update the summary line.
9224 ARTICLE can also be a list of articles."
9225   (let ((articles (if (listp article) article (list article))))
9226     (dolist (article articles)
9227       (push article gnus-newsgroup-forwarded)
9228       (let ((buffer-read-only nil))
9229         (when (gnus-summary-goto-subject article nil t)
9230           (gnus-summary-update-secondary-mark article))))))
9231
9232 (defun gnus-summary-set-bookmark (article)
9233   "Set a bookmark in current article."
9234   (interactive (list (gnus-summary-article-number)))
9235   (when (or (not (get-buffer gnus-article-buffer))
9236             (not gnus-current-article)
9237             (not gnus-article-current)
9238             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9239     (error "No current article selected"))
9240   ;; Remove old bookmark, if one exists.
9241   (let ((old (assq article gnus-newsgroup-bookmarks)))
9242     (when old
9243       (setq gnus-newsgroup-bookmarks
9244             (delq old gnus-newsgroup-bookmarks))))
9245   ;; Set the new bookmark, which is on the form
9246   ;; (article-number . line-number-in-body).
9247   (push
9248    (cons article
9249          (save-excursion
9250            (set-buffer gnus-article-buffer)
9251            (count-lines
9252             (min (point)
9253                  (save-excursion
9254                    (goto-char (point-min))
9255                    (search-forward "\n\n" nil t)
9256                    (point)))
9257             (point))))
9258    gnus-newsgroup-bookmarks)
9259   (gnus-message 6 "A bookmark has been added to the current article."))
9260
9261 (defun gnus-summary-remove-bookmark (article)
9262   "Remove the bookmark from the current article."
9263   (interactive (list (gnus-summary-article-number)))
9264   ;; Remove old bookmark, if one exists.
9265   (let ((old (assq article gnus-newsgroup-bookmarks)))
9266     (if old
9267         (progn
9268           (setq gnus-newsgroup-bookmarks
9269                 (delq old gnus-newsgroup-bookmarks))
9270           (gnus-message 6 "Removed bookmark."))
9271       (gnus-message 6 "No bookmark in current article."))))
9272
9273 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9274 (defun gnus-summary-mark-as-dormant (n)
9275   "Mark N articles forward as dormant.
9276 If N is negative, mark backward instead.  The difference between N and
9277 the actual number of articles marked is returned."
9278   (interactive "p")
9279   (gnus-summary-mark-forward n gnus-dormant-mark))
9280
9281 (defun gnus-summary-set-process-mark (article)
9282   "Set the process mark on ARTICLE and update the summary line."
9283   (setq gnus-newsgroup-processable
9284         (cons article
9285               (delq article gnus-newsgroup-processable)))
9286   (when (gnus-summary-goto-subject article)
9287     (gnus-summary-show-thread)
9288     (gnus-summary-goto-subject article)
9289     (gnus-summary-update-secondary-mark article)))
9290
9291 (defun gnus-summary-remove-process-mark (article)
9292   "Remove the process mark from ARTICLE and update the summary line."
9293   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9294   (when (gnus-summary-goto-subject article)
9295     (gnus-summary-show-thread)
9296     (gnus-summary-goto-subject article)
9297     (gnus-summary-update-secondary-mark article)))
9298
9299 (defun gnus-summary-set-saved-mark (article)
9300   "Set the process mark on ARTICLE and update the summary line."
9301   (push article gnus-newsgroup-saved)
9302   (when (gnus-summary-goto-subject article)
9303     (gnus-summary-update-secondary-mark article)))
9304
9305 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9306   "Mark N articles as read forwards.
9307 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9308 The difference between N and the actual number of articles marked is
9309 returned.
9310 Iff NO-EXPIRE, auto-expiry will be inhibited."
9311   (interactive "p")
9312   (gnus-summary-show-thread)
9313   (let ((backward (< n 0))
9314         (gnus-summary-goto-unread
9315          (and gnus-summary-goto-unread
9316               (not (eq gnus-summary-goto-unread 'never))
9317               (not (memq mark (list gnus-unread-mark
9318                                     gnus-ticked-mark gnus-dormant-mark)))))
9319         (n (abs n))
9320         (mark (or mark gnus-del-mark)))
9321     (while (and (> n 0)
9322                 (gnus-summary-mark-article nil mark no-expire)
9323                 (zerop (gnus-summary-next-subject
9324                         (if backward -1 1)
9325                         (and gnus-summary-goto-unread
9326                              (not (eq gnus-summary-goto-unread 'never)))
9327                         t)))
9328       (setq n (1- n)))
9329     (when (/= 0 n)
9330       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9331     (gnus-summary-recenter)
9332     (gnus-summary-position-point)
9333     (gnus-set-mode-line 'summary)
9334     n))
9335
9336 (defun gnus-summary-mark-article-as-read (mark)
9337   "Mark the current article quickly as read with MARK."
9338   (let ((article (gnus-summary-article-number)))
9339     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9340     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9341     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9342     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9343     (push (cons article mark) gnus-newsgroup-reads)
9344     ;; Possibly remove from cache, if that is used.
9345     (when gnus-use-cache
9346       (gnus-cache-enter-remove-article article))
9347     ;; Allow the backend to change the mark.
9348     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9349     ;; Check for auto-expiry.
9350     (when (and gnus-newsgroup-auto-expire
9351                (memq mark gnus-auto-expirable-marks))
9352       (setq mark gnus-expirable-mark)
9353       ;; Let the backend know about the mark change.
9354       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9355       (push article gnus-newsgroup-expirable))
9356     ;; Set the mark in the buffer.
9357     (gnus-summary-update-mark mark 'unread)
9358     t))
9359
9360 (defun gnus-summary-mark-article-as-unread (mark)
9361   "Mark the current article quickly as unread with MARK."
9362   (let* ((article (gnus-summary-article-number))
9363          (old-mark (gnus-summary-article-mark article)))
9364     ;; Allow the backend to change the mark.
9365     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9366     (if (eq mark old-mark)
9367         t
9368       (if (<= article 0)
9369           (progn
9370             (gnus-error 1 "Can't mark negative article numbers")
9371             nil)
9372         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9373         (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9374         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9375         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9376         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9377         (cond ((= mark gnus-ticked-mark)
9378                (setq gnus-newsgroup-marked
9379                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9380                                               article)))
9381               ((= mark gnus-spam-mark)
9382                (setq gnus-newsgroup-spam-marked
9383                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9384                                               article)))
9385               ((= mark gnus-dormant-mark)
9386                (setq gnus-newsgroup-dormant
9387                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9388                                               article)))
9389               (t
9390                (setq gnus-newsgroup-unreads
9391                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9392                                               article))))
9393         (gnus-pull article gnus-newsgroup-reads)
9394
9395         ;; See whether the article is to be put in the cache.
9396         (and gnus-use-cache
9397              (vectorp (gnus-summary-article-header article))
9398              (save-excursion
9399                (gnus-cache-possibly-enter-article
9400                 gnus-newsgroup-name article
9401                 (= mark gnus-ticked-mark)
9402                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9403
9404         ;; Fix the mark.
9405         (gnus-summary-update-mark mark 'unread)
9406         t))))
9407
9408 (defun gnus-summary-mark-article (&optional article mark no-expire)
9409   "Mark ARTICLE with MARK.  MARK can be any character.
9410 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9411 `??' (dormant) and `?E' (expirable).
9412 If MARK is nil, then the default character `?r' is used.
9413 If ARTICLE is nil, then the article on the current line will be
9414 marked.
9415 Iff NO-EXPIRE, auto-expiry will be inhibited."
9416   ;; The mark might be a string.
9417   (when (stringp mark)
9418     (setq mark (aref mark 0)))
9419   ;; If no mark is given, then we check auto-expiring.
9420   (when (null mark)
9421     (setq mark gnus-del-mark))
9422   (when (and (not no-expire)
9423              gnus-newsgroup-auto-expire
9424              (memq mark gnus-auto-expirable-marks))
9425     (setq mark gnus-expirable-mark))
9426   (let ((article (or article (gnus-summary-article-number)))
9427         (old-mark (gnus-summary-article-mark article)))
9428     ;; Allow the backend to change the mark.
9429     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9430     (if (eq mark old-mark)
9431         t
9432       (unless article
9433         (error "No article on current line"))
9434       (if (not (if (or (= mark gnus-unread-mark)
9435                        (= mark gnus-ticked-mark)
9436                        (= mark gnus-spam-mark)
9437                        (= mark gnus-dormant-mark))
9438                    (gnus-mark-article-as-unread article mark)
9439                  (gnus-mark-article-as-read article mark)))
9440           t
9441         ;; See whether the article is to be put in the cache.
9442         (and gnus-use-cache
9443              (not (= mark gnus-canceled-mark))
9444              (vectorp (gnus-summary-article-header article))
9445              (save-excursion
9446                (gnus-cache-possibly-enter-article
9447                 gnus-newsgroup-name article
9448                 (= mark gnus-ticked-mark)
9449                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9450
9451         (when (gnus-summary-goto-subject article nil t)
9452           (let ((buffer-read-only nil))
9453             (gnus-summary-show-thread)
9454             ;; Fix the mark.
9455             (gnus-summary-update-mark mark 'unread)
9456             t))))))
9457
9458 (defun gnus-summary-update-secondary-mark (article)
9459   "Update the secondary (read, process, cache) mark."
9460   (gnus-summary-update-mark
9461    (cond ((memq article gnus-newsgroup-processable)
9462           gnus-process-mark)
9463          ((memq article gnus-newsgroup-cached)
9464           gnus-cached-mark)
9465          ((memq article gnus-newsgroup-replied)
9466           gnus-replied-mark)
9467          ((memq article gnus-newsgroup-forwarded)
9468           gnus-forwarded-mark)
9469          ((memq article gnus-newsgroup-saved)
9470           gnus-saved-mark)
9471          ((memq article gnus-newsgroup-recent)
9472           gnus-recent-mark)
9473          ((memq article gnus-newsgroup-unseen)
9474           gnus-unseen-mark)
9475          (t gnus-no-mark))
9476    'replied)
9477   (when (gnus-visual-p 'summary-highlight 'highlight)
9478     (gnus-run-hooks 'gnus-summary-update-hook))
9479   t)
9480
9481 (defun gnus-summary-update-mark (mark type)
9482   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9483         (buffer-read-only nil))
9484     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9485     (when forward
9486       (when (looking-at "\r")
9487         (incf forward))
9488       (when (<= (+ forward (point)) (point-max))
9489         ;; Go to the right position on the line.
9490         (goto-char (+ forward (point)))
9491         ;; Replace the old mark with the new mark.
9492         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9493         ;; Optionally update the marks by some user rule.
9494         (when (eq type 'unread)
9495           (gnus-data-set-mark
9496            (gnus-data-find (gnus-summary-article-number)) mark)
9497           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9498
9499 (defun gnus-mark-article-as-read (article &optional mark)
9500   "Enter ARTICLE in the pertinent lists and remove it from others."
9501   ;; Make the article expirable.
9502   (let ((mark (or mark gnus-del-mark)))
9503     (setq gnus-newsgroup-expirable
9504           (if (= mark gnus-expirable-mark)
9505               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9506             (delq article gnus-newsgroup-expirable)))
9507     ;; Remove from unread and marked lists.
9508     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9509     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9510     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9511     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9512     (push (cons article mark) gnus-newsgroup-reads)
9513     ;; Possibly remove from cache, if that is used.
9514     (when gnus-use-cache
9515       (gnus-cache-enter-remove-article article))
9516     t))
9517
9518 (defun gnus-mark-article-as-unread (article &optional mark)
9519   "Enter ARTICLE in the pertinent lists and remove it from others."
9520   (let ((mark (or mark gnus-ticked-mark)))
9521     (if (<= article 0)
9522         (progn
9523           (gnus-error 1 "Can't mark negative article numbers")
9524           nil)
9525       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9526             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9527             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9528             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9529             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9530
9531       ;; Unsuppress duplicates?
9532       (when gnus-suppress-duplicates
9533         (gnus-dup-unsuppress-article article))
9534
9535       (cond ((= mark gnus-ticked-mark)
9536              (setq gnus-newsgroup-marked
9537                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9538             ((= mark gnus-spam-mark)
9539              (setq gnus-newsgroup-spam-marked
9540                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked article)))
9541             ((= mark gnus-dormant-mark)
9542              (setq gnus-newsgroup-dormant
9543                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9544             (t
9545              (setq gnus-newsgroup-unreads
9546                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9547       (gnus-pull article gnus-newsgroup-reads)
9548       t)))
9549
9550 (defalias 'gnus-summary-mark-as-unread-forward
9551   'gnus-summary-tick-article-forward)
9552 (make-obsolete 'gnus-summary-mark-as-unread-forward
9553                'gnus-summary-tick-article-forward)
9554 (defun gnus-summary-tick-article-forward (n)
9555   "Tick N articles forwards.
9556 If N is negative, tick backwards instead.
9557 The difference between N and the number of articles ticked is returned."
9558   (interactive "p")
9559   (gnus-summary-mark-forward n gnus-ticked-mark))
9560
9561 (defalias 'gnus-summary-mark-as-unread-backward
9562   'gnus-summary-tick-article-backward)
9563 (make-obsolete 'gnus-summary-mark-as-unread-backward
9564                'gnus-summary-tick-article-backward)
9565 (defun gnus-summary-tick-article-backward (n)
9566   "Tick N articles backwards.
9567 The difference between N and the number of articles ticked is returned."
9568   (interactive "p")
9569   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9570
9571 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9572 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9573 (defun gnus-summary-tick-article (&optional article clear-mark)
9574   "Mark current article as unread.
9575 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9576 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9577   (interactive)
9578   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9579                                        gnus-ticked-mark)))
9580
9581 (defun gnus-summary-mark-as-read-forward (n)
9582   "Mark N articles as read forwards.
9583 If N is negative, mark backwards instead.
9584 The difference between N and the actual number of articles marked is
9585 returned."
9586   (interactive "p")
9587   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9588
9589 (defun gnus-summary-mark-as-read-backward (n)
9590   "Mark the N articles as read backwards.
9591 The difference between N and the actual number of articles marked is
9592 returned."
9593   (interactive "p")
9594   (gnus-summary-mark-forward
9595    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9596
9597 (defun gnus-summary-mark-as-read (&optional article mark)
9598   "Mark current article as read.
9599 ARTICLE specifies the article to be marked as read.
9600 MARK specifies a string to be inserted at the beginning of the line."
9601   (gnus-summary-mark-article article mark))
9602
9603 (defun gnus-summary-clear-mark-forward (n)
9604   "Clear marks from N articles forward.
9605 If N is negative, clear backward instead.
9606 The difference between N and the number of marks cleared is returned."
9607   (interactive "p")
9608   (gnus-summary-mark-forward n gnus-unread-mark))
9609
9610 (defun gnus-summary-clear-mark-backward (n)
9611   "Clear marks from N articles backward.
9612 The difference between N and the number of marks cleared is returned."
9613   (interactive "p")
9614   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9615
9616 (defun gnus-summary-mark-unread-as-read ()
9617   "Intended to be used by `gnus-summary-mark-article-hook'."
9618   (when (memq gnus-current-article gnus-newsgroup-unreads)
9619     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9620
9621 (defun gnus-summary-mark-read-and-unread-as-read ()
9622   "Intended to be used by `gnus-summary-mark-article-hook'."
9623   (let ((mark (gnus-summary-article-mark)))
9624     (when (or (gnus-unread-mark-p mark)
9625               (gnus-read-mark-p mark))
9626       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9627
9628 (defun gnus-summary-mark-unread-as-ticked ()
9629   "Intended to be used by `gnus-summary-mark-article-hook'."
9630   (when (memq gnus-current-article gnus-newsgroup-unreads)
9631     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9632
9633 (defun gnus-summary-mark-region-as-read (point mark all)
9634   "Mark all unread articles between point and mark as read.
9635 If given a prefix, mark all articles between point and mark as read,
9636 even ticked and dormant ones."
9637   (interactive "r\nP")
9638   (save-excursion
9639     (let (article)
9640       (goto-char point)
9641       (beginning-of-line)
9642       (while (and
9643               (< (point) mark)
9644               (progn
9645                 (when (or all
9646                           (memq (setq article (gnus-summary-article-number))
9647                                 gnus-newsgroup-unreads))
9648                   (gnus-summary-mark-article article gnus-del-mark))
9649                 t)
9650               (gnus-summary-find-next))))))
9651
9652 (defun gnus-summary-mark-below (score mark)
9653   "Mark articles with score less than SCORE with MARK."
9654   (interactive "P\ncMark: ")
9655   (setq score (if score
9656                   (prefix-numeric-value score)
9657                 (or gnus-summary-default-score 0)))
9658   (save-excursion
9659     (set-buffer gnus-summary-buffer)
9660     (goto-char (point-min))
9661     (while
9662         (progn
9663           (and (< (gnus-summary-article-score) score)
9664                (gnus-summary-mark-article nil mark))
9665           (gnus-summary-find-next)))))
9666
9667 (defun gnus-summary-kill-below (&optional score)
9668   "Mark articles with score below SCORE as read."
9669   (interactive "P")
9670   (gnus-summary-mark-below score gnus-killed-mark))
9671
9672 (defun gnus-summary-clear-above (&optional score)
9673   "Clear all marks from articles with score above SCORE."
9674   (interactive "P")
9675   (gnus-summary-mark-above score gnus-unread-mark))
9676
9677 (defun gnus-summary-tick-above (&optional score)
9678   "Tick all articles with score above SCORE."
9679   (interactive "P")
9680   (gnus-summary-mark-above score gnus-ticked-mark))
9681
9682 (defun gnus-summary-mark-above (score mark)
9683   "Mark articles with score over SCORE with MARK."
9684   (interactive "P\ncMark: ")
9685   (setq score (if score
9686                   (prefix-numeric-value score)
9687                 (or gnus-summary-default-score 0)))
9688   (save-excursion
9689     (set-buffer gnus-summary-buffer)
9690     (goto-char (point-min))
9691     (while (and (progn
9692                   (when (> (gnus-summary-article-score) score)
9693                     (gnus-summary-mark-article nil mark))
9694                   t)
9695                 (gnus-summary-find-next)))))
9696
9697 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9698 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9699 (defun gnus-summary-limit-include-expunged (&optional no-error)
9700   "Display all the hidden articles that were expunged for low scores."
9701   (interactive)
9702   (let ((buffer-read-only nil))
9703     (let ((scored gnus-newsgroup-scored)
9704           headers h)
9705       (while scored
9706         (unless (gnus-summary-article-header (caar scored))
9707           (and (setq h (gnus-number-to-header (caar scored)))
9708                (< (cdar scored) gnus-summary-expunge-below)
9709                (push h headers)))
9710         (setq scored (cdr scored)))
9711       (if (not headers)
9712           (when (not no-error)
9713             (error "No expunged articles hidden"))
9714         (goto-char (point-min))
9715         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9716         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9717         (mapcar (lambda (x) (push (mail-header-number x)
9718                                   gnus-newsgroup-limit))
9719                 headers)
9720         (gnus-summary-prepare-unthreaded (nreverse headers))
9721         (goto-char (point-min))
9722         (gnus-summary-position-point)
9723         t))))
9724
9725 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9726   "Mark all unread articles in this newsgroup as read.
9727 If prefix argument ALL is non-nil, ticked and dormant articles will
9728 also be marked as read.
9729 If QUIETLY is non-nil, no questions will be asked.
9730 If TO-HERE is non-nil, it should be a point in the buffer.  All
9731 articles before (after, if REVERSE is set) this point will be marked as read.
9732 Note that this function will only catch up the unread article
9733 in the current summary buffer limitation.
9734 The number of articles marked as read is returned."
9735   (interactive "P")
9736   (prog1
9737       (save-excursion
9738         (when (or quietly
9739                   (not gnus-interactive-catchup) ;Without confirmation?
9740                   gnus-expert-user
9741                   (gnus-y-or-n-p
9742                    (if all
9743                        "Mark absolutely all articles as read? "
9744                      "Mark all unread articles as read? ")))
9745           (if (and not-mark
9746                    (not gnus-newsgroup-adaptive)
9747                    (not gnus-newsgroup-auto-expire)
9748                    (not gnus-suppress-duplicates)
9749                    (or (not gnus-use-cache)
9750                        (eq gnus-use-cache 'passive)))
9751               (progn
9752                 (when all
9753                   (setq gnus-newsgroup-marked nil
9754                         gnus-newsgroup-spam-marked nil
9755                         gnus-newsgroup-dormant nil))
9756                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9757             ;; We actually mark all articles as canceled, which we
9758             ;; have to do when using auto-expiry or adaptive scoring.
9759             (gnus-summary-show-all-threads)
9760             (if (and to-here reverse)
9761                 (progn
9762                   (goto-char to-here)
9763                   (while (and
9764                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9765                           (gnus-summary-find-next (not all) nil nil t))))
9766               (when (gnus-summary-first-subject (not all) t)
9767                 (while (and
9768                         (if to-here (< (point) to-here) t)
9769                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9770                         (gnus-summary-find-next (not all) nil nil t)))))
9771             (gnus-set-mode-line 'summary))
9772           t))
9773     (gnus-summary-position-point)))
9774
9775 (defun gnus-summary-catchup-to-here (&optional all)
9776   "Mark all unticked articles before the current one as read.
9777 If ALL is non-nil, also mark ticked and dormant articles as read."
9778   (interactive "P")
9779   (save-excursion
9780     (gnus-save-hidden-threads
9781       (let ((beg (point)))
9782         ;; We check that there are unread articles.
9783         (when (or all (gnus-summary-find-prev))
9784           (gnus-summary-catchup all t beg)))))
9785   (gnus-summary-position-point))
9786
9787 (defun gnus-summary-catchup-from-here (&optional all)
9788   "Mark all unticked articles after the current one as read.
9789 If ALL is non-nil, also mark ticked and dormant articles as read."
9790   (interactive "P")
9791   (save-excursion
9792     (gnus-save-hidden-threads
9793       (let ((beg (point)))
9794         ;; We check that there are unread articles.
9795         (when (or all (gnus-summary-find-next))
9796           (gnus-summary-catchup all t beg nil t)))))
9797
9798   (gnus-summary-position-point))
9799 (defun gnus-summary-catchup-all (&optional quietly)
9800   "Mark all articles in this newsgroup as read."
9801   (interactive "P")
9802   (gnus-summary-catchup t quietly))
9803
9804 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9805   "Mark all unread articles in this group as read, then exit.
9806 If prefix argument ALL is non-nil, all articles are marked as read.
9807 If QUIETLY is non-nil, no questions will be asked."
9808   (interactive "P")
9809   (when (gnus-summary-catchup all quietly nil 'fast)
9810     ;; Select next newsgroup or exit.
9811     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9812              (eq gnus-auto-select-next 'quietly))
9813         (gnus-summary-next-group nil)
9814       (gnus-summary-exit))))
9815
9816 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9817   "Mark all articles in this newsgroup as read, and then exit."
9818   (interactive "P")
9819   (gnus-summary-catchup-and-exit t quietly))
9820
9821 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9822   "Mark all articles in this group as read and select the next group.
9823 If given a prefix, mark all articles, unread as well as ticked, as
9824 read."
9825   (interactive "P")
9826   (save-excursion
9827     (gnus-summary-catchup all))
9828   (gnus-summary-next-group))
9829
9830 ;;;
9831 ;;; with article
9832 ;;;
9833
9834 (defmacro gnus-with-article (article &rest forms)
9835   "Select ARTICLE and perform FORMS in the original article buffer.
9836 Then replace the article with the result."
9837   `(progn
9838      ;; We don't want the article to be marked as read.
9839      (let (gnus-mark-article-hook)
9840        (gnus-summary-select-article t t nil ,article))
9841      (set-buffer gnus-original-article-buffer)
9842      ,@forms
9843      (if (not (gnus-check-backend-function
9844                'request-replace-article (car gnus-article-current)))
9845          (gnus-message 5 "Read-only group; not replacing")
9846        (unless (gnus-request-replace-article
9847                 ,article (car gnus-article-current)
9848                 (current-buffer) t)
9849          (error "Couldn't replace article")))
9850      ;; The cache and backlog have to be flushed somewhat.
9851      (when gnus-keep-backlog
9852        (gnus-backlog-remove-article
9853         (car gnus-article-current) (cdr gnus-article-current)))
9854      (when gnus-use-cache
9855        (gnus-cache-update-article
9856         (car gnus-article-current) (cdr gnus-article-current)))))
9857
9858 (put 'gnus-with-article 'lisp-indent-function 1)
9859 (put 'gnus-with-article 'edebug-form-spec '(form body))
9860
9861 ;; Thread-based commands.
9862
9863 (defun gnus-summary-articles-in-thread (&optional article)
9864   "Return a list of all articles in the current thread.
9865 If ARTICLE is non-nil, return all articles in the thread that starts
9866 with that article."
9867   (let* ((article (or article (gnus-summary-article-number)))
9868          (data (gnus-data-find-list article))
9869          (top-level (gnus-data-level (car data)))
9870          (top-subject
9871           (cond ((null gnus-thread-operation-ignore-subject)
9872                  (gnus-simplify-subject-re
9873                   (mail-header-subject (gnus-data-header (car data)))))
9874                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9875                  (gnus-simplify-subject-fuzzy
9876                   (mail-header-subject (gnus-data-header (car data)))))
9877                 (t nil)))
9878          (end-point (save-excursion
9879                       (if (gnus-summary-go-to-next-thread)
9880                           (point) (point-max))))
9881          articles)
9882     (while (and data
9883                 (< (gnus-data-pos (car data)) end-point))
9884       (when (or (not top-subject)
9885                 (string= top-subject
9886                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9887                              (gnus-simplify-subject-fuzzy
9888                               (mail-header-subject
9889                                (gnus-data-header (car data))))
9890                            (gnus-simplify-subject-re
9891                             (mail-header-subject
9892                              (gnus-data-header (car data)))))))
9893         (push (gnus-data-number (car data)) articles))
9894       (unless (and (setq data (cdr data))
9895                    (> (gnus-data-level (car data)) top-level))
9896         (setq data nil)))
9897     ;; Return the list of articles.
9898     (nreverse articles)))
9899
9900 (defun gnus-summary-rethread-current ()
9901   "Rethread the thread the current article is part of."
9902   (interactive)
9903   (let* ((gnus-show-threads t)
9904          (article (gnus-summary-article-number))
9905          (id (mail-header-id (gnus-summary-article-header)))
9906          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9907     (unless id
9908       (error "No article on the current line"))
9909     (gnus-rebuild-thread id)
9910     (gnus-summary-goto-subject article)))
9911
9912 (defun gnus-summary-reparent-thread ()
9913   "Make the current article child of the marked (or previous) article.
9914
9915 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9916 is non-nil or the Subject: of both articles are the same."
9917   (interactive)
9918   (unless (not (gnus-group-read-only-p))
9919     (error "The current newsgroup does not support article editing"))
9920   (unless (<= (length gnus-newsgroup-processable) 1)
9921     (error "No more than one article may be marked"))
9922   (save-window-excursion
9923     (let ((gnus-article-buffer " *reparent*")
9924           (current-article (gnus-summary-article-number))
9925           ;; First grab the marked article, otherwise one line up.
9926           (parent-article (if (not (null gnus-newsgroup-processable))
9927                               (car gnus-newsgroup-processable)
9928                             (save-excursion
9929                               (if (eq (forward-line -1) 0)
9930                                   (gnus-summary-article-number)
9931                                 (error "Beginning of summary buffer"))))))
9932       (unless (not (eq current-article parent-article))
9933         (error "An article may not be self-referential"))
9934       (let ((message-id (mail-header-id
9935                          (gnus-summary-article-header parent-article))))
9936         (unless (and message-id (not (equal message-id "")))
9937           (error "No message-id in desired parent"))
9938         (gnus-with-article current-article
9939           (save-restriction
9940             (goto-char (point-min))
9941             (message-narrow-to-head)
9942             (if (re-search-forward "^References: " nil t)
9943                 (progn
9944                   (re-search-forward "^[^ \t]" nil t)
9945                   (forward-line -1)
9946                   (end-of-line)
9947                   (insert " " message-id))
9948               (insert "References: " message-id "\n"))))
9949         (set-buffer gnus-summary-buffer)
9950         (gnus-summary-unmark-all-processable)
9951         (gnus-summary-update-article current-article)
9952         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9953             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9954         (gnus-summary-rethread-current)
9955         (gnus-message 3 "Article %d is now the child of article %d"
9956                       current-article parent-article)))))
9957
9958 (defun gnus-summary-toggle-threads (&optional arg)
9959   "Toggle showing conversation threads.
9960 If ARG is positive number, turn showing conversation threads on."
9961   (interactive "P")
9962   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9963     (setq gnus-show-threads
9964           (if (null arg) (not gnus-show-threads)
9965             (> (prefix-numeric-value arg) 0)))
9966     (gnus-summary-prepare)
9967     (gnus-summary-goto-subject current)
9968     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9969     (gnus-summary-position-point)))
9970
9971 (defun gnus-summary-show-all-threads ()
9972   "Show all threads."
9973   (interactive)
9974   (save-excursion
9975     (let ((buffer-read-only nil))
9976       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9977   (gnus-summary-position-point))
9978
9979 (defun gnus-summary-show-thread ()
9980   "Show thread subtrees.
9981 Returns nil if no thread was there to be shown."
9982   (interactive)
9983   (let ((buffer-read-only nil)
9984         (orig (point))
9985         ;; first goto end then to beg, to have point at beg after let
9986         (end (progn (end-of-line) (point)))
9987         (beg (progn (beginning-of-line) (point))))
9988     (prog1
9989         ;; Any hidden lines here?
9990         (search-forward "\r" end t)
9991       (subst-char-in-region beg end ?\^M ?\n t)
9992       (goto-char orig)
9993       (gnus-summary-position-point))))
9994
9995 (defun gnus-summary-maybe-hide-threads ()
9996   "If requested, hide the threads that should be hidden."
9997   (when (and gnus-show-threads
9998              gnus-thread-hide-subtree)
9999     (gnus-summary-hide-all-threads
10000      (if (or (consp gnus-thread-hide-subtree)
10001              (gnus-functionp gnus-thread-hide-subtree))
10002          (gnus-make-predicate gnus-thread-hide-subtree)
10003        nil))))
10004
10005 ;;; Hiding predicates.
10006
10007 (defun gnus-article-unread-p (header)
10008   (memq (mail-header-number header) gnus-newsgroup-unreads))
10009
10010 (defun gnus-article-unseen-p (header)
10011   (memq (mail-header-number header) gnus-newsgroup-unseen))
10012
10013 (defun gnus-map-articles (predicate articles)
10014   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10015   (apply 'gnus-or (mapcar predicate
10016                           (mapcar 'gnus-summary-article-header articles))))
10017
10018 (defun gnus-summary-hide-all-threads (&optional predicate)
10019   "Hide all thread subtrees.
10020 If PREDICATE is supplied, threads that satisfy this predicate
10021 will not be hidden."
10022   (interactive)
10023   (save-excursion
10024     (goto-char (point-min))
10025     (let ((end nil))
10026       (while (not end)
10027         (when (or (not predicate)
10028                   (gnus-map-articles
10029                    predicate (gnus-summary-article-children)))
10030             (gnus-summary-hide-thread))
10031         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10032   (gnus-summary-position-point))
10033
10034 (defun gnus-summary-hide-thread ()
10035   "Hide thread subtrees.
10036 If PREDICATE is supplied, threads that satisfy this predicate
10037 will not be hidden.
10038 Returns nil if no threads were there to be hidden."
10039   (interactive)
10040   (let ((buffer-read-only nil)
10041         (start (point))
10042         (article (gnus-summary-article-number)))
10043     (goto-char start)
10044     ;; Go forward until either the buffer ends or the subthread
10045     ;; ends.
10046     (when (and (not (eobp))
10047                (or (zerop (gnus-summary-next-thread 1 t))
10048                    (goto-char (point-max))))
10049       (prog1
10050           (if (and (> (point) start)
10051                    (search-backward "\n" start t))
10052               (progn
10053                 (subst-char-in-region start (point) ?\n ?\^M)
10054                 (gnus-summary-goto-subject article))
10055             (goto-char start)
10056             nil)))))
10057
10058 (defun gnus-summary-go-to-next-thread (&optional previous)
10059   "Go to the same level (or less) next thread.
10060 If PREVIOUS is non-nil, go to previous thread instead.
10061 Return the article number moved to, or nil if moving was impossible."
10062   (let ((level (gnus-summary-thread-level))
10063         (way (if previous -1 1))
10064         (beg (point)))
10065     (forward-line way)
10066     (while (and (not (eobp))
10067                 (< level (gnus-summary-thread-level)))
10068       (forward-line way))
10069     (if (eobp)
10070         (progn
10071           (goto-char beg)
10072           nil)
10073       (setq beg (point))
10074       (prog1
10075           (gnus-summary-article-number)
10076         (goto-char beg)))))
10077
10078 (defun gnus-summary-next-thread (n &optional silent)
10079   "Go to the same level next N'th thread.
10080 If N is negative, search backward instead.
10081 Returns the difference between N and the number of skips actually
10082 done.
10083
10084 If SILENT, don't output messages."
10085   (interactive "p")
10086   (let ((backward (< n 0))
10087         (n (abs n)))
10088     (while (and (> n 0)
10089                 (gnus-summary-go-to-next-thread backward))
10090       (decf n))
10091     (unless silent
10092       (gnus-summary-position-point))
10093     (when (and (not silent) (/= 0 n))
10094       (gnus-message 7 "No more threads"))
10095     n))
10096
10097 (defun gnus-summary-prev-thread (n)
10098   "Go to the same level previous N'th thread.
10099 Returns the difference between N and the number of skips actually
10100 done."
10101   (interactive "p")
10102   (gnus-summary-next-thread (- n)))
10103
10104 (defun gnus-summary-go-down-thread ()
10105   "Go down one level in the current thread."
10106   (let ((children (gnus-summary-article-children)))
10107     (when children
10108       (gnus-summary-goto-subject (car children)))))
10109
10110 (defun gnus-summary-go-up-thread ()
10111   "Go up one level in the current thread."
10112   (let ((parent (gnus-summary-article-parent)))
10113     (when parent
10114       (gnus-summary-goto-subject parent))))
10115
10116 (defun gnus-summary-down-thread (n)
10117   "Go down thread N steps.
10118 If N is negative, go up instead.
10119 Returns the difference between N and how many steps down that were
10120 taken."
10121   (interactive "p")
10122   (let ((up (< n 0))
10123         (n (abs n)))
10124     (while (and (> n 0)
10125                 (if up (gnus-summary-go-up-thread)
10126                   (gnus-summary-go-down-thread)))
10127       (setq n (1- n)))
10128     (gnus-summary-position-point)
10129     (when (/= 0 n)
10130       (gnus-message 7 "Can't go further"))
10131     n))
10132
10133 (defun gnus-summary-up-thread (n)
10134   "Go up thread N steps.
10135 If N is negative, go down instead.
10136 Returns the difference between N and how many steps down that were
10137 taken."
10138   (interactive "p")
10139   (gnus-summary-down-thread (- n)))
10140
10141 (defun gnus-summary-top-thread ()
10142   "Go to the top of the thread."
10143   (interactive)
10144   (while (gnus-summary-go-up-thread))
10145   (gnus-summary-article-number))
10146
10147 (defun gnus-summary-kill-thread (&optional unmark)
10148   "Mark articles under current thread as read.
10149 If the prefix argument is positive, remove any kinds of marks.
10150 If the prefix argument is negative, tick articles instead."
10151   (interactive "P")
10152   (when unmark
10153     (setq unmark (prefix-numeric-value unmark)))
10154   (let ((articles (gnus-summary-articles-in-thread)))
10155     (save-excursion
10156       ;; Expand the thread.
10157       (gnus-summary-show-thread)
10158       ;; Mark all the articles.
10159       (while articles
10160         (gnus-summary-goto-subject (car articles))
10161         (cond ((null unmark)
10162                (gnus-summary-mark-article-as-read gnus-killed-mark))
10163               ((> unmark 0)
10164                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10165               (t
10166                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10167         (setq articles (cdr articles))))
10168     ;; Hide killed subtrees.
10169     (and (null unmark)
10170          gnus-thread-hide-killed
10171          (gnus-summary-hide-thread))
10172     ;; If marked as read, go to next unread subject.
10173     (when (null unmark)
10174       ;; Go to next unread subject.
10175       (gnus-summary-next-subject 1 t)))
10176   (gnus-set-mode-line 'summary))
10177
10178 ;; Summary sorting commands
10179
10180 (defun gnus-summary-sort-by-number (&optional reverse)
10181   "Sort the summary buffer by article number.
10182 Argument REVERSE means reverse order."
10183   (interactive "P")
10184   (gnus-summary-sort 'number reverse))
10185
10186 (defun gnus-summary-sort-by-random (&optional reverse)
10187   "Randomize the order in the summary buffer.
10188 Argument REVERSE means to randomize in reverse order."
10189   (interactive "P")
10190   (gnus-summary-sort 'random reverse))
10191
10192 (defun gnus-summary-sort-by-author (&optional reverse)
10193   "Sort the summary buffer by author name alphabetically.
10194 If `case-fold-search' is non-nil, case of letters is ignored.
10195 Argument REVERSE means reverse order."
10196   (interactive "P")
10197   (gnus-summary-sort 'author reverse))
10198
10199 (defun gnus-summary-sort-by-subject (&optional reverse)
10200   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10201 If `case-fold-search' is non-nil, case of letters is ignored.
10202 Argument REVERSE means reverse order."
10203   (interactive "P")
10204   (gnus-summary-sort 'subject reverse))
10205
10206 (defun gnus-summary-sort-by-date (&optional reverse)
10207   "Sort the summary buffer by date.
10208 Argument REVERSE means reverse order."
10209   (interactive "P")
10210   (gnus-summary-sort 'date reverse))
10211
10212 (defun gnus-summary-sort-by-score (&optional reverse)
10213   "Sort the summary buffer by score.
10214 Argument REVERSE means reverse order."
10215   (interactive "P")
10216   (gnus-summary-sort 'score reverse))
10217
10218 (defun gnus-summary-sort-by-lines (&optional reverse)
10219   "Sort the summary buffer by the number of lines.
10220 Argument REVERSE means reverse order."
10221   (interactive "P")
10222   (gnus-summary-sort 'lines reverse))
10223
10224 (defun gnus-summary-sort-by-chars (&optional reverse)
10225   "Sort the summary buffer by article length.
10226 Argument REVERSE means reverse order."
10227   (interactive "P")
10228   (gnus-summary-sort 'chars reverse))
10229
10230 (defun gnus-summary-sort-by-original (&optional reverse)
10231   "Sort the summary buffer using the default sorting method.
10232 Argument REVERSE means reverse order."
10233   (interactive "P")
10234   (let* ((buffer-read-only)
10235          (gnus-summary-prepare-hook nil))
10236     ;; We do the sorting by regenerating the threads.
10237     (gnus-summary-prepare)
10238     ;; Hide subthreads if needed.
10239     (gnus-summary-maybe-hide-threads)))
10240
10241 (defun gnus-summary-sort (predicate reverse)
10242   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10243   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10244          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10245          (gnus-thread-sort-functions
10246           (if (not reverse)
10247               thread
10248             `(lambda (t1 t2)
10249                (,thread t2 t1))))
10250          (gnus-sort-gathered-threads-function
10251           gnus-thread-sort-functions)
10252          (gnus-article-sort-functions
10253           (if (not reverse)
10254               article
10255             `(lambda (t1 t2)
10256                (,article t2 t1))))
10257          (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 ;; Summary saving commands.
10265
10266 (defun gnus-summary-save-article (&optional n not-saved)
10267   "Save the current article using the default saver function.
10268 If N is a positive number, save the N next articles.
10269 If N is a negative number, save the N previous articles.
10270 If N is nil and any articles have been marked with the process mark,
10271 save those articles instead.
10272 The variable `gnus-default-article-saver' specifies the saver function."
10273   (interactive "P")
10274   (let* ((articles (gnus-summary-work-articles n))
10275          (save-buffer (save-excursion
10276                         (nnheader-set-temp-buffer " *Gnus Save*")))
10277          (num (length articles))
10278          header file)
10279     (dolist (article articles)
10280       (setq header (gnus-summary-article-header article))
10281       (if (not (vectorp header))
10282           ;; This is a pseudo-article.
10283           (if (assq 'name header)
10284               (gnus-copy-file (cdr (assq 'name header)))
10285             (gnus-message 1 "Article %d is unsaveable" article))
10286         ;; This is a real article.
10287         (save-window-excursion
10288           (let ((gnus-display-mime-function nil)
10289                 (gnus-article-prepare-hook nil))
10290             (gnus-summary-select-article t nil nil article)))
10291         (save-excursion
10292           (set-buffer save-buffer)
10293           (erase-buffer)
10294           (insert-buffer-substring gnus-original-article-buffer))
10295         (setq file (gnus-article-save save-buffer file num))
10296         (gnus-summary-remove-process-mark article)
10297         (unless not-saved
10298           (gnus-summary-set-saved-mark article))))
10299     (gnus-kill-buffer save-buffer)
10300     (gnus-summary-position-point)
10301     (gnus-set-mode-line 'summary)
10302     n))
10303
10304 (defun gnus-summary-pipe-output (&optional arg)
10305   "Pipe the current article to a subprocess.
10306 If N is a positive number, pipe the N next articles.
10307 If N is a negative number, pipe the N previous articles.
10308 If N is nil and any articles have been marked with the process mark,
10309 pipe those articles instead."
10310   (interactive "P")
10311   (require 'gnus-art)
10312   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10313     (gnus-summary-save-article arg t))
10314   (let ((buffer (get-buffer "*Shell Command Output*")))
10315     (if (and buffer
10316              (with-current-buffer buffer (> (point-max) (point-min))))
10317         (gnus-configure-windows 'pipe))))
10318
10319 (defun gnus-summary-save-article-mail (&optional arg)
10320   "Append the current article to an mail file.
10321 If N is a positive number, save the N next articles.
10322 If N is a negative number, save the N previous articles.
10323 If N is nil and any articles have been marked with the process mark,
10324 save those articles instead."
10325   (interactive "P")
10326   (require 'gnus-art)
10327   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10328     (gnus-summary-save-article arg)))
10329
10330 (defun gnus-summary-save-article-rmail (&optional arg)
10331   "Append the current article to an rmail file.
10332 If N is a positive number, save the N next articles.
10333 If N is a negative number, save the N previous articles.
10334 If N is nil and any articles have been marked with the process mark,
10335 save those articles instead."
10336   (interactive "P")
10337   (require 'gnus-art)
10338   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10339     (gnus-summary-save-article arg)))
10340
10341 (defun gnus-summary-save-article-file (&optional arg)
10342   "Append the current article to a file.
10343 If N is a positive number, save the N next articles.
10344 If N is a negative number, save the N previous articles.
10345 If N is nil and any articles have been marked with the process mark,
10346 save those articles instead."
10347   (interactive "P")
10348   (require 'gnus-art)
10349   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10350     (gnus-summary-save-article arg)))
10351
10352 (defun gnus-summary-write-article-file (&optional arg)
10353   "Write the current article to a file, deleting the previous file.
10354 If N is a positive number, save the N next articles.
10355 If N is a negative number, save the N previous articles.
10356 If N is nil and any articles have been marked with the process mark,
10357 save those articles instead."
10358   (interactive "P")
10359   (require 'gnus-art)
10360   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10361     (gnus-summary-save-article arg)))
10362
10363 (defun gnus-summary-save-article-body-file (&optional arg)
10364   "Append the current article body to a file.
10365 If N is a positive number, save the N next articles.
10366 If N is a negative number, save the N previous articles.
10367 If N is nil and any articles have been marked with the process mark,
10368 save those articles instead."
10369   (interactive "P")
10370   (require 'gnus-art)
10371   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10372     (gnus-summary-save-article arg)))
10373
10374 (defun gnus-summary-muttprint (&optional arg)
10375   "Print the current article using Muttprint.
10376 If N is a positive number, save the N next articles.
10377 If N is a negative number, save the N previous articles.
10378 If N is nil and any articles have been marked with the process mark,
10379 save those articles instead."
10380   (interactive "P")
10381   (require 'gnus-art)
10382   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10383     (gnus-summary-save-article arg t)))
10384
10385 (defun gnus-summary-pipe-message (program)
10386   "Pipe the current article through PROGRAM."
10387   (interactive "sProgram: ")
10388   (gnus-summary-select-article)
10389   (let ((mail-header-separator ""))
10390     (gnus-eval-in-buffer-window gnus-article-buffer
10391       (save-restriction
10392         (widen)
10393         (let ((start (window-start))
10394               buffer-read-only)
10395           (message-pipe-buffer-body program)
10396           (set-window-start (get-buffer-window (current-buffer)) start))))))
10397
10398 (defun gnus-get-split-value (methods)
10399   "Return a value based on the split METHODS."
10400   (let (split-name method result match)
10401     (when methods
10402       (save-excursion
10403         (set-buffer gnus-original-article-buffer)
10404         (save-restriction
10405           (nnheader-narrow-to-headers)
10406           (while (and methods (not split-name))
10407             (goto-char (point-min))
10408             (setq method (pop methods))
10409             (setq match (car method))
10410             (when (cond
10411                    ((stringp match)
10412                     ;; Regular expression.
10413                     (ignore-errors
10414                       (re-search-forward match nil t)))
10415                    ((gnus-functionp match)
10416                     ;; Function.
10417                     (save-restriction
10418                       (widen)
10419                       (setq result (funcall match gnus-newsgroup-name))))
10420                    ((consp match)
10421                     ;; Form.
10422                     (save-restriction
10423                       (widen)
10424                       (setq result (eval match)))))
10425               (setq split-name (cdr method))
10426               (cond ((stringp result)
10427                      (push (expand-file-name
10428                             result gnus-article-save-directory)
10429                            split-name))
10430                     ((consp result)
10431                      (setq split-name (append result split-name)))))))))
10432     (nreverse split-name)))
10433
10434 (defun gnus-valid-move-group-p (group)
10435   (and (boundp group)
10436        (symbol-name group)
10437        (symbol-value group)
10438        (gnus-get-function (gnus-find-method-for-group
10439                            (symbol-name group)) 'request-accept-article t)))
10440
10441 (defun gnus-read-move-group-name (prompt default articles prefix)
10442   "Read a group name."
10443   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10444          (minibuffer-confirm-incomplete nil) ; XEmacs
10445          (prom
10446           (format "%s %s to:"
10447                   prompt
10448                   (if (> (length articles) 1)
10449                       (format "these %d articles" (length articles))
10450                     "this article")))
10451          (to-newsgroup
10452           (cond
10453            ((null split-name)
10454             (gnus-completing-read-with-default
10455              default prom
10456              gnus-active-hashtb
10457              'gnus-valid-move-group-p
10458              nil prefix
10459              'gnus-group-history))
10460            ((= 1 (length split-name))
10461             (gnus-completing-read-with-default
10462              (car split-name) prom
10463              gnus-active-hashtb
10464              'gnus-valid-move-group-p
10465              nil nil
10466              'gnus-group-history))
10467            (t
10468             (gnus-completing-read-with-default
10469              nil prom
10470              (mapcar (lambda (el) (list el))
10471                      (nreverse split-name))
10472              nil nil nil
10473              'gnus-group-history))))
10474          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10475     (when to-newsgroup
10476       (if (or (string= to-newsgroup "")
10477               (string= to-newsgroup prefix))
10478           (setq to-newsgroup default))
10479       (unless to-newsgroup
10480         (error "No group name entered"))
10481       (or (gnus-active to-newsgroup)
10482           (gnus-activate-group to-newsgroup nil nil to-method)
10483           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10484                                      to-newsgroup))
10485               (or (and (gnus-request-create-group to-newsgroup to-method)
10486                        (gnus-activate-group
10487                         to-newsgroup nil nil to-method)
10488                        (gnus-subscribe-group to-newsgroup))
10489                   (error "Couldn't create group %s" to-newsgroup)))
10490           (error "No such group: %s" to-newsgroup)))
10491     to-newsgroup))
10492
10493 (defun gnus-summary-save-parts (type dir n &optional reverse)
10494   "Save parts matching TYPE to DIR.
10495 If REVERSE, save parts that do not match TYPE."
10496   (interactive
10497    (list (read-string "Save parts of type: "
10498                       (or (car gnus-summary-save-parts-type-history)
10499                           gnus-summary-save-parts-default-mime)
10500                       'gnus-summary-save-parts-type-history)
10501          (setq gnus-summary-save-parts-last-directory
10502                (read-file-name "Save to directory: "
10503                                gnus-summary-save-parts-last-directory
10504                                nil t))
10505          current-prefix-arg))
10506   (gnus-summary-iterate n
10507     (let ((gnus-display-mime-function nil)
10508           (gnus-inhibit-treatment t))
10509       (gnus-summary-select-article))
10510     (save-excursion
10511       (set-buffer gnus-article-buffer)
10512       (let ((handles (or gnus-article-mime-handles
10513                          (mm-dissect-buffer nil gnus-article-loose-mime)
10514                          (mm-uu-dissect))))
10515         (when handles
10516           (gnus-summary-save-parts-1 type dir handles reverse)
10517           (unless gnus-article-mime-handles ;; Don't destroy this case.
10518             (mm-destroy-parts handles)))))))
10519
10520 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10521   (if (stringp (car handle))
10522       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10523               (cdr handle))
10524     (when (if reverse
10525               (not (string-match type (mm-handle-media-type handle)))
10526             (string-match type (mm-handle-media-type handle)))
10527       (let ((file (expand-file-name
10528                    (file-name-nondirectory
10529                     (or
10530                      (mail-content-type-get
10531                       (mm-handle-disposition handle) 'filename)
10532                      (concat gnus-newsgroup-name
10533                              "." (number-to-string
10534                                   (cdr gnus-article-current)))))
10535                    dir)))
10536         (unless (file-exists-p file)
10537           (mm-save-part-to-file handle file))))))
10538
10539 ;; Summary extract commands
10540
10541 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10542   (let ((buffer-read-only nil)
10543         (article (gnus-summary-article-number))
10544         after-article b e)
10545     (unless (gnus-summary-goto-subject article)
10546       (error "No such article: %d" article))
10547     (gnus-summary-position-point)
10548     ;; If all commands are to be bunched up on one line, we collect
10549     ;; them here.
10550     (unless gnus-view-pseudos-separately
10551       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10552             files action)
10553         (while ps
10554           (setq action (cdr (assq 'action (car ps))))
10555           (setq files (list (cdr (assq 'name (car ps)))))
10556           (while (and ps (cdr ps)
10557                       (string= (or action "1")
10558                                (or (cdr (assq 'action (cadr ps))) "2")))
10559             (push (cdr (assq 'name (cadr ps))) files)
10560             (setcdr ps (cddr ps)))
10561           (when files
10562             (when (not (string-match "%s" action))
10563               (push " " files))
10564             (push " " files)
10565             (when (assq 'execute (car ps))
10566               (setcdr (assq 'execute (car ps))
10567                       (funcall (if (string-match "%s" action)
10568                                    'format 'concat)
10569                                action
10570                                (mapconcat
10571                                 (lambda (f)
10572                                   (if (equal f " ")
10573                                       f
10574                                     (mm-quote-arg f)))
10575                                 files " ")))))
10576           (setq ps (cdr ps)))))
10577     (if (and gnus-view-pseudos (not not-view))
10578         (while pslist
10579           (when (assq 'execute (car pslist))
10580             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10581                                   (eq gnus-view-pseudos 'not-confirm)))
10582           (setq pslist (cdr pslist)))
10583       (save-excursion
10584         (while pslist
10585           (setq after-article (or (cdr (assq 'article (car pslist)))
10586                                   (gnus-summary-article-number)))
10587           (gnus-summary-goto-subject after-article)
10588           (forward-line 1)
10589           (setq b (point))
10590           (insert "    " (file-name-nondirectory
10591                           (cdr (assq 'name (car pslist))))
10592                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10593           (setq e (point))
10594           (forward-line -1)             ; back to `b'
10595           (gnus-add-text-properties
10596            b (1- e) (list 'gnus-number gnus-reffed-article-number
10597                           gnus-mouse-face-prop gnus-mouse-face))
10598           (gnus-data-enter
10599            after-article gnus-reffed-article-number
10600            gnus-unread-mark b (car pslist) 0 (- e b))
10601           (setq gnus-newsgroup-unreads
10602                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10603                                          gnus-reffed-article-number))
10604           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10605           (setq pslist (cdr pslist)))))))
10606
10607 (defun gnus-pseudos< (p1 p2)
10608   (let ((c1 (cdr (assq 'action p1)))
10609         (c2 (cdr (assq 'action p2))))
10610     (and c1 c2 (string< c1 c2))))
10611
10612 (defun gnus-request-pseudo-article (props)
10613   (cond ((assq 'execute props)
10614          (gnus-execute-command (cdr (assq 'execute props)))))
10615   (let ((gnus-current-article (gnus-summary-article-number)))
10616     (gnus-run-hooks 'gnus-mark-article-hook)))
10617
10618 (defun gnus-execute-command (command &optional automatic)
10619   (save-excursion
10620     (gnus-article-setup-buffer)
10621     (set-buffer gnus-article-buffer)
10622     (setq buffer-read-only nil)
10623     (let ((command (if automatic command
10624                      (read-string "Command: " (cons command 0)))))
10625       (erase-buffer)
10626       (insert "$ " command "\n\n")
10627       (if gnus-view-pseudo-asynchronously
10628           (start-process "gnus-execute" (current-buffer) shell-file-name
10629                          shell-command-switch command)
10630         (call-process shell-file-name nil t nil
10631                       shell-command-switch command)))))
10632
10633 ;; Summary kill commands.
10634
10635 (defun gnus-summary-edit-global-kill (article)
10636   "Edit the \"global\" kill file."
10637   (interactive (list (gnus-summary-article-number)))
10638   (gnus-group-edit-global-kill article))
10639
10640 (defun gnus-summary-edit-local-kill ()
10641   "Edit a local kill file applied to the current newsgroup."
10642   (interactive)
10643   (setq gnus-current-headers (gnus-summary-article-header))
10644   (gnus-group-edit-local-kill
10645    (gnus-summary-article-number) gnus-newsgroup-name))
10646
10647 ;;; Header reading.
10648
10649 (defun gnus-read-header (id &optional header)
10650   "Read the headers of article ID and enter them into the Gnus system."
10651   (let ((group gnus-newsgroup-name)
10652         (gnus-override-method
10653          (or
10654           gnus-override-method
10655           (and (gnus-news-group-p gnus-newsgroup-name)
10656                (car (gnus-refer-article-methods)))))
10657         where)
10658     ;; First we check to see whether the header in question is already
10659     ;; fetched.
10660     (if (stringp id)
10661         ;; This is a Message-ID.
10662         (setq header (or header (gnus-id-to-header id)))
10663       ;; This is an article number.
10664       (setq header (or header (gnus-summary-article-header id))))
10665     (if (and header
10666              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10667         ;; We have found the header.
10668         header
10669       ;; If this is a sparse article, we have to nix out its
10670       ;; previous entry in the thread hashtb.
10671       (when (and header
10672                  (gnus-summary-article-sparse-p (mail-header-number header)))
10673         (let* ((parent (gnus-parent-id (mail-header-references header)))
10674                (thread (and parent (gnus-id-to-thread parent))))
10675           (when thread
10676             (delq (assq header thread) thread))))
10677       ;; We have to really fetch the header to this article.
10678       (save-excursion
10679         (set-buffer nntp-server-buffer)
10680         (when (setq where (gnus-request-head id group))
10681           (nnheader-fold-continuation-lines)
10682           (goto-char (point-max))
10683           (insert ".\n")
10684           (goto-char (point-min))
10685           (insert "211 ")
10686           (princ (cond
10687                   ((numberp id) id)
10688                   ((cdr where) (cdr where))
10689                   (header (mail-header-number header))
10690                   (t gnus-reffed-article-number))
10691                  (current-buffer))
10692           (insert " Article retrieved.\n"))
10693         (if (or (not where)
10694                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10695             ()                          ; Malformed head.
10696           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10697             (when (and (stringp id)
10698                        (not (string= (gnus-group-real-name group)
10699                                      (car where))))
10700               ;; If we fetched by Message-ID and the article came
10701               ;; from a different group, we fudge some bogus article
10702               ;; numbers for this article.
10703               (mail-header-set-number header gnus-reffed-article-number))
10704             (save-excursion
10705               (set-buffer gnus-summary-buffer)
10706               (decf gnus-reffed-article-number)
10707               (gnus-remove-header (mail-header-number header))
10708               (push header gnus-newsgroup-headers)
10709               (setq gnus-current-headers header)
10710               (push (mail-header-number header) gnus-newsgroup-limit)))
10711           header)))))
10712
10713 (defun gnus-remove-header (number)
10714   "Remove header NUMBER from `gnus-newsgroup-headers'."
10715   (if (and gnus-newsgroup-headers
10716            (= number (mail-header-number (car gnus-newsgroup-headers))))
10717       (pop gnus-newsgroup-headers)
10718     (let ((headers gnus-newsgroup-headers))
10719       (while (and (cdr headers)
10720                   (not (= number (mail-header-number (cadr headers)))))
10721         (pop headers))
10722       (when (cdr headers)
10723         (setcdr headers (cddr headers))))))
10724
10725 ;;;
10726 ;;; summary highlights
10727 ;;;
10728
10729 (defun gnus-highlight-selected-summary ()
10730   "Highlight selected article in summary buffer."
10731   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10732   (when gnus-summary-selected-face
10733     (save-excursion
10734       (let* ((beg (progn (beginning-of-line) (point)))
10735              (end (progn (end-of-line) (point)))
10736              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10737              (from (if (get-text-property beg gnus-mouse-face-prop)
10738                        beg
10739                      (or (next-single-property-change
10740                           beg gnus-mouse-face-prop nil end)
10741                          beg)))
10742              (to
10743               (if (= from end)
10744                   (- from 2)
10745                 (or (next-single-property-change
10746                      from gnus-mouse-face-prop nil end)
10747                     end))))
10748         ;; If no mouse-face prop on line we will have to = from = end,
10749         ;; so we highlight the entire line instead.
10750         (when (= (+ to 2) from)
10751           (setq from beg)
10752           (setq to end))
10753         (if gnus-newsgroup-selected-overlay
10754             ;; Move old overlay.
10755             (gnus-move-overlay
10756              gnus-newsgroup-selected-overlay from to (current-buffer))
10757           ;; Create new overlay.
10758           (gnus-overlay-put
10759            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10760            'face gnus-summary-selected-face))))))
10761
10762 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10763 (defun gnus-summary-highlight-line ()
10764   "Highlight current line according to `gnus-summary-highlight'."
10765   (let* ((list gnus-summary-highlight)
10766          (beg (gnus-point-at-bol))
10767          (article (gnus-summary-article-number))
10768          (score (or (cdr (assq (or article gnus-current-article)
10769                                gnus-newsgroup-scored))
10770                     gnus-summary-default-score 0))
10771          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10772          (inhibit-read-only t)
10773          (default gnus-summary-default-score)
10774          (default-high gnus-summary-default-high-score)
10775          (default-low gnus-summary-default-low-score))
10776     ;; Eval the cars of the lists until we find a match.
10777     (while (and list
10778                 (not (eval (caar list))))
10779       (setq list (cdr list)))
10780     (let ((face (cdar list)))
10781       (unless (eq face (get-text-property beg 'face))
10782         (gnus-put-text-property-excluding-characters-with-faces
10783          beg (gnus-point-at-eol) 'face
10784          (setq face (if (boundp face) (symbol-value face) face)))
10785         (when gnus-summary-highlight-line-function
10786           (funcall gnus-summary-highlight-line-function article face))))))
10787
10788 (defun gnus-update-read-articles (group unread &optional compute)
10789   "Update the list of read articles in GROUP.
10790 UNREAD is a sorted list."
10791   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10792          (entry (gnus-gethash group gnus-newsrc-hashtb))
10793          (info (nth 2 entry))
10794          (prev 1)
10795          read)
10796     (if (or (not info) (not active))
10797         ;; There is no info on this group if it was, in fact,
10798         ;; killed.  Gnus stores no information on killed groups, so
10799         ;; there's nothing to be done.
10800         ;; One could store the information somewhere temporarily,
10801         ;; perhaps...  Hmmm...
10802         ()
10803       ;; Remove any negative articles numbers.
10804       (while (and unread (< (car unread) 0))
10805         (setq unread (cdr unread)))
10806       ;; Remove any expired article numbers
10807       (while (and unread (< (car unread) (car active)))
10808         (setq unread (cdr unread)))
10809       ;; Compute the ranges of read articles by looking at the list of
10810       ;; unread articles.
10811       (while unread
10812         (when (/= (car unread) prev)
10813           (push (if (= prev (1- (car unread))) prev
10814                   (cons prev (1- (car unread))))
10815                 read))
10816         (setq prev (1+ (car unread)))
10817         (setq unread (cdr unread)))
10818       (when (<= prev (cdr active))
10819         (push (cons prev (cdr active)) read))
10820       (setq read (if (> (length read) 1) (nreverse read) read))
10821       (if compute
10822           read
10823         (save-excursion
10824           (let (setmarkundo)
10825             ;; Propagate the read marks to the backend.
10826             (when (gnus-check-backend-function 'request-set-mark group)
10827               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10828                     (add (gnus-remove-from-range read (gnus-info-read info))))
10829                 (when (or add del)
10830                   (unless (gnus-check-group group)
10831                     (error "Can't open server for %s" group))
10832                   (gnus-request-set-mark
10833                    group (delq nil (list (if add (list add 'add '(read)))
10834                                          (if del (list del 'del '(read))))))
10835                   (setq setmarkundo
10836                         `(gnus-request-set-mark
10837                           ,group
10838                           ',(delq nil (list
10839                                        (if del (list del 'add '(read)))
10840                                        (if add (list add 'del '(read))))))))))
10841             (set-buffer gnus-group-buffer)
10842             (gnus-undo-register
10843               `(progn
10844                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10845                  (gnus-info-set-read ',info ',(gnus-info-read info))
10846                  (gnus-get-unread-articles-in-group ',info
10847                                                     (gnus-active ,group))
10848                  (gnus-group-update-group ,group t)
10849                  ,setmarkundo))))
10850         ;; Enter this list into the group info.
10851         (gnus-info-set-read info read)
10852         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10853         (gnus-get-unread-articles-in-group info (gnus-active group))
10854         t))))
10855
10856 (defun gnus-offer-save-summaries ()
10857   "Offer to save all active summary buffers."
10858   (let (buffers)
10859     ;; Go through all buffers and find all summaries.
10860     (dolist (buffer (buffer-list))
10861       (when (and (setq buffer (buffer-name buffer))
10862                  (string-match "Summary" buffer)
10863                  (save-excursion
10864                    (set-buffer buffer)
10865                    ;; We check that this is, indeed, a summary buffer.
10866                    (and (eq major-mode 'gnus-summary-mode)
10867                         ;; Also make sure this isn't bogus.
10868                         gnus-newsgroup-prepared
10869                         ;; Also make sure that this isn't a
10870                         ;; dead summary buffer.
10871                         (not gnus-dead-summary-mode))))
10872         (push buffer buffers)))
10873     ;; Go through all these summary buffers and offer to save them.
10874     (when buffers
10875       (save-excursion
10876         (map-y-or-n-p
10877          "Update summary buffer %s? "
10878          (lambda (buf)
10879            (switch-to-buffer buf)
10880            (gnus-summary-exit))
10881          buffers)))))
10882
10883 (defun gnus-summary-setup-default-charset ()
10884   "Setup newsgroup default charset."
10885   (if (equal gnus-newsgroup-name "nndraft:drafts")
10886       (setq gnus-newsgroup-charset nil)
10887     (let* ((ignored-charsets
10888             (or gnus-newsgroup-ephemeral-ignored-charsets
10889                 (append
10890                  (and gnus-newsgroup-name
10891                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10892                  gnus-newsgroup-ignored-charsets))))
10893       (setq gnus-newsgroup-charset
10894             (or gnus-newsgroup-ephemeral-charset
10895                 (and gnus-newsgroup-name
10896                      (gnus-parameter-charset gnus-newsgroup-name))
10897                 gnus-default-charset))
10898       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10899            ignored-charsets))))
10900
10901 ;;;
10902 ;;; Mime Commands
10903 ;;;
10904
10905 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10906   "Display the current article buffer fully MIME-buttonized.
10907 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10908 treated as multipart/mixed."
10909   (interactive "P")
10910   (require 'gnus-art)
10911   (let ((gnus-unbuttonized-mime-types nil)
10912         (gnus-mime-display-multipart-as-mixed show-all-parts))
10913     (gnus-summary-show-article)))
10914
10915 (defun gnus-summary-repair-multipart (article)
10916   "Add a Content-Type header to a multipart article without one."
10917   (interactive (list (gnus-summary-article-number)))
10918   (gnus-with-article article
10919     (message-narrow-to-head)
10920     (message-remove-header "Mime-Version")
10921     (goto-char (point-max))
10922     (insert "Mime-Version: 1.0\n")
10923     (widen)
10924     (when (search-forward "\n--" nil t)
10925       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10926         (message-narrow-to-head)
10927         (message-remove-header "Content-Type")
10928         (goto-char (point-max))
10929         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10930                         separator))
10931         (widen))))
10932   (let (gnus-mark-article-hook)
10933     (gnus-summary-select-article t t nil article)))
10934
10935 (defun gnus-summary-toggle-display-buttonized ()
10936   "Toggle the buttonizing of the article buffer."
10937   (interactive)
10938   (require 'gnus-art)
10939   (if (setq gnus-inhibit-mime-unbuttonizing
10940             (not gnus-inhibit-mime-unbuttonizing))
10941       (let ((gnus-unbuttonized-mime-types nil))
10942         (gnus-summary-show-article))
10943     (gnus-summary-show-article)))
10944
10945 ;;;
10946 ;;; Generic summary marking commands
10947 ;;;
10948
10949 (defvar gnus-summary-marking-alist
10950   '((read gnus-del-mark "d")
10951     (unread gnus-unread-mark "u")
10952     (ticked gnus-ticked-mark "!")
10953     (dormant gnus-dormant-mark "?")
10954     (expirable gnus-expirable-mark "e"))
10955   "An alist of names/marks/keystrokes.")
10956
10957 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10958 (defvar gnus-summary-mark-map)
10959
10960 (defun gnus-summary-make-all-marking-commands ()
10961   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10962   (dolist (elem gnus-summary-marking-alist)
10963     (apply 'gnus-summary-make-marking-command elem)))
10964
10965 (defun gnus-summary-make-marking-command (name mark keystroke)
10966   (let ((map (make-sparse-keymap)))
10967     (define-key gnus-summary-generic-mark-map keystroke map)
10968     (dolist (lway `((next "next" next nil "n")
10969                     (next-unread "next unread" next t "N")
10970                     (prev "previous" prev nil "p")
10971                     (prev-unread "previous unread" prev t "P")
10972                     (nomove "" nil nil ,keystroke)))
10973       (let ((func (gnus-summary-make-marking-command-1
10974                    mark (car lway) lway name)))
10975         (setq func (eval func))
10976         (define-key map (nth 4 lway) func)))))
10977
10978 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10979   `(defun ,(intern
10980             (format "gnus-summary-put-mark-as-%s%s"
10981                     name (if (eq way 'nomove)
10982                              ""
10983                            (concat "-" (symbol-name way)))))
10984      (n)
10985      ,(format
10986        "Mark the current article as %s%s.
10987 If N, the prefix, then repeat N times.
10988 If N is negative, move in reverse order.
10989 The difference between N and the actual number of articles marked is
10990 returned."
10991        name (cadr lway))
10992      (interactive "p")
10993      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10994
10995 (defun gnus-summary-generic-mark (n mark move unread)
10996   "Mark N articles with MARK."
10997   (unless (eq major-mode 'gnus-summary-mode)
10998     (error "This command can only be used in the summary buffer"))
10999   (gnus-summary-show-thread)
11000   (let ((nummove
11001          (cond
11002           ((eq move 'next) 1)
11003           ((eq move 'prev) -1)
11004           (t 0))))
11005     (if (zerop nummove)
11006         (setq n 1)
11007       (when (< n 0)
11008         (setq n (abs n)
11009               nummove (* -1 nummove))))
11010     (while (and (> n 0)
11011                 (gnus-summary-mark-article nil mark)
11012                 (zerop (gnus-summary-next-subject nummove unread t)))
11013       (setq n (1- n)))
11014     (when (/= 0 n)
11015       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11016     (gnus-summary-recenter)
11017     (gnus-summary-position-point)
11018     (gnus-set-mode-line 'summary)
11019     n))
11020
11021 (defun gnus-summary-insert-articles (articles)
11022   (when (setq articles
11023               (gnus-sorted-difference articles
11024                                       (mapcar (lambda (h)
11025                                                 (mail-header-number h))
11026                                               gnus-newsgroup-headers)))
11027     (setq gnus-newsgroup-headers
11028           (merge 'list
11029                  gnus-newsgroup-headers
11030                  (gnus-fetch-headers articles)
11031                  'gnus-article-sort-by-number))
11032     ;; Suppress duplicates?
11033     (when gnus-suppress-duplicates
11034       (gnus-dup-suppress-articles))
11035
11036     ;; We might want to build some more threads first.
11037     (when (and gnus-fetch-old-headers
11038                (eq gnus-headers-retrieved-by 'nov))
11039       (if (eq gnus-fetch-old-headers 'invisible)
11040           (gnus-build-all-threads)
11041         (gnus-build-old-threads)))
11042     ;; Let the Gnus agent mark articles as read.
11043     (when gnus-agent
11044       (gnus-agent-get-undownloaded-list))
11045     ;; Remove list identifiers from subject
11046     (when gnus-list-identifiers
11047       (gnus-summary-remove-list-identifiers))
11048     ;; First and last article in this newsgroup.
11049     (when gnus-newsgroup-headers
11050       (setq gnus-newsgroup-begin
11051             (mail-header-number (car gnus-newsgroup-headers))
11052             gnus-newsgroup-end
11053             (mail-header-number
11054              (gnus-last-element gnus-newsgroup-headers))))
11055     (when gnus-use-scoring
11056       (gnus-possibly-score-headers))))
11057
11058 (defun gnus-summary-insert-old-articles (&optional all)
11059   "Insert all old articles in this group.
11060 If ALL is non-nil, already read articles become readable.
11061 If ALL is a number, fetch this number of articles."
11062   (interactive "P")
11063   (prog1
11064       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11065             older len)
11066         (setq older
11067               (gnus-sorted-difference
11068                (gnus-uncompress-range (list gnus-newsgroup-active))
11069                old))
11070         (setq len (length older))
11071         (cond
11072          ((null older) nil)
11073          ((numberp all)
11074           (if (< all len)
11075               (setq older (last older all))))
11076          (all nil)
11077          (t
11078           (if (and (numberp gnus-large-newsgroup)
11079                    (> len gnus-large-newsgroup))
11080               (let* ((cursor-in-echo-area nil)
11081                      (initial (gnus-parameter-large-newsgroup-initial
11082                                gnus-newsgroup-name))
11083                      (input
11084                       (read-string
11085                        (format
11086                         "How many articles from %s (%s %d): "
11087                         (gnus-limit-string
11088                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11089                         (if initial "max" "default")
11090                         len)
11091                        (if initial
11092                            (cons (number-to-string initial)
11093                                  0)))))
11094                 (unless (string-match "^[ \t]*$" input)
11095                   (setq all (string-to-number input))
11096                   (if (< all len)
11097                       (setq older (last older all))))))))
11098         (if (not older)
11099             (message "No old news.")
11100           (gnus-summary-insert-articles older)
11101           (gnus-summary-limit (gnus-sorted-nunion old older))))
11102     (gnus-summary-position-point)))
11103
11104 (defun gnus-summary-insert-new-articles ()
11105   "Insert all new articles in this group."
11106   (interactive)
11107   (prog1
11108       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11109             (old-active gnus-newsgroup-active)
11110             (nnmail-fetched-sources (list t))
11111             i new)
11112         (setq gnus-newsgroup-active
11113               (gnus-activate-group gnus-newsgroup-name 'scan))
11114         (setq i (cdr gnus-newsgroup-active))
11115         (while (> i (cdr old-active))
11116           (push i new)
11117           (decf i))
11118         (if (not new)
11119             (message "No gnus is bad news.")
11120           (gnus-summary-insert-articles new)
11121           (setq gnus-newsgroup-unreads
11122                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11123           (gnus-summary-limit (gnus-sorted-nunion old new))))
11124     (gnus-summary-position-point)))
11125
11126 (gnus-summary-make-all-marking-commands)
11127
11128 (gnus-ems-redefine)
11129
11130 (provide 'gnus-sum)
11131
11132 (run-hooks 'gnus-sum-load-hook)
11133
11134 ;;; gnus-sum.el ends here