* gnus-agent.el (gnus-agent-group-make-menu-bar): Include all
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
50 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
51 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
52
53 (defcustom gnus-kill-summary-on-exit t
54   "*If non-nil, kill the summary buffer when you exit from it.
55 If nil, the summary will become a \"*Dead Summary*\" buffer, and
56 it will be killed sometime later."
57   :group 'gnus-summary-exit
58   :type 'boolean)
59
60 (defcustom gnus-fetch-old-headers nil
61   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
62 If an unread article in the group refers to an older, already read (or
63 just marked as read) article, the old article will not normally be
64 displayed in the Summary buffer.  If this variable is non-nil, Gnus
65 will attempt to grab the headers to the old articles, and thereby
66 build complete threads.  If it has the value `some', only enough
67 headers to connect otherwise loose threads will be displayed.  This
68 variable can also be a number.  In that case, no more than that number
69 of old headers will be fetched.  If it has the value `invisible', all
70 old headers will be fetched, but none will be displayed.
71
72 The server has to support NOV for any of this to work."
73   :group 'gnus-thread
74   :type '(choice (const :tag "off" nil)
75                  (const some)
76                  number
77                  (sexp :menu-tag "other" t)))
78
79 (defcustom gnus-refer-thread-limit 200
80   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
81 If t, fetch all the available old headers."
82   :group 'gnus-thread
83   :type '(choice number
84                  (sexp :menu-tag "other" t)))
85
86 (defcustom gnus-summary-make-false-root 'adopt
87   "*nil means that Gnus won't gather loose threads.
88 If the root of a thread has expired or been read in a previous
89 session, the information necessary to build a complete thread has been
90 lost.  Instead of having many small sub-threads from this original thread
91 scattered all over the summary buffer, Gnus can gather them.
92
93 If non-nil, Gnus will try to gather all loose sub-threads from an
94 original thread into one large thread.
95
96 If this variable is non-nil, it should be one of `none', `adopt',
97 `dummy' or `empty'.
98
99 If this variable is `none', Gnus will not make a false root, but just
100 present the sub-threads after another.
101 If this variable is `dummy', Gnus will create a dummy root that will
102 have all the sub-threads as children.
103 If this variable is `adopt', Gnus will make one of the \"children\"
104 the parent and mark all the step-children as such.
105 If this variable is `empty', the \"children\" are printed with empty
106 subject fields.  (Or rather, they will be printed with a string
107 given by the `gnus-summary-same-subject' variable.)"
108   :group 'gnus-thread
109   :type '(choice (const :tag "off" nil)
110                  (const none)
111                  (const dummy)
112                  (const adopt)
113                  (const empty)))
114
115 (defcustom gnus-summary-make-false-root-always nil
116   "Always make a false dummy root."
117   :group 'gnus-thread
118   :type 'boolean)
119
120 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
121   "*A regexp to match subjects to be excluded from loose thread gathering.
122 As loose thread gathering is done on subjects only, that means that
123 there can be many false gatherings performed.  By rooting out certain
124 common subjects, gathering might become saner."
125   :group 'gnus-thread
126   :type 'regexp)
127
128 (defcustom gnus-summary-gather-subject-limit nil
129   "*Maximum length of subject comparisons when gathering loose threads.
130 Use nil to compare full subjects.  Setting this variable to a low
131 number will help gather threads that have been corrupted by
132 newsreaders chopping off subject lines, but it might also mean that
133 unrelated articles that have subject that happen to begin with the
134 same few characters will be incorrectly gathered.
135
136 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
137 comparing subjects."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  (const fuzzy)
141                  (sexp :menu-tag "on" t)))
142
143 (defcustom gnus-simplify-subject-functions nil
144   "List of functions taking a string argument that simplify subjects.
145 The functions are applied recursively.
146
147 Useful functions to put in this list include:
148 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
149 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
150   :group 'gnus-thread
151   :type '(repeat function))
152
153 (defcustom gnus-simplify-ignored-prefixes nil
154   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  regexp))
158
159 (defcustom gnus-build-sparse-threads nil
160   "*If non-nil, fill in the gaps in threads.
161 If `some', only fill in the gaps that are needed to tie loose threads
162 together.  If `more', fill in all leaf nodes that Gnus can find.  If
163 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
164   :group 'gnus-thread
165   :type '(choice (const :tag "off" nil)
166                  (const some)
167                  (const more)
168                  (sexp :menu-tag "all" t)))
169
170 (defcustom gnus-summary-thread-gathering-function
171   'gnus-gather-threads-by-subject
172   "*Function used for gathering loose threads.
173 There are two pre-defined functions: `gnus-gather-threads-by-subject',
174 which only takes Subjects into consideration; and
175 `gnus-gather-threads-by-references', which compared the References
176 headers of the articles to find matches."
177   :group 'gnus-thread
178   :type '(radio (function-item gnus-gather-threads-by-subject)
179                 (function-item gnus-gather-threads-by-references)
180                 (function :tag "other")))
181
182 (defcustom gnus-summary-same-subject ""
183   "*String indicating that the current article has the same subject as the previous.
184 This variable will only be used if the value of
185 `gnus-summary-make-false-root' is `empty'."
186   :group 'gnus-summary-format
187   :type 'string)
188
189 (defcustom gnus-summary-goto-unread t
190   "*If t, many commands will go to the next unread article.
191 This applies to marking commands as well as other commands that
192 \"naturally\" select the next article, like, for instance, `SPC' at
193 the end of an article.
194
195 If nil, the marking commands do NOT go to the next unread article
196 \(they go to the next article instead).  If `never', commands that
197 usually go to the next unread article, will go to the next article,
198 whether it is read or not."
199   :group 'gnus-summary-marks
200   :link '(custom-manual "(gnus)Setting Marks")
201   :type '(choice (const :tag "off" nil)
202                  (const never)
203                  (sexp :menu-tag "on" t)))
204
205 (defcustom gnus-summary-default-score 0
206   "*Default article score level.
207 All scores generated by the score files will be added to this score.
208 If this variable is nil, scoring will be disabled."
209   :group 'gnus-score-default
210   :type '(choice (const :tag "disable")
211                  integer))
212
213 (defcustom gnus-summary-default-high-score 0
214   "*Default threshold for a high scored article.
215 An article will be highlighted as high scored if its score is greater
216 than this score."
217   :group 'gnus-score-default
218   :type 'integer)
219
220 (defcustom gnus-summary-default-low-score 0
221   "*Default threshold for a low scored article.
222 An article will be highlighted as low scored if its score is smaller
223 than this score."
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-zcore-fuzz 0
228   "*Fuzziness factor for the zcore in the summary buffer.
229 Articles with scores closer than this to `gnus-summary-default-score'
230 will not be marked."
231   :group 'gnus-summary-format
232   :type 'integer)
233
234 (defcustom gnus-simplify-subject-fuzzy-regexp nil
235   "*Strings to be removed when doing fuzzy matches.
236 This can either be a regular expression or list of regular expressions
237 that will be removed from subject strings if fuzzy subject
238 simplification is selected."
239   :group 'gnus-thread
240   :type '(repeat regexp))
241
242 (defcustom gnus-show-threads t
243   "*If non-nil, display threads in summary mode."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-subtree nil
248   "*If non-nil, hide all threads initially.
249 This can be a predicate specifier which says which threads to hide.
250 If threads are hidden, you have to run the command
251 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
252 to expose hidden threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-killed t
257   "*If non-nil, hide killed threads automatically."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-ignore-subject t
262   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
263 If nil, articles that have different subjects from their parents will
264 start separate threads."
265   :group 'gnus-thread
266   :type 'boolean)
267
268 (defcustom gnus-thread-operation-ignore-subject t
269   "*If non-nil, subjects will be ignored when doing thread commands.
270 This affects commands like `gnus-summary-kill-thread' and
271 `gnus-summary-lower-thread'.
272
273 If this variable is nil, articles in the same thread with different
274 subjects will not be included in the operation in question.  If this
275 variable is `fuzzy', only articles that have subjects that are fuzzily
276 equal will be included."
277   :group 'gnus-thread
278   :type '(choice (const :tag "off" nil)
279                  (const fuzzy)
280                  (sexp :tag "on" t)))
281
282 (defcustom gnus-thread-indent-level 4
283   "*Number that says how much each sub-thread should be indented."
284   :group 'gnus-thread
285   :type 'integer)
286
287 (defcustom gnus-auto-extend-newsgroup t
288   "*If non-nil, extend newsgroup forward and backward when requested."
289   :group 'gnus-summary-choose
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-first t
293   "*If non-nil, select the article under point.
294 Which article this is is controlled by the `gnus-auto-select-subject'
295 variable.
296
297 If you want to prevent automatic selection of articles in some
298 newsgroups, set the variable to nil in `gnus-select-group-hook'."
299   :group 'gnus-group-select
300   :type '(choice (const :tag "none" nil)
301                  (sexp :menu-tag "first" t)))
302
303 (defcustom gnus-auto-select-subject 'unread
304   "*Says what subject to place under point when entering a group.
305
306 This variable can either be the symbols `first' (place point on the
307 first subject), `unread' (place point on the subject line of the first
308 unread article), `best' (place point on the subject line of the
309 higest-scored article), `unseen' (place point on the subject line of
310 the first unseen article), 'unseen-or-unread' (place point on the subject
311 line of the first unseen article or, if all article have been seen, on the
312 subject line of the first unread article), or a function to be called to
313 place point on some subject line."
314   :group 'gnus-group-select
315   :type '(choice (const best)
316                  (const unread)
317                  (const first)
318                  (const unseen)
319                  (const unseen-or-unread)))
320
321 (defcustom gnus-auto-select-next t
322   "*If non-nil, offer to go to the next group from the end of the previous.
323 If the value is t and the next newsgroup is empty, Gnus will exit
324 summary mode and go back to group mode.  If the value is neither nil
325 nor t, Gnus will select the following unread newsgroup.  In
326 particular, if the value is the symbol `quietly', the next unread
327 newsgroup will be selected without any confirmation, and if it is
328 `almost-quietly', the next group will be selected without any
329 confirmation if you are located on the last article in the group.
330 Finally, if this variable is `slightly-quietly', the `Z n' command
331 will go to the next group without confirmation."
332   :group 'gnus-summary-maneuvering
333   :type '(choice (const :tag "off" nil)
334                  (const quietly)
335                  (const almost-quietly)
336                  (const slightly-quietly)
337                  (sexp :menu-tag "on" t)))
338
339 (defcustom gnus-auto-select-same nil
340   "*If non-nil, select the next article with the same subject.
341 If there are no more articles with the same subject, go to
342 the first unread article."
343   :group 'gnus-summary-maneuvering
344   :type 'boolean)
345
346 (defcustom gnus-summary-check-current nil
347   "*If non-nil, consider the current article when moving.
348 The \"unread\" movement commands will stay on the same line if the
349 current article is unread."
350   :group 'gnus-summary-maneuvering
351   :type 'boolean)
352
353 (defcustom gnus-auto-center-summary t
354   "*If non-nil, always center the current summary buffer.
355 In particular, if `vertical' do only vertical recentering.  If non-nil
356 and non-`vertical', do both horizontal and vertical recentering."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "none" nil)
359                  (const vertical)
360                  (integer :tag "height")
361                  (sexp :menu-tag "both" t)))
362
363 (defvar gnus-auto-center-group t
364   "*If non-nil, always center the group buffer.")
365
366 (defcustom gnus-show-all-headers nil
367   "*If non-nil, don't hide any headers."
368   :group 'gnus-article-hiding
369   :group 'gnus-article-headers
370   :type 'boolean)
371
372 (defcustom gnus-summary-ignore-duplicates nil
373   "*If non-nil, ignore articles with identical Message-ID headers."
374   :group 'gnus-summary
375   :type 'boolean)
376
377 (defcustom gnus-single-article-buffer t
378   "*If non-nil, display all articles in the same buffer.
379 If nil, each group will get its own article buffer."
380   :group 'gnus-article-various
381   :type 'boolean)
382
383 (defcustom gnus-break-pages t
384   "*If non-nil, do page breaking on articles.
385 The page delimiter is specified by the `gnus-page-delimiter'
386 variable."
387   :group 'gnus-article-various
388   :type 'boolean)
389
390 (defcustom gnus-move-split-methods nil
391   "*Variable used to suggest where articles are to be moved to.
392 It uses the same syntax as the `gnus-split-methods' variable.
393 However, whereas `gnus-split-methods' specifies file names as targets,
394 this variable specifies group names."
395   :group 'gnus-summary-mail
396   :type '(repeat (choice (list :value (fun) function)
397                          (cons :value ("" "") regexp (repeat string))
398                          (sexp :value nil))))
399
400 (defcustom gnus-unread-mark ?           ;Whitespace
401   "*Mark used for unread articles."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-ticked-mark ?!
406   "*Mark used for ticked articles."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-dormant-mark ??
411   "*Mark used for dormant articles."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-del-mark ?r
416   "*Mark used for del'd articles."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-read-mark ?R
421   "*Mark used for read articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-expirable-mark ?E
426   "*Mark used for expirable articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-killed-mark ?K
431   "*Mark used for killed articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-spam-mark ?$
436   "*Mark used for spam articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-souped-mark ?F
441   "*Mark used for souped articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-kill-file-mark ?X
446   "*Mark used for articles killed by kill files."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-low-score-mark ?Y
451   "*Mark used for articles with a low score."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-catchup-mark ?C
456   "*Mark used for articles that are caught up."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-replied-mark ?A
461   "*Mark used for articles that have been replied to."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-forwarded-mark ?F
466   "*Mark used for articles that have been forwarded."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-recent-mark ?N
471   "*Mark used for articles that are recent."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-cached-mark ?*
476   "*Mark used for articles that are in the cache."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-saved-mark ?S
481   "*Mark used for articles that have been saved."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-unseen-mark ?.
486   "*Mark used for articles that haven't been seen."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-no-mark ?               ;Whitespace
491   "*Mark used for articles that have no other secondary mark."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-ancient-mark ?O
496   "*Mark used for ancient articles."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-sparse-mark ?Q
501   "*Mark used for sparsely reffed articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-canceled-mark ?G
506   "*Mark used for canceled articles."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-duplicate-mark ?M
511   "*Mark used for duplicate articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-undownloaded-mark ?-
516   "*Mark used for articles that weren't downloaded."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-downloaded-mark ?+
521   "*Mark used for articles that were downloaded."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-downloadable-mark ?%
526   "*Mark used for articles that are to be downloaded."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-unsendable-mark ?=
531   "*Mark used for articles that won't be sent."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-score-over-mark ?+
536   "*Score mark used for articles with high scores."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-score-below-mark ?-
541   "*Score mark used for articles with low scores."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-empty-thread-mark ?     ;Whitespace
546   "*There is no thread under the article."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-not-empty-thread-mark ?=
551   "*There is a thread under the article."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-view-pseudo-asynchronously nil
556   "*If non-nil, Gnus will view pseudo-articles asynchronously."
557   :group 'gnus-extract-view
558   :type 'boolean)
559
560 (defcustom gnus-auto-expirable-marks
561   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
562         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
563         gnus-souped-mark gnus-duplicate-mark)
564   "*The list of marks converted into expiration if a group is auto-expirable."
565   :version "21.1"
566   :group 'gnus-summary
567   :type '(repeat character))
568
569 (defcustom gnus-inhibit-user-auto-expire t
570   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
571   :version "21.1"
572   :group 'gnus-summary
573   :type 'boolean)
574
575 (defcustom gnus-view-pseudos nil
576   "*If `automatic', pseudo-articles will be viewed automatically.
577 If `not-confirm', pseudos will be viewed automatically, and the user
578 will not be asked to confirm the command."
579   :group 'gnus-extract-view
580   :type '(choice (const :tag "off" nil)
581                  (const automatic)
582                  (const not-confirm)))
583
584 (defcustom gnus-view-pseudos-separately t
585   "*If non-nil, one pseudo-article will be created for each file to be viewed.
586 If nil, all files that use the same viewing command will be given as a
587 list of parameters to that command."
588   :group 'gnus-extract-view
589   :type 'boolean)
590
591 (defcustom gnus-insert-pseudo-articles t
592   "*If non-nil, insert pseudo-articles when decoding articles."
593   :group 'gnus-extract-view
594   :type 'boolean)
595
596 (defcustom gnus-summary-dummy-line-format
597   "   %(:                             :%) %S\n"
598   "*The format specification for the dummy roots in the summary buffer.
599 It works along the same lines as a normal formatting string,
600 with some simple extensions.
601
602 %S  The subject
603
604 General format specifiers can also be used.
605 See `(gnus)Formatting Variables'."
606   :link '(custom-manual "(gnus)Formatting Variables")
607   :group 'gnus-threading
608   :type 'string)
609
610 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
611   "*The format specification for the summary mode line.
612 It works along the same lines as a normal formatting string,
613 with some simple extensions:
614
615 %G  Group name
616 %p  Unprefixed group name
617 %A  Current article number
618 %z  Current article score
619 %V  Gnus version
620 %U  Number of unread articles in the group
621 %e  Number of unselected articles in the group
622 %Z  A string with unread/unselected article counts
623 %g  Shortish group name
624 %S  Subject of the current article
625 %u  User-defined spec
626 %s  Current score file name
627 %d  Number of dormant articles
628 %r  Number of articles that have been marked as read in this session
629 %E  Number of articles expunged by the score files"
630   :group 'gnus-summary-format
631   :type 'string)
632
633 (defcustom gnus-list-identifiers nil
634   "Regexp that matches list identifiers to be removed from subject.
635 This can also be a list of regexps."
636   :version "21.1"
637   :group 'gnus-summary-format
638   :group 'gnus-article-hiding
639   :type '(choice (const :tag "none" nil)
640                  (regexp :value ".*")
641                  (repeat :value (".*") regexp)))
642
643 (defcustom gnus-summary-mark-below 0
644   "*Mark all articles with a score below this variable as read.
645 This variable is local to each summary buffer and usually set by the
646 score file."
647   :group 'gnus-score-default
648   :type 'integer)
649
650 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
651   "*List of functions used for sorting articles in the summary buffer.
652
653 Each function takes two articles and returns non-nil if the first
654 article should be sorted before the other.  If you use more than one
655 function, the primary sort function should be the last.  You should
656 probably always include `gnus-article-sort-by-number' in the list of
657 sorting functions -- preferably first.  Also note that sorting by date
658 is often much slower than sorting by number, and the sorting order is
659 very similar.  (Sorting by date means sorting by the time the message
660 was sent, sorting by number means sorting by arrival time.)
661
662 Ready-made functions include `gnus-article-sort-by-number',
663 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
664 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
665 and `gnus-article-sort-by-score'.
666
667 When threading is turned on, the variable `gnus-thread-sort-functions'
668 controls how articles are sorted."
669   :group 'gnus-summary-sort
670   :type '(repeat (choice (function-item gnus-article-sort-by-number)
671                          (function-item gnus-article-sort-by-author)
672                          (function-item gnus-article-sort-by-subject)
673                          (function-item gnus-article-sort-by-date)
674                          (function-item gnus-article-sort-by-score)
675                          (function-item gnus-article-sort-by-random)
676                          (function :tag "other"))))
677
678 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
679   "*List of functions used for sorting threads in the summary buffer.
680 By default, threads are sorted by article number.
681
682 Each function takes two threads and returns non-nil if the first
683 thread should be sorted before the other.  If you use more than one
684 function, the primary sort function should be the last.  You should
685 probably always include `gnus-thread-sort-by-number' in the list of
686 sorting functions -- preferably first.  Also note that sorting by date
687 is often much slower than sorting by number, and the sorting order is
688 very similar.  (Sorting by date means sorting by the time the message
689 was sent, sorting by number means sorting by arrival time.)
690
691 Ready-made functions include `gnus-thread-sort-by-number',
692 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
693 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
694 `gnus-thread-sort-by-most-recent-number',
695 `gnus-thread-sort-by-most-recent-date',
696 `gnus-thread-sort-by-random', and
697 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
698
699 When threading is turned off, the variable
700 `gnus-article-sort-functions' controls how articles are sorted."
701   :group 'gnus-summary-sort
702   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
703                          (function-item gnus-thread-sort-by-author)
704                          (function-item gnus-thread-sort-by-subject)
705                          (function-item gnus-thread-sort-by-date)
706                          (function-item gnus-thread-sort-by-score)
707                          (function-item gnus-thread-sort-by-total-score)
708                          (function-item gnus-thread-sort-by-random)
709                          (function :tag "other"))))
710
711 (defcustom gnus-thread-score-function '+
712   "*Function used for calculating the total score of a thread.
713
714 The function is called with the scores of the article and each
715 subthread and should then return the score of the thread.
716
717 Some functions you can use are `+', `max', or `min'."
718   :group 'gnus-summary-sort
719   :type 'function)
720
721 (defcustom gnus-summary-expunge-below nil
722   "All articles that have a score less than this variable will be expunged.
723 This variable is local to the summary buffers."
724   :group 'gnus-score-default
725   :type '(choice (const :tag "off" nil)
726                  integer))
727
728 (defcustom gnus-thread-expunge-below nil
729   "All threads that have a total score less than this variable will be expunged.
730 See `gnus-thread-score-function' for en explanation of what a
731 \"thread score\" is.
732
733 This variable is local to the summary buffers."
734   :group 'gnus-threading
735   :group 'gnus-score-default
736   :type '(choice (const :tag "off" nil)
737                  integer))
738
739 (defcustom gnus-summary-mode-hook nil
740   "*A hook for Gnus summary mode.
741 This hook is run before any variables are set in the summary buffer."
742   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
743   :group 'gnus-summary-various
744   :type 'hook)
745
746 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
747 (when (featurep 'xemacs)
748   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
749   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
750   (add-hook 'gnus-summary-mode-hook
751             'gnus-xmas-switch-horizontal-scrollbar-off))
752
753 (defcustom gnus-summary-menu-hook nil
754   "*Hook run after the creation of the summary mode menu."
755   :group 'gnus-summary-visual
756   :type 'hook)
757
758 (defcustom gnus-summary-exit-hook nil
759   "*A hook called on exit from the summary buffer.
760 It will be called with point in the group buffer."
761   :group 'gnus-summary-exit
762   :type 'hook)
763
764 (defcustom gnus-summary-prepare-hook nil
765   "*A hook called after the summary buffer has been generated.
766 If you want to modify the summary buffer, you can use this hook."
767   :group 'gnus-summary-various
768   :type 'hook)
769
770 (defcustom gnus-summary-prepared-hook nil
771   "*A hook called as the last thing after the summary buffer has been generated."
772   :group 'gnus-summary-various
773   :type 'hook)
774
775 (defcustom gnus-summary-generate-hook nil
776   "*A hook run just before generating the summary buffer.
777 This hook is commonly used to customize threading variables and the
778 like."
779   :group 'gnus-summary-various
780   :type 'hook)
781
782 (defcustom gnus-select-group-hook nil
783   "*A hook called when a newsgroup is selected.
784
785 If you'd like to simplify subjects like the
786 `gnus-summary-next-same-subject' command does, you can use the
787 following hook:
788
789  (add-hook gnus-select-group-hook
790            (lambda ()
791              (mapcar (lambda (header)
792                        (mail-header-set-subject
793                         header
794                         (gnus-simplify-subject
795                          (mail-header-subject header) 're-only)))
796                      gnus-newsgroup-headers)))"
797   :group 'gnus-group-select
798   :type 'hook)
799
800 (defcustom gnus-select-article-hook nil
801   "*A hook called when an article is selected."
802   :group 'gnus-summary-choose
803   :options '(gnus-agent-fetch-selected-article)
804   :type 'hook)
805
806 (defcustom gnus-visual-mark-article-hook
807   (list 'gnus-highlight-selected-summary)
808   "*Hook run after selecting an article in the summary buffer.
809 It is meant to be used for highlighting the article in some way.  It
810 is not run if `gnus-visual' is nil."
811   :group 'gnus-summary-visual
812   :type 'hook)
813
814 (defcustom gnus-parse-headers-hook nil
815   "*A hook called before parsing the headers."
816   :group 'gnus-various
817   :type 'hook)
818
819 (defcustom gnus-exit-group-hook nil
820   "*A hook called when exiting summary mode.
821 This hook is not called from the non-updating exit commands like `Q'."
822   :group 'gnus-various
823   :type 'hook)
824
825 (defcustom gnus-summary-update-hook
826   (list 'gnus-summary-highlight-line)
827   "*A hook called when a summary line is changed.
828 The hook will not be called if `gnus-visual' is nil.
829
830 The default function `gnus-summary-highlight-line' will
831 highlight the line according to the `gnus-summary-highlight'
832 variable."
833   :group 'gnus-summary-visual
834   :type 'hook)
835
836 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
837   "*A hook called when an article is selected for the first time.
838 The hook is intended to mark an article as read (or unread)
839 automatically when it is selected."
840   :group 'gnus-summary-choose
841   :type 'hook)
842
843 (defcustom gnus-group-no-more-groups-hook nil
844   "*A hook run when returning to group mode having no more (unread) groups."
845   :group 'gnus-group-select
846   :type 'hook)
847
848 (defcustom gnus-ps-print-hook nil
849   "*A hook run before ps-printing something from Gnus."
850   :group 'gnus-summary
851   :type 'hook)
852
853 (defcustom gnus-summary-article-move-hook nil
854   "*A hook called after an article is moved, copied, respooled, or crossposted."
855   :group 'gnus-summary
856   :type 'hook)
857
858 (defcustom gnus-summary-article-delete-hook nil
859   "*A hook called after an article is deleted."
860   :group 'gnus-summary
861   :type 'hook)
862
863 (defcustom gnus-summary-article-expire-hook nil
864   "*A hook called after an article is expired."
865   :group 'gnus-summary
866   :type 'hook)
867
868 (defcustom gnus-summary-display-arrow
869   (and (fboundp 'display-graphic-p)
870        (display-graphic-p))
871   "*If non-nil, display an arrow highlighting the current article."
872   :version "21.1"
873   :group 'gnus-summary
874   :type 'boolean)
875
876 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
877   "Face used for highlighting the current article in the summary buffer."
878   :group 'gnus-summary-visual
879   :type 'face)
880
881 (defvar gnus-tmp-downloaded nil)
882
883 (defcustom gnus-summary-highlight
884   '(((eq mark gnus-canceled-mark)
885      . gnus-summary-cancelled-face)
886     ((and uncached (> score default-high))
887      . gnus-summary-high-undownloaded-face)
888     ((and uncached (< score default-low))
889      . gnus-summary-low-undownloaded-face)
890     (uncached
891      . gnus-summary-normal-undownloaded-face)
892     ((and (> score default-high)
893           (or (eq mark gnus-dormant-mark)
894               (eq mark gnus-ticked-mark)))
895      . gnus-summary-high-ticked-face)
896     ((and (< score default-low)
897           (or (eq mark gnus-dormant-mark)
898               (eq mark gnus-ticked-mark)))
899      . gnus-summary-low-ticked-face)
900     ((or (eq mark gnus-dormant-mark)
901          (eq mark gnus-ticked-mark))
902      . gnus-summary-normal-ticked-face)
903     ((and (> score default-high) (eq mark gnus-ancient-mark))
904      . gnus-summary-high-ancient-face)
905     ((and (< score default-low) (eq mark gnus-ancient-mark))
906      . gnus-summary-low-ancient-face)
907     ((eq mark gnus-ancient-mark)
908      . gnus-summary-normal-ancient-face)
909     ((and (> score default-high) (eq mark gnus-unread-mark))
910      . gnus-summary-high-unread-face)
911     ((and (< score default-low) (eq mark gnus-unread-mark))
912      . gnus-summary-low-unread-face)
913     ((eq mark gnus-unread-mark)
914      . gnus-summary-normal-unread-face)
915     ((> score default-high)
916      . gnus-summary-high-read-face)
917     ((< score default-low)
918      . gnus-summary-low-read-face)
919     (t
920      . gnus-summary-normal-read-face))
921   "*Controls the highlighting of summary buffer lines.
922
923 A list of (FORM . FACE) pairs.  When deciding how a a particular
924 summary line should be displayed, each form is evaluated.  The content
925 of the face field after the first true form is used.  You can change
926 how those summary lines are displayed, by editing the face field.
927
928 You can use the following variables in the FORM field.
929
930 score:        The article's score
931 default:      The default article score.
932 default-high: The default score for high scored articles.
933 default-low:  The default score for low scored articles.
934 below:        The score below which articles are automatically marked as read.
935 mark:         The articles mark."
936   :group 'gnus-summary-visual
937   :type '(repeat (cons (sexp :tag "Form" nil)
938                        face)))
939
940 (defcustom gnus-alter-header-function nil
941   "Function called to allow alteration of article header structures.
942 The function is called with one parameter, the article header vector,
943 which it may alter in any way."
944   :type '(choice (const :tag "None" nil)
945                  function)
946   :group 'gnus-summary)
947
948 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
949   "Variable that says which function should be used to decode a string with encoded words.")
950
951 (defcustom gnus-extra-headers '(To Newsgroups)
952   "*Extra headers to parse."
953   :version "21.1"
954   :group 'gnus-summary
955   :type '(repeat symbol))
956
957 (defcustom gnus-ignored-from-addresses
958   (and user-mail-address (regexp-quote user-mail-address))
959   "*Regexp of From headers that may be suppressed in favor of To headers."
960   :version "21.1"
961   :group 'gnus-summary
962   :type 'regexp)
963
964 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
965   "List of charsets that should be ignored.
966 When these charsets are used in the \"charset\" parameter, the
967 default charset will be used instead."
968   :version "21.1"
969   :type '(repeat symbol)
970   :group 'gnus-charset)
971
972 (gnus-define-group-parameter
973  ignored-charsets
974  :type list
975  :function-document
976  "Return the ignored charsets of GROUP."
977  :variable gnus-group-ignored-charsets-alist
978  :variable-default
979  '(("alt\\.chinese\\.text" iso-8859-1))
980  :variable-document
981  "Alist of regexps (to match group names) and charsets that should be ignored.
982 When these charsets are used in the \"charset\" parameter, the
983 default charset will be used instead."
984  :variable-group gnus-charset
985  :variable-type '(repeat (cons (regexp :tag "Group")
986                                (repeat symbol)))
987  :parameter-type '(choice :tag "Ignored charsets"
988                           :value nil
989                           (repeat (symbol)))
990  :parameter-document       "\
991 List of charsets that should be ignored.
992
993 When these charsets are used in the \"charset\" parameter, the
994 default charset will be used instead.")
995
996 (defcustom gnus-group-highlight-words-alist nil
997   "Alist of group regexps and highlight regexps.
998 This variable uses the same syntax as `gnus-emphasis-alist'."
999   :version "21.1"
1000   :type '(repeat (cons (regexp :tag "Group")
1001                        (repeat (list (regexp :tag "Highlight regexp")
1002                                      (number :tag "Group for entire word" 0)
1003                                      (number :tag "Group for displayed part" 0)
1004                                      (symbol :tag "Face"
1005                                              gnus-emphasis-highlight-words)))))
1006   :group 'gnus-summary-visual)
1007
1008 (defcustom gnus-summary-show-article-charset-alist
1009   nil
1010   "Alist of number and charset.
1011 The article will be shown with the charset corresponding to the
1012 numbered argument.
1013 For example: ((1 . cn-gb-2312) (2 . big5))."
1014   :version "21.1"
1015   :type '(repeat (cons (number :tag "Argument" 1)
1016                        (symbol :tag "Charset")))
1017   :group 'gnus-charset)
1018
1019 (defcustom gnus-preserve-marks t
1020   "Whether marks are preserved when moving, copying and respooling messages."
1021   :version "21.1"
1022   :type 'boolean
1023   :group 'gnus-summary-marks)
1024
1025 (defcustom gnus-alter-articles-to-read-function nil
1026   "Function to be called to alter the list of articles to be selected."
1027   :type '(choice (const nil) function)
1028   :group 'gnus-summary)
1029
1030 (defcustom gnus-orphan-score nil
1031   "*All orphans get this score added.  Set in the score file."
1032   :group 'gnus-score-default
1033   :type '(choice (const nil)
1034                  integer))
1035
1036 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1037   "*A regexp to match MIME parts when saving multiple parts of a message
1038 with gnus-summary-save-parts (X m). This regexp will be used by default
1039 when prompting the user for which type of files to save."
1040   :group 'gnus-summary
1041   :type 'regexp)
1042
1043 (defcustom gnus-read-all-available-headers nil
1044   "Whether Gnus should parse all headers made available to it.
1045 This is mostly relevant for slow backends where the user may
1046 wish to widen the summary buffer to include all headers
1047 that were fetched.  Say, for nnultimate groups."
1048   :group 'gnus-summary
1049   :type '(choice boolean regexp))
1050
1051 (defcustom gnus-summary-muttprint-program "muttprint"
1052   "Command (and optional arguments) used to run Muttprint."
1053   :version "21.3"
1054   :group 'gnus-summary
1055   :type 'string)
1056
1057 (defcustom gnus-article-loose-mime nil
1058   "If non-nil, don't require MIME-Version header.
1059 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1060 supply the MIME-Version header or deliberately strip it From the mail.
1061 Set it to non-nil, Gnus will treat some articles as MIME even if
1062 the MIME-Version header is missed."
1063   :version "21.3"
1064   :type 'boolean
1065   :group 'gnus-article)
1066
1067 (defcustom gnus-article-emulate-mime t
1068   "If non-nil, use MIME emulation for uuencode and the like.
1069 This means that Gnus will search message bodies for text that look
1070 like uuencoded bits, yEncoded bits, and so on, and present that using
1071 the normal Gnus MIME machinery."
1072   :type 'boolean
1073   :group 'gnus-article)
1074
1075 ;;; Internal variables
1076
1077 (defvar gnus-summary-display-cache nil)
1078 (defvar gnus-article-mime-handles nil)
1079 (defvar gnus-article-decoded-p nil)
1080 (defvar gnus-article-charset nil)
1081 (defvar gnus-article-ignored-charsets nil)
1082 (defvar gnus-scores-exclude-files nil)
1083 (defvar gnus-page-broken nil)
1084
1085 (defvar gnus-original-article nil)
1086 (defvar gnus-article-internal-prepare-hook nil)
1087 (defvar gnus-newsgroup-process-stack nil)
1088
1089 (defvar gnus-thread-indent-array nil)
1090 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1091 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1092   "Function called to sort the articles within a thread after it has been gathered together.")
1093
1094 (defvar gnus-summary-save-parts-type-history nil)
1095 (defvar gnus-summary-save-parts-last-directory nil)
1096
1097 ;; Avoid highlighting in kill files.
1098 (defvar gnus-summary-inhibit-highlight nil)
1099 (defvar gnus-newsgroup-selected-overlay nil)
1100 (defvar gnus-inhibit-limiting nil)
1101 (defvar gnus-newsgroup-adaptive-score-file nil)
1102 (defvar gnus-current-score-file nil)
1103 (defvar gnus-current-move-group nil)
1104 (defvar gnus-current-copy-group nil)
1105 (defvar gnus-current-crosspost-group nil)
1106 (defvar gnus-newsgroup-display nil)
1107
1108 (defvar gnus-newsgroup-dependencies nil)
1109 (defvar gnus-newsgroup-adaptive nil)
1110 (defvar gnus-summary-display-article-function nil)
1111 (defvar gnus-summary-highlight-line-function nil
1112   "Function called after highlighting a summary line.")
1113
1114 (defvar gnus-summary-line-format-alist
1115   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1116     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1117     (?s gnus-tmp-subject-or-nil ?s)
1118     (?n gnus-tmp-name ?s)
1119     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1120         ?s)
1121     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1122             gnus-tmp-from) ?s)
1123     (?F gnus-tmp-from ?s)
1124     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1125     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1126     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1127     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1128     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1129     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1130     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1131     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1132     (?L gnus-tmp-lines ?s)
1133     (?O gnus-tmp-downloaded ?c)
1134     (?I gnus-tmp-indentation ?s)
1135     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1136     (?R gnus-tmp-replied ?c)
1137     (?\[ gnus-tmp-opening-bracket ?c)
1138     (?\] gnus-tmp-closing-bracket ?c)
1139     (?\> (make-string gnus-tmp-level ? ) ?s)
1140     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1141     (?i gnus-tmp-score ?d)
1142     (?z gnus-tmp-score-char ?c)
1143     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1144     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1145     (?U gnus-tmp-unread ?c)
1146     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1147         ?s)
1148     (?t (gnus-summary-number-of-articles-in-thread
1149          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1150         ?d)
1151     (?e (gnus-summary-number-of-articles-in-thread
1152          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1153         ?c)
1154     (?u gnus-tmp-user-defined ?s)
1155     (?P (gnus-pick-line-number) ?d)
1156     (?B gnus-tmp-thread-tree-header-string ?s)
1157     (user-date (gnus-user-date
1158                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1159   "An alist of format specifications that can appear in summary lines.
1160 These are paired with what variables they correspond with, along with
1161 the type of the variable (string, integer, character, etc).")
1162
1163 (defvar gnus-summary-dummy-line-format-alist
1164   `((?S gnus-tmp-subject ?s)
1165     (?N gnus-tmp-number ?d)
1166     (?u gnus-tmp-user-defined ?s)))
1167
1168 (defvar gnus-summary-mode-line-format-alist
1169   `((?G gnus-tmp-group-name ?s)
1170     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1171     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1172     (?A gnus-tmp-article-number ?d)
1173     (?Z gnus-tmp-unread-and-unselected ?s)
1174     (?V gnus-version ?s)
1175     (?U gnus-tmp-unread-and-unticked ?d)
1176     (?S gnus-tmp-subject ?s)
1177     (?e gnus-tmp-unselected ?d)
1178     (?u gnus-tmp-user-defined ?s)
1179     (?d (length gnus-newsgroup-dormant) ?d)
1180     (?t (length gnus-newsgroup-marked) ?d)
1181     (?h (length gnus-newsgroup-spam-marked) ?d)
1182     (?r (length gnus-newsgroup-reads) ?d)
1183     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1184     (?E gnus-newsgroup-expunged-tally ?d)
1185     (?s (gnus-current-score-file-nondirectory) ?s)))
1186
1187 (defvar gnus-last-search-regexp nil
1188   "Default regexp for article search command.")
1189
1190 (defvar gnus-last-shell-command nil
1191   "Default shell command on article.")
1192
1193 (defvar gnus-newsgroup-agentized nil
1194   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1195 (defvar gnus-newsgroup-begin nil)
1196 (defvar gnus-newsgroup-end nil)
1197 (defvar gnus-newsgroup-last-rmail nil)
1198 (defvar gnus-newsgroup-last-mail nil)
1199 (defvar gnus-newsgroup-last-folder nil)
1200 (defvar gnus-newsgroup-last-file nil)
1201 (defvar gnus-newsgroup-auto-expire nil)
1202 (defvar gnus-newsgroup-active nil)
1203
1204 (defvar gnus-newsgroup-data nil)
1205 (defvar gnus-newsgroup-data-reverse nil)
1206 (defvar gnus-newsgroup-limit nil)
1207 (defvar gnus-newsgroup-limits nil)
1208
1209 (defvar gnus-newsgroup-unreads nil
1210   "Sorted list of unread articles in the current newsgroup.")
1211
1212 (defvar gnus-newsgroup-unselected nil
1213   "Sorted list of unselected unread articles in the current newsgroup.")
1214
1215 (defvar gnus-newsgroup-reads nil
1216   "Alist of read articles and article marks in the current newsgroup.")
1217
1218 (defvar gnus-newsgroup-expunged-tally nil)
1219
1220 (defvar gnus-newsgroup-marked nil
1221   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1222
1223 (defvar gnus-newsgroup-spam-marked nil
1224   "List of ranges of articles that have been marked as spam.")
1225
1226 (defvar gnus-newsgroup-killed nil
1227   "List of ranges of articles that have been through the scoring process.")
1228
1229 (defvar gnus-newsgroup-cached nil
1230   "Sorted list of articles that come from the article cache.")
1231
1232 (defvar gnus-newsgroup-saved nil
1233   "List of articles that have been saved.")
1234
1235 (defvar gnus-newsgroup-kill-headers nil)
1236
1237 (defvar gnus-newsgroup-replied nil
1238   "List of articles that have been replied to in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-forwarded nil
1241   "List of articles that have been forwarded in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-recent nil
1244   "List of articles that have are recent in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-expirable nil
1247   "Sorted list of articles in the current newsgroup that can be expired.")
1248
1249 (defvar gnus-newsgroup-processable nil
1250   "List of articles in the current newsgroup that can be processed.")
1251
1252 (defvar gnus-newsgroup-downloadable nil
1253   "Sorted list of articles in the current newsgroup that can be processed.")
1254
1255 (defvar gnus-newsgroup-unfetched nil
1256   "Sorted list of articles in the current newsgroup whose headers have not been fetched into the agent.")
1257
1258 (defvar gnus-newsgroup-undownloaded nil
1259   "List of articles in the current newsgroup that haven't been downloaded..")
1260
1261 (defvar gnus-newsgroup-unsendable nil
1262   "List of articles in the current newsgroup that won't be sent.")
1263
1264 (defvar gnus-newsgroup-bookmarks nil
1265   "List of articles in the current newsgroup that have bookmarks.")
1266
1267 (defvar gnus-newsgroup-dormant nil
1268   "Sorted list of dormant articles in the current newsgroup.")
1269
1270 (defvar gnus-newsgroup-unseen nil
1271   "List of unseen articles in the current newsgroup.")
1272
1273 (defvar gnus-newsgroup-seen nil
1274   "Range of seen articles in the current newsgroup.")
1275
1276 (defvar gnus-newsgroup-articles nil
1277   "List of articles in the current newsgroup.")
1278
1279 (defvar gnus-newsgroup-scored nil
1280   "List of scored articles in the current newsgroup.")
1281
1282 (defvar gnus-newsgroup-headers nil
1283   "List of article headers in the current newsgroup.")
1284
1285 (defvar gnus-newsgroup-threads nil)
1286
1287 (defvar gnus-newsgroup-prepared nil
1288   "Whether the current group has been prepared properly.")
1289
1290 (defvar gnus-newsgroup-ancient nil
1291   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1292
1293 (defvar gnus-newsgroup-sparse nil)
1294
1295 (defvar gnus-current-article nil)
1296 (defvar gnus-article-current nil)
1297 (defvar gnus-current-headers nil)
1298 (defvar gnus-have-all-headers nil)
1299 (defvar gnus-last-article nil)
1300 (defvar gnus-newsgroup-history nil)
1301 (defvar gnus-newsgroup-charset nil)
1302 (defvar gnus-newsgroup-ephemeral-charset nil)
1303 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1304
1305 (defvar gnus-article-before-search nil)
1306
1307 (defconst gnus-summary-local-variables
1308   '(gnus-newsgroup-name
1309     gnus-newsgroup-begin gnus-newsgroup-end
1310     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1311     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1312     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1313     gnus-newsgroup-unselected gnus-newsgroup-marked
1314     gnus-newsgroup-spam-marked
1315     gnus-newsgroup-reads gnus-newsgroup-saved
1316     gnus-newsgroup-replied gnus-newsgroup-forwarded
1317     gnus-newsgroup-recent
1318     gnus-newsgroup-expirable
1319     gnus-newsgroup-processable gnus-newsgroup-killed
1320     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1321     gnus-newsgroup-unfetched
1322     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1323     gnus-newsgroup-seen gnus-newsgroup-articles
1324     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1325     gnus-newsgroup-headers gnus-newsgroup-threads
1326     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1327     gnus-current-article gnus-current-headers gnus-have-all-headers
1328     gnus-last-article gnus-article-internal-prepare-hook
1329     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1330     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1331     gnus-thread-expunge-below
1332     gnus-score-alist gnus-current-score-file
1333     (gnus-summary-expunge-below . global)
1334     (gnus-summary-mark-below . global)
1335     (gnus-orphan-score . global)
1336     gnus-newsgroup-active gnus-scores-exclude-files
1337     gnus-newsgroup-history gnus-newsgroup-ancient
1338     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1339     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1340     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1341     (gnus-newsgroup-expunged-tally . 0)
1342     gnus-cache-removable-articles gnus-newsgroup-cached
1343     gnus-newsgroup-data gnus-newsgroup-data-reverse
1344     gnus-newsgroup-limit gnus-newsgroup-limits
1345     gnus-newsgroup-charset gnus-newsgroup-display)
1346   "Variables that are buffer-local to the summary buffers.")
1347
1348 (defvar gnus-newsgroup-variables nil
1349   "A list of variables that have separate values in different newsgroups.
1350 A list of newsgroup (summary buffer) local variables, or cons of
1351 variables and their default values (when the default values are not
1352 nil), that should be made global while the summary buffer is active.
1353 These variables can be used to set variables in the group parameters
1354 while still allowing them to affect operations done in other
1355 buffers. For example:
1356
1357 \(setq gnus-newsgroup-variables
1358      '(message-use-followup-to
1359        (gnus-visible-headers .
1360          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1361 ")
1362
1363 ;; Byte-compiler warning.
1364 ;(eval-when-compile (defvar gnus-article-mode-map))
1365 (eval-when-compile
1366   (let ((features (cons 'gnus-sum features)))
1367     (require 'gnus)
1368     (require 'gnus-agent)
1369     (require 'gnus-art)))
1370
1371 ;; MIME stuff.
1372
1373 (defvar gnus-decode-encoded-word-methods
1374   '(mail-decode-encoded-word-string)
1375   "List of methods used to decode encoded words.
1376
1377 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1378 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1379 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1380 whose names match REGEXP.
1381
1382 For example:
1383 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1384  mail-decode-encoded-word-string
1385  (\"chinese\" . rfc1843-decode-string))")
1386
1387 (defvar gnus-decode-encoded-word-methods-cache nil)
1388
1389 (defun gnus-multi-decode-encoded-word-string (string)
1390   "Apply the functions from `gnus-encoded-word-methods' that match."
1391   (unless (and gnus-decode-encoded-word-methods-cache
1392                (eq gnus-newsgroup-name
1393                    (car gnus-decode-encoded-word-methods-cache)))
1394     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1395     (mapcar (lambda (x)
1396               (if (symbolp x)
1397                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1398                 (if (and gnus-newsgroup-name
1399                          (string-match (car x) gnus-newsgroup-name))
1400                     (nconc gnus-decode-encoded-word-methods-cache
1401                            (list (cdr x))))))
1402             gnus-decode-encoded-word-methods))
1403   (let ((xlist gnus-decode-encoded-word-methods-cache))
1404     (pop xlist)
1405     (while xlist
1406       (setq string (funcall (pop xlist) string))))
1407   string)
1408
1409 ;; Subject simplification.
1410
1411 (defun gnus-simplify-whitespace (str)
1412   "Remove excessive whitespace from STR."
1413   ;; Multiple spaces.
1414   (while (string-match "[ \t][ \t]+" str)
1415     (setq str (concat (substring str 0 (match-beginning 0))
1416                         " "
1417                         (substring str (match-end 0)))))
1418   ;; Leading spaces.
1419   (when (string-match "^[ \t]+" str)
1420     (setq str (substring str (match-end 0))))
1421   ;; Trailing spaces.
1422   (when (string-match "[ \t]+$" str)
1423     (setq str (substring str 0 (match-beginning 0))))
1424   str)
1425
1426 (defun gnus-simplify-all-whitespace (str)
1427   "Remove all whitespace from STR."
1428   (while (string-match "[ \t\n]+" str)
1429     (setq str (replace-match "" nil nil str)))
1430   str)
1431
1432 (defsubst gnus-simplify-subject-re (subject)
1433   "Remove \"Re:\" from subject lines."
1434   (if (string-match message-subject-re-regexp subject)
1435       (substring subject (match-end 0))
1436     subject))
1437
1438 (defun gnus-simplify-subject (subject &optional re-only)
1439   "Remove `Re:' and words in parentheses.
1440 If RE-ONLY is non-nil, strip leading `Re:'s only."
1441   (let ((case-fold-search t))           ;Ignore case.
1442     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1443     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1444       (setq subject (substring subject (match-end 0))))
1445     ;; Remove uninteresting prefixes.
1446     (when (and (not re-only)
1447                gnus-simplify-ignored-prefixes
1448                (string-match gnus-simplify-ignored-prefixes subject))
1449       (setq subject (substring subject (match-end 0))))
1450     ;; Remove words in parentheses from end.
1451     (unless re-only
1452       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1453         (setq subject (substring subject 0 (match-beginning 0)))))
1454     ;; Return subject string.
1455     subject))
1456
1457 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1458 ;; all whitespace.
1459 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1460   (goto-char (point-min))
1461   (while (re-search-forward regexp nil t)
1462     (replace-match (or newtext ""))))
1463
1464 (defun gnus-simplify-buffer-fuzzy ()
1465   "Simplify string in the buffer fuzzily.
1466 The string in the accessible portion of the current buffer is simplified.
1467 It is assumed to be a single-line subject.
1468 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1469 matter is removed.  Additional things can be deleted by setting
1470 `gnus-simplify-subject-fuzzy-regexp'."
1471   (let ((case-fold-search t)
1472         (modified-tick))
1473     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1474
1475     (while (not (eq modified-tick (buffer-modified-tick)))
1476       (setq modified-tick (buffer-modified-tick))
1477       (cond
1478        ((listp gnus-simplify-subject-fuzzy-regexp)
1479         (mapcar 'gnus-simplify-buffer-fuzzy-step
1480                 gnus-simplify-subject-fuzzy-regexp))
1481        (gnus-simplify-subject-fuzzy-regexp
1482         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1483       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1484       (gnus-simplify-buffer-fuzzy-step
1485        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1486       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1487
1488     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1489     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1490     (gnus-simplify-buffer-fuzzy-step " $")
1491     (gnus-simplify-buffer-fuzzy-step "^ +")))
1492
1493 (defun gnus-simplify-subject-fuzzy (subject)
1494   "Simplify a subject string fuzzily.
1495 See `gnus-simplify-buffer-fuzzy' for details."
1496   (save-excursion
1497     (gnus-set-work-buffer)
1498     (let ((case-fold-search t))
1499       ;; Remove uninteresting prefixes.
1500       (when (and gnus-simplify-ignored-prefixes
1501                  (string-match gnus-simplify-ignored-prefixes subject))
1502         (setq subject (substring subject (match-end 0))))
1503       (insert subject)
1504       (inline (gnus-simplify-buffer-fuzzy))
1505       (buffer-string))))
1506
1507 (defsubst gnus-simplify-subject-fully (subject)
1508   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1509   (cond
1510    (gnus-simplify-subject-functions
1511     (gnus-map-function gnus-simplify-subject-functions subject))
1512    ((null gnus-summary-gather-subject-limit)
1513     (gnus-simplify-subject-re subject))
1514    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1515     (gnus-simplify-subject-fuzzy subject))
1516    ((numberp gnus-summary-gather-subject-limit)
1517     (gnus-limit-string (gnus-simplify-subject-re subject)
1518                        gnus-summary-gather-subject-limit))
1519    (t
1520     subject)))
1521
1522 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1523   "Check whether two subjects are equal.
1524 If optional argument simple-first is t, first argument is already
1525 simplified."
1526   (cond
1527    ((null simple-first)
1528     (equal (gnus-simplify-subject-fully s1)
1529            (gnus-simplify-subject-fully s2)))
1530    (t
1531     (equal s1
1532            (gnus-simplify-subject-fully s2)))))
1533
1534 (defun gnus-summary-bubble-group ()
1535   "Increase the score of the current group.
1536 This is a handy function to add to `gnus-summary-exit-hook' to
1537 increase the score of each group you read."
1538   (gnus-group-add-score gnus-newsgroup-name))
1539
1540 \f
1541 ;;;
1542 ;;; Gnus summary mode
1543 ;;;
1544
1545 (put 'gnus-summary-mode 'mode-class 'special)
1546
1547 (defvar gnus-article-commands-menu)
1548
1549 ;; Non-orthogonal keys
1550
1551 (gnus-define-keys gnus-summary-mode-map
1552   " " gnus-summary-next-page
1553   "\177" gnus-summary-prev-page
1554   [delete] gnus-summary-prev-page
1555   [backspace] gnus-summary-prev-page
1556   "\r" gnus-summary-scroll-up
1557   "\M-\r" gnus-summary-scroll-down
1558   "n" gnus-summary-next-unread-article
1559   "p" gnus-summary-prev-unread-article
1560   "N" gnus-summary-next-article
1561   "P" gnus-summary-prev-article
1562   "\M-\C-n" gnus-summary-next-same-subject
1563   "\M-\C-p" gnus-summary-prev-same-subject
1564   "\M-n" gnus-summary-next-unread-subject
1565   "\M-p" gnus-summary-prev-unread-subject
1566   "." gnus-summary-first-unread-article
1567   "," gnus-summary-best-unread-article
1568   "\M-s" gnus-summary-search-article-forward
1569   "\M-r" gnus-summary-search-article-backward
1570   "<" gnus-summary-beginning-of-article
1571   ">" gnus-summary-end-of-article
1572   "j" gnus-summary-goto-article
1573   "^" gnus-summary-refer-parent-article
1574   "\M-^" gnus-summary-refer-article
1575   "u" gnus-summary-tick-article-forward
1576   "!" gnus-summary-tick-article-forward
1577   "U" gnus-summary-tick-article-backward
1578   "d" gnus-summary-mark-as-read-forward
1579   "D" gnus-summary-mark-as-read-backward
1580   "E" gnus-summary-mark-as-expirable
1581   "\M-u" gnus-summary-clear-mark-forward
1582   "\M-U" gnus-summary-clear-mark-backward
1583   "k" gnus-summary-kill-same-subject-and-select
1584   "\C-k" gnus-summary-kill-same-subject
1585   "\M-\C-k" gnus-summary-kill-thread
1586   "\M-\C-l" gnus-summary-lower-thread
1587   "e" gnus-summary-edit-article
1588   "#" gnus-summary-mark-as-processable
1589   "\M-#" gnus-summary-unmark-as-processable
1590   "\M-\C-t" gnus-summary-toggle-threads
1591   "\M-\C-s" gnus-summary-show-thread
1592   "\M-\C-h" gnus-summary-hide-thread
1593   "\M-\C-f" gnus-summary-next-thread
1594   "\M-\C-b" gnus-summary-prev-thread
1595   [(meta down)] gnus-summary-next-thread
1596   [(meta up)] gnus-summary-prev-thread
1597   "\M-\C-u" gnus-summary-up-thread
1598   "\M-\C-d" gnus-summary-down-thread
1599   "&" gnus-summary-execute-command
1600   "c" gnus-summary-catchup-and-exit
1601   "\C-w" gnus-summary-mark-region-as-read
1602   "\C-t" gnus-summary-toggle-truncation
1603   "?" gnus-summary-mark-as-dormant
1604   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1605   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1606   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1607   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1608   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1609   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1610   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1611   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1612   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1613   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1614   "=" gnus-summary-expand-window
1615   "\C-x\C-s" gnus-summary-reselect-current-group
1616   "\M-g" gnus-summary-rescan-group
1617   "w" gnus-summary-stop-page-breaking
1618   "\C-c\C-r" gnus-summary-caesar-message
1619   "f" gnus-summary-followup
1620   "F" gnus-summary-followup-with-original
1621   "C" gnus-summary-cancel-article
1622   "r" gnus-summary-reply
1623   "R" gnus-summary-reply-with-original
1624   "\C-c\C-f" gnus-summary-mail-forward
1625   "o" gnus-summary-save-article
1626   "\C-o" gnus-summary-save-article-mail
1627   "|" gnus-summary-pipe-output
1628   "\M-k" gnus-summary-edit-local-kill
1629   "\M-K" gnus-summary-edit-global-kill
1630   ;; "V" gnus-version
1631   "\C-c\C-d" gnus-summary-describe-group
1632   "q" gnus-summary-exit
1633   "Q" gnus-summary-exit-no-update
1634   "\C-c\C-i" gnus-info-find-node
1635   gnus-mouse-2 gnus-mouse-pick-article
1636   "m" gnus-summary-mail-other-window
1637   "a" gnus-summary-post-news
1638   "i" gnus-summary-news-other-window
1639   "x" gnus-summary-limit-to-unread
1640   "s" gnus-summary-isearch-article
1641   "t" gnus-summary-toggle-header
1642   "g" gnus-summary-show-article
1643   "l" gnus-summary-goto-last-article
1644   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1645   "\C-d" gnus-summary-enter-digest-group
1646   "\M-\C-d" gnus-summary-read-document
1647   "\M-\C-e" gnus-summary-edit-parameters
1648   "\M-\C-a" gnus-summary-customize-parameters
1649   "\C-c\C-b" gnus-bug
1650   "*" gnus-cache-enter-article
1651   "\M-*" gnus-cache-remove-article
1652   "\M-&" gnus-summary-universal-argument
1653   "\C-l" gnus-recenter
1654   "I" gnus-summary-increase-score
1655   "L" gnus-summary-lower-score
1656   "\M-i" gnus-symbolic-argument
1657   "h" gnus-summary-select-article-buffer
1658
1659   "b" gnus-article-view-part
1660   "\M-t" gnus-summary-toggle-display-buttonized
1661
1662   "V" gnus-summary-score-map
1663   "X" gnus-uu-extract-map
1664   "S" gnus-summary-send-map)
1665
1666 ;; Sort of orthogonal keymap
1667 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1668   "t" gnus-summary-tick-article-forward
1669   "!" gnus-summary-tick-article-forward
1670   "d" gnus-summary-mark-as-read-forward
1671   "r" gnus-summary-mark-as-read-forward
1672   "c" gnus-summary-clear-mark-forward
1673   " " gnus-summary-clear-mark-forward
1674   "e" gnus-summary-mark-as-expirable
1675   "x" gnus-summary-mark-as-expirable
1676   "?" gnus-summary-mark-as-dormant
1677   "b" gnus-summary-set-bookmark
1678   "B" gnus-summary-remove-bookmark
1679   "#" gnus-summary-mark-as-processable
1680   "\M-#" gnus-summary-unmark-as-processable
1681   "S" gnus-summary-limit-include-expunged
1682   "C" gnus-summary-catchup
1683   "H" gnus-summary-catchup-to-here
1684   "h" gnus-summary-catchup-from-here
1685   "\C-c" gnus-summary-catchup-all
1686   "k" gnus-summary-kill-same-subject-and-select
1687   "K" gnus-summary-kill-same-subject
1688   "P" gnus-uu-mark-map)
1689
1690 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1691   "c" gnus-summary-clear-above
1692   "u" gnus-summary-tick-above
1693   "m" gnus-summary-mark-above
1694   "k" gnus-summary-kill-below)
1695
1696 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1697   "/" gnus-summary-limit-to-subject
1698   "n" gnus-summary-limit-to-articles
1699   "w" gnus-summary-pop-limit
1700   "s" gnus-summary-limit-to-subject
1701   "a" gnus-summary-limit-to-author
1702   "u" gnus-summary-limit-to-unread
1703   "m" gnus-summary-limit-to-marks
1704   "M" gnus-summary-limit-exclude-marks
1705   "v" gnus-summary-limit-to-score
1706   "*" gnus-summary-limit-include-cached
1707   "D" gnus-summary-limit-include-dormant
1708   "T" gnus-summary-limit-include-thread
1709   "d" gnus-summary-limit-exclude-dormant
1710   "t" gnus-summary-limit-to-age
1711   "." gnus-summary-limit-to-unseen
1712   "x" gnus-summary-limit-to-extra
1713   "p" gnus-summary-limit-to-display-predicate
1714   "E" gnus-summary-limit-include-expunged
1715   "c" gnus-summary-limit-exclude-childless-dormant
1716   "C" gnus-summary-limit-mark-excluded-as-read
1717   "o" gnus-summary-insert-old-articles
1718   "N" gnus-summary-insert-new-articles)
1719
1720 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1721   "n" gnus-summary-next-unread-article
1722   "p" gnus-summary-prev-unread-article
1723   "N" gnus-summary-next-article
1724   "P" gnus-summary-prev-article
1725   "\C-n" gnus-summary-next-same-subject
1726   "\C-p" gnus-summary-prev-same-subject
1727   "\M-n" gnus-summary-next-unread-subject
1728   "\M-p" gnus-summary-prev-unread-subject
1729   "f" gnus-summary-first-unread-article
1730   "b" gnus-summary-best-unread-article
1731   "j" gnus-summary-goto-article
1732   "g" gnus-summary-goto-subject
1733   "l" gnus-summary-goto-last-article
1734   "o" gnus-summary-pop-article)
1735
1736 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1737   "k" gnus-summary-kill-thread
1738   "l" gnus-summary-lower-thread
1739   "i" gnus-summary-raise-thread
1740   "T" gnus-summary-toggle-threads
1741   "t" gnus-summary-rethread-current
1742   "^" gnus-summary-reparent-thread
1743   "s" gnus-summary-show-thread
1744   "S" gnus-summary-show-all-threads
1745   "h" gnus-summary-hide-thread
1746   "H" gnus-summary-hide-all-threads
1747   "n" gnus-summary-next-thread
1748   "p" gnus-summary-prev-thread
1749   "u" gnus-summary-up-thread
1750   "o" gnus-summary-top-thread
1751   "d" gnus-summary-down-thread
1752   "#" gnus-uu-mark-thread
1753   "\M-#" gnus-uu-unmark-thread)
1754
1755 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1756   "g" gnus-summary-prepare
1757   "c" gnus-summary-insert-cached-articles
1758   "d" gnus-summary-insert-dormant-articles)
1759
1760 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1761   "c" gnus-summary-catchup-and-exit
1762   "C" gnus-summary-catchup-all-and-exit
1763   "E" gnus-summary-exit-no-update
1764   "Q" gnus-summary-exit
1765   "Z" gnus-summary-exit
1766   "n" gnus-summary-catchup-and-goto-next-group
1767   "R" gnus-summary-reselect-current-group
1768   "G" gnus-summary-rescan-group
1769   "N" gnus-summary-next-group
1770   "s" gnus-summary-save-newsrc
1771   "P" gnus-summary-prev-group)
1772
1773 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1774   " " gnus-summary-next-page
1775   "n" gnus-summary-next-page
1776   "\177" gnus-summary-prev-page
1777   [delete] gnus-summary-prev-page
1778   "p" gnus-summary-prev-page
1779   "\r" gnus-summary-scroll-up
1780   "\M-\r" gnus-summary-scroll-down
1781   "<" gnus-summary-beginning-of-article
1782   ">" gnus-summary-end-of-article
1783   "b" gnus-summary-beginning-of-article
1784   "e" gnus-summary-end-of-article
1785   "^" gnus-summary-refer-parent-article
1786   "r" gnus-summary-refer-parent-article
1787   "D" gnus-summary-enter-digest-group
1788   "R" gnus-summary-refer-references
1789   "T" gnus-summary-refer-thread
1790   "g" gnus-summary-show-article
1791   "s" gnus-summary-isearch-article
1792   "P" gnus-summary-print-article
1793   "M" gnus-mailing-list-insinuate
1794   "t" gnus-article-babel)
1795
1796 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1797   "b" gnus-article-add-buttons
1798   "B" gnus-article-add-buttons-to-head
1799   "o" gnus-article-treat-overstrike
1800   "e" gnus-article-emphasize
1801   "w" gnus-article-fill-cited-article
1802   "Q" gnus-article-fill-long-lines
1803   "C" gnus-article-capitalize-sentences
1804   "c" gnus-article-remove-cr
1805   "q" gnus-article-de-quoted-unreadable
1806   "6" gnus-article-de-base64-unreadable
1807   "Z" gnus-article-decode-HZ
1808   "h" gnus-article-wash-html
1809   "u" gnus-article-unsplit-urls
1810   "s" gnus-summary-force-verify-and-decrypt
1811   "f" gnus-article-display-x-face
1812   "l" gnus-summary-stop-page-breaking
1813   "r" gnus-summary-caesar-message
1814   "m" gnus-summary-morse-message
1815   "t" gnus-summary-toggle-header
1816   "g" gnus-treat-smiley
1817   "v" gnus-summary-verbose-headers
1818   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1819   "p" gnus-article-verify-x-pgp-sig
1820   "d" gnus-article-treat-dumbquotes)
1821
1822 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1823   ;; mnemonic: deuglif*Y*
1824   "u" gnus-article-outlook-unwrap-lines
1825   "a" gnus-article-outlook-repair-attribution
1826   "c" gnus-article-outlook-rearrange-citation
1827   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1828
1829 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1830   "a" gnus-article-hide
1831   "h" gnus-article-hide-headers
1832   "b" gnus-article-hide-boring-headers
1833   "s" gnus-article-hide-signature
1834   "c" gnus-article-hide-citation
1835   "C" gnus-article-hide-citation-in-followups
1836   "l" gnus-article-hide-list-identifiers
1837   "B" gnus-article-strip-banner
1838   "P" gnus-article-hide-pem
1839   "\C-c" gnus-article-hide-citation-maybe)
1840
1841 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1842   "a" gnus-article-highlight
1843   "h" gnus-article-highlight-headers
1844   "c" gnus-article-highlight-citation
1845   "s" gnus-article-highlight-signature)
1846
1847 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1848   "f" gnus-article-treat-fold-headers
1849   "u" gnus-article-treat-unfold-headers
1850   "n" gnus-article-treat-fold-newsgroups)
1851
1852 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1853   "x" gnus-article-display-x-face
1854   "d" gnus-article-display-face
1855   "s" gnus-treat-smiley
1856   "D" gnus-article-remove-images
1857   "f" gnus-treat-from-picon
1858   "m" gnus-treat-mail-picon
1859   "n" gnus-treat-newsgroups-picon)
1860
1861 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1862   "w" gnus-article-decode-mime-words
1863   "c" gnus-article-decode-charset
1864   "v" gnus-mime-view-all-parts
1865   "b" gnus-article-view-part)
1866
1867 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1868   "z" gnus-article-date-ut
1869   "u" gnus-article-date-ut
1870   "l" gnus-article-date-local
1871   "p" gnus-article-date-english
1872   "e" gnus-article-date-lapsed
1873   "o" gnus-article-date-original
1874   "i" gnus-article-date-iso8601
1875   "s" gnus-article-date-user)
1876
1877 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1878   "t" gnus-article-remove-trailing-blank-lines
1879   "l" gnus-article-strip-leading-blank-lines
1880   "m" gnus-article-strip-multiple-blank-lines
1881   "a" gnus-article-strip-blank-lines
1882   "A" gnus-article-strip-all-blank-lines
1883   "s" gnus-article-strip-leading-space
1884   "e" gnus-article-strip-trailing-space
1885   "w" gnus-article-remove-leading-whitespace)
1886
1887 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1888   "v" gnus-version
1889   "f" gnus-summary-fetch-faq
1890   "d" gnus-summary-describe-group
1891   "h" gnus-summary-describe-briefly
1892   "i" gnus-info-find-node
1893   "c" gnus-group-fetch-charter
1894   "C" gnus-group-fetch-control)
1895
1896 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1897   "e" gnus-summary-expire-articles
1898   "\M-\C-e" gnus-summary-expire-articles-now
1899   "\177" gnus-summary-delete-article
1900   [delete] gnus-summary-delete-article
1901   [backspace] gnus-summary-delete-article
1902   "m" gnus-summary-move-article
1903   "r" gnus-summary-respool-article
1904   "w" gnus-summary-edit-article
1905   "c" gnus-summary-copy-article
1906   "B" gnus-summary-crosspost-article
1907   "q" gnus-summary-respool-query
1908   "t" gnus-summary-respool-trace
1909   "i" gnus-summary-import-article
1910   "I" gnus-summary-create-article
1911   "p" gnus-summary-article-posted-p)
1912
1913 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1914   "o" gnus-summary-save-article
1915   "m" gnus-summary-save-article-mail
1916   "F" gnus-summary-write-article-file
1917   "r" gnus-summary-save-article-rmail
1918   "f" gnus-summary-save-article-file
1919   "b" gnus-summary-save-article-body-file
1920   "h" gnus-summary-save-article-folder
1921   "v" gnus-summary-save-article-vm
1922   "p" gnus-summary-pipe-output
1923   "P" gnus-summary-muttprint
1924   "s" gnus-soup-add-article)
1925
1926 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1927   "b" gnus-summary-display-buttonized
1928   "m" gnus-summary-repair-multipart
1929   "v" gnus-article-view-part
1930   "o" gnus-article-save-part
1931   "c" gnus-article-copy-part
1932   "C" gnus-article-view-part-as-charset
1933   "e" gnus-article-view-part-externally
1934   "E" gnus-article-encrypt-body
1935   "i" gnus-article-inline-part
1936   "|" gnus-article-pipe-part)
1937
1938 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1939   "p" gnus-summary-mark-as-processable
1940   "u" gnus-summary-unmark-as-processable
1941   "U" gnus-summary-unmark-all-processable
1942   "v" gnus-uu-mark-over
1943   "s" gnus-uu-mark-series
1944   "r" gnus-uu-mark-region
1945   "g" gnus-uu-unmark-region
1946   "R" gnus-uu-mark-by-regexp
1947   "G" gnus-uu-unmark-by-regexp
1948   "t" gnus-uu-mark-thread
1949   "T" gnus-uu-unmark-thread
1950   "a" gnus-uu-mark-all
1951   "b" gnus-uu-mark-buffer
1952   "S" gnus-uu-mark-sparse
1953   "k" gnus-summary-kill-process-mark
1954   "y" gnus-summary-yank-process-mark
1955   "w" gnus-summary-save-process-mark
1956   "i" gnus-uu-invert-processable)
1957
1958 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1959   ;;"x" gnus-uu-extract-any
1960   "m" gnus-summary-save-parts
1961   "u" gnus-uu-decode-uu
1962   "U" gnus-uu-decode-uu-and-save
1963   "s" gnus-uu-decode-unshar
1964   "S" gnus-uu-decode-unshar-and-save
1965   "o" gnus-uu-decode-save
1966   "O" gnus-uu-decode-save
1967   "b" gnus-uu-decode-binhex
1968   "B" gnus-uu-decode-binhex
1969   "p" gnus-uu-decode-postscript
1970   "P" gnus-uu-decode-postscript-and-save)
1971
1972 (gnus-define-keys
1973     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1974   "u" gnus-uu-decode-uu-view
1975   "U" gnus-uu-decode-uu-and-save-view
1976   "s" gnus-uu-decode-unshar-view
1977   "S" gnus-uu-decode-unshar-and-save-view
1978   "o" gnus-uu-decode-save-view
1979   "O" gnus-uu-decode-save-view
1980   "b" gnus-uu-decode-binhex-view
1981   "B" gnus-uu-decode-binhex-view
1982   "p" gnus-uu-decode-postscript-view
1983   "P" gnus-uu-decode-postscript-and-save-view)
1984
1985 (defvar gnus-article-post-menu nil)
1986
1987 (defconst gnus-summary-menu-maxlen 20)
1988
1989 (defun gnus-summary-menu-split (menu)
1990   ;; If we have lots of elements, divide them into groups of 20
1991   ;; and make a pane (or submenu) for each one.
1992   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1993       (let ((menu menu) sublists next
1994             (i 1))
1995         (while menu
1996           ;; Pull off the next gnus-summary-menu-maxlen elements
1997           ;; and make them the next element of sublist.
1998           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1999           (if next
2000               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2001                       nil))
2002           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2003                                              (aref (car (last menu)) 0)) menu)
2004                                sublists))
2005           (setq i (1+ i))
2006           (setq menu next))
2007         (nreverse sublists))
2008     ;; Few elements--put them all in one pane.
2009     menu))
2010
2011 (defun gnus-summary-make-menu-bar ()
2012   (gnus-turn-off-edit-menu 'summary)
2013
2014   (unless (boundp 'gnus-summary-misc-menu)
2015
2016     (easy-menu-define
2017       gnus-summary-kill-menu gnus-summary-mode-map ""
2018       (cons
2019        "Score"
2020        (nconc
2021         (list
2022          ["Customize" gnus-score-customize t])
2023         (gnus-make-score-map 'increase)
2024         (gnus-make-score-map 'lower)
2025         '(("Mark"
2026            ["Kill below" gnus-summary-kill-below t]
2027            ["Mark above" gnus-summary-mark-above t]
2028            ["Tick above" gnus-summary-tick-above t]
2029            ["Clear above" gnus-summary-clear-above t])
2030           ["Current score" gnus-summary-current-score t]
2031           ["Set score" gnus-summary-set-score t]
2032           ["Switch current score file..." gnus-score-change-score-file t]
2033           ["Set mark below..." gnus-score-set-mark-below t]
2034           ["Set expunge below..." gnus-score-set-expunge-below t]
2035           ["Edit current score file" gnus-score-edit-current-scores t]
2036           ["Edit score file" gnus-score-edit-file t]
2037           ["Trace score" gnus-score-find-trace t]
2038           ["Find words" gnus-score-find-favourite-words t]
2039           ["Rescore buffer" gnus-summary-rescore t]
2040           ["Increase score..." gnus-summary-increase-score t]
2041           ["Lower score..." gnus-summary-lower-score t]))))
2042
2043     ;; Define both the Article menu in the summary buffer and the
2044     ;; equivalent Commands menu in the article buffer here for
2045     ;; consistency.
2046     (let ((innards
2047            `(("Hide"
2048               ["All" gnus-article-hide t]
2049               ["Headers" gnus-article-hide-headers t]
2050               ["Signature" gnus-article-hide-signature t]
2051               ["Citation" gnus-article-hide-citation t]
2052               ["List identifiers" gnus-article-hide-list-identifiers t]
2053               ["Banner" gnus-article-strip-banner t]
2054               ["Boring headers" gnus-article-hide-boring-headers t])
2055              ("Highlight"
2056               ["All" gnus-article-highlight t]
2057               ["Headers" gnus-article-highlight-headers t]
2058               ["Signature" gnus-article-highlight-signature t]
2059               ["Citation" gnus-article-highlight-citation t])
2060              ("MIME"
2061               ["Words" gnus-article-decode-mime-words t]
2062               ["Charset" gnus-article-decode-charset t]
2063               ["QP" gnus-article-de-quoted-unreadable t]
2064               ["Base64" gnus-article-de-base64-unreadable t]
2065               ["View MIME buttons" gnus-summary-display-buttonized t]
2066               ["View all" gnus-mime-view-all-parts t]
2067               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2068               ["Encrypt body" gnus-article-encrypt-body t]
2069               ["Extract all parts" gnus-summary-save-parts t])
2070              ("Date"
2071               ["Local" gnus-article-date-local t]
2072               ["ISO8601" gnus-article-date-iso8601 t]
2073               ["UT" gnus-article-date-ut t]
2074               ["Original" gnus-article-date-original t]
2075               ["Lapsed" gnus-article-date-lapsed t]
2076               ["User-defined" gnus-article-date-user t])
2077              ("Display"
2078               ["Remove images" gnus-article-remove-images t]
2079               ["Toggle smiley" gnus-treat-smiley t]
2080               ["Show X-Face" gnus-article-display-x-face t]
2081               ["Show picons in From" gnus-treat-from-picon t]
2082               ["Show picons in mail headers" gnus-treat-mail-picon t]
2083               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2084               ("View as different encoding"
2085                ,@(gnus-summary-menu-split
2086                   (mapcar
2087                    (lambda (cs)
2088                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2089                      ;; forms for menu commands, we should provide intern'ed
2090                      ;; function symbols.
2091                      (let ((command (intern (format "\
2092 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2093                        (fset command
2094                              `(lambda ()
2095                                 (interactive)
2096                                 (let ((gnus-summary-show-article-charset-alist
2097                                        '((1 . ,cs))))
2098                                   (gnus-summary-show-article 1))))
2099                        `[,(symbol-name cs) ,command t]))
2100                    (sort (if (fboundp 'coding-system-list)
2101                              (coding-system-list)
2102                            (mapcar 'car mm-mime-mule-charset-alist))
2103                          'string<)))))
2104              ("Washing"
2105               ("Remove Blanks"
2106                ["Leading" gnus-article-strip-leading-blank-lines t]
2107                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2108                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2109                ["All of the above" gnus-article-strip-blank-lines t]
2110                ["All" gnus-article-strip-all-blank-lines t]
2111                ["Leading space" gnus-article-strip-leading-space t]
2112                ["Trailing space" gnus-article-strip-trailing-space t]
2113                ["Leading space in headers"
2114                 gnus-article-remove-leading-whitespace t])
2115               ["Overstrike" gnus-article-treat-overstrike t]
2116               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2117               ["Emphasis" gnus-article-emphasize t]
2118               ["Word wrap" gnus-article-fill-cited-article t]
2119               ["Fill long lines" gnus-article-fill-long-lines t]
2120               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2121               ["CR" gnus-article-remove-cr t]
2122               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2123               ["Base64" gnus-article-de-base64-unreadable t]
2124               ["Rot 13" gnus-summary-caesar-message
2125                ,@(if (featurep 'xemacs) '(t)
2126                    '(:help "\"Caesar rotate\" article by 13"))]
2127               ["Morse decode" gnus-summary-morse-message t]
2128               ["Unix pipe..." gnus-summary-pipe-message t]
2129               ["Add buttons" gnus-article-add-buttons t]
2130               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2131               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2132               ["Verbose header" gnus-summary-verbose-headers t]
2133               ["Toggle header" gnus-summary-toggle-header t]
2134               ["Unfold headers" gnus-article-treat-unfold-headers t]
2135               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2136               ["Html" gnus-article-wash-html t]
2137               ["URLs" gnus-article-unsplit-urls t]
2138               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2139               ["HZ" gnus-article-decode-HZ t]
2140               ("(Outlook) Deuglify"
2141                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2142                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2143                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2144                ["Full (Outlook) deuglify"
2145                 gnus-article-outlook-deuglify-article t])
2146               )
2147              ("Output"
2148               ["Save in default format" gnus-summary-save-article
2149                ,@(if (featurep 'xemacs) '(t)
2150                    '(:help "Save article using default method"))]
2151               ["Save in file" gnus-summary-save-article-file
2152                ,@(if (featurep 'xemacs) '(t)
2153                    '(:help "Save article in file"))]
2154               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2155               ["Save in MH folder" gnus-summary-save-article-folder t]
2156               ["Save in VM folder" gnus-summary-save-article-vm t]
2157               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2158               ["Save body in file" gnus-summary-save-article-body-file t]
2159               ["Pipe through a filter" gnus-summary-pipe-output t]
2160               ["Add to SOUP packet" gnus-soup-add-article t]
2161               ["Print with Muttprint" gnus-summary-muttprint t]
2162               ["Print" gnus-summary-print-article t])
2163              ("Backend"
2164               ["Respool article..." gnus-summary-respool-article t]
2165               ["Move article..." gnus-summary-move-article
2166                (gnus-check-backend-function
2167                 'request-move-article gnus-newsgroup-name)]
2168               ["Copy article..." gnus-summary-copy-article t]
2169               ["Crosspost article..." gnus-summary-crosspost-article
2170                (gnus-check-backend-function
2171                 'request-replace-article gnus-newsgroup-name)]
2172               ["Import file..." gnus-summary-import-article t]
2173               ["Create article..." gnus-summary-create-article t]
2174               ["Check if posted" gnus-summary-article-posted-p t]
2175               ["Edit article" gnus-summary-edit-article
2176                (not (gnus-group-read-only-p))]
2177               ["Delete article" gnus-summary-delete-article
2178                (gnus-check-backend-function
2179                 'request-expire-articles gnus-newsgroup-name)]
2180               ["Query respool" gnus-summary-respool-query t]
2181               ["Trace respool" gnus-summary-respool-trace t]
2182               ["Delete expirable articles" gnus-summary-expire-articles-now
2183                (gnus-check-backend-function
2184                 'request-expire-articles gnus-newsgroup-name)])
2185              ("Extract"
2186               ["Uudecode" gnus-uu-decode-uu
2187                ,@(if (featurep 'xemacs) '(t)
2188                    '(:help "Decode uuencoded article(s)"))]
2189               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2190               ["Unshar" gnus-uu-decode-unshar t]
2191               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2192               ["Save" gnus-uu-decode-save t]
2193               ["Binhex" gnus-uu-decode-binhex t]
2194               ["Postscript" gnus-uu-decode-postscript t]
2195               ["All MIME parts" gnus-summary-save-parts t])
2196              ("Cache"
2197               ["Enter article" gnus-cache-enter-article t]
2198               ["Remove article" gnus-cache-remove-article t])
2199              ["Translate" gnus-article-babel t]
2200              ["Select article buffer" gnus-summary-select-article-buffer t]
2201              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2202              ["Isearch article..." gnus-summary-isearch-article t]
2203              ["Beginning of the article" gnus-summary-beginning-of-article t]
2204              ["End of the article" gnus-summary-end-of-article t]
2205              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2206              ["Fetch referenced articles" gnus-summary-refer-references t]
2207              ["Fetch current thread" gnus-summary-refer-thread t]
2208              ["Fetch article with id..." gnus-summary-refer-article t]
2209              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2210              ["Redisplay" gnus-summary-show-article t]
2211              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2212       (easy-menu-define
2213         gnus-summary-article-menu gnus-summary-mode-map ""
2214         (cons "Article" innards))
2215
2216       (if (not (keymapp gnus-summary-article-menu))
2217           (easy-menu-define
2218             gnus-article-commands-menu gnus-article-mode-map ""
2219             (cons "Commands" innards))
2220         ;; in Emacs, don't share menu.
2221         (setq gnus-article-commands-menu
2222               (copy-keymap gnus-summary-article-menu))
2223         (define-key gnus-article-mode-map [menu-bar commands]
2224           (cons "Commands" gnus-article-commands-menu))))
2225
2226     (easy-menu-define
2227       gnus-summary-thread-menu gnus-summary-mode-map ""
2228       '("Threads"
2229         ["Find all messages in thread" gnus-summary-refer-thread t]
2230         ["Toggle threading" gnus-summary-toggle-threads t]
2231         ["Hide threads" gnus-summary-hide-all-threads t]
2232         ["Show threads" gnus-summary-show-all-threads t]
2233         ["Hide thread" gnus-summary-hide-thread t]
2234         ["Show thread" gnus-summary-show-thread t]
2235         ["Go to next thread" gnus-summary-next-thread t]
2236         ["Go to previous thread" gnus-summary-prev-thread t]
2237         ["Go down thread" gnus-summary-down-thread t]
2238         ["Go up thread" gnus-summary-up-thread t]
2239         ["Top of thread" gnus-summary-top-thread t]
2240         ["Mark thread as read" gnus-summary-kill-thread t]
2241         ["Lower thread score" gnus-summary-lower-thread t]
2242         ["Raise thread score" gnus-summary-raise-thread t]
2243         ["Rethread current" gnus-summary-rethread-current t]))
2244
2245     (easy-menu-define
2246       gnus-summary-post-menu gnus-summary-mode-map ""
2247       `("Post"
2248         ["Send a message (mail or news)" gnus-summary-post-news
2249          ,@(if (featurep 'xemacs) '(t)
2250              '(:help "Post an article"))]
2251         ["Followup" gnus-summary-followup
2252          ,@(if (featurep 'xemacs) '(t)
2253              '(:help "Post followup to this article"))]
2254         ["Followup and yank" gnus-summary-followup-with-original
2255          ,@(if (featurep 'xemacs) '(t)
2256              '(:help "Post followup to this article, quoting its contents"))]
2257         ["Supersede article" gnus-summary-supersede-article t]
2258         ["Cancel article" gnus-summary-cancel-article
2259          ,@(if (featurep 'xemacs) '(t)
2260              '(:help "Cancel an article you posted"))]
2261         ["Reply" gnus-summary-reply t]
2262         ["Reply and yank" gnus-summary-reply-with-original t]
2263         ["Wide reply" gnus-summary-wide-reply t]
2264         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2265          ,@(if (featurep 'xemacs) '(t)
2266              '(:help "Mail a reply, quoting this article"))]
2267         ["Very wide reply" gnus-summary-very-wide-reply t]
2268         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2269          ,@(if (featurep 'xemacs) '(t)
2270              '(:help "Mail a very wide reply, quoting this article"))]
2271         ["Mail forward" gnus-summary-mail-forward t]
2272         ["Post forward" gnus-summary-post-forward t]
2273         ["Digest and mail" gnus-uu-digest-mail-forward t]
2274         ["Digest and post" gnus-uu-digest-post-forward t]
2275         ["Resend message" gnus-summary-resend-message t]
2276         ["Resend message edit" gnus-summary-resend-message-edit t]
2277         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2278         ["Send a mail" gnus-summary-mail-other-window t]
2279         ["Create a local message" gnus-summary-news-other-window t]
2280         ["Uuencode and post" gnus-uu-post-news
2281          ,@(if (featurep 'xemacs) '(t)
2282              '(:help "Post a uuencoded article"))]
2283         ["Followup via news" gnus-summary-followup-to-mail t]
2284         ["Followup via news and yank"
2285          gnus-summary-followup-to-mail-with-original t]
2286         ;;("Draft"
2287         ;;["Send" gnus-summary-send-draft t]
2288         ;;["Send bounced" gnus-resend-bounced-mail t])
2289         ))
2290
2291     (cond
2292      ((not (keymapp gnus-summary-post-menu))
2293       (setq gnus-article-post-menu gnus-summary-post-menu))
2294      ((not gnus-article-post-menu)
2295       ;; Don't share post menu.
2296       (setq gnus-article-post-menu
2297             (copy-keymap gnus-summary-post-menu))))
2298     (define-key gnus-article-mode-map [menu-bar post]
2299       (cons "Post" gnus-article-post-menu))
2300
2301     (easy-menu-define
2302       gnus-summary-misc-menu gnus-summary-mode-map ""
2303       `("Gnus"
2304         ("Mark Read"
2305          ["Mark as read" gnus-summary-mark-as-read-forward t]
2306          ["Mark same subject and select"
2307           gnus-summary-kill-same-subject-and-select t]
2308          ["Mark same subject" gnus-summary-kill-same-subject t]
2309          ["Catchup" gnus-summary-catchup
2310           ,@(if (featurep 'xemacs) '(t)
2311               '(:help "Mark unread articles in this group as read"))]
2312          ["Catchup all" gnus-summary-catchup-all t]
2313          ["Catchup to here" gnus-summary-catchup-to-here t]
2314          ["Catchup from here" gnus-summary-catchup-from-here t]
2315          ["Catchup region" gnus-summary-mark-region-as-read 
2316           (gnus-mark-active-p)]
2317          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2318         ("Mark Various"
2319          ["Tick" gnus-summary-tick-article-forward t]
2320          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2321          ["Remove marks" gnus-summary-clear-mark-forward t]
2322          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2323          ["Set bookmark" gnus-summary-set-bookmark t]
2324          ["Remove bookmark" gnus-summary-remove-bookmark t])
2325         ("Limit to"
2326          ["Marks..." gnus-summary-limit-to-marks t]
2327          ["Subject..." gnus-summary-limit-to-subject t]
2328          ["Author..." gnus-summary-limit-to-author t]
2329          ["Age..." gnus-summary-limit-to-age t]
2330          ["Extra..." gnus-summary-limit-to-extra t]
2331          ["Score..." gnus-summary-limit-to-score t]
2332          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2333          ["Unread" gnus-summary-limit-to-unread t]
2334          ["Unseen" gnus-summary-limit-to-unseen t]
2335          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2336          ["Next articles" gnus-summary-limit-to-articles t]
2337          ["Pop limit" gnus-summary-pop-limit t]
2338          ["Show dormant" gnus-summary-limit-include-dormant t]
2339          ["Hide childless dormant"
2340           gnus-summary-limit-exclude-childless-dormant t]
2341          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2342          ["Hide marked" gnus-summary-limit-exclude-marks t]
2343          ["Show expunged" gnus-summary-limit-include-expunged t])
2344         ("Process Mark"
2345          ["Set mark" gnus-summary-mark-as-processable t]
2346          ["Remove mark" gnus-summary-unmark-as-processable t]
2347          ["Remove all marks" gnus-summary-unmark-all-processable t]
2348          ["Mark above" gnus-uu-mark-over t]
2349          ["Mark series" gnus-uu-mark-series t]
2350          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2351          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2352          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2353          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2354          ["Mark all" gnus-uu-mark-all t]
2355          ["Mark buffer" gnus-uu-mark-buffer t]
2356          ["Mark sparse" gnus-uu-mark-sparse t]
2357          ["Mark thread" gnus-uu-mark-thread t]
2358          ["Unmark thread" gnus-uu-unmark-thread t]
2359          ("Process Mark Sets"
2360           ["Kill" gnus-summary-kill-process-mark t]
2361           ["Yank" gnus-summary-yank-process-mark
2362            gnus-newsgroup-process-stack]
2363           ["Save" gnus-summary-save-process-mark t]
2364           ["Run command on marked..." gnus-summary-universal-argument t]))
2365         ("Scroll article"
2366          ["Page forward" gnus-summary-next-page
2367           ,@(if (featurep 'xemacs) '(t)
2368               '(:help "Show next page of article"))]
2369          ["Page backward" gnus-summary-prev-page
2370           ,@(if (featurep 'xemacs) '(t)
2371               '(:help "Show previous page of article"))]
2372          ["Line forward" gnus-summary-scroll-up t])
2373         ("Move"
2374          ["Next unread article" gnus-summary-next-unread-article t]
2375          ["Previous unread article" gnus-summary-prev-unread-article t]
2376          ["Next article" gnus-summary-next-article t]
2377          ["Previous article" gnus-summary-prev-article t]
2378          ["Next unread subject" gnus-summary-next-unread-subject t]
2379          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2380          ["Next article same subject" gnus-summary-next-same-subject t]
2381          ["Previous article same subject" gnus-summary-prev-same-subject t]
2382          ["First unread article" gnus-summary-first-unread-article t]
2383          ["Best unread article" gnus-summary-best-unread-article t]
2384          ["Go to subject number..." gnus-summary-goto-subject t]
2385          ["Go to article number..." gnus-summary-goto-article t]
2386          ["Go to the last article" gnus-summary-goto-last-article t]
2387          ["Pop article off history" gnus-summary-pop-article t])
2388         ("Sort"
2389          ["Sort by number" gnus-summary-sort-by-number t]
2390          ["Sort by author" gnus-summary-sort-by-author t]
2391          ["Sort by subject" gnus-summary-sort-by-subject t]
2392          ["Sort by date" gnus-summary-sort-by-date t]
2393          ["Sort by score" gnus-summary-sort-by-score t]
2394          ["Sort by lines" gnus-summary-sort-by-lines t]
2395          ["Sort by characters" gnus-summary-sort-by-chars t]
2396          ["Randomize" gnus-summary-sort-by-random t]
2397          ["Original sort" gnus-summary-sort-by-original t])
2398         ("Help"
2399          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2400          ["Describe group" gnus-summary-describe-group t]
2401          ["Fetch charter" gnus-group-fetch-charter
2402           ,@(if (featurep 'xemacs) nil
2403               '(:help "Display the charter of the current group"))]
2404          ["Fetch control message" gnus-group-fetch-control
2405           ,@(if (featurep 'xemacs) nil
2406               '(:help "Display the archived control message for the current group"))]
2407          ["Read manual" gnus-info-find-node t])
2408         ("Modes"
2409          ["Pick and read" gnus-pick-mode t]
2410          ["Binary" gnus-binary-mode t])
2411         ("Regeneration"
2412          ["Regenerate" gnus-summary-prepare t]
2413          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2414          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2415          ["Toggle threading" gnus-summary-toggle-threads t])
2416         ["See old articles" gnus-summary-insert-old-articles t]
2417         ["See new articles" gnus-summary-insert-new-articles t]
2418         ["Filter articles..." gnus-summary-execute-command t]
2419         ["Run command on articles..." gnus-summary-universal-argument t]
2420         ["Search articles forward..." gnus-summary-search-article-forward t]
2421         ["Search articles backward..." gnus-summary-search-article-backward t]
2422         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2423         ["Expand window" gnus-summary-expand-window t]
2424         ["Expire expirable articles" gnus-summary-expire-articles
2425          (gnus-check-backend-function
2426           'request-expire-articles gnus-newsgroup-name)]
2427         ["Edit local kill file" gnus-summary-edit-local-kill t]
2428         ["Edit main kill file" gnus-summary-edit-global-kill t]
2429         ["Edit group parameters" gnus-summary-edit-parameters t]
2430         ["Customize group parameters" gnus-summary-customize-parameters t]
2431         ["Send a bug report" gnus-bug t]
2432         ("Exit"
2433          ["Catchup and exit" gnus-summary-catchup-and-exit
2434           ,@(if (featurep 'xemacs) '(t)
2435               '(:help "Mark unread articles in this group as read, then exit"))]
2436          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2437          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2438          ["Exit group" gnus-summary-exit
2439           ,@(if (featurep 'xemacs) '(t)
2440               '(:help "Exit current group, return to group selection mode"))]
2441          ["Exit group without updating" gnus-summary-exit-no-update t]
2442          ["Exit and goto next group" gnus-summary-next-group t]
2443          ["Exit and goto prev group" gnus-summary-prev-group t]
2444          ["Reselect group" gnus-summary-reselect-current-group t]
2445          ["Rescan group" gnus-summary-rescan-group t]
2446          ["Update dribble" gnus-summary-save-newsrc t])))
2447
2448     (gnus-run-hooks 'gnus-summary-menu-hook)))
2449
2450 (defvar gnus-summary-tool-bar-map nil)
2451
2452 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2453 (defun gnus-summary-make-tool-bar ()
2454   (if (and (fboundp 'tool-bar-add-item-from-menu)
2455            (default-value 'tool-bar-mode)
2456            (not gnus-summary-tool-bar-map))
2457       (setq gnus-summary-tool-bar-map
2458             (let ((tool-bar-map (make-sparse-keymap))
2459                   (load-path (mm-image-load-path)))
2460               (tool-bar-add-item-from-menu
2461                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2462               (tool-bar-add-item-from-menu
2463                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2464               (tool-bar-add-item-from-menu
2465                'gnus-summary-post-news "post" gnus-summary-mode-map)
2466               (tool-bar-add-item-from-menu
2467                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2468               (tool-bar-add-item-from-menu
2469                'gnus-summary-followup "followup" gnus-summary-mode-map)
2470               (tool-bar-add-item-from-menu
2471                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2472               (tool-bar-add-item-from-menu
2473                'gnus-summary-reply "reply" gnus-summary-mode-map)
2474               (tool-bar-add-item-from-menu
2475                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2476               (tool-bar-add-item-from-menu
2477                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2478               (tool-bar-add-item-from-menu
2479                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2480               (tool-bar-add-item-from-menu
2481                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2482               (tool-bar-add-item-from-menu
2483                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2484               (tool-bar-add-item-from-menu
2485                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2486               (tool-bar-add-item-from-menu
2487                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2488               (tool-bar-add-item-from-menu
2489                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2490               tool-bar-map)))
2491   (if gnus-summary-tool-bar-map
2492       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2493
2494 (defun gnus-score-set-default (var value)
2495   "A version of set that updates the GNU Emacs menu-bar."
2496   (set var value)
2497   ;; It is the message that forces the active status to be updated.
2498   (message ""))
2499
2500 (defun gnus-make-score-map (type)
2501   "Make a summary score map of type TYPE."
2502   (if t
2503       nil
2504     (let ((headers '(("author" "from" string)
2505                      ("subject" "subject" string)
2506                      ("article body" "body" string)
2507                      ("article head" "head" string)
2508                      ("xref" "xref" string)
2509                      ("extra header" "extra" string)
2510                      ("lines" "lines" number)
2511                      ("followups to author" "followup" string)))
2512           (types '((number ("less than" <)
2513                            ("greater than" >)
2514                            ("equal" =))
2515                    (string ("substring" s)
2516                            ("exact string" e)
2517                            ("fuzzy string" f)
2518                            ("regexp" r))))
2519           (perms '(("temporary" (current-time-string))
2520                    ("permanent" nil)
2521                    ("immediate" now)))
2522           header)
2523       (list
2524        (apply
2525         'nconc
2526         (list
2527          (if (eq type 'lower)
2528              "Lower score"
2529            "Increase score"))
2530         (let (outh)
2531           (while headers
2532             (setq header (car headers))
2533             (setq outh
2534                   (cons
2535                    (apply
2536                     'nconc
2537                     (list (car header))
2538                     (let ((ts (cdr (assoc (nth 2 header) types)))
2539                           outt)
2540                       (while ts
2541                         (setq outt
2542                               (cons
2543                                (apply
2544                                 'nconc
2545                                 (list (caar ts))
2546                                 (let ((ps perms)
2547                                       outp)
2548                                   (while ps
2549                                     (setq outp
2550                                           (cons
2551                                            (vector
2552                                             (caar ps)
2553                                             (list
2554                                              'gnus-summary-score-entry
2555                                              (nth 1 header)
2556                                              (if (or (string= (nth 1 header)
2557                                                               "head")
2558                                                      (string= (nth 1 header)
2559                                                               "body"))
2560                                                  ""
2561                                                (list 'gnus-summary-header
2562                                                      (nth 1 header)))
2563                                              (list 'quote (nth 1 (car ts)))
2564                                              (list 'gnus-score-delta-default
2565                                                    nil)
2566                                              (nth 1 (car ps))
2567                                              t)
2568                                             t)
2569                                            outp))
2570                                     (setq ps (cdr ps)))
2571                                   (list (nreverse outp))))
2572                                outt))
2573                         (setq ts (cdr ts)))
2574                       (list (nreverse outt))))
2575                    outh))
2576             (setq headers (cdr headers)))
2577           (list (nreverse outh))))))))
2578
2579 \f
2580
2581 (defun gnus-summary-mode (&optional group)
2582   "Major mode for reading articles.
2583
2584 All normal editing commands are switched off.
2585 \\<gnus-summary-mode-map>
2586 Each line in this buffer represents one article.  To read an
2587 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2588 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2589 respectively.
2590
2591 You can also post articles and send mail from this buffer.  To
2592 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2593 of an article, type `\\[gnus-summary-reply]'.
2594
2595 There are approx. one gazillion commands you can execute in this
2596 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2597
2598 The following commands are available:
2599
2600 \\{gnus-summary-mode-map}"
2601   (interactive)
2602   (kill-all-local-variables)
2603   (when (gnus-visual-p 'summary-menu 'menu)
2604     (gnus-summary-make-menu-bar)
2605     (gnus-summary-make-tool-bar))
2606   (gnus-summary-make-local-variables)
2607   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2608     (gnus-summary-make-local-variables))
2609   (gnus-make-thread-indent-array)
2610   (gnus-simplify-mode-line)
2611   (setq major-mode 'gnus-summary-mode)
2612   (setq mode-name "Summary")
2613   (make-local-variable 'minor-mode-alist)
2614   (use-local-map gnus-summary-mode-map)
2615   (buffer-disable-undo)
2616   (setq buffer-read-only t)             ;Disable modification
2617   (setq truncate-lines t)
2618   (setq selective-display t)
2619   (setq selective-display-ellipses t)   ;Display `...'
2620   (gnus-summary-set-display-table)
2621   (gnus-set-default-directory)
2622   (setq gnus-newsgroup-name group)
2623   (make-local-variable 'gnus-summary-line-format)
2624   (make-local-variable 'gnus-summary-line-format-spec)
2625   (make-local-variable 'gnus-summary-dummy-line-format)
2626   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2627   (make-local-variable 'gnus-summary-mark-positions)
2628   (make-local-hook 'pre-command-hook)
2629   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2630   (gnus-run-hooks 'gnus-summary-mode-hook)
2631   (turn-on-gnus-mailing-list-mode)
2632   (mm-enable-multibyte)
2633   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2634   (gnus-update-summary-mark-positions))
2635
2636 (defun gnus-summary-make-local-variables ()
2637   "Make all the local summary buffer variables."
2638   (let (global)
2639     (dolist (local gnus-summary-local-variables)
2640       (if (consp local)
2641           (progn
2642             (if (eq (cdr local) 'global)
2643                 ;; Copy the global value of the variable.
2644                 (setq global (symbol-value (car local)))
2645               ;; Use the value from the list.
2646               (setq global (eval (cdr local))))
2647             (set (make-local-variable (car local)) global))
2648         ;; Simple nil-valued local variable.
2649         (set (make-local-variable local) nil)))))
2650
2651 (defun gnus-summary-clear-local-variables ()
2652   (let ((locals gnus-summary-local-variables))
2653     (while locals
2654       (if (consp (car locals))
2655           (and (vectorp (caar locals))
2656                (set (caar locals) nil))
2657         (and (vectorp (car locals))
2658              (set (car locals) nil)))
2659       (setq locals (cdr locals)))))
2660
2661 ;; Summary data functions.
2662
2663 (defmacro gnus-data-number (data)
2664   `(car ,data))
2665
2666 (defmacro gnus-data-set-number (data number)
2667   `(setcar ,data ,number))
2668
2669 (defmacro gnus-data-mark (data)
2670   `(nth 1 ,data))
2671
2672 (defmacro gnus-data-set-mark (data mark)
2673   `(setcar (nthcdr 1 ,data) ,mark))
2674
2675 (defmacro gnus-data-pos (data)
2676   `(nth 2 ,data))
2677
2678 (defmacro gnus-data-set-pos (data pos)
2679   `(setcar (nthcdr 2 ,data) ,pos))
2680
2681 (defmacro gnus-data-header (data)
2682   `(nth 3 ,data))
2683
2684 (defmacro gnus-data-set-header (data header)
2685   `(setf (nth 3 ,data) ,header))
2686
2687 (defmacro gnus-data-level (data)
2688   `(nth 4 ,data))
2689
2690 (defmacro gnus-data-unread-p (data)
2691   `(= (nth 1 ,data) gnus-unread-mark))
2692
2693 (defmacro gnus-data-read-p (data)
2694   `(/= (nth 1 ,data) gnus-unread-mark))
2695
2696 (defmacro gnus-data-pseudo-p (data)
2697   `(consp (nth 3 ,data)))
2698
2699 (defmacro gnus-data-find (number)
2700   `(assq ,number gnus-newsgroup-data))
2701
2702 (defmacro gnus-data-find-list (number &optional data)
2703   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2704      (memq (assq ,number bdata)
2705            bdata)))
2706
2707 (defmacro gnus-data-make (number mark pos header level)
2708   `(list ,number ,mark ,pos ,header ,level))
2709
2710 (defun gnus-data-enter (after-article number mark pos header level offset)
2711   (let ((data (gnus-data-find-list after-article)))
2712     (unless data
2713       (error "No such article: %d" after-article))
2714     (setcdr data (cons (gnus-data-make number mark pos header level)
2715                        (cdr data)))
2716     (setq gnus-newsgroup-data-reverse nil)
2717     (gnus-data-update-list (cddr data) offset)))
2718
2719 (defun gnus-data-enter-list (after-article list &optional offset)
2720   (when list
2721     (let ((data (and after-article (gnus-data-find-list after-article)))
2722           (ilist list))
2723       (if (not (or data
2724                    after-article))
2725           (let ((odata gnus-newsgroup-data))
2726             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2727             (when offset
2728               (gnus-data-update-list odata offset)))
2729       ;; Find the last element in the list to be spliced into the main
2730         ;; list.
2731         (while (cdr list)
2732           (setq list (cdr list)))
2733         (if (not data)
2734             (progn
2735               (setcdr list gnus-newsgroup-data)
2736               (setq gnus-newsgroup-data ilist)
2737               (when offset
2738                 (gnus-data-update-list (cdr list) offset)))
2739           (setcdr list (cdr data))
2740           (setcdr data ilist)
2741           (when offset
2742             (gnus-data-update-list (cdr list) offset))))
2743       (setq gnus-newsgroup-data-reverse nil))))
2744
2745 (defun gnus-data-remove (article &optional offset)
2746   (let ((data gnus-newsgroup-data))
2747     (if (= (gnus-data-number (car data)) article)
2748         (progn
2749           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2750                 gnus-newsgroup-data-reverse nil)
2751           (when offset
2752             (gnus-data-update-list gnus-newsgroup-data offset)))
2753       (while (cdr data)
2754         (when (= (gnus-data-number (cadr data)) article)
2755           (setcdr data (cddr data))
2756           (when offset
2757             (gnus-data-update-list (cdr data) offset))
2758           (setq data nil
2759                 gnus-newsgroup-data-reverse nil))
2760         (setq data (cdr data))))))
2761
2762 (defmacro gnus-data-list (backward)
2763   `(if ,backward
2764        (or gnus-newsgroup-data-reverse
2765            (setq gnus-newsgroup-data-reverse
2766                  (reverse gnus-newsgroup-data)))
2767      gnus-newsgroup-data))
2768
2769 (defun gnus-data-update-list (data offset)
2770   "Add OFFSET to the POS of all data entries in DATA."
2771   (setq gnus-newsgroup-data-reverse nil)
2772   (while data
2773     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2774     (setq data (cdr data))))
2775
2776 (defun gnus-summary-article-pseudo-p (article)
2777   "Say whether this article is a pseudo article or not."
2778   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2779
2780 (defmacro gnus-summary-article-sparse-p (article)
2781   "Say whether this article is a sparse article or not."
2782   `(memq ,article gnus-newsgroup-sparse))
2783
2784 (defmacro gnus-summary-article-ancient-p (article)
2785   "Say whether this article is a sparse article or not."
2786   `(memq ,article gnus-newsgroup-ancient))
2787
2788 (defun gnus-article-parent-p (number)
2789   "Say whether this article is a parent or not."
2790   (let ((data (gnus-data-find-list number)))
2791     (and (cdr data)              ; There has to be an article after...
2792          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2793             (gnus-data-level (nth 1 data))))))
2794
2795 (defun gnus-article-children (number)
2796   "Return a list of all children to NUMBER."
2797   (let* ((data (gnus-data-find-list number))
2798          (level (gnus-data-level (car data)))
2799          children)
2800     (setq data (cdr data))
2801     (while (and data
2802                 (= (gnus-data-level (car data)) (1+ level)))
2803       (push (gnus-data-number (car data)) children)
2804       (setq data (cdr data)))
2805     children))
2806
2807 (defmacro gnus-summary-skip-intangible ()
2808   "If the current article is intangible, then jump to a different article."
2809   '(let ((to (get-text-property (point) 'gnus-intangible)))
2810      (and to (gnus-summary-goto-subject to))))
2811
2812 (defmacro gnus-summary-article-intangible-p ()
2813   "Say whether this article is intangible or not."
2814   '(get-text-property (point) 'gnus-intangible))
2815
2816 (defun gnus-article-read-p (article)
2817   "Say whether ARTICLE is read or not."
2818   (not (or (memq article gnus-newsgroup-marked)
2819            (memq article gnus-newsgroup-spam-marked)
2820            (memq article gnus-newsgroup-unreads)
2821            (memq article gnus-newsgroup-unselected)
2822            (memq article gnus-newsgroup-dormant))))
2823
2824 ;; Some summary mode macros.
2825
2826 (defmacro gnus-summary-article-number ()
2827   "The article number of the article on the current line.
2828 If there isn't an article number here, then we return the current
2829 article number."
2830   '(progn
2831      (gnus-summary-skip-intangible)
2832      (or (get-text-property (point) 'gnus-number)
2833          (gnus-summary-last-subject))))
2834
2835 (defmacro gnus-summary-article-header (&optional number)
2836   "Return the header of article NUMBER."
2837   `(gnus-data-header (gnus-data-find
2838                       ,(or number '(gnus-summary-article-number)))))
2839
2840 (defmacro gnus-summary-thread-level (&optional number)
2841   "Return the level of thread that starts with article NUMBER."
2842   `(if (and (eq gnus-summary-make-false-root 'dummy)
2843             (get-text-property (point) 'gnus-intangible))
2844        0
2845      (gnus-data-level (gnus-data-find
2846                        ,(or number '(gnus-summary-article-number))))))
2847
2848 (defmacro gnus-summary-article-mark (&optional number)
2849   "Return the mark of article NUMBER."
2850   `(gnus-data-mark (gnus-data-find
2851                     ,(or number '(gnus-summary-article-number)))))
2852
2853 (defmacro gnus-summary-article-pos (&optional number)
2854   "Return the position of the line of article NUMBER."
2855   `(gnus-data-pos (gnus-data-find
2856                    ,(or number '(gnus-summary-article-number)))))
2857
2858 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2859 (defmacro gnus-summary-article-subject (&optional number)
2860   "Return current subject string or nil if nothing."
2861   `(let ((headers
2862           ,(if number
2863                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2864              '(gnus-data-header (assq (gnus-summary-article-number)
2865                                       gnus-newsgroup-data)))))
2866      (and headers
2867           (vectorp headers)
2868           (mail-header-subject headers))))
2869
2870 (defmacro gnus-summary-article-score (&optional number)
2871   "Return current article score."
2872   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2873                   gnus-newsgroup-scored))
2874        gnus-summary-default-score 0))
2875
2876 (defun gnus-summary-article-children (&optional number)
2877   "Return a list of article numbers that are children of article NUMBER."
2878   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2879          (level (gnus-data-level (car data)))
2880          l children)
2881     (while (and (setq data (cdr data))
2882                 (> (setq l (gnus-data-level (car data))) level))
2883       (and (= (1+ level) l)
2884            (push (gnus-data-number (car data))
2885                  children)))
2886     (nreverse children)))
2887
2888 (defun gnus-summary-article-parent (&optional number)
2889   "Return the article number of the parent of article NUMBER."
2890   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2891                                     (gnus-data-list t)))
2892          (level (gnus-data-level (car data))))
2893     (if (zerop level)
2894         ()                              ; This is a root.
2895       ;; We search until we find an article with a level less than
2896       ;; this one.  That function has to be the parent.
2897       (while (and (setq data (cdr data))
2898                   (not (< (gnus-data-level (car data)) level))))
2899       (and data (gnus-data-number (car data))))))
2900
2901 (defun gnus-unread-mark-p (mark)
2902   "Say whether MARK is the unread mark."
2903   (= mark gnus-unread-mark))
2904
2905 (defun gnus-read-mark-p (mark)
2906   "Say whether MARK is one of the marks that mark as read.
2907 This is all marks except unread, ticked, dormant, and expirable."
2908   (not (or (= mark gnus-unread-mark)
2909            (= mark gnus-ticked-mark)
2910            (= mark gnus-spam-mark)
2911            (= mark gnus-dormant-mark)
2912            (= mark gnus-expirable-mark))))
2913
2914 (defmacro gnus-article-mark (number)
2915   "Return the MARK of article NUMBER.
2916 This macro should only be used when computing the mark the \"first\"
2917 time; i.e., when generating the summary lines.  After that,
2918 `gnus-summary-article-mark' should be used to examine the
2919 marks of articles."
2920   `(cond
2921     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2922     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2923     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2924     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2925     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2926     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2927     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2928     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2929            gnus-ancient-mark))))
2930
2931 ;; Saving hidden threads.
2932
2933 (defmacro gnus-save-hidden-threads (&rest forms)
2934   "Save hidden threads, eval FORMS, and restore the hidden threads."
2935   (let ((config (make-symbol "config")))
2936     `(let ((,config (gnus-hidden-threads-configuration)))
2937        (unwind-protect
2938            (save-excursion
2939              ,@forms)
2940          (gnus-restore-hidden-threads-configuration ,config)))))
2941 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2942 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2943
2944 (defun gnus-data-compute-positions ()
2945   "Compute the positions of all articles."
2946   (setq gnus-newsgroup-data-reverse nil)
2947   (let ((data gnus-newsgroup-data))
2948     (save-excursion
2949       (gnus-save-hidden-threads
2950         (gnus-summary-show-all-threads)
2951         (goto-char (point-min))
2952         (while data
2953           (while (get-text-property (point) 'gnus-intangible)
2954             (forward-line 1))
2955           (gnus-data-set-pos (car data) (+ (point) 3))
2956           (setq data (cdr data))
2957           (forward-line 1))))))
2958
2959 (defun gnus-hidden-threads-configuration ()
2960   "Return the current hidden threads configuration."
2961   (save-excursion
2962     (let (config)
2963       (goto-char (point-min))
2964       (while (search-forward "\r" nil t)
2965         (push (1- (point)) config))
2966       config)))
2967
2968 (defun gnus-restore-hidden-threads-configuration (config)
2969   "Restore hidden threads configuration from CONFIG."
2970   (save-excursion
2971     (let (point buffer-read-only)
2972       (while (setq point (pop config))
2973         (when (and (< point (point-max))
2974                    (goto-char point)
2975                    (eq (char-after) ?\n))
2976           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2977
2978 ;; Various summary mode internalish functions.
2979
2980 (defun gnus-mouse-pick-article (e)
2981   (interactive "e")
2982   (mouse-set-point e)
2983   (gnus-summary-next-page nil t))
2984
2985 (defun gnus-summary-set-display-table ()
2986   "Change the display table.
2987 Odd characters have a tendency to mess
2988 up nicely formatted displays - we make all possible glyphs
2989 display only a single character."
2990
2991   ;; We start from the standard display table, if any.
2992   (let ((table (or (copy-sequence standard-display-table)
2993                    (make-display-table)))
2994         (i 32))
2995     ;; Nix out all the control chars...
2996     (while (>= (setq i (1- i)) 0)
2997       (aset table i [??]))
2998    ;; ... but not newline and cr, of course.  (cr is necessary for the
2999     ;; selective display).
3000     (aset table ?\n nil)
3001     (aset table ?\r nil)
3002     ;; We keep TAB as well.
3003     (aset table ?\t nil)
3004     ;; We nix out any glyphs over 126 that are not set already.
3005     (let ((i 256))
3006       (while (>= (setq i (1- i)) 127)
3007         ;; Only modify if the entry is nil.
3008         (unless (aref table i)
3009           (aset table i [??]))))
3010     (setq buffer-display-table table)))
3011
3012 (defun gnus-summary-set-article-display-arrow (pos)
3013   "Update the overlay arrow to point to line at position POS."
3014   (when (and gnus-summary-display-arrow
3015              (boundp 'overlay-arrow-position)
3016              (boundp 'overlay-arrow-string))
3017     (save-excursion
3018       (goto-char pos)
3019       (beginning-of-line)
3020       (unless overlay-arrow-position
3021         (setq overlay-arrow-position (make-marker)))
3022       (setq overlay-arrow-string "=>"
3023             overlay-arrow-position (set-marker overlay-arrow-position
3024                                                (point)
3025                                                (current-buffer))))))
3026
3027 (defun gnus-summary-buffer-name (group)
3028   "Return the summary buffer name of GROUP."
3029   (concat "*Summary " (gnus-group-decoded-name group) "*"))
3030
3031 (defun gnus-summary-setup-buffer (group)
3032   "Initialize summary buffer."
3033   (let ((buffer (gnus-summary-buffer-name group))
3034         (dead-name (concat "*Dead Summary "
3035                            (gnus-group-decoded-name group) "*")))
3036     ;; If a dead summary buffer exists, we kill it.
3037     (when (gnus-buffer-live-p dead-name)
3038       (gnus-kill-buffer dead-name))
3039     (if (get-buffer buffer)
3040         (progn
3041           (set-buffer buffer)
3042           (setq gnus-summary-buffer (current-buffer))
3043           (not gnus-newsgroup-prepared))
3044       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3045       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3046       (gnus-summary-mode group)
3047       (when gnus-carpal
3048         (gnus-carpal-setup-buffer 'summary))
3049       (unless gnus-single-article-buffer
3050         (make-local-variable 'gnus-article-buffer)
3051         (make-local-variable 'gnus-article-current)
3052         (make-local-variable 'gnus-original-article-buffer))
3053       (setq gnus-newsgroup-name group)
3054       ;; Set any local variables in the group parameters.
3055       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3056       t)))
3057
3058 (defun gnus-set-global-variables ()
3059   "Set the global equivalents of the buffer-local variables.
3060 They are set to the latest values they had.  These reflect the summary
3061 buffer that was in action when the last article was fetched."
3062   (when (eq major-mode 'gnus-summary-mode)
3063     (setq gnus-summary-buffer (current-buffer))
3064     (let ((name gnus-newsgroup-name)
3065           (marked gnus-newsgroup-marked)
3066           (spam gnus-newsgroup-spam-marked)
3067           (unread gnus-newsgroup-unreads)
3068           (headers gnus-current-headers)
3069           (data gnus-newsgroup-data)
3070           (summary gnus-summary-buffer)
3071           (article-buffer gnus-article-buffer)
3072           (original gnus-original-article-buffer)
3073           (gac gnus-article-current)
3074           (reffed gnus-reffed-article-number)
3075           (score-file gnus-current-score-file)
3076           (default-charset gnus-newsgroup-charset)
3077           vlist)
3078       (let ((locals gnus-newsgroup-variables))
3079         (while locals
3080           (if (consp (car locals))
3081               (push (eval (caar locals)) vlist)
3082             (push (eval (car locals)) vlist))
3083           (setq locals (cdr locals)))
3084         (setq vlist (nreverse vlist)))
3085       (save-excursion
3086         (set-buffer gnus-group-buffer)
3087         (setq gnus-newsgroup-name name
3088               gnus-newsgroup-marked marked
3089               gnus-newsgroup-spam-marked spam
3090               gnus-newsgroup-unreads unread
3091               gnus-current-headers headers
3092               gnus-newsgroup-data data
3093               gnus-article-current gac
3094               gnus-summary-buffer summary
3095               gnus-article-buffer article-buffer
3096               gnus-original-article-buffer original
3097               gnus-reffed-article-number reffed
3098               gnus-current-score-file score-file
3099               gnus-newsgroup-charset default-charset)
3100         (let ((locals gnus-newsgroup-variables))
3101           (while locals
3102             (if (consp (car locals))
3103                 (set (caar locals) (pop vlist))
3104               (set (car locals) (pop vlist)))
3105             (setq locals (cdr locals))))
3106         ;; The article buffer also has local variables.
3107         (when (gnus-buffer-live-p gnus-article-buffer)
3108           (set-buffer gnus-article-buffer)
3109           (setq gnus-summary-buffer summary))))))
3110
3111 (defun gnus-summary-article-unread-p (article)
3112   "Say whether ARTICLE is unread or not."
3113   (memq article gnus-newsgroup-unreads))
3114
3115 (defun gnus-summary-first-article-p (&optional article)
3116   "Return whether ARTICLE is the first article in the buffer."
3117   (if (not (setq article (or article (gnus-summary-article-number))))
3118       nil
3119     (eq article (caar gnus-newsgroup-data))))
3120
3121 (defun gnus-summary-last-article-p (&optional article)
3122   "Return whether ARTICLE is the last article in the buffer."
3123   (if (not (setq article (or article (gnus-summary-article-number))))
3124       ;; All non-existent numbers are the last article.  :-)
3125       t
3126     (not (cdr (gnus-data-find-list article)))))
3127
3128 (defun gnus-make-thread-indent-array ()
3129   (let ((n 200))
3130     (unless (and gnus-thread-indent-array
3131                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3132       (setq gnus-thread-indent-array (make-vector 201 "")
3133             gnus-thread-indent-array-level gnus-thread-indent-level)
3134       (while (>= n 0)
3135         (aset gnus-thread-indent-array n
3136               (make-string (* n gnus-thread-indent-level) ? ))
3137         (setq n (1- n))))))
3138
3139 (defun gnus-update-summary-mark-positions ()
3140   "Compute where the summary marks are to go."
3141   (save-excursion
3142     (when (gnus-buffer-exists-p gnus-summary-buffer)
3143       (set-buffer gnus-summary-buffer))
3144     (let ((gnus-replied-mark 129)
3145           (gnus-score-below-mark 130)
3146           (gnus-score-over-mark 130)
3147           (gnus-undownloaded-mark 131)
3148           (spec gnus-summary-line-format-spec)
3149           gnus-visual pos)
3150       (save-excursion
3151         (gnus-set-work-buffer)
3152         (let ((gnus-summary-line-format-spec spec)
3153               (gnus-newsgroup-downloadable '(0)))
3154           (gnus-summary-insert-line
3155            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3156            0 nil t 128 t nil "" nil 1)
3157           (goto-char (point-min))
3158           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3159                                              (- (point) (point-min) 1)))))
3160           (goto-char (point-min))
3161           (push (cons 'replied (and (search-forward "\201" nil t)
3162                                     (- (point) (point-min) 1)))
3163                 pos)
3164           (goto-char (point-min))
3165           (push (cons 'score (and (search-forward "\202" nil t)
3166                                   (- (point) (point-min) 1)))
3167                 pos)
3168           (goto-char (point-min))
3169           (push (cons 'download
3170                       (and (search-forward "\203" nil t)
3171                            (- (point) (point-min) 1)))
3172                 pos)))
3173       (setq gnus-summary-mark-positions pos))))
3174
3175 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3176   "Insert a dummy root in the summary buffer."
3177   (beginning-of-line)
3178   (gnus-add-text-properties
3179    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3180    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3181
3182 (defun gnus-summary-extract-address-component (from)
3183   (or (car (funcall gnus-extract-address-components from))
3184       from))
3185
3186 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3187   (let ((mail-parse-charset gnus-newsgroup-charset)
3188         ; Is it really necessary to do this next part for each summary line?
3189         ; Luckily, doesn't seem to slow things down much.
3190         (mail-parse-ignored-charsets
3191          (save-excursion (set-buffer gnus-summary-buffer)
3192                          gnus-newsgroup-ignored-charsets)))
3193     (or
3194      (and gnus-ignored-from-addresses
3195           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3196           (let ((extra-headers (mail-header-extra header))
3197                 to
3198                 newsgroups)
3199             (cond
3200              ((setq to (cdr (assq 'To extra-headers)))
3201               (concat "-> "
3202                       (inline
3203                         (gnus-summary-extract-address-component
3204                          (funcall gnus-decode-encoded-word-function to)))))
3205              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3206               (concat "=> " newsgroups)))))
3207      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3208
3209 (defun gnus-summary-insert-line (gnus-tmp-header
3210                                  gnus-tmp-level gnus-tmp-current
3211                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3212                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3213                                  &optional gnus-tmp-dummy gnus-tmp-score
3214                                  gnus-tmp-process)
3215   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3216          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3217          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3218          (gnus-tmp-score-char
3219           (if (or (null gnus-summary-default-score)
3220                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3221                       gnus-summary-zcore-fuzz))
3222               ?                         ;Whitespace
3223             (if (< gnus-tmp-score gnus-summary-default-score)
3224                 gnus-score-below-mark gnus-score-over-mark)))
3225          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3226          (gnus-tmp-replied
3227           (cond (gnus-tmp-process gnus-process-mark)
3228                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3229                  gnus-cached-mark)
3230                 (gnus-tmp-replied gnus-replied-mark)
3231                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3232                  gnus-forwarded-mark)
3233                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3234                  gnus-saved-mark)
3235                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3236                  gnus-recent-mark)
3237                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3238                  gnus-unseen-mark)
3239                 (t gnus-no-mark)))
3240          (gnus-tmp-downloaded
3241           (cond (undownloaded 
3242                  gnus-undownloaded-mark)
3243                 (gnus-newsgroup-agentized
3244                  gnus-downloaded-mark)
3245                 (t
3246                  gnus-no-mark)))
3247          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3248          (gnus-tmp-name
3249           (cond
3250            ((string-match "<[^>]+> *$" gnus-tmp-from)
3251             (let ((beg (match-beginning 0)))
3252               (or (and (string-match "^\".+\"" gnus-tmp-from)
3253                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3254                   (substring gnus-tmp-from 0 beg))))
3255            ((string-match "(.+)" gnus-tmp-from)
3256             (substring gnus-tmp-from
3257                        (1+ (match-beginning 0)) (1- (match-end 0))))
3258            (t gnus-tmp-from)))
3259          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3260          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3261          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3262          (buffer-read-only nil))
3263     (when (string= gnus-tmp-name "")
3264       (setq gnus-tmp-name gnus-tmp-from))
3265     (unless (numberp gnus-tmp-lines)
3266       (setq gnus-tmp-lines -1))
3267     (if (= gnus-tmp-lines -1)
3268         (setq gnus-tmp-lines "?")
3269       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3270       (gnus-put-text-property
3271      (point)
3272      (progn (eval gnus-summary-line-format-spec) (point))
3273        'gnus-number gnus-tmp-number)
3274     (when (gnus-visual-p 'summary-highlight 'highlight)
3275       (forward-line -1)
3276       (gnus-run-hooks 'gnus-summary-update-hook)
3277       (forward-line 1))))
3278
3279 (defun gnus-summary-update-line (&optional dont-update)
3280   "Update summary line after change."
3281   (when (and gnus-summary-default-score
3282              (not gnus-summary-inhibit-highlight))
3283     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3284            (article (gnus-summary-article-number))
3285            (score (gnus-summary-article-score article)))
3286       (unless dont-update
3287         (if (and gnus-summary-mark-below
3288                  (< (gnus-summary-article-score)
3289                     gnus-summary-mark-below))
3290             ;; This article has a low score, so we mark it as read.
3291             (when (memq article gnus-newsgroup-unreads)
3292               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3293           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3294             ;; This article was previously marked as read on account
3295             ;; of a low score, but now it has risen, so we mark it as
3296             ;; unread.
3297             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3298         (gnus-summary-update-mark
3299          (if (or (null gnus-summary-default-score)
3300                  (<= (abs (- score gnus-summary-default-score))
3301                      gnus-summary-zcore-fuzz))
3302              ?                          ;Whitespace
3303            (if (< score gnus-summary-default-score)
3304                gnus-score-below-mark gnus-score-over-mark))
3305          'score))
3306       ;; Do visual highlighting.
3307       (when (gnus-visual-p 'summary-highlight 'highlight)
3308         (gnus-run-hooks 'gnus-summary-update-hook)))))
3309
3310 (defvar gnus-tmp-new-adopts nil)
3311
3312 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3313   "Return the number of articles in THREAD.
3314 This may be 0 in some cases -- if none of the articles in
3315 the thread are to be displayed."
3316   (let* ((number
3317          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3318           (cond
3319            ((not (listp thread))
3320             1)
3321            ((and (consp thread) (cdr thread))
3322             (apply
3323              '+ 1 (mapcar
3324                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3325            ((null thread)
3326             1)
3327            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3328             1)
3329            (t 0))))
3330     (when (and level (zerop level) gnus-tmp-new-adopts)
3331       (incf number
3332             (apply '+ (mapcar
3333                        'gnus-summary-number-of-articles-in-thread
3334                        gnus-tmp-new-adopts))))
3335     (if char
3336         (if (> number 1) gnus-not-empty-thread-mark
3337           gnus-empty-thread-mark)
3338       number)))
3339
3340 (defsubst gnus-summary-line-message-size (head)
3341   "Return pretty-printed version of message size.
3342 This function is intended to be used in
3343 `gnus-summary-line-format-alist', which see."
3344   (let ((c (or (mail-header-chars head) -1)))
3345     (cond ((< c 0) "n/a")               ; chars not available
3346           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3347           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3348           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3349           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3350
3351
3352 (defun gnus-summary-set-local-parameters (group)
3353   "Go through the local params of GROUP and set all variable specs in that list."
3354   (let ((params (gnus-group-find-parameter group))
3355         (vars '(quit-config))           ; Ignore quit-config.
3356         elem)
3357     (while params
3358       (setq elem (car params)
3359             params (cdr params))
3360       (and (consp elem)                 ; Has to be a cons.
3361            (consp (cdr elem))           ; The cdr has to be a list.
3362            (symbolp (car elem))         ; Has to be a symbol in there.
3363            (not (memq (car elem) vars))
3364            (ignore-errors               ; So we set it.
3365              (push (car elem) vars)
3366              (make-local-variable (car elem))
3367              (set (car elem) (eval (nth 1 elem))))))))
3368
3369 (defun gnus-summary-read-group (group &optional show-all no-article
3370                                       kill-buffer no-display backward
3371                                       select-articles)
3372   "Start reading news in newsgroup GROUP.
3373 If SHOW-ALL is non-nil, already read articles are also listed.
3374 If NO-ARTICLE is non-nil, no article is selected initially.
3375 If NO-DISPLAY, don't generate a summary buffer."
3376   (let (result)
3377     (while (and group
3378                 (null (setq result
3379                             (let ((gnus-auto-select-next nil))
3380                               (or (gnus-summary-read-group-1
3381                                    group show-all no-article
3382                                    kill-buffer no-display
3383                                    select-articles)
3384                                   (setq show-all nil
3385                                         select-articles nil)))))
3386                 (eq gnus-auto-select-next 'quietly))
3387       (set-buffer gnus-group-buffer)
3388       ;; The entry function called above goes to the next
3389       ;; group automatically, so we go two groups back
3390       ;; if we are searching for the previous group.
3391       (when backward
3392         (gnus-group-prev-unread-group 2))
3393       (if (not (equal group (gnus-group-group-name)))
3394           (setq group (gnus-group-group-name))
3395         (setq group nil)))
3396     result))
3397
3398 (defun gnus-summary-read-group-1 (group show-all no-article
3399                                         kill-buffer no-display
3400                                         &optional select-articles)
3401   ;; Killed foreign groups can't be entered.
3402   ;;  (when (and (not (gnus-group-native-p group))
3403   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3404   ;;    (error "Dead non-native groups can't be entered"))
3405   (gnus-message 5 "Retrieving newsgroup: %s..."
3406                 (gnus-group-decoded-name group))
3407   (let* ((new-group (gnus-summary-setup-buffer group))
3408          (quit-config (gnus-group-quit-config group))
3409          (did-select (and new-group (gnus-select-newsgroup
3410                                      group show-all select-articles))))
3411     (cond
3412      ;; This summary buffer exists already, so we just select it.
3413      ((not new-group)
3414       (gnus-set-global-variables)
3415       (when kill-buffer
3416         (gnus-kill-or-deaden-summary kill-buffer))
3417       (gnus-configure-windows 'summary 'force)
3418       (gnus-set-mode-line 'summary)
3419       (gnus-summary-position-point)
3420       (message "")
3421       t)
3422      ;; We couldn't select this group.
3423      ((null did-select)
3424       (when (and (eq major-mode 'gnus-summary-mode)
3425                  (not (equal (current-buffer) kill-buffer)))
3426         (kill-buffer (current-buffer))
3427         (if (not quit-config)
3428             (progn
3429               ;; Update the info -- marks might need to be removed,
3430               ;; for instance.
3431               (gnus-summary-update-info)
3432               (set-buffer gnus-group-buffer)
3433               (gnus-group-jump-to-group group)
3434               (gnus-group-next-unread-group 1))
3435           (gnus-handle-ephemeral-exit quit-config)))
3436       (let ((grpinfo (gnus-get-info group)))
3437         (if (null (gnus-info-read grpinfo))
3438             (gnus-message 3 "Group %s contains no messages"
3439                           (gnus-group-decoded-name group))
3440           (gnus-message 3 "Can't select group")))
3441       nil)
3442      ;; The user did a `C-g' while prompting for number of articles,
3443      ;; so we exit this group.
3444      ((eq did-select 'quit)
3445       (and (eq major-mode 'gnus-summary-mode)
3446            (not (equal (current-buffer) kill-buffer))
3447            (kill-buffer (current-buffer)))
3448       (when kill-buffer
3449         (gnus-kill-or-deaden-summary kill-buffer))
3450       (if (not quit-config)
3451           (progn
3452             (set-buffer gnus-group-buffer)
3453             (gnus-group-jump-to-group group)
3454             (gnus-group-next-unread-group 1)
3455             (gnus-configure-windows 'group 'force))
3456         (gnus-handle-ephemeral-exit quit-config))
3457       ;; Finally signal the quit.
3458       (signal 'quit nil))
3459      ;; The group was successfully selected.
3460      (t
3461       (gnus-set-global-variables)
3462       ;; Save the active value in effect when the group was entered.
3463       (setq gnus-newsgroup-active
3464             (gnus-copy-sequence
3465              (gnus-active gnus-newsgroup-name)))
3466       ;; You can change the summary buffer in some way with this hook.
3467       (gnus-run-hooks 'gnus-select-group-hook)
3468       (gnus-update-format-specifications
3469        nil 'summary 'summary-mode 'summary-dummy)
3470       (gnus-update-summary-mark-positions)
3471       ;; Do score processing.
3472       (when gnus-use-scoring
3473         (gnus-possibly-score-headers))
3474       ;; Check whether to fill in the gaps in the threads.
3475       (when gnus-build-sparse-threads
3476         (gnus-build-sparse-threads))
3477       ;; Find the initial limit.
3478       (if gnus-show-threads
3479           (if show-all
3480               (let ((gnus-newsgroup-dormant nil))
3481                 (gnus-summary-initial-limit show-all))
3482             (gnus-summary-initial-limit show-all))
3483         ;; When unthreaded, all articles are always shown.
3484         (setq gnus-newsgroup-limit
3485               (mapcar
3486                (lambda (header) (mail-header-number header))
3487                gnus-newsgroup-headers)))
3488       ;; Generate the summary buffer.
3489       (unless no-display
3490         (gnus-summary-prepare))
3491       (when gnus-use-trees
3492         (gnus-tree-open group)
3493         (setq gnus-summary-highlight-line-function
3494               'gnus-tree-highlight-article))
3495       ;; If the summary buffer is empty, but there are some low-scored
3496       ;; articles or some excluded dormants, we include these in the
3497       ;; buffer.
3498       (when (and (zerop (buffer-size))
3499                  (not no-display))
3500         (cond (gnus-newsgroup-dormant
3501                (gnus-summary-limit-include-dormant))
3502               ((and gnus-newsgroup-scored show-all)
3503                (gnus-summary-limit-include-expunged t))))
3504       ;; Function `gnus-apply-kill-file' must be called in this hook.
3505       (gnus-run-hooks 'gnus-apply-kill-hook)
3506       (if (and (zerop (buffer-size))
3507                (not no-display))
3508           (progn
3509             ;; This newsgroup is empty.
3510             (gnus-summary-catchup-and-exit nil t)
3511             (gnus-message 6 "No unread news")
3512             (when kill-buffer
3513               (gnus-kill-or-deaden-summary kill-buffer))
3514             ;; Return nil from this function.
3515             nil)
3516         ;; Hide conversation thread subtrees.  We cannot do this in
3517         ;; gnus-summary-prepare-hook since kill processing may not
3518         ;; work with hidden articles.
3519         (gnus-summary-maybe-hide-threads)
3520         (when kill-buffer
3521           (gnus-kill-or-deaden-summary kill-buffer))
3522         (gnus-summary-auto-select-subject)
3523         ;; Show first unread article if requested.
3524         (if (and (not no-article)
3525                  (not no-display)
3526                  gnus-newsgroup-unreads
3527                  gnus-auto-select-first)
3528             (progn
3529               (gnus-configure-windows 'summary)
3530               (let ((art (gnus-summary-article-number)))
3531                 (unless (and (not gnus-plugged)
3532                              (or (memq art gnus-newsgroup-undownloaded)
3533                                  (memq art gnus-newsgroup-downloadable)))
3534                   (gnus-summary-goto-article art))))
3535           ;; Don't select any articles.
3536           (gnus-summary-position-point)
3537           (gnus-configure-windows 'summary 'force)
3538           (gnus-set-mode-line 'summary))
3539         (when (and gnus-auto-center-group
3540                    (get-buffer-window gnus-group-buffer t))
3541           ;; Gotta use windows, because recenter does weird stuff if
3542           ;; the current buffer ain't the displayed window.
3543           (let ((owin (selected-window)))
3544             (select-window (get-buffer-window gnus-group-buffer t))
3545             (when (gnus-group-goto-group group)
3546               (recenter))
3547             (select-window owin)))
3548         ;; Mark this buffer as "prepared".
3549         (setq gnus-newsgroup-prepared t)
3550         (gnus-run-hooks 'gnus-summary-prepared-hook)
3551         (unless (gnus-ephemeral-group-p group)
3552           (gnus-group-update-group group))
3553         t)))))
3554
3555 (defun gnus-summary-auto-select-subject ()
3556   "Select the subject line on initial group entry."
3557   (goto-char (point-min))
3558   (cond
3559    ((eq gnus-auto-select-subject 'best)
3560     (gnus-summary-best-unread-subject))
3561    ((eq gnus-auto-select-subject 'unread)
3562     (gnus-summary-first-unread-subject))
3563    ((eq gnus-auto-select-subject 'unseen)
3564     (gnus-summary-first-unseen-subject))
3565    ((eq gnus-auto-select-subject 'unseen-or-unread)
3566     (gnus-summary-first-unseen-or-unread-subject))
3567    ((eq gnus-auto-select-subject 'first)
3568     ;; Do nothing.
3569     )
3570    ((gnus-functionp gnus-auto-select-subject)
3571     (funcall gnus-auto-select-subject))))
3572
3573 (defun gnus-summary-prepare ()
3574   "Generate the summary buffer."
3575   (interactive)
3576   (let ((buffer-read-only nil))
3577     (erase-buffer)
3578     (setq gnus-newsgroup-data nil
3579           gnus-newsgroup-data-reverse nil)
3580     (gnus-run-hooks 'gnus-summary-generate-hook)
3581     ;; Generate the buffer, either with threads or without.
3582     (when gnus-newsgroup-headers
3583       (gnus-summary-prepare-threads
3584        (if gnus-show-threads
3585            (gnus-sort-gathered-threads
3586             (funcall gnus-summary-thread-gathering-function
3587                      (gnus-sort-threads
3588                       (gnus-cut-threads (gnus-make-threads)))))
3589          ;; Unthreaded display.
3590          (gnus-sort-articles gnus-newsgroup-headers))))
3591     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3592     ;; Call hooks for modifying summary buffer.
3593     (goto-char (point-min))
3594     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3595
3596 (defsubst gnus-general-simplify-subject (subject)
3597   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3598   (setq subject
3599         (cond
3600          ;; Truncate the subject.
3601          (gnus-simplify-subject-functions
3602           (gnus-map-function gnus-simplify-subject-functions subject))
3603          ((numberp gnus-summary-gather-subject-limit)
3604           (setq subject (gnus-simplify-subject-re subject))
3605           (if (> (length subject) gnus-summary-gather-subject-limit)
3606               (substring subject 0 gnus-summary-gather-subject-limit)
3607             subject))
3608          ;; Fuzzily simplify it.
3609          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3610           (gnus-simplify-subject-fuzzy subject))
3611          ;; Just remove the leading "Re:".
3612          (t
3613           (gnus-simplify-subject-re subject))))
3614
3615   (if (and gnus-summary-gather-exclude-subject
3616            (string-match gnus-summary-gather-exclude-subject subject))
3617       nil                         ; This article shouldn't be gathered
3618     subject))
3619
3620 (defun gnus-summary-simplify-subject-query ()
3621   "Query where the respool algorithm would put this article."
3622   (interactive)
3623   (gnus-summary-select-article)
3624   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3625
3626 (defun gnus-gather-threads-by-subject (threads)
3627   "Gather threads by looking at Subject headers."
3628   (if (not gnus-summary-make-false-root)
3629       threads
3630     (let ((hashtb (gnus-make-hashtable 1024))
3631           (prev threads)
3632           (result threads)
3633           subject hthread whole-subject)
3634       (while threads
3635         (setq subject (gnus-general-simplify-subject
3636                        (setq whole-subject (mail-header-subject
3637                                             (caar threads)))))
3638         (when subject
3639           (if (setq hthread (gnus-gethash subject hashtb))
3640               (progn
3641                 ;; We enter a dummy root into the thread, if we
3642                 ;; haven't done that already.
3643                 (unless (stringp (caar hthread))
3644                   (setcar hthread (list whole-subject (car hthread))))
3645                 ;; We add this new gathered thread to this gathered
3646                 ;; thread.
3647                 (setcdr (car hthread)
3648                         (nconc (cdar hthread) (list (car threads))))
3649                 ;; Remove it from the list of threads.
3650                 (setcdr prev (cdr threads))
3651                 (setq threads prev))
3652             ;; Enter this thread into the hash table.
3653             (gnus-sethash subject
3654                           (if gnus-summary-make-false-root-always
3655                               (progn
3656                                 ;; If you want a dummy root above all
3657                                 ;; threads...
3658                                 (setcar threads (list whole-subject
3659                                                       (car threads)))
3660                                 threads)
3661                             threads)
3662                           hashtb)))
3663         (setq prev threads)
3664         (setq threads (cdr threads)))
3665       result)))
3666
3667 (defun gnus-gather-threads-by-references (threads)
3668   "Gather threads by looking at References headers."
3669   (let ((idhashtb (gnus-make-hashtable 1024))
3670         (thhashtb (gnus-make-hashtable 1024))
3671         (prev threads)
3672         (result threads)
3673         ids references id gthread gid entered ref)
3674     (while threads
3675       (when (setq references (mail-header-references (caar threads)))
3676         (setq id (mail-header-id (caar threads))
3677               ids (inline (gnus-split-references references))
3678               entered nil)
3679         (while (setq ref (pop ids))
3680           (setq ids (delete ref ids))
3681           (if (not (setq gid (gnus-gethash ref idhashtb)))
3682               (progn
3683                 (gnus-sethash ref id idhashtb)
3684                 (gnus-sethash id threads thhashtb))
3685             (setq gthread (gnus-gethash gid thhashtb))
3686             (unless entered
3687               ;; We enter a dummy root into the thread, if we
3688               ;; haven't done that already.
3689               (unless (stringp (caar gthread))
3690                 (setcar gthread (list (mail-header-subject (caar gthread))
3691                                       (car gthread))))
3692               ;; We add this new gathered thread to this gathered
3693               ;; thread.
3694               (setcdr (car gthread)
3695                       (nconc (cdar gthread) (list (car threads)))))
3696             ;; Add it into the thread hash table.
3697             (gnus-sethash id gthread thhashtb)
3698             (setq entered t)
3699             ;; Remove it from the list of threads.
3700             (setcdr prev (cdr threads))
3701             (setq threads prev))))
3702       (setq prev threads)
3703       (setq threads (cdr threads)))
3704     result))
3705
3706 (defun gnus-sort-gathered-threads (threads)
3707   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3708   (let ((result threads))
3709     (while threads
3710       (when (stringp (caar threads))
3711         (setcdr (car threads)
3712                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3713       (setq threads (cdr threads)))
3714     result))
3715
3716 (defun gnus-thread-loop-p (root thread)
3717   "Say whether ROOT is in THREAD."
3718   (let ((stack (list thread))
3719         (infloop 0)
3720         th)
3721     (while (setq thread (pop stack))
3722       (setq th (cdr thread))
3723       (while (and th
3724                   (not (eq (caar th) root)))
3725         (pop th))
3726       (if th
3727           ;; We have found a loop.
3728           (let (ref-dep)
3729             (setcdr thread (delq (car th) (cdr thread)))
3730             (if (boundp (setq ref-dep (intern "none"
3731                                               gnus-newsgroup-dependencies)))
3732                 (setcdr (symbol-value ref-dep)
3733                         (nconc (cdr (symbol-value ref-dep))
3734                                (list (car th))))
3735               (set ref-dep (list nil (car th))))
3736             (setq infloop 1
3737                   stack nil))
3738         ;; Push all the subthreads onto the stack.
3739         (push (cdr thread) stack)))
3740     infloop))
3741
3742 (defun gnus-make-threads ()
3743   "Go through the dependency hashtb and find the roots.  Return all threads."
3744   (let (threads)
3745     (while (catch 'infloop
3746              (mapatoms
3747               (lambda (refs)
3748                 ;; Deal with self-referencing References loops.
3749                 (when (and (car (symbol-value refs))
3750                            (not (zerop
3751                                  (apply
3752                                   '+
3753                                   (mapcar
3754                                    (lambda (thread)
3755                                      (gnus-thread-loop-p
3756                                       (car (symbol-value refs)) thread))
3757                                    (cdr (symbol-value refs)))))))
3758                   (setq threads nil)
3759                   (throw 'infloop t))
3760                 (unless (car (symbol-value refs))
3761                   ;; These threads do not refer back to any other
3762                   ;; articles, so they're roots.
3763                   (setq threads (append (cdr (symbol-value refs)) threads))))
3764               gnus-newsgroup-dependencies)))
3765     threads))
3766
3767 ;; Build the thread tree.
3768 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3769   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3770
3771 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3772 if it was already present.
3773
3774 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3775 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3776 Message-IDs will be renamed to a unique Message-ID before being
3777 entered.
3778
3779 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3780   (let* ((id (mail-header-id header))
3781          (id-dep (and id (intern id dependencies)))
3782          parent-id ref ref-dep ref-header replaced)
3783     ;; Enter this `header' in the `dependencies' table.
3784     (cond
3785      ((not id-dep)
3786       (setq header nil))
3787      ;; The first two cases do the normal part: enter a new `header'
3788      ;; in the `dependencies' table.
3789      ((not (boundp id-dep))
3790       (set id-dep (list header)))
3791      ((null (car (symbol-value id-dep)))
3792       (setcar (symbol-value id-dep) header))
3793
3794      ;; From here the `header' was already present in the
3795      ;; `dependencies' table.
3796      (force-new
3797       ;; Overrides an existing entry;
3798       ;; just set the header part of the entry.
3799       (setcar (symbol-value id-dep) header)
3800       (setq replaced t))
3801
3802      ;; Renames the existing `header' to a unique Message-ID.
3803      ((not gnus-summary-ignore-duplicates)
3804       ;; An article with this Message-ID has already been seen.
3805       ;; We rename the Message-ID.
3806       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3807            (list header))
3808       (mail-header-set-id header id))
3809
3810      ;; The last case ignores an existing entry, except it adds any
3811      ;; additional Xrefs (in case the two articles came from different
3812      ;; servers.
3813      ;; Also sets `header' to `nil' meaning that the `dependencies'
3814      ;; table was *not* modified.
3815      (t
3816       (mail-header-set-xref
3817        (car (symbol-value id-dep))
3818        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3819                    "")
3820                (or (mail-header-xref header) "")))
3821       (setq header nil)))
3822
3823     (when (and header (not replaced))
3824       ;; First check that we are not creating a References loop.
3825       (setq parent-id (gnus-parent-id (mail-header-references header)))
3826       (setq ref parent-id)
3827       (while (and ref
3828                   (setq ref-dep (intern-soft ref dependencies))
3829                   (boundp ref-dep)
3830                   (setq ref-header (car (symbol-value ref-dep))))
3831         (if (string= id ref)
3832             ;; Yuk!  This is a reference loop.  Make the article be a
3833             ;; root article.
3834             (progn
3835               (mail-header-set-references (car (symbol-value id-dep)) "none")
3836               (setq ref nil)
3837               (setq parent-id nil))
3838           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3839       (setq ref-dep (intern (or parent-id "none") dependencies))
3840       (if (boundp ref-dep)
3841           (setcdr (symbol-value ref-dep)
3842                   (nconc (cdr (symbol-value ref-dep))
3843                          (list (symbol-value id-dep))))
3844         (set ref-dep (list nil (symbol-value id-dep)))))
3845     header))
3846
3847 (defun gnus-extract-message-id-from-in-reply-to (string)
3848   (if (string-match "<[^>]+>" string)
3849       (substring string (match-beginning 0) (match-end 0))
3850     nil))
3851
3852 (defun gnus-build-sparse-threads ()
3853   (let ((headers gnus-newsgroup-headers)
3854         (mail-parse-charset gnus-newsgroup-charset)
3855         (gnus-summary-ignore-duplicates t)
3856         header references generation relations
3857         subject child end new-child date)
3858     ;; First we create an alist of generations/relations, where
3859     ;; generations is how much we trust the relation, and the relation
3860     ;; is parent/child.
3861     (gnus-message 7 "Making sparse threads...")
3862     (save-excursion
3863       (nnheader-set-temp-buffer " *gnus sparse threads*")
3864       (while (setq header (pop headers))
3865         (when (and (setq references (mail-header-references header))
3866                    (not (string= references "")))
3867           (insert references)
3868           (setq child (mail-header-id header)
3869                 subject (mail-header-subject header)
3870                 date (mail-header-date header)
3871                 generation 0)
3872           (while (search-backward ">" nil t)
3873             (setq end (1+ (point)))
3874             (when (search-backward "<" nil t)
3875               (setq new-child (buffer-substring (point) end))
3876               (push (list (incf generation)
3877                           child (setq child new-child)
3878                           subject date)
3879                     relations)))
3880           (when child
3881             (push (list (1+ generation) child nil subject) relations))
3882           (erase-buffer)))
3883       (kill-buffer (current-buffer)))
3884     ;; Sort over trustworthiness.
3885     (mapcar
3886      (lambda (relation)
3887        (when (gnus-dependencies-add-header
3888               (make-full-mail-header
3889                gnus-reffed-article-number
3890                (nth 3 relation) "" (or (nth 4 relation) "")
3891                (nth 1 relation)
3892                (or (nth 2 relation) "") 0 0 "")
3893               gnus-newsgroup-dependencies nil)
3894          (push gnus-reffed-article-number gnus-newsgroup-limit)
3895          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3896          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3897                gnus-newsgroup-reads)
3898          (decf gnus-reffed-article-number)))
3899      (sort relations 'car-less-than-car))
3900     (gnus-message 7 "Making sparse threads...done")))
3901
3902 (defun gnus-build-old-threads ()
3903   ;; Look at all the articles that refer back to old articles, and
3904   ;; fetch the headers for the articles that aren't there.  This will
3905   ;; build complete threads - if the roots haven't been expired by the
3906   ;; server, that is.
3907   (let ((mail-parse-charset gnus-newsgroup-charset)
3908         id heads)
3909     (mapatoms
3910      (lambda (refs)
3911        (when (not (car (symbol-value refs)))
3912          (setq heads (cdr (symbol-value refs)))
3913          (while heads
3914            (if (memq (mail-header-number (caar heads))
3915                      gnus-newsgroup-dormant)
3916                (setq heads (cdr heads))
3917              (setq id (symbol-name refs))
3918              (while (and (setq id (gnus-build-get-header id))
3919                          (not (car (gnus-id-to-thread id)))))
3920              (setq heads nil)))))
3921      gnus-newsgroup-dependencies)))
3922
3923 ;; This function has to be called with point after the article number
3924 ;; on the beginning of the line.
3925 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3926   (let ((eol (gnus-point-at-eol))
3927         (buffer (current-buffer))
3928         header references in-reply-to)
3929
3930     ;; overview: [num subject from date id refs chars lines misc]
3931     (unwind-protect
3932         (let (x)
3933           (narrow-to-region (point) eol)
3934           (unless (eobp)
3935             (forward-char))
3936
3937           (setq header
3938                 (make-full-mail-header
3939                  number                 ; number
3940                  (condition-case ()     ; subject
3941                      (funcall gnus-decode-encoded-word-function
3942                               (setq x (nnheader-nov-field)))
3943                    (error x))
3944                  (condition-case ()     ; from
3945                      (funcall gnus-decode-encoded-word-function
3946                               (setq x (nnheader-nov-field)))
3947                    (error x))
3948                  (nnheader-nov-field)   ; date
3949                  (nnheader-nov-read-message-id) ; id
3950                  (setq references (nnheader-nov-field)) ; refs
3951                  (nnheader-nov-read-integer) ; chars
3952                  (nnheader-nov-read-integer) ; lines
3953                  (unless (eobp)
3954                    (if (looking-at "Xref: ")
3955                        (goto-char (match-end 0)))
3956                    (nnheader-nov-field)) ; Xref
3957                  (nnheader-nov-parse-extra)))) ; extra
3958
3959       (widen))
3960
3961     (when (and (string= references "")
3962                (setq in-reply-to (mail-header-extra header))
3963                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3964       (mail-header-set-references
3965        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3966
3967     (when gnus-alter-header-function
3968       (funcall gnus-alter-header-function header))
3969     (gnus-dependencies-add-header header dependencies force-new)))
3970
3971 (defun gnus-build-get-header (id)
3972   "Look through the buffer of NOV lines and find the header to ID.
3973 Enter this line into the dependencies hash table, and return
3974 the id of the parent article (if any)."
3975   (let ((deps gnus-newsgroup-dependencies)
3976         found header)
3977     (prog1
3978         (save-excursion
3979           (set-buffer nntp-server-buffer)
3980           (let ((case-fold-search nil))
3981             (goto-char (point-min))
3982             (while (and (not found)
3983                         (search-forward id nil t))
3984               (beginning-of-line)
3985               (setq found (looking-at
3986                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3987                                    (regexp-quote id))))
3988               (or found (beginning-of-line 2)))
3989             (when found
3990               (beginning-of-line)
3991               (and
3992                (setq header (gnus-nov-parse-line
3993                              (read (current-buffer)) deps))
3994                (gnus-parent-id (mail-header-references header))))))
3995       (when header
3996         (let ((number (mail-header-number header)))
3997           (push number gnus-newsgroup-limit)
3998           (push header gnus-newsgroup-headers)
3999           (if (memq number gnus-newsgroup-unselected)
4000               (progn
4001                 (setq gnus-newsgroup-unreads
4002                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4003                                                number))
4004                 (setq gnus-newsgroup-unselected
4005                       (delq number gnus-newsgroup-unselected)))
4006             (push number gnus-newsgroup-ancient)))))))
4007
4008 (defun gnus-build-all-threads ()
4009   "Read all the headers."
4010   (let ((gnus-summary-ignore-duplicates t)
4011         (mail-parse-charset gnus-newsgroup-charset)
4012         (dependencies gnus-newsgroup-dependencies)
4013         header article)
4014     (save-excursion
4015       (set-buffer nntp-server-buffer)
4016       (let ((case-fold-search nil))
4017         (goto-char (point-min))
4018         (while (not (eobp))
4019           (ignore-errors
4020             (setq article (read (current-buffer))
4021                   header (gnus-nov-parse-line article dependencies)))
4022           (when header
4023             (save-excursion
4024               (set-buffer gnus-summary-buffer)
4025               (push header gnus-newsgroup-headers)
4026               (if (memq (setq article (mail-header-number header))
4027                         gnus-newsgroup-unselected)
4028                   (progn
4029                     (setq gnus-newsgroup-unreads
4030                           (gnus-add-to-sorted-list
4031                            gnus-newsgroup-unreads article))
4032                     (setq gnus-newsgroup-unselected
4033                           (delq article gnus-newsgroup-unselected)))
4034                 (push article gnus-newsgroup-ancient)))
4035             (forward-line 1)))))))
4036
4037 (defun gnus-summary-update-article-line (article header)
4038   "Update the line for ARTICLE using HEADERS."
4039   (let* ((id (mail-header-id header))
4040          (thread (gnus-id-to-thread id)))
4041     (unless thread
4042       (error "Article in no thread"))
4043     ;; Update the thread.
4044     (setcar thread header)
4045     (gnus-summary-goto-subject article)
4046     (let* ((datal (gnus-data-find-list article))
4047            (data (car datal))
4048            (buffer-read-only nil)
4049            (level (gnus-summary-thread-level)))
4050       (gnus-delete-line)
4051       (let ((inserted (- (point)
4052                          (progn
4053                            (gnus-summary-insert-line
4054                             header level nil 
4055                             (memq article gnus-newsgroup-undownloaded)
4056                             (gnus-article-mark article)
4057                             (memq article gnus-newsgroup-replied)
4058                             (memq article gnus-newsgroup-expirable)
4059                             ;; Only insert the Subject string when it's different
4060                             ;; from the previous Subject string.
4061                             (if (and
4062                                  gnus-show-threads
4063                                  (gnus-subject-equal
4064                                   (condition-case ()
4065                                       (mail-header-subject
4066                                        (gnus-data-header
4067                                         (cadr
4068                                          (gnus-data-find-list
4069                                           article
4070                                           (gnus-data-list t)))))
4071                                     ;; Error on the side of excessive subjects.
4072                                     (error ""))
4073                                   (mail-header-subject header)))
4074                                 ""
4075                               (mail-header-subject header))
4076                             nil (cdr (assq article gnus-newsgroup-scored))
4077                             (memq article gnus-newsgroup-processable))
4078                            (point)))))
4079         (when (cdr datal)
4080           (gnus-data-update-list
4081            (cdr datal) 
4082            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4083
4084 (defun gnus-summary-update-article (article &optional iheader)
4085   "Update ARTICLE in the summary buffer."
4086   (set-buffer gnus-summary-buffer)
4087   (let* ((header (gnus-summary-article-header article))
4088          (id (mail-header-id header))
4089          (data (gnus-data-find article))
4090          (thread (gnus-id-to-thread id))
4091          (references (mail-header-references header))
4092          (parent
4093           (gnus-id-to-thread
4094            (or (gnus-parent-id
4095                 (when (and references
4096                            (not (equal "" references)))
4097                   references))
4098                "none")))
4099          (buffer-read-only nil)
4100          (old (car thread)))
4101     (when thread
4102       (unless iheader
4103         (setcar thread nil)
4104         (when parent
4105           (delq thread parent)))
4106       (if (gnus-summary-insert-subject id header)
4107           ;; Set the (possibly) new article number in the data structure.
4108           (gnus-data-set-number data (gnus-id-to-article id))
4109         (setcar thread old)
4110         nil))))
4111
4112 (defun gnus-rebuild-thread (id &optional line)
4113   "Rebuild the thread containing ID.
4114 If LINE, insert the rebuilt thread starting on line LINE."
4115   (let ((buffer-read-only nil)
4116         old-pos current thread data)
4117     (if (not gnus-show-threads)
4118         (setq thread (list (car (gnus-id-to-thread id))))
4119       ;; Get the thread this article is part of.
4120       (setq thread (gnus-remove-thread id)))
4121     (setq old-pos (gnus-point-at-bol))
4122     (setq current (save-excursion
4123                     (and (re-search-backward "[\r\n]" nil t)
4124                          (gnus-summary-article-number))))
4125     ;; If this is a gathered thread, we have to go some re-gathering.
4126     (when (stringp (car thread))
4127       (let ((subject (car thread))
4128             roots thr)
4129         (setq thread (cdr thread))
4130         (while thread
4131           (unless (memq (setq thr (gnus-id-to-thread
4132                                    (gnus-root-id
4133                                     (mail-header-id (caar thread)))))
4134                         roots)
4135             (push thr roots))
4136           (setq thread (cdr thread)))
4137         ;; We now have all (unique) roots.
4138         (if (= (length roots) 1)
4139             ;; All the loose roots are now one solid root.
4140             (setq thread (car roots))
4141           (setq thread (cons subject (gnus-sort-threads roots))))))
4142     (let (threads)
4143       ;; We then insert this thread into the summary buffer.
4144       (when line
4145         (goto-char (point-min))
4146         (forward-line (1- line)))
4147       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4148         (if gnus-show-threads
4149             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4150           (gnus-summary-prepare-unthreaded thread))
4151         (setq data (nreverse gnus-newsgroup-data))
4152         (setq threads gnus-newsgroup-threads))
4153       ;; We splice the new data into the data structure.
4154       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4155       ;;!!! then we want to insert at the beginning of the buffer.
4156       ;;!!! That happens to be true with Gnus now, but that may
4157       ;;!!! change in the future.  Perhaps.
4158       (gnus-data-enter-list
4159        (if line nil current) data (- (point) old-pos))
4160       (setq gnus-newsgroup-threads
4161             (nconc threads gnus-newsgroup-threads))
4162       (gnus-data-compute-positions))))
4163
4164 (defun gnus-number-to-header (number)
4165   "Return the header for article NUMBER."
4166   (let ((headers gnus-newsgroup-headers))
4167     (while (and headers
4168                 (not (= number (mail-header-number (car headers)))))
4169       (pop headers))
4170     (when headers
4171       (car headers))))
4172
4173 (defun gnus-parent-headers (in-headers &optional generation)
4174   "Return the headers of the GENERATIONeth parent of HEADERS."
4175   (unless generation
4176     (setq generation 1))
4177   (let ((parent t)
4178         (headers in-headers)
4179         references)
4180     (while (and parent
4181                 (not (zerop generation))
4182                 (setq references (mail-header-references headers)))
4183       (setq headers (if (and references
4184                              (setq parent (gnus-parent-id references)))
4185                         (car (gnus-id-to-thread parent))
4186                       nil))
4187       (decf generation))
4188     (and (not (eq headers in-headers))
4189          headers)))
4190
4191 (defun gnus-id-to-thread (id)
4192   "Return the (sub-)thread where ID appears."
4193   (gnus-gethash id gnus-newsgroup-dependencies))
4194
4195 (defun gnus-id-to-article (id)
4196   "Return the article number of ID."
4197   (let ((thread (gnus-id-to-thread id)))
4198     (when (and thread
4199                (car thread))
4200       (mail-header-number (car thread)))))
4201
4202 (defun gnus-id-to-header (id)
4203   "Return the article headers of ID."
4204   (car (gnus-id-to-thread id)))
4205
4206 (defun gnus-article-displayed-root-p (article)
4207   "Say whether ARTICLE is a root(ish) article."
4208   (let ((level (gnus-summary-thread-level article))
4209         (refs (mail-header-references  (gnus-summary-article-header article)))
4210         particle)
4211     (cond
4212      ((null level) nil)
4213      ((zerop level) t)
4214      ((null refs) t)
4215      ((null (gnus-parent-id refs)) t)
4216      ((and (= 1 level)
4217            (null (setq particle (gnus-id-to-article
4218                                  (gnus-parent-id refs))))
4219            (null (gnus-summary-thread-level particle)))))))
4220
4221 (defun gnus-root-id (id)
4222   "Return the id of the root of the thread where ID appears."
4223   (let (last-id prev)
4224     (while (and id (setq prev (car (gnus-id-to-thread id))))
4225       (setq last-id id
4226             id (gnus-parent-id (mail-header-references prev))))
4227     last-id))
4228
4229 (defun gnus-articles-in-thread (thread)
4230   "Return the list of articles in THREAD."
4231   (cons (mail-header-number (car thread))
4232         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4233
4234 (defun gnus-remove-thread (id &optional dont-remove)
4235   "Remove the thread that has ID in it."
4236   (let (headers thread last-id)
4237     ;; First go up in this thread until we find the root.
4238     (setq last-id (gnus-root-id id)
4239           headers (message-flatten-list (gnus-id-to-thread last-id)))
4240     ;; We have now found the real root of this thread.  It might have
4241     ;; been gathered into some loose thread, so we have to search
4242     ;; through the threads to find the thread we wanted.
4243     (let ((threads gnus-newsgroup-threads)
4244           sub)
4245       (while threads
4246         (setq sub (car threads))
4247         (if (stringp (car sub))
4248             ;; This is a gathered thread, so we look at the roots
4249             ;; below it to find whether this article is in this
4250             ;; gathered root.
4251             (progn
4252               (setq sub (cdr sub))
4253               (while sub
4254                 (when (member (caar sub) headers)
4255                   (setq thread (car threads)
4256                         threads nil
4257                         sub nil))
4258                 (setq sub (cdr sub))))
4259           ;; It's an ordinary thread, so we check it.
4260           (when (eq (car sub) (car headers))
4261             (setq thread sub
4262                   threads nil)))
4263         (setq threads (cdr threads)))
4264       ;; If this article is in no thread, then it's a root.
4265       (if thread
4266           (unless dont-remove
4267             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4268         (setq thread (gnus-id-to-thread last-id)))
4269       (when thread
4270         (prog1
4271             thread                      ; We return this thread.
4272           (unless dont-remove
4273             (if (stringp (car thread))
4274                 (progn
4275                   ;; If we use dummy roots, then we have to remove the
4276                   ;; dummy root as well.
4277                   (when (eq gnus-summary-make-false-root 'dummy)
4278                     ;; We go to the dummy root by going to
4279                     ;; the first sub-"thread", and then one line up.
4280                     (gnus-summary-goto-article
4281                      (mail-header-number (caadr thread)))
4282                     (forward-line -1)
4283                     (gnus-delete-line)
4284                     (gnus-data-compute-positions))
4285                   (setq thread (cdr thread))
4286                   (while thread
4287                     (gnus-remove-thread-1 (car thread))
4288                     (setq thread (cdr thread))))
4289               (gnus-remove-thread-1 thread))))))))
4290
4291 (defun gnus-remove-thread-1 (thread)
4292   "Remove the thread THREAD recursively."
4293   (let ((number (mail-header-number (pop thread)))
4294         d)
4295     (setq thread (reverse thread))
4296     (while thread
4297       (gnus-remove-thread-1 (pop thread)))
4298     (when (setq d (gnus-data-find number))
4299       (goto-char (gnus-data-pos d))
4300       (gnus-summary-show-thread)
4301       (gnus-data-remove
4302        number
4303        (- (gnus-point-at-bol)
4304           (prog1
4305               (1+ (gnus-point-at-eol))
4306             (gnus-delete-line)))))))
4307
4308 (defun gnus-sort-threads-1 (threads func)
4309   (sort (mapcar (lambda (thread)
4310                   (cons (car thread)
4311                         (and (cdr thread)
4312                              (gnus-sort-threads-1 (cdr thread) func))))
4313                 threads) func))
4314
4315 (defun gnus-sort-threads (threads)
4316   "Sort THREADS."
4317   (if (not gnus-thread-sort-functions)
4318       threads
4319     (gnus-message 8 "Sorting threads...")
4320     (let ((max-lisp-eval-depth 5000))
4321       (prog1 (gnus-sort-threads-1
4322          threads
4323          (gnus-make-sort-function gnus-thread-sort-functions))
4324         (gnus-message 8 "Sorting threads...done")))))
4325
4326 (defun gnus-sort-articles (articles)
4327   "Sort ARTICLES."
4328   (when gnus-article-sort-functions
4329     (gnus-message 7 "Sorting articles...")
4330     (prog1
4331         (setq gnus-newsgroup-headers
4332               (sort articles (gnus-make-sort-function
4333                               gnus-article-sort-functions)))
4334       (gnus-message 7 "Sorting articles...done"))))
4335
4336 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4337 (defmacro gnus-thread-header (thread)
4338   "Return header of first article in THREAD.
4339 Note that THREAD must never, ever be anything else than a variable -
4340 using some other form will lead to serious barfage."
4341   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4342   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4343   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4344         (vector thread) 2))
4345
4346 (defsubst gnus-article-sort-by-number (h1 h2)
4347   "Sort articles by article number."
4348   (< (mail-header-number h1)
4349      (mail-header-number h2)))
4350
4351 (defun gnus-thread-sort-by-number (h1 h2)
4352   "Sort threads by root article number."
4353   (gnus-article-sort-by-number
4354    (gnus-thread-header h1) (gnus-thread-header h2)))
4355
4356 (defsubst gnus-article-sort-by-random (h1 h2)
4357   "Sort articles by article number."
4358   (zerop (random 2)))
4359
4360 (defun gnus-thread-sort-by-random (h1 h2)
4361   "Sort threads by root article number."
4362   (gnus-article-sort-by-random
4363    (gnus-thread-header h1) (gnus-thread-header h2)))
4364
4365 (defsubst gnus-article-sort-by-lines (h1 h2)
4366   "Sort articles by article Lines header."
4367   (< (mail-header-lines h1)
4368      (mail-header-lines h2)))
4369
4370 (defun gnus-thread-sort-by-lines (h1 h2)
4371   "Sort threads by root article Lines header."
4372   (gnus-article-sort-by-lines
4373    (gnus-thread-header h1) (gnus-thread-header h2)))
4374
4375 (defsubst gnus-article-sort-by-chars (h1 h2)
4376   "Sort articles by octet length."
4377   (< (mail-header-chars h1)
4378      (mail-header-chars h2)))
4379
4380 (defun gnus-thread-sort-by-chars (h1 h2)
4381   "Sort threads by root article octet length."
4382   (gnus-article-sort-by-chars
4383    (gnus-thread-header h1) (gnus-thread-header h2)))
4384
4385 (defsubst gnus-article-sort-by-author (h1 h2)
4386   "Sort articles by root author."
4387   (string-lessp
4388    (let ((extract (funcall
4389                    gnus-extract-address-components
4390                    (mail-header-from h1))))
4391      (or (car extract) (cadr extract) ""))
4392    (let ((extract (funcall
4393                    gnus-extract-address-components
4394                    (mail-header-from h2))))
4395      (or (car extract) (cadr extract) ""))))
4396
4397 (defun gnus-thread-sort-by-author (h1 h2)
4398   "Sort threads by root author."
4399   (gnus-article-sort-by-author
4400    (gnus-thread-header h1)  (gnus-thread-header h2)))
4401
4402 (defsubst gnus-article-sort-by-subject (h1 h2)
4403   "Sort articles by root subject."
4404   (string-lessp
4405    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4406    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4407
4408 (defun gnus-thread-sort-by-subject (h1 h2)
4409   "Sort threads by root subject."
4410   (gnus-article-sort-by-subject
4411    (gnus-thread-header h1) (gnus-thread-header h2)))
4412
4413 (defsubst gnus-article-sort-by-date (h1 h2)
4414   "Sort articles by root article date."
4415   (time-less-p
4416    (gnus-date-get-time (mail-header-date h1))
4417    (gnus-date-get-time (mail-header-date h2))))
4418
4419 (defun gnus-thread-sort-by-date (h1 h2)
4420   "Sort threads by root article date."
4421   (gnus-article-sort-by-date
4422    (gnus-thread-header h1) (gnus-thread-header h2)))
4423
4424 (defsubst gnus-article-sort-by-score (h1 h2)
4425   "Sort articles by root article score.
4426 Unscored articles will be counted as having a score of zero."
4427   (> (or (cdr (assq (mail-header-number h1)
4428                     gnus-newsgroup-scored))
4429          gnus-summary-default-score 0)
4430      (or (cdr (assq (mail-header-number h2)
4431                     gnus-newsgroup-scored))
4432          gnus-summary-default-score 0)))
4433
4434 (defun gnus-thread-sort-by-score (h1 h2)
4435   "Sort threads by root article score."
4436   (gnus-article-sort-by-score
4437    (gnus-thread-header h1) (gnus-thread-header h2)))
4438
4439 (defun gnus-thread-sort-by-total-score (h1 h2)
4440   "Sort threads by the sum of all scores in the thread.
4441 Unscored articles will be counted as having a score of zero."
4442   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4443
4444 (defun gnus-thread-total-score (thread)
4445   ;; This function find the total score of THREAD.
4446   (cond
4447    ((null thread)
4448     0)
4449    ((consp thread)
4450     (if (stringp (car thread))
4451         (apply gnus-thread-score-function 0
4452                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4453       (gnus-thread-total-score-1 thread)))
4454    (t
4455     (gnus-thread-total-score-1 (list thread)))))
4456
4457 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4458   "Sort threads such that the thread with the most recently arrived article comes first."
4459   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4460
4461 (defun gnus-thread-highest-number (thread)
4462   "Return the highest article number in THREAD."
4463   (apply 'max (mapcar (lambda (header)
4464                         (mail-header-number header))
4465                       (message-flatten-list thread))))
4466
4467 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4468   "Sort threads such that the thread with the most recently dated article comes first."
4469   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4470
4471 (defun gnus-thread-latest-date (thread)
4472   "Return the highest article date in THREAD."
4473   (let ((previous-time 0))
4474     (apply 'max
4475            (mapcar
4476             (lambda (header)
4477               (setq previous-time
4478                     (time-to-seconds
4479                      (condition-case ()
4480                          (mail-header-parse-date (mail-header-date header))
4481                        (error previous-time)))))
4482             (sort
4483              (message-flatten-list thread)
4484              (lambda (h1 h2)
4485                (< (mail-header-number h1)
4486                   (mail-header-number h2))))))))
4487
4488 (defun gnus-thread-total-score-1 (root)
4489   ;; This function find the total score of the thread below ROOT.
4490   (setq root (car root))
4491   (apply gnus-thread-score-function
4492          (or (append
4493               (mapcar 'gnus-thread-total-score
4494                       (cdr (gnus-id-to-thread (mail-header-id root))))
4495               (when (> (mail-header-number root) 0)
4496                 (list (or (cdr (assq (mail-header-number root)
4497                                      gnus-newsgroup-scored))
4498                           gnus-summary-default-score 0))))
4499              (list gnus-summary-default-score)
4500              '(0))))
4501
4502 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4503 (defvar gnus-tmp-prev-subject nil)
4504 (defvar gnus-tmp-false-parent nil)
4505 (defvar gnus-tmp-root-expunged nil)
4506 (defvar gnus-tmp-dummy-line nil)
4507
4508 (eval-when-compile (defvar gnus-tmp-header))
4509 (defun gnus-extra-header (type &optional header)
4510   "Return the extra header of TYPE."
4511   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4512       ""))
4513
4514 (defvar gnus-tmp-thread-tree-header-string "")
4515
4516 (defcustom gnus-sum-thread-tree-root "> "
4517   "With %B spec, used for the root of a thread.
4518 If nil, use subject instead."
4519   :type 'string
4520   :group 'gnus-thread)
4521 (defcustom gnus-sum-thread-tree-single-indent ""
4522   "With %B spec, used for a thread with just one message.
4523 If nil, use subject instead."
4524   :type 'string
4525   :group 'gnus-thread)
4526 (defcustom gnus-sum-thread-tree-vertical "| "
4527   "With %B spec, used for drawing a vertical line."
4528   :type 'string
4529   :group 'gnus-thread)
4530 (defcustom gnus-sum-thread-tree-indent "  "
4531   "With %B spec, used for indenting."
4532   :type 'string
4533   :group 'gnus-thread)
4534 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4535   "With %B spec, used for a leaf with brothers."
4536   :type 'string
4537   :group 'gnus-thread)
4538 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4539   "With %B spec, used for a leaf without brothers."
4540   :type 'string
4541   :group 'gnus-thread)
4542
4543 (defun gnus-summary-prepare-threads (threads)
4544   "Prepare summary buffer from THREADS and indentation LEVEL.
4545 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4546 or a straight list of headers."
4547   (gnus-message 7 "Generating summary...")
4548
4549   (setq gnus-newsgroup-threads threads)
4550   (beginning-of-line)
4551
4552   (let ((gnus-tmp-level 0)
4553         (default-score (or gnus-summary-default-score 0))
4554         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4555         (building-line-count gnus-summary-display-while-building)
4556         (building-count (integerp gnus-summary-display-while-building))
4557         thread number subject stack state gnus-tmp-gathered beg-match
4558         new-roots gnus-tmp-new-adopts thread-end simp-subject
4559         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4560         gnus-tmp-replied gnus-tmp-subject-or-nil
4561         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4562         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4563         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4564         tree-stack)
4565
4566     (setq gnus-tmp-prev-subject nil
4567           gnus-tmp-thread-tree-header-string "")
4568
4569     (if (vectorp (car threads))
4570         ;; If this is a straight (sic) list of headers, then a
4571         ;; threaded summary display isn't required, so we just create
4572         ;; an unthreaded one.
4573         (gnus-summary-prepare-unthreaded threads)
4574
4575       ;; Do the threaded display.
4576
4577       (if gnus-summary-display-while-building
4578           (switch-to-buffer (buffer-name)))
4579       (while (or threads stack gnus-tmp-new-adopts new-roots)
4580
4581         (if (and (= gnus-tmp-level 0)
4582                  (or (not stack)
4583                      (= (caar stack) 0))
4584                  (not gnus-tmp-false-parent)
4585                  (or gnus-tmp-new-adopts new-roots))
4586             (if gnus-tmp-new-adopts
4587                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4588                       thread (list (car gnus-tmp-new-adopts))
4589                       gnus-tmp-header (caar thread)
4590                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4591               (when new-roots
4592                 (setq thread (list (car new-roots))
4593                       gnus-tmp-header (caar thread)
4594                       new-roots (cdr new-roots))))
4595
4596           (if threads
4597               ;; If there are some threads, we do them before the
4598               ;; threads on the stack.
4599               (setq thread threads
4600                     gnus-tmp-header (caar thread))
4601             ;; There were no current threads, so we pop something off
4602             ;; the stack.
4603             (setq state (car stack)
4604                   gnus-tmp-level (car state)
4605                   tree-stack (cadr state)
4606                   thread (caddr state)
4607                   stack (cdr stack)
4608                   gnus-tmp-header (caar thread))))
4609
4610         (setq gnus-tmp-false-parent nil)
4611         (setq gnus-tmp-root-expunged nil)
4612         (setq thread-end nil)
4613
4614         (if (stringp gnus-tmp-header)
4615             ;; The header is a dummy root.
4616             (cond
4617              ((eq gnus-summary-make-false-root 'adopt)
4618               ;; We let the first article adopt the rest.
4619               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4620                                                (cddar thread)))
4621               (setq gnus-tmp-gathered
4622                     (nconc (mapcar
4623                             (lambda (h) (mail-header-number (car h)))
4624                             (cddar thread))
4625                            gnus-tmp-gathered))
4626               (setq thread (cons (list (caar thread)
4627                                        (cadar thread))
4628                                  (cdr thread)))
4629               (setq gnus-tmp-level -1
4630                     gnus-tmp-false-parent t))
4631              ((eq gnus-summary-make-false-root 'empty)
4632               ;; We print adopted articles with empty subject fields.
4633               (setq gnus-tmp-gathered
4634                     (nconc (mapcar
4635                             (lambda (h) (mail-header-number (car h)))
4636                             (cddar thread))
4637                            gnus-tmp-gathered))
4638               (setq gnus-tmp-level -1))
4639              ((eq gnus-summary-make-false-root 'dummy)
4640               ;; We remember that we probably want to output a dummy
4641               ;; root.
4642               (setq gnus-tmp-dummy-line gnus-tmp-header)
4643               (setq gnus-tmp-prev-subject gnus-tmp-header))
4644              (t
4645               ;; We do not make a root for the gathered
4646               ;; sub-threads at all.
4647               (setq gnus-tmp-level -1)))
4648
4649           (setq number (mail-header-number gnus-tmp-header)
4650                 subject (mail-header-subject gnus-tmp-header)
4651                 simp-subject (gnus-simplify-subject-fully subject))
4652
4653           (cond
4654            ;; If the thread has changed subject, we might want to make
4655            ;; this subthread into a root.
4656            ((and (null gnus-thread-ignore-subject)
4657                  (not (zerop gnus-tmp-level))
4658                  gnus-tmp-prev-subject
4659                  (not (string= gnus-tmp-prev-subject simp-subject)))
4660             (setq new-roots (nconc new-roots (list (car thread)))
4661                   thread-end t
4662                   gnus-tmp-header nil))
4663            ;; If the article lies outside the current limit,
4664            ;; then we do not display it.
4665            ((not (memq number gnus-newsgroup-limit))
4666             (setq gnus-tmp-gathered
4667                   (nconc (mapcar
4668                           (lambda (h) (mail-header-number (car h)))
4669                           (cdar thread))
4670                          gnus-tmp-gathered))
4671             (setq gnus-tmp-new-adopts (if (cdar thread)
4672                                           (append gnus-tmp-new-adopts
4673                                                   (cdar thread))
4674                                         gnus-tmp-new-adopts)
4675                   thread-end t
4676                   gnus-tmp-header nil)
4677             (when (zerop gnus-tmp-level)
4678               (setq gnus-tmp-root-expunged t)))
4679            ;; Perhaps this article is to be marked as read?
4680            ((and gnus-summary-mark-below
4681                  (< (or (cdr (assq number gnus-newsgroup-scored))
4682                         default-score)
4683                     gnus-summary-mark-below)
4684                  ;; Don't touch sparse articles.
4685                  (not (gnus-summary-article-sparse-p number))
4686                  (not (gnus-summary-article-ancient-p number)))
4687             (setq gnus-newsgroup-unreads
4688                   (delq number gnus-newsgroup-unreads))
4689             (if gnus-newsgroup-auto-expire
4690                 (setq gnus-newsgroup-expirable
4691                       (gnus-add-to-sorted-list
4692                        gnus-newsgroup-expirable number))
4693               (push (cons number gnus-low-score-mark)
4694                     gnus-newsgroup-reads))))
4695
4696           (when gnus-tmp-header
4697             ;; We may have an old dummy line to output before this
4698             ;; article.
4699             (when (and gnus-tmp-dummy-line
4700                        (gnus-subject-equal
4701                         gnus-tmp-dummy-line
4702                         (mail-header-subject gnus-tmp-header)))
4703               (gnus-summary-insert-dummy-line
4704                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4705               (setq gnus-tmp-dummy-line nil))
4706
4707             ;; Compute the mark.
4708             (setq gnus-tmp-unread (gnus-article-mark number))
4709
4710             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4711                                   gnus-tmp-header gnus-tmp-level)
4712                   gnus-newsgroup-data)
4713
4714             ;; Actually insert the line.
4715             (setq
4716              gnus-tmp-subject-or-nil
4717              (cond
4718               ((and gnus-thread-ignore-subject
4719                     gnus-tmp-prev-subject
4720                     (not (string= gnus-tmp-prev-subject simp-subject)))
4721                subject)
4722               ((zerop gnus-tmp-level)
4723                (if (and (eq gnus-summary-make-false-root 'empty)
4724                         (memq number gnus-tmp-gathered)
4725                         gnus-tmp-prev-subject
4726                         (string= gnus-tmp-prev-subject simp-subject))
4727                    gnus-summary-same-subject
4728                  subject))
4729               (t gnus-summary-same-subject)))
4730             (if (and (eq gnus-summary-make-false-root 'adopt)
4731                      (= gnus-tmp-level 1)
4732                      (memq number gnus-tmp-gathered))
4733                 (setq gnus-tmp-opening-bracket ?\<
4734                       gnus-tmp-closing-bracket ?\>)
4735               (setq gnus-tmp-opening-bracket ?\[
4736                     gnus-tmp-closing-bracket ?\]))
4737             (setq
4738              gnus-tmp-indentation
4739              (aref gnus-thread-indent-array gnus-tmp-level)
4740              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4741              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4742                                 gnus-summary-default-score 0)
4743              gnus-tmp-score-char
4744              (if (or (null gnus-summary-default-score)
4745                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4746                          gnus-summary-zcore-fuzz))
4747                  ?                      ;Whitespace
4748                (if (< gnus-tmp-score gnus-summary-default-score)
4749                    gnus-score-below-mark gnus-score-over-mark))
4750              gnus-tmp-replied
4751              (cond ((memq number gnus-newsgroup-processable)
4752                     gnus-process-mark)
4753                    ((memq number gnus-newsgroup-cached)
4754                     gnus-cached-mark)
4755                    ((memq number gnus-newsgroup-replied)
4756                     gnus-replied-mark)
4757                    ((memq number gnus-newsgroup-forwarded)
4758                     gnus-forwarded-mark)
4759                    ((memq number gnus-newsgroup-saved)
4760                     gnus-saved-mark)
4761                    ((memq number gnus-newsgroup-recent)
4762                     gnus-recent-mark)
4763                    ((memq number gnus-newsgroup-unseen)
4764                     gnus-unseen-mark)
4765                    (t gnus-no-mark))
4766              gnus-tmp-downloaded
4767              (cond ((memq number gnus-newsgroup-undownloaded) 
4768                     gnus-undownloaded-mark)
4769                    (gnus-newsgroup-agentized
4770                     gnus-downloaded-mark)
4771                    (t
4772                     gnus-no-mark))
4773              gnus-tmp-from (mail-header-from gnus-tmp-header)
4774              gnus-tmp-name
4775              (cond
4776               ((string-match "<[^>]+> *$" gnus-tmp-from)
4777                (setq beg-match (match-beginning 0))
4778                (or (and (string-match "^\".+\"" gnus-tmp-from)
4779                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4780                    (substring gnus-tmp-from 0 beg-match)))
4781               ((string-match "(.+)" gnus-tmp-from)
4782                (substring gnus-tmp-from
4783                           (1+ (match-beginning 0)) (1- (match-end 0))))
4784               (t gnus-tmp-from))
4785              gnus-tmp-thread-tree-header-string
4786              (cond
4787               ((not gnus-show-threads) "")
4788               ((zerop gnus-tmp-level)
4789                (if (cdar thread)
4790                    (or gnus-sum-thread-tree-root subject)
4791                  (or gnus-sum-thread-tree-single-indent subject)))
4792               (t
4793                (concat (apply 'concat
4794                               (mapcar (lambda (item)
4795                                         (if (= item 1)
4796                                             gnus-sum-thread-tree-vertical
4797                                           gnus-sum-thread-tree-indent))
4798                                       (cdr (reverse tree-stack))))
4799                        (if (nth 1 thread)
4800                            gnus-sum-thread-tree-leaf-with-other
4801                          gnus-sum-thread-tree-single-leaf)))))
4802             (when (string= gnus-tmp-name "")
4803               (setq gnus-tmp-name gnus-tmp-from))
4804             (unless (numberp gnus-tmp-lines)
4805               (setq gnus-tmp-lines -1))
4806             (if (= gnus-tmp-lines -1)
4807                 (setq gnus-tmp-lines "?")
4808               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4809               (gnus-put-text-property
4810              (point)
4811              (progn (eval gnus-summary-line-format-spec) (point))
4812                'gnus-number number)
4813             (when gnus-visual-p
4814               (forward-line -1)
4815               (gnus-run-hooks 'gnus-summary-update-hook)
4816               (forward-line 1))
4817
4818             (setq gnus-tmp-prev-subject simp-subject)))
4819
4820         (when (nth 1 thread)
4821           (push (list (max 0 gnus-tmp-level)
4822                       (copy-list tree-stack)
4823                       (nthcdr 1 thread))
4824                 stack))
4825         (push (if (nth 1 thread) 1 0) tree-stack)
4826         (incf gnus-tmp-level)
4827         (setq threads (if thread-end nil (cdar thread)))
4828         (if gnus-summary-display-while-building
4829             (if building-count
4830                 (progn
4831                   ;; use a set frequency
4832                   (setq building-line-count (1- building-line-count))
4833                   (when (= building-line-count 0)
4834                     (sit-for 0)
4835                     (setq building-line-count
4836                           gnus-summary-display-while-building)))
4837               ;; always
4838               (sit-for 0)))
4839         (unless threads
4840           (setq gnus-tmp-level 0)))))
4841   (gnus-message 7 "Generating summary...done"))
4842
4843 (defun gnus-summary-prepare-unthreaded (headers)
4844   "Generate an unthreaded summary buffer based on HEADERS."
4845   (let (header number mark)
4846
4847     (beginning-of-line)
4848
4849     (while headers
4850       ;; We may have to root out some bad articles...
4851       (when (memq (setq number (mail-header-number
4852                                 (setq header (pop headers))))
4853                   gnus-newsgroup-limit)
4854         ;; Mark article as read when it has a low score.
4855         (when (and gnus-summary-mark-below
4856                    (< (or (cdr (assq number gnus-newsgroup-scored))
4857                           gnus-summary-default-score 0)
4858                       gnus-summary-mark-below)
4859                    (not (gnus-summary-article-ancient-p number)))
4860           (setq gnus-newsgroup-unreads
4861                 (delq number gnus-newsgroup-unreads))
4862           (if gnus-newsgroup-auto-expire
4863               (push number gnus-newsgroup-expirable)
4864             (push (cons number gnus-low-score-mark)
4865                   gnus-newsgroup-reads)))
4866
4867         (setq mark (gnus-article-mark number))
4868         (push (gnus-data-make number mark (1+ (point)) header 0)
4869               gnus-newsgroup-data)
4870         (gnus-summary-insert-line
4871          header 0 number
4872          (memq number gnus-newsgroup-undownloaded)
4873          mark (memq number gnus-newsgroup-replied)
4874          (memq number gnus-newsgroup-expirable)
4875          (mail-header-subject header) nil
4876          (cdr (assq number gnus-newsgroup-scored))
4877          (memq number gnus-newsgroup-processable))))))
4878
4879 (defun gnus-summary-remove-list-identifiers ()
4880   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4881   (let ((regexp (if (consp gnus-list-identifiers)
4882                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4883                   gnus-list-identifiers))
4884         changed subject)
4885     (when regexp
4886       (dolist (header gnus-newsgroup-headers)
4887         (setq subject (mail-header-subject header)
4888               changed nil)
4889         (while (string-match
4890                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4891                 subject)
4892           (setq subject
4893                 (concat (substring subject 0 (match-beginning 2))
4894                         (substring subject (match-end 0)))
4895                 changed t))
4896         (when (and changed
4897                    (string-match
4898                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4899           (setq subject
4900                 (concat (substring subject 0 (match-beginning 1))
4901                         (substring subject (match-end 1)))))
4902         (when changed
4903           (mail-header-set-subject header subject))))))
4904
4905 (defun gnus-fetch-headers (articles)
4906   "Fetch headers of ARTICLES."
4907   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4908     (gnus-message 5 "Fetching headers for %s..." name)
4909     (prog1
4910         (if (eq 'nov
4911                 (setq gnus-headers-retrieved-by
4912                       (gnus-retrieve-headers
4913                        articles gnus-newsgroup-name
4914                        ;; We might want to fetch old headers, but
4915                        ;; not if there is only 1 article.
4916                        (and (or (and
4917                                  (not (eq gnus-fetch-old-headers 'some))
4918                                  (not (numberp gnus-fetch-old-headers)))
4919                                 (> (length articles) 1))
4920                             gnus-fetch-old-headers))))
4921             (gnus-get-newsgroup-headers-xover
4922              articles nil nil gnus-newsgroup-name t)
4923           (gnus-get-newsgroup-headers))
4924       (gnus-message 5 "Fetching headers for %s...done" name))))
4925
4926 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4927   "Select newsgroup GROUP.
4928 If READ-ALL is non-nil, all articles in the group are selected.
4929 If SELECT-ARTICLES, only select those articles from GROUP."
4930   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4931          ;;!!! Dirty hack; should be removed.
4932          (gnus-summary-ignore-duplicates
4933           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4934               t
4935             gnus-summary-ignore-duplicates))
4936          (info (nth 2 entry))
4937          articles fetched-articles cached)
4938
4939     (unless (gnus-check-server
4940              (set (make-local-variable 'gnus-current-select-method)
4941                   (gnus-find-method-for-group group)))
4942       (error "Couldn't open server"))
4943
4944     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4945         (gnus-activate-group group)     ; Or we can activate it...
4946         (progn                          ; Or we bug out.
4947           (when (equal major-mode 'gnus-summary-mode)
4948             (gnus-kill-buffer (current-buffer)))
4949           (error "Couldn't activate group %s: %s"
4950                  group (gnus-status-message group))))
4951
4952     (unless (gnus-request-group group t)
4953       (when (equal major-mode 'gnus-summary-mode)
4954         (gnus-kill-buffer (current-buffer)))
4955       (error "Couldn't request group %s: %s"
4956              group (gnus-status-message group)))
4957
4958     (setq gnus-newsgroup-name group
4959           gnus-newsgroup-unselected nil
4960           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4961
4962     (let ((display (gnus-group-find-parameter group 'display)))
4963       (setq gnus-newsgroup-display
4964             (cond
4965              ((not (zerop (or (car-safe read-all) 0)))
4966               ;; The user entered the group with C-u SPC/RET, let's show
4967               ;; all articles.
4968               'gnus-not-ignore)
4969              ((eq display 'all)
4970               'gnus-not-ignore)
4971              ((arrayp display)
4972               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4973              ((numberp display)
4974               ;; The following is probably the "correct" solution, but
4975               ;; it makes Gnus fetch all headers and then limit the
4976               ;; articles (which is slow), so instead we hack the
4977               ;; select-articles parameter instead. -- Simon Josefsson
4978               ;; <jas@kth.se>
4979               ;;
4980               ;; (gnus-byte-compile
4981               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4982               ;;                         display)))))
4983               (setq select-articles
4984                     (gnus-uncompress-range
4985                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4986                              (if (> tmp 0)
4987                                  tmp
4988                                1))
4989                            (cdr (gnus-active group)))))
4990               nil)
4991              (t
4992               nil))))
4993
4994     (gnus-summary-setup-default-charset)
4995
4996     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4997     (when (gnus-virtual-group-p group)
4998       (setq cached gnus-newsgroup-cached))
4999
5000     (setq gnus-newsgroup-unreads
5001           (gnus-sorted-ndifference
5002            (gnus-sorted-ndifference gnus-newsgroup-unreads
5003                                     gnus-newsgroup-marked)
5004            gnus-newsgroup-dormant))
5005
5006     (setq gnus-newsgroup-processable nil)
5007
5008     (gnus-update-read-articles group gnus-newsgroup-unreads)
5009
5010     ;; Adjust and set lists of article marks.
5011     (when info
5012       (gnus-adjust-marked-articles info))
5013     (if (setq articles select-articles)
5014         (setq gnus-newsgroup-unselected
5015               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5016       (setq articles (gnus-articles-to-read group read-all)))
5017
5018     (cond
5019      ((null articles)
5020       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5021       'quit)
5022      ((eq articles 0) nil)
5023      (t
5024       ;; Init the dependencies hash table.
5025       (setq gnus-newsgroup-dependencies
5026             (gnus-make-hashtable (length articles)))
5027       (gnus-set-global-variables)
5028       ;; Retrieve the headers and read them in.
5029
5030       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5031
5032       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5033       (when cached
5034         (setq gnus-newsgroup-cached cached))
5035
5036       ;; Suppress duplicates?
5037       (when gnus-suppress-duplicates
5038         (gnus-dup-suppress-articles))
5039
5040       ;; Set the initial limit.
5041       (setq gnus-newsgroup-limit (copy-sequence articles))
5042       ;; Remove canceled articles from the list of unread articles.
5043       (setq fetched-articles
5044             (mapcar (lambda (headers) (mail-header-number headers))
5045                     gnus-newsgroup-headers))
5046       (setq gnus-newsgroup-articles fetched-articles)
5047       (setq gnus-newsgroup-unreads
5048             (gnus-sorted-nintersection
5049              gnus-newsgroup-unreads fetched-articles))
5050       (gnus-compute-unseen-list)
5051
5052       ;; Removed marked articles that do not exist.
5053       (gnus-update-missing-marks
5054        (gnus-sorted-difference articles fetched-articles))
5055       ;; We might want to build some more threads first.
5056       (when (and gnus-fetch-old-headers
5057                  (eq gnus-headers-retrieved-by 'nov))
5058         (if (eq gnus-fetch-old-headers 'invisible)
5059             (gnus-build-all-threads)
5060           (gnus-build-old-threads)))
5061       ;; Let the Gnus agent mark articles as read.
5062       (when gnus-agent
5063         (gnus-agent-get-undownloaded-list))
5064       ;; Remove list identifiers from subject
5065       (when gnus-list-identifiers
5066         (gnus-summary-remove-list-identifiers))
5067       ;; Check whether auto-expire is to be done in this group.
5068       (setq gnus-newsgroup-auto-expire
5069             (gnus-group-auto-expirable-p group))
5070       ;; Set up the article buffer now, if necessary.
5071       (unless gnus-single-article-buffer
5072         (gnus-article-setup-buffer))
5073       ;; First and last article in this newsgroup.
5074       (when gnus-newsgroup-headers
5075         (setq gnus-newsgroup-begin
5076               (mail-header-number (car gnus-newsgroup-headers))
5077               gnus-newsgroup-end
5078               (mail-header-number
5079                (gnus-last-element gnus-newsgroup-headers))))
5080       ;; GROUP is successfully selected.
5081       (or gnus-newsgroup-headers t)))))
5082
5083 (defun gnus-compute-unseen-list ()
5084   ;; The `seen' marks are treated specially.
5085   (if (not gnus-newsgroup-seen)
5086       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5087     (setq gnus-newsgroup-unseen
5088           (gnus-inverse-list-range-intersection
5089            gnus-newsgroup-articles gnus-newsgroup-seen))))
5090
5091 (defun gnus-summary-display-make-predicate (display)
5092   (require 'gnus-agent)
5093   (when (= (length display) 1)
5094     (setq display (car display)))
5095   (unless gnus-summary-display-cache
5096     (dolist (elem (append '((unread . unread)
5097                             (read . read)
5098                             (unseen . unseen))
5099                           gnus-article-mark-lists))
5100       (push (cons (cdr elem)
5101                   (gnus-byte-compile
5102                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5103             gnus-summary-display-cache)))
5104   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5105         (gnus-category-predicate-cache gnus-summary-display-cache))
5106     (gnus-get-predicate display)))
5107
5108 ;; Uses the dynamically bound `number' variable.
5109 (eval-when-compile
5110   (defvar number))
5111 (defun gnus-article-marked-p (type &optional article)
5112   (let ((article (or article number)))
5113     (cond
5114      ((eq type 'tick)
5115       (memq article gnus-newsgroup-marked))
5116      ((eq type 'spam)
5117       (memq article gnus-newsgroup-spam-marked))
5118      ((eq type 'unsend)
5119       (memq article gnus-newsgroup-unsendable))
5120      ((eq type 'undownload)
5121       (memq article gnus-newsgroup-undownloaded))
5122      ((eq type 'download)
5123       (memq article gnus-newsgroup-downloadable))
5124      ((eq type 'unread)
5125       (memq article gnus-newsgroup-unreads))
5126      ((eq type 'read)
5127       (memq article gnus-newsgroup-reads))
5128      ((eq type 'dormant)
5129       (memq article gnus-newsgroup-dormant) )
5130      ((eq type 'expire)
5131       (memq article gnus-newsgroup-expirable))
5132      ((eq type 'reply)
5133       (memq article gnus-newsgroup-replied))
5134      ((eq type 'killed)
5135       (memq article gnus-newsgroup-killed))
5136      ((eq type 'bookmark)
5137       (assq article gnus-newsgroup-bookmarks))
5138      ((eq type 'score)
5139       (assq article gnus-newsgroup-scored))
5140      ((eq type 'save)
5141       (memq article gnus-newsgroup-saved))
5142      ((eq type 'cache)
5143       (memq article gnus-newsgroup-cached))
5144      ((eq type 'forward)
5145       (memq article gnus-newsgroup-forwarded))
5146      ((eq type 'seen)
5147       (not (memq article gnus-newsgroup-unseen)))
5148      ((eq type 'recent)
5149       (memq article gnus-newsgroup-recent))
5150      (t t))))
5151
5152 (defun gnus-articles-to-read (group &optional read-all)
5153   "Find out what articles the user wants to read."
5154   (let* ((display (gnus-group-find-parameter group 'display))
5155          (articles
5156           ;; Select all articles if `read-all' is non-nil, or if there
5157           ;; are no unread articles.
5158           (if (or read-all
5159                   (and (zerop (length gnus-newsgroup-marked))
5160                        (zerop (length gnus-newsgroup-unreads)))
5161                   ;; Fetch all if the predicate is non-nil.
5162                   gnus-newsgroup-display)
5163               ;; We want to select the headers for all the articles in
5164               ;; the group, so we select either all the active
5165               ;; articles in the group, or (if that's nil), the
5166               ;; articles in the cache.
5167               (or
5168                (gnus-uncompress-range (gnus-active group))
5169                (gnus-cache-articles-in-group group))
5170             ;; Select only the "normal" subset of articles.
5171             (gnus-sorted-nunion
5172              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5173              gnus-newsgroup-unreads)))
5174          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5175          (scored (length scored-list))
5176          (number (length articles))
5177          (marked (+ (length gnus-newsgroup-marked)
5178                     (length gnus-newsgroup-dormant)))
5179          (select
5180           (cond
5181            ((numberp read-all)
5182             read-all)
5183            ((numberp gnus-newsgroup-display)
5184             gnus-newsgroup-display)
5185            (t
5186             (condition-case ()
5187                 (cond
5188                  ((and (or (<= scored marked) (= scored number))
5189                        (numberp gnus-large-newsgroup)
5190                        (> number gnus-large-newsgroup))
5191                   (let* ((cursor-in-echo-area nil)
5192                          (initial (gnus-parameter-large-newsgroup-initial
5193                                    gnus-newsgroup-name))
5194                          (input
5195                           (read-string
5196                            (format
5197                             "How many articles from %s (%s %d): "
5198                             (gnus-limit-string
5199                              (gnus-group-decoded-name gnus-newsgroup-name)
5200                              35)
5201                             (if initial "max" "default")
5202                             number)
5203                            (if initial
5204                                (cons (number-to-string initial)
5205                                      0)))))
5206                     (if (string-match "^[ \t]*$" input) number input)))
5207                  ((and (> scored marked) (< scored number)
5208                        (> (- scored number) 20))
5209                   (let ((input
5210                          (read-string
5211                           (format "%s %s (%d scored, %d total): "
5212                                   "How many articles from"
5213                                   (gnus-group-decoded-name group)
5214                                   scored number))))
5215                     (if (string-match "^[ \t]*$" input)
5216                         number input)))
5217                  (t number))
5218               (quit
5219                (message "Quit getting the articles to read")
5220                nil))))))
5221     (setq select (if (stringp select) (string-to-number select) select))
5222     (if (or (null select) (zerop select))
5223         select
5224       (if (and (not (zerop scored)) (<= (abs select) scored))
5225           (progn
5226             (setq articles (sort scored-list '<))
5227             (setq number (length articles)))
5228         (setq articles (copy-sequence articles)))
5229
5230       (when (< (abs select) number)
5231         (if (< select 0)
5232             ;; Select the N oldest articles.
5233             (setcdr (nthcdr (1- (abs select)) articles) nil)
5234           ;; Select the N most recent articles.
5235           (setq articles (nthcdr (- number select) articles))))
5236       (setq gnus-newsgroup-unselected
5237             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5238       (when gnus-alter-articles-to-read-function
5239         (setq articles
5240               (sort
5241                (funcall gnus-alter-articles-to-read-function
5242                         gnus-newsgroup-name articles)
5243                '<)))
5244       articles)))
5245
5246 (defun gnus-killed-articles (killed articles)
5247   (let (out)
5248     (while articles
5249       (when (inline (gnus-member-of-range (car articles) killed))
5250         (push (car articles) out))
5251       (setq articles (cdr articles)))
5252     out))
5253
5254 (defun gnus-uncompress-marks (marks)
5255   "Uncompress the mark ranges in MARKS."
5256   (let ((uncompressed '(score bookmark))
5257         out)
5258     (while marks
5259       (if (memq (caar marks) uncompressed)
5260           (push (car marks) out)
5261         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5262       (setq marks (cdr marks)))
5263     out))
5264
5265 (defun gnus-article-mark-to-type (mark)
5266   "Return the type of MARK."
5267   (or (cadr (assq mark gnus-article-special-mark-lists))
5268       'list))
5269
5270 (defun gnus-article-unpropagatable-p (mark)
5271   "Return whether MARK should be propagated to backend."
5272   (memq mark gnus-article-unpropagated-mark-lists))
5273
5274 (defun gnus-adjust-marked-articles (info)
5275   "Set all article lists and remove all marks that are no longer valid."
5276   (let* ((marked-lists (gnus-info-marks info))
5277          (active (gnus-active (gnus-info-group info)))
5278          (min (car active))
5279          (max (cdr active))
5280          (types gnus-article-mark-lists)
5281          marks var articles article mark mark-type)
5282
5283     (dolist (marks marked-lists)
5284       (setq mark (car marks)
5285             mark-type (gnus-article-mark-to-type mark)
5286             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5287
5288       ;; We set the variable according to the type of the marks list,
5289       ;; and then adjust the marks to a subset of the active articles.
5290       (cond
5291        ;; Adjust "simple" lists.
5292        ((eq mark-type 'list)
5293         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5294         (when (memq mark '(tick dormant expire reply save))
5295           (while articles
5296             (when (or (< (setq article (pop articles)) min) (> article max))
5297               (set var (delq article (symbol-value var)))))))
5298        ;; Adjust assocs.
5299        ((eq mark-type 'tuple)
5300         (set var (setq articles (cdr marks)))
5301         (when (not (listp (cdr (symbol-value var))))
5302           (set var (list (symbol-value var))))
5303         (when (not (listp (cdr articles)))
5304           (setq articles (list articles)))
5305         (while articles
5306           (when (or (not (consp (setq article (pop articles))))
5307                     (< (car article) min)
5308                     (> (car article) max))
5309             (set var (delq article (symbol-value var))))))
5310        ;; Adjust ranges (sloppily).
5311        ((eq mark-type 'range)
5312         (cond
5313          ((eq mark 'seen)
5314           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5315           ;; It should be (seen (NUM1 . NUM2)).
5316           (when (numberp (cddr marks))
5317             (setcdr marks (list (cdr marks))))
5318           (setq articles (cdr marks))
5319           (while (and articles
5320                       (or (and (consp (car articles))
5321                                (> min (cdar articles)))
5322                           (and (numberp (car articles))
5323                                (> min (car articles)))))
5324             (pop articles))
5325           (set var articles))))))))
5326
5327 (defun gnus-update-missing-marks (missing)
5328   "Go through the list of MISSING articles and remove them from the mark lists."
5329   (when missing
5330     (let (var m)
5331       ;; Go through all types.
5332       (dolist (elem gnus-article-mark-lists)
5333         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5334           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5335           (when (symbol-value var)
5336             ;; This list has articles.  So we delete all missing
5337             ;; articles from it.
5338             (setq m missing)
5339             (while m
5340               (set var (delq (pop m) (symbol-value var))))))))))
5341
5342 (defun gnus-update-marks ()
5343   "Enter the various lists of marked articles into the newsgroup info list."
5344   (let ((types gnus-article-mark-lists)
5345         (info (gnus-get-info gnus-newsgroup-name))
5346         type list newmarked symbol delta-marks)
5347     (when info
5348       ;; Add all marks lists to the list of marks lists.
5349       (while (setq type (pop types))
5350         (setq list (symbol-value
5351                     (setq symbol
5352                           (intern (format "gnus-newsgroup-%s" (car type))))))
5353
5354         (when list
5355           ;; Get rid of the entries of the articles that have the
5356           ;; default score.
5357           (when (and (eq (cdr type) 'score)
5358                      gnus-save-score
5359                      list)
5360             (let* ((arts list)
5361                    (prev (cons nil list))
5362                    (all prev))
5363               (while arts
5364                 (if (or (not (consp (car arts)))
5365                         (= (cdar arts) gnus-summary-default-score))
5366                     (setcdr prev (cdr arts))
5367                   (setq prev arts))
5368                 (setq arts (cdr arts)))
5369               (setq list (cdr all)))))
5370
5371         (when (eq (cdr type) 'seen)
5372           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5373
5374         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5375           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5376
5377         (when (and (gnus-check-backend-function
5378                     'request-set-mark gnus-newsgroup-name)
5379                    (not (gnus-article-unpropagatable-p (cdr type))))
5380           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5381                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5382                  (add (gnus-remove-from-range
5383                        (gnus-copy-sequence list) old)))
5384             (when add
5385               (push (list add 'add (list (cdr type))) delta-marks))
5386             (when del
5387               (push (list del 'del (list (cdr type))) delta-marks))))
5388
5389         (when list
5390           (push (cons (cdr type) list) newmarked)))
5391
5392       (when delta-marks
5393         (unless (gnus-check-group gnus-newsgroup-name)
5394           (error "Can't open server for %s" gnus-newsgroup-name))
5395         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5396
5397       ;; Enter these new marks into the info of the group.
5398       (if (nthcdr 3 info)
5399           (setcar (nthcdr 3 info) newmarked)
5400         ;; Add the marks lists to the end of the info.
5401         (when newmarked
5402           (setcdr (nthcdr 2 info) (list newmarked))))
5403
5404       ;; Cut off the end of the info if there's nothing else there.
5405       (let ((i 5))
5406         (while (and (> i 2)
5407                     (not (nth i info)))
5408           (when (nthcdr (decf i) info)
5409             (setcdr (nthcdr i info) nil)))))))
5410
5411 (defun gnus-set-mode-line (where)
5412   "Set the mode line of the article or summary buffers.
5413 If WHERE is `summary', the summary mode line format will be used."
5414   ;; Is this mode line one we keep updated?
5415   (when (and (memq where gnus-updated-mode-lines)
5416              (symbol-value
5417               (intern (format "gnus-%s-mode-line-format-spec" where))))
5418     (let (mode-string)
5419       (save-excursion
5420         ;; We evaluate this in the summary buffer since these
5421         ;; variables are buffer-local to that buffer.
5422         (set-buffer gnus-summary-buffer)
5423        ;; We bind all these variables that are used in the `eval' form
5424         ;; below.
5425         (let* ((mformat (symbol-value
5426                          (intern
5427                           (format "gnus-%s-mode-line-format-spec" where))))
5428                (gnus-tmp-group-name (gnus-group-decoded-name
5429                                      gnus-newsgroup-name))
5430                (gnus-tmp-article-number (or gnus-current-article 0))
5431                (gnus-tmp-unread gnus-newsgroup-unreads)
5432                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5433                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5434                (gnus-tmp-unread-and-unselected
5435                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5436                             (zerop gnus-tmp-unselected))
5437                        "")
5438                       ((zerop gnus-tmp-unselected)
5439                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5440                       (t (format "{%d(+%d) more}"
5441                                  gnus-tmp-unread-and-unticked
5442                                  gnus-tmp-unselected))))
5443                (gnus-tmp-subject
5444                 (if (and gnus-current-headers
5445                          (vectorp gnus-current-headers))
5446                     (gnus-mode-string-quote
5447                      (mail-header-subject gnus-current-headers))
5448                   ""))
5449                bufname-length max-len
5450                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5451           (setq mode-string (eval mformat))
5452           (setq bufname-length (if (string-match "%b" mode-string)
5453                                    (- (length
5454                                        (buffer-name
5455                                         (if (eq where 'summary)
5456                                             nil
5457                                           (get-buffer gnus-article-buffer))))
5458                                       2)
5459                                  0))
5460           (setq max-len (max 4 (if gnus-mode-non-string-length
5461                                    (- (window-width)
5462                                       gnus-mode-non-string-length
5463                                       bufname-length)
5464                                  (length mode-string))))
5465           ;; We might have to chop a bit of the string off...
5466           (when (> (length mode-string) max-len)
5467             (setq mode-string
5468                   (concat (truncate-string-to-width mode-string (- max-len 3))
5469                           "...")))
5470           ;; Pad the mode string a bit.
5471           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5472       ;; Update the mode line.
5473       (setq mode-line-buffer-identification
5474             (gnus-mode-line-buffer-identification (list mode-string)))
5475       (set-buffer-modified-p t))))
5476
5477 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5478   "Go through the HEADERS list and add all Xrefs to a hash table.
5479 The resulting hash table is returned, or nil if no Xrefs were found."
5480   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5481          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5482          (xref-hashtb (gnus-make-hashtable))
5483          start group entry number xrefs header)
5484     (while headers
5485       (setq header (pop headers))
5486       (when (and (setq xrefs (mail-header-xref header))
5487                  (not (memq (setq number (mail-header-number header))
5488                             unreads)))
5489         (setq start 0)
5490         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5491           (setq start (match-end 0))
5492           (setq group (if prefix
5493                           (concat prefix (substring xrefs (match-beginning 1)
5494                                                     (match-end 1)))
5495                         (substring xrefs (match-beginning 1) (match-end 1))))
5496           (setq number
5497                 (string-to-int (substring xrefs (match-beginning 2)
5498                                           (match-end 2))))
5499           (if (setq entry (gnus-gethash group xref-hashtb))
5500               (setcdr entry (cons number (cdr entry)))
5501             (gnus-sethash group (cons number nil) xref-hashtb)))))
5502     (and start xref-hashtb)))
5503
5504 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5505   "Look through all the headers and mark the Xrefs as read."
5506   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5507         name entry info xref-hashtb idlist method nth4)
5508     (save-excursion
5509       (set-buffer gnus-group-buffer)
5510       (when (setq xref-hashtb
5511                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5512         (mapatoms
5513          (lambda (group)
5514            (unless (string= from-newsgroup (setq name (symbol-name group)))
5515              (setq idlist (symbol-value group))
5516              ;; Dead groups are not updated.
5517              (and (prog1
5518                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5519                             info (nth 2 entry))
5520                     (when (stringp (setq nth4 (gnus-info-method info)))
5521                       (setq nth4 (gnus-server-to-method nth4))))
5522                   ;; Only do the xrefs if the group has the same
5523                   ;; select method as the group we have just read.
5524                   (or (gnus-methods-equal-p
5525                        nth4 (gnus-find-method-for-group from-newsgroup))
5526                       virtual
5527                       (equal nth4 (setq method (gnus-find-method-for-group
5528                                                 from-newsgroup)))
5529                       (and (equal (car nth4) (car method))
5530                            (equal (nth 1 nth4) (nth 1 method))))
5531                   gnus-use-cross-reference
5532                   (or (not (eq gnus-use-cross-reference t))
5533                       virtual
5534                       ;; Only do cross-references on subscribed
5535                       ;; groups, if that is what is wanted.
5536                       (<= (gnus-info-level info) gnus-level-subscribed))
5537                   (gnus-group-make-articles-read name idlist))))
5538          xref-hashtb)))))
5539
5540 (defun gnus-compute-read-articles (group articles)
5541   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5542          (info (nth 2 entry))
5543          (active (gnus-active group))
5544          ninfo)
5545     (when entry
5546       ;; First peel off all invalid article numbers.
5547       (when active
5548         (let ((ids articles)
5549               id first)
5550           (while (setq id (pop ids))
5551             (when (and first (> id (cdr active)))
5552               ;; We'll end up in this situation in one particular
5553               ;; obscure situation.  If you re-scan a group and get
5554               ;; a new article that is cross-posted to a different
5555               ;; group that has not been re-scanned, you might get
5556               ;; crossposted article that has a higher number than
5557               ;; Gnus believes possible.  So we re-activate this
5558               ;; group as well.  This might mean doing the
5559               ;; crossposting thingy will *increase* the number
5560               ;; of articles in some groups.  Tsk, tsk.
5561               (setq active (or (gnus-activate-group group) active)))
5562             (when (or (> id (cdr active))
5563                       (< id (car active)))
5564               (setq articles (delq id articles))))))
5565       ;; If the read list is nil, we init it.
5566       (if (and active
5567                (null (gnus-info-read info))
5568                (> (car active) 1))
5569           (setq ninfo (cons 1 (1- (car active))))
5570         (setq ninfo (gnus-info-read info)))
5571       ;; Then we add the read articles to the range.
5572       (gnus-add-to-range
5573        ninfo (setq articles (sort articles '<))))))
5574
5575 (defun gnus-group-make-articles-read (group articles)
5576   "Update the info of GROUP to say that ARTICLES are read."
5577   (let* ((num 0)
5578          (entry (gnus-gethash group gnus-newsrc-hashtb))
5579          (info (nth 2 entry))
5580          (active (gnus-active group))
5581          range)
5582     (when entry
5583       (setq range (gnus-compute-read-articles group articles))
5584       (save-excursion
5585         (set-buffer gnus-group-buffer)
5586         (gnus-undo-register
5587           `(progn
5588              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5589              (gnus-info-set-read ',info ',(gnus-info-read info))
5590              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5591              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5592              (gnus-group-update-group ,group t))))
5593       ;; Add the read articles to the range.
5594       (gnus-info-set-read info range)
5595       (gnus-request-set-mark group (list (list range 'add '(read))))
5596       ;; Then we have to re-compute how many unread
5597       ;; articles there are in this group.
5598       (when active
5599         (cond
5600          ((not range)
5601           (setq num (- (1+ (cdr active)) (car active))))
5602          ((not (listp (cdr range)))
5603           (setq num (- (cdr active) (- (1+ (cdr range))
5604                                        (car range)))))
5605          (t
5606           (while range
5607             (if (numberp (car range))
5608                 (setq num (1+ num))
5609               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5610             (setq range (cdr range)))
5611           (setq num (- (cdr active) num))))
5612         ;; Update the number of unread articles.
5613         (setcar entry num)
5614         ;; Update the group buffer.
5615         (unless (gnus-ephemeral-group-p group)
5616           (gnus-group-update-group group t))))))
5617
5618 (defvar gnus-newsgroup-none-id 0)
5619
5620 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5621   (let ((cur nntp-server-buffer)
5622         (dependencies
5623          (or dependencies
5624              (save-excursion (set-buffer gnus-summary-buffer)
5625                              gnus-newsgroup-dependencies)))
5626         headers id end ref
5627         (mail-parse-charset gnus-newsgroup-charset)
5628         (mail-parse-ignored-charsets
5629          (save-excursion (condition-case nil
5630                              (set-buffer gnus-summary-buffer)
5631                            (error))
5632                          gnus-newsgroup-ignored-charsets)))
5633     (save-excursion
5634       (set-buffer nntp-server-buffer)
5635       ;; Translate all TAB characters into SPACE characters.
5636       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5637       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5638       (gnus-run-hooks 'gnus-parse-headers-hook)
5639       (let ((case-fold-search t)
5640             in-reply-to header p lines chars)
5641         (goto-char (point-min))
5642         ;; Search to the beginning of the next header.  Error messages
5643         ;; do not begin with 2 or 3.
5644         (while (re-search-forward "^[23][0-9]+ " nil t)
5645           (setq id nil
5646                 ref nil)
5647           ;; This implementation of this function, with nine
5648           ;; search-forwards instead of the one re-search-forward and
5649           ;; a case (which basically was the old function) is actually
5650           ;; about twice as fast, even though it looks messier.  You
5651           ;; can't have everything, I guess.  Speed and elegance
5652           ;; doesn't always go hand in hand.
5653           (setq
5654            header
5655            (vector
5656             ;; Number.
5657             (prog1
5658                 (read cur)
5659               (end-of-line)
5660               (setq p (point))
5661               (narrow-to-region (point)
5662                                 (or (and (search-forward "\n.\n" nil t)
5663                                          (- (point) 2))
5664                                     (point))))
5665             ;; Subject.
5666             (progn
5667               (goto-char p)
5668               (if (search-forward "\nsubject:" nil t)
5669                   (funcall gnus-decode-encoded-word-function
5670                            (nnheader-header-value))
5671                 "(none)"))
5672             ;; From.
5673             (progn
5674               (goto-char p)
5675               (if (search-forward "\nfrom:" nil t)
5676                   (funcall gnus-decode-encoded-word-function
5677                            (nnheader-header-value))
5678                 "(nobody)"))
5679             ;; Date.
5680             (progn
5681               (goto-char p)
5682               (if (search-forward "\ndate:" nil t)
5683                   (nnheader-header-value) ""))
5684             ;; Message-ID.
5685             (progn
5686               (goto-char p)
5687               (setq id (if (re-search-forward
5688                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5689                            ;; We do it this way to make sure the Message-ID
5690                            ;; is (somewhat) syntactically valid.
5691                            (buffer-substring (match-beginning 1)
5692                                              (match-end 1))
5693                          ;; If there was no message-id, we just fake one
5694                          ;; to make subsequent routines simpler.
5695                          (nnheader-generate-fake-message-id))))
5696             ;; References.
5697             (progn
5698               (goto-char p)
5699               (if (search-forward "\nreferences:" nil t)
5700                   (progn
5701                     (setq end (point))
5702                     (prog1
5703                         (nnheader-header-value)
5704                       (setq ref
5705                             (buffer-substring
5706                              (progn
5707                                (end-of-line)
5708                                (search-backward ">" end t)
5709                                (1+ (point)))
5710                              (progn
5711                                (search-backward "<" end t)
5712                                (point))))))
5713                 ;; Get the references from the in-reply-to header if there
5714                 ;; were no references and the in-reply-to header looks
5715                 ;; promising.
5716                 (if (and (search-forward "\nin-reply-to:" nil t)
5717                          (setq in-reply-to (nnheader-header-value))
5718                          (string-match "<[^>]+>" in-reply-to))
5719                     (let (ref2)
5720                       (setq ref (substring in-reply-to (match-beginning 0)
5721                                            (match-end 0)))
5722                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5723                         (setq ref2 (substring in-reply-to (match-beginning 0)
5724                                               (match-end 0)))
5725                         (when (> (length ref2) (length ref))
5726                           (setq ref ref2)))
5727                       ref)
5728                   (setq ref nil))))
5729             ;; Chars.
5730             (progn
5731               (goto-char p)
5732               (if (search-forward "\nchars: " nil t)
5733                   (if (numberp (setq chars (ignore-errors (read cur))))
5734                       chars -1)
5735                 -1))
5736             ;; Lines.
5737             (progn
5738               (goto-char p)
5739               (if (search-forward "\nlines: " nil t)
5740                   (if (numberp (setq lines (ignore-errors (read cur))))
5741                       lines -1)
5742                 -1))
5743             ;; Xref.
5744             (progn
5745               (goto-char p)
5746               (and (search-forward "\nxref:" nil t)
5747                    (nnheader-header-value)))
5748             ;; Extra.
5749             (when gnus-extra-headers
5750               (let ((extra gnus-extra-headers)
5751                     out)
5752                 (while extra
5753                   (goto-char p)
5754                   (when (search-forward
5755                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5756                     (push (cons (car extra) (nnheader-header-value))
5757                           out))
5758                   (pop extra))
5759                 out))))
5760           (when (equal id ref)
5761             (setq ref nil))
5762
5763           (when gnus-alter-header-function
5764             (funcall gnus-alter-header-function header)
5765             (setq id (mail-header-id header)
5766                   ref (gnus-parent-id (mail-header-references header))))
5767
5768           (when (setq header
5769                       (gnus-dependencies-add-header
5770                        header dependencies force-new))
5771             (push header headers))
5772           (goto-char (point-max))
5773           (widen))
5774         (nreverse headers)))))
5775
5776 ;; Goes through the xover lines and returns a list of vectors
5777 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5778                                                   force-new dependencies
5779                                                   group also-fetch-heads)
5780   "Parse the news overview data in the server buffer.
5781 Return a list of headers that match SEQUENCE (see
5782 `nntp-retrieve-headers')."
5783   ;; Get the Xref when the users reads the articles since most/some
5784   ;; NNTP servers do not include Xrefs when using XOVER.
5785   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5786   (let ((mail-parse-charset gnus-newsgroup-charset)
5787         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5788         (cur nntp-server-buffer)
5789         (dependencies (or dependencies gnus-newsgroup-dependencies))
5790         (allp (cond
5791                ((eq gnus-read-all-available-headers t)
5792                 t)
5793                ((stringp gnus-read-all-available-headers)
5794                 (string-match gnus-read-all-available-headers group))
5795                (t
5796                 nil)))
5797         number headers header)
5798     (save-excursion
5799       (set-buffer nntp-server-buffer)
5800       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5801       ;; Allow the user to mangle the headers before parsing them.
5802       (gnus-run-hooks 'gnus-parse-headers-hook)
5803       (goto-char (point-min))
5804       (gnus-parse-without-error
5805         (while (and (or sequence allp)
5806                     (not (eobp)))
5807           (setq number (read cur))
5808           (when (not allp)
5809             (while (and sequence
5810                         (< (car sequence) number))
5811               (setq sequence (cdr sequence))))
5812           (when (and (or allp
5813                          (and sequence
5814                               (eq number (car sequence))))
5815                      (progn
5816                        (setq sequence (cdr sequence))
5817                        (setq header (inline
5818                                       (gnus-nov-parse-line
5819                                        number dependencies force-new)))))
5820             (push header headers))
5821           (forward-line 1)))
5822       ;; A common bug in inn is that if you have posted an article and
5823       ;; then retrieves the active file, it will answer correctly --
5824       ;; the new article is included.  However, a NOV entry for the
5825       ;; article may not have been generated yet, so this may fail.
5826       ;; We work around this problem by retrieving the last few
5827       ;; headers using HEAD.
5828       (if (or (not also-fetch-heads)
5829               (not sequence))
5830           ;; We (probably) got all the headers.
5831           (nreverse headers)
5832         (let ((gnus-nov-is-evil t))
5833           (nconc
5834            (nreverse headers)
5835            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5836              (gnus-get-newsgroup-headers))))))))
5837
5838 (defun gnus-article-get-xrefs ()
5839   "Fill in the Xref value in `gnus-current-headers', if necessary.
5840 This is meant to be called in `gnus-article-internal-prepare-hook'."
5841   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5842                                  gnus-current-headers)))
5843     (or (not gnus-use-cross-reference)
5844         (not headers)
5845         (and (mail-header-xref headers)
5846              (not (string= (mail-header-xref headers) "")))
5847         (let ((case-fold-search t)
5848               xref)
5849           (save-restriction
5850             (nnheader-narrow-to-headers)
5851             (goto-char (point-min))
5852             (when (or (and (not (eobp))
5853                            (eq (downcase (char-after)) ?x)
5854                            (looking-at "Xref:"))
5855                       (search-forward "\nXref:" nil t))
5856               (goto-char (1+ (match-end 0)))
5857               (setq xref (buffer-substring (point)
5858                                            (progn (end-of-line) (point))))
5859               (mail-header-set-xref headers xref)))))))
5860
5861 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5862   "Find article ID and insert the summary line for that article.
5863 OLD-HEADER can either be a header or a line number to insert
5864 the subject line on."
5865   (let* ((line (and (numberp old-header) old-header))
5866          (old-header (and (vectorp old-header) old-header))
5867          (header (cond ((and old-header use-old-header)
5868                         old-header)
5869                        ((and (numberp id)
5870                              (gnus-number-to-header id))
5871                         (gnus-number-to-header id))
5872                        (t
5873                         (gnus-read-header id))))
5874          (number (and (numberp id) id))
5875          d)
5876     (when header
5877       ;; Rebuild the thread that this article is part of and go to the
5878       ;; article we have fetched.
5879       (when (and (not gnus-show-threads)
5880                  old-header)
5881         (when (and number
5882                    (setq d (gnus-data-find (mail-header-number old-header))))
5883           (goto-char (gnus-data-pos d))
5884           (gnus-data-remove
5885            number
5886            (- (gnus-point-at-bol)
5887               (prog1
5888                   (1+ (gnus-point-at-eol))
5889                 (gnus-delete-line))))))
5890       (when old-header
5891         (mail-header-set-number header (mail-header-number old-header)))
5892       (setq gnus-newsgroup-sparse
5893             (delq (setq number (mail-header-number header))
5894                   gnus-newsgroup-sparse))
5895       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5896       (push number gnus-newsgroup-limit)
5897       (gnus-rebuild-thread (mail-header-id header) line)
5898       (gnus-summary-goto-subject number nil t))
5899     (when (and (numberp number)
5900                (> number 0))
5901       ;; We have to update the boundaries even if we can't fetch the
5902       ;; article if ID is a number -- so that the next `P' or `N'
5903       ;; command will fetch the previous (or next) article even
5904       ;; if the one we tried to fetch this time has been canceled.
5905       (when (> number gnus-newsgroup-end)
5906         (setq gnus-newsgroup-end number))
5907       (when (< number gnus-newsgroup-begin)
5908         (setq gnus-newsgroup-begin number))
5909       (setq gnus-newsgroup-unselected
5910             (delq number gnus-newsgroup-unselected)))
5911     ;; Report back a success?
5912     (and header (mail-header-number header))))
5913
5914 ;;; Process/prefix in the summary buffer
5915
5916 (defun gnus-summary-work-articles (n)
5917   "Return a list of articles to be worked upon.
5918 The prefix argument, the list of process marked articles, and the
5919 current article will be taken into consideration."
5920   (save-excursion
5921     (set-buffer gnus-summary-buffer)
5922     (cond
5923      (n
5924       ;; A numerical prefix has been given.
5925       (setq n (prefix-numeric-value n))
5926       (let ((backward (< n 0))
5927             (n (abs (prefix-numeric-value n)))
5928             articles article)
5929         (save-excursion
5930           (while
5931               (and (> n 0)
5932                    (push (setq article (gnus-summary-article-number))
5933                          articles)
5934                    (if backward
5935                        (gnus-summary-find-prev nil article)
5936                      (gnus-summary-find-next nil article)))
5937             (decf n)))
5938         (nreverse articles)))
5939      ((and (gnus-region-active-p) (mark))
5940       (message "region active")
5941       ;; Work on the region between point and mark.
5942       (let ((max (max (point) (mark)))
5943             articles article)
5944         (save-excursion
5945           (goto-char (min (min (point) (mark))))
5946           (while
5947               (and
5948                (push (setq article (gnus-summary-article-number)) articles)
5949                (gnus-summary-find-next nil article)
5950                (< (point) max)))
5951           (nreverse articles))))
5952      (gnus-newsgroup-processable
5953       ;; There are process-marked articles present.
5954       ;; Save current state.
5955       (gnus-summary-save-process-mark)
5956       ;; Return the list.
5957       (reverse gnus-newsgroup-processable))
5958      (t
5959       ;; Just return the current article.
5960       (list (gnus-summary-article-number))))))
5961
5962 (defmacro gnus-summary-iterate (arg &rest forms)
5963   "Iterate over the process/prefixed articles and do FORMS.
5964 ARG is the interactive prefix given to the command.  FORMS will be
5965 executed with point over the summary line of the articles."
5966   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5967     `(let ((,articles (gnus-summary-work-articles ,arg)))
5968        (while ,articles
5969          (gnus-summary-goto-subject (car ,articles))
5970          ,@forms
5971          (pop ,articles)))))
5972
5973 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5974 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5975
5976 (defun gnus-summary-save-process-mark ()
5977   "Push the current set of process marked articles on the stack."
5978   (interactive)
5979   (push (copy-sequence gnus-newsgroup-processable)
5980         gnus-newsgroup-process-stack))
5981
5982 (defun gnus-summary-kill-process-mark ()
5983   "Push the current set of process marked articles on the stack and unmark."
5984   (interactive)
5985   (gnus-summary-save-process-mark)
5986   (gnus-summary-unmark-all-processable))
5987
5988 (defun gnus-summary-yank-process-mark ()
5989   "Pop the last process mark state off the stack and restore it."
5990   (interactive)
5991   (unless gnus-newsgroup-process-stack
5992     (error "Empty mark stack"))
5993   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5994
5995 (defun gnus-summary-process-mark-set (set)
5996   "Make SET into the current process marked articles."
5997   (gnus-summary-unmark-all-processable)
5998   (while set
5999     (gnus-summary-set-process-mark (pop set))))
6000
6001 ;;; Searching and stuff
6002
6003 (defun gnus-summary-search-group (&optional backward use-level)
6004   "Search for next unread newsgroup.
6005 If optional argument BACKWARD is non-nil, search backward instead."
6006   (save-excursion
6007     (set-buffer gnus-group-buffer)
6008     (when (gnus-group-search-forward
6009            backward nil (if use-level (gnus-group-group-level) nil))
6010       (gnus-group-group-name))))
6011
6012 (defun gnus-summary-best-group (&optional exclude-group)
6013   "Find the name of the best unread group.
6014 If EXCLUDE-GROUP, do not go to this group."
6015   (save-excursion
6016     (set-buffer gnus-group-buffer)
6017     (save-excursion
6018       (gnus-group-best-unread-group exclude-group))))
6019
6020 (defun gnus-summary-find-next (&optional unread article backward)
6021   (if backward (gnus-summary-find-prev)
6022     (let* ((dummy (gnus-summary-article-intangible-p))
6023            (article (or article (gnus-summary-article-number)))
6024            (data (gnus-data-find-list article))
6025            result)
6026       (when (and (not dummy)
6027                  (or (not gnus-summary-check-current)
6028                      (not unread)
6029                      (not (gnus-data-unread-p (car data)))))
6030         (setq data (cdr data)))
6031       (when (setq result
6032                   (if unread
6033                       (progn
6034                         (while data
6035                           (unless (memq (gnus-data-number (car data)) 
6036                                         gnus-newsgroup-unfetched)
6037                             (when (gnus-data-unread-p (car data))
6038                               (setq result (car data)
6039                                     data nil)))
6040                           (setq data (cdr data)))
6041                         result)
6042                     (car data)))
6043         (goto-char (gnus-data-pos result))
6044         (gnus-data-number result)))))
6045
6046 (defun gnus-summary-find-prev (&optional unread article)
6047   (let* ((eobp (eobp))
6048          (article (or article (gnus-summary-article-number)))
6049          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6050          result)
6051     (when (and (not eobp)
6052                (or (not gnus-summary-check-current)
6053                    (not unread)
6054                    (not (gnus-data-unread-p (car data)))))
6055       (setq data (cdr data)))
6056     (when (setq result
6057                 (if unread
6058                     (progn
6059                       (while data
6060                         (unless (memq (gnus-data-number (car data)) gnus-newsgroup-unfetched)
6061                           (when (gnus-data-unread-p (car data))
6062                             (setq result (car data)
6063                                   data nil)))
6064                         (setq data (cdr data)))
6065                       result)
6066                   (car data)))
6067       (goto-char (gnus-data-pos result))
6068       (gnus-data-number result))))
6069
6070 (defun gnus-summary-find-subject (subject &optional unread backward article)
6071   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6072          (article (or article (gnus-summary-article-number)))
6073          (articles (gnus-data-list backward))
6074          (arts (gnus-data-find-list article articles))
6075          result)
6076     (when (or (not gnus-summary-check-current)
6077               (not unread)
6078               (not (gnus-data-unread-p (car arts))))
6079       (setq arts (cdr arts)))
6080     (while arts
6081       (and (or (not unread)
6082                (gnus-data-unread-p (car arts)))
6083            (vectorp (gnus-data-header (car arts)))
6084            (gnus-subject-equal
6085             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6086            (setq result (car arts)
6087                  arts nil))
6088       (setq arts (cdr arts)))
6089     (and result
6090          (goto-char (gnus-data-pos result))
6091          (gnus-data-number result))))
6092
6093 (defun gnus-summary-search-forward (&optional unread subject backward)
6094   "Search forward for an article.
6095 If UNREAD, look for unread articles.  If SUBJECT, look for
6096 articles with that subject.  If BACKWARD, search backward instead."
6097   (cond (subject (gnus-summary-find-subject subject unread backward))
6098         (backward (gnus-summary-find-prev unread))
6099         (t (gnus-summary-find-next unread))))
6100
6101 (defun gnus-recenter (&optional n)
6102   "Center point in window and redisplay frame.
6103 Also do horizontal recentering."
6104   (interactive "P")
6105   (when (and gnus-auto-center-summary
6106              (not (eq gnus-auto-center-summary 'vertical)))
6107     (gnus-horizontal-recenter))
6108   (recenter n))
6109
6110 (defun gnus-summary-recenter ()
6111   "Center point in the summary window.
6112 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6113 displayed, no centering will be performed."
6114   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6115   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6116   (interactive)
6117   ;; The user has to want it.
6118   (when gnus-auto-center-summary
6119     (let* ((top (cond ((< (window-height) 4) 0)
6120                       ((< (window-height) 7) 1)
6121                       (t (if (numberp gnus-auto-center-summary)
6122                              gnus-auto-center-summary
6123                            2))))
6124            (height (1- (window-height)))
6125            (bottom (save-excursion (goto-char (point-max))
6126                                    (forward-line (- height))
6127                                    (point)))
6128            (window (get-buffer-window (current-buffer))))
6129       (when (get-buffer-window gnus-article-buffer)
6130         ;; Only do recentering when the article buffer is displayed,
6131         ;; Set the window start to either `bottom', which is the biggest
6132         ;; possible valid number, or the second line from the top,
6133         ;; whichever is the least.
6134         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6135           (if (> bottom top-pos)
6136               ;; Keep the second line from the top visible
6137               (set-window-start window top-pos t)
6138             ;; Try to keep the bottom line visible; if it's partially
6139             ;; obscured, either scroll one more line to make it fully
6140             ;; visible, or revert to using TOP-POS.
6141             (save-excursion
6142               (goto-char (point-max))
6143               (forward-line -1)
6144               (let ((last-line-start (point)))
6145                 (goto-char bottom)
6146                 (set-window-start window (point) t)
6147                 (when (not (pos-visible-in-window-p last-line-start window))
6148                   (forward-line 1)
6149                   (set-window-start window (min (point) top-pos) t)))))))
6150       ;; Do horizontal recentering while we're at it.
6151       (when (and (get-buffer-window (current-buffer) t)
6152                  (not (eq gnus-auto-center-summary 'vertical)))
6153         (let ((selected (selected-window)))
6154           (select-window (get-buffer-window (current-buffer) t))
6155           (gnus-summary-position-point)
6156           (gnus-horizontal-recenter)
6157           (select-window selected))))))
6158
6159 (defun gnus-summary-jump-to-group (newsgroup)
6160   "Move point to NEWSGROUP in group mode buffer."
6161   ;; Keep update point of group mode buffer if visible.
6162   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6163       (save-window-excursion
6164         ;; Take care of tree window mode.
6165         (when (get-buffer-window gnus-group-buffer)
6166           (pop-to-buffer gnus-group-buffer))
6167         (gnus-group-jump-to-group newsgroup))
6168     (save-excursion
6169       ;; Take care of tree window mode.
6170       (if (get-buffer-window gnus-group-buffer)
6171           (pop-to-buffer gnus-group-buffer)
6172         (set-buffer gnus-group-buffer))
6173       (gnus-group-jump-to-group newsgroup))))
6174
6175 ;; This function returns a list of article numbers based on the
6176 ;; difference between the ranges of read articles in this group and
6177 ;; the range of active articles.
6178 (defun gnus-list-of-unread-articles (group)
6179   (let* ((read (gnus-info-read (gnus-get-info group)))
6180          (active (or (gnus-active group) (gnus-activate-group group)))
6181          (last (cdr active))
6182          first nlast unread)
6183     ;; If none are read, then all are unread.
6184     (if (not read)
6185         (setq first (car active))
6186       ;; If the range of read articles is a single range, then the
6187       ;; first unread article is the article after the last read
6188       ;; article.  Sounds logical, doesn't it?
6189       (if (and (not (listp (cdr read)))
6190                (or (< (car read) (car active))
6191                    (progn (setq read (list read))
6192                           nil)))
6193           (setq first (max (car active) (1+ (cdr read))))
6194         ;; `read' is a list of ranges.
6195         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6196                                   (caar read)))
6197                   1)
6198           (setq first (car active)))
6199         (while read
6200           (when first
6201             (while (< first nlast)
6202               (push first unread)
6203               (setq first (1+ first))))
6204           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6205           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6206           (setq read (cdr read)))))
6207     ;; And add the last unread articles.
6208     (while (<= first last)
6209       (push first unread)
6210       (setq first (1+ first)))
6211     ;; Return the list of unread articles.
6212     (delq 0 (nreverse unread))))
6213
6214 (defun gnus-list-of-read-articles (group)
6215   "Return a list of unread, unticked and non-dormant articles."
6216   (let* ((info (gnus-get-info group))
6217          (marked (gnus-info-marks info))
6218          (active (gnus-active group)))
6219     (and info active
6220          (gnus-list-range-difference
6221           (gnus-list-range-difference
6222            (gnus-sorted-complement
6223             (gnus-uncompress-range active)
6224             (gnus-list-of-unread-articles group))
6225            (cdr (assq 'dormant marked)))
6226           (cdr (assq 'tick marked))))))
6227
6228 ;; Various summary commands
6229
6230 (defun gnus-summary-select-article-buffer ()
6231   "Reconfigure windows to show article buffer."
6232   (interactive)
6233   (if (not (gnus-buffer-live-p gnus-article-buffer))
6234       (error "There is no article buffer for this summary buffer")
6235     (gnus-configure-windows 'article)
6236     (select-window (get-buffer-window gnus-article-buffer))))
6237
6238 (defun gnus-summary-universal-argument (arg)
6239   "Perform any operation on all articles that are process/prefixed."
6240   (interactive "P")
6241   (let ((articles (gnus-summary-work-articles arg))
6242         func article)
6243     (if (eq
6244          (setq
6245           func
6246           (key-binding
6247            (read-key-sequence
6248             (substitute-command-keys
6249              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6250          'undefined)
6251         (gnus-error 1 "Undefined key")
6252       (save-excursion
6253         (while articles
6254           (gnus-summary-goto-subject (setq article (pop articles)))
6255           (let (gnus-newsgroup-processable)
6256             (command-execute func))
6257           (gnus-summary-remove-process-mark article)))))
6258   (gnus-summary-position-point))
6259
6260 (defun gnus-summary-toggle-truncation (&optional arg)
6261   "Toggle truncation of summary lines.
6262 With arg, turn line truncation on if arg is positive."
6263   (interactive "P")
6264   (setq truncate-lines
6265         (if (null arg) (not truncate-lines)
6266           (> (prefix-numeric-value arg) 0)))
6267   (redraw-display))
6268
6269 (defun gnus-summary-find-uncancelled ()
6270   "Return the number of an uncancelled article.
6271 The current article is considered, then following articles, then previous
6272 articles.  If all articles are cancelled then return a dummy 0."
6273   (let (found)
6274     (dolist (rev '(nil t))
6275       (unless found      ; don't demand the reverse list if we don't need it
6276         (let ((data (gnus-data-find-list
6277                      (gnus-summary-article-number) (gnus-data-list rev))))
6278           (while (and data (not found))
6279             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6280                 (setq found (gnus-data-number (car data))))
6281             (setq data (cdr data))))))
6282     (or found 0)))
6283
6284 (defun gnus-summary-reselect-current-group (&optional all rescan)
6285   "Exit and then reselect the current newsgroup.
6286 The prefix argument ALL means to select all articles."
6287   (interactive "P")
6288   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6289     (error "Ephemeral groups can't be reselected"))
6290   (let ((current-subject (gnus-summary-find-uncancelled))
6291         (group gnus-newsgroup-name))
6292     (setq gnus-newsgroup-begin nil)
6293     (gnus-summary-exit)
6294     ;; We have to adjust the point of group mode buffer because
6295     ;; point was moved to the next unread newsgroup by exiting.
6296     (gnus-summary-jump-to-group group)
6297     (when rescan
6298       (save-excursion
6299         (gnus-group-get-new-news-this-group 1)))
6300     (gnus-group-read-group all t)
6301     (gnus-summary-goto-subject current-subject nil t)))
6302
6303 (defun gnus-summary-rescan-group (&optional all)
6304   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6305   (interactive "P")
6306   (gnus-summary-reselect-current-group all t))
6307
6308 (defun gnus-summary-update-info (&optional non-destructive)
6309   (save-excursion
6310     (let ((group gnus-newsgroup-name))
6311       (when group
6312         (when gnus-newsgroup-kill-headers
6313           (setq gnus-newsgroup-killed
6314                 (gnus-compress-sequence
6315                  (gnus-sorted-union
6316                   (gnus-list-range-intersection
6317                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6318                   gnus-newsgroup-unreads)
6319                  t)))
6320         (unless (listp (cdr gnus-newsgroup-killed))
6321           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6322         (let ((headers gnus-newsgroup-headers))
6323           ;; Set the new ranges of read articles.
6324           (save-excursion
6325             (set-buffer gnus-group-buffer)
6326             (gnus-undo-force-boundary))
6327           (gnus-update-read-articles
6328            group (gnus-sorted-union
6329                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6330           ;; Set the current article marks.
6331           (let ((gnus-newsgroup-scored
6332                  (if (and (not gnus-save-score)
6333                           (not non-destructive))
6334                      nil
6335                    gnus-newsgroup-scored)))
6336             (save-excursion
6337               (gnus-update-marks)))
6338           ;; Do the cross-ref thing.
6339           (when gnus-use-cross-reference
6340             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6341           ;; Do not switch windows but change the buffer to work.
6342           (set-buffer gnus-group-buffer)
6343           (unless (gnus-ephemeral-group-p group)
6344             (gnus-group-update-group group)))))))
6345
6346 (defun gnus-summary-save-newsrc (&optional force)
6347   "Save the current number of read/marked articles in the dribble buffer.
6348 The dribble buffer will then be saved.
6349 If FORCE (the prefix), also save the .newsrc file(s)."
6350   (interactive "P")
6351   (gnus-summary-update-info t)
6352   (if force
6353       (gnus-save-newsrc-file)
6354     (gnus-dribble-save)))
6355
6356 (defun gnus-summary-exit (&optional temporary)
6357   "Exit reading current newsgroup, and then return to group selection mode.
6358 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6359   (interactive)
6360   (gnus-set-global-variables)
6361   (when (gnus-buffer-live-p gnus-article-buffer)
6362     (save-excursion
6363       (set-buffer gnus-article-buffer)
6364       (mm-destroy-parts gnus-article-mime-handles)
6365       ;; Set it to nil for safety reason.
6366       (setq gnus-article-mime-handle-alist nil)
6367       (setq gnus-article-mime-handles nil)))
6368   (gnus-kill-save-kill-buffer)
6369   (gnus-async-halt-prefetch)
6370   (let* ((group gnus-newsgroup-name)
6371          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6372          (gnus-group-is-exiting-p t)
6373          (mode major-mode)
6374          (group-point nil)
6375          (buf (current-buffer)))
6376     (unless quit-config
6377       ;; Do adaptive scoring, and possibly save score files.
6378       (when gnus-newsgroup-adaptive
6379         (gnus-score-adaptive))
6380       (when gnus-use-scoring
6381         (gnus-score-save)))
6382     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6383     ;; If we have several article buffers, we kill them at exit.
6384     (unless gnus-single-article-buffer
6385       (gnus-kill-buffer gnus-original-article-buffer)
6386       (setq gnus-article-current nil))
6387     (when gnus-use-cache
6388       (gnus-cache-possibly-remove-articles)
6389       (gnus-cache-save-buffers))
6390     (gnus-async-prefetch-remove-group group)
6391     (when gnus-suppress-duplicates
6392       (gnus-dup-enter-articles))
6393     (when gnus-use-trees
6394       (gnus-tree-close group))
6395     (when gnus-use-cache
6396       (gnus-cache-write-active))
6397     ;; Remove entries for this group.
6398     (nnmail-purge-split-history (gnus-group-real-name group))
6399     ;; Make all changes in this group permanent.
6400     (unless quit-config
6401       (gnus-run-hooks 'gnus-exit-group-hook)
6402       (gnus-summary-update-info))
6403     (gnus-close-group group)
6404     ;; Make sure where we were, and go to next newsgroup.
6405     (set-buffer gnus-group-buffer)
6406     (unless quit-config
6407       (gnus-group-jump-to-group group))
6408     (gnus-run-hooks 'gnus-summary-exit-hook)
6409     (unless (or quit-config
6410                 ;; If this group has disappeared from the summary
6411                 ;; buffer, don't skip forwards.
6412                 (not (string= group (gnus-group-group-name))))
6413       (gnus-group-next-unread-group 1))
6414     (setq group-point (point))
6415     (if temporary
6416         nil                             ;Nothing to do.
6417       ;; If we have several article buffers, we kill them at exit.
6418       (unless gnus-single-article-buffer
6419         (gnus-kill-buffer gnus-article-buffer)
6420         (gnus-kill-buffer gnus-original-article-buffer)
6421         (setq gnus-article-current nil))
6422       (set-buffer buf)
6423       (if (not gnus-kill-summary-on-exit)
6424           (progn
6425             (gnus-deaden-summary)
6426             (setq mode nil))
6427         ;; We set all buffer-local variables to nil.  It is unclear why
6428         ;; this is needed, but if we don't, buffer-local variables are
6429         ;; not garbage-collected, it seems.  This would the lead to en
6430         ;; ever-growing Emacs.
6431         (gnus-summary-clear-local-variables)
6432         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6433           (gnus-summary-clear-local-variables))
6434         (when (get-buffer gnus-article-buffer)
6435           (bury-buffer gnus-article-buffer))
6436         ;; We clear the global counterparts of the buffer-local
6437         ;; variables as well, just to be on the safe side.
6438         (set-buffer gnus-group-buffer)
6439         (gnus-summary-clear-local-variables)
6440         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6441           (gnus-summary-clear-local-variables))
6442         ;; Return to group mode buffer.
6443         (when (eq mode 'gnus-summary-mode)
6444           (gnus-kill-buffer buf)))
6445       (setq gnus-current-select-method gnus-select-method)
6446       (pop-to-buffer gnus-group-buffer)
6447       (if (not quit-config)
6448           (progn
6449             (goto-char group-point)
6450             (gnus-configure-windows 'group 'force))
6451         (gnus-handle-ephemeral-exit quit-config))
6452       ;; Clear the current group name.
6453       (unless quit-config
6454         (setq gnus-newsgroup-name nil)))))
6455
6456 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6457 (defun gnus-summary-exit-no-update (&optional no-questions)
6458   "Quit reading current newsgroup without updating read article info."
6459   (interactive)
6460   (let* ((group gnus-newsgroup-name)
6461          (gnus-group-is-exiting-p t)
6462          (quit-config (gnus-group-quit-config group)))
6463     (when (or no-questions
6464               gnus-expert-user
6465               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6466       (gnus-async-halt-prefetch)
6467       (mapcar 'funcall
6468               (delq 'gnus-summary-expire-articles
6469                     (copy-sequence gnus-summary-prepare-exit-hook)))
6470       (when (gnus-buffer-live-p gnus-article-buffer)
6471         (save-excursion
6472           (set-buffer gnus-article-buffer)
6473           (mm-destroy-parts gnus-article-mime-handles)
6474           ;; Set it to nil for safety reason.
6475           (setq gnus-article-mime-handle-alist nil)
6476           (setq gnus-article-mime-handles nil)))
6477       ;; If we have several article buffers, we kill them at exit.
6478       (unless gnus-single-article-buffer
6479         (gnus-kill-buffer gnus-article-buffer)
6480         (gnus-kill-buffer gnus-original-article-buffer)
6481         (setq gnus-article-current nil))
6482       (if (not gnus-kill-summary-on-exit)
6483           (gnus-deaden-summary)
6484         (gnus-close-group group)
6485         (gnus-summary-clear-local-variables)
6486         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6487           (gnus-summary-clear-local-variables))
6488         (set-buffer gnus-group-buffer)
6489         (gnus-summary-clear-local-variables)
6490         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6491           (gnus-summary-clear-local-variables))
6492         (when (get-buffer gnus-summary-buffer)
6493           (kill-buffer gnus-summary-buffer)))
6494       (unless gnus-single-article-buffer
6495         (setq gnus-article-current nil))
6496       (when gnus-use-trees
6497         (gnus-tree-close group))
6498       (gnus-async-prefetch-remove-group group)
6499       (when (get-buffer gnus-article-buffer)
6500         (bury-buffer gnus-article-buffer))
6501       ;; Return to the group buffer.
6502       (gnus-configure-windows 'group 'force)
6503       ;; Clear the current group name.
6504       (setq gnus-newsgroup-name nil)
6505       (unless (gnus-ephemeral-group-p group)
6506         (gnus-group-update-group group))
6507       (when (equal (gnus-group-group-name) group)
6508         (gnus-group-next-unread-group 1))
6509       (when quit-config
6510         (gnus-handle-ephemeral-exit quit-config)))))
6511
6512 (defun gnus-handle-ephemeral-exit (quit-config)
6513   "Handle movement when leaving an ephemeral group.
6514 The state which existed when entering the ephemeral is reset."
6515   (if (not (buffer-name (car quit-config)))
6516       (gnus-configure-windows 'group 'force)
6517     (set-buffer (car quit-config))
6518     (cond ((eq major-mode 'gnus-summary-mode)
6519            (gnus-set-global-variables))
6520           ((eq major-mode 'gnus-article-mode)
6521            (save-excursion
6522              ;; The `gnus-summary-buffer' variable may point
6523              ;; to the old summary buffer when using a single
6524              ;; article buffer.
6525              (unless (gnus-buffer-live-p gnus-summary-buffer)
6526                (set-buffer gnus-group-buffer))
6527              (set-buffer gnus-summary-buffer)
6528              (gnus-set-global-variables))))
6529     (if (or (eq (cdr quit-config) 'article)
6530             (eq (cdr quit-config) 'pick))
6531         (progn
6532           ;; The current article may be from the ephemeral group
6533           ;; thus it is best that we reload this article
6534           (gnus-summary-show-article)
6535           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6536               (gnus-configure-windows 'pick 'force)
6537             (gnus-configure-windows (cdr quit-config) 'force)))
6538       (gnus-configure-windows (cdr quit-config) 'force))
6539     (when (eq major-mode 'gnus-summary-mode)
6540       (gnus-summary-next-subject 1 nil t)
6541       (gnus-summary-recenter)
6542       (gnus-summary-position-point))))
6543
6544 ;;; Dead summaries.
6545
6546 (defvar gnus-dead-summary-mode-map nil)
6547
6548 (unless gnus-dead-summary-mode-map
6549   (setq gnus-dead-summary-mode-map (make-keymap))
6550   (suppress-keymap gnus-dead-summary-mode-map)
6551   (substitute-key-definition
6552    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6553   (dolist (key '("\C-d" "\r" "\177" [delete]))
6554     (define-key gnus-dead-summary-mode-map
6555       key 'gnus-summary-wake-up-the-dead))
6556   (dolist (key '("q" "Q"))
6557     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6558
6559 (defvar gnus-dead-summary-mode nil
6560   "Minor mode for Gnus summary buffers.")
6561
6562 (defun gnus-dead-summary-mode (&optional arg)
6563   "Minor mode for Gnus summary buffers."
6564   (interactive "P")
6565   (when (eq major-mode 'gnus-summary-mode)
6566     (make-local-variable 'gnus-dead-summary-mode)
6567     (setq gnus-dead-summary-mode
6568           (if (null arg) (not gnus-dead-summary-mode)
6569             (> (prefix-numeric-value arg) 0)))
6570     (when gnus-dead-summary-mode
6571       (gnus-add-minor-mode
6572        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6573
6574 (defun gnus-deaden-summary ()
6575   "Make the current summary buffer into a dead summary buffer."
6576   ;; Kill any previous dead summary buffer.
6577   (when (and gnus-dead-summary
6578              (buffer-name gnus-dead-summary))
6579     (save-excursion
6580       (set-buffer gnus-dead-summary)
6581       (when gnus-dead-summary-mode
6582         (kill-buffer (current-buffer)))))
6583   ;; Make this the current dead summary.
6584   (setq gnus-dead-summary (current-buffer))
6585   (gnus-dead-summary-mode 1)
6586   (let ((name (buffer-name)))
6587     (when (string-match "Summary" name)
6588       (rename-buffer
6589        (concat (substring name 0 (match-beginning 0)) "Dead "
6590                (substring name (match-beginning 0)))
6591        t)
6592       (bury-buffer))))
6593
6594 (defun gnus-kill-or-deaden-summary (buffer)
6595   "Kill or deaden the summary BUFFER."
6596   (save-excursion
6597     (when (and (buffer-name buffer)
6598                (not gnus-single-article-buffer))
6599       (save-excursion
6600         (set-buffer buffer)
6601         (gnus-kill-buffer gnus-article-buffer)
6602         (gnus-kill-buffer gnus-original-article-buffer)))
6603     (cond
6604      ;; Kill the buffer.
6605      (gnus-kill-summary-on-exit
6606       (when (and gnus-use-trees
6607                  (gnus-buffer-exists-p buffer))
6608         (save-excursion
6609           (set-buffer buffer)
6610           (gnus-tree-close gnus-newsgroup-name)))
6611       (gnus-kill-buffer buffer))
6612      ;; Deaden the buffer.
6613      ((gnus-buffer-exists-p buffer)
6614       (save-excursion
6615         (set-buffer buffer)
6616         (gnus-deaden-summary))))))
6617
6618 (defun gnus-summary-wake-up-the-dead (&rest args)
6619   "Wake up the dead summary buffer."
6620   (interactive)
6621   (gnus-dead-summary-mode -1)
6622   (let ((name (buffer-name)))
6623     (when (string-match "Dead " name)
6624       (rename-buffer
6625        (concat (substring name 0 (match-beginning 0))
6626                (substring name (match-end 0)))
6627        t)))
6628   (gnus-message 3 "This dead summary is now alive again"))
6629
6630 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6631 (defun gnus-summary-fetch-faq (&optional faq-dir)
6632   "Fetch the FAQ for the current group.
6633 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6634 in."
6635   (interactive
6636    (list
6637     (when current-prefix-arg
6638       (completing-read
6639        "FAQ dir: " (and (listp gnus-group-faq-directory)
6640                         (mapcar (lambda (file) (list file))
6641                                 gnus-group-faq-directory))))))
6642   (let (gnus-faq-buffer)
6643     (when (setq gnus-faq-buffer
6644                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6645       (gnus-configure-windows 'summary-faq))))
6646
6647 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6648 (defun gnus-summary-describe-group (&optional force)
6649   "Describe the current newsgroup."
6650   (interactive "P")
6651   (gnus-group-describe-group force gnus-newsgroup-name))
6652
6653 (defun gnus-summary-describe-briefly ()
6654   "Describe summary mode commands briefly."
6655   (interactive)
6656   (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")))
6657
6658 ;; Walking around group mode buffer from summary mode.
6659
6660 (defun gnus-summary-next-group (&optional no-article target-group backward)
6661   "Exit current newsgroup and then select next unread newsgroup.
6662 If prefix argument NO-ARTICLE is non-nil, no article is selected
6663 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6664 previous group instead."
6665   (interactive "P")
6666   ;; Stop pre-fetching.
6667   (gnus-async-halt-prefetch)
6668   (let ((current-group gnus-newsgroup-name)
6669         (current-buffer (current-buffer))
6670         entered)
6671     (while (not entered)
6672       ;; Then we find what group we are supposed to enter.
6673       (set-buffer gnus-group-buffer)
6674       (gnus-group-jump-to-group current-group)
6675       (setq target-group
6676             (or target-group
6677                 (if (eq gnus-keep-same-level 'best)
6678                     (gnus-summary-best-group gnus-newsgroup-name)
6679                   (gnus-summary-search-group backward gnus-keep-same-level))))
6680       (if (not target-group)
6681           ;; There are no further groups, so we return to the group
6682           ;; buffer.
6683           (progn
6684             (gnus-message 5 "Returning to the group buffer")
6685             (setq entered t)
6686             (when (gnus-buffer-live-p current-buffer)
6687               (set-buffer current-buffer)
6688               (gnus-summary-exit))
6689             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6690         ;; We try to enter the target group.
6691         (gnus-group-jump-to-group target-group)
6692         (let ((unreads (gnus-group-group-unread)))
6693           (if (and (or (eq t unreads)
6694                        (and unreads (not (zerop unreads))))
6695                    (progn
6696                      ;; Now we semi-exit this group to update Xrefs
6697                      ;; and all variables.  We can't do a real exit,
6698                      ;; because the window conf must remain the same
6699                      ;; in case the user is prompted for info, and we
6700                      ;; don't want the window conf to change before
6701                      ;; that...
6702                      (when (gnus-buffer-live-p current-buffer)
6703                        (set-buffer current-buffer)
6704                        (gnus-summary-exit t))
6705                      (gnus-summary-read-group
6706                       target-group nil no-article
6707                       (and (buffer-name current-buffer) current-buffer)
6708                       nil backward)))
6709               (setq entered t)
6710             (setq current-group target-group
6711                   target-group nil)))))))
6712
6713 (defun gnus-summary-prev-group (&optional no-article)
6714   "Exit current newsgroup and then select previous unread newsgroup.
6715 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6716   (interactive "P")
6717   (gnus-summary-next-group no-article nil t))
6718
6719 ;; Walking around summary lines.
6720
6721 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6722   "Go to the first subject satisfying any non-nil constraint.
6723 If UNREAD is non-nil, the article should be unread.
6724 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6725 If UNSEED is non-nil, the article should be unseen.
6726 Returns the article selected or nil if there are no matching articles."
6727   (interactive "P")
6728   (cond
6729    ;; Empty summary.
6730    ((null gnus-newsgroup-data)
6731     (gnus-message 3 "No articles in the group")
6732     nil)
6733    ;; Pick the first article.
6734    ((not (or unread undownloaded unseen))
6735     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6736     (gnus-data-number (car gnus-newsgroup-data)))
6737    ;; Find the first unread article.
6738    (t
6739     (let ((data gnus-newsgroup-data))
6740       (while (and data
6741                   (let ((num (gnus-data-number (car data))))
6742                     (or (memq num gnus-newsgroup-unfetched)
6743                         (not (or (and unread
6744                                       (memq num gnus-newsgroup-unreads))
6745                                  (and undownloaded
6746                                       (memq num gnus-newsgroup-undownloaded))
6747                                  (and unseen
6748                                       (memq num gnus-newsgroup-unseen)))))))
6749         (setq data (cdr data)))
6750       (prog1 
6751           (if data
6752               (progn
6753                 (goto-char (gnus-data-pos (car data)))
6754                 (gnus-data-number (car data)))
6755             (gnus-message 3 "No more%s articles"
6756                           (let* ((r (when unread " unread"))
6757                                  (d (when undownloaded " undownloaded"))
6758                                  (s (when unseen " unseen"))
6759                                  (l (delq nil (list r d s))))
6760                             (cond ((= 3 (length l))
6761                                    (concat r "," d ", or" s))
6762                                   ((= 2 (length l))
6763                                    (concat (car l) ", or" (cadr l)))
6764                                   ((= 1 (length l))
6765                                    (car l))
6766                                   (t
6767                                    ""))))
6768             nil
6769             )
6770         (gnus-summary-position-point))))))
6771
6772 (defun gnus-summary-next-subject (n &optional unread dont-display)
6773   "Go to next N'th summary line.
6774 If N is negative, go to the previous N'th subject line.
6775 If UNREAD is non-nil, only unread articles are selected.
6776 The difference between N and the actual number of steps taken is
6777 returned."
6778   (interactive "p")
6779   (let ((backward (< n 0))
6780         (n (abs n)))
6781     (while (and (> n 0)
6782                 (if backward
6783                     (gnus-summary-find-prev unread)
6784                   (gnus-summary-find-next unread)))
6785       (unless (zerop (setq n (1- n)))
6786         (gnus-summary-show-thread)))
6787     (when (/= 0 n)
6788       (gnus-message 7 "No more%s articles"
6789                     (if unread " unread" "")))
6790     (unless dont-display
6791       (gnus-summary-recenter)
6792       (gnus-summary-position-point))
6793     n))
6794
6795 (defun gnus-summary-next-unread-subject (n)
6796   "Go to next N'th unread summary line."
6797   (interactive "p")
6798   (gnus-summary-next-subject n t))
6799
6800 (defun gnus-summary-prev-subject (n &optional unread)
6801   "Go to previous N'th summary line.
6802 If optional argument UNREAD is non-nil, only unread article is selected."
6803   (interactive "p")
6804   (gnus-summary-next-subject (- n) unread))
6805
6806 (defun gnus-summary-prev-unread-subject (n)
6807   "Go to previous N'th unread summary line."
6808   (interactive "p")
6809   (gnus-summary-next-subject (- n) t))
6810
6811 (defun gnus-summary-goto-subjects (articles)
6812   "Insert the subject header for ARTICLES in the current buffer."
6813   (save-excursion
6814     (dolist (article articles)
6815       (gnus-summary-goto-subject article t)))
6816   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6817   (gnus-summary-position-point))
6818  
6819 (defun gnus-summary-goto-subject (article &optional force silent)
6820   "Go the subject line of ARTICLE.
6821 If FORCE, also allow jumping to articles not currently shown."
6822   (interactive "nArticle number: ")
6823   (unless (numberp article)
6824     (error "Article %s is not a number" article))
6825   (let ((b (point))
6826         (data (gnus-data-find article)))
6827     ;; We read in the article if we have to.
6828     (and (not data)
6829          force
6830          (gnus-summary-insert-subject
6831           article
6832           (if (or (numberp force) (vectorp force)) force)
6833           t)
6834          (setq data (gnus-data-find article)))
6835     (goto-char b)
6836     (if (not data)
6837         (progn
6838           (unless silent
6839             (gnus-message 3 "Can't find article %d" article))
6840           nil)
6841       (let ((pt (gnus-data-pos data)))
6842         (goto-char pt)
6843         (gnus-summary-set-article-display-arrow pt))
6844       (gnus-summary-position-point)
6845       article)))
6846
6847 ;; Walking around summary lines with displaying articles.
6848
6849 (defun gnus-summary-expand-window (&optional arg)
6850   "Make the summary buffer take up the entire Emacs frame.
6851 Given a prefix, will force an `article' buffer configuration."
6852   (interactive "P")
6853   (if arg
6854       (gnus-configure-windows 'article 'force)
6855     (gnus-configure-windows 'summary 'force)))
6856
6857 (defun gnus-summary-display-article (article &optional all-header)
6858   "Display ARTICLE in article buffer."
6859   (when (gnus-buffer-live-p gnus-article-buffer)
6860     (with-current-buffer gnus-article-buffer
6861       (mm-enable-multibyte)))
6862   (gnus-set-global-variables)
6863   (when (gnus-buffer-live-p gnus-article-buffer)
6864     (with-current-buffer gnus-article-buffer
6865       (setq gnus-article-charset gnus-newsgroup-charset)
6866       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6867       (mm-enable-multibyte)))
6868   (if (null article)
6869       nil
6870     (prog1
6871         (if gnus-summary-display-article-function
6872             (funcall gnus-summary-display-article-function article all-header)
6873           (gnus-article-prepare article all-header))
6874       (gnus-run-hooks 'gnus-select-article-hook)
6875       (when (and gnus-current-article
6876                  (not (zerop gnus-current-article)))
6877         (gnus-summary-goto-subject gnus-current-article))
6878       (gnus-summary-recenter)
6879       (when (and gnus-use-trees gnus-show-threads)
6880         (gnus-possibly-generate-tree article)
6881         (gnus-highlight-selected-tree article))
6882       ;; Successfully display article.
6883       (gnus-article-set-window-start
6884        (cdr (assq article gnus-newsgroup-bookmarks))))))
6885
6886 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6887   "Select the current article.
6888 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6889 non-nil, the article will be re-fetched even if it already present in
6890 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6891 be displayed."
6892   ;; Make sure we are in the summary buffer to work around bbdb bug.
6893   (unless (eq major-mode 'gnus-summary-mode)
6894     (set-buffer gnus-summary-buffer))
6895   (let ((article (or article (gnus-summary-article-number)))
6896         (all-headers (not (not all-headers))) ;Must be t or nil.
6897         gnus-summary-display-article-function)
6898     (and (not pseudo)
6899          (gnus-summary-article-pseudo-p article)
6900          (error "This is a pseudo-article"))
6901     (save-excursion
6902       (set-buffer gnus-summary-buffer)
6903       (if (or (and gnus-single-article-buffer
6904                    (or (null gnus-current-article)
6905                        (null gnus-article-current)
6906                        (null (get-buffer gnus-article-buffer))
6907                        (not (eq article (cdr gnus-article-current)))
6908                        (not (equal (car gnus-article-current)
6909                                    gnus-newsgroup-name))))
6910               (and (not gnus-single-article-buffer)
6911                    (or (null gnus-current-article)
6912                        (not (eq gnus-current-article article))))
6913               force)
6914           ;; The requested article is different from the current article.
6915           (progn
6916             (gnus-summary-display-article article all-headers)
6917             (when (gnus-buffer-live-p gnus-article-buffer)
6918               (with-current-buffer gnus-article-buffer
6919                 (if (not gnus-article-decoded-p) ;; a local variable
6920                     (mm-disable-multibyte))))
6921             (gnus-article-set-window-start
6922              (cdr (assq article gnus-newsgroup-bookmarks)))
6923             article)
6924         'old))))
6925
6926 (defun gnus-summary-force-verify-and-decrypt ()
6927   "Display buttons for signed/encrypted parts and verify/decrypt them."
6928   (interactive)
6929   (let ((mm-verify-option 'known)
6930         (mm-decrypt-option 'known)
6931         (gnus-buttonized-mime-types (append (list "multipart/signed"
6932                                                   "multipart/encrypted")
6933                                             gnus-buttonized-mime-types)))
6934     (gnus-summary-select-article nil 'force)))
6935
6936 (defun gnus-summary-set-current-mark (&optional current-mark)
6937   "Obsolete function."
6938   nil)
6939
6940 (defun gnus-summary-next-article (&optional unread subject backward push)
6941   "Select the next article.
6942 If UNREAD, only unread articles are selected.
6943 If SUBJECT, only articles with SUBJECT are selected.
6944 If BACKWARD, the previous article is selected instead of the next."
6945   (interactive "P")
6946   (cond
6947    ;; Is there such an article?
6948    ((and (gnus-summary-search-forward unread subject backward)
6949          (or (gnus-summary-display-article (gnus-summary-article-number))
6950              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6951     (gnus-summary-position-point))
6952    ;; If not, we try the first unread, if that is wanted.
6953    ((and subject
6954          gnus-auto-select-same
6955          (gnus-summary-first-unread-article))
6956     (gnus-summary-position-point)
6957     (gnus-message 6 "Wrapped"))
6958    ;; Try to get next/previous article not displayed in this group.
6959    ((and gnus-auto-extend-newsgroup
6960          (not unread) (not subject))
6961     (gnus-summary-goto-article
6962      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6963      nil (count-lines (point-min) (point))))
6964    ;; Go to next/previous group.
6965    (t
6966     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6967       (gnus-summary-jump-to-group gnus-newsgroup-name))
6968     (let ((cmd last-command-char)
6969           (point
6970            (save-excursion
6971              (set-buffer gnus-group-buffer)
6972              (point)))
6973           (group
6974            (if (eq gnus-keep-same-level 'best)
6975                (gnus-summary-best-group gnus-newsgroup-name)
6976              (gnus-summary-search-group backward gnus-keep-same-level))))
6977       ;; For some reason, the group window gets selected.  We change
6978       ;; it back.
6979       (select-window (get-buffer-window (current-buffer)))
6980       ;; Select next unread newsgroup automagically.
6981       (cond
6982        ((or (not gnus-auto-select-next)
6983             (not cmd))
6984         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6985        ((or (eq gnus-auto-select-next 'quietly)
6986             (and (eq gnus-auto-select-next 'slightly-quietly)
6987                  push)
6988             (and (eq gnus-auto-select-next 'almost-quietly)
6989                  (gnus-summary-last-article-p)))
6990         ;; Select quietly.
6991         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6992             (gnus-summary-exit)
6993           (gnus-message 7 "No more%s articles (%s)..."
6994                         (if unread " unread" "")
6995                         (if group (concat "selecting " group)
6996                           "exiting"))
6997           (gnus-summary-next-group nil group backward)))
6998        (t
6999         (when (gnus-key-press-event-p last-input-event)
7000           (gnus-summary-walk-group-buffer
7001            gnus-newsgroup-name cmd unread backward point))))))))
7002
7003 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7004   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7005                       (?\C-p (gnus-group-prev-unread-group 1))))
7006         (cursor-in-echo-area t)
7007         keve key group ended prompt)
7008     (save-excursion
7009       (set-buffer gnus-group-buffer)
7010       (goto-char start)
7011       (setq group
7012             (if (eq gnus-keep-same-level 'best)
7013                 (gnus-summary-best-group gnus-newsgroup-name)
7014               (gnus-summary-search-group backward gnus-keep-same-level))))
7015     (while (not ended)
7016       (setq prompt
7017             (format
7018              "No more%s articles%s " (if unread " unread" "")
7019              (if (and group
7020                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7021                  (format " (Type %s for %s [%s])"
7022                          (single-key-description cmd) group
7023                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7024                (format " (Type %s to exit %s)"
7025                        (single-key-description cmd)
7026                        gnus-newsgroup-name))))
7027       ;; Confirm auto selection.
7028       (setq key (car (setq keve (gnus-read-event-char prompt)))
7029             ended t)
7030       (cond
7031        ((assq key keystrokes)
7032         (let ((obuf (current-buffer)))
7033           (switch-to-buffer gnus-group-buffer)
7034           (when group
7035             (gnus-group-jump-to-group group))
7036           (eval (cadr (assq key keystrokes)))
7037           (setq group (gnus-group-group-name))
7038           (switch-to-buffer obuf))
7039         (setq ended nil))
7040        ((equal key cmd)
7041         (if (or (not group)
7042                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7043             (gnus-summary-exit)
7044           (gnus-summary-next-group nil group backward)))
7045        (t
7046         (push (cdr keve) unread-command-events))))))
7047
7048 (defun gnus-summary-next-unread-article ()
7049   "Select unread article after current one."
7050   (interactive)
7051   (gnus-summary-next-article
7052    (or (not (eq gnus-summary-goto-unread 'never))
7053        (gnus-summary-last-article-p (gnus-summary-article-number)))
7054    (and gnus-auto-select-same
7055         (gnus-summary-article-subject))))
7056
7057 (defun gnus-summary-prev-article (&optional unread subject)
7058   "Select the article after the current one.
7059 If UNREAD is non-nil, only unread articles are selected."
7060   (interactive "P")
7061   (gnus-summary-next-article unread subject t))
7062
7063 (defun gnus-summary-prev-unread-article ()
7064   "Select unread article before current one."
7065   (interactive)
7066   (gnus-summary-prev-article
7067    (or (not (eq gnus-summary-goto-unread 'never))
7068        (gnus-summary-first-article-p (gnus-summary-article-number)))
7069    (and gnus-auto-select-same
7070         (gnus-summary-article-subject))))
7071
7072 (defun gnus-summary-next-page (&optional lines circular stop)
7073   "Show next page of the selected article.
7074 If at the end of the current article, select the next article.
7075 LINES says how many lines should be scrolled up.
7076
7077 If CIRCULAR is non-nil, go to the start of the article instead of
7078 selecting the next article when reaching the end of the current
7079 article.
7080
7081 If STOP is non-nil, just stop when reaching the end of the message."
7082   (interactive "P")
7083   (setq gnus-summary-buffer (current-buffer))
7084   (gnus-set-global-variables)
7085   (let ((article (gnus-summary-article-number))
7086         (article-window (get-buffer-window gnus-article-buffer t))
7087         endp)
7088     ;; If the buffer is empty, we have no article.
7089     (unless article
7090       (error "No article to select"))
7091     (gnus-configure-windows 'article)
7092     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7093         (if (and (eq gnus-summary-goto-unread 'never)
7094                  (not (gnus-summary-last-article-p article)))
7095             (gnus-summary-next-article)
7096           (gnus-summary-next-unread-article))
7097       (if (or (null gnus-current-article)
7098               (null gnus-article-current)
7099               (/= article (cdr gnus-article-current))
7100               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7101           ;; Selected subject is different from current article's.
7102           (gnus-summary-display-article article)
7103         (when article-window
7104           (gnus-eval-in-buffer-window gnus-article-buffer
7105             (setq endp (or (gnus-article-next-page lines)
7106                            (gnus-article-only-boring-p))))
7107           (when endp
7108             (cond (stop
7109                    (gnus-message 3 "End of message"))
7110                   (circular
7111                    (gnus-summary-beginning-of-article))
7112                   (lines
7113                    (gnus-message 3 "End of message"))
7114                   ((null lines)
7115                    (if (and (eq gnus-summary-goto-unread 'never)
7116                             (not (gnus-summary-last-article-p article)))
7117                        (gnus-summary-next-article)
7118                      (gnus-summary-next-unread-article))))))))
7119     (gnus-summary-recenter)
7120     (gnus-summary-position-point)))
7121
7122 (defun gnus-summary-prev-page (&optional lines move)
7123   "Show previous page of selected article.
7124 Argument LINES specifies lines to be scrolled down.
7125 If MOVE, move to the previous unread article if point is at
7126 the beginning of the buffer."
7127   (interactive "P")
7128   (let ((article (gnus-summary-article-number))
7129         (article-window (get-buffer-window gnus-article-buffer t))
7130         endp)
7131     (gnus-configure-windows 'article)
7132     (if (or (null gnus-current-article)
7133             (null gnus-article-current)
7134             (/= article (cdr gnus-article-current))
7135             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7136         ;; Selected subject is different from current article's.
7137         (gnus-summary-display-article article)
7138       (gnus-summary-recenter)
7139       (when article-window
7140         (gnus-eval-in-buffer-window gnus-article-buffer
7141           (setq endp (gnus-article-prev-page lines)))
7142         (when (and move endp)
7143           (cond (lines
7144                  (gnus-message 3 "Beginning of message"))
7145                 ((null lines)
7146                  (if (and (eq gnus-summary-goto-unread 'never)
7147                           (not (gnus-summary-first-article-p article)))
7148                      (gnus-summary-prev-article)
7149                    (gnus-summary-prev-unread-article))))))))
7150   (gnus-summary-position-point))
7151
7152 (defun gnus-summary-prev-page-or-article (&optional lines)
7153   "Show previous page of selected article.
7154 Argument LINES specifies lines to be scrolled down.
7155 If at the beginning of the article, go to the next article."
7156   (interactive "P")
7157   (gnus-summary-prev-page lines t))
7158
7159 (defun gnus-summary-scroll-up (lines)
7160   "Scroll up (or down) one line current article.
7161 Argument LINES specifies lines to be scrolled up (or down if negative)."
7162   (interactive "p")
7163   (gnus-configure-windows 'article)
7164   (gnus-summary-show-thread)
7165   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7166     (gnus-eval-in-buffer-window gnus-article-buffer
7167       (cond ((> lines 0)
7168              (when (gnus-article-next-page lines)
7169                (gnus-message 3 "End of message")))
7170             ((< lines 0)
7171              (gnus-article-prev-page (- lines))))))
7172   (gnus-summary-recenter)
7173   (gnus-summary-position-point))
7174
7175 (defun gnus-summary-scroll-down (lines)
7176   "Scroll down (or up) one line current article.
7177 Argument LINES specifies lines to be scrolled down (or up if negative)."
7178   (interactive "p")
7179   (gnus-summary-scroll-up (- lines)))
7180
7181 (defun gnus-summary-next-same-subject ()
7182   "Select next article which has the same subject as current one."
7183   (interactive)
7184   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7185
7186 (defun gnus-summary-prev-same-subject ()
7187   "Select previous article which has the same subject as current one."
7188   (interactive)
7189   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7190
7191 (defun gnus-summary-next-unread-same-subject ()
7192   "Select next unread article which has the same subject as current one."
7193   (interactive)
7194   (gnus-summary-next-article t (gnus-summary-article-subject)))
7195
7196 (defun gnus-summary-prev-unread-same-subject ()
7197   "Select previous unread article which has the same subject as current one."
7198   (interactive)
7199   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7200
7201 (defun gnus-summary-first-unread-article ()
7202   "Select the first unread article.
7203 Return nil if there are no unread articles."
7204   (interactive)
7205   (prog1
7206       (when (gnus-summary-first-subject t)
7207         (gnus-summary-show-thread)
7208         (gnus-summary-first-subject t)
7209         (gnus-summary-display-article (gnus-summary-article-number)))
7210     (gnus-summary-position-point)))
7211
7212 (defun gnus-summary-first-unread-subject ()
7213   "Place the point on the subject line of the first unread article.
7214 Return nil if there are no unread articles."
7215   (interactive)
7216   (prog1
7217       (when (gnus-summary-first-subject t)
7218         (gnus-summary-show-thread)
7219         (gnus-summary-first-subject t))
7220     (gnus-summary-position-point)))
7221
7222 (defun gnus-summary-first-unseen-subject ()
7223   "Place the point on the subject line of the first unseen article.
7224 Return nil if there are no unseen articles."
7225   (interactive)
7226   (prog1
7227       (when (gnus-summary-first-subject nil nil t)
7228         (gnus-summary-show-thread)
7229         (gnus-summary-first-subject nil nil t))
7230     (gnus-summary-position-point)))
7231
7232 (defun gnus-summary-first-unseen-or-unread-subject ()
7233   "Place the point on the subject line of the first unseen article or,
7234 if all article have been seen, on the subject line of the first unread
7235 article."
7236   (interactive)
7237   (prog1
7238       (unless (when (gnus-summary-first-subject nil nil t)
7239                 (gnus-summary-show-thread)
7240                 (gnus-summary-first-subject nil nil t))
7241         (when (gnus-summary-first-subject t)
7242           (gnus-summary-show-thread)
7243           (gnus-summary-first-subject t)))
7244     (gnus-summary-position-point)))
7245
7246 (defun gnus-summary-first-article ()
7247   "Select the first article.
7248 Return nil if there are no articles."
7249   (interactive)
7250   (prog1
7251       (when (gnus-summary-first-subject)
7252         (gnus-summary-show-thread)
7253         (gnus-summary-first-subject)
7254         (gnus-summary-display-article (gnus-summary-article-number)))
7255     (gnus-summary-position-point)))
7256
7257 (defun gnus-summary-best-unread-article (&optional arg)
7258   "Select the unread article with the highest score.
7259 If given a prefix argument, select the next unread article that has a
7260 score higher than the default score."
7261   (interactive "P")
7262   (let ((article (if arg
7263                      (gnus-summary-better-unread-subject)
7264                    (gnus-summary-best-unread-subject))))
7265     (if article
7266         (gnus-summary-goto-article article)
7267       (error "No unread articles"))))
7268
7269 (defun gnus-summary-best-unread-subject ()
7270   "Select the unread subject with the highest score."
7271   (interactive)
7272   (let ((best -1000000)
7273         (data gnus-newsgroup-data)
7274         article score)
7275     (while data
7276       (and (gnus-data-unread-p (car data))
7277            (> (setq score
7278                     (gnus-summary-article-score (gnus-data-number (car data))))
7279               best)
7280            (setq best score
7281                  article (gnus-data-number (car data))))
7282       (setq data (cdr data)))
7283     (when article
7284       (gnus-summary-goto-subject article))
7285     (gnus-summary-position-point)
7286     article))
7287
7288 (defun gnus-summary-better-unread-subject ()
7289   "Select the first unread subject that has a score over the default score."
7290   (interactive)
7291   (let ((data gnus-newsgroup-data)
7292         article score)
7293     (while (and (setq article (gnus-data-number (car data)))
7294                 (or (gnus-data-read-p (car data))
7295                     (not (> (gnus-summary-article-score article)
7296                             gnus-summary-default-score))))
7297       (setq data (cdr data)))
7298     (when article
7299       (gnus-summary-goto-subject article))
7300     (gnus-summary-position-point)
7301     article))
7302
7303 (defun gnus-summary-last-subject ()
7304   "Go to the last displayed subject line in the group."
7305   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7306     (when article
7307       (gnus-summary-goto-subject article))))
7308
7309 (defun gnus-summary-goto-article (article &optional all-headers force)
7310   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7311 If ALL-HEADERS is non-nil, no header lines are hidden.
7312 If FORCE, go to the article even if it isn't displayed.  If FORCE
7313 is a number, it is the line the article is to be displayed on."
7314   (interactive
7315    (list
7316     (completing-read
7317      "Article number or Message-ID: "
7318      (mapcar (lambda (number) (list (int-to-string number)))
7319              gnus-newsgroup-limit))
7320     current-prefix-arg
7321     t))
7322   (prog1
7323       (if (and (stringp article)
7324                (string-match "@" article))
7325           (gnus-summary-refer-article article)
7326         (when (stringp article)
7327           (setq article (string-to-number article)))
7328         (if (gnus-summary-goto-subject article force)
7329             (gnus-summary-display-article article all-headers)
7330           (gnus-message 4 "Couldn't go to article %s" article) nil))
7331     (gnus-summary-position-point)))
7332
7333 (defun gnus-summary-goto-last-article ()
7334   "Go to the previously read article."
7335   (interactive)
7336   (prog1
7337       (when gnus-last-article
7338         (gnus-summary-goto-article gnus-last-article nil t))
7339     (gnus-summary-position-point)))
7340
7341 (defun gnus-summary-pop-article (number)
7342   "Pop one article off the history and go to the previous.
7343 NUMBER articles will be popped off."
7344   (interactive "p")
7345   (let (to)
7346     (setq gnus-newsgroup-history
7347           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7348     (if to
7349         (gnus-summary-goto-article (car to) nil t)
7350       (error "Article history empty")))
7351   (gnus-summary-position-point))
7352
7353 ;; Summary commands and functions for limiting the summary buffer.
7354
7355 (defun gnus-summary-limit-to-articles (n)
7356   "Limit the summary buffer to the next N articles.
7357 If not given a prefix, use the process marked articles instead."
7358   (interactive "P")
7359   (prog1
7360       (let ((articles (gnus-summary-work-articles n)))
7361         (setq gnus-newsgroup-processable nil)
7362         (gnus-summary-limit articles))
7363     (gnus-summary-position-point)))
7364
7365 (defun gnus-summary-pop-limit (&optional total)
7366   "Restore the previous limit.
7367 If given a prefix, remove all limits."
7368   (interactive "P")
7369   (when total
7370     (setq gnus-newsgroup-limits
7371           (list (mapcar (lambda (h) (mail-header-number h))
7372                         gnus-newsgroup-headers))))
7373   (unless gnus-newsgroup-limits
7374     (error "No limit to pop"))
7375   (prog1
7376       (gnus-summary-limit nil 'pop)
7377     (gnus-summary-position-point)))
7378
7379 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7380   "Limit the summary buffer to articles that have subjects that match a regexp.
7381 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7382   (interactive
7383    (list (read-string (if current-prefix-arg
7384                           "Exclude subject (regexp): "
7385                         "Limit to subject (regexp): "))
7386          nil current-prefix-arg))
7387   (unless header
7388     (setq header "subject"))
7389   (when (not (equal "" subject))
7390     (prog1
7391         (let ((articles (gnus-summary-find-matching
7392                          (or header "subject") subject 'all nil nil
7393                          not-matching)))
7394           (unless articles
7395             (error "Found no matches for \"%s\"" subject))
7396           (gnus-summary-limit articles))
7397       (gnus-summary-position-point))))
7398
7399 (defun gnus-summary-limit-to-author (from &optional not-matching)
7400   "Limit the summary buffer to articles that have authors that match a regexp.
7401 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7402   (interactive
7403    (list (read-string (if current-prefix-arg
7404                           "Exclude author (regexp): "
7405                         "Limit to author (regexp): "))
7406          current-prefix-arg))
7407   (gnus-summary-limit-to-subject from "from" not-matching))
7408
7409 (defun gnus-summary-limit-to-age (age &optional younger-p)
7410   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7411 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7412 articles that are younger than AGE days."
7413   (interactive
7414    (let ((younger current-prefix-arg)
7415          (days-got nil)
7416          days)
7417      (while (not days-got)
7418        (setq days (if younger
7419                       (read-string "Limit to articles younger than (in days, older when negative): ")
7420                     (read-string
7421                      "Limit to articles older than (in days, younger when negative): ")))
7422        (when (> (length days) 0)
7423          (setq days (read days)))
7424        (if (numberp days)
7425            (progn
7426              (setq days-got t)
7427              (if (< days 0)
7428                  (progn
7429                    (setq younger (not younger))
7430                    (setq days (* days -1)))))
7431          (message "Please enter a number.")
7432          (sleep-for 1)))
7433      (list days younger)))
7434   (prog1
7435       (let ((data gnus-newsgroup-data)
7436             (cutoff (days-to-time age))
7437             articles d date is-younger)
7438         (while (setq d (pop data))
7439           (when (and (vectorp (gnus-data-header d))
7440                      (setq date (mail-header-date (gnus-data-header d))))
7441             (setq is-younger (time-less-p
7442                               (time-since (condition-case ()
7443                                               (date-to-time date)
7444                                             (error '(0 0))))
7445                               cutoff))
7446             (when (if younger-p
7447                       is-younger
7448                     (not is-younger))
7449               (push (gnus-data-number d) articles))))
7450         (gnus-summary-limit (nreverse articles)))
7451     (gnus-summary-position-point)))
7452
7453 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7454   "Limit the summary buffer to articles that match an 'extra' header."
7455   (interactive
7456    (let ((header
7457           (intern
7458            (gnus-completing-read-with-default
7459             (symbol-name (car gnus-extra-headers))
7460             (if current-prefix-arg
7461                 "Exclude extra header:"
7462               "Limit extra header:")
7463             (mapcar (lambda (x)
7464                       (cons (symbol-name x) x))
7465                     gnus-extra-headers)
7466             nil
7467             t))))
7468      (list header
7469            (read-string (format "%s header %s (regexp): "
7470                                 (if current-prefix-arg "Exclude" "Limit to")
7471                                 header))
7472            current-prefix-arg)))
7473   (when (not (equal "" regexp))
7474     (prog1
7475         (let ((articles (gnus-summary-find-matching
7476                          (cons 'extra header) regexp 'all nil nil
7477                          not-matching)))
7478           (unless articles
7479             (error "Found no matches for \"%s\"" regexp))
7480           (gnus-summary-limit articles))
7481       (gnus-summary-position-point))))
7482
7483 (defun gnus-summary-limit-to-display-predicate ()
7484   "Limit the summary buffer to the predicated in the `display' group parameter."
7485   (interactive)
7486   (unless gnus-newsgroup-display
7487     (error "There is no `display' group parameter"))
7488   (let (articles)
7489     (dolist (number gnus-newsgroup-articles)
7490       (when (funcall gnus-newsgroup-display)
7491         (push number articles)))
7492     (gnus-summary-limit articles))
7493   (gnus-summary-position-point))
7494
7495 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7496 (make-obsolete
7497  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7498
7499 (defun gnus-summary-limit-to-unread (&optional all)
7500   "Limit the summary buffer to articles that are not marked as read.
7501 If ALL is non-nil, limit strictly to unread articles."
7502   (interactive "P")
7503   (if all
7504       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7505     (gnus-summary-limit-to-marks
7506      ;; Concat all the marks that say that an article is read and have
7507      ;; those removed.
7508      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7509            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7510            gnus-low-score-mark gnus-expirable-mark
7511            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7512            gnus-duplicate-mark gnus-souped-mark)
7513      'reverse)))
7514
7515 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7516 (make-obsolete 'gnus-summary-delete-marked-with
7517                'gnus-summary-limit-exclude-marks)
7518
7519 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7520   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7521 If REVERSE, limit the summary buffer to articles that are marked
7522 with MARKS.  MARKS can either be a string of marks or a list of marks.
7523 Returns how many articles were removed."
7524   (interactive "sMarks: ")
7525   (gnus-summary-limit-to-marks marks t))
7526
7527 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7528   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7529 If REVERSE (the prefix), limit the summary buffer to articles that are
7530 not marked with MARKS.  MARKS can either be a string of marks or a
7531 list of marks.
7532 Returns how many articles were removed."
7533   (interactive "sMarks: \nP")
7534   (prog1
7535       (let ((data gnus-newsgroup-data)
7536             (marks (if (listp marks) marks
7537                      (append marks nil))) ; Transform to list.
7538             articles)
7539         (while data
7540           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7541                   (memq (gnus-data-mark (car data)) marks))
7542             (push (gnus-data-number (car data)) articles))
7543           (setq data (cdr data)))
7544         (gnus-summary-limit articles))
7545     (gnus-summary-position-point)))
7546
7547 (defun gnus-summary-limit-to-score (score)
7548   "Limit to articles with score at or above SCORE."
7549   (interactive "NLimit to articles with score of at least: ")
7550   (let ((data gnus-newsgroup-data)
7551         articles)
7552     (while data
7553       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7554                 score)
7555         (push (gnus-data-number (car data)) articles))
7556       (setq data (cdr data)))
7557     (prog1
7558         (gnus-summary-limit articles)
7559       (gnus-summary-position-point))))
7560
7561 (defun gnus-summary-limit-to-unseen ()
7562   "Limit to unseen articles."
7563   (interactive)
7564   (prog1
7565       (gnus-summary-limit gnus-newsgroup-unseen)
7566     (gnus-summary-position-point)))
7567
7568 (defun gnus-summary-limit-include-thread (id)
7569   "Display all the hidden articles that is in the thread with ID in it.
7570 When called interactively, ID is the Message-ID of the current
7571 article."
7572   (interactive (list (mail-header-id (gnus-summary-article-header))))
7573   (let ((articles (gnus-articles-in-thread
7574                    (gnus-id-to-thread (gnus-root-id id)))))
7575     (prog1
7576         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7577       (gnus-summary-limit-include-matching-articles
7578        "subject"
7579        (regexp-quote (gnus-simplify-subject-re
7580                       (mail-header-subject (gnus-id-to-header id)))))
7581       (gnus-summary-position-point))))
7582
7583 (defun gnus-summary-limit-include-matching-articles (header regexp)
7584   "Display all the hidden articles that have HEADERs that match REGEXP."
7585   (interactive (list (read-string "Match on header: ")
7586                      (read-string "Regexp: ")))
7587   (let ((articles (gnus-find-matching-articles header regexp)))
7588     (prog1
7589         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7590       (gnus-summary-position-point))))
7591
7592 (defun gnus-summary-insert-dormant-articles ()
7593   "Insert all the dormat articles for this group into the current buffer."
7594   (interactive)
7595   (let ((gnus-verbose (max 6 gnus-verbose)))
7596     (if (not gnus-newsgroup-dormant)
7597         (gnus-message 3 "No cached articles for this group")
7598       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7599
7600 (defun gnus-summary-limit-include-dormant ()
7601   "Display all the hidden articles that are marked as dormant.
7602 Note that this command only works on a subset of the articles currently
7603 fetched for this group."
7604   (interactive)
7605   (unless gnus-newsgroup-dormant
7606     (error "There are no dormant articles in this group"))
7607   (prog1
7608       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7609     (gnus-summary-position-point)))
7610
7611 (defun gnus-summary-limit-exclude-dormant ()
7612   "Hide all dormant articles."
7613   (interactive)
7614   (prog1
7615       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7616     (gnus-summary-position-point)))
7617
7618 (defun gnus-summary-limit-exclude-childless-dormant ()
7619   "Hide all dormant articles that have no children."
7620   (interactive)
7621   (let ((data (gnus-data-list t))
7622         articles d children)
7623     ;; Find all articles that are either not dormant or have
7624     ;; children.
7625     (while (setq d (pop data))
7626       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7627                 (and (setq children
7628                            (gnus-article-children (gnus-data-number d)))
7629                      (let (found)
7630                        (while children
7631                          (when (memq (car children) articles)
7632                            (setq children nil
7633                                  found t))
7634                          (pop children))
7635                        found)))
7636         (push (gnus-data-number d) articles)))
7637     ;; Do the limiting.
7638     (prog1
7639         (gnus-summary-limit articles)
7640       (gnus-summary-position-point))))
7641
7642 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7643   "Mark all unread excluded articles as read.
7644 If ALL, mark even excluded ticked and dormants as read."
7645   (interactive "P")
7646   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7647   (let ((articles (gnus-sorted-ndifference
7648                    (sort
7649                     (mapcar (lambda (h) (mail-header-number h))
7650                             gnus-newsgroup-headers)
7651                     '<)
7652                    gnus-newsgroup-limit))
7653         article)
7654     (setq gnus-newsgroup-unreads
7655           (gnus-sorted-intersection gnus-newsgroup-unreads
7656                                     gnus-newsgroup-limit))
7657     (if all
7658         (setq gnus-newsgroup-dormant nil
7659               gnus-newsgroup-marked nil
7660               gnus-newsgroup-reads
7661               (nconc
7662                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7663                gnus-newsgroup-reads))
7664       (while (setq article (pop articles))
7665         (unless (or (memq article gnus-newsgroup-dormant)
7666                     (memq article gnus-newsgroup-marked))
7667           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7668
7669 (defun gnus-summary-limit (articles &optional pop)
7670   (if pop
7671       ;; We pop the previous limit off the stack and use that.
7672       (setq articles (car gnus-newsgroup-limits)
7673             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7674     ;; We use the new limit, so we push the old limit on the stack.
7675     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7676   ;; Set the limit.
7677   (setq gnus-newsgroup-limit articles)
7678   (let ((total (length gnus-newsgroup-data))
7679         (data (gnus-data-find-list (gnus-summary-article-number)))
7680         (gnus-summary-mark-below nil)   ; Inhibit this.
7681         found)
7682     ;; This will do all the work of generating the new summary buffer
7683     ;; according to the new limit.
7684     (gnus-summary-prepare)
7685     ;; Hide any threads, possibly.
7686     (gnus-summary-maybe-hide-threads)
7687     ;; Try to return to the article you were at, or one in the
7688     ;; neighborhood.
7689     (when data
7690       ;; We try to find some article after the current one.
7691       (while data
7692         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7693           (setq data nil
7694                 found t))
7695         (setq data (cdr data))))
7696     (unless found
7697       ;; If there is no data, that means that we were after the last
7698       ;; article.  The same goes when we can't find any articles
7699       ;; after the current one.
7700       (goto-char (point-max))
7701       (gnus-summary-find-prev))
7702     (gnus-set-mode-line 'summary)
7703     ;; We return how many articles were removed from the summary
7704     ;; buffer as a result of the new limit.
7705     (- total (length gnus-newsgroup-data))))
7706
7707 (defsubst gnus-invisible-cut-children (threads)
7708   (let ((num 0))
7709     (while threads
7710       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7711         (incf num))
7712       (pop threads))
7713     (< num 2)))
7714
7715 (defsubst gnus-cut-thread (thread)
7716   "Go forwards in the thread until we find an article that we want to display."
7717   (when (or (eq gnus-fetch-old-headers 'some)
7718             (eq gnus-fetch-old-headers 'invisible)
7719             (numberp gnus-fetch-old-headers)
7720             (eq gnus-build-sparse-threads 'some)
7721             (eq gnus-build-sparse-threads 'more))
7722     ;; Deal with old-fetched headers and sparse threads.
7723     (while (and
7724             thread
7725             (or
7726              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7727              (gnus-summary-article-ancient-p
7728               (mail-header-number (car thread))))
7729             (if (or (<= (length (cdr thread)) 1)
7730                     (eq gnus-fetch-old-headers 'invisible))
7731                 (setq gnus-newsgroup-limit
7732                       (delq (mail-header-number (car thread))
7733                             gnus-newsgroup-limit)
7734                       thread (cadr thread))
7735               (when (gnus-invisible-cut-children (cdr thread))
7736                 (let ((th (cdr thread)))
7737                   (while th
7738                     (if (memq (mail-header-number (caar th))
7739                               gnus-newsgroup-limit)
7740                         (setq thread (car th)
7741                               th nil)
7742                       (setq th (cdr th))))))))))
7743   thread)
7744
7745 (defun gnus-cut-threads (threads)
7746   "Cut off all uninteresting articles from the beginning of threads."
7747   (when (or (eq gnus-fetch-old-headers 'some)
7748             (eq gnus-fetch-old-headers 'invisible)
7749             (numberp gnus-fetch-old-headers)
7750             (eq gnus-build-sparse-threads 'some)
7751             (eq gnus-build-sparse-threads 'more))
7752     (let ((th threads))
7753       (while th
7754         (setcar th (gnus-cut-thread (car th)))
7755         (setq th (cdr th)))))
7756   ;; Remove nixed out threads.
7757   (delq nil threads))
7758
7759 (defun gnus-summary-initial-limit (&optional show-if-empty)
7760   "Figure out what the initial limit is supposed to be on group entry.
7761 This entails weeding out unwanted dormants, low-scored articles,
7762 fetch-old-headers verbiage, and so on."
7763   ;; Most groups have nothing to remove.
7764   (if (or gnus-inhibit-limiting
7765           (and (null gnus-newsgroup-dormant)
7766                (eq gnus-newsgroup-display 'gnus-not-ignore)
7767                (not (eq gnus-fetch-old-headers 'some))
7768                (not (numberp gnus-fetch-old-headers))
7769                (not (eq gnus-fetch-old-headers 'invisible))
7770                (null gnus-summary-expunge-below)
7771                (not (eq gnus-build-sparse-threads 'some))
7772                (not (eq gnus-build-sparse-threads 'more))
7773                (null gnus-thread-expunge-below)
7774                (not gnus-use-nocem)))
7775       ()                                ; Do nothing.
7776     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7777     (setq gnus-newsgroup-limit nil)
7778     (mapatoms
7779      (lambda (node)
7780        (unless (car (symbol-value node))
7781          ;; These threads have no parents -- they are roots.
7782          (let ((nodes (cdr (symbol-value node)))
7783                thread)
7784            (while nodes
7785              (if (and gnus-thread-expunge-below
7786                       (< (gnus-thread-total-score (car nodes))
7787                          gnus-thread-expunge-below))
7788                  (gnus-expunge-thread (pop nodes))
7789                (setq thread (pop nodes))
7790                (gnus-summary-limit-children thread))))))
7791      gnus-newsgroup-dependencies)
7792     ;; If this limitation resulted in an empty group, we might
7793     ;; pop the previous limit and use it instead.
7794     (when (and (not gnus-newsgroup-limit)
7795                show-if-empty)
7796       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7797     gnus-newsgroup-limit))
7798
7799 (defun gnus-summary-limit-children (thread)
7800   "Return 1 if this subthread is visible and 0 if it is not."
7801   ;; First we get the number of visible children to this thread.  This
7802   ;; is done by recursing down the thread using this function, so this
7803   ;; will really go down to a leaf article first, before slowly
7804   ;; working its way up towards the root.
7805   (when thread
7806     (let* ((max-lisp-eval-depth 5000)
7807            (children
7808            (if (cdr thread)
7809                (apply '+ (mapcar 'gnus-summary-limit-children
7810                                  (cdr thread)))
7811              0))
7812           (number (mail-header-number (car thread)))
7813           score)
7814       (if (and
7815            (not (memq number gnus-newsgroup-marked))
7816            (or
7817             ;; If this article is dormant and has absolutely no visible
7818             ;; children, then this article isn't visible.
7819             (and (memq number gnus-newsgroup-dormant)
7820                  (zerop children))
7821             ;; If this is "fetch-old-headered" and there is no
7822             ;; visible children, then we don't want this article.
7823             (and (or (eq gnus-fetch-old-headers 'some)
7824                      (numberp gnus-fetch-old-headers))
7825                  (gnus-summary-article-ancient-p number)
7826                  (zerop children))
7827             ;; If this is "fetch-old-headered" and `invisible', then
7828             ;; we don't want this article.
7829             (and (eq gnus-fetch-old-headers 'invisible)
7830                  (gnus-summary-article-ancient-p number))
7831             ;; If this is a sparsely inserted article with no children,
7832             ;; we don't want it.
7833             (and (eq gnus-build-sparse-threads 'some)
7834                  (gnus-summary-article-sparse-p number)
7835                  (zerop children))
7836             ;; If we use expunging, and this article is really
7837             ;; low-scored, then we don't want this article.
7838             (when (and gnus-summary-expunge-below
7839                        (< (setq score
7840                                 (or (cdr (assq number gnus-newsgroup-scored))
7841                                     gnus-summary-default-score))
7842                           gnus-summary-expunge-below))
7843               ;; We increase the expunge-tally here, but that has
7844               ;; nothing to do with the limits, really.
7845               (incf gnus-newsgroup-expunged-tally)
7846               ;; We also mark as read here, if that's wanted.
7847               (when (and gnus-summary-mark-below
7848                          (< score gnus-summary-mark-below))
7849                 (setq gnus-newsgroup-unreads
7850                       (delq number gnus-newsgroup-unreads))
7851                 (if gnus-newsgroup-auto-expire
7852                     (push number gnus-newsgroup-expirable)
7853                   (push (cons number gnus-low-score-mark)
7854                         gnus-newsgroup-reads)))
7855               t)
7856             ;; Do the `display' group parameter.
7857             (and gnus-newsgroup-display
7858                  (not (funcall gnus-newsgroup-display)))
7859             ;; Check NoCeM things.
7860             (if (and gnus-use-nocem
7861                      (gnus-nocem-unwanted-article-p
7862                       (mail-header-id (car thread))))
7863                 (progn
7864                   (setq gnus-newsgroup-unreads
7865                         (delq number gnus-newsgroup-unreads))
7866                   t))))
7867           ;; Nope, invisible article.
7868           0
7869         ;; Ok, this article is to be visible, so we add it to the limit
7870         ;; and return 1.
7871         (push number gnus-newsgroup-limit)
7872         1))))
7873
7874 (defun gnus-expunge-thread (thread)
7875   "Mark all articles in THREAD as read."
7876   (let* ((number (mail-header-number (car thread))))
7877     (incf gnus-newsgroup-expunged-tally)
7878     ;; We also mark as read here, if that's wanted.
7879     (setq gnus-newsgroup-unreads
7880           (delq number gnus-newsgroup-unreads))
7881     (if gnus-newsgroup-auto-expire
7882         (push number gnus-newsgroup-expirable)
7883       (push (cons number gnus-low-score-mark)
7884             gnus-newsgroup-reads)))
7885   ;; Go recursively through all subthreads.
7886   (mapcar 'gnus-expunge-thread (cdr thread)))
7887
7888 ;; Summary article oriented commands
7889
7890 (defun gnus-summary-refer-parent-article (n)
7891   "Refer parent article N times.
7892 If N is negative, go to ancestor -N instead.
7893 The difference between N and the number of articles fetched is returned."
7894   (interactive "p")
7895   (let ((skip 1)
7896         error header ref)
7897     (when (not (natnump n))
7898       (setq skip (abs n)
7899             n 1))
7900     (while (and (> n 0)
7901                 (not error))
7902       (setq header (gnus-summary-article-header))
7903       (if (and (eq (mail-header-number header)
7904                    (cdr gnus-article-current))
7905                (equal gnus-newsgroup-name
7906                       (car gnus-article-current)))
7907           ;; If we try to find the parent of the currently
7908           ;; displayed article, then we take a look at the actual
7909           ;; References header, since this is slightly more
7910           ;; reliable than the References field we got from the
7911           ;; server.
7912           (save-excursion
7913             (set-buffer gnus-original-article-buffer)
7914             (nnheader-narrow-to-headers)
7915             (unless (setq ref (message-fetch-field "references"))
7916               (setq ref (message-fetch-field "in-reply-to")))
7917             (widen))
7918         (setq ref
7919               ;; It's not the current article, so we take a bet on
7920               ;; the value we got from the server.
7921               (mail-header-references header)))
7922       (if (and ref
7923                (not (equal ref "")))
7924           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7925             (gnus-message 1 "Couldn't find parent"))
7926         (gnus-message 1 "No references in article %d"
7927                       (gnus-summary-article-number))
7928         (setq error t))
7929       (decf n))
7930     (gnus-summary-position-point)
7931     n))
7932
7933 (defun gnus-summary-refer-references ()
7934   "Fetch all articles mentioned in the References header.
7935 Return the number of articles fetched."
7936   (interactive)
7937   (let ((ref (mail-header-references (gnus-summary-article-header)))
7938         (current (gnus-summary-article-number))
7939         (n 0))
7940     (if (or (not ref)
7941             (equal ref ""))
7942         (error "No References in the current article")
7943       ;; For each Message-ID in the References header...
7944       (while (string-match "<[^>]*>" ref)
7945         (incf n)
7946         ;; ... fetch that article.
7947         (gnus-summary-refer-article
7948          (prog1 (match-string 0 ref)
7949            (setq ref (substring ref (match-end 0))))))
7950       (gnus-summary-goto-subject current)
7951       (gnus-summary-position-point)
7952       n)))
7953
7954 (defun gnus-summary-refer-thread (&optional limit)
7955   "Fetch all articles in the current thread.
7956 If LIMIT (the numerical prefix), fetch that many old headers instead
7957 of what's specified by the `gnus-refer-thread-limit' variable."
7958   (interactive "P")
7959   (let ((id (mail-header-id (gnus-summary-article-header)))
7960         (limit (if limit (prefix-numeric-value limit)
7961                  gnus-refer-thread-limit)))
7962     (unless (eq gnus-fetch-old-headers 'invisible)
7963       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7964       ;; Retrieve the headers and read them in.
7965       (if (eq (if (numberp limit)
7966                   (gnus-retrieve-headers
7967                    (list (min
7968                           (+ (mail-header-number
7969                               (gnus-summary-article-header))
7970                              limit)
7971                           gnus-newsgroup-end))
7972                    gnus-newsgroup-name (* limit 2))
7973                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
7974                 ;; headers.
7975                 (gnus-retrieve-headers (list gnus-newsgroup-end)
7976                                        gnus-newsgroup-name limit))
7977               'nov)
7978           (gnus-build-all-threads)
7979         (error "Can't fetch thread from backends that don't support NOV"))
7980       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7981     (gnus-summary-limit-include-thread id)))
7982
7983 (defun gnus-summary-refer-article (message-id)
7984   "Fetch an article specified by MESSAGE-ID."
7985   (interactive "sMessage-ID: ")
7986   (when (and (stringp message-id)
7987              (not (zerop (length message-id))))
7988     ;; Construct the correct Message-ID if necessary.
7989     ;; Suggested by tale@pawl.rpi.edu.
7990     (unless (string-match "^<" message-id)
7991       (setq message-id (concat "<" message-id)))
7992     (unless (string-match ">$" message-id)
7993       (setq message-id (concat message-id ">")))
7994     (let* ((header (gnus-id-to-header message-id))
7995            (sparse (and header
7996                         (gnus-summary-article-sparse-p
7997                          (mail-header-number header))
7998                         (memq (mail-header-number header)
7999                               gnus-newsgroup-limit)))
8000            number)
8001       (cond
8002        ;; If the article is present in the buffer we just go to it.
8003        ((and header
8004              (or (not (gnus-summary-article-sparse-p
8005                        (mail-header-number header)))
8006                  sparse))
8007         (prog1
8008             (gnus-summary-goto-article
8009              (mail-header-number header) nil t)
8010           (when sparse
8011             (gnus-summary-update-article (mail-header-number header)))))
8012        (t
8013         ;; We fetch the article.
8014         (catch 'found
8015           (dolist (gnus-override-method (gnus-refer-article-methods))
8016             (gnus-check-server gnus-override-method)
8017             ;; Fetch the header, and display the article.
8018             (when (setq number (gnus-summary-insert-subject message-id))
8019               (gnus-summary-select-article nil nil nil number)
8020               (throw 'found t)))
8021           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8022
8023 (defun gnus-refer-article-methods ()
8024   "Return a list of referable methods."
8025   (cond
8026    ;; No method, so we default to current and native.
8027    ((null gnus-refer-article-method)
8028     (list gnus-current-select-method gnus-select-method))
8029    ;; Current.
8030    ((eq 'current gnus-refer-article-method)
8031     (list gnus-current-select-method))
8032    ;; List of select methods.
8033    ((not (and (symbolp (car gnus-refer-article-method))
8034               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8035     (let (out)
8036       (dolist (method gnus-refer-article-method)
8037         (push (if (eq 'current method)
8038                   gnus-current-select-method
8039                 method)
8040               out))
8041       (nreverse out)))
8042    ;; One single select method.
8043    (t
8044     (list gnus-refer-article-method))))
8045
8046 (defun gnus-summary-edit-parameters ()
8047   "Edit the group parameters of the current group."
8048   (interactive)
8049   (gnus-group-edit-group gnus-newsgroup-name 'params))
8050
8051 (defun gnus-summary-customize-parameters ()
8052   "Customize the group parameters of the current group."
8053   (interactive)
8054   (gnus-group-customize gnus-newsgroup-name))
8055
8056 (defun gnus-summary-enter-digest-group (&optional force)
8057   "Enter an nndoc group based on the current article.
8058 If FORCE, force a digest interpretation.  If not, try
8059 to guess what the document format is."
8060   (interactive "P")
8061   (let ((conf gnus-current-window-configuration))
8062     (save-excursion
8063       (gnus-summary-select-article))
8064     (setq gnus-current-window-configuration conf)
8065     (let* ((name (format "%s-%d"
8066                          (gnus-group-prefixed-name
8067                           gnus-newsgroup-name (list 'nndoc ""))
8068                          (save-excursion
8069                            (set-buffer gnus-summary-buffer)
8070                            gnus-current-article)))
8071            (ogroup gnus-newsgroup-name)
8072            (params (append (gnus-info-params (gnus-get-info ogroup))
8073                            (list (cons 'to-group ogroup))
8074                            (list (cons 'save-article-group ogroup))))
8075            (case-fold-search t)
8076            (buf (current-buffer))
8077            dig to-address)
8078       (save-excursion
8079         (set-buffer gnus-original-article-buffer)
8080         ;; Have the digest group inherit the main mail address of
8081         ;; the parent article.
8082         (when (setq to-address (or (gnus-fetch-field "reply-to")
8083                                    (gnus-fetch-field "from")))
8084           (setq params (append
8085                         (list (cons 'to-address
8086                                     (funcall gnus-decode-encoded-word-function
8087                                              to-address))))))
8088         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8089         (insert-buffer-substring gnus-original-article-buffer)
8090         ;; Remove lines that may lead nndoc to misinterpret the
8091         ;; document type.
8092         (narrow-to-region
8093          (goto-char (point-min))
8094          (or (search-forward "\n\n" nil t) (point)))
8095         (goto-char (point-min))
8096         (delete-matching-lines "^Path:\\|^From ")
8097         (widen))
8098       (unwind-protect
8099           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8100                     (gnus-newsgroup-ephemeral-ignored-charsets
8101                      gnus-newsgroup-ignored-charsets))
8102                 (gnus-group-read-ephemeral-group
8103                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8104                               (nndoc-article-type
8105                                ,(if force 'mbox 'guess)))
8106                  t nil nil nil
8107                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8108                                                         "ADAPT")))))
8109               ;; Make all postings to this group go to the parent group.
8110               (nconc (gnus-info-params (gnus-get-info name))
8111                      params)
8112             ;; Couldn't select this doc group.
8113             (switch-to-buffer buf)
8114             (gnus-set-global-variables)
8115             (gnus-configure-windows 'summary)
8116             (gnus-message 3 "Article couldn't be entered?"))
8117         (kill-buffer dig)))))
8118
8119 (defun gnus-summary-read-document (n)
8120   "Open a new group based on the current article(s).
8121 This will allow you to read digests and other similar
8122 documents as newsgroups.
8123 Obeys the standard process/prefix convention."
8124   (interactive "P")
8125   (let* ((articles (gnus-summary-work-articles n))
8126          (ogroup gnus-newsgroup-name)
8127          (params (append (gnus-info-params (gnus-get-info ogroup))
8128                          (list (cons 'to-group ogroup))))
8129          article group egroup groups vgroup)
8130     (while (setq article (pop articles))
8131       (setq group (format "%s-%d" gnus-newsgroup-name article))
8132       (gnus-summary-remove-process-mark article)
8133       (when (gnus-summary-display-article article)
8134         (save-excursion
8135           (with-temp-buffer
8136             (insert-buffer-substring gnus-original-article-buffer)
8137             ;; Remove some headers that may lead nndoc to make
8138             ;; the wrong guess.
8139             (message-narrow-to-head)
8140             (goto-char (point-min))
8141             (delete-matching-lines "^\\(Path\\):\\|^From ")
8142             (widen)
8143             (if (setq egroup
8144                       (gnus-group-read-ephemeral-group
8145                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8146                                      (nndoc-article-type guess))
8147                        t nil t))
8148                 (progn
8149             ;; Make all postings to this group go to the parent group.
8150                   (nconc (gnus-info-params (gnus-get-info egroup))
8151                          params)
8152                   (push egroup groups))
8153               ;; Couldn't select this doc group.
8154               (gnus-error 3 "Article couldn't be entered"))))))
8155     ;; Now we have selected all the documents.
8156     (cond
8157      ((not groups)
8158       (error "None of the articles could be interpreted as documents"))
8159      ((gnus-group-read-ephemeral-group
8160        (setq vgroup (format
8161                      "nnvirtual:%s-%s" gnus-newsgroup-name
8162                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8163        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8164        t
8165        (cons (current-buffer) 'summary)))
8166      (t
8167       (error "Couldn't select virtual nndoc group")))))
8168
8169 (defun gnus-summary-isearch-article (&optional regexp-p)
8170   "Do incremental search forward on the current article.
8171 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8172   (interactive "P")
8173   (gnus-summary-select-article)
8174   (gnus-configure-windows 'article)
8175   (gnus-eval-in-buffer-window gnus-article-buffer
8176     (save-restriction
8177       (widen)
8178       (isearch-forward regexp-p))))
8179
8180 (defun gnus-summary-search-article-forward (regexp &optional backward)
8181   "Search for an article containing REGEXP forward.
8182 If BACKWARD, search backward instead."
8183   (interactive
8184    (list (read-string
8185           (format "Search article %s (regexp%s): "
8186                   (if current-prefix-arg "backward" "forward")
8187                   (if gnus-last-search-regexp
8188                       (concat ", default " gnus-last-search-regexp)
8189                     "")))
8190          current-prefix-arg))
8191   (if (string-equal regexp "")
8192       (setq regexp (or gnus-last-search-regexp ""))
8193     (setq gnus-last-search-regexp regexp)
8194     (setq gnus-article-before-search gnus-current-article))
8195   ;; Intentionally set gnus-last-article.
8196   (setq gnus-last-article gnus-article-before-search)
8197   (let ((gnus-last-article gnus-last-article))
8198     (if (gnus-summary-search-article regexp backward)
8199         (gnus-summary-show-thread)
8200       (error "Search failed: \"%s\"" regexp))))
8201
8202 (defun gnus-summary-search-article-backward (regexp)
8203   "Search for an article containing REGEXP backward."
8204   (interactive
8205    (list (read-string
8206           (format "Search article backward (regexp%s): "
8207                   (if gnus-last-search-regexp
8208                       (concat ", default " gnus-last-search-regexp)
8209                     "")))))
8210   (gnus-summary-search-article-forward regexp 'backward))
8211
8212 (defun gnus-summary-search-article (regexp &optional backward)
8213   "Search for an article containing REGEXP.
8214 Optional argument BACKWARD means do search for backward.
8215 `gnus-select-article-hook' is not called during the search."
8216   ;; We have to require this here to make sure that the following
8217   ;; dynamic binding isn't shadowed by autoloading.
8218   (require 'gnus-async)
8219   (require 'gnus-art)
8220   (let ((gnus-select-article-hook nil)  ;Disable hook.
8221         (gnus-article-prepare-hook nil)
8222         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8223         (gnus-use-article-prefetch nil)
8224         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8225         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8226         (gnus-visual nil)
8227         (gnus-keep-backlog nil)
8228         (gnus-break-pages nil)
8229         (gnus-summary-display-arrow nil)
8230         (gnus-updated-mode-lines nil)
8231         (gnus-auto-center-summary nil)
8232         (sum (current-buffer))
8233         (gnus-display-mime-function nil)
8234         (found nil)
8235         point)
8236     (gnus-save-hidden-threads
8237       (gnus-summary-select-article)
8238       (set-buffer gnus-article-buffer)
8239       (goto-char (window-point (get-buffer-window (current-buffer))))
8240       (when backward
8241         (forward-line -1))
8242       (while (not found)
8243         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8244         (if (if backward
8245                 (re-search-backward regexp nil t)
8246               (re-search-forward regexp nil t))
8247             ;; We found the regexp.
8248             (progn
8249               (setq found 'found)
8250               (beginning-of-line)
8251               (set-window-start
8252                (get-buffer-window (current-buffer))
8253                (point))
8254               (forward-line 1)
8255               (set-window-point
8256                (get-buffer-window (current-buffer))
8257                (point))
8258               (set-buffer sum)
8259               (setq point (point)))
8260           ;; We didn't find it, so we go to the next article.
8261           (set-buffer sum)
8262           (setq found 'not)
8263           (while (eq found 'not)
8264             (if (not (if backward (gnus-summary-find-prev)
8265                        (gnus-summary-find-next)))
8266                 ;; No more articles.
8267                 (setq found t)
8268               ;; Select the next article and adjust point.
8269               (unless (gnus-summary-article-sparse-p
8270                        (gnus-summary-article-number))
8271                 (setq found nil)
8272                 (gnus-summary-select-article)
8273                 (set-buffer gnus-article-buffer)
8274                 (widen)
8275                 (goto-char (if backward (point-max) (point-min))))))))
8276       (gnus-message 7 ""))
8277     ;; Return whether we found the regexp.
8278     (when (eq found 'found)
8279       (goto-char point)
8280       (gnus-summary-show-thread)
8281       (gnus-summary-goto-subject gnus-current-article)
8282       (gnus-summary-position-point)
8283       t)))
8284
8285 (defun gnus-find-matching-articles (header regexp)
8286   "Return a list of all articles that match REGEXP on HEADER.
8287 This search includes all articles in the current group that Gnus has
8288 fetched headers for, whether they are displayed or not."
8289   (let ((articles nil)
8290         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8291         (case-fold-search t))
8292     (dolist (header gnus-newsgroup-headers)
8293       (when (string-match regexp (funcall func header))
8294         (push (mail-header-number header) articles)))
8295     (nreverse articles)))
8296
8297 (defun gnus-summary-find-matching (header regexp &optional backward unread
8298                                           not-case-fold not-matching)
8299   "Return a list of all articles that match REGEXP on HEADER.
8300 The search stars on the current article and goes forwards unless
8301 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8302 If UNREAD is non-nil, only unread articles will
8303 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8304 in the comparisons. If NOT-MATCHING, return a list of all articles that
8305 not match REGEXP on HEADER."
8306   (let ((case-fold-search (not not-case-fold))
8307         articles d func)
8308     (if (consp header)
8309         (if (eq (car header) 'extra)
8310             (setq func
8311                   `(lambda (h)
8312                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8313                          "")))
8314           (error "%s is an invalid header" header))
8315       (unless (fboundp (intern (concat "mail-header-" header)))
8316         (error "%s is not a valid header" header))
8317       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8318     (dolist (d (if (eq backward 'all)
8319                    gnus-newsgroup-data
8320                  (gnus-data-find-list
8321                   (gnus-summary-article-number)
8322                   (gnus-data-list backward))))
8323       (when (and (or (not unread)       ; We want all articles...
8324                      (gnus-data-unread-p d)) ; Or just unreads.
8325                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8326                  (if not-matching
8327                      (not (string-match
8328                            regexp
8329                            (funcall func (gnus-data-header d))))
8330                    (string-match regexp
8331                                  (funcall func (gnus-data-header d)))))
8332         (push (gnus-data-number d) articles))) ; Success!
8333     (nreverse articles)))
8334
8335 (defun gnus-summary-execute-command (header regexp command &optional backward)
8336   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8337 If HEADER is an empty string (or nil), the match is done on the entire
8338 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8339   (interactive
8340    (list (let ((completion-ignore-case t))
8341            (completing-read
8342             "Header name: "
8343             (mapcar (lambda (header) (list (format "%s" header)))
8344                     (append
8345                      '("Number" "Subject" "From" "Lines" "Date"
8346                        "Message-ID" "Xref" "References" "Body")
8347                      gnus-extra-headers))
8348             nil 'require-match))
8349          (read-string "Regexp: ")
8350          (read-key-sequence "Command: ")
8351          current-prefix-arg))
8352   (when (equal header "Body")
8353     (setq header ""))
8354   ;; Hidden thread subtrees must be searched as well.
8355   (gnus-summary-show-all-threads)
8356   ;; We don't want to change current point nor window configuration.
8357   (save-excursion
8358     (save-window-excursion
8359       (let (gnus-visual
8360             gnus-treat-strip-trailing-blank-lines
8361             gnus-treat-strip-leading-blank-lines
8362             gnus-treat-strip-multiple-blank-lines
8363             gnus-treat-hide-boring-headers
8364             gnus-treat-fold-newsgroups
8365             gnus-article-prepare-hook)
8366         (gnus-message 6 "Executing %s..." (key-description command))
8367         ;; We'd like to execute COMMAND interactively so as to give arguments.
8368         (gnus-execute header regexp
8369                       `(call-interactively ',(key-binding command))
8370                       backward)
8371         (gnus-message 6 "Executing %s...done" (key-description command))))))
8372
8373 (defun gnus-summary-beginning-of-article ()
8374   "Scroll the article back to the beginning."
8375   (interactive)
8376   (gnus-summary-select-article)
8377   (gnus-configure-windows 'article)
8378   (gnus-eval-in-buffer-window gnus-article-buffer
8379     (widen)
8380     (goto-char (point-min))
8381     (when gnus-page-broken
8382       (gnus-narrow-to-page))))
8383
8384 (defun gnus-summary-end-of-article ()
8385   "Scroll to the end of the article."
8386   (interactive)
8387   (gnus-summary-select-article)
8388   (gnus-configure-windows 'article)
8389   (gnus-eval-in-buffer-window gnus-article-buffer
8390     (widen)
8391     (goto-char (point-max))
8392     (recenter -3)
8393     (when gnus-page-broken
8394       (gnus-narrow-to-page))))
8395
8396 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8397   "Truncate to LEN and quote all \"(\"'s in STRING."
8398   (gnus-replace-in-string (if (and len (> (length string) len))
8399                               (substring string 0 len)
8400                             string)
8401                           "[()]" "\\\\\\&"))
8402
8403 (defun gnus-summary-print-article (&optional filename n)
8404   "Generate and print a PostScript image of the N next (mail) articles.
8405
8406 If N is negative, print the N previous articles.  If N is nil and articles
8407 have been marked with the process mark, print these instead.
8408
8409 If the optional first argument FILENAME is nil, send the image to the
8410 printer.  If FILENAME is a string, save the PostScript image in a file with
8411 that name.  If FILENAME is a number, prompt the user for the name of the file
8412 to save in."
8413   (interactive (list (ps-print-preprint current-prefix-arg)))
8414   (dolist (article (gnus-summary-work-articles n))
8415     (gnus-summary-select-article nil nil 'pseudo article)
8416     (gnus-eval-in-buffer-window gnus-article-buffer
8417       (gnus-print-buffer))
8418     (gnus-summary-remove-process-mark article))
8419   (ps-despool filename))
8420
8421 (defun gnus-print-buffer ()
8422   (let ((buffer (generate-new-buffer " *print*")))
8423     (unwind-protect
8424         (progn
8425           (copy-to-buffer buffer (point-min) (point-max))
8426           (set-buffer buffer)
8427           (gnus-article-delete-invisible-text)
8428           (gnus-remove-text-with-property 'gnus-decoration)
8429           (when (gnus-visual-p 'article-highlight 'highlight)
8430             ;; Copy-to-buffer doesn't copy overlay.  So redo
8431             ;; highlight.
8432             (let ((gnus-article-buffer buffer))
8433               (gnus-article-highlight-citation t)
8434               (gnus-article-highlight-signature)))
8435           (let ((ps-left-header
8436                  (list
8437                   (concat "("
8438                           (gnus-summary-print-truncate-and-quote
8439                            (mail-header-subject gnus-current-headers)
8440                            66) ")")
8441                   (concat "("
8442                           (gnus-summary-print-truncate-and-quote
8443                            (mail-header-from gnus-current-headers)
8444                            45) ")")))
8445                 (ps-right-header
8446                  (list
8447                   "/pagenumberstring load"
8448                   (concat "("
8449                           (mail-header-date gnus-current-headers) ")"))))
8450             (gnus-run-hooks 'gnus-ps-print-hook)
8451             (save-excursion
8452               (if window-system
8453                   (ps-spool-buffer-with-faces)
8454                 (ps-spool-buffer)))))
8455       (kill-buffer buffer))))
8456
8457 (defun gnus-summary-show-article (&optional arg)
8458   "Force redisplaying of the current article.
8459 If ARG (the prefix) is a number, show the article with the charset
8460 defined in `gnus-summary-show-article-charset-alist', or the charset
8461 input.
8462 If ARG (the prefix) is non-nil and not a number, show the raw article
8463 without any article massaging functions being run.  Normally, the key strokes
8464 are `C-u g'."
8465   (interactive "P")
8466   (cond
8467    ((numberp arg)
8468     (gnus-summary-show-article t)
8469     (let ((gnus-newsgroup-charset
8470            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8471                (mm-read-coding-system
8472                 "View as charset: " ;; actually it is coding system.
8473                 (save-excursion
8474                   (set-buffer gnus-article-buffer)
8475                   (mm-detect-coding-region (point) (point-max))))))
8476           (gnus-newsgroup-ignored-charsets 'gnus-all))
8477       (gnus-summary-select-article nil 'force)
8478       (let ((deps gnus-newsgroup-dependencies)
8479             head header lines)
8480         (save-excursion
8481           (set-buffer gnus-original-article-buffer)
8482           (save-restriction
8483             (message-narrow-to-head)
8484             (setq head (buffer-string))
8485             (goto-char (point-min))
8486             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8487               (goto-char (point-max))
8488               (widen)
8489               (setq lines (1- (count-lines (point) (point-max))))))
8490           (with-temp-buffer
8491             (insert (format "211 %d Article retrieved.\n"
8492                             (cdr gnus-article-current)))
8493             (insert head)
8494             (if lines (insert (format "Lines: %d\n" lines)))
8495             (insert ".\n")
8496             (let ((nntp-server-buffer (current-buffer)))
8497               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8498         (gnus-data-set-header
8499          (gnus-data-find (cdr gnus-article-current))
8500          header)
8501         (gnus-summary-update-article-line
8502          (cdr gnus-article-current) header)
8503         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8504           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8505    ((not arg)
8506     ;; Select the article the normal way.
8507     (gnus-summary-select-article nil 'force))
8508    (t
8509     ;; We have to require this here to make sure that the following
8510     ;; dynamic binding isn't shadowed by autoloading.
8511     (require 'gnus-async)
8512     (require 'gnus-art)
8513     ;; Bind the article treatment functions to nil.
8514     (let ((gnus-have-all-headers t)
8515           gnus-article-prepare-hook
8516           gnus-article-decode-hook
8517           gnus-display-mime-function
8518           gnus-break-pages)
8519       ;; Destroy any MIME parts.
8520       (when (gnus-buffer-live-p gnus-article-buffer)
8521         (save-excursion
8522           (set-buffer gnus-article-buffer)
8523           (mm-destroy-parts gnus-article-mime-handles)
8524           ;; Set it to nil for safety reason.
8525           (setq gnus-article-mime-handle-alist nil)
8526           (setq gnus-article-mime-handles nil)))
8527       (gnus-summary-select-article nil 'force))))
8528   (gnus-summary-goto-subject gnus-current-article)
8529   (gnus-summary-position-point))
8530
8531 (defun gnus-summary-show-raw-article ()
8532   "Show the raw article without any article massaging functions being run."
8533   (interactive)
8534   (gnus-summary-show-article t))
8535
8536 (defun gnus-summary-verbose-headers (&optional arg)
8537   "Toggle permanent full header display.
8538 If ARG is a positive number, turn header display on.
8539 If ARG is a negative number, turn header display off."
8540   (interactive "P")
8541   (setq gnus-show-all-headers
8542         (cond ((or (not (numberp arg))
8543                    (zerop arg))
8544                (not gnus-show-all-headers))
8545               ((natnump arg)
8546                t)))
8547   (gnus-summary-show-article))
8548
8549 (defun gnus-summary-toggle-header (&optional arg)
8550   "Show the headers if they are hidden, or hide them if they are shown.
8551 If ARG is a positive number, show the entire header.
8552 If ARG is a negative number, hide the unwanted header lines."
8553   (interactive "P")
8554   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8555                      (get-buffer-window gnus-article-buffer t))))
8556     (with-current-buffer gnus-article-buffer
8557       (widen)
8558       (article-narrow-to-head)
8559       (let* ((buffer-read-only nil)
8560              (inhibit-point-motion-hooks t)
8561              (hidden (if (numberp arg)
8562                          (>= arg 0)
8563                        (gnus-article-hidden-text-p 'headers)))
8564              s e)
8565         (delete-region (point-min) (point-max))
8566         (with-current-buffer gnus-original-article-buffer
8567           (goto-char (setq s (point-min)))
8568           (setq e (if (search-forward "\n\n" nil t)
8569                       (1- (point))
8570                     (point-max))))
8571         (insert-buffer-substring gnus-original-article-buffer s e)
8572         (article-decode-encoded-words)
8573         (if hidden
8574             (let ((gnus-treat-hide-headers nil)
8575                   (gnus-treat-hide-boring-headers nil))
8576               (gnus-delete-wash-type 'headers)
8577               (gnus-treat-article 'head))
8578           (gnus-treat-article 'head))
8579         (widen)
8580         (if window
8581             (set-window-start window (goto-char (point-min))))
8582         (setq gnus-page-broken
8583               (when gnus-break-pages
8584                 (gnus-narrow-to-page)
8585                 t))
8586         (gnus-set-mode-line 'article)))))
8587
8588 (defun gnus-summary-show-all-headers ()
8589   "Make all header lines visible."
8590   (interactive)
8591   (gnus-summary-toggle-header 1))
8592
8593 (defun gnus-summary-caesar-message (&optional arg)
8594   "Caesar rotate the current article by 13.
8595 The numerical prefix specifies how many places to rotate each letter
8596 forward."
8597   (interactive "P")
8598   (gnus-summary-select-article)
8599   (let ((mail-header-separator ""))
8600     (gnus-eval-in-buffer-window gnus-article-buffer
8601       (save-restriction
8602         (widen)
8603         (let ((start (window-start))
8604               buffer-read-only)
8605           (message-caesar-buffer-body arg)
8606           (set-window-start (get-buffer-window (current-buffer)) start))))))
8607
8608 (autoload 'unmorse-region "morse"
8609   "Convert morse coded text in region to ordinary ASCII text."
8610   t)
8611
8612 (defun gnus-summary-morse-message (&optional arg)
8613   "Morse decode the current article."
8614   (interactive "P")
8615   (gnus-summary-select-article)
8616   (let ((mail-header-separator ""))
8617     (gnus-eval-in-buffer-window gnus-article-buffer
8618       (save-excursion
8619         (save-restriction
8620           (widen)
8621           (let ((pos (window-start))
8622                 buffer-read-only)
8623             (goto-char (point-min))
8624             (when (message-goto-body)
8625               (gnus-narrow-to-body))
8626             (goto-char (point-min))
8627             (while (re-search-forward "·" (point-max) t)
8628               (replace-match "."))
8629             (unmorse-region (point-min) (point-max))
8630             (widen)
8631             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8632
8633 (defun gnus-summary-stop-page-breaking ()
8634   "Stop page breaking in the current article."
8635   (interactive)
8636   (gnus-summary-select-article)
8637   (gnus-eval-in-buffer-window gnus-article-buffer
8638     (widen)
8639     (when (gnus-visual-p 'page-marker)
8640       (let ((buffer-read-only nil))
8641         (gnus-remove-text-with-property 'gnus-prev)
8642         (gnus-remove-text-with-property 'gnus-next))
8643       (setq gnus-page-broken nil))))
8644
8645 (defun gnus-summary-move-article (&optional n to-newsgroup
8646                                             select-method action)
8647   "Move the current article to a different newsgroup.
8648 If N is a positive number, move the N next articles.
8649 If N is a negative number, move the N previous articles.
8650 If N is nil and any articles have been marked with the process mark,
8651 move those articles instead.
8652 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8653 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8654 re-spool using this method.
8655
8656 When called interactively with TO-NEWSGROUP being nil, the value of
8657 the variable `gnus-move-split-methods' is used for finding a default
8658 for the target newsgroup.
8659
8660 For this function to work, both the current newsgroup and the
8661 newsgroup that you want to move to have to support the `request-move'
8662 and `request-accept' functions.
8663
8664 ACTION can be either `move' (the default), `crosspost' or `copy'."
8665   (interactive "P")
8666   (unless action
8667     (setq action 'move))
8668   ;; Check whether the source group supports the required functions.
8669   (cond ((and (eq action 'move)
8670               (not (gnus-check-backend-function
8671                     'request-move-article gnus-newsgroup-name)))
8672          (error "The current group does not support article moving"))
8673         ((and (eq action 'crosspost)
8674               (not (gnus-check-backend-function
8675                     'request-replace-article gnus-newsgroup-name)))
8676          (error "The current group does not support article editing")))
8677   (let ((articles (gnus-summary-work-articles n))
8678         (prefix (if (gnus-check-backend-function
8679                      'request-move-article gnus-newsgroup-name)
8680                     (gnus-group-real-prefix gnus-newsgroup-name)
8681                   ""))
8682         (names '((move "Move" "Moving")
8683                  (copy "Copy" "Copying")
8684                  (crosspost "Crosspost" "Crossposting")))
8685         (copy-buf (save-excursion
8686                     (nnheader-set-temp-buffer " *copy article*")))
8687         art-group to-method new-xref article to-groups)
8688     (unless (assq action names)
8689       (error "Unknown action %s" action))
8690     ;; Read the newsgroup name.
8691     (when (and (not to-newsgroup)
8692                (not select-method))
8693       (if (and gnus-move-split-methods
8694                (not
8695                 (and (memq gnus-current-article articles)
8696                      (gnus-buffer-live-p gnus-original-article-buffer))))
8697           ;; When `gnus-move-split-methods' is non-nil, we have to
8698           ;; select an article to give `gnus-read-move-group-name' an
8699           ;; opportunity to suggest an appropriate default.  However,
8700           ;; we needn't render or mark the article.
8701           (let ((gnus-display-mime-function nil)
8702                 (gnus-article-prepare-hook nil)
8703                 (gnus-mark-article-hook nil))
8704             (gnus-summary-select-article nil nil nil (car articles))))
8705       (setq to-newsgroup
8706             (gnus-read-move-group-name
8707              (cadr (assq action names))
8708              (symbol-value (intern (format "gnus-current-%s-group" action)))
8709              articles prefix))
8710       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8711     (setq to-method (or select-method
8712                         (gnus-server-to-method
8713                          (gnus-group-method to-newsgroup))))
8714     ;; Check the method we are to move this article to...
8715     (unless (gnus-check-backend-function
8716              'request-accept-article (car to-method))
8717       (error "%s does not support article copying" (car to-method)))
8718     (unless (gnus-check-server to-method)
8719       (error "Can't open server %s" (car to-method)))
8720     (gnus-message 6 "%s to %s: %s..."
8721                   (caddr (assq action names))
8722                   (or (car select-method) to-newsgroup) articles)
8723     (while articles
8724       (setq article (pop articles))
8725       (setq
8726        art-group
8727        (cond
8728         ;; Move the article.
8729         ((eq action 'move)
8730          ;; Remove this article from future suppression.
8731          (gnus-dup-unsuppress-article article)
8732          (gnus-request-move-article
8733           article                       ; Article to move
8734           gnus-newsgroup-name           ; From newsgroup
8735           (nth 1 (gnus-find-method-for-group
8736                   gnus-newsgroup-name)) ; Server
8737           (list 'gnus-request-accept-article
8738                 to-newsgroup (list 'quote select-method)
8739                 (not articles) t)       ; Accept form
8740           (not articles)))              ; Only save nov last time
8741         ;; Copy the article.
8742         ((eq action 'copy)
8743          (save-excursion
8744            (set-buffer copy-buf)
8745            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8746              (gnus-request-accept-article
8747               to-newsgroup select-method (not articles) t))))
8748         ;; Crosspost the article.
8749         ((eq action 'crosspost)
8750          (let ((xref (message-tokenize-header
8751                       (mail-header-xref (gnus-summary-article-header article))
8752                       " ")))
8753            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8754                                   ":" (number-to-string article)))
8755            (unless xref
8756              (setq xref (list (system-name))))
8757            (setq new-xref
8758                  (concat
8759                   (mapconcat 'identity
8760                              (delete "Xref:" (delete new-xref xref))
8761                              " ")
8762                   " " new-xref))
8763            (save-excursion
8764              (set-buffer copy-buf)
8765              ;; First put the article in the destination group.
8766              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8767              (when (consp (setq art-group
8768                                 (gnus-request-accept-article
8769                                  to-newsgroup select-method (not articles))))
8770                (setq new-xref (concat new-xref " " (car art-group)
8771                                       ":"
8772                                       (number-to-string (cdr art-group))))
8773                ;; Now we have the new Xrefs header, so we insert
8774                ;; it and replace the new article.
8775                (nnheader-replace-header "Xref" new-xref)
8776                (gnus-request-replace-article
8777                 (cdr art-group) to-newsgroup (current-buffer))
8778                art-group))))))
8779       (cond
8780        ((not art-group)
8781         (gnus-message 1 "Couldn't %s article %s: %s"
8782                       (cadr (assq action names)) article
8783                       (nnheader-get-report (car to-method))))
8784        ((eq art-group 'junk)
8785         (when (eq action 'move)
8786           (let ((id (mail-header-id (gnus-data-header 
8787                                      (assoc article (gnus-data-list nil))))))
8788             (gnus-summary-mark-article article gnus-canceled-mark)
8789             (gnus-message 4 "Deleted article %s" article)
8790             ;; run the move/copy/crosspost/respool hook
8791             (run-hook-with-args 'gnus-summary-article-delete-hook 
8792                                 action id gnus-newsgroup-name nil
8793                                 select-method))))
8794        (t
8795         (let* ((pto-group (gnus-group-prefixed-name
8796                            (car art-group) to-method))
8797                (entry
8798                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8799                (info (nth 2 entry))
8800                (to-group (gnus-info-group info))
8801                to-marks)
8802           ;; Update the group that has been moved to.
8803           (when (and info
8804                      (memq action '(move copy)))
8805             (unless (member to-group to-groups)
8806               (push to-group to-groups))
8807
8808             (unless (memq article gnus-newsgroup-unreads)
8809               (push 'read to-marks)
8810               (gnus-info-set-read
8811                info (gnus-add-to-range (gnus-info-read info)
8812                                        (list (cdr art-group)))))
8813
8814             ;; See whether the article is to be put in the cache.
8815             (let ((marks gnus-article-mark-lists)
8816                   (to-article (cdr art-group)))
8817
8818               ;; Enter the article into the cache in the new group,
8819               ;; if that is required.
8820               (when gnus-use-cache
8821                 (gnus-cache-possibly-enter-article
8822                  to-group to-article
8823                  (memq article gnus-newsgroup-marked)
8824                  (memq article gnus-newsgroup-dormant)
8825                  (memq article gnus-newsgroup-unreads)))
8826
8827               (when gnus-preserve-marks
8828                 ;; Copy any marks over to the new group.
8829                 (when (and (equal to-group gnus-newsgroup-name)
8830                            (not (memq article gnus-newsgroup-unreads)))
8831                   ;; Mark this article as read in this group.
8832                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8833                   (setcdr (gnus-active to-group) to-article)
8834                   (setcdr gnus-newsgroup-active to-article))
8835
8836                 (while marks
8837                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8838                     (when (memq article (symbol-value
8839                                          (intern (format "gnus-newsgroup-%s"
8840                                                          (caar marks)))))
8841                       (push (cdar marks) to-marks)
8842                       ;; If the other group is the same as this group,
8843                       ;; then we have to add the mark to the list.
8844                       (when (equal to-group gnus-newsgroup-name)
8845                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8846                              (cons to-article
8847                                    (symbol-value
8848                                     (intern (format "gnus-newsgroup-%s"
8849                                                     (caar marks)))))))
8850                       ;; Copy the marks to other group.
8851                       (gnus-add-marked-articles
8852                        to-group (cdar marks) (list to-article) info)))
8853                   (setq marks (cdr marks)))
8854
8855                 (gnus-request-set-mark
8856                  to-group (list (list (list to-article) 'add to-marks))))
8857
8858               (gnus-dribble-enter
8859                (concat "(gnus-group-set-info '"
8860                        (gnus-prin1-to-string (gnus-get-info to-group))
8861                        ")"))))
8862
8863           ;; Update the Xref header in this article to point to
8864           ;; the new crossposted article we have just created.
8865           (when (eq action 'crosspost)
8866             (save-excursion
8867               (set-buffer copy-buf)
8868               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8869               (nnheader-replace-header "Xref" new-xref)
8870               (gnus-request-replace-article
8871                article gnus-newsgroup-name (current-buffer))))
8872
8873           ;; run the move/copy/crosspost/respool hook
8874           (let ((id (mail-header-id (gnus-data-header 
8875                                    (assoc article (gnus-data-list nil))))))
8876           (run-hook-with-args 'gnus-summary-article-move-hook 
8877                               action id gnus-newsgroup-name to-newsgroup
8878                               select-method)))
8879
8880         ;;;!!!Why is this necessary?
8881         (set-buffer gnus-summary-buffer)
8882
8883         (gnus-summary-goto-subject article)
8884         (when (eq action 'move)
8885           (gnus-summary-mark-article article gnus-canceled-mark))))
8886       (gnus-summary-remove-process-mark article))
8887     ;; Re-activate all groups that have been moved to.
8888     (save-excursion
8889       (set-buffer gnus-group-buffer)
8890       (let ((gnus-group-marked to-groups))
8891         (gnus-group-get-new-news-this-group nil t)))
8892
8893     (gnus-kill-buffer copy-buf)
8894     (gnus-summary-position-point)
8895     (gnus-set-mode-line 'summary)))
8896
8897 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8898   "Move the current article to a different newsgroup.
8899 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8900 When called interactively, if TO-NEWSGROUP is nil, use the value of
8901 the variable `gnus-move-split-methods' for finding a default target
8902 newsgroup.
8903 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8904 re-spool using this method."
8905   (interactive "P")
8906   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8907
8908 (defun gnus-summary-crosspost-article (&optional n)
8909   "Crosspost the current article to some other group."
8910   (interactive "P")
8911   (gnus-summary-move-article n nil nil 'crosspost))
8912
8913 (defcustom gnus-summary-respool-default-method nil
8914   "Default method type for respooling an article.
8915 If nil, use to the current newsgroup method."
8916   :type 'symbol
8917   :group 'gnus-summary-mail)
8918
8919 (defcustom gnus-summary-display-while-building nil
8920   "If not-nil, show and update the summary buffer as it's being built.
8921 If the value is t, update the buffer after every line is inserted.  If
8922 the value is an integer (N), update the display every N lines."
8923   :group 'gnus-thread
8924   :type '(choice (const :tag "off" nil)
8925                  number
8926                  (const :tag "frequently" t)))
8927
8928 (defun gnus-summary-respool-article (&optional n method)
8929   "Respool the current article.
8930 The article will be squeezed through the mail spooling process again,
8931 which means that it will be put in some mail newsgroup or other
8932 depending on `nnmail-split-methods'.
8933 If N is a positive number, respool the N next articles.
8934 If N is a negative number, respool the N previous articles.
8935 If N is nil and any articles have been marked with the process mark,
8936 respool those articles instead.
8937
8938 Respooling can be done both from mail groups and \"real\" newsgroups.
8939 In the former case, the articles in question will be moved from the
8940 current group into whatever groups they are destined to.  In the
8941 latter case, they will be copied into the relevant groups."
8942   (interactive
8943    (list current-prefix-arg
8944          (let* ((methods (gnus-methods-using 'respool))
8945                 (methname
8946                  (symbol-name (or gnus-summary-respool-default-method
8947                                   (car (gnus-find-method-for-group
8948                                         gnus-newsgroup-name)))))
8949                 (method
8950                  (gnus-completing-read-with-default
8951                   methname "What backend do you want to use when respooling?"
8952                   methods nil t nil 'gnus-mail-method-history))
8953                 ms)
8954            (cond
8955             ((zerop (length (setq ms (gnus-servers-using-backend
8956                                       (intern method)))))
8957              (list (intern method) ""))
8958             ((= 1 (length ms))
8959              (car ms))
8960             (t
8961              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8962                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8963                            ms-alist))))))))
8964   (unless method
8965     (error "No method given for respooling"))
8966   (if (assoc (symbol-name
8967               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8968              (gnus-methods-using 'respool))
8969       (gnus-summary-move-article n nil method)
8970     (gnus-summary-copy-article n nil method)))
8971
8972 (defun gnus-summary-import-article (file &optional edit)
8973   "Import an arbitrary file into a mail newsgroup."
8974   (interactive "fImport file: \nP")
8975   (let ((group gnus-newsgroup-name)
8976         (now (current-time))
8977         atts lines group-art)
8978     (unless (gnus-check-backend-function 'request-accept-article group)
8979       (error "%s does not support article importing" group))
8980     (or (file-readable-p file)
8981         (not (file-regular-p file))
8982         (error "Can't read %s" file))
8983     (save-excursion
8984       (set-buffer (gnus-get-buffer-create " *import file*"))
8985       (erase-buffer)
8986       (nnheader-insert-file-contents file)
8987       (goto-char (point-min))
8988       (if (nnheader-article-p)
8989           (save-restriction
8990             (goto-char (point-min))
8991             (search-forward "\n\n" nil t)
8992             (narrow-to-region (point-min) (1- (point)))
8993             (goto-char (point-min))
8994             (unless (re-search-forward "^date:" nil t)
8995               (goto-char (point-max))
8996               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8997        ;; This doesn't look like an article, so we fudge some headers.
8998         (setq atts (file-attributes file)
8999               lines (count-lines (point-min) (point-max)))
9000         (insert "From: " (read-string "From: ") "\n"
9001                 "Subject: " (read-string "Subject: ") "\n"
9002                 "Date: " (message-make-date (nth 5 atts)) "\n"
9003                 "Message-ID: " (message-make-message-id) "\n"
9004                 "Lines: " (int-to-string lines) "\n"
9005                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9006       (setq group-art (gnus-request-accept-article group nil t))
9007       (kill-buffer (current-buffer)))
9008     (setq gnus-newsgroup-active (gnus-activate-group group))
9009     (forward-line 1)
9010     (gnus-summary-goto-article (cdr group-art) nil t)
9011     (when edit
9012       (gnus-summary-edit-article))))
9013
9014 (defun gnus-summary-create-article ()
9015   "Create an article in a mail newsgroup."
9016   (interactive)
9017   (let ((group gnus-newsgroup-name)
9018         (now (current-time))
9019         group-art)
9020     (unless (gnus-check-backend-function 'request-accept-article group)
9021       (error "%s does not support article importing" group))
9022     (save-excursion
9023       (set-buffer (gnus-get-buffer-create " *import file*"))
9024       (erase-buffer)
9025       (goto-char (point-min))
9026       ;; This doesn't look like an article, so we fudge some headers.
9027       (insert "From: " (read-string "From: ") "\n"
9028               "Subject: " (read-string "Subject: ") "\n"
9029               "Date: " (message-make-date now) "\n"
9030               "Message-ID: " (message-make-message-id) "\n")
9031       (setq group-art (gnus-request-accept-article group nil t))
9032       (kill-buffer (current-buffer)))
9033     (setq gnus-newsgroup-active (gnus-activate-group group))
9034     (forward-line 1)
9035     (gnus-summary-goto-article (cdr group-art) nil t)
9036     (gnus-summary-edit-article)))
9037
9038 (defun gnus-summary-article-posted-p ()
9039   "Say whether the current (mail) article is available from news as well.
9040 This will be the case if the article has both been mailed and posted."
9041   (interactive)
9042   (let ((id (mail-header-references (gnus-summary-article-header)))
9043         (gnus-override-method (car (gnus-refer-article-methods))))
9044     (if (gnus-request-head id "")
9045         (gnus-message 2 "The current message was found on %s"
9046                       gnus-override-method)
9047       (gnus-message 2 "The current message couldn't be found on %s"
9048                     gnus-override-method)
9049       nil)))
9050
9051 (defun gnus-summary-expire-articles (&optional now)
9052   "Expire all articles that are marked as expirable in the current group."
9053   (interactive)
9054   (when (gnus-check-backend-function
9055          'request-expire-articles gnus-newsgroup-name)
9056     ;; This backend supports expiry.
9057     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9058            (expirable (if total
9059                           (progn
9060                             ;; We need to update the info for
9061                             ;; this group for `gnus-list-of-read-articles'
9062                             ;; to give us the right answer.
9063                             (gnus-run-hooks 'gnus-exit-group-hook)
9064                             (gnus-summary-update-info)
9065                             (gnus-list-of-read-articles gnus-newsgroup-name))
9066                         (setq gnus-newsgroup-expirable
9067                               (sort gnus-newsgroup-expirable '<))))
9068            (expiry-wait (if now 'immediate
9069                           (gnus-group-find-parameter
9070                            gnus-newsgroup-name 'expiry-wait)))
9071            (nnmail-expiry-target
9072             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9073                 nnmail-expiry-target))
9074            es)
9075       (when expirable
9076         ;; There are expirable articles in this group, so we run them
9077         ;; through the expiry process.
9078         (gnus-message 6 "Expiring articles...")
9079         (unless (gnus-check-group gnus-newsgroup-name)
9080           (error "Can't open server for %s" gnus-newsgroup-name))
9081         ;; The list of articles that weren't expired is returned.
9082         (save-excursion
9083           (if expiry-wait
9084               (let ((nnmail-expiry-wait-function nil)
9085                     (nnmail-expiry-wait expiry-wait))
9086                 (setq es (gnus-request-expire-articles
9087                           expirable gnus-newsgroup-name)))
9088             (setq es (gnus-request-expire-articles
9089                       expirable gnus-newsgroup-name)))
9090           (unless total
9091             (setq gnus-newsgroup-expirable es))
9092           ;; We go through the old list of expirable, and mark all
9093           ;; really expired articles as nonexistent.
9094           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9095             (let ((gnus-use-cache nil))
9096               (dolist (article expirable)
9097                 (when (and (not (memq article es))
9098                            (gnus-data-find article))
9099                   (gnus-summary-mark-article article gnus-canceled-mark)
9100                   (let ((id (mail-header-id (gnus-data-header 
9101                                              (assoc article 
9102                                                     (gnus-data-list nil))))))
9103                     (run-hook-with-args 'gnus-summary-article-expire-hook
9104                                         'delete id gnus-newsgroup-name nil
9105                                         nil)))))))
9106         (gnus-message 6 "Expiring articles...done")))))
9107
9108 (defun gnus-summary-expire-articles-now ()
9109   "Expunge all expirable articles in the current group.
9110 This means that *all* articles that are marked as expirable will be
9111 deleted forever, right now."
9112   (interactive)
9113   (or gnus-expert-user
9114       (gnus-yes-or-no-p
9115        "Are you really, really, really sure you want to delete all these messages? ")
9116       (error "Phew!"))
9117   (gnus-summary-expire-articles t))
9118
9119 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9120 (defun gnus-summary-delete-article (&optional n)
9121   "Delete the N next (mail) articles.
9122 This command actually deletes articles.  This is not a marking
9123 command.  The article will disappear forever from your life, never to
9124 return.
9125 If N is negative, delete backwards.
9126 If N is nil and articles have been marked with the process mark,
9127 delete these instead."
9128   (interactive "P")
9129   (unless (gnus-check-backend-function 'request-expire-articles
9130                                        gnus-newsgroup-name)
9131     (error "The current newsgroup does not support article deletion"))
9132   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9133     (error "Couldn't open server"))
9134   ;; Compute the list of articles to delete.
9135   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9136         (nnmail-expiry-target 'delete)
9137         not-deleted)
9138     (if (and gnus-novice-user
9139              (not (gnus-yes-or-no-p
9140                    (format "Do you really want to delete %s forever? "
9141                            (if (> (length articles) 1)
9142                                (format "these %s articles" (length articles))
9143                              "this article")))))
9144         ()
9145       ;; Delete the articles.
9146       (setq not-deleted (gnus-request-expire-articles
9147                          articles gnus-newsgroup-name 'force))
9148       (while articles
9149         (gnus-summary-remove-process-mark (car articles))
9150         ;; The backend might not have been able to delete the article
9151         ;; after all.
9152         (unless (memq (car articles) not-deleted)
9153           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9154         (let* ((article (car articles))
9155                (id (mail-header-id (gnus-data-header 
9156                                     (assoc article (gnus-data-list nil))))))
9157           (run-hook-with-args 'gnus-summary-article-delete-hook
9158                               'delete id gnus-newsgroup-name nil
9159                               nil))
9160         (setq articles (cdr articles)))
9161       (when not-deleted
9162         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9163     (gnus-summary-position-point)
9164     (gnus-set-mode-line 'summary)
9165     not-deleted))
9166
9167 (defun gnus-summary-edit-article (&optional arg)
9168   "Edit the current article.
9169 This will have permanent effect only in mail groups.
9170 If ARG is nil, edit the decoded articles.
9171 If ARG is 1, edit the raw articles.
9172 If ARG is 2, edit the raw articles even in read-only groups.
9173 If ARG is 3, edit the articles with the current handles.
9174 Otherwise, allow editing of articles even in read-only
9175 groups."
9176   (interactive "P")
9177   (let (force raw current-handles)
9178     (cond
9179      ((null arg))
9180      ((eq arg 1)
9181       (setq raw t))
9182      ((eq arg 2)
9183       (setq raw t
9184             force t))
9185      ((eq arg 3)
9186       (setq current-handles
9187             (and (gnus-buffer-live-p gnus-article-buffer)
9188                  (with-current-buffer gnus-article-buffer
9189                    (prog1
9190                        gnus-article-mime-handles
9191                      (setq gnus-article-mime-handles nil))))))
9192      (t
9193       (setq force t)))
9194     (when (and raw (not force)
9195                (member gnus-newsgroup-name '("nndraft:delayed"
9196                                              "nndraft:drafts"
9197                                              "nndraft:queue")))
9198       (error "Can't edit the raw article in group %s"
9199              gnus-newsgroup-name))
9200     (save-excursion
9201       (set-buffer gnus-summary-buffer)
9202       (let ((mail-parse-charset gnus-newsgroup-charset)
9203             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9204         (gnus-set-global-variables)
9205         (when (and (not force)
9206                    (gnus-group-read-only-p))
9207           (error "The current newsgroup does not support article editing"))
9208         (gnus-summary-show-article t)
9209         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9210           (with-current-buffer gnus-article-buffer
9211             (mm-enable-multibyte)))
9212         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9213             (setq raw t))
9214         (gnus-article-edit-article
9215          (if raw 'ignore
9216            `(lambda ()
9217               (let ((mbl mml-buffer-list))
9218                 (setq mml-buffer-list nil)
9219                 (mime-to-mml ,'current-handles)
9220                 (let ((mbl1 mml-buffer-list))
9221                   (setq mml-buffer-list mbl)
9222                   (set (make-local-variable 'mml-buffer-list) mbl1))
9223                 (make-local-hook 'kill-buffer-hook)
9224                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9225          `(lambda (no-highlight)
9226             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9227                   (message-options message-options)
9228                   (message-options-set-recipient)
9229                   (mail-parse-ignored-charsets
9230                    ',gnus-newsgroup-ignored-charsets))
9231               ,(if (not raw) '(progn
9232                                 (mml-to-mime)
9233                                 (mml-destroy-buffers)
9234                                 (remove-hook 'kill-buffer-hook
9235                                              'mml-destroy-buffers t)
9236                                 (kill-local-variable 'mml-buffer-list)))
9237               (gnus-summary-edit-article-done
9238                ,(or (mail-header-references gnus-current-headers) "")
9239                ,(gnus-group-read-only-p)
9240                ,gnus-summary-buffer no-highlight))))))))
9241
9242 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9243
9244 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9245                                                  no-highlight)
9246   "Make edits to the current article permanent."
9247   (interactive)
9248   (save-excursion
9249    ;; The buffer restriction contains the entire article if it exists.
9250     (when (article-goto-body)
9251       (let ((lines (count-lines (point) (point-max)))
9252             (length (- (point-max) (point)))
9253             (case-fold-search t)
9254             (body (copy-marker (point))))
9255         (goto-char (point-min))
9256         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9257           (delete-region (match-beginning 1) (match-end 1))
9258           (insert (number-to-string length)))
9259         (goto-char (point-min))
9260         (when (re-search-forward
9261                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9262           (delete-region (match-beginning 1) (match-end 1))
9263           (insert (number-to-string length)))
9264         (goto-char (point-min))
9265         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9266           (delete-region (match-beginning 1) (match-end 1))
9267           (insert (number-to-string lines))))))
9268   ;; Replace the article.
9269   (let ((buf (current-buffer)))
9270     (with-temp-buffer
9271       (insert-buffer-substring buf)
9272
9273       (if (and (not read-only)
9274                (not (gnus-request-replace-article
9275                      (cdr gnus-article-current) (car gnus-article-current)
9276                      (current-buffer) t)))
9277           (error "Couldn't replace article")
9278         ;; Update the summary buffer.
9279         (if (and references
9280                  (equal (message-tokenize-header references " ")
9281                         (message-tokenize-header
9282                          (or (message-fetch-field "references") "") " ")))
9283             ;; We only have to update this line.
9284             (save-excursion
9285               (save-restriction
9286                 (message-narrow-to-head)
9287                 (let ((head (buffer-string))
9288                       header)
9289                   (with-temp-buffer
9290                     (insert (format "211 %d Article retrieved.\n"
9291                                     (cdr gnus-article-current)))
9292                     (insert head)
9293                     (insert ".\n")
9294                     (let ((nntp-server-buffer (current-buffer)))
9295                       (setq header (car (gnus-get-newsgroup-headers
9296                                          nil t))))
9297                     (save-excursion
9298                       (set-buffer gnus-summary-buffer)
9299                       (gnus-data-set-header
9300                        (gnus-data-find (cdr gnus-article-current))
9301                        header)
9302                       (gnus-summary-update-article-line
9303                        (cdr gnus-article-current) header)
9304                       (if (gnus-summary-goto-subject
9305                            (cdr gnus-article-current) nil t)
9306                           (gnus-summary-update-secondary-mark
9307                            (cdr gnus-article-current))))))))
9308           ;; Update threads.
9309           (set-buffer (or buffer gnus-summary-buffer))
9310           (gnus-summary-update-article (cdr gnus-article-current))
9311           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9312               (gnus-summary-update-secondary-mark
9313                (cdr gnus-article-current))))
9314         ;; Prettify the article buffer again.
9315         (unless no-highlight
9316           (save-excursion
9317             (set-buffer gnus-article-buffer)
9318             ;;;!!! Fix this -- article should be rehighlighted.
9319             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9320             (set-buffer gnus-original-article-buffer)
9321             (gnus-request-article
9322              (cdr gnus-article-current)
9323              (car gnus-article-current) (current-buffer))))
9324         ;; Prettify the summary buffer line.
9325         (when (gnus-visual-p 'summary-highlight 'highlight)
9326           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9327
9328 (defun gnus-summary-edit-wash (key)
9329   "Perform editing command KEY in the article buffer."
9330   (interactive
9331    (list
9332     (progn
9333       (message "%s" (concat (this-command-keys) "- "))
9334       (read-char))))
9335   (message "")
9336   (gnus-summary-edit-article)
9337   (execute-kbd-macro (concat (this-command-keys) key))
9338   (gnus-article-edit-done))
9339
9340 ;;; Respooling
9341
9342 (defun gnus-summary-respool-query (&optional silent trace)
9343   "Query where the respool algorithm would put this article."
9344   (interactive)
9345   (let (gnus-mark-article-hook)
9346     (gnus-summary-select-article)
9347     (save-excursion
9348       (set-buffer gnus-original-article-buffer)
9349       (save-restriction
9350         (message-narrow-to-head)
9351         (let ((groups (nnmail-article-group 'identity trace)))
9352           (unless silent
9353             (if groups
9354                 (message "This message would go to %s"
9355                          (mapconcat 'car groups ", "))
9356               (message "This message would go to no groups"))
9357             groups))))))
9358
9359 (defun gnus-summary-respool-trace ()
9360   "Trace where the respool algorithm would put this article.
9361 Display a buffer showing all fancy splitting patterns which matched."
9362   (interactive)
9363   (gnus-summary-respool-query nil t))
9364
9365 ;; Summary marking commands.
9366
9367 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9368   "Mark articles which has the same subject as read, and then select the next.
9369 If UNMARK is positive, remove any kind of mark.
9370 If UNMARK is negative, tick articles."
9371   (interactive "P")
9372   (when unmark
9373     (setq unmark (prefix-numeric-value unmark)))
9374   (let ((count
9375          (gnus-summary-mark-same-subject
9376           (gnus-summary-article-subject) unmark)))
9377     ;; Select next unread article.  If auto-select-same mode, should
9378     ;; select the first unread article.
9379     (gnus-summary-next-article t (and gnus-auto-select-same
9380                                       (gnus-summary-article-subject)))
9381     (gnus-message 7 "%d article%s marked as %s"
9382                   count (if (= count 1) " is" "s are")
9383                   (if unmark "unread" "read"))))
9384
9385 (defun gnus-summary-kill-same-subject (&optional unmark)
9386   "Mark articles which has the same subject as read.
9387 If UNMARK is positive, remove any kind of mark.
9388 If UNMARK is negative, tick articles."
9389   (interactive "P")
9390   (when unmark
9391     (setq unmark (prefix-numeric-value unmark)))
9392   (let ((count
9393          (gnus-summary-mark-same-subject
9394           (gnus-summary-article-subject) unmark)))
9395     ;; If marked as read, go to next unread subject.
9396     (when (null unmark)
9397       ;; Go to next unread subject.
9398       (gnus-summary-next-subject 1 t))
9399     (gnus-message 7 "%d articles are marked as %s"
9400                   count (if unmark "unread" "read"))))
9401
9402 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9403   "Mark articles with same SUBJECT as read, and return marked number.
9404 If optional argument UNMARK is positive, remove any kinds of marks.
9405 If optional argument UNMARK is negative, mark articles as unread instead."
9406   (let ((count 1))
9407     (save-excursion
9408       (cond
9409        ((null unmark)                   ; Mark as read.
9410         (while (and
9411                 (progn
9412                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9413                   (gnus-summary-show-thread) t)
9414                 (gnus-summary-find-subject subject))
9415           (setq count (1+ count))))
9416        ((> unmark 0)                    ; Tick.
9417         (while (and
9418                 (progn
9419                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9420                   (gnus-summary-show-thread) t)
9421                 (gnus-summary-find-subject subject))
9422           (setq count (1+ count))))
9423        (t                               ; Mark as unread.
9424         (while (and
9425                 (progn
9426                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9427                   (gnus-summary-show-thread) t)
9428                 (gnus-summary-find-subject subject))
9429           (setq count (1+ count)))))
9430       (gnus-set-mode-line 'summary)
9431       ;; Return the number of marked articles.
9432       count)))
9433
9434 (defun gnus-summary-mark-as-processable (n &optional unmark)
9435   "Set the process mark on the next N articles.
9436 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9437 the process mark instead.  The difference between N and the actual
9438 number of articles marked is returned."
9439   (interactive "P")
9440   (if (and (null n) (gnus-region-active-p))
9441       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9442     (setq n (prefix-numeric-value n))
9443     (let ((backward (< n 0))
9444           (n (abs n)))
9445       (while (and
9446               (> n 0)
9447               (if unmark
9448                   (gnus-summary-remove-process-mark
9449                    (gnus-summary-article-number))
9450                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9451               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9452         (setq n (1- n)))
9453       (when (/= 0 n)
9454         (gnus-message 7 "No more articles"))
9455       (gnus-summary-recenter)
9456       (gnus-summary-position-point)
9457       n)))
9458
9459 (defun gnus-summary-unmark-as-processable (n)
9460   "Remove the process mark from the next N articles.
9461 If N is negative, unmark backward instead.  The difference between N and
9462 the actual number of articles unmarked is returned."
9463   (interactive "P")
9464   (gnus-summary-mark-as-processable n t))
9465
9466 (defun gnus-summary-unmark-all-processable ()
9467   "Remove the process mark from all articles."
9468   (interactive)
9469   (save-excursion
9470     (while gnus-newsgroup-processable
9471       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9472   (gnus-summary-position-point))
9473
9474 (defun gnus-summary-add-mark (article type)
9475   "Mark ARTICLE with a mark of TYPE."
9476   (let ((vtype (car (assq type gnus-article-mark-lists)))
9477         var)
9478     (if (not vtype)
9479         (error "No such mark type: %s" type)
9480       (setq var (intern (format "gnus-newsgroup-%s" type)))
9481       (set var (cons article (symbol-value var)))
9482       (if (memq type '(processable cached replied forwarded recent saved))
9483           (gnus-summary-update-secondary-mark article)
9484         ;;; !!! This is bogus.  We should find out what primary
9485         ;;; !!! mark we want to set.
9486         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9487
9488 (defun gnus-summary-mark-as-expirable (n)
9489   "Mark N articles forward as expirable.
9490 If N is negative, mark backward instead.  The difference between N and
9491 the actual number of articles marked is returned."
9492   (interactive "p")
9493   (gnus-summary-mark-forward n gnus-expirable-mark))
9494
9495 (defun gnus-summary-mark-as-spam (n)
9496   "Mark N articles forward as spam.
9497 If N is negative, mark backward instead.  The difference between N and
9498 the actual number of articles marked is returned."
9499   (interactive "p")
9500   (gnus-summary-mark-forward n gnus-spam-mark))
9501
9502 (defun gnus-summary-mark-article-as-replied (article)
9503   "Mark ARTICLE as replied to and update the summary line.
9504 ARTICLE can also be a list of articles."
9505   (interactive (list (gnus-summary-article-number)))
9506   (let ((articles (if (listp article) article (list article))))
9507     (dolist (article articles)
9508       (unless (numberp article)
9509         (error "%s is not a number" article))
9510       (push article gnus-newsgroup-replied)
9511       (let ((buffer-read-only nil))
9512         (when (gnus-summary-goto-subject article nil t)
9513           (gnus-summary-update-secondary-mark article))))))
9514
9515 (defun gnus-summary-mark-article-as-forwarded (article)
9516   "Mark ARTICLE as forwarded and update the summary line.
9517 ARTICLE can also be a list of articles."
9518   (let ((articles (if (listp article) article (list article))))
9519     (dolist (article articles)
9520       (push article gnus-newsgroup-forwarded)
9521       (let ((buffer-read-only nil))
9522         (when (gnus-summary-goto-subject article nil t)
9523           (gnus-summary-update-secondary-mark article))))))
9524
9525 (defun gnus-summary-set-bookmark (article)
9526   "Set a bookmark in current article."
9527   (interactive (list (gnus-summary-article-number)))
9528   (when (or (not (get-buffer gnus-article-buffer))
9529             (not gnus-current-article)
9530             (not gnus-article-current)
9531             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9532     (error "No current article selected"))
9533   ;; Remove old bookmark, if one exists.
9534   (gnus-pull article gnus-newsgroup-bookmarks)
9535   ;; Set the new bookmark, which is on the form
9536   ;; (article-number . line-number-in-body).
9537   (push
9538    (cons article
9539          (save-excursion
9540            (set-buffer gnus-article-buffer)
9541            (count-lines
9542             (min (point)
9543                  (save-excursion
9544                    (article-goto-body)
9545                    (point)))
9546             (point))))
9547    gnus-newsgroup-bookmarks)
9548   (gnus-message 6 "A bookmark has been added to the current article."))
9549
9550 (defun gnus-summary-remove-bookmark (article)
9551   "Remove the bookmark from the current article."
9552   (interactive (list (gnus-summary-article-number)))
9553   ;; Remove old bookmark, if one exists.
9554   (if (not (assq article gnus-newsgroup-bookmarks))
9555       (gnus-message 6 "No bookmark in current article.")
9556     (gnus-pull article gnus-newsgroup-bookmarks)
9557     (gnus-message 6 "Removed bookmark.")))
9558
9559 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9560 (defun gnus-summary-mark-as-dormant (n)
9561   "Mark N articles forward as dormant.
9562 If N is negative, mark backward instead.  The difference between N and
9563 the actual number of articles marked is returned."
9564   (interactive "p")
9565   (gnus-summary-mark-forward n gnus-dormant-mark))
9566
9567 (defun gnus-summary-set-process-mark (article)
9568   "Set the process mark on ARTICLE and update the summary line."
9569   (setq gnus-newsgroup-processable
9570         (cons article
9571               (delq article gnus-newsgroup-processable)))
9572   (when (gnus-summary-goto-subject article)
9573     (gnus-summary-show-thread)
9574     (gnus-summary-goto-subject article)
9575     (gnus-summary-update-secondary-mark article)))
9576
9577 (defun gnus-summary-remove-process-mark (article)
9578   "Remove the process mark from ARTICLE and update the summary line."
9579   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9580   (when (gnus-summary-goto-subject article)
9581     (gnus-summary-show-thread)
9582     (gnus-summary-goto-subject article)
9583     (gnus-summary-update-secondary-mark article)))
9584
9585 (defun gnus-summary-set-saved-mark (article)
9586   "Set the process mark on ARTICLE and update the summary line."
9587   (push article gnus-newsgroup-saved)
9588   (when (gnus-summary-goto-subject article)
9589     (gnus-summary-update-secondary-mark article)))
9590
9591 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9592   "Mark N articles as read forwards.
9593 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9594 The difference between N and the actual number of articles marked is
9595 returned.
9596 If NO-EXPIRE, auto-expiry will be inhibited."
9597   (interactive "p")
9598   (gnus-summary-show-thread)
9599   (let ((backward (< n 0))
9600         (gnus-summary-goto-unread
9601          (and gnus-summary-goto-unread
9602               (not (eq gnus-summary-goto-unread 'never))
9603               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9604                                     gnus-ticked-mark gnus-dormant-mark)))))
9605         (n (abs n))
9606         (mark (or mark gnus-del-mark)))
9607     (while (and (> n 0)
9608                 (gnus-summary-mark-article nil mark no-expire)
9609                 (zerop (gnus-summary-next-subject
9610                         (if backward -1 1)
9611                         (and gnus-summary-goto-unread
9612                              (not (eq gnus-summary-goto-unread 'never)))
9613                         t)))
9614       (setq n (1- n)))
9615     (when (/= 0 n)
9616       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9617     (gnus-summary-recenter)
9618     (gnus-summary-position-point)
9619     (gnus-set-mode-line 'summary)
9620     n))
9621
9622 (defun gnus-summary-mark-article-as-read (mark)
9623   "Mark the current article quickly as read with MARK."
9624   (let ((article (gnus-summary-article-number)))
9625     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9626     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9627     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9628     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9629     (push (cons article mark) gnus-newsgroup-reads)
9630     ;; Possibly remove from cache, if that is used.
9631     (when gnus-use-cache
9632       (gnus-cache-enter-remove-article article))
9633     ;; Allow the backend to change the mark.
9634     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9635     ;; Check for auto-expiry.
9636     (when (and gnus-newsgroup-auto-expire
9637                (memq mark gnus-auto-expirable-marks))
9638       (setq mark gnus-expirable-mark)
9639       ;; Let the backend know about the mark change.
9640       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9641       (push article gnus-newsgroup-expirable))
9642     ;; Set the mark in the buffer.
9643     (gnus-summary-update-mark mark 'unread)
9644     t))
9645
9646 (defun gnus-summary-mark-article-as-unread (mark)
9647   "Mark the current article quickly as unread with MARK."
9648   (let* ((article (gnus-summary-article-number))
9649          (old-mark (gnus-summary-article-mark article)))
9650     ;; Allow the backend to change the mark.
9651     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9652     (if (eq mark old-mark)
9653         t
9654       (if (<= article 0)
9655           (progn
9656             (gnus-error 1 "Can't mark negative article numbers")
9657             nil)
9658         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9659         (setq gnus-newsgroup-spam-marked
9660               (delq article gnus-newsgroup-spam-marked))
9661         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9662         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9663         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9664         (cond ((= mark gnus-ticked-mark)
9665                (setq gnus-newsgroup-marked
9666                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9667                                               article)))
9668               ((= mark gnus-spam-mark)
9669                (setq gnus-newsgroup-spam-marked
9670                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9671                                               article)))
9672               ((= mark gnus-dormant-mark)
9673                (setq gnus-newsgroup-dormant
9674                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9675                                               article)))
9676               (t
9677                (setq gnus-newsgroup-unreads
9678                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9679                                               article))))
9680         (gnus-pull article gnus-newsgroup-reads)
9681
9682         ;; See whether the article is to be put in the cache.
9683         (and gnus-use-cache
9684              (vectorp (gnus-summary-article-header article))
9685              (save-excursion
9686                (gnus-cache-possibly-enter-article
9687                 gnus-newsgroup-name article
9688                 (= mark gnus-ticked-mark)
9689                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9690
9691         ;; Fix the mark.
9692         (gnus-summary-update-mark mark 'unread)
9693         t))))
9694
9695 (defun gnus-summary-mark-article (&optional article mark no-expire)
9696   "Mark ARTICLE with MARK.  MARK can be any character.
9697 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9698 `??' (dormant) and `?E' (expirable).
9699 If MARK is nil, then the default character `?r' is used.
9700 If ARTICLE is nil, then the article on the current line will be
9701 marked.
9702 If NO-EXPIRE, auto-expiry will be inhibited."
9703   ;; The mark might be a string.
9704   (when (stringp mark)
9705     (setq mark (aref mark 0)))
9706   ;; If no mark is given, then we check auto-expiring.
9707   (when (null mark)
9708     (setq mark gnus-del-mark))
9709   (when (and (not no-expire)
9710              gnus-newsgroup-auto-expire
9711              (memq mark gnus-auto-expirable-marks))
9712     (setq mark gnus-expirable-mark))
9713   (let ((article (or article (gnus-summary-article-number)))
9714         (old-mark (gnus-summary-article-mark article)))
9715     ;; Allow the backend to change the mark.
9716     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9717     (if (eq mark old-mark)
9718         t
9719       (unless article
9720         (error "No article on current line"))
9721       (if (not (if (or (= mark gnus-unread-mark)
9722                        (= mark gnus-ticked-mark)
9723                        (= mark gnus-spam-mark)
9724                        (= mark gnus-dormant-mark))
9725                    (gnus-mark-article-as-unread article mark)
9726                  (gnus-mark-article-as-read article mark)))
9727           t
9728         ;; See whether the article is to be put in the cache.
9729         (and gnus-use-cache
9730              (not (= mark gnus-canceled-mark))
9731              (vectorp (gnus-summary-article-header article))
9732              (save-excursion
9733                (gnus-cache-possibly-enter-article
9734                 gnus-newsgroup-name article
9735                 (= mark gnus-ticked-mark)
9736                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9737
9738         (when (gnus-summary-goto-subject article nil t)
9739           (let ((buffer-read-only nil))
9740             (gnus-summary-show-thread)
9741             ;; Fix the mark.
9742             (gnus-summary-update-mark mark 'unread)
9743             t))))))
9744
9745 (defun gnus-summary-update-secondary-mark (article)
9746   "Update the secondary (read, process, cache) mark."
9747   (gnus-summary-update-mark
9748    (cond ((memq article gnus-newsgroup-processable)
9749           gnus-process-mark)
9750          ((memq article gnus-newsgroup-cached)
9751           gnus-cached-mark)
9752          ((memq article gnus-newsgroup-replied)
9753           gnus-replied-mark)
9754          ((memq article gnus-newsgroup-forwarded)
9755           gnus-forwarded-mark)
9756          ((memq article gnus-newsgroup-saved)
9757           gnus-saved-mark)
9758          ((memq article gnus-newsgroup-recent)
9759           gnus-recent-mark)
9760          ((memq article gnus-newsgroup-unseen)
9761           gnus-unseen-mark)
9762          (t gnus-no-mark))
9763    'replied)
9764   (when (gnus-visual-p 'summary-highlight 'highlight)
9765     (gnus-run-hooks 'gnus-summary-update-hook))
9766   t)
9767
9768 (defun gnus-summary-update-download-mark (article)
9769   "Update the secondary (read, process, cache) mark."
9770   (gnus-summary-update-mark
9771    (cond ((memq article gnus-newsgroup-undownloaded) 
9772           gnus-undownloaded-mark)
9773          (gnus-newsgroup-agentized
9774           gnus-downloaded-mark)
9775          (t
9776           gnus-no-mark))
9777    'download)
9778   (gnus-summary-update-line t)
9779   t)
9780
9781 (defun gnus-summary-update-mark (mark type)
9782   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9783         (buffer-read-only nil))
9784     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9785     (when forward
9786       (when (looking-at "\r")
9787         (incf forward))
9788       (when (<= (+ forward (point)) (point-max))
9789         ;; Go to the right position on the line.
9790         (goto-char (+ forward (point)))
9791         ;; Replace the old mark with the new mark.
9792         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9793         ;; Optionally update the marks by some user rule.
9794         (when (eq type 'unread)
9795           (gnus-data-set-mark
9796            (gnus-data-find (gnus-summary-article-number)) mark)
9797           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9798
9799 (defun gnus-mark-article-as-read (article &optional mark)
9800   "Enter ARTICLE in the pertinent lists and remove it from others."
9801   ;; Make the article expirable.
9802   (let ((mark (or mark gnus-del-mark)))
9803     (setq gnus-newsgroup-expirable
9804           (if (= mark gnus-expirable-mark)
9805               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9806             (delq article gnus-newsgroup-expirable)))
9807     ;; Remove from unread and marked lists.
9808     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9809     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9810     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9811     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9812     (push (cons article mark) gnus-newsgroup-reads)
9813     ;; Possibly remove from cache, if that is used.
9814     (when gnus-use-cache
9815       (gnus-cache-enter-remove-article article))
9816     t))
9817
9818 (defun gnus-mark-article-as-unread (article &optional mark)
9819   "Enter ARTICLE in the pertinent lists and remove it from others."
9820   (let ((mark (or mark gnus-ticked-mark)))
9821     (if (<= article 0)
9822         (progn
9823           (gnus-error 1 "Can't mark negative article numbers")
9824           nil)
9825       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9826             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9827             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9828             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9829             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9830
9831       ;; Unsuppress duplicates?
9832       (when gnus-suppress-duplicates
9833         (gnus-dup-unsuppress-article article))
9834
9835       (cond ((= mark gnus-ticked-mark)
9836              (setq gnus-newsgroup-marked
9837                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9838             ((= mark gnus-spam-mark)
9839              (setq gnus-newsgroup-spam-marked
9840                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9841                                             article)))
9842             ((= mark gnus-dormant-mark)
9843              (setq gnus-newsgroup-dormant
9844                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9845             (t
9846              (setq gnus-newsgroup-unreads
9847                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9848       (gnus-pull article gnus-newsgroup-reads)
9849       t)))
9850
9851 (defalias 'gnus-summary-mark-as-unread-forward
9852   'gnus-summary-tick-article-forward)
9853 (make-obsolete 'gnus-summary-mark-as-unread-forward
9854                'gnus-summary-tick-article-forward)
9855 (defun gnus-summary-tick-article-forward (n)
9856   "Tick N articles forwards.
9857 If N is negative, tick backwards instead.
9858 The difference between N and the number of articles ticked is returned."
9859   (interactive "p")
9860   (gnus-summary-mark-forward n gnus-ticked-mark))
9861
9862 (defalias 'gnus-summary-mark-as-unread-backward
9863   'gnus-summary-tick-article-backward)
9864 (make-obsolete 'gnus-summary-mark-as-unread-backward
9865                'gnus-summary-tick-article-backward)
9866 (defun gnus-summary-tick-article-backward (n)
9867   "Tick N articles backwards.
9868 The difference between N and the number of articles ticked is returned."
9869   (interactive "p")
9870   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9871
9872 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9873 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9874 (defun gnus-summary-tick-article (&optional article clear-mark)
9875   "Mark current article as unread.
9876 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9877 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9878   (interactive)
9879   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9880                                        gnus-ticked-mark)))
9881
9882 (defun gnus-summary-mark-as-read-forward (n)
9883   "Mark N articles as read forwards.
9884 If N is negative, mark backwards instead.
9885 The difference between N and the actual number of articles marked is
9886 returned."
9887   (interactive "p")
9888   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9889
9890 (defun gnus-summary-mark-as-read-backward (n)
9891   "Mark the N articles as read backwards.
9892 The difference between N and the actual number of articles marked is
9893 returned."
9894   (interactive "p")
9895   (gnus-summary-mark-forward
9896    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9897
9898 (defun gnus-summary-mark-as-read (&optional article mark)
9899   "Mark current article as read.
9900 ARTICLE specifies the article to be marked as read.
9901 MARK specifies a string to be inserted at the beginning of the line."
9902   (gnus-summary-mark-article article mark))
9903
9904 (defun gnus-summary-clear-mark-forward (n)
9905   "Clear marks from N articles forward.
9906 If N is negative, clear backward instead.
9907 The difference between N and the number of marks cleared is returned."
9908   (interactive "p")
9909   (gnus-summary-mark-forward n gnus-unread-mark))
9910
9911 (defun gnus-summary-clear-mark-backward (n)
9912   "Clear marks from N articles backward.
9913 The difference between N and the number of marks cleared is returned."
9914   (interactive "p")
9915   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9916
9917 (defun gnus-summary-mark-unread-as-read ()
9918   "Intended to be used by `gnus-summary-mark-article-hook'."
9919   (when (memq gnus-current-article gnus-newsgroup-unreads)
9920     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9921
9922 (defun gnus-summary-mark-read-and-unread-as-read ()
9923   "Intended to be used by `gnus-summary-mark-article-hook'."
9924   (let ((mark (gnus-summary-article-mark)))
9925     (when (or (gnus-unread-mark-p mark)
9926               (gnus-read-mark-p mark))
9927       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9928
9929 (defun gnus-summary-mark-unread-as-ticked ()
9930   "Intended to be used by `gnus-summary-mark-article-hook'."
9931   (when (memq gnus-current-article gnus-newsgroup-unreads)
9932     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9933
9934 (defun gnus-summary-mark-region-as-read (point mark all)
9935   "Mark all unread articles between point and mark as read.
9936 If given a prefix, mark all articles between point and mark as read,
9937 even ticked and dormant ones."
9938   (interactive "r\nP")
9939   (save-excursion
9940     (let (article)
9941       (goto-char point)
9942       (beginning-of-line)
9943       (while (and
9944               (< (point) mark)
9945               (progn
9946                 (when (or all
9947                           (memq (setq article (gnus-summary-article-number))
9948                                 gnus-newsgroup-unreads))
9949                   (gnus-summary-mark-article article gnus-del-mark))
9950                 t)
9951               (gnus-summary-find-next))))))
9952
9953 (defun gnus-summary-mark-below (score mark)
9954   "Mark articles with score less than SCORE with MARK."
9955   (interactive "P\ncMark: ")
9956   (setq score (if score
9957                   (prefix-numeric-value score)
9958                 (or gnus-summary-default-score 0)))
9959   (save-excursion
9960     (set-buffer gnus-summary-buffer)
9961     (goto-char (point-min))
9962     (while
9963         (progn
9964           (and (< (gnus-summary-article-score) score)
9965                (gnus-summary-mark-article nil mark))
9966           (gnus-summary-find-next)))))
9967
9968 (defun gnus-summary-kill-below (&optional score)
9969   "Mark articles with score below SCORE as read."
9970   (interactive "P")
9971   (gnus-summary-mark-below score gnus-killed-mark))
9972
9973 (defun gnus-summary-clear-above (&optional score)
9974   "Clear all marks from articles with score above SCORE."
9975   (interactive "P")
9976   (gnus-summary-mark-above score gnus-unread-mark))
9977
9978 (defun gnus-summary-tick-above (&optional score)
9979   "Tick all articles with score above SCORE."
9980   (interactive "P")
9981   (gnus-summary-mark-above score gnus-ticked-mark))
9982
9983 (defun gnus-summary-mark-above (score mark)
9984   "Mark articles with score over SCORE with MARK."
9985   (interactive "P\ncMark: ")
9986   (setq score (if score
9987                   (prefix-numeric-value score)
9988                 (or gnus-summary-default-score 0)))
9989   (save-excursion
9990     (set-buffer gnus-summary-buffer)
9991     (goto-char (point-min))
9992     (while (and (progn
9993                   (when (> (gnus-summary-article-score) score)
9994                     (gnus-summary-mark-article nil mark))
9995                   t)
9996                 (gnus-summary-find-next)))))
9997
9998 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9999 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10000 (defun gnus-summary-limit-include-expunged (&optional no-error)
10001   "Display all the hidden articles that were expunged for low scores."
10002   (interactive)
10003   (let ((buffer-read-only nil))
10004     (let ((scored gnus-newsgroup-scored)
10005           headers h)
10006       (while scored
10007         (unless (gnus-summary-article-header (caar scored))
10008           (and (setq h (gnus-number-to-header (caar scored)))
10009                (< (cdar scored) gnus-summary-expunge-below)
10010                (push h headers)))
10011         (setq scored (cdr scored)))
10012       (if (not headers)
10013           (when (not no-error)
10014             (error "No expunged articles hidden"))
10015         (goto-char (point-min))
10016         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10017         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10018         (mapcar (lambda (x) (push (mail-header-number x)
10019                                   gnus-newsgroup-limit))
10020                 headers)
10021         (gnus-summary-prepare-unthreaded (nreverse headers))
10022         (goto-char (point-min))
10023         (gnus-summary-position-point)
10024         t))))
10025
10026 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10027   "Mark all unread articles in this newsgroup as read.
10028 If prefix argument ALL is non-nil, ticked and dormant articles will
10029 also be marked as read.
10030 If QUIETLY is non-nil, no questions will be asked.
10031 If TO-HERE is non-nil, it should be a point in the buffer.  All
10032 articles before (after, if REVERSE is set) this point will be marked as read.
10033 Note that this function will only catch up the unread article
10034 in the current summary buffer limitation.
10035 The number of articles marked as read is returned."
10036   (interactive "P")
10037   (prog1
10038       (save-excursion
10039         (when (or quietly
10040                   (not gnus-interactive-catchup) ;Without confirmation?
10041                   gnus-expert-user
10042                   (gnus-y-or-n-p
10043                    (if all
10044                        "Mark absolutely all articles as read? "
10045                      "Mark all unread articles as read? ")))
10046           (if (and not-mark
10047                    (not gnus-newsgroup-adaptive)
10048                    (not gnus-newsgroup-auto-expire)
10049                    (not gnus-suppress-duplicates)
10050                    (or (not gnus-use-cache)
10051                        (eq gnus-use-cache 'passive)))
10052               (progn
10053                 (when all
10054                   (setq gnus-newsgroup-marked nil
10055                         gnus-newsgroup-spam-marked nil
10056                         gnus-newsgroup-dormant nil))
10057                 (setq gnus-newsgroup-unreads
10058                       (gnus-sorted-nunion
10059                        (gnus-intersection gnus-newsgroup-unreads
10060                                           gnus-newsgroup-downloadable)
10061                        gnus-newsgroup-unfetched)))
10062             ;; We actually mark all articles as canceled, which we
10063             ;; have to do when using auto-expiry or adaptive scoring.
10064             (gnus-summary-show-all-threads)
10065             (if (and to-here reverse)
10066                 (progn
10067                   (goto-char to-here)
10068                   (while (and
10069                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10070                           (gnus-summary-find-next (not all)))))
10071               (when (gnus-summary-first-subject (not all))
10072                 (while (and
10073                         (if to-here (< (point) to-here) t)
10074                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10075                         (gnus-summary-find-next (not all))))))
10076             (gnus-set-mode-line 'summary))
10077           t))
10078     (gnus-summary-position-point)))
10079
10080 (defun gnus-summary-catchup-to-here (&optional all)
10081   "Mark all unticked articles before the current one as read.
10082 If ALL is non-nil, also mark ticked and dormant articles as read."
10083   (interactive "P")
10084   (save-excursion
10085     (gnus-save-hidden-threads
10086       (let ((beg (point)))
10087         ;; We check that there are unread articles.
10088         (when (or all (gnus-summary-find-prev))
10089           (gnus-summary-catchup all t beg)))))
10090   (gnus-summary-position-point))
10091
10092 (defun gnus-summary-catchup-from-here (&optional all)
10093   "Mark all unticked articles after the current one as read.
10094 If ALL is non-nil, also mark ticked and dormant articles as read."
10095   (interactive "P")
10096   (save-excursion
10097     (gnus-save-hidden-threads
10098       (let ((beg (point)))
10099         ;; We check that there are unread articles.
10100         (when (or all (gnus-summary-find-next))
10101           (gnus-summary-catchup all t beg nil t)))))
10102
10103   (gnus-summary-position-point))
10104 (defun gnus-summary-catchup-all (&optional quietly)
10105   "Mark all articles in this newsgroup as read.
10106 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10107 instead, which marks only unread articles as read."
10108   (interactive "P")
10109   (gnus-summary-catchup t quietly))
10110
10111 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10112   "Mark all unread articles in this group as read, then exit.
10113 If prefix argument ALL is non-nil, all articles are marked as read.
10114 If QUIETLY is non-nil, no questions will be asked."
10115   (interactive "P")
10116   (when (gnus-summary-catchup all quietly nil 'fast)
10117     ;; Select next newsgroup or exit.
10118     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10119              (eq gnus-auto-select-next 'quietly))
10120         (gnus-summary-next-group nil)
10121       (gnus-summary-exit))))
10122
10123 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10124   "Mark all articles in this newsgroup as read, and then exit.
10125 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10126 instead, which marks only unread articles as read."
10127   (interactive "P")
10128   (gnus-summary-catchup-and-exit t quietly))
10129
10130 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10131   "Mark all articles in this group as read and select the next group.
10132 If given a prefix, mark all articles, unread as well as ticked, as
10133 read."
10134   (interactive "P")
10135   (save-excursion
10136     (gnus-summary-catchup all))
10137   (gnus-summary-next-group))
10138
10139 ;;;
10140 ;;; with article
10141 ;;;
10142
10143 (defmacro gnus-with-article (article &rest forms)
10144   "Select ARTICLE and perform FORMS in the original article buffer.
10145 Then replace the article with the result."
10146   `(progn
10147      ;; We don't want the article to be marked as read.
10148      (let (gnus-mark-article-hook)
10149        (gnus-summary-select-article t t nil ,article))
10150      (set-buffer gnus-original-article-buffer)
10151      ,@forms
10152      (if (not (gnus-check-backend-function
10153                'request-replace-article (car gnus-article-current)))
10154          (gnus-message 5 "Read-only group; not replacing")
10155        (unless (gnus-request-replace-article
10156                 ,article (car gnus-article-current)
10157                 (current-buffer) t)
10158          (error "Couldn't replace article")))
10159      ;; The cache and backlog have to be flushed somewhat.
10160      (when gnus-keep-backlog
10161        (gnus-backlog-remove-article
10162         (car gnus-article-current) (cdr gnus-article-current)))
10163      (when gnus-use-cache
10164        (gnus-cache-update-article
10165         (car gnus-article-current) (cdr gnus-article-current)))))
10166
10167 (put 'gnus-with-article 'lisp-indent-function 1)
10168 (put 'gnus-with-article 'edebug-form-spec '(form body))
10169
10170 ;; Thread-based commands.
10171
10172 (defun gnus-summary-articles-in-thread (&optional article)
10173   "Return a list of all articles in the current thread.
10174 If ARTICLE is non-nil, return all articles in the thread that starts
10175 with that article."
10176   (let* ((article (or article (gnus-summary-article-number)))
10177          (data (gnus-data-find-list article))
10178          (top-level (gnus-data-level (car data)))
10179          (top-subject
10180           (cond ((null gnus-thread-operation-ignore-subject)
10181                  (gnus-simplify-subject-re
10182                   (mail-header-subject (gnus-data-header (car data)))))
10183                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10184                  (gnus-simplify-subject-fuzzy
10185                   (mail-header-subject (gnus-data-header (car data)))))
10186                 (t nil)))
10187          (end-point (save-excursion
10188                       (if (gnus-summary-go-to-next-thread)
10189                           (point) (point-max))))
10190          articles)
10191     (while (and data
10192                 (< (gnus-data-pos (car data)) end-point))
10193       (when (or (not top-subject)
10194                 (string= top-subject
10195                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10196                              (gnus-simplify-subject-fuzzy
10197                               (mail-header-subject
10198                                (gnus-data-header (car data))))
10199                            (gnus-simplify-subject-re
10200                             (mail-header-subject
10201                              (gnus-data-header (car data)))))))
10202         (push (gnus-data-number (car data)) articles))
10203       (unless (and (setq data (cdr data))
10204                    (> (gnus-data-level (car data)) top-level))
10205         (setq data nil)))
10206     ;; Return the list of articles.
10207     (nreverse articles)))
10208
10209 (defun gnus-summary-rethread-current ()
10210   "Rethread the thread the current article is part of."
10211   (interactive)
10212   (let* ((gnus-show-threads t)
10213          (article (gnus-summary-article-number))
10214          (id (mail-header-id (gnus-summary-article-header)))
10215          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10216     (unless id
10217       (error "No article on the current line"))
10218     (gnus-rebuild-thread id)
10219     (gnus-summary-goto-subject article)))
10220
10221 (defun gnus-summary-reparent-thread ()
10222   "Make the current article child of the marked (or previous) article.
10223
10224 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10225 is non-nil or the Subject: of both articles are the same."
10226   (interactive)
10227   (unless (not (gnus-group-read-only-p))
10228     (error "The current newsgroup does not support article editing"))
10229   (unless (<= (length gnus-newsgroup-processable) 1)
10230     (error "No more than one article may be marked"))
10231   (save-window-excursion
10232     (let ((gnus-article-buffer " *reparent*")
10233           (current-article (gnus-summary-article-number))
10234           ;; First grab the marked article, otherwise one line up.
10235           (parent-article (if (not (null gnus-newsgroup-processable))
10236                               (car gnus-newsgroup-processable)
10237                             (save-excursion
10238                               (if (eq (forward-line -1) 0)
10239                                   (gnus-summary-article-number)
10240                                 (error "Beginning of summary buffer"))))))
10241       (unless (not (eq current-article parent-article))
10242         (error "An article may not be self-referential"))
10243       (let ((message-id (mail-header-id
10244                          (gnus-summary-article-header parent-article))))
10245         (unless (and message-id (not (equal message-id "")))
10246           (error "No message-id in desired parent"))
10247         (gnus-with-article current-article
10248           (save-restriction
10249             (goto-char (point-min))
10250             (message-narrow-to-head)
10251             (if (re-search-forward "^References: " nil t)
10252                 (progn
10253                   (re-search-forward "^[^ \t]" nil t)
10254                   (forward-line -1)
10255                   (end-of-line)
10256                   (insert " " message-id))
10257               (insert "References: " message-id "\n"))))
10258         (set-buffer gnus-summary-buffer)
10259         (gnus-summary-unmark-all-processable)
10260         (gnus-summary-update-article current-article)
10261         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10262             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10263         (gnus-summary-rethread-current)
10264         (gnus-message 3 "Article %d is now the child of article %d"
10265                       current-article parent-article)))))
10266
10267 (defun gnus-summary-toggle-threads (&optional arg)
10268   "Toggle showing conversation threads.
10269 If ARG is positive number, turn showing conversation threads on."
10270   (interactive "P")
10271   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10272     (setq gnus-show-threads
10273           (if (null arg) (not gnus-show-threads)
10274             (> (prefix-numeric-value arg) 0)))
10275     (gnus-summary-prepare)
10276     (gnus-summary-goto-subject current)
10277     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10278     (gnus-summary-position-point)))
10279
10280 (defun gnus-summary-show-all-threads ()
10281   "Show all threads."
10282   (interactive)
10283   (save-excursion
10284     (let ((buffer-read-only nil))
10285       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10286   (gnus-summary-position-point))
10287
10288 (defun gnus-summary-show-thread ()
10289   "Show thread subtrees.
10290 Returns nil if no thread was there to be shown."
10291   (interactive)
10292   (let ((buffer-read-only nil)
10293         (orig (point))
10294         ;; first goto end then to beg, to have point at beg after let
10295         (end (progn (end-of-line) (point)))
10296         (beg (progn (beginning-of-line) (point))))
10297     (prog1
10298         ;; Any hidden lines here?
10299         (search-forward "\r" end t)
10300       (subst-char-in-region beg end ?\^M ?\n t)
10301       (goto-char orig)
10302       (gnus-summary-position-point))))
10303
10304 (defun gnus-summary-maybe-hide-threads ()
10305   "If requested, hide the threads that should be hidden."
10306   (when (and gnus-show-threads
10307              gnus-thread-hide-subtree)
10308     (gnus-summary-hide-all-threads
10309      (if (or (consp gnus-thread-hide-subtree)
10310              (gnus-functionp gnus-thread-hide-subtree))
10311          (gnus-make-predicate gnus-thread-hide-subtree)
10312        nil))))
10313
10314 ;;; Hiding predicates.
10315
10316 (defun gnus-article-unread-p (header)
10317   (memq (mail-header-number header) gnus-newsgroup-unreads))
10318
10319 (defun gnus-article-unseen-p (header)
10320   (memq (mail-header-number header) gnus-newsgroup-unseen))
10321
10322 (defun gnus-map-articles (predicate articles)
10323   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10324   (apply 'gnus-or (mapcar predicate
10325                           (mapcar 'gnus-summary-article-header articles))))
10326
10327 (defun gnus-summary-hide-all-threads (&optional predicate)
10328   "Hide all thread subtrees.
10329 If PREDICATE is supplied, threads that satisfy this predicate
10330 will not be hidden."
10331   (interactive)
10332   (save-excursion
10333     (goto-char (point-min))
10334     (let ((end nil))
10335       (while (not end)
10336         (when (or (not predicate)
10337                   (gnus-map-articles
10338                    predicate (gnus-summary-article-children)))
10339             (gnus-summary-hide-thread))
10340         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10341   (gnus-summary-position-point))
10342
10343 (defun gnus-summary-hide-thread ()
10344   "Hide thread subtrees.
10345 If PREDICATE is supplied, threads that satisfy this predicate
10346 will not be hidden.
10347 Returns nil if no threads were there to be hidden."
10348   (interactive)
10349   (let ((buffer-read-only nil)
10350         (start (point))
10351         (article (gnus-summary-article-number)))
10352     (goto-char start)
10353     ;; Go forward until either the buffer ends or the subthread
10354     ;; ends.
10355     (when (and (not (eobp))
10356                (or (zerop (gnus-summary-next-thread 1 t))
10357                    (goto-char (point-max))))
10358       (prog1
10359           (if (and (> (point) start)
10360                    (search-backward "\n" start t))
10361               (progn
10362                 (subst-char-in-region start (point) ?\n ?\^M)
10363                 (gnus-summary-goto-subject article))
10364             (goto-char start)
10365             nil)))))
10366
10367 (defun gnus-summary-go-to-next-thread (&optional previous)
10368   "Go to the same level (or less) next thread.
10369 If PREVIOUS is non-nil, go to previous thread instead.
10370 Return the article number moved to, or nil if moving was impossible."
10371   (let ((level (gnus-summary-thread-level))
10372         (way (if previous -1 1))
10373         (beg (point)))
10374     (forward-line way)
10375     (while (and (not (eobp))
10376                 (< level (gnus-summary-thread-level)))
10377       (forward-line way))
10378     (if (eobp)
10379         (progn
10380           (goto-char beg)
10381           nil)
10382       (setq beg (point))
10383       (prog1
10384           (gnus-summary-article-number)
10385         (goto-char beg)))))
10386
10387 (defun gnus-summary-next-thread (n &optional silent)
10388   "Go to the same level next N'th thread.
10389 If N is negative, search backward instead.
10390 Returns the difference between N and the number of skips actually
10391 done.
10392
10393 If SILENT, don't output messages."
10394   (interactive "p")
10395   (let ((backward (< n 0))
10396         (n (abs n)))
10397     (while (and (> n 0)
10398                 (gnus-summary-go-to-next-thread backward))
10399       (decf n))
10400     (unless silent
10401       (gnus-summary-position-point))
10402     (when (and (not silent) (/= 0 n))
10403       (gnus-message 7 "No more threads"))
10404     n))
10405
10406 (defun gnus-summary-prev-thread (n)
10407   "Go to the same level previous N'th thread.
10408 Returns the difference between N and the number of skips actually
10409 done."
10410   (interactive "p")
10411   (gnus-summary-next-thread (- n)))
10412
10413 (defun gnus-summary-go-down-thread ()
10414   "Go down one level in the current thread."
10415   (let ((children (gnus-summary-article-children)))
10416     (when children
10417       (gnus-summary-goto-subject (car children)))))
10418
10419 (defun gnus-summary-go-up-thread ()
10420   "Go up one level in the current thread."
10421   (let ((parent (gnus-summary-article-parent)))
10422     (when parent
10423       (gnus-summary-goto-subject parent))))
10424
10425 (defun gnus-summary-down-thread (n)
10426   "Go down thread N steps.
10427 If N is negative, go up instead.
10428 Returns the difference between N and how many steps down that were
10429 taken."
10430   (interactive "p")
10431   (let ((up (< n 0))
10432         (n (abs n)))
10433     (while (and (> n 0)
10434                 (if up (gnus-summary-go-up-thread)
10435                   (gnus-summary-go-down-thread)))
10436       (setq n (1- n)))
10437     (gnus-summary-position-point)
10438     (when (/= 0 n)
10439       (gnus-message 7 "Can't go further"))
10440     n))
10441
10442 (defun gnus-summary-up-thread (n)
10443   "Go up thread N steps.
10444 If N is negative, go down instead.
10445 Returns the difference between N and how many steps down that were
10446 taken."
10447   (interactive "p")
10448   (gnus-summary-down-thread (- n)))
10449
10450 (defun gnus-summary-top-thread ()
10451   "Go to the top of the thread."
10452   (interactive)
10453   (while (gnus-summary-go-up-thread))
10454   (gnus-summary-article-number))
10455
10456 (defun gnus-summary-kill-thread (&optional unmark)
10457   "Mark articles under current thread as read.
10458 If the prefix argument is positive, remove any kinds of marks.
10459 If the prefix argument is negative, tick articles instead."
10460   (interactive "P")
10461   (when unmark
10462     (setq unmark (prefix-numeric-value unmark)))
10463   (let ((articles (gnus-summary-articles-in-thread)))
10464     (save-excursion
10465       ;; Expand the thread.
10466       (gnus-summary-show-thread)
10467       ;; Mark all the articles.
10468       (while articles
10469         (gnus-summary-goto-subject (car articles))
10470         (cond ((null unmark)
10471                (gnus-summary-mark-article-as-read gnus-killed-mark))
10472               ((> unmark 0)
10473                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10474               (t
10475                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10476         (setq articles (cdr articles))))
10477     ;; Hide killed subtrees.
10478     (and (null unmark)
10479          gnus-thread-hide-killed
10480          (gnus-summary-hide-thread))
10481     ;; If marked as read, go to next unread subject.
10482     (when (null unmark)
10483       ;; Go to next unread subject.
10484       (gnus-summary-next-subject 1 t)))
10485   (gnus-set-mode-line 'summary))
10486
10487 ;; Summary sorting commands
10488
10489 (defun gnus-summary-sort-by-number (&optional reverse)
10490   "Sort the summary buffer by article number.
10491 Argument REVERSE means reverse order."
10492   (interactive "P")
10493   (gnus-summary-sort 'number reverse))
10494
10495 (defun gnus-summary-sort-by-random (&optional reverse)
10496   "Randomize the order in the summary buffer.
10497 Argument REVERSE means to randomize in reverse order."
10498   (interactive "P")
10499   (gnus-summary-sort 'random reverse))
10500
10501 (defun gnus-summary-sort-by-author (&optional reverse)
10502   "Sort the summary buffer by author name alphabetically.
10503 If `case-fold-search' is non-nil, case of letters is ignored.
10504 Argument REVERSE means reverse order."
10505   (interactive "P")
10506   (gnus-summary-sort 'author reverse))
10507
10508 (defun gnus-summary-sort-by-subject (&optional reverse)
10509   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10510 If `case-fold-search' is non-nil, case of letters is ignored.
10511 Argument REVERSE means reverse order."
10512   (interactive "P")
10513   (gnus-summary-sort 'subject reverse))
10514
10515 (defun gnus-summary-sort-by-date (&optional reverse)
10516   "Sort the summary buffer by date.
10517 Argument REVERSE means reverse order."
10518   (interactive "P")
10519   (gnus-summary-sort 'date reverse))
10520
10521 (defun gnus-summary-sort-by-score (&optional reverse)
10522   "Sort the summary buffer by score.
10523 Argument REVERSE means reverse order."
10524   (interactive "P")
10525   (gnus-summary-sort 'score reverse))
10526
10527 (defun gnus-summary-sort-by-lines (&optional reverse)
10528   "Sort the summary buffer by the number of lines.
10529 Argument REVERSE means reverse order."
10530   (interactive "P")
10531   (gnus-summary-sort 'lines reverse))
10532
10533 (defun gnus-summary-sort-by-chars (&optional reverse)
10534   "Sort the summary buffer by article length.
10535 Argument REVERSE means reverse order."
10536   (interactive "P")
10537   (gnus-summary-sort 'chars reverse))
10538
10539 (defun gnus-summary-sort-by-original (&optional reverse)
10540   "Sort the summary buffer using the default sorting method.
10541 Argument REVERSE means reverse order."
10542   (interactive "P")
10543   (let* ((buffer-read-only)
10544          (gnus-summary-prepare-hook nil))
10545     ;; We do the sorting by regenerating the threads.
10546     (gnus-summary-prepare)
10547     ;; Hide subthreads if needed.
10548     (gnus-summary-maybe-hide-threads)))
10549
10550 (defun gnus-summary-sort (predicate reverse)
10551   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10552   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10553          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10554          (gnus-thread-sort-functions
10555           (if (not reverse)
10556               thread
10557             `(lambda (t1 t2)
10558                (,thread t2 t1))))
10559          (gnus-sort-gathered-threads-function
10560           gnus-thread-sort-functions)
10561          (gnus-article-sort-functions
10562           (if (not reverse)
10563               article
10564             `(lambda (t1 t2)
10565                (,article t2 t1))))
10566          (buffer-read-only)
10567          (gnus-summary-prepare-hook nil))
10568     ;; We do the sorting by regenerating the threads.
10569     (gnus-summary-prepare)
10570     ;; Hide subthreads if needed.
10571     (gnus-summary-maybe-hide-threads)))
10572
10573 ;; Summary saving commands.
10574
10575 (defun gnus-summary-save-article (&optional n not-saved)
10576   "Save the current article using the default saver function.
10577 If N is a positive number, save the N next articles.
10578 If N is a negative number, save the N previous articles.
10579 If N is nil and any articles have been marked with the process mark,
10580 save those articles instead.
10581 The variable `gnus-default-article-saver' specifies the saver function."
10582   (interactive "P")
10583   (let* ((articles (gnus-summary-work-articles n))
10584          (save-buffer (save-excursion
10585                         (nnheader-set-temp-buffer " *Gnus Save*")))
10586          (num (length articles))
10587          header file)
10588     (dolist (article articles)
10589       (setq header (gnus-summary-article-header article))
10590       (if (not (vectorp header))
10591           ;; This is a pseudo-article.
10592           (if (assq 'name header)
10593               (gnus-copy-file (cdr (assq 'name header)))
10594             (gnus-message 1 "Article %d is unsaveable" article))
10595         ;; This is a real article.
10596         (save-window-excursion
10597           (let ((gnus-display-mime-function nil)
10598                 (gnus-article-prepare-hook nil))
10599             (gnus-summary-select-article t nil nil article)))
10600         (save-excursion
10601           (set-buffer save-buffer)
10602           (erase-buffer)
10603           (insert-buffer-substring gnus-original-article-buffer))
10604         (setq file (gnus-article-save save-buffer file num))
10605         (gnus-summary-remove-process-mark article)
10606         (unless not-saved
10607           (gnus-summary-set-saved-mark article))))
10608     (gnus-kill-buffer save-buffer)
10609     (gnus-summary-position-point)
10610     (gnus-set-mode-line 'summary)
10611     n))
10612
10613 (defun gnus-summary-pipe-output (&optional arg headers)
10614   "Pipe the current article to a subprocess.
10615 If N is a positive number, pipe the N next articles.
10616 If N is a negative number, pipe the N previous articles.
10617 If N is nil and any articles have been marked with the process mark,
10618 pipe those articles instead.
10619 If HEADERS (the symbolic prefix), include the headers, too."
10620   (interactive (gnus-interactive "P\ny"))
10621   (require 'gnus-art)
10622   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10623         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10624     (gnus-summary-save-article arg t))
10625   (let ((buffer (get-buffer "*Shell Command Output*")))
10626     (when (and buffer
10627                (not (zerop (buffer-size buffer))))
10628       (gnus-configure-windows 'pipe))))
10629
10630 (defun gnus-summary-save-article-mail (&optional arg)
10631   "Append the current article to an mail file.
10632 If N is a positive number, save the N next articles.
10633 If N is a negative number, save the N previous articles.
10634 If N is nil and any articles have been marked with the process mark,
10635 save those articles instead."
10636   (interactive "P")
10637   (require 'gnus-art)
10638   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10639     (gnus-summary-save-article arg)))
10640
10641 (defun gnus-summary-save-article-rmail (&optional arg)
10642   "Append the current article to an rmail file.
10643 If N is a positive number, save the N next articles.
10644 If N is a negative number, save the N previous articles.
10645 If N is nil and any articles have been marked with the process mark,
10646 save those articles instead."
10647   (interactive "P")
10648   (require 'gnus-art)
10649   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10650     (gnus-summary-save-article arg)))
10651
10652 (defun gnus-summary-save-article-file (&optional arg)
10653   "Append the current article to a file.
10654 If N is a positive number, save the N next articles.
10655 If N is a negative number, save the N previous articles.
10656 If N is nil and any articles have been marked with the process mark,
10657 save those articles instead."
10658   (interactive "P")
10659   (require 'gnus-art)
10660   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10661     (gnus-summary-save-article arg)))
10662
10663 (defun gnus-summary-write-article-file (&optional arg)
10664   "Write the current article to a file, deleting the previous file.
10665 If N is a positive number, save the N next articles.
10666 If N is a negative number, save the N previous articles.
10667 If N is nil and any articles have been marked with the process mark,
10668 save those articles instead."
10669   (interactive "P")
10670   (require 'gnus-art)
10671   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10672     (gnus-summary-save-article arg)))
10673
10674 (defun gnus-summary-save-article-body-file (&optional arg)
10675   "Append the current article body to a file.
10676 If N is a positive number, save the N next articles.
10677 If N is a negative number, save the N previous articles.
10678 If N is nil and any articles have been marked with the process mark,
10679 save those articles instead."
10680   (interactive "P")
10681   (require 'gnus-art)
10682   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10683     (gnus-summary-save-article arg)))
10684
10685 (defun gnus-summary-muttprint (&optional arg)
10686   "Print the current article using Muttprint.
10687 If N is a positive number, save the N next articles.
10688 If N is a negative number, save the N previous articles.
10689 If N is nil and any articles have been marked with the process mark,
10690 save those articles instead."
10691   (interactive "P")
10692   (require 'gnus-art)
10693   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10694     (gnus-summary-save-article arg t)))
10695
10696 (defun gnus-summary-pipe-message (program)
10697   "Pipe the current article through PROGRAM."
10698   (interactive "sProgram: ")
10699   (gnus-summary-select-article)
10700   (let ((mail-header-separator ""))
10701     (gnus-eval-in-buffer-window gnus-article-buffer
10702       (save-restriction
10703         (widen)
10704         (let ((start (window-start))
10705               buffer-read-only)
10706           (message-pipe-buffer-body program)
10707           (set-window-start (get-buffer-window (current-buffer)) start))))))
10708
10709 (defun gnus-get-split-value (methods)
10710   "Return a value based on the split METHODS."
10711   (let (split-name method result match)
10712     (when methods
10713       (save-excursion
10714         (set-buffer gnus-original-article-buffer)
10715         (save-restriction
10716           (nnheader-narrow-to-headers)
10717           (while (and methods (not split-name))
10718             (goto-char (point-min))
10719             (setq method (pop methods))
10720             (setq match (car method))
10721             (when (cond
10722                    ((stringp match)
10723                     ;; Regular expression.
10724                     (ignore-errors
10725                       (re-search-forward match nil t)))
10726                    ((gnus-functionp match)
10727                     ;; Function.
10728                     (save-restriction
10729                       (widen)
10730                       (setq result (funcall match gnus-newsgroup-name))))
10731                    ((consp match)
10732                     ;; Form.
10733                     (save-restriction
10734                       (widen)
10735                       (setq result (eval match)))))
10736               (setq split-name (cdr method))
10737               (cond ((stringp result)
10738                      (push (expand-file-name
10739                             result gnus-article-save-directory)
10740                            split-name))
10741                     ((consp result)
10742                      (setq split-name (append result split-name)))))))))
10743     (nreverse split-name)))
10744
10745 (defun gnus-valid-move-group-p (group)
10746   (and (boundp group)
10747        (symbol-name group)
10748        (symbol-value group)
10749        (gnus-get-function (gnus-find-method-for-group
10750                            (symbol-name group)) 'request-accept-article t)))
10751
10752 (defun gnus-read-move-group-name (prompt default articles prefix)
10753   "Read a group name."
10754   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10755          (minibuffer-confirm-incomplete nil) ; XEmacs
10756          (prom
10757           (format "%s %s to:"
10758                   prompt
10759                   (if (> (length articles) 1)
10760                       (format "these %d articles" (length articles))
10761                     "this article")))
10762          (to-newsgroup
10763           (cond
10764            ((null split-name)
10765             (gnus-completing-read-with-default
10766              default prom
10767              gnus-active-hashtb
10768              'gnus-valid-move-group-p
10769              nil prefix
10770              'gnus-group-history))
10771            ((= 1 (length split-name))
10772             (gnus-completing-read-with-default
10773              (car split-name) prom
10774              gnus-active-hashtb
10775              'gnus-valid-move-group-p
10776              nil nil
10777              'gnus-group-history))
10778            (t
10779             (gnus-completing-read-with-default
10780              nil prom
10781              (mapcar (lambda (el) (list el))
10782                      (nreverse split-name))
10783              nil nil nil
10784              'gnus-group-history))))
10785          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10786     (when to-newsgroup
10787       (if (or (string= to-newsgroup "")
10788               (string= to-newsgroup prefix))
10789           (setq to-newsgroup default))
10790       (unless to-newsgroup
10791         (error "No group name entered"))
10792       (or (gnus-active to-newsgroup)
10793           (gnus-activate-group to-newsgroup nil nil to-method)
10794           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10795                                      to-newsgroup))
10796               (or (and (gnus-request-create-group to-newsgroup to-method)
10797                        (gnus-activate-group
10798                         to-newsgroup nil nil to-method)
10799                        (gnus-subscribe-group to-newsgroup))
10800                   (error "Couldn't create group %s" to-newsgroup)))
10801           (error "No such group: %s" to-newsgroup)))
10802     to-newsgroup))
10803
10804 (defun gnus-summary-save-parts (type dir n &optional reverse)
10805   "Save parts matching TYPE to DIR.
10806 If REVERSE, save parts that do not match TYPE."
10807   (interactive
10808    (list (read-string "Save parts of type: "
10809                       (or (car gnus-summary-save-parts-type-history)
10810                           gnus-summary-save-parts-default-mime)
10811                       'gnus-summary-save-parts-type-history)
10812          (setq gnus-summary-save-parts-last-directory
10813                (read-file-name "Save to directory: "
10814                                gnus-summary-save-parts-last-directory
10815                                nil t))
10816          current-prefix-arg))
10817   (gnus-summary-iterate n
10818     (let ((gnus-display-mime-function nil)
10819           (gnus-inhibit-treatment t))
10820       (gnus-summary-select-article))
10821     (save-excursion
10822       (set-buffer gnus-article-buffer)
10823       (let ((handles (or gnus-article-mime-handles
10824                          (mm-dissect-buffer nil gnus-article-loose-mime)
10825                          (and gnus-article-emulate-mime
10826                               (mm-uu-dissect)))))
10827         (when handles
10828           (gnus-summary-save-parts-1 type dir handles reverse)
10829           (unless gnus-article-mime-handles ;; Don't destroy this case.
10830             (mm-destroy-parts handles)))))))
10831
10832 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10833   (if (stringp (car handle))
10834       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10835               (cdr handle))
10836     (when (if reverse
10837               (not (string-match type (mm-handle-media-type handle)))
10838             (string-match type (mm-handle-media-type handle)))
10839       (let ((file (expand-file-name
10840                    (file-name-nondirectory
10841                     (or
10842                      (mail-content-type-get
10843                       (mm-handle-disposition handle) 'filename)
10844                      (concat gnus-newsgroup-name
10845                              "." (number-to-string
10846                                   (cdr gnus-article-current)))))
10847                    dir)))
10848         (unless (file-exists-p file)
10849           (mm-save-part-to-file handle file))))))
10850
10851 ;; Summary extract commands
10852
10853 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10854   (let ((buffer-read-only nil)
10855         (article (gnus-summary-article-number))
10856         after-article b e)
10857     (unless (gnus-summary-goto-subject article)
10858       (error "No such article: %d" article))
10859     (gnus-summary-position-point)
10860     ;; If all commands are to be bunched up on one line, we collect
10861     ;; them here.
10862     (unless gnus-view-pseudos-separately
10863       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10864             files action)
10865         (while ps
10866           (setq action (cdr (assq 'action (car ps))))
10867           (setq files (list (cdr (assq 'name (car ps)))))
10868           (while (and ps (cdr ps)
10869                       (string= (or action "1")
10870                                (or (cdr (assq 'action (cadr ps))) "2")))
10871             (push (cdr (assq 'name (cadr ps))) files)
10872             (setcdr ps (cddr ps)))
10873           (when files
10874             (when (not (string-match "%s" action))
10875               (push " " files))
10876             (push " " files)
10877             (when (assq 'execute (car ps))
10878               (setcdr (assq 'execute (car ps))
10879                       (funcall (if (string-match "%s" action)
10880                                    'format 'concat)
10881                                action
10882                                (mapconcat
10883                                 (lambda (f)
10884                                   (if (equal f " ")
10885                                       f
10886                                     (mm-quote-arg f)))
10887                                 files " ")))))
10888           (setq ps (cdr ps)))))
10889     (if (and gnus-view-pseudos (not not-view))
10890         (while pslist
10891           (when (assq 'execute (car pslist))
10892             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10893                                   (eq gnus-view-pseudos 'not-confirm)))
10894           (setq pslist (cdr pslist)))
10895       (save-excursion
10896         (while pslist
10897           (setq after-article (or (cdr (assq 'article (car pslist)))
10898                                   (gnus-summary-article-number)))
10899           (gnus-summary-goto-subject after-article)
10900           (forward-line 1)
10901           (setq b (point))
10902           (insert "    " (file-name-nondirectory
10903                           (cdr (assq 'name (car pslist))))
10904                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10905           (setq e (point))
10906           (forward-line -1)             ; back to `b'
10907           (gnus-add-text-properties
10908            b (1- e) (list 'gnus-number gnus-reffed-article-number
10909                           gnus-mouse-face-prop gnus-mouse-face))
10910           (gnus-data-enter
10911            after-article gnus-reffed-article-number
10912            gnus-unread-mark b (car pslist) 0 (- e b))
10913           (setq gnus-newsgroup-unreads
10914                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10915                                          gnus-reffed-article-number))
10916           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10917           (setq pslist (cdr pslist)))))))
10918
10919 (defun gnus-pseudos< (p1 p2)
10920   (let ((c1 (cdr (assq 'action p1)))
10921         (c2 (cdr (assq 'action p2))))
10922     (and c1 c2 (string< c1 c2))))
10923
10924 (defun gnus-request-pseudo-article (props)
10925   (cond ((assq 'execute props)
10926          (gnus-execute-command (cdr (assq 'execute props)))))
10927   (let ((gnus-current-article (gnus-summary-article-number)))
10928     (gnus-run-hooks 'gnus-mark-article-hook)))
10929
10930 (defun gnus-execute-command (command &optional automatic)
10931   (save-excursion
10932     (gnus-article-setup-buffer)
10933     (set-buffer gnus-article-buffer)
10934     (setq buffer-read-only nil)
10935     (let ((command (if automatic command
10936                      (read-string "Command: " (cons command 0)))))
10937       (erase-buffer)
10938       (insert "$ " command "\n\n")
10939       (if gnus-view-pseudo-asynchronously
10940           (start-process "gnus-execute" (current-buffer) shell-file-name
10941                          shell-command-switch command)
10942         (call-process shell-file-name nil t nil
10943                       shell-command-switch command)))))
10944
10945 ;; Summary kill commands.
10946
10947 (defun gnus-summary-edit-global-kill (article)
10948   "Edit the \"global\" kill file."
10949   (interactive (list (gnus-summary-article-number)))
10950   (gnus-group-edit-global-kill article))
10951
10952 (defun gnus-summary-edit-local-kill ()
10953   "Edit a local kill file applied to the current newsgroup."
10954   (interactive)
10955   (setq gnus-current-headers (gnus-summary-article-header))
10956   (gnus-group-edit-local-kill
10957    (gnus-summary-article-number) gnus-newsgroup-name))
10958
10959 ;;; Header reading.
10960
10961 (defun gnus-read-header (id &optional header)
10962   "Read the headers of article ID and enter them into the Gnus system."
10963   (let ((group gnus-newsgroup-name)
10964         (gnus-override-method
10965          (or
10966           gnus-override-method
10967           (and (gnus-news-group-p gnus-newsgroup-name)
10968                (car (gnus-refer-article-methods)))))
10969         where)
10970     ;; First we check to see whether the header in question is already
10971     ;; fetched.
10972     (if (stringp id)
10973         ;; This is a Message-ID.
10974         (setq header (or header (gnus-id-to-header id)))
10975       ;; This is an article number.
10976       (setq header (or header (gnus-summary-article-header id))))
10977     (if (and header
10978              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10979         ;; We have found the header.
10980         header
10981       ;; If this is a sparse article, we have to nix out its
10982       ;; previous entry in the thread hashtb.
10983       (when (and header
10984                  (gnus-summary-article-sparse-p (mail-header-number header)))
10985         (let* ((parent (gnus-parent-id (mail-header-references header)))
10986                (thread (and parent (gnus-id-to-thread parent))))
10987           (when thread
10988             (delq (assq header thread) thread))))
10989       ;; We have to really fetch the header to this article.
10990       (save-excursion
10991         (set-buffer nntp-server-buffer)
10992         (when (setq where (gnus-request-head id group))
10993           (nnheader-fold-continuation-lines)
10994           (goto-char (point-max))
10995           (insert ".\n")
10996           (goto-char (point-min))
10997           (insert "211 ")
10998           (princ (cond
10999                   ((numberp id) id)
11000                   ((cdr where) (cdr where))
11001                   (header (mail-header-number header))
11002                   (t gnus-reffed-article-number))
11003                  (current-buffer))
11004           (insert " Article retrieved.\n"))
11005         (if (or (not where)
11006                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11007             ()                          ; Malformed head.
11008           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11009             (when (and (stringp id)
11010                        (not (string= (gnus-group-real-name group)
11011                                      (car where))))
11012               ;; If we fetched by Message-ID and the article came
11013               ;; from a different group, we fudge some bogus article
11014               ;; numbers for this article.
11015               (mail-header-set-number header gnus-reffed-article-number))
11016             (save-excursion
11017               (set-buffer gnus-summary-buffer)
11018               (decf gnus-reffed-article-number)
11019               (gnus-remove-header (mail-header-number header))
11020               (push header gnus-newsgroup-headers)
11021               (setq gnus-current-headers header)
11022               (push (mail-header-number header) gnus-newsgroup-limit)))
11023           header)))))
11024
11025 (defun gnus-remove-header (number)
11026   "Remove header NUMBER from `gnus-newsgroup-headers'."
11027   (if (and gnus-newsgroup-headers
11028            (= number (mail-header-number (car gnus-newsgroup-headers))))
11029       (pop gnus-newsgroup-headers)
11030     (let ((headers gnus-newsgroup-headers))
11031       (while (and (cdr headers)
11032                   (not (= number (mail-header-number (cadr headers)))))
11033         (pop headers))
11034       (when (cdr headers)
11035         (setcdr headers (cddr headers))))))
11036
11037 ;;;
11038 ;;; summary highlights
11039 ;;;
11040
11041 (defun gnus-highlight-selected-summary ()
11042   "Highlight selected article in summary buffer."
11043   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11044   (when gnus-summary-selected-face
11045     (save-excursion
11046       (let* ((beg (progn (beginning-of-line) (point)))
11047              (end (progn (end-of-line) (point)))
11048              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11049              (from (if (get-text-property beg gnus-mouse-face-prop)
11050                        beg
11051                      (or (next-single-property-change
11052                           beg gnus-mouse-face-prop nil end)
11053                          beg)))
11054              (to
11055               (if (= from end)
11056                   (- from 2)
11057                 (or (next-single-property-change
11058                      from gnus-mouse-face-prop nil end)
11059                     end))))
11060         ;; If no mouse-face prop on line we will have to = from = end,
11061         ;; so we highlight the entire line instead.
11062         (when (= (+ to 2) from)
11063           (setq from beg)
11064           (setq to end))
11065         (if gnus-newsgroup-selected-overlay
11066             ;; Move old overlay.
11067             (gnus-move-overlay
11068              gnus-newsgroup-selected-overlay from to (current-buffer))
11069           ;; Create new overlay.
11070           (gnus-overlay-put
11071            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11072            'face gnus-summary-selected-face))))))
11073
11074 (defvar gnus-summary-highlight-line-cached nil)
11075 (defvar gnus-summary-highlight-line-trigger nil)
11076
11077 (defun gnus-summary-highlight-line-0 ()
11078   (if (and (eq gnus-summary-highlight-line-trigger 
11079                gnus-summary-highlight)
11080            gnus-summary-highlight-line-cached)
11081       gnus-summary-highlight-line-cached
11082     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11083           gnus-summary-highlight-line-cached
11084           (let* ((cond (list 'cond))
11085                  (c cond)
11086                  (list gnus-summary-highlight))
11087             (while list
11088               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11089               (setq c (cdr c)
11090                     list (cdr list)))
11091             (gnus-byte-compile (list 'lambda nil cond))))))
11092
11093 (defun gnus-summary-highlight-line ()
11094   "Highlight current line according to `gnus-summary-highlight'."
11095   (let* ((beg (gnus-point-at-bol))
11096          (article (or (gnus-summary-article-number) gnus-current-article))
11097          (score (or (cdr (assq article
11098                                gnus-newsgroup-scored))
11099                     gnus-summary-default-score 0))
11100          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11101          (inhibit-read-only t)
11102          (default gnus-summary-default-score)
11103          (default-high gnus-summary-default-high-score)
11104          (default-low gnus-summary-default-low-score)
11105          (uncached (memq article gnus-newsgroup-undownloaded))
11106          (downloaded (not uncached)))
11107     (let ((face (funcall (gnus-summary-highlight-line-0))))
11108       (unless (eq face (get-text-property beg 'face))
11109         (gnus-put-text-property-excluding-characters-with-faces
11110          beg (gnus-point-at-eol) 'face
11111          (setq face (if (boundp face) (symbol-value face) face)))
11112         (when gnus-summary-highlight-line-function
11113           (funcall gnus-summary-highlight-line-function article face))))))
11114
11115 (defun gnus-update-read-articles (group unread &optional compute)
11116   "Update the list of read articles in GROUP.
11117 UNREAD is a sorted list."
11118   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11119          (entry (gnus-gethash group gnus-newsrc-hashtb))
11120          (info (nth 2 entry))
11121          (prev 1)
11122          read)
11123     (if (or (not info) (not active))
11124         ;; There is no info on this group if it was, in fact,
11125         ;; killed.  Gnus stores no information on killed groups, so
11126         ;; there's nothing to be done.
11127         ;; One could store the information somewhere temporarily,
11128         ;; perhaps...  Hmmm...
11129         ()
11130       ;; Remove any negative articles numbers.
11131       (while (and unread (< (car unread) 0))
11132         (setq unread (cdr unread)))
11133       ;; Remove any expired article numbers
11134       (while (and unread (< (car unread) (car active)))
11135         (setq unread (cdr unread)))
11136       ;; Compute the ranges of read articles by looking at the list of
11137       ;; unread articles.
11138       (while unread
11139         (when (/= (car unread) prev)
11140           (push (if (= prev (1- (car unread))) prev
11141                   (cons prev (1- (car unread))))
11142                 read))
11143         (setq prev (1+ (car unread)))
11144         (setq unread (cdr unread)))
11145       (when (<= prev (cdr active))
11146         (push (cons prev (cdr active)) read))
11147       (setq read (if (> (length read) 1) (nreverse read) read))
11148       (if compute
11149           read
11150         (save-excursion
11151           (let (setmarkundo)
11152             ;; Propagate the read marks to the backend.
11153             (when (gnus-check-backend-function 'request-set-mark group)
11154               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11155                     (add (gnus-remove-from-range read (gnus-info-read info))))
11156                 (when (or add del)
11157                   (unless (gnus-check-group group)
11158                     (error "Can't open server for %s" group))
11159                   (gnus-request-set-mark
11160                    group (delq nil (list (if add (list add 'add '(read)))
11161                                          (if del (list del 'del '(read))))))
11162                   (setq setmarkundo
11163                         `(gnus-request-set-mark
11164                           ,group
11165                           ',(delq nil (list
11166                                        (if del (list del 'add '(read)))
11167                                        (if add (list add 'del '(read))))))))))
11168             (set-buffer gnus-group-buffer)
11169             (gnus-undo-register
11170               `(progn
11171                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11172                  (gnus-info-set-read ',info ',(gnus-info-read info))
11173                  (gnus-get-unread-articles-in-group ',info
11174                                                     (gnus-active ,group))
11175                  (gnus-group-update-group ,group t)
11176                  ,setmarkundo))))
11177         ;; Enter this list into the group info.
11178         (gnus-info-set-read info read)
11179         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11180         (gnus-get-unread-articles-in-group info (gnus-active group))
11181         t))))
11182
11183 (defun gnus-offer-save-summaries ()
11184   "Offer to save all active summary buffers."
11185   (let (buffers)
11186     ;; Go through all buffers and find all summaries.
11187     (dolist (buffer (buffer-list))
11188       (when (and (setq buffer (buffer-name buffer))
11189                  (string-match "Summary" buffer)
11190                  (save-excursion
11191                    (set-buffer buffer)
11192                    ;; We check that this is, indeed, a summary buffer.
11193                    (and (eq major-mode 'gnus-summary-mode)
11194                         ;; Also make sure this isn't bogus.
11195                         gnus-newsgroup-prepared
11196                         ;; Also make sure that this isn't a
11197                         ;; dead summary buffer.
11198                         (not gnus-dead-summary-mode))))
11199         (push buffer buffers)))
11200     ;; Go through all these summary buffers and offer to save them.
11201     (when buffers
11202       (save-excursion
11203         (map-y-or-n-p
11204          "Update summary buffer %s? "
11205          (lambda (buf)
11206            (switch-to-buffer buf)
11207            (gnus-summary-exit))
11208          buffers)))))
11209
11210 (defun gnus-summary-setup-default-charset ()
11211   "Setup newsgroup default charset."
11212   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11213       (setq gnus-newsgroup-charset nil)
11214     (let* ((ignored-charsets
11215             (or gnus-newsgroup-ephemeral-ignored-charsets
11216                 (append
11217                  (and gnus-newsgroup-name
11218                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11219                  gnus-newsgroup-ignored-charsets))))
11220       (setq gnus-newsgroup-charset
11221             (or gnus-newsgroup-ephemeral-charset
11222                 (and gnus-newsgroup-name
11223                      (gnus-parameter-charset gnus-newsgroup-name))
11224                 gnus-default-charset))
11225       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11226            ignored-charsets))))
11227
11228 ;;;
11229 ;;; Mime Commands
11230 ;;;
11231
11232 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11233   "Display the current article buffer fully MIME-buttonized.
11234 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11235 treated as multipart/mixed."
11236   (interactive "P")
11237   (require 'gnus-art)
11238   (let ((gnus-unbuttonized-mime-types nil)
11239         (gnus-mime-display-multipart-as-mixed show-all-parts))
11240     (gnus-summary-show-article)))
11241
11242 (defun gnus-summary-repair-multipart (article)
11243   "Add a Content-Type header to a multipart article without one."
11244   (interactive (list (gnus-summary-article-number)))
11245   (gnus-with-article article
11246     (message-narrow-to-head)
11247     (message-remove-header "Mime-Version")
11248     (goto-char (point-max))
11249     (insert "Mime-Version: 1.0\n")
11250     (widen)
11251     (when (search-forward "\n--" nil t)
11252       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11253         (message-narrow-to-head)
11254         (message-remove-header "Content-Type")
11255         (goto-char (point-max))
11256         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11257                         separator))
11258         (widen))))
11259   (let (gnus-mark-article-hook)
11260     (gnus-summary-select-article t t nil article)))
11261
11262 (defun gnus-summary-toggle-display-buttonized ()
11263   "Toggle the buttonizing of the article buffer."
11264   (interactive)
11265   (require 'gnus-art)
11266   (if (setq gnus-inhibit-mime-unbuttonizing
11267             (not gnus-inhibit-mime-unbuttonizing))
11268       (let ((gnus-unbuttonized-mime-types nil))
11269         (gnus-summary-show-article))
11270     (gnus-summary-show-article)))
11271
11272 ;;;
11273 ;;; Generic summary marking commands
11274 ;;;
11275
11276 (defvar gnus-summary-marking-alist
11277   '((read gnus-del-mark "d")
11278     (unread gnus-unread-mark "u")
11279     (ticked gnus-ticked-mark "!")
11280     (dormant gnus-dormant-mark "?")
11281     (expirable gnus-expirable-mark "e"))
11282   "An alist of names/marks/keystrokes.")
11283
11284 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11285 (defvar gnus-summary-mark-map)
11286
11287 (defun gnus-summary-make-all-marking-commands ()
11288   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11289   (dolist (elem gnus-summary-marking-alist)
11290     (apply 'gnus-summary-make-marking-command elem)))
11291
11292 (defun gnus-summary-make-marking-command (name mark keystroke)
11293   (let ((map (make-sparse-keymap)))
11294     (define-key gnus-summary-generic-mark-map keystroke map)
11295     (dolist (lway `((next "next" next nil "n")
11296                     (next-unread "next unread" next t "N")
11297                     (prev "previous" prev nil "p")
11298                     (prev-unread "previous unread" prev t "P")
11299                     (nomove "" nil nil ,keystroke)))
11300       (let ((func (gnus-summary-make-marking-command-1
11301                    mark (car lway) lway name)))
11302         (setq func (eval func))
11303         (define-key map (nth 4 lway) func)))))
11304
11305 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11306   `(defun ,(intern
11307             (format "gnus-summary-put-mark-as-%s%s"
11308                     name (if (eq way 'nomove)
11309                              ""
11310                            (concat "-" (symbol-name way)))))
11311      (n)
11312      ,(format
11313        "Mark the current article as %s%s.
11314 If N, the prefix, then repeat N times.
11315 If N is negative, move in reverse order.
11316 The difference between N and the actual number of articles marked is
11317 returned."
11318        name (cadr lway))
11319      (interactive "p")
11320      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11321
11322 (defun gnus-summary-generic-mark (n mark move unread)
11323   "Mark N articles with MARK."
11324   (unless (eq major-mode 'gnus-summary-mode)
11325     (error "This command can only be used in the summary buffer"))
11326   (gnus-summary-show-thread)
11327   (let ((nummove
11328          (cond
11329           ((eq move 'next) 1)
11330           ((eq move 'prev) -1)
11331           (t 0))))
11332     (if (zerop nummove)
11333         (setq n 1)
11334       (when (< n 0)
11335         (setq n (abs n)
11336               nummove (* -1 nummove))))
11337     (while (and (> n 0)
11338                 (gnus-summary-mark-article nil mark)
11339                 (zerop (gnus-summary-next-subject nummove unread t)))
11340       (setq n (1- n)))
11341     (when (/= 0 n)
11342       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11343     (gnus-summary-recenter)
11344     (gnus-summary-position-point)
11345     (gnus-set-mode-line 'summary)
11346     n))
11347
11348 (defun gnus-summary-insert-articles (articles)
11349   (when (setq articles
11350               (gnus-sorted-difference articles
11351                                       (mapcar (lambda (h)
11352                                                 (mail-header-number h))
11353                                               gnus-newsgroup-headers)))
11354     (setq gnus-newsgroup-headers
11355           (merge 'list
11356                  gnus-newsgroup-headers
11357                  (gnus-fetch-headers articles)
11358                  'gnus-article-sort-by-number))
11359     ;; Suppress duplicates?
11360     (when gnus-suppress-duplicates
11361       (gnus-dup-suppress-articles))
11362
11363     ;; We might want to build some more threads first.
11364     (when (and gnus-fetch-old-headers
11365                (eq gnus-headers-retrieved-by 'nov))
11366       (if (eq gnus-fetch-old-headers 'invisible)
11367           (gnus-build-all-threads)
11368         (gnus-build-old-threads)))
11369     ;; Let the Gnus agent mark articles as read.
11370     (when gnus-agent
11371       (gnus-agent-get-undownloaded-list))
11372     ;; Remove list identifiers from subject
11373     (when gnus-list-identifiers
11374       (gnus-summary-remove-list-identifiers))
11375     ;; First and last article in this newsgroup.
11376     (when gnus-newsgroup-headers
11377       (setq gnus-newsgroup-begin
11378             (mail-header-number (car gnus-newsgroup-headers))
11379             gnus-newsgroup-end
11380             (mail-header-number
11381              (gnus-last-element gnus-newsgroup-headers))))
11382     (when gnus-use-scoring
11383       (gnus-possibly-score-headers))))
11384
11385 (defun gnus-summary-insert-old-articles (&optional all)
11386   "Insert all old articles in this group.
11387 If ALL is non-nil, already read articles become readable.
11388 If ALL is a number, fetch this number of articles."
11389   (interactive "P")
11390   (prog1
11391       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11392             older len)
11393         (setq older
11394               ;; Some nntp servers lie about their active range.  When
11395               ;; this happens, the active range can be in the millions.
11396               ;; Use a compressed range to avoid creating a huge list.
11397               (gnus-range-difference (list gnus-newsgroup-active) old))
11398         (setq len (gnus-range-length older))
11399         (cond
11400          ((null older) nil)
11401          ((numberp all)
11402           (if (< all len)
11403               (let ((older-range (nreverse older)))
11404                 (setq older nil)
11405
11406                 (while (> all 0)
11407                   (let* ((r (pop older-range))
11408                          (min (if (numberp r) r (car r)))
11409                          (max (if (numberp r) r (cdr r))))
11410                     (while (and (<= min max)
11411                                 (> all 0))
11412                       (push max older)
11413                       (setq all (1- all)
11414                             max (1- max))))))
11415             (setq older (gnus-uncompress-range older))))
11416          (all
11417           (setq older (gnus-uncompress-range older)))
11418          (t
11419           (when (and (numberp gnus-large-newsgroup)
11420                    (> len gnus-large-newsgroup))
11421               (let* ((cursor-in-echo-area nil)
11422                      (initial (gnus-parameter-large-newsgroup-initial
11423                                gnus-newsgroup-name))
11424                      (input
11425                       (read-string
11426                        (format
11427                         "How many articles from %s (%s %d): "
11428                         (gnus-limit-string
11429                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11430                         (if initial "max" "default")
11431                         len)
11432                        (if initial
11433                            (cons (number-to-string initial)
11434                                  0)))))
11435                 (unless (string-match "^[ \t]*$" input)
11436                   (setq all (string-to-number input))
11437                   (if (< all len)
11438                       (let ((older-range (nreverse older)))
11439                         (setq older nil)
11440
11441                         (while (> all 0)
11442                           (let* ((r (pop older-range))
11443                                  (min (if (numberp r) r (car r)))
11444                                  (max (if (numberp r) r (cdr r))))
11445                             (while (and (<= min max)
11446                                         (> all 0))
11447                               (push max older)
11448                               (setq all (1- all)
11449                                     max (1- max))))))))))
11450           (setq older (gnus-uncompress-range older))))
11451         (if (not older)
11452             (message "No old news.")
11453           (gnus-summary-insert-articles older)
11454           (gnus-summary-limit (gnus-sorted-nunion old older))))
11455     (gnus-summary-position-point)))
11456
11457 (defun gnus-summary-insert-new-articles ()
11458   "Insert all new articles in this group."
11459   (interactive)
11460   (prog1
11461       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11462             (old-active gnus-newsgroup-active)
11463             (nnmail-fetched-sources (list t))
11464             i new)
11465         (setq gnus-newsgroup-active
11466               (gnus-activate-group gnus-newsgroup-name 'scan))
11467         (setq i (cdr gnus-newsgroup-active))
11468         (while (> i (cdr old-active))
11469           (push i new)
11470           (decf i))
11471         (if (not new)
11472             (message "No gnus is bad news.")
11473           (gnus-summary-insert-articles new)
11474           (setq gnus-newsgroup-unreads
11475                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11476           (gnus-summary-limit (gnus-sorted-nunion old new))))
11477     (gnus-summary-position-point)))
11478
11479 (gnus-summary-make-all-marking-commands)
11480
11481 (gnus-ems-redefine)
11482
11483 (provide 'gnus-sum)
11484
11485 (run-hooks 'gnus-sum-load-hook)
11486
11487 ;;; gnus-sum.el ends here