* gnus-sum.el (gnus-summary-display-while-building): Docstring fix.
[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
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'mm-uu-dissect "mm-uu")
48 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
49   "Deuglify broken Outlook (Express) articles and redisplay."
50   t)
51 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
52 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
53 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
54
55 (defcustom gnus-kill-summary-on-exit t
56   "*If non-nil, kill the summary buffer when you exit from it.
57 If nil, the summary will become a \"*Dead Summary*\" buffer, and
58 it will be killed sometime later."
59   :group 'gnus-summary-exit
60   :type 'boolean)
61
62 (defcustom gnus-fetch-old-headers nil
63   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
64 If an unread article in the group refers to an older, already read (or
65 just marked as read) article, the old article will not normally be
66 displayed in the Summary buffer.  If this variable is non-nil, Gnus
67 will attempt to grab the headers to the old articles, and thereby
68 build complete threads.  If it has the value `some', only enough
69 headers to connect otherwise loose threads will be displayed.  This
70 variable can also be a number.  In that case, no more than that number
71 of old headers will be fetched.  If it has the value `invisible', all
72 old headers will be fetched, but none will be displayed.
73
74 The server has to support NOV for any of this to work."
75   :group 'gnus-thread
76   :type '(choice (const :tag "off" nil)
77                  (const some)
78                  number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-refer-thread-limit 200
82   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
83 If t, fetch all the available old headers."
84   :group 'gnus-thread
85   :type '(choice number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-summary-make-false-root 'adopt
89   "*nil means that Gnus won't gather loose threads.
90 If the root of a thread has expired or been read in a previous
91 session, the information necessary to build a complete thread has been
92 lost.  Instead of having many small sub-threads from this original thread
93 scattered all over the summary buffer, Gnus can gather them.
94
95 If non-nil, Gnus will try to gather all loose sub-threads from an
96 original thread into one large thread.
97
98 If this variable is non-nil, it should be one of `none', `adopt',
99 `dummy' or `empty'.
100
101 If this variable is `none', Gnus will not make a false root, but just
102 present the sub-threads after another.
103 If this variable is `dummy', Gnus will create a dummy root that will
104 have all the sub-threads as children.
105 If this variable is `adopt', Gnus will make one of the \"children\"
106 the parent and mark all the step-children as such.
107 If this variable is `empty', the \"children\" are printed with empty
108 subject fields.  (Or rather, they will be printed with a string
109 given by the `gnus-summary-same-subject' variable.)"
110   :group 'gnus-thread
111   :type '(choice (const :tag "off" nil)
112                  (const none)
113                  (const dummy)
114                  (const adopt)
115                  (const empty)))
116
117 (defcustom gnus-summary-make-false-root-always nil
118   "Always make a false dummy root."
119   :group 'gnus-thread
120   :type 'boolean)
121
122 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
123   "*A regexp to match subjects to be excluded from loose thread gathering.
124 As loose thread gathering is done on subjects only, that means that
125 there can be many false gatherings performed.  By rooting out certain
126 common subjects, gathering might become saner."
127   :group 'gnus-thread
128   :type 'regexp)
129
130 (defcustom gnus-summary-gather-subject-limit nil
131   "*Maximum length of subject comparisons when gathering loose threads.
132 Use nil to compare full subjects.  Setting this variable to a low
133 number will help gather threads that have been corrupted by
134 newsreaders chopping off subject lines, but it might also mean that
135 unrelated articles that have subject that happen to begin with the
136 same few characters will be incorrectly gathered.
137
138 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
139 comparing subjects."
140   :group 'gnus-thread
141   :type '(choice (const :tag "off" nil)
142                  (const fuzzy)
143                  (sexp :menu-tag "on" t)))
144
145 (defcustom gnus-simplify-subject-functions nil
146   "List of functions taking a string argument that simplify subjects.
147 The functions are applied recursively.
148
149 Useful functions to put in this list include:
150 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
151 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 \(they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-default-high-score 0
216   "*Default threshold for a high scored article.
217 An article will be highlighted as high scored if its score is greater
218 than this score."
219   :group 'gnus-score-default
220   :type 'integer)
221
222 (defcustom gnus-summary-default-low-score 0
223   "*Default threshold for a low scored article.
224 An article will be highlighted as low scored if its score is smaller
225 than this score."
226   :group 'gnus-score-default
227   :type 'integer)
228
229 (defcustom gnus-summary-zcore-fuzz 0
230   "*Fuzziness factor for the zcore in the summary buffer.
231 Articles with scores closer than this to `gnus-summary-default-score'
232 will not be marked."
233   :group 'gnus-summary-format
234   :type 'integer)
235
236 (defcustom gnus-simplify-subject-fuzzy-regexp nil
237   "*Strings to be removed when doing fuzzy matches.
238 This can either be a regular expression or list of regular expressions
239 that will be removed from subject strings if fuzzy subject
240 simplification is selected."
241   :group 'gnus-thread
242   :type '(repeat regexp))
243
244 (defcustom gnus-show-threads t
245   "*If non-nil, display threads in summary mode."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-hide-subtree nil
250   "*If non-nil, hide all threads initially.
251 This can be a predicate specifier which says which threads to hide.
252 If threads are hidden, you have to run the command
253 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
254 to expose hidden threads."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-killed t
259   "*If non-nil, hide killed threads automatically."
260   :group 'gnus-thread
261   :type 'boolean)
262
263 (defcustom gnus-thread-ignore-subject t
264   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
265 If nil, articles that have different subjects from their parents will
266 start separate threads."
267   :group 'gnus-thread
268   :type 'boolean)
269
270 (defcustom gnus-thread-operation-ignore-subject t
271   "*If non-nil, subjects will be ignored when doing thread commands.
272 This affects commands like `gnus-summary-kill-thread' and
273 `gnus-summary-lower-thread'.
274
275 If this variable is nil, articles in the same thread with different
276 subjects will not be included in the operation in question.  If this
277 variable is `fuzzy', only articles that have subjects that are fuzzily
278 equal will be included."
279   :group 'gnus-thread
280   :type '(choice (const :tag "off" nil)
281                  (const fuzzy)
282                  (sexp :tag "on" t)))
283
284 (defcustom gnus-thread-indent-level 4
285   "*Number that says how much each sub-thread should be indented."
286   :group 'gnus-thread
287   :type 'integer)
288
289 (defcustom gnus-auto-extend-newsgroup t
290   "*If non-nil, extend newsgroup forward and backward when requested."
291   :group 'gnus-summary-choose
292   :type 'boolean)
293
294 (defcustom gnus-auto-select-first t
295   "*If non-nil, select the article under point.
296 Which article this is is controlled by the `gnus-auto-select-subject'
297 variable.
298
299 If you want to prevent automatic selection of articles in some
300 newsgroups, set the variable to nil in `gnus-select-group-hook'."
301   :group 'gnus-group-select
302   :type '(choice (const :tag "none" nil)
303                  (sexp :menu-tag "first" t)))
304
305 (defcustom gnus-auto-select-subject 'unread
306   "*Says what subject to place under point when entering a group.
307
308 This variable can either be the symbols `first' (place point on the
309 first subject), `unread' (place point on the subject line of the first
310 unread article), `best' (place point on the subject line of the
311 higest-scored article), `unseen' (place point on the subject line of
312 the first unseen article), 'unseen-or-unread' (place point on the subject
313 line of the first unseen article or, if all article have been seen, on the
314 subject line of the first unread article), or a function to be called to
315 place point on some subject line."
316   :group 'gnus-group-select
317   :type '(choice (const best)
318                  (const unread)
319                  (const first)
320                  (const unseen)
321                  (const unseen-or-unread)))
322
323 (defcustom gnus-auto-select-next t
324   "*If non-nil, offer to go to the next group from the end of the previous.
325 If the value is t and the next newsgroup is empty, Gnus will exit
326 summary mode and go back to group mode.  If the value is neither nil
327 nor t, Gnus will select the following unread newsgroup.  In
328 particular, if the value is the symbol `quietly', the next unread
329 newsgroup will be selected without any confirmation, and if it is
330 `almost-quietly', the next group will be selected without any
331 confirmation if you are located on the last article in the group.
332 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
333 will go to the next group without confirmation."
334   :group 'gnus-summary-maneuvering
335   :type '(choice (const :tag "off" nil)
336                  (const quietly)
337                  (const almost-quietly)
338                  (const slightly-quietly)
339                  (sexp :menu-tag "on" t)))
340
341 (defcustom gnus-auto-select-same nil
342   "*If non-nil, select the next article with the same subject.
343 If there are no more articles with the same subject, go to
344 the first unread article."
345   :group 'gnus-summary-maneuvering
346   :type 'boolean)
347
348 (defcustom gnus-auto-goto-ignores 'unfetched
349   "*Says how to handle unfetched articles when maneuvering.
350
351 This variable can either be the symbols `nil' (maneuver to any
352 article), `undownloaded' (maneuvering while unplugged ignores articles
353 that have not been fetched), `always-undownloaded' (maneuvering always
354 ignores articles that have not been fetched), `unfetched' (maneuvering
355 ignores articles whose headers have not been fetched).
356
357 NOTE: The list of unfetched articles will always be nil when plugged
358 and, when unplugged, a subset of the undownloaded article list."
359   :group 'gnus-summary-maneuvering
360   :type '(choice (const :tag "None" nil)
361                  (const :tag "Undownloaded when unplugged" undownloaded)
362                  (const :tag "Undownloaded" always-undownloaded)
363                  (const :tag "Unfetched" unfetched)))
364
365 (defcustom gnus-summary-check-current nil
366   "*If non-nil, consider the current article when moving.
367 The \"unread\" movement commands will stay on the same line if the
368 current article is unread."
369   :group 'gnus-summary-maneuvering
370   :type 'boolean)
371
372 (defcustom gnus-auto-center-summary t
373   "*If non-nil, always center the current summary buffer.
374 In particular, if `vertical' do only vertical recentering.  If non-nil
375 and non-`vertical', do both horizontal and vertical recentering."
376   :group 'gnus-summary-maneuvering
377   :type '(choice (const :tag "none" nil)
378                  (const vertical)
379                  (integer :tag "height")
380                  (sexp :menu-tag "both" t)))
381
382 (defvar gnus-auto-center-group t
383   "*If non-nil, always center the group buffer.")
384
385 (defcustom gnus-show-all-headers nil
386   "*If non-nil, don't hide any headers."
387   :group 'gnus-article-hiding
388   :group 'gnus-article-headers
389   :type 'boolean)
390
391 (defcustom gnus-summary-ignore-duplicates nil
392   "*If non-nil, ignore articles with identical Message-ID headers."
393   :group 'gnus-summary
394   :type 'boolean)
395
396 (defcustom gnus-single-article-buffer t
397   "*If non-nil, display all articles in the same buffer.
398 If nil, each group will get its own article buffer."
399   :group 'gnus-article-various
400   :type 'boolean)
401
402 (defcustom gnus-break-pages t
403   "*If non-nil, do page breaking on articles.
404 The page delimiter is specified by the `gnus-page-delimiter'
405 variable."
406   :group 'gnus-article-various
407   :type 'boolean)
408
409 (defcustom gnus-move-split-methods nil
410   "*Variable used to suggest where articles are to be moved to.
411 It uses the same syntax as the `gnus-split-methods' variable.
412 However, whereas `gnus-split-methods' specifies file names as targets,
413 this variable specifies group names."
414   :group 'gnus-summary-mail
415   :type '(repeat (choice (list :value (fun) function)
416                          (cons :value ("" "") regexp (repeat string))
417                          (sexp :value nil))))
418
419 (defcustom gnus-unread-mark ?           ;Whitespace
420   "*Mark used for unread articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-ticked-mark ?!
425   "*Mark used for ticked articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-dormant-mark ??
430   "*Mark used for dormant articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-del-mark ?r
435   "*Mark used for del'd articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-read-mark ?R
440   "*Mark used for read articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-expirable-mark ?E
445   "*Mark used for expirable articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-killed-mark ?K
450   "*Mark used for killed articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-spam-mark ?$
455   "*Mark used for spam articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-souped-mark ?F
460   "*Mark used for souped articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-kill-file-mark ?X
465   "*Mark used for articles killed by kill files."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-low-score-mark ?Y
470   "*Mark used for articles with a low score."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-catchup-mark ?C
475   "*Mark used for articles that are caught up."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-replied-mark ?A
480   "*Mark used for articles that have been replied to."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-forwarded-mark ?F
485   "*Mark used for articles that have been forwarded."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-recent-mark ?N
490   "*Mark used for articles that are recent."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-cached-mark ?*
495   "*Mark used for articles that are in the cache."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-saved-mark ?S
500   "*Mark used for articles that have been saved."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-unseen-mark ?.
505   "*Mark used for articles that haven't been seen."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-no-mark ?               ;Whitespace
510   "*Mark used for articles that have no other secondary mark."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-ancient-mark ?O
515   "*Mark used for ancient articles."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-sparse-mark ?Q
520   "*Mark used for sparsely reffed articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-canceled-mark ?G
525   "*Mark used for canceled articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-duplicate-mark ?M
530   "*Mark used for duplicate articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-undownloaded-mark ?-
535   "*Mark used for articles that weren't downloaded."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-downloaded-mark ?+
540   "*Mark used for articles that were downloaded."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-downloadable-mark ?%
545   "*Mark used for articles that are to be downloaded."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-unsendable-mark ?=
550   "*Mark used for articles that won't be sent."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-score-over-mark ?+
555   "*Score mark used for articles with high scores."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-score-below-mark ?-
560   "*Score mark used for articles with low scores."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-empty-thread-mark ?     ;Whitespace
565   "*There is no thread under the article."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-not-empty-thread-mark ?=
570   "*There is a thread under the article."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-view-pseudo-asynchronously nil
575   "*If non-nil, Gnus will view pseudo-articles asynchronously."
576   :group 'gnus-extract-view
577   :type 'boolean)
578
579 (defcustom gnus-auto-expirable-marks
580   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
581         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
582         gnus-souped-mark gnus-duplicate-mark)
583   "*The list of marks converted into expiration if a group is auto-expirable."
584   :version "21.1"
585   :group 'gnus-summary
586   :type '(repeat character))
587
588 (defcustom gnus-inhibit-user-auto-expire t
589   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
590   :version "21.1"
591   :group 'gnus-summary
592   :type 'boolean)
593
594 (defcustom gnus-view-pseudos nil
595   "*If `automatic', pseudo-articles will be viewed automatically.
596 If `not-confirm', pseudos will be viewed automatically, and the user
597 will not be asked to confirm the command."
598   :group 'gnus-extract-view
599   :type '(choice (const :tag "off" nil)
600                  (const automatic)
601                  (const not-confirm)))
602
603 (defcustom gnus-view-pseudos-separately t
604   "*If non-nil, one pseudo-article will be created for each file to be viewed.
605 If nil, all files that use the same viewing command will be given as a
606 list of parameters to that command."
607   :group 'gnus-extract-view
608   :type 'boolean)
609
610 (defcustom gnus-insert-pseudo-articles t
611   "*If non-nil, insert pseudo-articles when decoding articles."
612   :group 'gnus-extract-view
613   :type 'boolean)
614
615 (defcustom gnus-summary-dummy-line-format
616   "   %(:                             :%) %S\n"
617   "*The format specification for the dummy roots in the summary buffer.
618 It works along the same lines as a normal formatting string,
619 with some simple extensions.
620
621 %S  The subject
622
623 General format specifiers can also be used.
624 See `(gnus)Formatting Variables'."
625   :link '(custom-manual "(gnus)Formatting Variables")
626   :group 'gnus-threading
627   :type 'string)
628
629 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
630   "*The format specification for the summary mode line.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions:
633
634 %G  Group name
635 %p  Unprefixed group name
636 %A  Current article number
637 %z  Current article score
638 %V  Gnus version
639 %U  Number of unread articles in the group
640 %e  Number of unselected articles in the group
641 %Z  A string with unread/unselected article counts
642 %g  Shortish group name
643 %S  Subject of the current article
644 %u  User-defined spec
645 %s  Current score file name
646 %d  Number of dormant articles
647 %r  Number of articles that have been marked as read in this session
648 %E  Number of articles expunged by the score files"
649   :group 'gnus-summary-format
650   :type 'string)
651
652 (defcustom gnus-list-identifiers nil
653   "Regexp that matches list identifiers to be removed from subject.
654 This can also be a list of regexps."
655   :version "21.1"
656   :group 'gnus-summary-format
657   :group 'gnus-article-hiding
658   :type '(choice (const :tag "none" nil)
659                  (regexp :value ".*")
660                  (repeat :value (".*") regexp)))
661
662 (defcustom gnus-summary-mark-below 0
663   "*Mark all articles with a score below this variable as read.
664 This variable is local to each summary buffer and usually set by the
665 score file."
666   :group 'gnus-score-default
667   :type 'integer)
668
669 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
670   "*List of functions used for sorting articles in the summary buffer.
671
672 Each function takes two articles and returns non-nil if the first
673 article should be sorted before the other.  If you use more than one
674 function, the primary sort function should be the last.  You should
675 probably always include `gnus-article-sort-by-number' in the list of
676 sorting functions -- preferably first.  Also note that sorting by date
677 is often much slower than sorting by number, and the sorting order is
678 very similar.  (Sorting by date means sorting by the time the message
679 was sent, sorting by number means sorting by arrival time.)
680
681 Ready-made functions include `gnus-article-sort-by-number',
682 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
683 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
684 and `gnus-article-sort-by-score'.
685
686 When threading is turned on, the variable `gnus-thread-sort-functions'
687 controls how articles are sorted."
688   :group 'gnus-summary-sort
689   :type '(repeat (choice (function-item gnus-article-sort-by-number)
690                          (function-item gnus-article-sort-by-author)
691                          (function-item gnus-article-sort-by-subject)
692                          (function-item gnus-article-sort-by-date)
693                          (function-item gnus-article-sort-by-score)
694                          (function-item gnus-article-sort-by-random)
695                          (function :tag "other"))))
696
697 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
698   "*List of functions used for sorting threads in the summary buffer.
699 By default, threads are sorted by article number.
700
701 Each function takes two threads and returns non-nil if the first
702 thread should be sorted before the other.  If you use more than one
703 function, the primary sort function should be the last.  You should
704 probably always include `gnus-thread-sort-by-number' in the list of
705 sorting functions -- preferably first.  Also note that sorting by date
706 is often much slower than sorting by number, and the sorting order is
707 very similar.  (Sorting by date means sorting by the time the message
708 was sent, sorting by number means sorting by arrival time.)
709
710 Ready-made functions include `gnus-thread-sort-by-number',
711 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
712 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
713 `gnus-thread-sort-by-most-recent-number',
714 `gnus-thread-sort-by-most-recent-date',
715 `gnus-thread-sort-by-random', and
716 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
717
718 When threading is turned off, the variable
719 `gnus-article-sort-functions' controls how articles are sorted."
720   :group 'gnus-summary-sort
721   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
722                          (function-item gnus-thread-sort-by-author)
723                          (function-item gnus-thread-sort-by-subject)
724                          (function-item gnus-thread-sort-by-date)
725                          (function-item gnus-thread-sort-by-score)
726                          (function-item gnus-thread-sort-by-total-score)
727                          (function-item gnus-thread-sort-by-random)
728                          (function :tag "other"))))
729
730 (defcustom gnus-thread-score-function '+
731   "*Function used for calculating the total score of a thread.
732
733 The function is called with the scores of the article and each
734 subthread and should then return the score of the thread.
735
736 Some functions you can use are `+', `max', or `min'."
737   :group 'gnus-summary-sort
738   :type 'function)
739
740 (defcustom gnus-summary-expunge-below nil
741   "All articles that have a score less than this variable will be expunged.
742 This variable is local to the summary buffers."
743   :group 'gnus-score-default
744   :type '(choice (const :tag "off" nil)
745                  integer))
746
747 (defcustom gnus-thread-expunge-below nil
748   "All threads that have a total score less than this variable will be expunged.
749 See `gnus-thread-score-function' for en explanation of what a
750 \"thread score\" is.
751
752 This variable is local to the summary buffers."
753   :group 'gnus-threading
754   :group 'gnus-score-default
755   :type '(choice (const :tag "off" nil)
756                  integer))
757
758 (defcustom gnus-summary-mode-hook nil
759   "*A hook for Gnus summary mode.
760 This hook is run before any variables are set in the summary buffer."
761   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
762   :group 'gnus-summary-various
763   :type 'hook)
764
765 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
766 (when (featurep 'xemacs)
767   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
768   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
769   (add-hook 'gnus-summary-mode-hook
770             'gnus-xmas-switch-horizontal-scrollbar-off))
771
772 (defcustom gnus-summary-menu-hook nil
773   "*Hook run after the creation of the summary mode menu."
774   :group 'gnus-summary-visual
775   :type 'hook)
776
777 (defcustom gnus-summary-exit-hook nil
778   "*A hook called on exit from the summary buffer.
779 It will be called with point in the group buffer."
780   :group 'gnus-summary-exit
781   :type 'hook)
782
783 (defcustom gnus-summary-prepare-hook nil
784   "*A hook called after the summary buffer has been generated.
785 If you want to modify the summary buffer, you can use this hook."
786   :group 'gnus-summary-various
787   :type 'hook)
788
789 (defcustom gnus-summary-prepared-hook nil
790   "*A hook called as the last thing after the summary buffer has been generated."
791   :group 'gnus-summary-various
792   :type 'hook)
793
794 (defcustom gnus-summary-generate-hook nil
795   "*A hook run just before generating the summary buffer.
796 This hook is commonly used to customize threading variables and the
797 like."
798   :group 'gnus-summary-various
799   :type 'hook)
800
801 (defcustom gnus-select-group-hook nil
802   "*A hook called when a newsgroup is selected.
803
804 If you'd like to simplify subjects like the
805 `gnus-summary-next-same-subject' command does, you can use the
806 following hook:
807
808  (add-hook gnus-select-group-hook
809            (lambda ()
810              (mapcar (lambda (header)
811                        (mail-header-set-subject
812                         header
813                         (gnus-simplify-subject
814                          (mail-header-subject header) 're-only)))
815                      gnus-newsgroup-headers)))"
816   :group 'gnus-group-select
817   :type 'hook)
818
819 (defcustom gnus-select-article-hook nil
820   "*A hook called when an article is selected."
821   :group 'gnus-summary-choose
822   :options '(gnus-agent-fetch-selected-article)
823   :type 'hook)
824
825 (defcustom gnus-visual-mark-article-hook
826   (list 'gnus-highlight-selected-summary)
827   "*Hook run after selecting an article in the summary buffer.
828 It is meant to be used for highlighting the article in some way.  It
829 is not run if `gnus-visual' is nil."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-parse-headers-hook nil
834   "*A hook called before parsing the headers."
835   :group 'gnus-various
836   :type 'hook)
837
838 (defcustom gnus-exit-group-hook nil
839   "*A hook called when exiting summary mode.
840 This hook is not called from the non-updating exit commands like `Q'."
841   :group 'gnus-various
842   :type 'hook)
843
844 (defcustom gnus-summary-update-hook
845   (list 'gnus-summary-highlight-line)
846   "*A hook called when a summary line is changed.
847 The hook will not be called if `gnus-visual' is nil.
848
849 The default function `gnus-summary-highlight-line' will
850 highlight the line according to the `gnus-summary-highlight'
851 variable."
852   :group 'gnus-summary-visual
853   :type 'hook)
854
855 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
856   "*A hook called when an article is selected for the first time.
857 The hook is intended to mark an article as read (or unread)
858 automatically when it is selected."
859   :group 'gnus-summary-choose
860   :type 'hook)
861
862 (defcustom gnus-group-no-more-groups-hook nil
863   "*A hook run when returning to group mode having no more (unread) groups."
864   :group 'gnus-group-select
865   :type 'hook)
866
867 (defcustom gnus-ps-print-hook nil
868   "*A hook run before ps-printing something from Gnus."
869   :group 'gnus-summary
870   :type 'hook)
871
872 (defcustom gnus-summary-article-move-hook nil
873   "*A hook called after an article is moved, copied, respooled, or crossposted."
874   :group 'gnus-summary
875   :type 'hook)
876
877 (defcustom gnus-summary-article-delete-hook nil
878   "*A hook called after an article is deleted."
879   :group 'gnus-summary
880   :type 'hook)
881
882 (defcustom gnus-summary-article-expire-hook nil
883   "*A hook called after an article is expired."
884   :group 'gnus-summary
885   :type 'hook)
886
887 (defcustom gnus-summary-display-arrow
888   (and (fboundp 'display-graphic-p)
889        (display-graphic-p))
890   "*If non-nil, display an arrow highlighting the current article."
891   :version "21.1"
892   :group 'gnus-summary
893   :type 'boolean)
894
895 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
896   "Face used for highlighting the current article in the summary buffer."
897   :group 'gnus-summary-visual
898   :type 'face)
899
900 (defvar gnus-tmp-downloaded nil)
901
902 (defcustom gnus-summary-highlight
903   '(((eq mark gnus-canceled-mark)
904      . gnus-summary-cancelled-face)
905     ((and uncached (> score default-high))
906      . gnus-summary-high-undownloaded-face)
907     ((and uncached (< score default-low))
908      . gnus-summary-low-undownloaded-face)
909     (uncached
910      . gnus-summary-normal-undownloaded-face)
911     ((and (> score default-high)
912           (or (eq mark gnus-dormant-mark)
913               (eq mark gnus-ticked-mark)))
914      . gnus-summary-high-ticked-face)
915     ((and (< score default-low)
916           (or (eq mark gnus-dormant-mark)
917               (eq mark gnus-ticked-mark)))
918      . gnus-summary-low-ticked-face)
919     ((or (eq mark gnus-dormant-mark)
920          (eq mark gnus-ticked-mark))
921      . gnus-summary-normal-ticked-face)
922     ((and (> score default-high) (eq mark gnus-ancient-mark))
923      . gnus-summary-high-ancient-face)
924     ((and (< score default-low) (eq mark gnus-ancient-mark))
925      . gnus-summary-low-ancient-face)
926     ((eq mark gnus-ancient-mark)
927      . gnus-summary-normal-ancient-face)
928     ((and (> score default-high) (eq mark gnus-unread-mark))
929      . gnus-summary-high-unread-face)
930     ((and (< score default-low) (eq mark gnus-unread-mark))
931      . gnus-summary-low-unread-face)
932     ((eq mark gnus-unread-mark)
933      . gnus-summary-normal-unread-face)
934     ((> score default-high)
935      . gnus-summary-high-read-face)
936     ((< score default-low)
937      . gnus-summary-low-read-face)
938     (t
939      . gnus-summary-normal-read-face))
940   "*Controls the highlighting of summary buffer lines.
941
942 A list of (FORM . FACE) pairs.  When deciding how a a particular
943 summary line should be displayed, each form is evaluated.  The content
944 of the face field after the first true form is used.  You can change
945 how those summary lines are displayed, by editing the face field.
946
947 You can use the following variables in the FORM field.
948
949 score:        The article's score
950 default:      The default article score.
951 default-high: The default score for high scored articles.
952 default-low:  The default score for low scored articles.
953 below:        The score below which articles are automatically marked as read.
954 mark:         The articles mark."
955   :group 'gnus-summary-visual
956   :type '(repeat (cons (sexp :tag "Form" nil)
957                        face)))
958
959 (defcustom gnus-alter-header-function nil
960   "Function called to allow alteration of article header structures.
961 The function is called with one parameter, the article header vector,
962 which it may alter in any way."
963   :type '(choice (const :tag "None" nil)
964                  function)
965   :group 'gnus-summary)
966
967 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
968   "Variable that says which function should be used to decode a string with encoded words.")
969
970 (defcustom gnus-extra-headers '(To Newsgroups)
971   "*Extra headers to parse."
972   :version "21.1"
973   :group 'gnus-summary
974   :type '(repeat symbol))
975
976 (defcustom gnus-ignored-from-addresses
977   (and user-mail-address (regexp-quote user-mail-address))
978   "*Regexp of From headers that may be suppressed in favor of To headers."
979   :version "21.1"
980   :group 'gnus-summary
981   :type 'regexp)
982
983 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
984   "List of charsets that should be ignored.
985 When these charsets are used in the \"charset\" parameter, the
986 default charset will be used instead."
987   :version "21.1"
988   :type '(repeat symbol)
989   :group 'gnus-charset)
990
991 (gnus-define-group-parameter
992  ignored-charsets
993  :type list
994  :function-document
995  "Return the ignored charsets of GROUP."
996  :variable gnus-group-ignored-charsets-alist
997  :variable-default
998  '(("alt\\.chinese\\.text" iso-8859-1))
999  :variable-document
1000  "Alist of regexps (to match group names) and charsets that should be ignored.
1001 When these charsets are used in the \"charset\" parameter, the
1002 default charset will be used instead."
1003  :variable-group gnus-charset
1004  :variable-type '(repeat (cons (regexp :tag "Group")
1005                                (repeat symbol)))
1006  :parameter-type '(choice :tag "Ignored charsets"
1007                           :value nil
1008                           (repeat (symbol)))
1009  :parameter-document       "\
1010 List of charsets that should be ignored.
1011
1012 When these charsets are used in the \"charset\" parameter, the
1013 default charset will be used instead.")
1014
1015 (defcustom gnus-group-highlight-words-alist nil
1016   "Alist of group regexps and highlight regexps.
1017 This variable uses the same syntax as `gnus-emphasis-alist'."
1018   :version "21.1"
1019   :type '(repeat (cons (regexp :tag "Group")
1020                        (repeat (list (regexp :tag "Highlight regexp")
1021                                      (number :tag "Group for entire word" 0)
1022                                      (number :tag "Group for displayed part" 0)
1023                                      (symbol :tag "Face"
1024                                              gnus-emphasis-highlight-words)))))
1025   :group 'gnus-summary-visual)
1026
1027 (defcustom gnus-summary-show-article-charset-alist
1028   nil
1029   "Alist of number and charset.
1030 The article will be shown with the charset corresponding to the
1031 numbered argument.
1032 For example: ((1 . cn-gb-2312) (2 . big5))."
1033   :version "21.1"
1034   :type '(repeat (cons (number :tag "Argument" 1)
1035                        (symbol :tag "Charset")))
1036   :group 'gnus-charset)
1037
1038 (defcustom gnus-preserve-marks t
1039   "Whether marks are preserved when moving, copying and respooling messages."
1040   :version "21.1"
1041   :type 'boolean
1042   :group 'gnus-summary-marks)
1043
1044 (defcustom gnus-alter-articles-to-read-function nil
1045   "Function to be called to alter the list of articles to be selected."
1046   :type '(choice (const nil) function)
1047   :group 'gnus-summary)
1048
1049 (defcustom gnus-orphan-score nil
1050   "*All orphans get this score added.  Set in the score file."
1051   :group 'gnus-score-default
1052   :type '(choice (const nil)
1053                  integer))
1054
1055 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1056   "*A regexp to match MIME parts when saving multiple parts of a
1057 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1058 This regexp will be used by default when prompting the user for which
1059 type of files to save."
1060   :group 'gnus-summary
1061   :type 'regexp)
1062
1063 (defcustom gnus-read-all-available-headers nil
1064   "Whether Gnus should parse all headers made available to it.
1065 This is mostly relevant for slow backends where the user may
1066 wish to widen the summary buffer to include all headers
1067 that were fetched.  Say, for nnultimate groups."
1068   :group 'gnus-summary
1069   :type '(choice boolean regexp))
1070
1071 (defcustom gnus-summary-muttprint-program "muttprint"
1072   "Command (and optional arguments) used to run Muttprint."
1073   :version "21.3"
1074   :group 'gnus-summary
1075   :type 'string)
1076
1077 (defcustom gnus-article-loose-mime nil
1078   "If non-nil, don't require MIME-Version header.
1079 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1080 supply the MIME-Version header or deliberately strip it From the mail.
1081 Set it to non-nil, Gnus will treat some articles as MIME even if
1082 the MIME-Version header is missed."
1083   :version "21.3"
1084   :type 'boolean
1085   :group 'gnus-article-mime)
1086
1087 (defcustom gnus-article-emulate-mime t
1088   "If non-nil, use MIME emulation for uuencode and the like.
1089 This means that Gnus will search message bodies for text that look
1090 like uuencoded bits, yEncoded bits, and so on, and present that using
1091 the normal Gnus MIME machinery."
1092   :type 'boolean
1093   :group 'gnus-article-mime)
1094
1095 ;;; Internal variables
1096
1097 (defvar gnus-summary-display-cache nil)
1098 (defvar gnus-article-mime-handles nil)
1099 (defvar gnus-article-decoded-p nil)
1100 (defvar gnus-article-charset nil)
1101 (defvar gnus-article-ignored-charsets nil)
1102 (defvar gnus-scores-exclude-files nil)
1103 (defvar gnus-page-broken nil)
1104
1105 (defvar gnus-original-article nil)
1106 (defvar gnus-article-internal-prepare-hook nil)
1107 (defvar gnus-newsgroup-process-stack nil)
1108
1109 (defvar gnus-thread-indent-array nil)
1110 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1111 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1112   "Function called to sort the articles within a thread after it has been gathered together.")
1113
1114 (defvar gnus-summary-save-parts-type-history nil)
1115 (defvar gnus-summary-save-parts-last-directory nil)
1116
1117 ;; Avoid highlighting in kill files.
1118 (defvar gnus-summary-inhibit-highlight nil)
1119 (defvar gnus-newsgroup-selected-overlay nil)
1120 (defvar gnus-inhibit-limiting nil)
1121 (defvar gnus-newsgroup-adaptive-score-file nil)
1122 (defvar gnus-current-score-file nil)
1123 (defvar gnus-current-move-group nil)
1124 (defvar gnus-current-copy-group nil)
1125 (defvar gnus-current-crosspost-group nil)
1126 (defvar gnus-newsgroup-display nil)
1127
1128 (defvar gnus-newsgroup-dependencies nil)
1129 (defvar gnus-newsgroup-adaptive nil)
1130 (defvar gnus-summary-display-article-function nil)
1131 (defvar gnus-summary-highlight-line-function nil
1132   "Function called after highlighting a summary line.")
1133
1134 (defvar gnus-summary-line-format-alist
1135   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1136     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1137     (?s gnus-tmp-subject-or-nil ?s)
1138     (?n gnus-tmp-name ?s)
1139     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1140         ?s)
1141     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1142             gnus-tmp-from) ?s)
1143     (?F gnus-tmp-from ?s)
1144     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1145     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1146     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1147     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1148     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1149     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1150     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1151     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1152     (?L gnus-tmp-lines ?s)
1153     (?O gnus-tmp-downloaded ?c)
1154     (?I gnus-tmp-indentation ?s)
1155     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1156     (?R gnus-tmp-replied ?c)
1157     (?\[ gnus-tmp-opening-bracket ?c)
1158     (?\] gnus-tmp-closing-bracket ?c)
1159     (?\> (make-string gnus-tmp-level ? ) ?s)
1160     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1161     (?i gnus-tmp-score ?d)
1162     (?z gnus-tmp-score-char ?c)
1163     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1164     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1165     (?U gnus-tmp-unread ?c)
1166     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1167         ?s)
1168     (?t (gnus-summary-number-of-articles-in-thread
1169          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1170         ?d)
1171     (?e (gnus-summary-number-of-articles-in-thread
1172          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1173         ?c)
1174     (?u gnus-tmp-user-defined ?s)
1175     (?P (gnus-pick-line-number) ?d)
1176     (?B gnus-tmp-thread-tree-header-string ?s)
1177     (user-date (gnus-user-date
1178                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1179   "An alist of format specifications that can appear in summary lines.
1180 These are paired with what variables they correspond with, along with
1181 the type of the variable (string, integer, character, etc).")
1182
1183 (defvar gnus-summary-dummy-line-format-alist
1184   `((?S gnus-tmp-subject ?s)
1185     (?N gnus-tmp-number ?d)
1186     (?u gnus-tmp-user-defined ?s)))
1187
1188 (defvar gnus-summary-mode-line-format-alist
1189   `((?G gnus-tmp-group-name ?s)
1190     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1191     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1192     (?A gnus-tmp-article-number ?d)
1193     (?Z gnus-tmp-unread-and-unselected ?s)
1194     (?V gnus-version ?s)
1195     (?U gnus-tmp-unread-and-unticked ?d)
1196     (?S gnus-tmp-subject ?s)
1197     (?e gnus-tmp-unselected ?d)
1198     (?u gnus-tmp-user-defined ?s)
1199     (?d (length gnus-newsgroup-dormant) ?d)
1200     (?t (length gnus-newsgroup-marked) ?d)
1201     (?h (length gnus-newsgroup-spam-marked) ?d)
1202     (?r (length gnus-newsgroup-reads) ?d)
1203     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1204     (?E gnus-newsgroup-expunged-tally ?d)
1205     (?s (gnus-current-score-file-nondirectory) ?s)))
1206
1207 (defvar gnus-last-search-regexp nil
1208   "Default regexp for article search command.")
1209
1210 (defvar gnus-last-shell-command nil
1211   "Default shell command on article.")
1212
1213 (defvar gnus-newsgroup-agentized nil
1214   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1215 (defvar gnus-newsgroup-begin nil)
1216 (defvar gnus-newsgroup-end nil)
1217 (defvar gnus-newsgroup-last-rmail nil)
1218 (defvar gnus-newsgroup-last-mail nil)
1219 (defvar gnus-newsgroup-last-folder nil)
1220 (defvar gnus-newsgroup-last-file nil)
1221 (defvar gnus-newsgroup-auto-expire nil)
1222 (defvar gnus-newsgroup-active nil)
1223
1224 (defvar gnus-newsgroup-data nil)
1225 (defvar gnus-newsgroup-data-reverse nil)
1226 (defvar gnus-newsgroup-limit nil)
1227 (defvar gnus-newsgroup-limits nil)
1228
1229 (defvar gnus-newsgroup-unreads nil
1230   "Sorted list of unread articles in the current newsgroup.")
1231
1232 (defvar gnus-newsgroup-unselected nil
1233   "Sorted list of unselected unread articles in the current newsgroup.")
1234
1235 (defvar gnus-newsgroup-reads nil
1236   "Alist of read articles and article marks in the current newsgroup.")
1237
1238 (defvar gnus-newsgroup-expunged-tally nil)
1239
1240 (defvar gnus-newsgroup-marked nil
1241   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1242
1243 (defvar gnus-newsgroup-spam-marked nil
1244   "List of ranges of articles that have been marked as spam.")
1245
1246 (defvar gnus-newsgroup-killed nil
1247   "List of ranges of articles that have been through the scoring process.")
1248
1249 (defvar gnus-newsgroup-cached nil
1250   "Sorted list of articles that come from the article cache.")
1251
1252 (defvar gnus-newsgroup-saved nil
1253   "List of articles that have been saved.")
1254
1255 (defvar gnus-newsgroup-kill-headers nil)
1256
1257 (defvar gnus-newsgroup-replied nil
1258   "List of articles that have been replied to in the current newsgroup.")
1259
1260 (defvar gnus-newsgroup-forwarded nil
1261   "List of articles that have been forwarded in the current newsgroup.")
1262
1263 (defvar gnus-newsgroup-recent nil
1264   "List of articles that have are recent in the current newsgroup.")
1265
1266 (defvar gnus-newsgroup-expirable nil
1267   "Sorted list of articles in the current newsgroup that can be expired.")
1268
1269 (defvar gnus-newsgroup-processable nil
1270   "List of articles in the current newsgroup that can be processed.")
1271
1272 (defvar gnus-newsgroup-downloadable nil
1273   "Sorted list of articles in the current newsgroup that can be processed.")
1274
1275 (defvar gnus-newsgroup-unfetched nil
1276   "Sorted list of articles in the current newsgroup whose headers have
1277 not been fetched into the agent.
1278
1279 This list will always be a subset of gnus-newsgroup-undownloaded.")
1280
1281 (defvar gnus-newsgroup-undownloaded nil
1282   "List of articles in the current newsgroup that haven't been downloaded.")
1283
1284 (defvar gnus-newsgroup-unsendable nil
1285   "List of articles in the current newsgroup that won't be sent.")
1286
1287 (defvar gnus-newsgroup-bookmarks nil
1288   "List of articles in the current newsgroup that have bookmarks.")
1289
1290 (defvar gnus-newsgroup-dormant nil
1291   "Sorted list of dormant articles in the current newsgroup.")
1292
1293 (defvar gnus-newsgroup-unseen nil
1294   "List of unseen articles in the current newsgroup.")
1295
1296 (defvar gnus-newsgroup-seen nil
1297   "Range of seen articles in the current newsgroup.")
1298
1299 (defvar gnus-newsgroup-articles nil
1300   "List of articles in the current newsgroup.")
1301
1302 (defvar gnus-newsgroup-scored nil
1303   "List of scored articles in the current newsgroup.")
1304
1305 (defvar gnus-newsgroup-headers nil
1306   "List of article headers in the current newsgroup.")
1307
1308 (defvar gnus-newsgroup-threads nil)
1309
1310 (defvar gnus-newsgroup-prepared nil
1311   "Whether the current group has been prepared properly.")
1312
1313 (defvar gnus-newsgroup-ancient nil
1314   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1315
1316 (defvar gnus-newsgroup-sparse nil)
1317
1318 (defvar gnus-current-article nil)
1319 (defvar gnus-article-current nil)
1320 (defvar gnus-current-headers nil)
1321 (defvar gnus-have-all-headers nil)
1322 (defvar gnus-last-article nil)
1323 (defvar gnus-newsgroup-history nil)
1324 (defvar gnus-newsgroup-charset nil)
1325 (defvar gnus-newsgroup-ephemeral-charset nil)
1326 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1327
1328 (defvar gnus-article-before-search nil)
1329
1330 (defvar gnus-summary-local-variables
1331   '(gnus-newsgroup-name
1332     gnus-newsgroup-begin gnus-newsgroup-end
1333     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1334     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1335     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1336     gnus-newsgroup-unselected gnus-newsgroup-marked
1337     gnus-newsgroup-spam-marked
1338     gnus-newsgroup-reads gnus-newsgroup-saved
1339     gnus-newsgroup-replied gnus-newsgroup-forwarded
1340     gnus-newsgroup-recent
1341     gnus-newsgroup-expirable
1342     gnus-newsgroup-processable gnus-newsgroup-killed
1343     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1344     gnus-newsgroup-unfetched
1345     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1346     gnus-newsgroup-seen gnus-newsgroup-articles
1347     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1348     gnus-newsgroup-headers gnus-newsgroup-threads
1349     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1350     gnus-current-article gnus-current-headers gnus-have-all-headers
1351     gnus-last-article gnus-article-internal-prepare-hook
1352     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1353     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1354     gnus-thread-expunge-below
1355     gnus-score-alist gnus-current-score-file
1356     (gnus-summary-expunge-below . global)
1357     (gnus-summary-mark-below . global)
1358     (gnus-orphan-score . global)
1359     gnus-newsgroup-active gnus-scores-exclude-files
1360     gnus-newsgroup-history gnus-newsgroup-ancient
1361     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1362     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1363     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1364     (gnus-newsgroup-expunged-tally . 0)
1365     gnus-cache-removable-articles gnus-newsgroup-cached
1366     gnus-newsgroup-data gnus-newsgroup-data-reverse
1367     gnus-newsgroup-limit gnus-newsgroup-limits
1368     gnus-newsgroup-charset gnus-newsgroup-display)
1369   "Variables that are buffer-local to the summary buffers.")
1370
1371 (defvar gnus-newsgroup-variables nil
1372   "A list of variables that have separate values in different newsgroups.
1373 A list of newsgroup (summary buffer) local variables, or cons of
1374 variables and their default values (when the default values are not
1375 nil), that should be made global while the summary buffer is active.
1376 These variables can be used to set variables in the group parameters
1377 while still allowing them to affect operations done in other
1378 buffers. For example:
1379
1380 \(setq gnus-newsgroup-variables
1381      '(message-use-followup-to
1382        (gnus-visible-headers .
1383          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1384 ")
1385
1386 ;; Byte-compiler warning.
1387 (eval-when-compile
1388   ;; Bind features so that require will believe that gnus-sum has
1389   ;; already been loaded (avoids infinite recursion)
1390   (let ((features (cons 'gnus-sum features)))
1391     ;; Several of the declarations in gnus-sum are needed to load the
1392     ;; following files. Right now, these definitions have been
1393     ;; compiled but not defined (evaluated).  We could either do a
1394     ;; eval-and-compile about all of the declarations or evaluate the
1395     ;; source file.
1396     (if (boundp 'gnus-newsgroup-variables)
1397         nil
1398       (load "gnus-sum.el" t t t))
1399     (require 'gnus)
1400     (require 'gnus-agent)
1401     (require 'gnus-art)))
1402
1403 ;; MIME stuff.
1404
1405 (defvar gnus-decode-encoded-word-methods
1406   '(mail-decode-encoded-word-string)
1407   "List of methods used to decode encoded words.
1408
1409 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1410 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1411 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1412 whose names match REGEXP.
1413
1414 For example:
1415 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1416  mail-decode-encoded-word-string
1417  (\"chinese\" . rfc1843-decode-string))")
1418
1419 (defvar gnus-decode-encoded-word-methods-cache nil)
1420
1421 (defun gnus-multi-decode-encoded-word-string (string)
1422   "Apply the functions from `gnus-encoded-word-methods' that match."
1423   (unless (and gnus-decode-encoded-word-methods-cache
1424                (eq gnus-newsgroup-name
1425                    (car gnus-decode-encoded-word-methods-cache)))
1426     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1427     (mapcar (lambda (x)
1428               (if (symbolp x)
1429                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1430                 (if (and gnus-newsgroup-name
1431                          (string-match (car x) gnus-newsgroup-name))
1432                     (nconc gnus-decode-encoded-word-methods-cache
1433                            (list (cdr x))))))
1434             gnus-decode-encoded-word-methods))
1435   (let ((xlist gnus-decode-encoded-word-methods-cache))
1436     (pop xlist)
1437     (while xlist
1438       (setq string (funcall (pop xlist) string))))
1439   string)
1440
1441 ;; Subject simplification.
1442
1443 (defun gnus-simplify-whitespace (str)
1444   "Remove excessive whitespace from STR."
1445   ;; Multiple spaces.
1446   (while (string-match "[ \t][ \t]+" str)
1447     (setq str (concat (substring str 0 (match-beginning 0))
1448                         " "
1449                         (substring str (match-end 0)))))
1450   ;; Leading spaces.
1451   (when (string-match "^[ \t]+" str)
1452     (setq str (substring str (match-end 0))))
1453   ;; Trailing spaces.
1454   (when (string-match "[ \t]+$" str)
1455     (setq str (substring str 0 (match-beginning 0))))
1456   str)
1457
1458 (defun gnus-simplify-all-whitespace (str)
1459   "Remove all whitespace from STR."
1460   (while (string-match "[ \t\n]+" str)
1461     (setq str (replace-match "" nil nil str)))
1462   str)
1463
1464 (defsubst gnus-simplify-subject-re (subject)
1465   "Remove \"Re:\" from subject lines."
1466   (if (string-match message-subject-re-regexp subject)
1467       (substring subject (match-end 0))
1468     subject))
1469
1470 (defun gnus-simplify-subject (subject &optional re-only)
1471   "Remove `Re:' and words in parentheses.
1472 If RE-ONLY is non-nil, strip leading `Re:'s only."
1473   (let ((case-fold-search t))           ;Ignore case.
1474     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1475     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1476       (setq subject (substring subject (match-end 0))))
1477     ;; Remove uninteresting prefixes.
1478     (when (and (not re-only)
1479                gnus-simplify-ignored-prefixes
1480                (string-match gnus-simplify-ignored-prefixes subject))
1481       (setq subject (substring subject (match-end 0))))
1482     ;; Remove words in parentheses from end.
1483     (unless re-only
1484       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1485         (setq subject (substring subject 0 (match-beginning 0)))))
1486     ;; Return subject string.
1487     subject))
1488
1489 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1490 ;; all whitespace.
1491 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1492   (goto-char (point-min))
1493   (while (re-search-forward regexp nil t)
1494     (replace-match (or newtext ""))))
1495
1496 (defun gnus-simplify-buffer-fuzzy ()
1497   "Simplify string in the buffer fuzzily.
1498 The string in the accessible portion of the current buffer is simplified.
1499 It is assumed to be a single-line subject.
1500 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1501 matter is removed.  Additional things can be deleted by setting
1502 `gnus-simplify-subject-fuzzy-regexp'."
1503   (let ((case-fold-search t)
1504         (modified-tick))
1505     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1506
1507     (while (not (eq modified-tick (buffer-modified-tick)))
1508       (setq modified-tick (buffer-modified-tick))
1509       (cond
1510        ((listp gnus-simplify-subject-fuzzy-regexp)
1511         (mapcar 'gnus-simplify-buffer-fuzzy-step
1512                 gnus-simplify-subject-fuzzy-regexp))
1513        (gnus-simplify-subject-fuzzy-regexp
1514         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1515       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1516       (gnus-simplify-buffer-fuzzy-step
1517        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1518       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1519
1520     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1521     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1522     (gnus-simplify-buffer-fuzzy-step " $")
1523     (gnus-simplify-buffer-fuzzy-step "^ +")))
1524
1525 (defun gnus-simplify-subject-fuzzy (subject)
1526   "Simplify a subject string fuzzily.
1527 See `gnus-simplify-buffer-fuzzy' for details."
1528   (save-excursion
1529     (gnus-set-work-buffer)
1530     (let ((case-fold-search t))
1531       ;; Remove uninteresting prefixes.
1532       (when (and gnus-simplify-ignored-prefixes
1533                  (string-match gnus-simplify-ignored-prefixes subject))
1534         (setq subject (substring subject (match-end 0))))
1535       (insert subject)
1536       (inline (gnus-simplify-buffer-fuzzy))
1537       (buffer-string))))
1538
1539 (defsubst gnus-simplify-subject-fully (subject)
1540   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1541   (cond
1542    (gnus-simplify-subject-functions
1543     (gnus-map-function gnus-simplify-subject-functions subject))
1544    ((null gnus-summary-gather-subject-limit)
1545     (gnus-simplify-subject-re subject))
1546    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1547     (gnus-simplify-subject-fuzzy subject))
1548    ((numberp gnus-summary-gather-subject-limit)
1549     (gnus-limit-string (gnus-simplify-subject-re subject)
1550                        gnus-summary-gather-subject-limit))
1551    (t
1552     subject)))
1553
1554 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1555   "Check whether two subjects are equal.
1556 If optional argument SIMPLE-FIRST is t, first argument is already
1557 simplified."
1558   (cond
1559    ((null simple-first)
1560     (equal (gnus-simplify-subject-fully s1)
1561            (gnus-simplify-subject-fully s2)))
1562    (t
1563     (equal s1
1564            (gnus-simplify-subject-fully s2)))))
1565
1566 (defun gnus-summary-bubble-group ()
1567   "Increase the score of the current group.
1568 This is a handy function to add to `gnus-summary-exit-hook' to
1569 increase the score of each group you read."
1570   (gnus-group-add-score gnus-newsgroup-name))
1571
1572 \f
1573 ;;;
1574 ;;; Gnus summary mode
1575 ;;;
1576
1577 (put 'gnus-summary-mode 'mode-class 'special)
1578
1579 (defvar gnus-article-commands-menu)
1580
1581 ;; Non-orthogonal keys
1582
1583 (gnus-define-keys gnus-summary-mode-map
1584   " " gnus-summary-next-page
1585   "\177" gnus-summary-prev-page
1586   [delete] gnus-summary-prev-page
1587   [backspace] gnus-summary-prev-page
1588   "\r" gnus-summary-scroll-up
1589   "\M-\r" gnus-summary-scroll-down
1590   "n" gnus-summary-next-unread-article
1591   "p" gnus-summary-prev-unread-article
1592   "N" gnus-summary-next-article
1593   "P" gnus-summary-prev-article
1594   "\M-\C-n" gnus-summary-next-same-subject
1595   "\M-\C-p" gnus-summary-prev-same-subject
1596   "\M-n" gnus-summary-next-unread-subject
1597   "\M-p" gnus-summary-prev-unread-subject
1598   "." gnus-summary-first-unread-article
1599   "," gnus-summary-best-unread-article
1600   "\M-s" gnus-summary-search-article-forward
1601   "\M-r" gnus-summary-search-article-backward
1602   "<" gnus-summary-beginning-of-article
1603   ">" gnus-summary-end-of-article
1604   "j" gnus-summary-goto-article
1605   "^" gnus-summary-refer-parent-article
1606   "\M-^" gnus-summary-refer-article
1607   "u" gnus-summary-tick-article-forward
1608   "!" gnus-summary-tick-article-forward
1609   "U" gnus-summary-tick-article-backward
1610   "d" gnus-summary-mark-as-read-forward
1611   "D" gnus-summary-mark-as-read-backward
1612   "E" gnus-summary-mark-as-expirable
1613   "\M-u" gnus-summary-clear-mark-forward
1614   "\M-U" gnus-summary-clear-mark-backward
1615   "k" gnus-summary-kill-same-subject-and-select
1616   "\C-k" gnus-summary-kill-same-subject
1617   "\M-\C-k" gnus-summary-kill-thread
1618   "\M-\C-l" gnus-summary-lower-thread
1619   "e" gnus-summary-edit-article
1620   "#" gnus-summary-mark-as-processable
1621   "\M-#" gnus-summary-unmark-as-processable
1622   "\M-\C-t" gnus-summary-toggle-threads
1623   "\M-\C-s" gnus-summary-show-thread
1624   "\M-\C-h" gnus-summary-hide-thread
1625   "\M-\C-f" gnus-summary-next-thread
1626   "\M-\C-b" gnus-summary-prev-thread
1627   [(meta down)] gnus-summary-next-thread
1628   [(meta up)] gnus-summary-prev-thread
1629   "\M-\C-u" gnus-summary-up-thread
1630   "\M-\C-d" gnus-summary-down-thread
1631   "&" gnus-summary-execute-command
1632   "c" gnus-summary-catchup-and-exit
1633   "\C-w" gnus-summary-mark-region-as-read
1634   "\C-t" gnus-summary-toggle-truncation
1635   "?" gnus-summary-mark-as-dormant
1636   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1637   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1638   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1639   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1640   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1641   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1642   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1643   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1644   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1645   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1646   "=" gnus-summary-expand-window
1647   "\C-x\C-s" gnus-summary-reselect-current-group
1648   "\M-g" gnus-summary-rescan-group
1649   "w" gnus-summary-stop-page-breaking
1650   "\C-c\C-r" gnus-summary-caesar-message
1651   "f" gnus-summary-followup
1652   "F" gnus-summary-followup-with-original
1653   "C" gnus-summary-cancel-article
1654   "r" gnus-summary-reply
1655   "R" gnus-summary-reply-with-original
1656   "\C-c\C-f" gnus-summary-mail-forward
1657   "o" gnus-summary-save-article
1658   "\C-o" gnus-summary-save-article-mail
1659   "|" gnus-summary-pipe-output
1660   "\M-k" gnus-summary-edit-local-kill
1661   "\M-K" gnus-summary-edit-global-kill
1662   ;; "V" gnus-version
1663   "\C-c\C-d" gnus-summary-describe-group
1664   "q" gnus-summary-exit
1665   "Q" gnus-summary-exit-no-update
1666   "\C-c\C-i" gnus-info-find-node
1667   gnus-mouse-2 gnus-mouse-pick-article
1668   "m" gnus-summary-mail-other-window
1669   "a" gnus-summary-post-news
1670   "i" gnus-summary-news-other-window
1671   "x" gnus-summary-limit-to-unread
1672   "s" gnus-summary-isearch-article
1673   "t" gnus-summary-toggle-header
1674   "g" gnus-summary-show-article
1675   "l" gnus-summary-goto-last-article
1676   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1677   "\C-d" gnus-summary-enter-digest-group
1678   "\M-\C-d" gnus-summary-read-document
1679   "\M-\C-e" gnus-summary-edit-parameters
1680   "\M-\C-a" gnus-summary-customize-parameters
1681   "\C-c\C-b" gnus-bug
1682   "*" gnus-cache-enter-article
1683   "\M-*" gnus-cache-remove-article
1684   "\M-&" gnus-summary-universal-argument
1685   "\C-l" gnus-recenter
1686   "I" gnus-summary-increase-score
1687   "L" gnus-summary-lower-score
1688   "\M-i" gnus-symbolic-argument
1689   "h" gnus-summary-select-article-buffer
1690
1691   "b" gnus-article-view-part
1692   "\M-t" gnus-summary-toggle-display-buttonized
1693
1694   "V" gnus-summary-score-map
1695   "X" gnus-uu-extract-map
1696   "S" gnus-summary-send-map)
1697
1698 ;; Sort of orthogonal keymap
1699 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1700   "t" gnus-summary-tick-article-forward
1701   "!" gnus-summary-tick-article-forward
1702   "d" gnus-summary-mark-as-read-forward
1703   "r" gnus-summary-mark-as-read-forward
1704   "c" gnus-summary-clear-mark-forward
1705   " " gnus-summary-clear-mark-forward
1706   "e" gnus-summary-mark-as-expirable
1707   "x" gnus-summary-mark-as-expirable
1708   "?" gnus-summary-mark-as-dormant
1709   "b" gnus-summary-set-bookmark
1710   "B" gnus-summary-remove-bookmark
1711   "#" gnus-summary-mark-as-processable
1712   "\M-#" gnus-summary-unmark-as-processable
1713   "S" gnus-summary-limit-include-expunged
1714   "C" gnus-summary-catchup
1715   "H" gnus-summary-catchup-to-here
1716   "h" gnus-summary-catchup-from-here
1717   "\C-c" gnus-summary-catchup-all
1718   "k" gnus-summary-kill-same-subject-and-select
1719   "K" gnus-summary-kill-same-subject
1720   "P" gnus-uu-mark-map)
1721
1722 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1723   "c" gnus-summary-clear-above
1724   "u" gnus-summary-tick-above
1725   "m" gnus-summary-mark-above
1726   "k" gnus-summary-kill-below)
1727
1728 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1729   "/" gnus-summary-limit-to-subject
1730   "n" gnus-summary-limit-to-articles
1731   "w" gnus-summary-pop-limit
1732   "s" gnus-summary-limit-to-subject
1733   "a" gnus-summary-limit-to-author
1734   "u" gnus-summary-limit-to-unread
1735   "m" gnus-summary-limit-to-marks
1736   "M" gnus-summary-limit-exclude-marks
1737   "v" gnus-summary-limit-to-score
1738   "*" gnus-summary-limit-include-cached
1739   "D" gnus-summary-limit-include-dormant
1740   "T" gnus-summary-limit-include-thread
1741   "d" gnus-summary-limit-exclude-dormant
1742   "t" gnus-summary-limit-to-age
1743   "." gnus-summary-limit-to-unseen
1744   "x" gnus-summary-limit-to-extra
1745   "p" gnus-summary-limit-to-display-predicate
1746   "E" gnus-summary-limit-include-expunged
1747   "c" gnus-summary-limit-exclude-childless-dormant
1748   "C" gnus-summary-limit-mark-excluded-as-read
1749   "o" gnus-summary-insert-old-articles
1750   "N" gnus-summary-insert-new-articles)
1751
1752 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1753   "n" gnus-summary-next-unread-article
1754   "p" gnus-summary-prev-unread-article
1755   "N" gnus-summary-next-article
1756   "P" gnus-summary-prev-article
1757   "\C-n" gnus-summary-next-same-subject
1758   "\C-p" gnus-summary-prev-same-subject
1759   "\M-n" gnus-summary-next-unread-subject
1760   "\M-p" gnus-summary-prev-unread-subject
1761   "f" gnus-summary-first-unread-article
1762   "b" gnus-summary-best-unread-article
1763   "j" gnus-summary-goto-article
1764   "g" gnus-summary-goto-subject
1765   "l" gnus-summary-goto-last-article
1766   "o" gnus-summary-pop-article)
1767
1768 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1769   "k" gnus-summary-kill-thread
1770   "l" gnus-summary-lower-thread
1771   "i" gnus-summary-raise-thread
1772   "T" gnus-summary-toggle-threads
1773   "t" gnus-summary-rethread-current
1774   "^" gnus-summary-reparent-thread
1775   "s" gnus-summary-show-thread
1776   "S" gnus-summary-show-all-threads
1777   "h" gnus-summary-hide-thread
1778   "H" gnus-summary-hide-all-threads
1779   "n" gnus-summary-next-thread
1780   "p" gnus-summary-prev-thread
1781   "u" gnus-summary-up-thread
1782   "o" gnus-summary-top-thread
1783   "d" gnus-summary-down-thread
1784   "#" gnus-uu-mark-thread
1785   "\M-#" gnus-uu-unmark-thread)
1786
1787 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1788   "g" gnus-summary-prepare
1789   "c" gnus-summary-insert-cached-articles
1790   "d" gnus-summary-insert-dormant-articles)
1791
1792 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1793   "c" gnus-summary-catchup-and-exit
1794   "C" gnus-summary-catchup-all-and-exit
1795   "E" gnus-summary-exit-no-update
1796   "Q" gnus-summary-exit
1797   "Z" gnus-summary-exit
1798   "n" gnus-summary-catchup-and-goto-next-group
1799   "R" gnus-summary-reselect-current-group
1800   "G" gnus-summary-rescan-group
1801   "N" gnus-summary-next-group
1802   "s" gnus-summary-save-newsrc
1803   "P" gnus-summary-prev-group)
1804
1805 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1806   " " gnus-summary-next-page
1807   "n" gnus-summary-next-page
1808   "\177" gnus-summary-prev-page
1809   [delete] gnus-summary-prev-page
1810   "p" gnus-summary-prev-page
1811   "\r" gnus-summary-scroll-up
1812   "\M-\r" gnus-summary-scroll-down
1813   "<" gnus-summary-beginning-of-article
1814   ">" gnus-summary-end-of-article
1815   "b" gnus-summary-beginning-of-article
1816   "e" gnus-summary-end-of-article
1817   "^" gnus-summary-refer-parent-article
1818   "r" gnus-summary-refer-parent-article
1819   "D" gnus-summary-enter-digest-group
1820   "R" gnus-summary-refer-references
1821   "T" gnus-summary-refer-thread
1822   "g" gnus-summary-show-article
1823   "s" gnus-summary-isearch-article
1824   "P" gnus-summary-print-article
1825   "M" gnus-mailing-list-insinuate
1826   "t" gnus-article-babel)
1827
1828 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1829   "b" gnus-article-add-buttons
1830   "B" gnus-article-add-buttons-to-head
1831   "o" gnus-article-treat-overstrike
1832   "e" gnus-article-emphasize
1833   "w" gnus-article-fill-cited-article
1834   "Q" gnus-article-fill-long-lines
1835   "C" gnus-article-capitalize-sentences
1836   "c" gnus-article-remove-cr
1837   "q" gnus-article-de-quoted-unreadable
1838   "6" gnus-article-de-base64-unreadable
1839   "Z" gnus-article-decode-HZ
1840   "h" gnus-article-wash-html
1841   "u" gnus-article-unsplit-urls
1842   "s" gnus-summary-force-verify-and-decrypt
1843   "f" gnus-article-display-x-face
1844   "l" gnus-summary-stop-page-breaking
1845   "r" gnus-summary-caesar-message
1846   "m" gnus-summary-morse-message
1847   "t" gnus-summary-toggle-header
1848   "g" gnus-treat-smiley
1849   "v" gnus-summary-verbose-headers
1850   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1851   "p" gnus-article-verify-x-pgp-sig
1852   "d" gnus-article-treat-dumbquotes)
1853
1854 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1855   ;; mnemonic: deuglif*Y*
1856   "u" gnus-article-outlook-unwrap-lines
1857   "a" gnus-article-outlook-repair-attribution
1858   "c" gnus-article-outlook-rearrange-citation
1859   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1860
1861 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1862   "a" gnus-article-hide
1863   "h" gnus-article-hide-headers
1864   "b" gnus-article-hide-boring-headers
1865   "s" gnus-article-hide-signature
1866   "c" gnus-article-hide-citation
1867   "C" gnus-article-hide-citation-in-followups
1868   "l" gnus-article-hide-list-identifiers
1869   "B" gnus-article-strip-banner
1870   "P" gnus-article-hide-pem
1871   "\C-c" gnus-article-hide-citation-maybe)
1872
1873 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1874   "a" gnus-article-highlight
1875   "h" gnus-article-highlight-headers
1876   "c" gnus-article-highlight-citation
1877   "s" gnus-article-highlight-signature)
1878
1879 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1880   "f" gnus-article-treat-fold-headers
1881   "u" gnus-article-treat-unfold-headers
1882   "n" gnus-article-treat-fold-newsgroups)
1883
1884 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1885   "x" gnus-article-display-x-face
1886   "d" gnus-article-display-face
1887   "s" gnus-treat-smiley
1888   "D" gnus-article-remove-images
1889   "f" gnus-treat-from-picon
1890   "m" gnus-treat-mail-picon
1891   "n" gnus-treat-newsgroups-picon)
1892
1893 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1894   "w" gnus-article-decode-mime-words
1895   "c" gnus-article-decode-charset
1896   "v" gnus-mime-view-all-parts
1897   "b" gnus-article-view-part)
1898
1899 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1900   "z" gnus-article-date-ut
1901   "u" gnus-article-date-ut
1902   "l" gnus-article-date-local
1903   "p" gnus-article-date-english
1904   "e" gnus-article-date-lapsed
1905   "o" gnus-article-date-original
1906   "i" gnus-article-date-iso8601
1907   "s" gnus-article-date-user)
1908
1909 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1910   "t" gnus-article-remove-trailing-blank-lines
1911   "l" gnus-article-strip-leading-blank-lines
1912   "m" gnus-article-strip-multiple-blank-lines
1913   "a" gnus-article-strip-blank-lines
1914   "A" gnus-article-strip-all-blank-lines
1915   "s" gnus-article-strip-leading-space
1916   "e" gnus-article-strip-trailing-space
1917   "w" gnus-article-remove-leading-whitespace)
1918
1919 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1920   "v" gnus-version
1921   "f" gnus-summary-fetch-faq
1922   "d" gnus-summary-describe-group
1923   "h" gnus-summary-describe-briefly
1924   "i" gnus-info-find-node
1925   "c" gnus-group-fetch-charter
1926   "C" gnus-group-fetch-control)
1927
1928 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1929   "e" gnus-summary-expire-articles
1930   "\M-\C-e" gnus-summary-expire-articles-now
1931   "\177" gnus-summary-delete-article
1932   [delete] gnus-summary-delete-article
1933   [backspace] gnus-summary-delete-article
1934   "m" gnus-summary-move-article
1935   "r" gnus-summary-respool-article
1936   "w" gnus-summary-edit-article
1937   "c" gnus-summary-copy-article
1938   "B" gnus-summary-crosspost-article
1939   "q" gnus-summary-respool-query
1940   "t" gnus-summary-respool-trace
1941   "i" gnus-summary-import-article
1942   "I" gnus-summary-create-article
1943   "p" gnus-summary-article-posted-p)
1944
1945 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1946   "o" gnus-summary-save-article
1947   "m" gnus-summary-save-article-mail
1948   "F" gnus-summary-write-article-file
1949   "r" gnus-summary-save-article-rmail
1950   "f" gnus-summary-save-article-file
1951   "b" gnus-summary-save-article-body-file
1952   "h" gnus-summary-save-article-folder
1953   "v" gnus-summary-save-article-vm
1954   "p" gnus-summary-pipe-output
1955   "P" gnus-summary-muttprint
1956   "s" gnus-soup-add-article)
1957
1958 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1959   "b" gnus-summary-display-buttonized
1960   "m" gnus-summary-repair-multipart
1961   "v" gnus-article-view-part
1962   "o" gnus-article-save-part
1963   "c" gnus-article-copy-part
1964   "C" gnus-article-view-part-as-charset
1965   "e" gnus-article-view-part-externally
1966   "E" gnus-article-encrypt-body
1967   "i" gnus-article-inline-part
1968   "|" gnus-article-pipe-part)
1969
1970 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1971   "p" gnus-summary-mark-as-processable
1972   "u" gnus-summary-unmark-as-processable
1973   "U" gnus-summary-unmark-all-processable
1974   "v" gnus-uu-mark-over
1975   "s" gnus-uu-mark-series
1976   "r" gnus-uu-mark-region
1977   "g" gnus-uu-unmark-region
1978   "R" gnus-uu-mark-by-regexp
1979   "G" gnus-uu-unmark-by-regexp
1980   "t" gnus-uu-mark-thread
1981   "T" gnus-uu-unmark-thread
1982   "a" gnus-uu-mark-all
1983   "b" gnus-uu-mark-buffer
1984   "S" gnus-uu-mark-sparse
1985   "k" gnus-summary-kill-process-mark
1986   "y" gnus-summary-yank-process-mark
1987   "w" gnus-summary-save-process-mark
1988   "i" gnus-uu-invert-processable)
1989
1990 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1991   ;;"x" gnus-uu-extract-any
1992   "m" gnus-summary-save-parts
1993   "u" gnus-uu-decode-uu
1994   "U" gnus-uu-decode-uu-and-save
1995   "s" gnus-uu-decode-unshar
1996   "S" gnus-uu-decode-unshar-and-save
1997   "o" gnus-uu-decode-save
1998   "O" gnus-uu-decode-save
1999   "b" gnus-uu-decode-binhex
2000   "B" gnus-uu-decode-binhex
2001   "p" gnus-uu-decode-postscript
2002   "P" gnus-uu-decode-postscript-and-save)
2003
2004 (gnus-define-keys
2005     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2006   "u" gnus-uu-decode-uu-view
2007   "U" gnus-uu-decode-uu-and-save-view
2008   "s" gnus-uu-decode-unshar-view
2009   "S" gnus-uu-decode-unshar-and-save-view
2010   "o" gnus-uu-decode-save-view
2011   "O" gnus-uu-decode-save-view
2012   "b" gnus-uu-decode-binhex-view
2013   "B" gnus-uu-decode-binhex-view
2014   "p" gnus-uu-decode-postscript-view
2015   "P" gnus-uu-decode-postscript-and-save-view)
2016
2017 (defvar gnus-article-post-menu nil)
2018
2019 (defconst gnus-summary-menu-maxlen 20)
2020
2021 (defun gnus-summary-menu-split (menu)
2022   ;; If we have lots of elements, divide them into groups of 20
2023   ;; and make a pane (or submenu) for each one.
2024   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2025       (let ((menu menu) sublists next
2026             (i 1))
2027         (while menu
2028           ;; Pull off the next gnus-summary-menu-maxlen elements
2029           ;; and make them the next element of sublist.
2030           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2031           (if next
2032               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2033                       nil))
2034           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2035                                              (aref (car (last menu)) 0)) menu)
2036                                sublists))
2037           (setq i (1+ i))
2038           (setq menu next))
2039         (nreverse sublists))
2040     ;; Few elements--put them all in one pane.
2041     menu))
2042
2043 (defun gnus-summary-make-menu-bar ()
2044   (gnus-turn-off-edit-menu 'summary)
2045
2046   (unless (boundp 'gnus-summary-misc-menu)
2047
2048     (easy-menu-define
2049       gnus-summary-kill-menu gnus-summary-mode-map ""
2050       (cons
2051        "Score"
2052        (nconc
2053         (list
2054          ["Customize" gnus-score-customize t])
2055         (gnus-make-score-map 'increase)
2056         (gnus-make-score-map 'lower)
2057         '(("Mark"
2058            ["Kill below" gnus-summary-kill-below t]
2059            ["Mark above" gnus-summary-mark-above t]
2060            ["Tick above" gnus-summary-tick-above t]
2061            ["Clear above" gnus-summary-clear-above t])
2062           ["Current score" gnus-summary-current-score t]
2063           ["Set score" gnus-summary-set-score t]
2064           ["Switch current score file..." gnus-score-change-score-file t]
2065           ["Set mark below..." gnus-score-set-mark-below t]
2066           ["Set expunge below..." gnus-score-set-expunge-below t]
2067           ["Edit current score file" gnus-score-edit-current-scores t]
2068           ["Edit score file" gnus-score-edit-file t]
2069           ["Trace score" gnus-score-find-trace t]
2070           ["Find words" gnus-score-find-favourite-words t]
2071           ["Rescore buffer" gnus-summary-rescore t]
2072           ["Increase score..." gnus-summary-increase-score t]
2073           ["Lower score..." gnus-summary-lower-score t]))))
2074
2075     ;; Define both the Article menu in the summary buffer and the
2076     ;; equivalent Commands menu in the article buffer here for
2077     ;; consistency.
2078     (let ((innards
2079            `(("Hide"
2080               ["All" gnus-article-hide t]
2081               ["Headers" gnus-article-hide-headers t]
2082               ["Signature" gnus-article-hide-signature t]
2083               ["Citation" gnus-article-hide-citation t]
2084               ["List identifiers" gnus-article-hide-list-identifiers t]
2085               ["Banner" gnus-article-strip-banner t]
2086               ["Boring headers" gnus-article-hide-boring-headers t])
2087              ("Highlight"
2088               ["All" gnus-article-highlight t]
2089               ["Headers" gnus-article-highlight-headers t]
2090               ["Signature" gnus-article-highlight-signature t]
2091               ["Citation" gnus-article-highlight-citation t])
2092              ("MIME"
2093               ["Words" gnus-article-decode-mime-words t]
2094               ["Charset" gnus-article-decode-charset t]
2095               ["QP" gnus-article-de-quoted-unreadable t]
2096               ["Base64" gnus-article-de-base64-unreadable t]
2097               ["View MIME buttons" gnus-summary-display-buttonized t]
2098               ["View all" gnus-mime-view-all-parts t]
2099               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2100               ["Encrypt body" gnus-article-encrypt-body
2101                :active (not (gnus-group-read-only-p))
2102                ,@(if (featurep 'xemacs) nil
2103                    '(:help "Encrypt the message body on disk"))]
2104               ["Extract all parts" gnus-summary-save-parts t]
2105               ("Multipart"
2106                ["Repair multipart" gnus-summary-repair-multipart t]
2107                ["Add buttons" gnus-summary-display-buttonized t]
2108                ["Pipe part" gnus-article-pipe-part t]
2109                ["Inline part" gnus-article-inline-part t]
2110                ["Encrypt body" gnus-article-encrypt-body
2111                 :active (not (gnus-group-read-only-p))
2112                ,@(if (featurep 'xemacs) nil
2113                    '(:help "Encrypt the message body on disk"))]
2114                ["View part externally" gnus-article-view-part-externally t]
2115                ["View part with charset" gnus-article-view-part-as-charset t]
2116                ["Copy part" gnus-article-copy-part t]
2117                ["Save part" gnus-article-save-part t]
2118                ["View part" gnus-article-view-part t]))
2119              ("Date"
2120               ["Local" gnus-article-date-local t]
2121               ["ISO8601" gnus-article-date-iso8601 t]
2122               ["UT" gnus-article-date-ut t]
2123               ["Original" gnus-article-date-original t]
2124               ["Lapsed" gnus-article-date-lapsed t]
2125               ["User-defined" gnus-article-date-user t])
2126              ("Display"
2127               ["Remove images" gnus-article-remove-images t]
2128               ["Toggle smiley" gnus-treat-smiley t]
2129               ["Show X-Face" gnus-article-display-x-face t]
2130               ["Show picons in From" gnus-treat-from-picon t]
2131               ["Show picons in mail headers" gnus-treat-mail-picon t]
2132               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2133               ("View as different encoding"
2134                ,@(gnus-summary-menu-split
2135                   (mapcar
2136                    (lambda (cs)
2137                      ;; Since easymenu under Emacs doesn't allow
2138                      ;; lambda forms for menu commands, we should
2139                      ;; provide intern'ed function symbols.
2140                      (let ((command (intern (format "\
2141 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2142                        (fset command
2143                              `(lambda ()
2144                                 (interactive)
2145                                 (let ((gnus-summary-show-article-charset-alist
2146                                        '((1 . ,cs))))
2147                                   (gnus-summary-show-article 1))))
2148                        `[,(symbol-name cs) ,command t]))
2149                    (sort (if (fboundp 'coding-system-list)
2150                              (coding-system-list)
2151                            (mapcar 'car mm-mime-mule-charset-alist))
2152                          'string<)))))
2153              ("Washing"
2154               ("Remove Blanks"
2155                ["Leading" gnus-article-strip-leading-blank-lines t]
2156                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2157                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2158                ["All of the above" gnus-article-strip-blank-lines t]
2159                ["All" gnus-article-strip-all-blank-lines t]
2160                ["Leading space" gnus-article-strip-leading-space t]
2161                ["Trailing space" gnus-article-strip-trailing-space t]
2162                ["Leading space in headers"
2163                 gnus-article-remove-leading-whitespace t])
2164               ["Overstrike" gnus-article-treat-overstrike t]
2165               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2166               ["Emphasis" gnus-article-emphasize t]
2167               ["Word wrap" gnus-article-fill-cited-article t]
2168               ["Fill long lines" gnus-article-fill-long-lines t]
2169               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2170               ["CR" gnus-article-remove-cr t]
2171               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2172               ["Base64" gnus-article-de-base64-unreadable t]
2173               ["Rot 13" gnus-summary-caesar-message
2174                ,@(if (featurep 'xemacs) '(t)
2175                    '(:help "\"Caesar rotate\" article by 13"))]
2176               ["Morse decode" gnus-summary-morse-message t]
2177               ["Unix pipe..." gnus-summary-pipe-message t]
2178               ["Add buttons" gnus-article-add-buttons t]
2179               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2180               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2181               ["Verbose header" gnus-summary-verbose-headers t]
2182               ["Toggle header" gnus-summary-toggle-header t]
2183               ["Unfold headers" gnus-article-treat-unfold-headers t]
2184               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2185               ["Html" gnus-article-wash-html t]
2186               ["URLs" gnus-article-unsplit-urls t]
2187               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2188               ["HZ" gnus-article-decode-HZ t]
2189               ("(Outlook) Deuglify"
2190                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2191                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2192                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2193                ["Full (Outlook) deuglify"
2194                 gnus-article-outlook-deuglify-article t])
2195               )
2196              ("Output"
2197               ["Save in default format" gnus-summary-save-article
2198                ,@(if (featurep 'xemacs) '(t)
2199                    '(:help "Save article using default method"))]
2200               ["Save in file" gnus-summary-save-article-file
2201                ,@(if (featurep 'xemacs) '(t)
2202                    '(:help "Save article in file"))]
2203               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2204               ["Save in MH folder" gnus-summary-save-article-folder t]
2205               ["Save in VM folder" gnus-summary-save-article-vm t]
2206               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2207               ["Save body in file" gnus-summary-save-article-body-file t]
2208               ["Pipe through a filter" gnus-summary-pipe-output t]
2209               ["Add to SOUP packet" gnus-soup-add-article t]
2210               ["Print with Muttprint" gnus-summary-muttprint t]
2211               ["Print" gnus-summary-print-article t])
2212              ("Backend"
2213               ["Respool article..." gnus-summary-respool-article t]
2214               ["Move article..." gnus-summary-move-article
2215                (gnus-check-backend-function
2216                 'request-move-article gnus-newsgroup-name)]
2217               ["Copy article..." gnus-summary-copy-article t]
2218               ["Crosspost article..." gnus-summary-crosspost-article
2219                (gnus-check-backend-function
2220                 'request-replace-article gnus-newsgroup-name)]
2221               ["Import file..." gnus-summary-import-article
2222                (gnus-check-backend-function
2223                 'request-accept-article gnus-newsgroup-name)]
2224               ["Create article..." gnus-summary-create-article
2225                (gnus-check-backend-function
2226                 'request-accept-article gnus-newsgroup-name)]
2227               ["Check if posted" gnus-summary-article-posted-p t]
2228               ["Edit article" gnus-summary-edit-article
2229                (not (gnus-group-read-only-p))]
2230               ["Delete article" gnus-summary-delete-article
2231                (gnus-check-backend-function
2232                 'request-expire-articles gnus-newsgroup-name)]
2233               ["Query respool" gnus-summary-respool-query t]
2234               ["Trace respool" gnus-summary-respool-trace t]
2235               ["Delete expirable articles" gnus-summary-expire-articles-now
2236                (gnus-check-backend-function
2237                 'request-expire-articles gnus-newsgroup-name)])
2238              ("Extract"
2239               ["Uudecode" gnus-uu-decode-uu
2240                ,@(if (featurep 'xemacs) '(t)
2241                    '(:help "Decode uuencoded article(s)"))]
2242               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2243               ["Unshar" gnus-uu-decode-unshar t]
2244               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2245               ["Save" gnus-uu-decode-save t]
2246               ["Binhex" gnus-uu-decode-binhex t]
2247               ["Postscript" gnus-uu-decode-postscript t]
2248               ["All MIME parts" gnus-summary-save-parts t])
2249              ("Cache"
2250               ["Enter article" gnus-cache-enter-article t]
2251               ["Remove article" gnus-cache-remove-article t])
2252              ["Translate" gnus-article-babel t]
2253              ["Select article buffer" gnus-summary-select-article-buffer t]
2254              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2255              ["Isearch article..." gnus-summary-isearch-article t]
2256              ["Beginning of the article" gnus-summary-beginning-of-article t]
2257              ["End of the article" gnus-summary-end-of-article t]
2258              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2259              ["Fetch referenced articles" gnus-summary-refer-references t]
2260              ["Fetch current thread" gnus-summary-refer-thread t]
2261              ["Fetch article with id..." gnus-summary-refer-article t]
2262              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2263              ["Redisplay" gnus-summary-show-article t]
2264              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2265       (easy-menu-define
2266         gnus-summary-article-menu gnus-summary-mode-map ""
2267         (cons "Article" innards))
2268
2269       (if (not (keymapp gnus-summary-article-menu))
2270           (easy-menu-define
2271             gnus-article-commands-menu gnus-article-mode-map ""
2272             (cons "Commands" innards))
2273         ;; in Emacs, don't share menu.
2274         (setq gnus-article-commands-menu
2275               (copy-keymap gnus-summary-article-menu))
2276         (define-key gnus-article-mode-map [menu-bar commands]
2277           (cons "Commands" gnus-article-commands-menu))))
2278
2279     (easy-menu-define
2280       gnus-summary-thread-menu gnus-summary-mode-map ""
2281       '("Threads"
2282         ["Find all messages in thread" gnus-summary-refer-thread t]
2283         ["Toggle threading" gnus-summary-toggle-threads t]
2284         ["Hide threads" gnus-summary-hide-all-threads t]
2285         ["Show threads" gnus-summary-show-all-threads t]
2286         ["Hide thread" gnus-summary-hide-thread t]
2287         ["Show thread" gnus-summary-show-thread t]
2288         ["Go to next thread" gnus-summary-next-thread t]
2289         ["Go to previous thread" gnus-summary-prev-thread t]
2290         ["Go down thread" gnus-summary-down-thread t]
2291         ["Go up thread" gnus-summary-up-thread t]
2292         ["Top of thread" gnus-summary-top-thread t]
2293         ["Mark thread as read" gnus-summary-kill-thread t]
2294         ["Lower thread score" gnus-summary-lower-thread t]
2295         ["Raise thread score" gnus-summary-raise-thread t]
2296         ["Rethread current" gnus-summary-rethread-current t]))
2297
2298     (easy-menu-define
2299       gnus-summary-post-menu gnus-summary-mode-map ""
2300       `("Post"
2301         ["Send a message (mail or news)" gnus-summary-post-news
2302          ,@(if (featurep 'xemacs) '(t)
2303              '(:help "Post an article"))]
2304         ["Followup" gnus-summary-followup
2305          ,@(if (featurep 'xemacs) '(t)
2306              '(:help "Post followup to this article"))]
2307         ["Followup and yank" gnus-summary-followup-with-original
2308          ,@(if (featurep 'xemacs) '(t)
2309              '(:help "Post followup to this article, quoting its contents"))]
2310         ["Supersede article" gnus-summary-supersede-article t]
2311         ["Cancel article" gnus-summary-cancel-article
2312          ,@(if (featurep 'xemacs) '(t)
2313              '(:help "Cancel an article you posted"))]
2314         ["Reply" gnus-summary-reply t]
2315         ["Reply and yank" gnus-summary-reply-with-original t]
2316         ["Wide reply" gnus-summary-wide-reply t]
2317         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2318          ,@(if (featurep 'xemacs) '(t)
2319              '(:help "Mail a reply, quoting this article"))]
2320         ["Very wide reply" gnus-summary-very-wide-reply t]
2321         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2322          ,@(if (featurep 'xemacs) '(t)
2323              '(:help "Mail a very wide reply, quoting this article"))]
2324         ["Mail forward" gnus-summary-mail-forward t]
2325         ["Post forward" gnus-summary-post-forward t]
2326         ["Digest and mail" gnus-uu-digest-mail-forward t]
2327         ["Digest and post" gnus-uu-digest-post-forward t]
2328         ["Resend message" gnus-summary-resend-message t]
2329         ["Resend message edit" gnus-summary-resend-message-edit t]
2330         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2331         ["Send a mail" gnus-summary-mail-other-window t]
2332         ["Create a local message" gnus-summary-news-other-window t]
2333         ["Uuencode and post" gnus-uu-post-news
2334          ,@(if (featurep 'xemacs) '(t)
2335              '(:help "Post a uuencoded article"))]
2336         ["Followup via news" gnus-summary-followup-to-mail t]
2337         ["Followup via news and yank"
2338          gnus-summary-followup-to-mail-with-original t]
2339         ;;("Draft"
2340         ;;["Send" gnus-summary-send-draft t]
2341         ;;["Send bounced" gnus-resend-bounced-mail t])
2342         ))
2343
2344     (cond
2345      ((not (keymapp gnus-summary-post-menu))
2346       (setq gnus-article-post-menu gnus-summary-post-menu))
2347      ((not gnus-article-post-menu)
2348       ;; Don't share post menu.
2349       (setq gnus-article-post-menu
2350             (copy-keymap gnus-summary-post-menu))))
2351     (define-key gnus-article-mode-map [menu-bar post]
2352       (cons "Post" gnus-article-post-menu))
2353
2354     (easy-menu-define
2355       gnus-summary-misc-menu gnus-summary-mode-map ""
2356       `("Gnus"
2357         ("Mark Read"
2358          ["Mark as read" gnus-summary-mark-as-read-forward t]
2359          ["Mark same subject and select"
2360           gnus-summary-kill-same-subject-and-select t]
2361          ["Mark same subject" gnus-summary-kill-same-subject t]
2362          ["Catchup" gnus-summary-catchup
2363           ,@(if (featurep 'xemacs) '(t)
2364               '(:help "Mark unread articles in this group as read"))]
2365          ["Catchup all" gnus-summary-catchup-all t]
2366          ["Catchup to here" gnus-summary-catchup-to-here t]
2367          ["Catchup from here" gnus-summary-catchup-from-here t]
2368          ["Catchup region" gnus-summary-mark-region-as-read 
2369           (gnus-mark-active-p)]
2370          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2371         ("Mark Various"
2372          ["Tick" gnus-summary-tick-article-forward t]
2373          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2374          ["Remove marks" gnus-summary-clear-mark-forward t]
2375          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2376          ["Set bookmark" gnus-summary-set-bookmark t]
2377          ["Remove bookmark" gnus-summary-remove-bookmark t])
2378         ("Limit to"
2379          ["Marks..." gnus-summary-limit-to-marks t]
2380          ["Subject..." gnus-summary-limit-to-subject t]
2381          ["Author..." gnus-summary-limit-to-author t]
2382          ["Age..." gnus-summary-limit-to-age t]
2383          ["Extra..." gnus-summary-limit-to-extra t]
2384          ["Score..." gnus-summary-limit-to-score t]
2385          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2386          ["Unread" gnus-summary-limit-to-unread t]
2387          ["Unseen" gnus-summary-limit-to-unseen t]
2388          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2389          ["Next articles" gnus-summary-limit-to-articles t]
2390          ["Pop limit" gnus-summary-pop-limit t]
2391          ["Show dormant" gnus-summary-limit-include-dormant t]
2392          ["Hide childless dormant"
2393           gnus-summary-limit-exclude-childless-dormant t]
2394          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2395          ["Hide marked" gnus-summary-limit-exclude-marks t]
2396          ["Show expunged" gnus-summary-limit-include-expunged t])
2397         ("Process Mark"
2398          ["Set mark" gnus-summary-mark-as-processable t]
2399          ["Remove mark" gnus-summary-unmark-as-processable t]
2400          ["Remove all marks" gnus-summary-unmark-all-processable t]
2401          ["Mark above" gnus-uu-mark-over t]
2402          ["Mark series" gnus-uu-mark-series t]
2403          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2404          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2405          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2406          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2407          ["Mark all" gnus-uu-mark-all t]
2408          ["Mark buffer" gnus-uu-mark-buffer t]
2409          ["Mark sparse" gnus-uu-mark-sparse t]
2410          ["Mark thread" gnus-uu-mark-thread t]
2411          ["Unmark thread" gnus-uu-unmark-thread t]
2412          ("Process Mark Sets"
2413           ["Kill" gnus-summary-kill-process-mark t]
2414           ["Yank" gnus-summary-yank-process-mark
2415            gnus-newsgroup-process-stack]
2416           ["Save" gnus-summary-save-process-mark t]
2417           ["Run command on marked..." gnus-summary-universal-argument t]))
2418         ("Scroll article"
2419          ["Page forward" gnus-summary-next-page
2420           ,@(if (featurep 'xemacs) '(t)
2421               '(:help "Show next page of article"))]
2422          ["Page backward" gnus-summary-prev-page
2423           ,@(if (featurep 'xemacs) '(t)
2424               '(:help "Show previous page of article"))]
2425          ["Line forward" gnus-summary-scroll-up t])
2426         ("Move"
2427          ["Next unread article" gnus-summary-next-unread-article t]
2428          ["Previous unread article" gnus-summary-prev-unread-article t]
2429          ["Next article" gnus-summary-next-article t]
2430          ["Previous article" gnus-summary-prev-article t]
2431          ["Next unread subject" gnus-summary-next-unread-subject t]
2432          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2433          ["Next article same subject" gnus-summary-next-same-subject t]
2434          ["Previous article same subject" gnus-summary-prev-same-subject t]
2435          ["First unread article" gnus-summary-first-unread-article t]
2436          ["Best unread article" gnus-summary-best-unread-article t]
2437          ["Go to subject number..." gnus-summary-goto-subject t]
2438          ["Go to article number..." gnus-summary-goto-article t]
2439          ["Go to the last article" gnus-summary-goto-last-article t]
2440          ["Pop article off history" gnus-summary-pop-article t])
2441         ("Sort"
2442          ["Sort by number" gnus-summary-sort-by-number t]
2443          ["Sort by author" gnus-summary-sort-by-author t]
2444          ["Sort by subject" gnus-summary-sort-by-subject t]
2445          ["Sort by date" gnus-summary-sort-by-date t]
2446          ["Sort by score" gnus-summary-sort-by-score t]
2447          ["Sort by lines" gnus-summary-sort-by-lines t]
2448          ["Sort by characters" gnus-summary-sort-by-chars t]
2449          ["Randomize" gnus-summary-sort-by-random t]
2450          ["Original sort" gnus-summary-sort-by-original t])
2451         ("Help"
2452          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2453          ["Describe group" gnus-summary-describe-group t]
2454          ["Fetch charter" gnus-group-fetch-charter
2455           ,@(if (featurep 'xemacs) nil
2456               '(:help "Display the charter of the current group"))]
2457          ["Fetch control message" gnus-group-fetch-control
2458           ,@(if (featurep 'xemacs) nil
2459               '(:help "Display the archived control message for the current group"))]
2460          ["Read manual" gnus-info-find-node t])
2461         ("Modes"
2462          ["Pick and read" gnus-pick-mode t]
2463          ["Binary" gnus-binary-mode t])
2464         ("Regeneration"
2465          ["Regenerate" gnus-summary-prepare t]
2466          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2467          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2468          ["Toggle threading" gnus-summary-toggle-threads t])
2469         ["See old articles" gnus-summary-insert-old-articles t]
2470         ["See new articles" gnus-summary-insert-new-articles t]
2471         ["Filter articles..." gnus-summary-execute-command t]
2472         ["Run command on articles..." gnus-summary-universal-argument t]
2473         ["Search articles forward..." gnus-summary-search-article-forward t]
2474         ["Search articles backward..." gnus-summary-search-article-backward t]
2475         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2476         ["Expand window" gnus-summary-expand-window t]
2477         ["Expire expirable articles" gnus-summary-expire-articles
2478          (gnus-check-backend-function
2479           'request-expire-articles gnus-newsgroup-name)]
2480         ["Edit local kill file" gnus-summary-edit-local-kill t]
2481         ["Edit main kill file" gnus-summary-edit-global-kill t]
2482         ["Edit group parameters" gnus-summary-edit-parameters t]
2483         ["Customize group parameters" gnus-summary-customize-parameters t]
2484         ["Send a bug report" gnus-bug t]
2485         ("Exit"
2486          ["Catchup and exit" gnus-summary-catchup-and-exit
2487           ,@(if (featurep 'xemacs) '(t)
2488               '(:help "Mark unread articles in this group as read, then exit"))]
2489          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2490          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2491          ["Exit group" gnus-summary-exit
2492           ,@(if (featurep 'xemacs) '(t)
2493               '(:help "Exit current group, return to group selection mode"))]
2494          ["Exit group without updating" gnus-summary-exit-no-update t]
2495          ["Exit and goto next group" gnus-summary-next-group t]
2496          ["Exit and goto prev group" gnus-summary-prev-group t]
2497          ["Reselect group" gnus-summary-reselect-current-group t]
2498          ["Rescan group" gnus-summary-rescan-group t]
2499          ["Update dribble" gnus-summary-save-newsrc t])))
2500
2501     (gnus-run-hooks 'gnus-summary-menu-hook)))
2502
2503 (defvar gnus-summary-tool-bar-map nil)
2504
2505 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2506 (defun gnus-summary-make-tool-bar ()
2507   (if (and (fboundp 'tool-bar-add-item-from-menu)
2508            (default-value 'tool-bar-mode)
2509            (not gnus-summary-tool-bar-map))
2510       (setq gnus-summary-tool-bar-map
2511             (let ((tool-bar-map (make-sparse-keymap))
2512                   (load-path (mm-image-load-path)))
2513               (tool-bar-add-item-from-menu
2514                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2515               (tool-bar-add-item-from-menu
2516                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2517               (tool-bar-add-item-from-menu
2518                'gnus-summary-post-news "post" gnus-summary-mode-map)
2519               (tool-bar-add-item-from-menu
2520                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-followup "followup" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-reply "reply" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2533               (tool-bar-add-item-from-menu
2534                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2535               (tool-bar-add-item-from-menu
2536                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2537               (tool-bar-add-item-from-menu
2538                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2539               (tool-bar-add-item-from-menu
2540                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2541               (tool-bar-add-item-from-menu
2542                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2543               tool-bar-map)))
2544   (if gnus-summary-tool-bar-map
2545       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2546
2547 (defun gnus-score-set-default (var value)
2548   "A version of set that updates the GNU Emacs menu-bar."
2549   (set var value)
2550   ;; It is the message that forces the active status to be updated.
2551   (message ""))
2552
2553 (defun gnus-make-score-map (type)
2554   "Make a summary score map of type TYPE."
2555   (if t
2556       nil
2557     (let ((headers '(("author" "from" string)
2558                      ("subject" "subject" string)
2559                      ("article body" "body" string)
2560                      ("article head" "head" string)
2561                      ("xref" "xref" string)
2562                      ("extra header" "extra" string)
2563                      ("lines" "lines" number)
2564                      ("followups to author" "followup" string)))
2565           (types '((number ("less than" <)
2566                            ("greater than" >)
2567                            ("equal" =))
2568                    (string ("substring" s)
2569                            ("exact string" e)
2570                            ("fuzzy string" f)
2571                            ("regexp" r))))
2572           (perms '(("temporary" (current-time-string))
2573                    ("permanent" nil)
2574                    ("immediate" now)))
2575           header)
2576       (list
2577        (apply
2578         'nconc
2579         (list
2580          (if (eq type 'lower)
2581              "Lower score"
2582            "Increase score"))
2583         (let (outh)
2584           (while headers
2585             (setq header (car headers))
2586             (setq outh
2587                   (cons
2588                    (apply
2589                     'nconc
2590                     (list (car header))
2591                     (let ((ts (cdr (assoc (nth 2 header) types)))
2592                           outt)
2593                       (while ts
2594                         (setq outt
2595                               (cons
2596                                (apply
2597                                 'nconc
2598                                 (list (caar ts))
2599                                 (let ((ps perms)
2600                                       outp)
2601                                   (while ps
2602                                     (setq outp
2603                                           (cons
2604                                            (vector
2605                                             (caar ps)
2606                                             (list
2607                                              'gnus-summary-score-entry
2608                                              (nth 1 header)
2609                                              (if (or (string= (nth 1 header)
2610                                                               "head")
2611                                                      (string= (nth 1 header)
2612                                                               "body"))
2613                                                  ""
2614                                                (list 'gnus-summary-header
2615                                                      (nth 1 header)))
2616                                              (list 'quote (nth 1 (car ts)))
2617                                              (list 'gnus-score-delta-default
2618                                                    nil)
2619                                              (nth 1 (car ps))
2620                                              t)
2621                                             t)
2622                                            outp))
2623                                     (setq ps (cdr ps)))
2624                                   (list (nreverse outp))))
2625                                outt))
2626                         (setq ts (cdr ts)))
2627                       (list (nreverse outt))))
2628                    outh))
2629             (setq headers (cdr headers)))
2630           (list (nreverse outh))))))))
2631
2632 \f
2633
2634 (defun gnus-summary-mode (&optional group)
2635   "Major mode for reading articles.
2636
2637 All normal editing commands are switched off.
2638 \\<gnus-summary-mode-map>
2639 Each line in this buffer represents one article.  To read an
2640 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2641 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2642 respectively.
2643
2644 You can also post articles and send mail from this buffer.  To
2645 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2646 of an article, type `\\[gnus-summary-reply]'.
2647
2648 There are approx. one gazillion commands you can execute in this
2649 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2650
2651 The following commands are available:
2652
2653 \\{gnus-summary-mode-map}"
2654   (interactive)
2655   (kill-all-local-variables)
2656   (when (gnus-visual-p 'summary-menu 'menu)
2657     (gnus-summary-make-menu-bar)
2658     (gnus-summary-make-tool-bar))
2659   (gnus-summary-make-local-variables)
2660   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2661     (gnus-summary-make-local-variables))
2662   (gnus-make-thread-indent-array)
2663   (gnus-simplify-mode-line)
2664   (setq major-mode 'gnus-summary-mode)
2665   (setq mode-name "Summary")
2666   (make-local-variable 'minor-mode-alist)
2667   (use-local-map gnus-summary-mode-map)
2668   (buffer-disable-undo)
2669   (setq buffer-read-only t)             ;Disable modification
2670   (setq truncate-lines t)
2671   (setq selective-display t)
2672   (setq selective-display-ellipses t)   ;Display `...'
2673   (gnus-summary-set-display-table)
2674   (gnus-set-default-directory)
2675   (setq gnus-newsgroup-name group)
2676   (make-local-variable 'gnus-summary-line-format)
2677   (make-local-variable 'gnus-summary-line-format-spec)
2678   (make-local-variable 'gnus-summary-dummy-line-format)
2679   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2680   (make-local-variable 'gnus-summary-mark-positions)
2681   (gnus-make-local-hook 'pre-command-hook)
2682   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2683   (gnus-run-hooks 'gnus-summary-mode-hook)
2684   (turn-on-gnus-mailing-list-mode)
2685   (mm-enable-multibyte)
2686   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2687   (gnus-update-summary-mark-positions))
2688
2689 (defun gnus-summary-make-local-variables ()
2690   "Make all the local summary buffer variables."
2691   (let (global)
2692     (dolist (local gnus-summary-local-variables)
2693       (if (consp local)
2694           (progn
2695             (if (eq (cdr local) 'global)
2696                 ;; Copy the global value of the variable.
2697                 (setq global (symbol-value (car local)))
2698               ;; Use the value from the list.
2699               (setq global (eval (cdr local))))
2700             (set (make-local-variable (car local)) global))
2701         ;; Simple nil-valued local variable.
2702         (set (make-local-variable local) nil)))))
2703
2704 (defun gnus-summary-clear-local-variables ()
2705   (let ((locals gnus-summary-local-variables))
2706     (while locals
2707       (if (consp (car locals))
2708           (and (vectorp (caar locals))
2709                (set (caar locals) nil))
2710         (and (vectorp (car locals))
2711              (set (car locals) nil)))
2712       (setq locals (cdr locals)))))
2713
2714 ;; Summary data functions.
2715
2716 (defmacro gnus-data-number (data)
2717   `(car ,data))
2718
2719 (defmacro gnus-data-set-number (data number)
2720   `(setcar ,data ,number))
2721
2722 (defmacro gnus-data-mark (data)
2723   `(nth 1 ,data))
2724
2725 (defmacro gnus-data-set-mark (data mark)
2726   `(setcar (nthcdr 1 ,data) ,mark))
2727
2728 (defmacro gnus-data-pos (data)
2729   `(nth 2 ,data))
2730
2731 (defmacro gnus-data-set-pos (data pos)
2732   `(setcar (nthcdr 2 ,data) ,pos))
2733
2734 (defmacro gnus-data-header (data)
2735   `(nth 3 ,data))
2736
2737 (defmacro gnus-data-set-header (data header)
2738   `(setf (nth 3 ,data) ,header))
2739
2740 (defmacro gnus-data-level (data)
2741   `(nth 4 ,data))
2742
2743 (defmacro gnus-data-unread-p (data)
2744   `(= (nth 1 ,data) gnus-unread-mark))
2745
2746 (defmacro gnus-data-read-p (data)
2747   `(/= (nth 1 ,data) gnus-unread-mark))
2748
2749 (defmacro gnus-data-pseudo-p (data)
2750   `(consp (nth 3 ,data)))
2751
2752 (defmacro gnus-data-find (number)
2753   `(assq ,number gnus-newsgroup-data))
2754
2755 (defmacro gnus-data-find-list (number &optional data)
2756   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2757      (memq (assq ,number bdata)
2758            bdata)))
2759
2760 (defmacro gnus-data-make (number mark pos header level)
2761   `(list ,number ,mark ,pos ,header ,level))
2762
2763 (defun gnus-data-enter (after-article number mark pos header level offset)
2764   (let ((data (gnus-data-find-list after-article)))
2765     (unless data
2766       (error "No such article: %d" after-article))
2767     (setcdr data (cons (gnus-data-make number mark pos header level)
2768                        (cdr data)))
2769     (setq gnus-newsgroup-data-reverse nil)
2770     (gnus-data-update-list (cddr data) offset)))
2771
2772 (defun gnus-data-enter-list (after-article list &optional offset)
2773   (when list
2774     (let ((data (and after-article (gnus-data-find-list after-article)))
2775           (ilist list))
2776       (if (not (or data
2777                    after-article))
2778           (let ((odata gnus-newsgroup-data))
2779             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2780             (when offset
2781               (gnus-data-update-list odata offset)))
2782       ;; Find the last element in the list to be spliced into the main
2783         ;; list.
2784         (while (cdr list)
2785           (setq list (cdr list)))
2786         (if (not data)
2787             (progn
2788               (setcdr list gnus-newsgroup-data)
2789               (setq gnus-newsgroup-data ilist)
2790               (when offset
2791                 (gnus-data-update-list (cdr list) offset)))
2792           (setcdr list (cdr data))
2793           (setcdr data ilist)
2794           (when offset
2795             (gnus-data-update-list (cdr list) offset))))
2796       (setq gnus-newsgroup-data-reverse nil))))
2797
2798 (defun gnus-data-remove (article &optional offset)
2799   (let ((data gnus-newsgroup-data))
2800     (if (= (gnus-data-number (car data)) article)
2801         (progn
2802           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2803                 gnus-newsgroup-data-reverse nil)
2804           (when offset
2805             (gnus-data-update-list gnus-newsgroup-data offset)))
2806       (while (cdr data)
2807         (when (= (gnus-data-number (cadr data)) article)
2808           (setcdr data (cddr data))
2809           (when offset
2810             (gnus-data-update-list (cdr data) offset))
2811           (setq data nil
2812                 gnus-newsgroup-data-reverse nil))
2813         (setq data (cdr data))))))
2814
2815 (defmacro gnus-data-list (backward)
2816   `(if ,backward
2817        (or gnus-newsgroup-data-reverse
2818            (setq gnus-newsgroup-data-reverse
2819                  (reverse gnus-newsgroup-data)))
2820      gnus-newsgroup-data))
2821
2822 (defun gnus-data-update-list (data offset)
2823   "Add OFFSET to the POS of all data entries in DATA."
2824   (setq gnus-newsgroup-data-reverse nil)
2825   (while data
2826     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2827     (setq data (cdr data))))
2828
2829 (defun gnus-summary-article-pseudo-p (article)
2830   "Say whether this article is a pseudo article or not."
2831   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2832
2833 (defmacro gnus-summary-article-sparse-p (article)
2834   "Say whether this article is a sparse article or not."
2835   `(memq ,article gnus-newsgroup-sparse))
2836
2837 (defmacro gnus-summary-article-ancient-p (article)
2838   "Say whether this article is a sparse article or not."
2839   `(memq ,article gnus-newsgroup-ancient))
2840
2841 (defun gnus-article-parent-p (number)
2842   "Say whether this article is a parent or not."
2843   (let ((data (gnus-data-find-list number)))
2844     (and (cdr data)              ; There has to be an article after...
2845          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2846             (gnus-data-level (nth 1 data))))))
2847
2848 (defun gnus-article-children (number)
2849   "Return a list of all children to NUMBER."
2850   (let* ((data (gnus-data-find-list number))
2851          (level (gnus-data-level (car data)))
2852          children)
2853     (setq data (cdr data))
2854     (while (and data
2855                 (= (gnus-data-level (car data)) (1+ level)))
2856       (push (gnus-data-number (car data)) children)
2857       (setq data (cdr data)))
2858     children))
2859
2860 (defmacro gnus-summary-skip-intangible ()
2861   "If the current article is intangible, then jump to a different article."
2862   '(let ((to (get-text-property (point) 'gnus-intangible)))
2863      (and to (gnus-summary-goto-subject to))))
2864
2865 (defmacro gnus-summary-article-intangible-p ()
2866   "Say whether this article is intangible or not."
2867   '(get-text-property (point) 'gnus-intangible))
2868
2869 (defun gnus-article-read-p (article)
2870   "Say whether ARTICLE is read or not."
2871   (not (or (memq article gnus-newsgroup-marked)
2872            (memq article gnus-newsgroup-spam-marked)
2873            (memq article gnus-newsgroup-unreads)
2874            (memq article gnus-newsgroup-unselected)
2875            (memq article gnus-newsgroup-dormant))))
2876
2877 ;; Some summary mode macros.
2878
2879 (defmacro gnus-summary-article-number ()
2880   "The article number of the article on the current line.
2881 If there isn't an article number here, then we return the current
2882 article number."
2883   '(progn
2884      (gnus-summary-skip-intangible)
2885      (or (get-text-property (point) 'gnus-number)
2886          (gnus-summary-last-subject))))
2887
2888 (defmacro gnus-summary-article-header (&optional number)
2889   "Return the header of article NUMBER."
2890   `(gnus-data-header (gnus-data-find
2891                       ,(or number '(gnus-summary-article-number)))))
2892
2893 (defmacro gnus-summary-thread-level (&optional number)
2894   "Return the level of thread that starts with article NUMBER."
2895   `(if (and (eq gnus-summary-make-false-root 'dummy)
2896             (get-text-property (point) 'gnus-intangible))
2897        0
2898      (gnus-data-level (gnus-data-find
2899                        ,(or number '(gnus-summary-article-number))))))
2900
2901 (defmacro gnus-summary-article-mark (&optional number)
2902   "Return the mark of article NUMBER."
2903   `(gnus-data-mark (gnus-data-find
2904                     ,(or number '(gnus-summary-article-number)))))
2905
2906 (defmacro gnus-summary-article-pos (&optional number)
2907   "Return the position of the line of article NUMBER."
2908   `(gnus-data-pos (gnus-data-find
2909                    ,(or number '(gnus-summary-article-number)))))
2910
2911 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2912 (defmacro gnus-summary-article-subject (&optional number)
2913   "Return current subject string or nil if nothing."
2914   `(let ((headers
2915           ,(if number
2916                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2917              '(gnus-data-header (assq (gnus-summary-article-number)
2918                                       gnus-newsgroup-data)))))
2919      (and headers
2920           (vectorp headers)
2921           (mail-header-subject headers))))
2922
2923 (defmacro gnus-summary-article-score (&optional number)
2924   "Return current article score."
2925   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2926                   gnus-newsgroup-scored))
2927        gnus-summary-default-score 0))
2928
2929 (defun gnus-summary-article-children (&optional number)
2930   "Return a list of article numbers that are children of article NUMBER."
2931   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2932          (level (gnus-data-level (car data)))
2933          l children)
2934     (while (and (setq data (cdr data))
2935                 (> (setq l (gnus-data-level (car data))) level))
2936       (and (= (1+ level) l)
2937            (push (gnus-data-number (car data))
2938                  children)))
2939     (nreverse children)))
2940
2941 (defun gnus-summary-article-parent (&optional number)
2942   "Return the article number of the parent of article NUMBER."
2943   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2944                                     (gnus-data-list t)))
2945          (level (gnus-data-level (car data))))
2946     (if (zerop level)
2947         ()                              ; This is a root.
2948       ;; We search until we find an article with a level less than
2949       ;; this one.  That function has to be the parent.
2950       (while (and (setq data (cdr data))
2951                   (not (< (gnus-data-level (car data)) level))))
2952       (and data (gnus-data-number (car data))))))
2953
2954 (defun gnus-unread-mark-p (mark)
2955   "Say whether MARK is the unread mark."
2956   (= mark gnus-unread-mark))
2957
2958 (defun gnus-read-mark-p (mark)
2959   "Say whether MARK is one of the marks that mark as read.
2960 This is all marks except unread, ticked, dormant, and expirable."
2961   (not (or (= mark gnus-unread-mark)
2962            (= mark gnus-ticked-mark)
2963            (= mark gnus-spam-mark)
2964            (= mark gnus-dormant-mark)
2965            (= mark gnus-expirable-mark))))
2966
2967 (defmacro gnus-article-mark (number)
2968   "Return the MARK of article NUMBER.
2969 This macro should only be used when computing the mark the \"first\"
2970 time; i.e., when generating the summary lines.  After that,
2971 `gnus-summary-article-mark' should be used to examine the
2972 marks of articles."
2973   `(cond
2974     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2975     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2976     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2977     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2978     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2979     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2980     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2981     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2982            gnus-ancient-mark))))
2983
2984 ;; Saving hidden threads.
2985
2986 (defmacro gnus-save-hidden-threads (&rest forms)
2987   "Save hidden threads, eval FORMS, and restore the hidden threads."
2988   (let ((config (make-symbol "config")))
2989     `(let ((,config (gnus-hidden-threads-configuration)))
2990        (unwind-protect
2991            (save-excursion
2992              ,@forms)
2993          (gnus-restore-hidden-threads-configuration ,config)))))
2994 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2995 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2996
2997 (defun gnus-data-compute-positions ()
2998   "Compute the positions of all articles."
2999   (setq gnus-newsgroup-data-reverse nil)
3000   (let ((data gnus-newsgroup-data))
3001     (save-excursion
3002       (gnus-save-hidden-threads
3003         (gnus-summary-show-all-threads)
3004         (goto-char (point-min))
3005         (while data
3006           (while (get-text-property (point) 'gnus-intangible)
3007             (forward-line 1))
3008           (gnus-data-set-pos (car data) (+ (point) 3))
3009           (setq data (cdr data))
3010           (forward-line 1))))))
3011
3012 (defun gnus-hidden-threads-configuration ()
3013   "Return the current hidden threads configuration."
3014   (save-excursion
3015     (let (config)
3016       (goto-char (point-min))
3017       (while (search-forward "\r" nil t)
3018         (push (1- (point)) config))
3019       config)))
3020
3021 (defun gnus-restore-hidden-threads-configuration (config)
3022   "Restore hidden threads configuration from CONFIG."
3023   (save-excursion
3024     (let (point buffer-read-only)
3025       (while (setq point (pop config))
3026         (when (and (< point (point-max))
3027                    (goto-char point)
3028                    (eq (char-after) ?\n))
3029           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3030
3031 ;; Various summary mode internalish functions.
3032
3033 (defun gnus-mouse-pick-article (e)
3034   (interactive "e")
3035   (mouse-set-point e)
3036   (gnus-summary-next-page nil t))
3037
3038 (defun gnus-summary-set-display-table ()
3039   "Change the display table.
3040 Odd characters have a tendency to mess
3041 up nicely formatted displays - we make all possible glyphs
3042 display only a single character."
3043
3044   ;; We start from the standard display table, if any.
3045   (let ((table (or (copy-sequence standard-display-table)
3046                    (make-display-table)))
3047         (i 32))
3048     ;; Nix out all the control chars...
3049     (while (>= (setq i (1- i)) 0)
3050       (aset table i [??]))
3051    ;; ... but not newline and cr, of course.  (cr is necessary for the
3052     ;; selective display).
3053     (aset table ?\n nil)
3054     (aset table ?\r nil)
3055     ;; We keep TAB as well.
3056     (aset table ?\t nil)
3057     ;; We nix out any glyphs over 126 that are not set already.
3058     (let ((i 256))
3059       (while (>= (setq i (1- i)) 127)
3060         ;; Only modify if the entry is nil.
3061         (unless (aref table i)
3062           (aset table i [??]))))
3063     (setq buffer-display-table table)))
3064
3065 (defun gnus-summary-set-article-display-arrow (pos)
3066   "Update the overlay arrow to point to line at position POS."
3067   (when (and gnus-summary-display-arrow
3068              (boundp 'overlay-arrow-position)
3069              (boundp 'overlay-arrow-string))
3070     (save-excursion
3071       (goto-char pos)
3072       (beginning-of-line)
3073       (unless overlay-arrow-position
3074         (setq overlay-arrow-position (make-marker)))
3075       (setq overlay-arrow-string "=>"
3076             overlay-arrow-position (set-marker overlay-arrow-position
3077                                                (point)
3078                                                (current-buffer))))))
3079
3080 (defun gnus-summary-setup-buffer (group)
3081   "Initialize summary buffer."
3082   (let ((buffer (gnus-summary-buffer-name group))
3083         (dead-name (concat "*Dead Summary "
3084                            (gnus-group-decoded-name group) "*")))
3085     ;; If a dead summary buffer exists, we kill it.
3086     (when (gnus-buffer-live-p dead-name)
3087       (gnus-kill-buffer dead-name))
3088     (if (get-buffer buffer)
3089         (progn
3090           (set-buffer buffer)
3091           (setq gnus-summary-buffer (current-buffer))
3092           (not gnus-newsgroup-prepared))
3093       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3094       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3095       (gnus-summary-mode group)
3096       (when gnus-carpal
3097         (gnus-carpal-setup-buffer 'summary))
3098       (unless gnus-single-article-buffer
3099         (make-local-variable 'gnus-article-buffer)
3100         (make-local-variable 'gnus-article-current)
3101         (make-local-variable 'gnus-original-article-buffer))
3102       (setq gnus-newsgroup-name group)
3103       ;; Set any local variables in the group parameters.
3104       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3105       t)))
3106
3107 (defun gnus-set-global-variables ()
3108   "Set the global equivalents of the buffer-local variables.
3109 They are set to the latest values they had.  These reflect the summary
3110 buffer that was in action when the last article was fetched."
3111   (when (eq major-mode 'gnus-summary-mode)
3112     (setq gnus-summary-buffer (current-buffer))
3113     (let ((name gnus-newsgroup-name)
3114           (marked gnus-newsgroup-marked)
3115           (spam gnus-newsgroup-spam-marked)
3116           (unread gnus-newsgroup-unreads)
3117           (headers gnus-current-headers)
3118           (data gnus-newsgroup-data)
3119           (summary gnus-summary-buffer)
3120           (article-buffer gnus-article-buffer)
3121           (original gnus-original-article-buffer)
3122           (gac gnus-article-current)
3123           (reffed gnus-reffed-article-number)
3124           (score-file gnus-current-score-file)
3125           (default-charset gnus-newsgroup-charset)
3126           vlist)
3127       (let ((locals gnus-newsgroup-variables))
3128         (while locals
3129           (if (consp (car locals))
3130               (push (eval (caar locals)) vlist)
3131             (push (eval (car locals)) vlist))
3132           (setq locals (cdr locals)))
3133         (setq vlist (nreverse vlist)))
3134       (save-excursion
3135         (set-buffer gnus-group-buffer)
3136         (setq gnus-newsgroup-name name
3137               gnus-newsgroup-marked marked
3138               gnus-newsgroup-spam-marked spam
3139               gnus-newsgroup-unreads unread
3140               gnus-current-headers headers
3141               gnus-newsgroup-data data
3142               gnus-article-current gac
3143               gnus-summary-buffer summary
3144               gnus-article-buffer article-buffer
3145               gnus-original-article-buffer original
3146               gnus-reffed-article-number reffed
3147               gnus-current-score-file score-file
3148               gnus-newsgroup-charset default-charset)
3149         (let ((locals gnus-newsgroup-variables))
3150           (while locals
3151             (if (consp (car locals))
3152                 (set (caar locals) (pop vlist))
3153               (set (car locals) (pop vlist)))
3154             (setq locals (cdr locals))))
3155         ;; The article buffer also has local variables.
3156         (when (gnus-buffer-live-p gnus-article-buffer)
3157           (set-buffer gnus-article-buffer)
3158           (setq gnus-summary-buffer summary))))))
3159
3160 (defun gnus-summary-article-unread-p (article)
3161   "Say whether ARTICLE is unread or not."
3162   (memq article gnus-newsgroup-unreads))
3163
3164 (defun gnus-summary-first-article-p (&optional article)
3165   "Return whether ARTICLE is the first article in the buffer."
3166   (if (not (setq article (or article (gnus-summary-article-number))))
3167       nil
3168     (eq article (caar gnus-newsgroup-data))))
3169
3170 (defun gnus-summary-last-article-p (&optional article)
3171   "Return whether ARTICLE is the last article in the buffer."
3172   (if (not (setq article (or article (gnus-summary-article-number))))
3173       ;; All non-existent numbers are the last article.  :-)
3174       t
3175     (not (cdr (gnus-data-find-list article)))))
3176
3177 (defun gnus-make-thread-indent-array ()
3178   (let ((n 200))
3179     (unless (and gnus-thread-indent-array
3180                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3181       (setq gnus-thread-indent-array (make-vector 201 "")
3182             gnus-thread-indent-array-level gnus-thread-indent-level)
3183       (while (>= n 0)
3184         (aset gnus-thread-indent-array n
3185               (make-string (* n gnus-thread-indent-level) ? ))
3186         (setq n (1- n))))))
3187
3188 (defun gnus-update-summary-mark-positions ()
3189   "Compute where the summary marks are to go."
3190   (save-excursion
3191     (when (gnus-buffer-exists-p gnus-summary-buffer)
3192       (set-buffer gnus-summary-buffer))
3193     (let ((gnus-replied-mark 129)
3194           (gnus-score-below-mark 130)
3195           (gnus-score-over-mark 130)
3196           (gnus-undownloaded-mark 131)
3197           (spec gnus-summary-line-format-spec)
3198           gnus-visual pos)
3199       (save-excursion
3200         (gnus-set-work-buffer)
3201         (let ((gnus-summary-line-format-spec spec)
3202               (gnus-newsgroup-downloadable '(0)))
3203           (gnus-summary-insert-line
3204            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3205            0 nil t 128 t nil "" nil 1)
3206           (goto-char (point-min))
3207           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3208                                              (- (point) (point-min) 1)))))
3209           (goto-char (point-min))
3210           (push (cons 'replied (and (search-forward "\201" nil t)
3211                                     (- (point) (point-min) 1)))
3212                 pos)
3213           (goto-char (point-min))
3214           (push (cons 'score (and (search-forward "\202" nil t)
3215                                   (- (point) (point-min) 1)))
3216                 pos)
3217           (goto-char (point-min))
3218           (push (cons 'download
3219                       (and (search-forward "\203" nil t)
3220                            (- (point) (point-min) 1)))
3221                 pos)))
3222       (setq gnus-summary-mark-positions pos))))
3223
3224 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3225   "Insert a dummy root in the summary buffer."
3226   (beginning-of-line)
3227   (gnus-add-text-properties
3228    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3229    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3230
3231 (defun gnus-summary-extract-address-component (from)
3232   (or (car (funcall gnus-extract-address-components from))
3233       from))
3234
3235 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3236   (let ((mail-parse-charset gnus-newsgroup-charset)
3237         ; Is it really necessary to do this next part for each summary line?
3238         ; Luckily, doesn't seem to slow things down much.
3239         (mail-parse-ignored-charsets
3240          (save-excursion (set-buffer gnus-summary-buffer)
3241                          gnus-newsgroup-ignored-charsets)))
3242     (or
3243      (and gnus-ignored-from-addresses
3244           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3245           (let ((extra-headers (mail-header-extra header))
3246                 to
3247                 newsgroups)
3248             (cond
3249              ((setq to (cdr (assq 'To extra-headers)))
3250               (concat "-> "
3251                       (inline
3252                         (gnus-summary-extract-address-component
3253                          (funcall gnus-decode-encoded-word-function to)))))
3254              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3255               (concat "=> " newsgroups)))))
3256      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3257
3258 (defun gnus-summary-insert-line (gnus-tmp-header
3259                                  gnus-tmp-level gnus-tmp-current
3260                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3261                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3262                                  &optional gnus-tmp-dummy gnus-tmp-score
3263                                  gnus-tmp-process)
3264   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3265          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3266          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3267          (gnus-tmp-score-char
3268           (if (or (null gnus-summary-default-score)
3269                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3270                       gnus-summary-zcore-fuzz))
3271               ?                         ;Whitespace
3272             (if (< gnus-tmp-score gnus-summary-default-score)
3273                 gnus-score-below-mark gnus-score-over-mark)))
3274          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3275          (gnus-tmp-replied
3276           (cond (gnus-tmp-process gnus-process-mark)
3277                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3278                  gnus-cached-mark)
3279                 (gnus-tmp-replied gnus-replied-mark)
3280                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3281                  gnus-forwarded-mark)
3282                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3283                  gnus-saved-mark)
3284                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3285                  gnus-recent-mark)
3286                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3287                  gnus-unseen-mark)
3288                 (t gnus-no-mark)))
3289          (gnus-tmp-downloaded
3290           (cond (undownloaded 
3291                  gnus-undownloaded-mark)
3292                 (gnus-newsgroup-agentized
3293                  gnus-downloaded-mark)
3294                 (t
3295                  gnus-no-mark)))
3296          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3297          (gnus-tmp-name
3298           (cond
3299            ((string-match "<[^>]+> *$" gnus-tmp-from)
3300             (let ((beg (match-beginning 0)))
3301               (or (and (string-match "^\".+\"" gnus-tmp-from)
3302                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3303                   (substring gnus-tmp-from 0 beg))))
3304            ((string-match "(.+)" gnus-tmp-from)
3305             (substring gnus-tmp-from
3306                        (1+ (match-beginning 0)) (1- (match-end 0))))
3307            (t gnus-tmp-from)))
3308          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3309          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3310          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3311          (buffer-read-only nil))
3312     (when (string= gnus-tmp-name "")
3313       (setq gnus-tmp-name gnus-tmp-from))
3314     (unless (numberp gnus-tmp-lines)
3315       (setq gnus-tmp-lines -1))
3316     (if (= gnus-tmp-lines -1)
3317         (setq gnus-tmp-lines "?")
3318       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3319       (gnus-put-text-property
3320      (point)
3321      (progn (eval gnus-summary-line-format-spec) (point))
3322        'gnus-number gnus-tmp-number)
3323     (when (gnus-visual-p 'summary-highlight 'highlight)
3324       (forward-line -1)
3325       (gnus-run-hooks 'gnus-summary-update-hook)
3326       (forward-line 1))))
3327
3328 (defun gnus-summary-update-line (&optional dont-update)
3329   "Update summary line after change."
3330   (when (and gnus-summary-default-score
3331              (not gnus-summary-inhibit-highlight))
3332     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3333            (article (gnus-summary-article-number))
3334            (score (gnus-summary-article-score article)))
3335       (unless dont-update
3336         (if (and gnus-summary-mark-below
3337                  (< (gnus-summary-article-score)
3338                     gnus-summary-mark-below))
3339             ;; This article has a low score, so we mark it as read.
3340             (when (memq article gnus-newsgroup-unreads)
3341               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3342           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3343             ;; This article was previously marked as read on account
3344             ;; of a low score, but now it has risen, so we mark it as
3345             ;; unread.
3346             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3347         (gnus-summary-update-mark
3348          (if (or (null gnus-summary-default-score)
3349                  (<= (abs (- score gnus-summary-default-score))
3350                      gnus-summary-zcore-fuzz))
3351              ?                          ;Whitespace
3352            (if (< score gnus-summary-default-score)
3353                gnus-score-below-mark gnus-score-over-mark))
3354          'score))
3355       ;; Do visual highlighting.
3356       (when (gnus-visual-p 'summary-highlight 'highlight)
3357         (gnus-run-hooks 'gnus-summary-update-hook)))))
3358
3359 (defvar gnus-tmp-new-adopts nil)
3360
3361 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3362   "Return the number of articles in THREAD.
3363 This may be 0 in some cases -- if none of the articles in
3364 the thread are to be displayed."
3365   (let* ((number
3366          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3367           (cond
3368            ((not (listp thread))
3369             1)
3370            ((and (consp thread) (cdr thread))
3371             (apply
3372              '+ 1 (mapcar
3373                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3374            ((null thread)
3375             1)
3376            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3377             1)
3378            (t 0))))
3379     (when (and level (zerop level) gnus-tmp-new-adopts)
3380       (incf number
3381             (apply '+ (mapcar
3382                        'gnus-summary-number-of-articles-in-thread
3383                        gnus-tmp-new-adopts))))
3384     (if char
3385         (if (> number 1) gnus-not-empty-thread-mark
3386           gnus-empty-thread-mark)
3387       number)))
3388
3389 (defsubst gnus-summary-line-message-size (head)
3390   "Return pretty-printed version of message size.
3391 This function is intended to be used in
3392 `gnus-summary-line-format-alist'."
3393   (let ((c (or (mail-header-chars head) -1)))
3394     (cond ((< c 0) "n/a")               ; chars not available
3395           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3396           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3397           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3398           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3399
3400
3401 (defun gnus-summary-set-local-parameters (group)
3402   "Go through the local params of GROUP and set all variable specs in that list."
3403   (let ((params (gnus-group-find-parameter group))
3404         (vars '(quit-config))           ; Ignore quit-config.
3405         elem)
3406     (while params
3407       (setq elem (car params)
3408             params (cdr params))
3409       (and (consp elem)                 ; Has to be a cons.
3410            (consp (cdr elem))           ; The cdr has to be a list.
3411            (symbolp (car elem))         ; Has to be a symbol in there.
3412            (not (memq (car elem) vars))
3413            (ignore-errors               ; So we set it.
3414              (push (car elem) vars)
3415              (make-local-variable (car elem))
3416              (set (car elem) (eval (nth 1 elem))))))))
3417
3418 (defun gnus-summary-read-group (group &optional show-all no-article
3419                                       kill-buffer no-display backward
3420                                       select-articles)
3421   "Start reading news in newsgroup GROUP.
3422 If SHOW-ALL is non-nil, already read articles are also listed.
3423 If NO-ARTICLE is non-nil, no article is selected initially.
3424 If NO-DISPLAY, don't generate a summary buffer."
3425   (let (result)
3426     (while (and group
3427                 (null (setq result
3428                             (let ((gnus-auto-select-next nil))
3429                               (or (gnus-summary-read-group-1
3430                                    group show-all no-article
3431                                    kill-buffer no-display
3432                                    select-articles)
3433                                   (setq show-all nil
3434                                         select-articles nil)))))
3435                 (eq gnus-auto-select-next 'quietly))
3436       (set-buffer gnus-group-buffer)
3437       ;; The entry function called above goes to the next
3438       ;; group automatically, so we go two groups back
3439       ;; if we are searching for the previous group.
3440       (when backward
3441         (gnus-group-prev-unread-group 2))
3442       (if (not (equal group (gnus-group-group-name)))
3443           (setq group (gnus-group-group-name))
3444         (setq group nil)))
3445     result))
3446
3447 (defun gnus-summary-read-group-1 (group show-all no-article
3448                                         kill-buffer no-display
3449                                         &optional select-articles)
3450   ;; Killed foreign groups can't be entered.
3451   ;;  (when (and (not (gnus-group-native-p group))
3452   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3453   ;;    (error "Dead non-native groups can't be entered"))
3454   (gnus-message 5 "Retrieving newsgroup: %s..."
3455                 (gnus-group-decoded-name group))
3456   (let* ((new-group (gnus-summary-setup-buffer group))
3457          (quit-config (gnus-group-quit-config group))
3458          (did-select (and new-group (gnus-select-newsgroup
3459                                      group show-all select-articles))))
3460     (cond
3461      ;; This summary buffer exists already, so we just select it.
3462      ((not new-group)
3463       (gnus-set-global-variables)
3464       (when kill-buffer
3465         (gnus-kill-or-deaden-summary kill-buffer))
3466       (gnus-configure-windows 'summary 'force)
3467       (gnus-set-mode-line 'summary)
3468       (gnus-summary-position-point)
3469       (message "")
3470       t)
3471      ;; We couldn't select this group.
3472      ((null did-select)
3473       (when (and (eq major-mode 'gnus-summary-mode)
3474                  (not (equal (current-buffer) kill-buffer)))
3475         (kill-buffer (current-buffer))
3476         (if (not quit-config)
3477             (progn
3478               ;; Update the info -- marks might need to be removed,
3479               ;; for instance.
3480               (gnus-summary-update-info)
3481               (set-buffer gnus-group-buffer)
3482               (gnus-group-jump-to-group group)
3483               (gnus-group-next-unread-group 1))
3484           (gnus-handle-ephemeral-exit quit-config)))
3485       (let ((grpinfo (gnus-get-info group)))
3486         (if (null (gnus-info-read grpinfo))
3487             (gnus-message 3 "Group %s contains no messages"
3488                           (gnus-group-decoded-name group))
3489           (gnus-message 3 "Can't select group")))
3490       nil)
3491      ;; The user did a `C-g' while prompting for number of articles,
3492      ;; so we exit this group.
3493      ((eq did-select 'quit)
3494       (and (eq major-mode 'gnus-summary-mode)
3495            (not (equal (current-buffer) kill-buffer))
3496            (kill-buffer (current-buffer)))
3497       (when kill-buffer
3498         (gnus-kill-or-deaden-summary kill-buffer))
3499       (if (not quit-config)
3500           (progn
3501             (set-buffer gnus-group-buffer)
3502             (gnus-group-jump-to-group group)
3503             (gnus-group-next-unread-group 1)
3504             (gnus-configure-windows 'group 'force))
3505         (gnus-handle-ephemeral-exit quit-config))
3506       ;; Finally signal the quit.
3507       (signal 'quit nil))
3508      ;; The group was successfully selected.
3509      (t
3510       (gnus-set-global-variables)
3511       ;; Save the active value in effect when the group was entered.
3512       (setq gnus-newsgroup-active
3513             (gnus-copy-sequence
3514              (gnus-active gnus-newsgroup-name)))
3515       ;; You can change the summary buffer in some way with this hook.
3516       (gnus-run-hooks 'gnus-select-group-hook)
3517       (gnus-update-format-specifications
3518        nil 'summary 'summary-mode 'summary-dummy)
3519       (gnus-update-summary-mark-positions)
3520       ;; Do score processing.
3521       (when gnus-use-scoring
3522         (gnus-possibly-score-headers))
3523       ;; Check whether to fill in the gaps in the threads.
3524       (when gnus-build-sparse-threads
3525         (gnus-build-sparse-threads))
3526       ;; Find the initial limit.
3527       (if gnus-show-threads
3528           (if show-all
3529               (let ((gnus-newsgroup-dormant nil))
3530                 (gnus-summary-initial-limit show-all))
3531             (gnus-summary-initial-limit show-all))
3532         ;; When unthreaded, all articles are always shown.
3533         (setq gnus-newsgroup-limit
3534               (mapcar
3535                (lambda (header) (mail-header-number header))
3536                gnus-newsgroup-headers)))
3537       ;; Generate the summary buffer.
3538       (unless no-display
3539         (gnus-summary-prepare))
3540       (when gnus-use-trees
3541         (gnus-tree-open group)
3542         (setq gnus-summary-highlight-line-function
3543               'gnus-tree-highlight-article))
3544       ;; If the summary buffer is empty, but there are some low-scored
3545       ;; articles or some excluded dormants, we include these in the
3546       ;; buffer.
3547       (when (and (zerop (buffer-size))
3548                  (not no-display))
3549         (cond (gnus-newsgroup-dormant
3550                (gnus-summary-limit-include-dormant))
3551               ((and gnus-newsgroup-scored show-all)
3552                (gnus-summary-limit-include-expunged t))))
3553       ;; Function `gnus-apply-kill-file' must be called in this hook.
3554       (gnus-run-hooks 'gnus-apply-kill-hook)
3555       (if (and (zerop (buffer-size))
3556                (not no-display))
3557           (progn
3558             ;; This newsgroup is empty.
3559             (gnus-summary-catchup-and-exit nil t)
3560             (gnus-message 6 "No unread news")
3561             (when kill-buffer
3562               (gnus-kill-or-deaden-summary kill-buffer))
3563             ;; Return nil from this function.
3564             nil)
3565         ;; Hide conversation thread subtrees.  We cannot do this in
3566         ;; gnus-summary-prepare-hook since kill processing may not
3567         ;; work with hidden articles.
3568         (gnus-summary-maybe-hide-threads)
3569         (when kill-buffer
3570           (gnus-kill-or-deaden-summary kill-buffer))
3571         (gnus-summary-auto-select-subject)
3572         ;; Show first unread article if requested.
3573         (if (and (not no-article)
3574                  (not no-display)
3575                  gnus-newsgroup-unreads
3576                  gnus-auto-select-first)
3577             (progn
3578               (gnus-configure-windows 'summary)
3579               (let ((art (gnus-summary-article-number)))
3580                 (unless (and (not gnus-plugged)
3581                              (or (memq art gnus-newsgroup-undownloaded)
3582                                  (memq art gnus-newsgroup-downloadable)))
3583                   (gnus-summary-goto-article art))))
3584           ;; Don't select any articles.
3585           (gnus-summary-position-point)
3586           (gnus-configure-windows 'summary 'force)
3587           (gnus-set-mode-line 'summary))
3588         (when (and gnus-auto-center-group
3589                    (get-buffer-window gnus-group-buffer t))
3590           ;; Gotta use windows, because recenter does weird stuff if
3591           ;; the current buffer ain't the displayed window.
3592           (let ((owin (selected-window)))
3593             (select-window (get-buffer-window gnus-group-buffer t))
3594             (when (gnus-group-goto-group group)
3595               (recenter))
3596             (select-window owin)))
3597         ;; Mark this buffer as "prepared".
3598         (setq gnus-newsgroup-prepared t)
3599         (gnus-run-hooks 'gnus-summary-prepared-hook)
3600         (unless (gnus-ephemeral-group-p group)
3601           (gnus-group-update-group group))
3602         t)))))
3603
3604 (defun gnus-summary-auto-select-subject ()
3605   "Select the subject line on initial group entry."
3606   (goto-char (point-min))
3607   (cond
3608    ((eq gnus-auto-select-subject 'best)
3609     (gnus-summary-best-unread-subject))
3610    ((eq gnus-auto-select-subject 'unread)
3611     (gnus-summary-first-unread-subject))
3612    ((eq gnus-auto-select-subject 'unseen)
3613     (gnus-summary-first-unseen-subject))
3614    ((eq gnus-auto-select-subject 'unseen-or-unread)
3615     (gnus-summary-first-unseen-or-unread-subject))
3616    ((eq gnus-auto-select-subject 'first)
3617     ;; Do nothing.
3618     )
3619    ((functionp gnus-auto-select-subject)
3620     (funcall gnus-auto-select-subject))))
3621
3622 (defun gnus-summary-prepare ()
3623   "Generate the summary buffer."
3624   (interactive)
3625   (let ((buffer-read-only nil))
3626     (erase-buffer)
3627     (setq gnus-newsgroup-data nil
3628           gnus-newsgroup-data-reverse nil)
3629     (gnus-run-hooks 'gnus-summary-generate-hook)
3630     ;; Generate the buffer, either with threads or without.
3631     (when gnus-newsgroup-headers
3632       (gnus-summary-prepare-threads
3633        (if gnus-show-threads
3634            (gnus-sort-gathered-threads
3635             (funcall gnus-summary-thread-gathering-function
3636                      (gnus-sort-threads
3637                       (gnus-cut-threads (gnus-make-threads)))))
3638          ;; Unthreaded display.
3639          (gnus-sort-articles gnus-newsgroup-headers))))
3640     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3641     ;; Call hooks for modifying summary buffer.
3642     (goto-char (point-min))
3643     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3644
3645 (defsubst gnus-general-simplify-subject (subject)
3646   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3647   (setq subject
3648         (cond
3649          ;; Truncate the subject.
3650          (gnus-simplify-subject-functions
3651           (gnus-map-function gnus-simplify-subject-functions subject))
3652          ((numberp gnus-summary-gather-subject-limit)
3653           (setq subject (gnus-simplify-subject-re subject))
3654           (if (> (length subject) gnus-summary-gather-subject-limit)
3655               (substring subject 0 gnus-summary-gather-subject-limit)
3656             subject))
3657          ;; Fuzzily simplify it.
3658          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3659           (gnus-simplify-subject-fuzzy subject))
3660          ;; Just remove the leading "Re:".
3661          (t
3662           (gnus-simplify-subject-re subject))))
3663
3664   (if (and gnus-summary-gather-exclude-subject
3665            (string-match gnus-summary-gather-exclude-subject subject))
3666       nil                         ; This article shouldn't be gathered
3667     subject))
3668
3669 (defun gnus-summary-simplify-subject-query ()
3670   "Query where the respool algorithm would put this article."
3671   (interactive)
3672   (gnus-summary-select-article)
3673   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3674
3675 (defun gnus-gather-threads-by-subject (threads)
3676   "Gather threads by looking at Subject headers."
3677   (if (not gnus-summary-make-false-root)
3678       threads
3679     (let ((hashtb (gnus-make-hashtable 1024))
3680           (prev threads)
3681           (result threads)
3682           subject hthread whole-subject)
3683       (while threads
3684         (setq subject (gnus-general-simplify-subject
3685                        (setq whole-subject (mail-header-subject
3686                                             (caar threads)))))
3687         (when subject
3688           (if (setq hthread (gnus-gethash subject hashtb))
3689               (progn
3690                 ;; We enter a dummy root into the thread, if we
3691                 ;; haven't done that already.
3692                 (unless (stringp (caar hthread))
3693                   (setcar hthread (list whole-subject (car hthread))))
3694                 ;; We add this new gathered thread to this gathered
3695                 ;; thread.
3696                 (setcdr (car hthread)
3697                         (nconc (cdar hthread) (list (car threads))))
3698                 ;; Remove it from the list of threads.
3699                 (setcdr prev (cdr threads))
3700                 (setq threads prev))
3701             ;; Enter this thread into the hash table.
3702             (gnus-sethash subject
3703                           (if gnus-summary-make-false-root-always
3704                               (progn
3705                                 ;; If you want a dummy root above all
3706                                 ;; threads...
3707                                 (setcar threads (list whole-subject
3708                                                       (car threads)))
3709                                 threads)
3710                             threads)
3711                           hashtb)))
3712         (setq prev threads)
3713         (setq threads (cdr threads)))
3714       result)))
3715
3716 (defun gnus-gather-threads-by-references (threads)
3717   "Gather threads by looking at References headers."
3718   (let ((idhashtb (gnus-make-hashtable 1024))
3719         (thhashtb (gnus-make-hashtable 1024))
3720         (prev threads)
3721         (result threads)
3722         ids references id gthread gid entered ref)
3723     (while threads
3724       (when (setq references (mail-header-references (caar threads)))
3725         (setq id (mail-header-id (caar threads))
3726               ids (inline (gnus-split-references references))
3727               entered nil)
3728         (while (setq ref (pop ids))
3729           (setq ids (delete ref ids))
3730           (if (not (setq gid (gnus-gethash ref idhashtb)))
3731               (progn
3732                 (gnus-sethash ref id idhashtb)
3733                 (gnus-sethash id threads thhashtb))
3734             (setq gthread (gnus-gethash gid thhashtb))
3735             (unless entered
3736               ;; We enter a dummy root into the thread, if we
3737               ;; haven't done that already.
3738               (unless (stringp (caar gthread))
3739                 (setcar gthread (list (mail-header-subject (caar gthread))
3740                                       (car gthread))))
3741               ;; We add this new gathered thread to this gathered
3742               ;; thread.
3743               (setcdr (car gthread)
3744                       (nconc (cdar gthread) (list (car threads)))))
3745             ;; Add it into the thread hash table.
3746             (gnus-sethash id gthread thhashtb)
3747             (setq entered t)
3748             ;; Remove it from the list of threads.
3749             (setcdr prev (cdr threads))
3750             (setq threads prev))))
3751       (setq prev threads)
3752       (setq threads (cdr threads)))
3753     result))
3754
3755 (defun gnus-sort-gathered-threads (threads)
3756   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3757   (let ((result threads))
3758     (while threads
3759       (when (stringp (caar threads))
3760         (setcdr (car threads)
3761                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3762       (setq threads (cdr threads)))
3763     result))
3764
3765 (defun gnus-thread-loop-p (root thread)
3766   "Say whether ROOT is in THREAD."
3767   (let ((stack (list thread))
3768         (infloop 0)
3769         th)
3770     (while (setq thread (pop stack))
3771       (setq th (cdr thread))
3772       (while (and th
3773                   (not (eq (caar th) root)))
3774         (pop th))
3775       (if th
3776           ;; We have found a loop.
3777           (let (ref-dep)
3778             (setcdr thread (delq (car th) (cdr thread)))
3779             (if (boundp (setq ref-dep (intern "none"
3780                                               gnus-newsgroup-dependencies)))
3781                 (setcdr (symbol-value ref-dep)
3782                         (nconc (cdr (symbol-value ref-dep))
3783                                (list (car th))))
3784               (set ref-dep (list nil (car th))))
3785             (setq infloop 1
3786                   stack nil))
3787         ;; Push all the subthreads onto the stack.
3788         (push (cdr thread) stack)))
3789     infloop))
3790
3791 (defun gnus-make-threads ()
3792   "Go through the dependency hashtb and find the roots.  Return all threads."
3793   (let (threads)
3794     (while (catch 'infloop
3795              (mapatoms
3796               (lambda (refs)
3797                 ;; Deal with self-referencing References loops.
3798                 (when (and (car (symbol-value refs))
3799                            (not (zerop
3800                                  (apply
3801                                   '+
3802                                   (mapcar
3803                                    (lambda (thread)
3804                                      (gnus-thread-loop-p
3805                                       (car (symbol-value refs)) thread))
3806                                    (cdr (symbol-value refs)))))))
3807                   (setq threads nil)
3808                   (throw 'infloop t))
3809                 (unless (car (symbol-value refs))
3810                   ;; These threads do not refer back to any other
3811                   ;; articles, so they're roots.
3812                   (setq threads (append (cdr (symbol-value refs)) threads))))
3813               gnus-newsgroup-dependencies)))
3814     threads))
3815
3816 ;; Build the thread tree.
3817 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3818   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3819
3820 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3821 if it was already present.
3822
3823 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3824 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3825 Message-IDs will be renamed to a unique Message-ID before being
3826 entered.
3827
3828 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3829   (let* ((id (mail-header-id header))
3830          (id-dep (and id (intern id dependencies)))
3831          parent-id ref ref-dep ref-header replaced)
3832     ;; Enter this `header' in the `dependencies' table.
3833     (cond
3834      ((not id-dep)
3835       (setq header nil))
3836      ;; The first two cases do the normal part: enter a new `header'
3837      ;; in the `dependencies' table.
3838      ((not (boundp id-dep))
3839       (set id-dep (list header)))
3840      ((null (car (symbol-value id-dep)))
3841       (setcar (symbol-value id-dep) header))
3842
3843      ;; From here the `header' was already present in the
3844      ;; `dependencies' table.
3845      (force-new
3846       ;; Overrides an existing entry;
3847       ;; just set the header part of the entry.
3848       (setcar (symbol-value id-dep) header)
3849       (setq replaced t))
3850
3851      ;; Renames the existing `header' to a unique Message-ID.
3852      ((not gnus-summary-ignore-duplicates)
3853       ;; An article with this Message-ID has already been seen.
3854       ;; We rename the Message-ID.
3855       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3856            (list header))
3857       (mail-header-set-id header id))
3858
3859      ;; The last case ignores an existing entry, except it adds any
3860      ;; additional Xrefs (in case the two articles came from different
3861      ;; servers.
3862      ;; Also sets `header' to `nil' meaning that the `dependencies'
3863      ;; table was *not* modified.
3864      (t
3865       (mail-header-set-xref
3866        (car (symbol-value id-dep))
3867        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3868                    "")
3869                (or (mail-header-xref header) "")))
3870       (setq header nil)))
3871
3872     (when (and header (not replaced))
3873       ;; First check that we are not creating a References loop.
3874       (setq parent-id (gnus-parent-id (mail-header-references header)))
3875       (setq ref parent-id)
3876       (while (and ref
3877                   (setq ref-dep (intern-soft ref dependencies))
3878                   (boundp ref-dep)
3879                   (setq ref-header (car (symbol-value ref-dep))))
3880         (if (string= id ref)
3881             ;; Yuk!  This is a reference loop.  Make the article be a
3882             ;; root article.
3883             (progn
3884               (mail-header-set-references (car (symbol-value id-dep)) "none")
3885               (setq ref nil)
3886               (setq parent-id nil))
3887           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3888       (setq ref-dep (intern (or parent-id "none") dependencies))
3889       (if (boundp ref-dep)
3890           (setcdr (symbol-value ref-dep)
3891                   (nconc (cdr (symbol-value ref-dep))
3892                          (list (symbol-value id-dep))))
3893         (set ref-dep (list nil (symbol-value id-dep)))))
3894     header))
3895
3896 (defun gnus-extract-message-id-from-in-reply-to (string)
3897   (if (string-match "<[^>]+>" string)
3898       (substring string (match-beginning 0) (match-end 0))
3899     nil))
3900
3901 (defun gnus-build-sparse-threads ()
3902   (let ((headers gnus-newsgroup-headers)
3903         (mail-parse-charset gnus-newsgroup-charset)
3904         (gnus-summary-ignore-duplicates t)
3905         header references generation relations
3906         subject child end new-child date)
3907     ;; First we create an alist of generations/relations, where
3908     ;; generations is how much we trust the relation, and the relation
3909     ;; is parent/child.
3910     (gnus-message 7 "Making sparse threads...")
3911     (save-excursion
3912       (nnheader-set-temp-buffer " *gnus sparse threads*")
3913       (while (setq header (pop headers))
3914         (when (and (setq references (mail-header-references header))
3915                    (not (string= references "")))
3916           (insert references)
3917           (setq child (mail-header-id header)
3918                 subject (mail-header-subject header)
3919                 date (mail-header-date header)
3920                 generation 0)
3921           (while (search-backward ">" nil t)
3922             (setq end (1+ (point)))
3923             (when (search-backward "<" nil t)
3924               (setq new-child (buffer-substring (point) end))
3925               (push (list (incf generation)
3926                           child (setq child new-child)
3927                           subject date)
3928                     relations)))
3929           (when child
3930             (push (list (1+ generation) child nil subject) relations))
3931           (erase-buffer)))
3932       (kill-buffer (current-buffer)))
3933     ;; Sort over trustworthiness.
3934     (mapcar
3935      (lambda (relation)
3936        (when (gnus-dependencies-add-header
3937               (make-full-mail-header
3938                gnus-reffed-article-number
3939                (nth 3 relation) "" (or (nth 4 relation) "")
3940                (nth 1 relation)
3941                (or (nth 2 relation) "") 0 0 "")
3942               gnus-newsgroup-dependencies nil)
3943          (push gnus-reffed-article-number gnus-newsgroup-limit)
3944          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3945          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3946                gnus-newsgroup-reads)
3947          (decf gnus-reffed-article-number)))
3948      (sort relations 'car-less-than-car))
3949     (gnus-message 7 "Making sparse threads...done")))
3950
3951 (defun gnus-build-old-threads ()
3952   ;; Look at all the articles that refer back to old articles, and
3953   ;; fetch the headers for the articles that aren't there.  This will
3954   ;; build complete threads - if the roots haven't been expired by the
3955   ;; server, that is.
3956   (let ((mail-parse-charset gnus-newsgroup-charset)
3957         id heads)
3958     (mapatoms
3959      (lambda (refs)
3960        (when (not (car (symbol-value refs)))
3961          (setq heads (cdr (symbol-value refs)))
3962          (while heads
3963            (if (memq (mail-header-number (caar heads))
3964                      gnus-newsgroup-dormant)
3965                (setq heads (cdr heads))
3966              (setq id (symbol-name refs))
3967              (while (and (setq id (gnus-build-get-header id))
3968                          (not (car (gnus-id-to-thread id)))))
3969              (setq heads nil)))))
3970      gnus-newsgroup-dependencies)))
3971
3972 ;; This function has to be called with point after the article number
3973 ;; on the beginning of the line.
3974 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3975   (let ((eol (gnus-point-at-eol))
3976         (buffer (current-buffer))
3977         header references in-reply-to)
3978
3979     ;; overview: [num subject from date id refs chars lines misc]
3980     (unwind-protect
3981         (let (x)
3982           (narrow-to-region (point) eol)
3983           (unless (eobp)
3984             (forward-char))
3985
3986           (setq header
3987                 (make-full-mail-header
3988                  number                 ; number
3989                  (condition-case ()     ; subject
3990                      (funcall gnus-decode-encoded-word-function
3991                               (setq x (nnheader-nov-field)))
3992                    (error x))
3993                  (condition-case ()     ; from
3994                      (funcall gnus-decode-encoded-word-function
3995                               (setq x (nnheader-nov-field)))
3996                    (error x))
3997                  (nnheader-nov-field)   ; date
3998                  (nnheader-nov-read-message-id) ; id
3999                  (setq references (nnheader-nov-field)) ; refs
4000                  (nnheader-nov-read-integer) ; chars
4001                  (nnheader-nov-read-integer) ; lines
4002                  (unless (eobp)
4003                    (if (looking-at "Xref: ")
4004                        (goto-char (match-end 0)))
4005                    (nnheader-nov-field)) ; Xref
4006                  (nnheader-nov-parse-extra)))) ; extra
4007
4008       (widen))
4009
4010     (when (and (string= references "")
4011                (setq in-reply-to (mail-header-extra header))
4012                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4013       (mail-header-set-references
4014        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4015
4016     (when gnus-alter-header-function
4017       (funcall gnus-alter-header-function header))
4018     (gnus-dependencies-add-header header dependencies force-new)))
4019
4020 (defun gnus-build-get-header (id)
4021   "Look through the buffer of NOV lines and find the header to ID.
4022 Enter this line into the dependencies hash table, and return
4023 the id of the parent article (if any)."
4024   (let ((deps gnus-newsgroup-dependencies)
4025         found header)
4026     (prog1
4027         (save-excursion
4028           (set-buffer nntp-server-buffer)
4029           (let ((case-fold-search nil))
4030             (goto-char (point-min))
4031             (while (and (not found)
4032                         (search-forward id nil t))
4033               (beginning-of-line)
4034               (setq found (looking-at
4035                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4036                                    (regexp-quote id))))
4037               (or found (beginning-of-line 2)))
4038             (when found
4039               (beginning-of-line)
4040               (and
4041                (setq header (gnus-nov-parse-line
4042                              (read (current-buffer)) deps))
4043                (gnus-parent-id (mail-header-references header))))))
4044       (when header
4045         (let ((number (mail-header-number header)))
4046           (push number gnus-newsgroup-limit)
4047           (push header gnus-newsgroup-headers)
4048           (if (memq number gnus-newsgroup-unselected)
4049               (progn
4050                 (setq gnus-newsgroup-unreads
4051                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4052                                                number))
4053                 (setq gnus-newsgroup-unselected
4054                       (delq number gnus-newsgroup-unselected)))
4055             (push number gnus-newsgroup-ancient)))))))
4056
4057 (defun gnus-build-all-threads ()
4058   "Read all the headers."
4059   (let ((gnus-summary-ignore-duplicates t)
4060         (mail-parse-charset gnus-newsgroup-charset)
4061         (dependencies gnus-newsgroup-dependencies)
4062         header article)
4063     (save-excursion
4064       (set-buffer nntp-server-buffer)
4065       (let ((case-fold-search nil))
4066         (goto-char (point-min))
4067         (while (not (eobp))
4068           (ignore-errors
4069             (setq article (read (current-buffer))
4070                   header (gnus-nov-parse-line article dependencies)))
4071           (when header
4072             (save-excursion
4073               (set-buffer gnus-summary-buffer)
4074               (push header gnus-newsgroup-headers)
4075               (if (memq (setq article (mail-header-number header))
4076                         gnus-newsgroup-unselected)
4077                   (progn
4078                     (setq gnus-newsgroup-unreads
4079                           (gnus-add-to-sorted-list
4080                            gnus-newsgroup-unreads article))
4081                     (setq gnus-newsgroup-unselected
4082                           (delq article gnus-newsgroup-unselected)))
4083                 (push article gnus-newsgroup-ancient)))
4084             (forward-line 1)))))))
4085
4086 (defun gnus-summary-update-article-line (article header)
4087   "Update the line for ARTICLE using HEADERS."
4088   (let* ((id (mail-header-id header))
4089          (thread (gnus-id-to-thread id)))
4090     (unless thread
4091       (error "Article in no thread"))
4092     ;; Update the thread.
4093     (setcar thread header)
4094     (gnus-summary-goto-subject article)
4095     (let* ((datal (gnus-data-find-list article))
4096            (data (car datal))
4097            (buffer-read-only nil)
4098            (level (gnus-summary-thread-level)))
4099       (gnus-delete-line)
4100       (let ((inserted (- (point)
4101                          (progn
4102                            (gnus-summary-insert-line
4103                             header level nil 
4104                             (memq article gnus-newsgroup-undownloaded)
4105                             (gnus-article-mark article)
4106                             (memq article gnus-newsgroup-replied)
4107                             (memq article gnus-newsgroup-expirable)
4108                             ;; Only insert the Subject string when it's different
4109                             ;; from the previous Subject string.
4110                             (if (and
4111                                  gnus-show-threads
4112                                  (gnus-subject-equal
4113                                   (condition-case ()
4114                                       (mail-header-subject
4115                                        (gnus-data-header
4116                                         (cadr
4117                                          (gnus-data-find-list
4118                                           article
4119                                           (gnus-data-list t)))))
4120                                     ;; Error on the side of excessive subjects.
4121                                     (error ""))
4122                                   (mail-header-subject header)))
4123                                 ""
4124                               (mail-header-subject header))
4125                             nil (cdr (assq article gnus-newsgroup-scored))
4126                             (memq article gnus-newsgroup-processable))
4127                            (point)))))
4128         (when (cdr datal)
4129           (gnus-data-update-list
4130            (cdr datal) 
4131            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4132
4133 (defun gnus-summary-update-article (article &optional iheader)
4134   "Update ARTICLE in the summary buffer."
4135   (set-buffer gnus-summary-buffer)
4136   (let* ((header (gnus-summary-article-header article))
4137          (id (mail-header-id header))
4138          (data (gnus-data-find article))
4139          (thread (gnus-id-to-thread id))
4140          (references (mail-header-references header))
4141          (parent
4142           (gnus-id-to-thread
4143            (or (gnus-parent-id
4144                 (when (and references
4145                            (not (equal "" references)))
4146                   references))
4147                "none")))
4148          (buffer-read-only nil)
4149          (old (car thread)))
4150     (when thread
4151       (unless iheader
4152         (setcar thread nil)
4153         (when parent
4154           (delq thread parent)))
4155       (if (gnus-summary-insert-subject id header)
4156           ;; Set the (possibly) new article number in the data structure.
4157           (gnus-data-set-number data (gnus-id-to-article id))
4158         (setcar thread old)
4159         nil))))
4160
4161 (defun gnus-rebuild-thread (id &optional line)
4162   "Rebuild the thread containing ID.
4163 If LINE, insert the rebuilt thread starting on line LINE."
4164   (let ((buffer-read-only nil)
4165         old-pos current thread data)
4166     (if (not gnus-show-threads)
4167         (setq thread (list (car (gnus-id-to-thread id))))
4168       ;; Get the thread this article is part of.
4169       (setq thread (gnus-remove-thread id)))
4170     (setq old-pos (gnus-point-at-bol))
4171     (setq current (save-excursion
4172                     (and (re-search-backward "[\r\n]" nil t)
4173                          (gnus-summary-article-number))))
4174     ;; If this is a gathered thread, we have to go some re-gathering.
4175     (when (stringp (car thread))
4176       (let ((subject (car thread))
4177             roots thr)
4178         (setq thread (cdr thread))
4179         (while thread
4180           (unless (memq (setq thr (gnus-id-to-thread
4181                                    (gnus-root-id
4182                                     (mail-header-id (caar thread)))))
4183                         roots)
4184             (push thr roots))
4185           (setq thread (cdr thread)))
4186         ;; We now have all (unique) roots.
4187         (if (= (length roots) 1)
4188             ;; All the loose roots are now one solid root.
4189             (setq thread (car roots))
4190           (setq thread (cons subject (gnus-sort-threads roots))))))
4191     (let (threads)
4192       ;; We then insert this thread into the summary buffer.
4193       (when line
4194         (goto-char (point-min))
4195         (forward-line (1- line)))
4196       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4197         (if gnus-show-threads
4198             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4199           (gnus-summary-prepare-unthreaded thread))
4200         (setq data (nreverse gnus-newsgroup-data))
4201         (setq threads gnus-newsgroup-threads))
4202       ;; We splice the new data into the data structure.
4203       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4204       ;;!!! then we want to insert at the beginning of the buffer.
4205       ;;!!! That happens to be true with Gnus now, but that may
4206       ;;!!! change in the future.  Perhaps.
4207       (gnus-data-enter-list
4208        (if line nil current) data (- (point) old-pos))
4209       (setq gnus-newsgroup-threads
4210             (nconc threads gnus-newsgroup-threads))
4211       (gnus-data-compute-positions))))
4212
4213 (defun gnus-number-to-header (number)
4214   "Return the header for article NUMBER."
4215   (let ((headers gnus-newsgroup-headers))
4216     (while (and headers
4217                 (not (= number (mail-header-number (car headers)))))
4218       (pop headers))
4219     (when headers
4220       (car headers))))
4221
4222 (defun gnus-parent-headers (in-headers &optional generation)
4223   "Return the headers of the GENERATIONeth parent of HEADERS."
4224   (unless generation
4225     (setq generation 1))
4226   (let ((parent t)
4227         (headers in-headers)
4228         references)
4229     (while (and parent
4230                 (not (zerop generation))
4231                 (setq references (mail-header-references headers)))
4232       (setq headers (if (and references
4233                              (setq parent (gnus-parent-id references)))
4234                         (car (gnus-id-to-thread parent))
4235                       nil))
4236       (decf generation))
4237     (and (not (eq headers in-headers))
4238          headers)))
4239
4240 (defun gnus-id-to-thread (id)
4241   "Return the (sub-)thread where ID appears."
4242   (gnus-gethash id gnus-newsgroup-dependencies))
4243
4244 (defun gnus-id-to-article (id)
4245   "Return the article number of ID."
4246   (let ((thread (gnus-id-to-thread id)))
4247     (when (and thread
4248                (car thread))
4249       (mail-header-number (car thread)))))
4250
4251 (defun gnus-id-to-header (id)
4252   "Return the article headers of ID."
4253   (car (gnus-id-to-thread id)))
4254
4255 (defun gnus-article-displayed-root-p (article)
4256   "Say whether ARTICLE is a root(ish) article."
4257   (let ((level (gnus-summary-thread-level article))
4258         (refs (mail-header-references  (gnus-summary-article-header article)))
4259         particle)
4260     (cond
4261      ((null level) nil)
4262      ((zerop level) t)
4263      ((null refs) t)
4264      ((null (gnus-parent-id refs)) t)
4265      ((and (= 1 level)
4266            (null (setq particle (gnus-id-to-article
4267                                  (gnus-parent-id refs))))
4268            (null (gnus-summary-thread-level particle)))))))
4269
4270 (defun gnus-root-id (id)
4271   "Return the id of the root of the thread where ID appears."
4272   (let (last-id prev)
4273     (while (and id (setq prev (car (gnus-id-to-thread id))))
4274       (setq last-id id
4275             id (gnus-parent-id (mail-header-references prev))))
4276     last-id))
4277
4278 (defun gnus-articles-in-thread (thread)
4279   "Return the list of articles in THREAD."
4280   (cons (mail-header-number (car thread))
4281         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4282
4283 (defun gnus-remove-thread (id &optional dont-remove)
4284   "Remove the thread that has ID in it."
4285   (let (headers thread last-id)
4286     ;; First go up in this thread until we find the root.
4287     (setq last-id (gnus-root-id id)
4288           headers (message-flatten-list (gnus-id-to-thread last-id)))
4289     ;; We have now found the real root of this thread.  It might have
4290     ;; been gathered into some loose thread, so we have to search
4291     ;; through the threads to find the thread we wanted.
4292     (let ((threads gnus-newsgroup-threads)
4293           sub)
4294       (while threads
4295         (setq sub (car threads))
4296         (if (stringp (car sub))
4297             ;; This is a gathered thread, so we look at the roots
4298             ;; below it to find whether this article is in this
4299             ;; gathered root.
4300             (progn
4301               (setq sub (cdr sub))
4302               (while sub
4303                 (when (member (caar sub) headers)
4304                   (setq thread (car threads)
4305                         threads nil
4306                         sub nil))
4307                 (setq sub (cdr sub))))
4308           ;; It's an ordinary thread, so we check it.
4309           (when (eq (car sub) (car headers))
4310             (setq thread sub
4311                   threads nil)))
4312         (setq threads (cdr threads)))
4313       ;; If this article is in no thread, then it's a root.
4314       (if thread
4315           (unless dont-remove
4316             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4317         (setq thread (gnus-id-to-thread last-id)))
4318       (when thread
4319         (prog1
4320             thread                      ; We return this thread.
4321           (unless dont-remove
4322             (if (stringp (car thread))
4323                 (progn
4324                   ;; If we use dummy roots, then we have to remove the
4325                   ;; dummy root as well.
4326                   (when (eq gnus-summary-make-false-root 'dummy)
4327                     ;; We go to the dummy root by going to
4328                     ;; the first sub-"thread", and then one line up.
4329                     (gnus-summary-goto-article
4330                      (mail-header-number (caadr thread)))
4331                     (forward-line -1)
4332                     (gnus-delete-line)
4333                     (gnus-data-compute-positions))
4334                   (setq thread (cdr thread))
4335                   (while thread
4336                     (gnus-remove-thread-1 (car thread))
4337                     (setq thread (cdr thread))))
4338               (gnus-remove-thread-1 thread))))))))
4339
4340 (defun gnus-remove-thread-1 (thread)
4341   "Remove the thread THREAD recursively."
4342   (let ((number (mail-header-number (pop thread)))
4343         d)
4344     (setq thread (reverse thread))
4345     (while thread
4346       (gnus-remove-thread-1 (pop thread)))
4347     (when (setq d (gnus-data-find number))
4348       (goto-char (gnus-data-pos d))
4349       (gnus-summary-show-thread)
4350       (gnus-data-remove
4351        number
4352        (- (gnus-point-at-bol)
4353           (prog1
4354               (1+ (gnus-point-at-eol))
4355             (gnus-delete-line)))))))
4356
4357 (defun gnus-sort-threads-1 (threads func)
4358   (sort (mapcar (lambda (thread)
4359                   (cons (car thread)
4360                         (and (cdr thread)
4361                              (gnus-sort-threads-1 (cdr thread) func))))
4362                 threads) func))
4363
4364 (defun gnus-sort-threads (threads)
4365   "Sort THREADS."
4366   (if (not gnus-thread-sort-functions)
4367       threads
4368     (gnus-message 8 "Sorting threads...")
4369     (let ((max-lisp-eval-depth 5000))
4370       (prog1 (gnus-sort-threads-1
4371          threads
4372          (gnus-make-sort-function gnus-thread-sort-functions))
4373         (gnus-message 8 "Sorting threads...done")))))
4374
4375 (defun gnus-sort-articles (articles)
4376   "Sort ARTICLES."
4377   (when gnus-article-sort-functions
4378     (gnus-message 7 "Sorting articles...")
4379     (prog1
4380         (setq gnus-newsgroup-headers
4381               (sort articles (gnus-make-sort-function
4382                               gnus-article-sort-functions)))
4383       (gnus-message 7 "Sorting articles...done"))))
4384
4385 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4386 (defmacro gnus-thread-header (thread)
4387   "Return header of first article in THREAD.
4388 Note that THREAD must never, ever be anything else than a variable -
4389 using some other form will lead to serious barfage."
4390   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4391   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4392   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4393         (vector thread) 2))
4394
4395 (defsubst gnus-article-sort-by-number (h1 h2)
4396   "Sort articles by article number."
4397   (< (mail-header-number h1)
4398      (mail-header-number h2)))
4399
4400 (defun gnus-thread-sort-by-number (h1 h2)
4401   "Sort threads by root article number."
4402   (gnus-article-sort-by-number
4403    (gnus-thread-header h1) (gnus-thread-header h2)))
4404
4405 (defsubst gnus-article-sort-by-random (h1 h2)
4406   "Sort articles by article number."
4407   (zerop (random 2)))
4408
4409 (defun gnus-thread-sort-by-random (h1 h2)
4410   "Sort threads by root article number."
4411   (gnus-article-sort-by-random
4412    (gnus-thread-header h1) (gnus-thread-header h2)))
4413
4414 (defsubst gnus-article-sort-by-lines (h1 h2)
4415   "Sort articles by article Lines header."
4416   (< (mail-header-lines h1)
4417      (mail-header-lines h2)))
4418
4419 (defun gnus-thread-sort-by-lines (h1 h2)
4420   "Sort threads by root article Lines header."
4421   (gnus-article-sort-by-lines
4422    (gnus-thread-header h1) (gnus-thread-header h2)))
4423
4424 (defsubst gnus-article-sort-by-chars (h1 h2)
4425   "Sort articles by octet length."
4426   (< (mail-header-chars h1)
4427      (mail-header-chars h2)))
4428
4429 (defun gnus-thread-sort-by-chars (h1 h2)
4430   "Sort threads by root article octet length."
4431   (gnus-article-sort-by-chars
4432    (gnus-thread-header h1) (gnus-thread-header h2)))
4433
4434 (defsubst gnus-article-sort-by-author (h1 h2)
4435   "Sort articles by root author."
4436   (string-lessp
4437    (let ((extract (funcall
4438                    gnus-extract-address-components
4439                    (mail-header-from h1))))
4440      (or (car extract) (cadr extract) ""))
4441    (let ((extract (funcall
4442                    gnus-extract-address-components
4443                    (mail-header-from h2))))
4444      (or (car extract) (cadr extract) ""))))
4445
4446 (defun gnus-thread-sort-by-author (h1 h2)
4447   "Sort threads by root author."
4448   (gnus-article-sort-by-author
4449    (gnus-thread-header h1)  (gnus-thread-header h2)))
4450
4451 (defsubst gnus-article-sort-by-subject (h1 h2)
4452   "Sort articles by root subject."
4453   (string-lessp
4454    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4455    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4456
4457 (defun gnus-thread-sort-by-subject (h1 h2)
4458   "Sort threads by root subject."
4459   (gnus-article-sort-by-subject
4460    (gnus-thread-header h1) (gnus-thread-header h2)))
4461
4462 (defsubst gnus-article-sort-by-date (h1 h2)
4463   "Sort articles by root article date."
4464   (time-less-p
4465    (gnus-date-get-time (mail-header-date h1))
4466    (gnus-date-get-time (mail-header-date h2))))
4467
4468 (defun gnus-thread-sort-by-date (h1 h2)
4469   "Sort threads by root article date."
4470   (gnus-article-sort-by-date
4471    (gnus-thread-header h1) (gnus-thread-header h2)))
4472
4473 (defsubst gnus-article-sort-by-score (h1 h2)
4474   "Sort articles by root article score.
4475 Unscored articles will be counted as having a score of zero."
4476   (> (or (cdr (assq (mail-header-number h1)
4477                     gnus-newsgroup-scored))
4478          gnus-summary-default-score 0)
4479      (or (cdr (assq (mail-header-number h2)
4480                     gnus-newsgroup-scored))
4481          gnus-summary-default-score 0)))
4482
4483 (defun gnus-thread-sort-by-score (h1 h2)
4484   "Sort threads by root article score."
4485   (gnus-article-sort-by-score
4486    (gnus-thread-header h1) (gnus-thread-header h2)))
4487
4488 (defun gnus-thread-sort-by-total-score (h1 h2)
4489   "Sort threads by the sum of all scores in the thread.
4490 Unscored articles will be counted as having a score of zero."
4491   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4492
4493 (defun gnus-thread-total-score (thread)
4494   ;; This function find the total score of THREAD.
4495   (cond
4496    ((null thread)
4497     0)
4498    ((consp thread)
4499     (if (stringp (car thread))
4500         (apply gnus-thread-score-function 0
4501                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4502       (gnus-thread-total-score-1 thread)))
4503    (t
4504     (gnus-thread-total-score-1 (list thread)))))
4505
4506 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4507   "Sort threads such that the thread with the most recently arrived article comes first."
4508   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4509
4510 (defun gnus-thread-highest-number (thread)
4511   "Return the highest article number in THREAD."
4512   (apply 'max (mapcar (lambda (header)
4513                         (mail-header-number header))
4514                       (message-flatten-list thread))))
4515
4516 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4517   "Sort threads such that the thread with the most recently dated article comes first."
4518   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4519
4520 (defun gnus-thread-latest-date (thread)
4521   "Return the highest article date in THREAD."
4522   (let ((previous-time 0))
4523     (apply 'max
4524            (mapcar
4525             (lambda (header)
4526               (setq previous-time
4527                     (time-to-seconds
4528                      (condition-case ()
4529                          (mail-header-parse-date (mail-header-date header))
4530                        (error previous-time)))))
4531             (sort
4532              (message-flatten-list thread)
4533              (lambda (h1 h2)
4534                (< (mail-header-number h1)
4535                   (mail-header-number h2))))))))
4536
4537 (defun gnus-thread-total-score-1 (root)
4538   ;; This function find the total score of the thread below ROOT.
4539   (setq root (car root))
4540   (apply gnus-thread-score-function
4541          (or (append
4542               (mapcar 'gnus-thread-total-score
4543                       (cdr (gnus-id-to-thread (mail-header-id root))))
4544               (when (> (mail-header-number root) 0)
4545                 (list (or (cdr (assq (mail-header-number root)
4546                                      gnus-newsgroup-scored))
4547                           gnus-summary-default-score 0))))
4548              (list gnus-summary-default-score)
4549              '(0))))
4550
4551 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4552 (defvar gnus-tmp-prev-subject nil)
4553 (defvar gnus-tmp-false-parent nil)
4554 (defvar gnus-tmp-root-expunged nil)
4555 (defvar gnus-tmp-dummy-line nil)
4556
4557 (eval-when-compile (defvar gnus-tmp-header))
4558 (defun gnus-extra-header (type &optional header)
4559   "Return the extra header of TYPE."
4560   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4561       ""))
4562
4563 (defvar gnus-tmp-thread-tree-header-string "")
4564
4565 (defcustom gnus-sum-thread-tree-root "> "
4566   "With %B spec, used for the root of a thread.
4567 If nil, use subject instead."
4568   :type 'string
4569   :group 'gnus-thread)
4570 (defcustom gnus-sum-thread-tree-false-root "> "
4571   "With %B spec, used for a false root of a thread.
4572 If nil, use subject instead."
4573   :type 'string
4574   :group 'gnus-thread)
4575 (defcustom gnus-sum-thread-tree-single-indent ""
4576   "With %B spec, used for a thread with just one message.
4577 If nil, use subject instead."
4578   :type 'string
4579   :group 'gnus-thread)
4580 (defcustom gnus-sum-thread-tree-vertical "| "
4581   "With %B spec, used for drawing a vertical line."
4582   :type 'string
4583   :group 'gnus-thread)
4584 (defcustom gnus-sum-thread-tree-indent "  "
4585   "With %B spec, used for indenting."
4586   :type 'string
4587   :group 'gnus-thread)
4588 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4589   "With %B spec, used for a leaf with brothers."
4590   :type 'string
4591   :group 'gnus-thread)
4592 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4593   "With %B spec, used for a leaf without brothers."
4594   :type 'string
4595   :group 'gnus-thread)
4596
4597 (defun gnus-summary-prepare-threads (threads)
4598   "Prepare summary buffer from THREADS and indentation LEVEL.
4599 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4600 or a straight list of headers."
4601   (gnus-message 7 "Generating summary...")
4602
4603   (setq gnus-newsgroup-threads threads)
4604   (beginning-of-line)
4605
4606   (let ((gnus-tmp-level 0)
4607         (default-score (or gnus-summary-default-score 0))
4608         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4609         (building-line-count gnus-summary-display-while-building)
4610         (building-count (integerp gnus-summary-display-while-building))
4611         thread number subject stack state gnus-tmp-gathered beg-match
4612         new-roots gnus-tmp-new-adopts thread-end simp-subject
4613         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4614         gnus-tmp-replied gnus-tmp-subject-or-nil
4615         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4616         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4617         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4618         tree-stack)
4619
4620     (setq gnus-tmp-prev-subject nil
4621           gnus-tmp-thread-tree-header-string "")
4622
4623     (if (vectorp (car threads))
4624         ;; If this is a straight (sic) list of headers, then a
4625         ;; threaded summary display isn't required, so we just create
4626         ;; an unthreaded one.
4627         (gnus-summary-prepare-unthreaded threads)
4628
4629       ;; Do the threaded display.
4630
4631       (if gnus-summary-display-while-building
4632           (switch-to-buffer (buffer-name)))
4633       (while (or threads stack gnus-tmp-new-adopts new-roots)
4634
4635         (if (and (= gnus-tmp-level 0)
4636                  (or (not stack)
4637                      (= (caar stack) 0))
4638                  (not gnus-tmp-false-parent)
4639                  (or gnus-tmp-new-adopts new-roots))
4640             (if gnus-tmp-new-adopts
4641                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4642                       thread (list (car gnus-tmp-new-adopts))
4643                       gnus-tmp-header (caar thread)
4644                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4645               (when new-roots
4646                 (setq thread (list (car new-roots))
4647                       gnus-tmp-header (caar thread)
4648                       new-roots (cdr new-roots))))
4649
4650           (if threads
4651               ;; If there are some threads, we do them before the
4652               ;; threads on the stack.
4653               (setq thread threads
4654                     gnus-tmp-header (caar thread))
4655             ;; There were no current threads, so we pop something off
4656             ;; the stack.
4657             (setq state (car stack)
4658                   gnus-tmp-level (car state)
4659                   tree-stack (cadr state)
4660                   thread (caddr state)
4661                   stack (cdr stack)
4662                   gnus-tmp-header (caar thread))))
4663
4664         (setq gnus-tmp-false-parent nil)
4665         (setq gnus-tmp-root-expunged nil)
4666         (setq thread-end nil)
4667
4668         (if (stringp gnus-tmp-header)
4669             ;; The header is a dummy root.
4670             (cond
4671              ((eq gnus-summary-make-false-root 'adopt)
4672               ;; We let the first article adopt the rest.
4673               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4674                                                (cddar thread)))
4675               (setq gnus-tmp-gathered
4676                     (nconc (mapcar
4677                             (lambda (h) (mail-header-number (car h)))
4678                             (cddar thread))
4679                            gnus-tmp-gathered))
4680               (setq thread (cons (list (caar thread)
4681                                        (cadar thread))
4682                                  (cdr thread)))
4683               (setq gnus-tmp-level -1
4684                     gnus-tmp-false-parent t))
4685              ((eq gnus-summary-make-false-root 'empty)
4686               ;; We print adopted articles with empty subject fields.
4687               (setq gnus-tmp-gathered
4688                     (nconc (mapcar
4689                             (lambda (h) (mail-header-number (car h)))
4690                             (cddar thread))
4691                            gnus-tmp-gathered))
4692               (setq gnus-tmp-level -1))
4693              ((eq gnus-summary-make-false-root 'dummy)
4694               ;; We remember that we probably want to output a dummy
4695               ;; root.
4696               (setq gnus-tmp-dummy-line gnus-tmp-header)
4697               (setq gnus-tmp-prev-subject gnus-tmp-header))
4698              (t
4699               ;; We do not make a root for the gathered
4700               ;; sub-threads at all.
4701               (setq gnus-tmp-level -1)))
4702
4703           (setq number (mail-header-number gnus-tmp-header)
4704                 subject (mail-header-subject gnus-tmp-header)
4705                 simp-subject (gnus-simplify-subject-fully subject))
4706
4707           (cond
4708            ;; If the thread has changed subject, we might want to make
4709            ;; this subthread into a root.
4710            ((and (null gnus-thread-ignore-subject)
4711                  (not (zerop gnus-tmp-level))
4712                  gnus-tmp-prev-subject
4713                  (not (string= gnus-tmp-prev-subject simp-subject)))
4714             (setq new-roots (nconc new-roots (list (car thread)))
4715                   thread-end t
4716                   gnus-tmp-header nil))
4717            ;; If the article lies outside the current limit,
4718            ;; then we do not display it.
4719            ((not (memq number gnus-newsgroup-limit))
4720             (setq gnus-tmp-gathered
4721                   (nconc (mapcar
4722                           (lambda (h) (mail-header-number (car h)))
4723                           (cdar thread))
4724                          gnus-tmp-gathered))
4725             (setq gnus-tmp-new-adopts (if (cdar thread)
4726                                           (append gnus-tmp-new-adopts
4727                                                   (cdar thread))
4728                                         gnus-tmp-new-adopts)
4729                   thread-end t
4730                   gnus-tmp-header nil)
4731             (when (zerop gnus-tmp-level)
4732               (setq gnus-tmp-root-expunged t)))
4733            ;; Perhaps this article is to be marked as read?
4734            ((and gnus-summary-mark-below
4735                  (< (or (cdr (assq number gnus-newsgroup-scored))
4736                         default-score)
4737                     gnus-summary-mark-below)
4738                  ;; Don't touch sparse articles.
4739                  (not (gnus-summary-article-sparse-p number))
4740                  (not (gnus-summary-article-ancient-p number)))
4741             (setq gnus-newsgroup-unreads
4742                   (delq number gnus-newsgroup-unreads))
4743             (if gnus-newsgroup-auto-expire
4744                 (setq gnus-newsgroup-expirable
4745                       (gnus-add-to-sorted-list
4746                        gnus-newsgroup-expirable number))
4747               (push (cons number gnus-low-score-mark)
4748                     gnus-newsgroup-reads))))
4749
4750           (when gnus-tmp-header
4751             ;; We may have an old dummy line to output before this
4752             ;; article.
4753             (when (and gnus-tmp-dummy-line
4754                        (gnus-subject-equal
4755                         gnus-tmp-dummy-line
4756                         (mail-header-subject gnus-tmp-header)))
4757               (gnus-summary-insert-dummy-line
4758                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4759               (setq gnus-tmp-dummy-line nil))
4760
4761             ;; Compute the mark.
4762             (setq gnus-tmp-unread (gnus-article-mark number))
4763
4764             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4765                                   gnus-tmp-header gnus-tmp-level)
4766                   gnus-newsgroup-data)
4767
4768             ;; Actually insert the line.
4769             (setq
4770              gnus-tmp-subject-or-nil
4771              (cond
4772               ((and gnus-thread-ignore-subject
4773                     gnus-tmp-prev-subject
4774                     (not (string= gnus-tmp-prev-subject simp-subject)))
4775                subject)
4776               ((zerop gnus-tmp-level)
4777                (if (and (eq gnus-summary-make-false-root 'empty)
4778                         (memq number gnus-tmp-gathered)
4779                         gnus-tmp-prev-subject
4780                         (string= gnus-tmp-prev-subject simp-subject))
4781                    gnus-summary-same-subject
4782                  subject))
4783               (t gnus-summary-same-subject)))
4784             (if (and (eq gnus-summary-make-false-root 'adopt)
4785                      (= gnus-tmp-level 1)
4786                      (memq number gnus-tmp-gathered))
4787                 (setq gnus-tmp-opening-bracket ?\<
4788                       gnus-tmp-closing-bracket ?\>)
4789               (setq gnus-tmp-opening-bracket ?\[
4790                     gnus-tmp-closing-bracket ?\]))
4791             (setq
4792              gnus-tmp-indentation
4793              (aref gnus-thread-indent-array gnus-tmp-level)
4794              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4795              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4796                                 gnus-summary-default-score 0)
4797              gnus-tmp-score-char
4798              (if (or (null gnus-summary-default-score)
4799                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4800                          gnus-summary-zcore-fuzz))
4801                  ?                      ;Whitespace
4802                (if (< gnus-tmp-score gnus-summary-default-score)
4803                    gnus-score-below-mark gnus-score-over-mark))
4804              gnus-tmp-replied
4805              (cond ((memq number gnus-newsgroup-processable)
4806                     gnus-process-mark)
4807                    ((memq number gnus-newsgroup-cached)
4808                     gnus-cached-mark)
4809                    ((memq number gnus-newsgroup-replied)
4810                     gnus-replied-mark)
4811                    ((memq number gnus-newsgroup-forwarded)
4812                     gnus-forwarded-mark)
4813                    ((memq number gnus-newsgroup-saved)
4814                     gnus-saved-mark)
4815                    ((memq number gnus-newsgroup-recent)
4816                     gnus-recent-mark)
4817                    ((memq number gnus-newsgroup-unseen)
4818                     gnus-unseen-mark)
4819                    (t gnus-no-mark))
4820              gnus-tmp-downloaded
4821              (cond ((memq number gnus-newsgroup-undownloaded) 
4822                     gnus-undownloaded-mark)
4823                    (gnus-newsgroup-agentized
4824                     gnus-downloaded-mark)
4825                    (t
4826                     gnus-no-mark))
4827              gnus-tmp-from (mail-header-from gnus-tmp-header)
4828              gnus-tmp-name
4829              (cond
4830               ((string-match "<[^>]+> *$" gnus-tmp-from)
4831                (setq beg-match (match-beginning 0))
4832                (or (and (string-match "^\".+\"" gnus-tmp-from)
4833                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4834                    (substring gnus-tmp-from 0 beg-match)))
4835               ((string-match "(.+)" gnus-tmp-from)
4836                (substring gnus-tmp-from
4837                           (1+ (match-beginning 0)) (1- (match-end 0))))
4838               (t gnus-tmp-from))
4839
4840              ;; Do the %B string
4841              gnus-tmp-thread-tree-header-string
4842              (cond
4843               ((not gnus-show-threads) "")
4844               ((zerop gnus-tmp-level)
4845                (cond ((cdar thread)
4846                       (or gnus-sum-thread-tree-root subject))
4847                      (gnus-tmp-new-adopts
4848                       (or gnus-sum-thread-tree-false-root subject))
4849                      (t
4850                       (or gnus-sum-thread-tree-single-indent subject))))
4851               (t
4852                (concat (apply 'concat
4853                               (mapcar (lambda (item)
4854                                         (if (= item 1)
4855                                             gnus-sum-thread-tree-vertical
4856                                           gnus-sum-thread-tree-indent))
4857                                       (cdr (reverse tree-stack))))
4858                        (if (nth 1 thread)
4859                            gnus-sum-thread-tree-leaf-with-other
4860                          gnus-sum-thread-tree-single-leaf)))))
4861             (when (string= gnus-tmp-name "")
4862               (setq gnus-tmp-name gnus-tmp-from))
4863             (unless (numberp gnus-tmp-lines)
4864               (setq gnus-tmp-lines -1))
4865             (if (= gnus-tmp-lines -1)
4866                 (setq gnus-tmp-lines "?")
4867               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4868               (gnus-put-text-property
4869              (point)
4870              (progn (eval gnus-summary-line-format-spec) (point))
4871                'gnus-number number)
4872             (when gnus-visual-p
4873               (forward-line -1)
4874               (gnus-run-hooks 'gnus-summary-update-hook)
4875               (forward-line 1))
4876
4877             (setq gnus-tmp-prev-subject simp-subject)))
4878
4879         (when (nth 1 thread)
4880           (push (list (max 0 gnus-tmp-level)
4881                       (copy-sequence tree-stack)
4882                       (nthcdr 1 thread))
4883                 stack))
4884         (push (if (nth 1 thread) 1 0) tree-stack)
4885         (incf gnus-tmp-level)
4886         (setq threads (if thread-end nil (cdar thread)))
4887         (if gnus-summary-display-while-building
4888             (if building-count
4889                 (progn
4890                   ;; use a set frequency
4891                   (setq building-line-count (1- building-line-count))
4892                   (when (= building-line-count 0)
4893                     (sit-for 0)
4894                     (setq building-line-count
4895                           gnus-summary-display-while-building)))
4896               ;; always
4897               (sit-for 0)))
4898         (unless threads
4899           (setq gnus-tmp-level 0)))))
4900   (gnus-message 7 "Generating summary...done"))
4901
4902 (defun gnus-summary-prepare-unthreaded (headers)
4903   "Generate an unthreaded summary buffer based on HEADERS."
4904   (let (header number mark)
4905
4906     (beginning-of-line)
4907
4908     (while headers
4909       ;; We may have to root out some bad articles...
4910       (when (memq (setq number (mail-header-number
4911                                 (setq header (pop headers))))
4912                   gnus-newsgroup-limit)
4913         ;; Mark article as read when it has a low score.
4914         (when (and gnus-summary-mark-below
4915                    (< (or (cdr (assq number gnus-newsgroup-scored))
4916                           gnus-summary-default-score 0)
4917                       gnus-summary-mark-below)
4918                    (not (gnus-summary-article-ancient-p number)))
4919           (setq gnus-newsgroup-unreads
4920                 (delq number gnus-newsgroup-unreads))
4921           (if gnus-newsgroup-auto-expire
4922               (push number gnus-newsgroup-expirable)
4923             (push (cons number gnus-low-score-mark)
4924                   gnus-newsgroup-reads)))
4925
4926         (setq mark (gnus-article-mark number))
4927         (push (gnus-data-make number mark (1+ (point)) header 0)
4928               gnus-newsgroup-data)
4929         (gnus-summary-insert-line
4930          header 0 number
4931          (memq number gnus-newsgroup-undownloaded)
4932          mark (memq number gnus-newsgroup-replied)
4933          (memq number gnus-newsgroup-expirable)
4934          (mail-header-subject header) nil
4935          (cdr (assq number gnus-newsgroup-scored))
4936          (memq number gnus-newsgroup-processable))))))
4937
4938 (defun gnus-summary-remove-list-identifiers ()
4939   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4940   (let ((regexp (if (consp gnus-list-identifiers)
4941                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4942                   gnus-list-identifiers))
4943         changed subject)
4944     (when regexp
4945       (dolist (header gnus-newsgroup-headers)
4946         (setq subject (mail-header-subject header)
4947               changed nil)
4948         (while (string-match
4949                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4950                 subject)
4951           (setq subject
4952                 (concat (substring subject 0 (match-beginning 2))
4953                         (substring subject (match-end 0)))
4954                 changed t))
4955         (when (and changed
4956                    (string-match
4957                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4958           (setq subject
4959                 (concat (substring subject 0 (match-beginning 1))
4960                         (substring subject (match-end 1)))))
4961         (when changed
4962           (mail-header-set-subject header subject))))))
4963
4964 (defun gnus-fetch-headers (articles)
4965   "Fetch headers of ARTICLES."
4966   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4967     (gnus-message 5 "Fetching headers for %s..." name)
4968     (prog1
4969         (if (eq 'nov
4970                 (setq gnus-headers-retrieved-by
4971                       (gnus-retrieve-headers
4972                        articles gnus-newsgroup-name
4973                        ;; We might want to fetch old headers, but
4974                        ;; not if there is only 1 article.
4975                        (and (or (and
4976                                  (not (eq gnus-fetch-old-headers 'some))
4977                                  (not (numberp gnus-fetch-old-headers)))
4978                                 (> (length articles) 1))
4979                             gnus-fetch-old-headers))))
4980             (gnus-get-newsgroup-headers-xover
4981              articles nil nil gnus-newsgroup-name t)
4982           (gnus-get-newsgroup-headers))
4983       (gnus-message 5 "Fetching headers for %s...done" name))))
4984
4985 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4986   "Select newsgroup GROUP.
4987 If READ-ALL is non-nil, all articles in the group are selected.
4988 If SELECT-ARTICLES, only select those articles from GROUP."
4989   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4990          ;;!!! Dirty hack; should be removed.
4991          (gnus-summary-ignore-duplicates
4992           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4993               t
4994             gnus-summary-ignore-duplicates))
4995          (info (nth 2 entry))
4996          articles fetched-articles cached)
4997
4998     (unless (gnus-check-server
4999              (set (make-local-variable 'gnus-current-select-method)
5000                   (gnus-find-method-for-group group)))
5001       (error "Couldn't open server"))
5002
5003     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5004         (gnus-activate-group group)     ; Or we can activate it...
5005         (progn                          ; Or we bug out.
5006           (when (equal major-mode 'gnus-summary-mode)
5007             (gnus-kill-buffer (current-buffer)))
5008           (error "Couldn't activate group %s: %s"
5009                  group (gnus-status-message group))))
5010
5011     (unless (gnus-request-group group t)
5012       (when (equal major-mode 'gnus-summary-mode)
5013         (gnus-kill-buffer (current-buffer)))
5014       (error "Couldn't request group %s: %s"
5015              group (gnus-status-message group)))
5016
5017     (when gnus-agent
5018       ;; The agent may be storing articles that are no longer in the
5019       ;; server's active range.  If that is the case, the active range
5020       ;; needs to be expanded such that the agent's articles can be
5021       ;; included in the summary.
5022       (let* ((gnus-command-method (gnus-find-method-for-group group))
5023              (alist (gnus-agent-load-alist group))
5024              (active (gnus-active group)))
5025         (if (and (car alist)
5026                  (< (caar alist) (car active)))
5027             (gnus-set-active group (cons (caar alist) (cdr active))))))
5028
5029     (setq gnus-newsgroup-name group
5030           gnus-newsgroup-unselected nil
5031           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5032
5033     (let ((display (gnus-group-find-parameter group 'display)))
5034       (setq gnus-newsgroup-display
5035             (cond
5036              ((not (zerop (or (car-safe read-all) 0)))
5037               ;; The user entered the group with C-u SPC/RET, let's show
5038               ;; all articles.
5039               'gnus-not-ignore)
5040              ((eq display 'all)
5041               'gnus-not-ignore)
5042              ((arrayp display)
5043               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5044              ((numberp display)
5045               ;; The following is probably the "correct" solution, but
5046               ;; it makes Gnus fetch all headers and then limit the
5047               ;; articles (which is slow), so instead we hack the
5048               ;; select-articles parameter instead. -- Simon Josefsson
5049               ;; <jas@kth.se>
5050               ;;
5051               ;; (gnus-byte-compile
5052               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5053               ;;                         display)))))
5054               (setq select-articles
5055                     (gnus-uncompress-range
5056                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5057                              (if (> tmp 0)
5058                                  tmp
5059                                1))
5060                            (cdr (gnus-active group)))))
5061               nil)
5062              (t
5063               nil))))
5064
5065     (gnus-summary-setup-default-charset)
5066
5067     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5068     (when (gnus-virtual-group-p group)
5069       (setq cached gnus-newsgroup-cached))
5070
5071     (setq gnus-newsgroup-unreads
5072           (gnus-sorted-ndifference
5073            (gnus-sorted-ndifference gnus-newsgroup-unreads
5074                                     gnus-newsgroup-marked)
5075            gnus-newsgroup-dormant))
5076
5077     (setq gnus-newsgroup-processable nil)
5078
5079     (gnus-update-read-articles group gnus-newsgroup-unreads)
5080
5081     ;; Adjust and set lists of article marks.
5082     (when info
5083       (gnus-adjust-marked-articles info))
5084     (if (setq articles select-articles)
5085         (setq gnus-newsgroup-unselected
5086               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5087       (setq articles (gnus-articles-to-read group read-all)))
5088
5089     (cond
5090      ((null articles)
5091       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5092       'quit)
5093      ((eq articles 0) nil)
5094      (t
5095       ;; Init the dependencies hash table.
5096       (setq gnus-newsgroup-dependencies
5097             (gnus-make-hashtable (length articles)))
5098       (gnus-set-global-variables)
5099       ;; Retrieve the headers and read them in.
5100
5101       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5102
5103       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5104       (when cached
5105         (setq gnus-newsgroup-cached cached))
5106
5107       ;; Suppress duplicates?
5108       (when gnus-suppress-duplicates
5109         (gnus-dup-suppress-articles))
5110
5111       ;; Set the initial limit.
5112       (setq gnus-newsgroup-limit (copy-sequence articles))
5113       ;; Remove canceled articles from the list of unread articles.
5114       (setq fetched-articles
5115             (mapcar (lambda (headers) (mail-header-number headers))
5116                     gnus-newsgroup-headers))
5117       (setq gnus-newsgroup-articles fetched-articles)
5118       (setq gnus-newsgroup-unreads
5119             (gnus-sorted-nintersection
5120              gnus-newsgroup-unreads fetched-articles))
5121       (gnus-compute-unseen-list)
5122
5123       ;; Removed marked articles that do not exist.
5124       (gnus-update-missing-marks
5125        (gnus-sorted-difference articles fetched-articles))
5126       ;; We might want to build some more threads first.
5127       (when (and gnus-fetch-old-headers
5128                  (eq gnus-headers-retrieved-by 'nov))
5129         (if (eq gnus-fetch-old-headers 'invisible)
5130             (gnus-build-all-threads)
5131           (gnus-build-old-threads)))
5132       ;; Let the Gnus agent mark articles as read.
5133       (when gnus-agent
5134         (gnus-agent-get-undownloaded-list))
5135       ;; Remove list identifiers from subject
5136       (when gnus-list-identifiers
5137         (gnus-summary-remove-list-identifiers))
5138       ;; Check whether auto-expire is to be done in this group.
5139       (setq gnus-newsgroup-auto-expire
5140             (gnus-group-auto-expirable-p group))
5141       ;; Set up the article buffer now, if necessary.
5142       (unless gnus-single-article-buffer
5143         (gnus-article-setup-buffer))
5144       ;; First and last article in this newsgroup.
5145       (when gnus-newsgroup-headers
5146         (setq gnus-newsgroup-begin
5147               (mail-header-number (car gnus-newsgroup-headers))
5148               gnus-newsgroup-end
5149               (mail-header-number
5150                (gnus-last-element gnus-newsgroup-headers))))
5151       ;; GROUP is successfully selected.
5152       (or gnus-newsgroup-headers t)))))
5153
5154 (defun gnus-compute-unseen-list ()
5155   ;; The `seen' marks are treated specially.
5156   (if (not gnus-newsgroup-seen)
5157       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5158     (setq gnus-newsgroup-unseen
5159           (gnus-inverse-list-range-intersection
5160            gnus-newsgroup-articles gnus-newsgroup-seen))))
5161
5162 (defun gnus-summary-display-make-predicate (display)
5163   (require 'gnus-agent)
5164   (when (= (length display) 1)
5165     (setq display (car display)))
5166   (unless gnus-summary-display-cache
5167     (dolist (elem (append '((unread . unread)
5168                             (read . read)
5169                             (unseen . unseen))
5170                           gnus-article-mark-lists))
5171       (push (cons (cdr elem)
5172                   (gnus-byte-compile
5173                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5174             gnus-summary-display-cache)))
5175   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5176         (gnus-category-predicate-cache gnus-summary-display-cache))
5177     (gnus-get-predicate display)))
5178
5179 ;; Uses the dynamically bound `number' variable.
5180 (eval-when-compile
5181   (defvar number))
5182 (defun gnus-article-marked-p (type &optional article)
5183   (let ((article (or article number)))
5184     (cond
5185      ((eq type 'tick)
5186       (memq article gnus-newsgroup-marked))
5187      ((eq type 'spam)
5188       (memq article gnus-newsgroup-spam-marked))
5189      ((eq type 'unsend)
5190       (memq article gnus-newsgroup-unsendable))
5191      ((eq type 'undownload)
5192       (memq article gnus-newsgroup-undownloaded))
5193      ((eq type 'download)
5194       (memq article gnus-newsgroup-downloadable))
5195      ((eq type 'unread)
5196       (memq article gnus-newsgroup-unreads))
5197      ((eq type 'read)
5198       (memq article gnus-newsgroup-reads))
5199      ((eq type 'dormant)
5200       (memq article gnus-newsgroup-dormant) )
5201      ((eq type 'expire)
5202       (memq article gnus-newsgroup-expirable))
5203      ((eq type 'reply)
5204       (memq article gnus-newsgroup-replied))
5205      ((eq type 'killed)
5206       (memq article gnus-newsgroup-killed))
5207      ((eq type 'bookmark)
5208       (assq article gnus-newsgroup-bookmarks))
5209      ((eq type 'score)
5210       (assq article gnus-newsgroup-scored))
5211      ((eq type 'save)
5212       (memq article gnus-newsgroup-saved))
5213      ((eq type 'cache)
5214       (memq article gnus-newsgroup-cached))
5215      ((eq type 'forward)
5216       (memq article gnus-newsgroup-forwarded))
5217      ((eq type 'seen)
5218       (not (memq article gnus-newsgroup-unseen)))
5219      ((eq type 'recent)
5220       (memq article gnus-newsgroup-recent))
5221      (t t))))
5222
5223 (defun gnus-articles-to-read (group &optional read-all)
5224   "Find out what articles the user wants to read."
5225   (let* ((display (gnus-group-find-parameter group 'display))
5226          (articles
5227           ;; Select all articles if `read-all' is non-nil, or if there
5228           ;; are no unread articles.
5229           (if (or read-all
5230                   (and (zerop (length gnus-newsgroup-marked))
5231                        (zerop (length gnus-newsgroup-unreads)))
5232                   ;; Fetch all if the predicate is non-nil.
5233                   gnus-newsgroup-display)
5234               ;; We want to select the headers for all the articles in
5235               ;; the group, so we select either all the active
5236               ;; articles in the group, or (if that's nil), the
5237               ;; articles in the cache.
5238               (or
5239                (gnus-uncompress-range (gnus-active group))
5240                (gnus-cache-articles-in-group group))
5241             ;; Select only the "normal" subset of articles.
5242             (gnus-sorted-nunion
5243              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5244              gnus-newsgroup-unreads)))
5245          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5246          (scored (length scored-list))
5247          (number (length articles))
5248          (marked (+ (length gnus-newsgroup-marked)
5249                     (length gnus-newsgroup-dormant)))
5250          (select
5251           (cond
5252            ((numberp read-all)
5253             read-all)
5254            ((numberp gnus-newsgroup-display)
5255             gnus-newsgroup-display)
5256            (t
5257             (condition-case ()
5258                 (cond
5259                  ((and (or (<= scored marked) (= scored number))
5260                        (numberp gnus-large-newsgroup)
5261                        (> number gnus-large-newsgroup))
5262                   (let* ((cursor-in-echo-area nil)
5263                          (initial (gnus-parameter-large-newsgroup-initial
5264                                    gnus-newsgroup-name))
5265                          (input
5266                           (read-string
5267                            (format
5268                             "How many articles from %s (%s %d): "
5269                             (gnus-limit-string
5270                              (gnus-group-decoded-name gnus-newsgroup-name)
5271                              35)
5272                             (if initial "max" "default")
5273                             number)
5274                            (if initial
5275                                (cons (number-to-string initial)
5276                                      0)))))
5277                     (if (string-match "^[ \t]*$" input) number input)))
5278                  ((and (> scored marked) (< scored number)
5279                        (> (- scored number) 20))
5280                   (let ((input
5281                          (read-string
5282                           (format "%s %s (%d scored, %d total): "
5283                                   "How many articles from"
5284                                   (gnus-group-decoded-name group)
5285                                   scored number))))
5286                     (if (string-match "^[ \t]*$" input)
5287                         number input)))
5288                  (t number))
5289               (quit
5290                (message "Quit getting the articles to read")
5291                nil))))))
5292     (setq select (if (stringp select) (string-to-number select) select))
5293     (if (or (null select) (zerop select))
5294         select
5295       (if (and (not (zerop scored)) (<= (abs select) scored))
5296           (progn
5297             (setq articles (sort scored-list '<))
5298             (setq number (length articles)))
5299         (setq articles (copy-sequence articles)))
5300
5301       (when (< (abs select) number)
5302         (if (< select 0)
5303             ;; Select the N oldest articles.
5304             (setcdr (nthcdr (1- (abs select)) articles) nil)
5305           ;; Select the N most recent articles.
5306           (setq articles (nthcdr (- number select) articles))))
5307       (setq gnus-newsgroup-unselected
5308             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5309       (when gnus-alter-articles-to-read-function
5310         (setq articles
5311               (sort
5312                (funcall gnus-alter-articles-to-read-function
5313                         gnus-newsgroup-name articles)
5314                '<)))
5315       articles)))
5316
5317 (defun gnus-killed-articles (killed articles)
5318   (let (out)
5319     (while articles
5320       (when (inline (gnus-member-of-range (car articles) killed))
5321         (push (car articles) out))
5322       (setq articles (cdr articles)))
5323     out))
5324
5325 (defun gnus-uncompress-marks (marks)
5326   "Uncompress the mark ranges in MARKS."
5327   (let ((uncompressed '(score bookmark))
5328         out)
5329     (while marks
5330       (if (memq (caar marks) uncompressed)
5331           (push (car marks) out)
5332         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5333       (setq marks (cdr marks)))
5334     out))
5335
5336 (defun gnus-article-mark-to-type (mark)
5337   "Return the type of MARK."
5338   (or (cadr (assq mark gnus-article-special-mark-lists))
5339       'list))
5340
5341 (defun gnus-article-unpropagatable-p (mark)
5342   "Return whether MARK should be propagated to backend."
5343   (memq mark gnus-article-unpropagated-mark-lists))
5344
5345 (defun gnus-adjust-marked-articles (info)
5346   "Set all article lists and remove all marks that are no longer valid."
5347   (let* ((marked-lists (gnus-info-marks info))
5348          (active (gnus-active (gnus-info-group info)))
5349          (min (car active))
5350          (max (cdr active))
5351          (types gnus-article-mark-lists)
5352          marks var articles article mark mark-type)
5353
5354     (dolist (marks marked-lists)
5355       (setq mark (car marks)
5356             mark-type (gnus-article-mark-to-type mark)
5357             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5358
5359       ;; We set the variable according to the type of the marks list,
5360       ;; and then adjust the marks to a subset of the active articles.
5361       (cond
5362        ;; Adjust "simple" lists.
5363        ((eq mark-type 'list)
5364         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5365         (when (memq mark '(tick dormant expire reply save))
5366           (while articles
5367             (when (or (< (setq article (pop articles)) min) (> article max))
5368               (set var (delq article (symbol-value var)))))))
5369        ;; Adjust assocs.
5370        ((eq mark-type 'tuple)
5371         (set var (setq articles (cdr marks)))
5372         (when (not (listp (cdr (symbol-value var))))
5373           (set var (list (symbol-value var))))
5374         (when (not (listp (cdr articles)))
5375           (setq articles (list articles)))
5376         (while articles
5377           (when (or (not (consp (setq article (pop articles))))
5378                     (< (car article) min)
5379                     (> (car article) max))
5380             (set var (delq article (symbol-value var))))))
5381        ;; Adjust ranges (sloppily).
5382        ((eq mark-type 'range)
5383         (cond
5384          ((eq mark 'seen)
5385           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5386           ;; It should be (seen (NUM1 . NUM2)).
5387           (when (numberp (cddr marks))
5388             (setcdr marks (list (cdr marks))))
5389           (setq articles (cdr marks))
5390           (while (and articles
5391                       (or (and (consp (car articles))
5392                                (> min (cdar articles)))
5393                           (and (numberp (car articles))
5394                                (> min (car articles)))))
5395             (pop articles))
5396           (set var articles))))))))
5397
5398 (defun gnus-update-missing-marks (missing)
5399   "Go through the list of MISSING articles and remove them from the mark lists."
5400   (when missing
5401     (let (var m)
5402       ;; Go through all types.
5403       (dolist (elem gnus-article-mark-lists)
5404         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5405           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5406           (when (symbol-value var)
5407             ;; This list has articles.  So we delete all missing
5408             ;; articles from it.
5409             (setq m missing)
5410             (while m
5411               (set var (delq (pop m) (symbol-value var))))))))))
5412
5413 (defun gnus-update-marks ()
5414   "Enter the various lists of marked articles into the newsgroup info list."
5415   (let ((types gnus-article-mark-lists)
5416         (info (gnus-get-info gnus-newsgroup-name))
5417         type list newmarked symbol delta-marks)
5418     (when info
5419       ;; Add all marks lists to the list of marks lists.
5420       (while (setq type (pop types))
5421         (setq list (symbol-value
5422                     (setq symbol
5423                           (intern (format "gnus-newsgroup-%s" (car type))))))
5424
5425         (when list
5426           ;; Get rid of the entries of the articles that have the
5427           ;; default score.
5428           (when (and (eq (cdr type) 'score)
5429                      gnus-save-score
5430                      list)
5431             (let* ((arts list)
5432                    (prev (cons nil list))
5433                    (all prev))
5434               (while arts
5435                 (if (or (not (consp (car arts)))
5436                         (= (cdar arts) gnus-summary-default-score))
5437                     (setcdr prev (cdr arts))
5438                   (setq prev arts))
5439                 (setq arts (cdr arts)))
5440               (setq list (cdr all)))))
5441
5442         (when (eq (cdr type) 'seen)
5443           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5444
5445         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5446           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5447
5448         (when (and (gnus-check-backend-function
5449                     'request-set-mark gnus-newsgroup-name)
5450                    (not (gnus-article-unpropagatable-p (cdr type))))
5451           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5452                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5453                  (add (gnus-remove-from-range
5454                        (gnus-copy-sequence list) old)))
5455             (when add
5456               (push (list add 'add (list (cdr type))) delta-marks))
5457             (when del
5458               (push (list del 'del (list (cdr type))) delta-marks))))
5459
5460         (when list
5461           (push (cons (cdr type) list) newmarked)))
5462
5463       (when delta-marks
5464         (unless (gnus-check-group gnus-newsgroup-name)
5465           (error "Can't open server for %s" gnus-newsgroup-name))
5466         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5467
5468       ;; Enter these new marks into the info of the group.
5469       (if (nthcdr 3 info)
5470           (setcar (nthcdr 3 info) newmarked)
5471         ;; Add the marks lists to the end of the info.
5472         (when newmarked
5473           (setcdr (nthcdr 2 info) (list newmarked))))
5474
5475       ;; Cut off the end of the info if there's nothing else there.
5476       (let ((i 5))
5477         (while (and (> i 2)
5478                     (not (nth i info)))
5479           (when (nthcdr (decf i) info)
5480             (setcdr (nthcdr i info) nil)))))))
5481
5482 (defun gnus-set-mode-line (where)
5483   "Set the mode line of the article or summary buffers.
5484 If WHERE is `summary', the summary mode line format will be used."
5485   ;; Is this mode line one we keep updated?
5486   (when (and (memq where gnus-updated-mode-lines)
5487              (symbol-value
5488               (intern (format "gnus-%s-mode-line-format-spec" where))))
5489     (let (mode-string)
5490       (save-excursion
5491         ;; We evaluate this in the summary buffer since these
5492         ;; variables are buffer-local to that buffer.
5493         (set-buffer gnus-summary-buffer)
5494        ;; We bind all these variables that are used in the `eval' form
5495         ;; below.
5496         (let* ((mformat (symbol-value
5497                          (intern
5498                           (format "gnus-%s-mode-line-format-spec" where))))
5499                (gnus-tmp-group-name (gnus-group-decoded-name
5500                                      gnus-newsgroup-name))
5501                (gnus-tmp-article-number (or gnus-current-article 0))
5502                (gnus-tmp-unread gnus-newsgroup-unreads)
5503                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5504                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5505                (gnus-tmp-unread-and-unselected
5506                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5507                             (zerop gnus-tmp-unselected))
5508                        "")
5509                       ((zerop gnus-tmp-unselected)
5510                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5511                       (t (format "{%d(+%d) more}"
5512                                  gnus-tmp-unread-and-unticked
5513                                  gnus-tmp-unselected))))
5514                (gnus-tmp-subject
5515                 (if (and gnus-current-headers
5516                          (vectorp gnus-current-headers))
5517                     (gnus-mode-string-quote
5518                      (mail-header-subject gnus-current-headers))
5519                   ""))
5520                bufname-length max-len
5521                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5522           (setq mode-string (eval mformat))
5523           (setq bufname-length (if (string-match "%b" mode-string)
5524                                    (- (length
5525                                        (buffer-name
5526                                         (if (eq where 'summary)
5527                                             nil
5528                                           (get-buffer gnus-article-buffer))))
5529                                       2)
5530                                  0))
5531           (setq max-len (max 4 (if gnus-mode-non-string-length
5532                                    (- (window-width)
5533                                       gnus-mode-non-string-length
5534                                       bufname-length)
5535                                  (length mode-string))))
5536           ;; We might have to chop a bit of the string off...
5537           (when (> (length mode-string) max-len)
5538             (setq mode-string
5539                   (concat (truncate-string-to-width mode-string (- max-len 3))
5540                           "...")))
5541           ;; Pad the mode string a bit.
5542           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5543       ;; Update the mode line.
5544       (setq mode-line-buffer-identification
5545             (gnus-mode-line-buffer-identification (list mode-string)))
5546       (set-buffer-modified-p t))))
5547
5548 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5549   "Go through the HEADERS list and add all Xrefs to a hash table.
5550 The resulting hash table is returned, or nil if no Xrefs were found."
5551   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5552          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5553          (xref-hashtb (gnus-make-hashtable))
5554          start group entry number xrefs header)
5555     (while headers
5556       (setq header (pop headers))
5557       (when (and (setq xrefs (mail-header-xref header))
5558                  (not (memq (setq number (mail-header-number header))
5559                             unreads)))
5560         (setq start 0)
5561         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5562           (setq start (match-end 0))
5563           (setq group (if prefix
5564                           (concat prefix (substring xrefs (match-beginning 1)
5565                                                     (match-end 1)))
5566                         (substring xrefs (match-beginning 1) (match-end 1))))
5567           (setq number
5568                 (string-to-int (substring xrefs (match-beginning 2)
5569                                           (match-end 2))))
5570           (if (setq entry (gnus-gethash group xref-hashtb))
5571               (setcdr entry (cons number (cdr entry)))
5572             (gnus-sethash group (cons number nil) xref-hashtb)))))
5573     (and start xref-hashtb)))
5574
5575 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5576   "Look through all the headers and mark the Xrefs as read."
5577   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5578         name entry info xref-hashtb idlist method nth4)
5579     (save-excursion
5580       (set-buffer gnus-group-buffer)
5581       (when (setq xref-hashtb
5582                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5583         (mapatoms
5584          (lambda (group)
5585            (unless (string= from-newsgroup (setq name (symbol-name group)))
5586              (setq idlist (symbol-value group))
5587              ;; Dead groups are not updated.
5588              (and (prog1
5589                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5590                             info (nth 2 entry))
5591                     (when (stringp (setq nth4 (gnus-info-method info)))
5592                       (setq nth4 (gnus-server-to-method nth4))))
5593                   ;; Only do the xrefs if the group has the same
5594                   ;; select method as the group we have just read.
5595                   (or (gnus-methods-equal-p
5596                        nth4 (gnus-find-method-for-group from-newsgroup))
5597                       virtual
5598                       (equal nth4 (setq method (gnus-find-method-for-group
5599                                                 from-newsgroup)))
5600                       (and (equal (car nth4) (car method))
5601                            (equal (nth 1 nth4) (nth 1 method))))
5602                   gnus-use-cross-reference
5603                   (or (not (eq gnus-use-cross-reference t))
5604                       virtual
5605                       ;; Only do cross-references on subscribed
5606                       ;; groups, if that is what is wanted.
5607                       (<= (gnus-info-level info) gnus-level-subscribed))
5608                   (gnus-group-make-articles-read name idlist))))
5609          xref-hashtb)))))
5610
5611 (defun gnus-compute-read-articles (group articles)
5612   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5613          (info (nth 2 entry))
5614          (active (gnus-active group))
5615          ninfo)
5616     (when entry
5617       ;; First peel off all invalid article numbers.
5618       (when active
5619         (let ((ids articles)
5620               id first)
5621           (while (setq id (pop ids))
5622             (when (and first (> id (cdr active)))
5623               ;; We'll end up in this situation in one particular
5624               ;; obscure situation.  If you re-scan a group and get
5625               ;; a new article that is cross-posted to a different
5626               ;; group that has not been re-scanned, you might get
5627               ;; crossposted article that has a higher number than
5628               ;; Gnus believes possible.  So we re-activate this
5629               ;; group as well.  This might mean doing the
5630               ;; crossposting thingy will *increase* the number
5631               ;; of articles in some groups.  Tsk, tsk.
5632               (setq active (or (gnus-activate-group group) active)))
5633             (when (or (> id (cdr active))
5634                       (< id (car active)))
5635               (setq articles (delq id articles))))))
5636       ;; If the read list is nil, we init it.
5637       (if (and active
5638                (null (gnus-info-read info))
5639                (> (car active) 1))
5640           (setq ninfo (cons 1 (1- (car active))))
5641         (setq ninfo (gnus-info-read info)))
5642       ;; Then we add the read articles to the range.
5643       (gnus-add-to-range
5644        ninfo (setq articles (sort articles '<))))))
5645
5646 (defun gnus-group-make-articles-read (group articles)
5647   "Update the info of GROUP to say that ARTICLES are read."
5648   (let* ((num 0)
5649          (entry (gnus-gethash group gnus-newsrc-hashtb))
5650          (info (nth 2 entry))
5651          (active (gnus-active group))
5652          range)
5653     (when entry
5654       (setq range (gnus-compute-read-articles group articles))
5655       (save-excursion
5656         (set-buffer gnus-group-buffer)
5657         (gnus-undo-register
5658           `(progn
5659              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5660              (gnus-info-set-read ',info ',(gnus-info-read info))
5661              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5662              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5663              (gnus-group-update-group ,group t))))
5664       ;; Add the read articles to the range.
5665       (gnus-info-set-read info range)
5666       (gnus-request-set-mark group (list (list range 'add '(read))))
5667       ;; Then we have to re-compute how many unread
5668       ;; articles there are in this group.
5669       (when active
5670         (cond
5671          ((not range)
5672           (setq num (- (1+ (cdr active)) (car active))))
5673          ((not (listp (cdr range)))
5674           (setq num (- (cdr active) (- (1+ (cdr range))
5675                                        (car range)))))
5676          (t
5677           (while range
5678             (if (numberp (car range))
5679                 (setq num (1+ num))
5680               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5681             (setq range (cdr range)))
5682           (setq num (- (cdr active) num))))
5683         ;; Update the number of unread articles.
5684         (setcar entry num)
5685         ;; Update the group buffer.
5686         (unless (gnus-ephemeral-group-p group)
5687           (gnus-group-update-group group t))))))
5688
5689 (defvar gnus-newsgroup-none-id 0)
5690
5691 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5692   (let ((cur nntp-server-buffer)
5693         (dependencies
5694          (or dependencies
5695              (save-excursion (set-buffer gnus-summary-buffer)
5696                              gnus-newsgroup-dependencies)))
5697         headers id end ref
5698         (mail-parse-charset gnus-newsgroup-charset)
5699         (mail-parse-ignored-charsets
5700          (save-excursion (condition-case nil
5701                              (set-buffer gnus-summary-buffer)
5702                            (error))
5703                          gnus-newsgroup-ignored-charsets)))
5704     (save-excursion
5705       (set-buffer nntp-server-buffer)
5706       ;; Translate all TAB characters into SPACE characters.
5707       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5708       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5709       (gnus-run-hooks 'gnus-parse-headers-hook)
5710       (let ((case-fold-search t)
5711             in-reply-to header p lines chars)
5712         (goto-char (point-min))
5713         ;; Search to the beginning of the next header.  Error messages
5714         ;; do not begin with 2 or 3.
5715         (while (re-search-forward "^[23][0-9]+ " nil t)
5716           (setq id nil
5717                 ref nil)
5718           ;; This implementation of this function, with nine
5719           ;; search-forwards instead of the one re-search-forward and
5720           ;; a case (which basically was the old function) is actually
5721           ;; about twice as fast, even though it looks messier.  You
5722           ;; can't have everything, I guess.  Speed and elegance
5723           ;; doesn't always go hand in hand.
5724           (setq
5725            header
5726            (vector
5727             ;; Number.
5728             (prog1
5729                 (read cur)
5730               (end-of-line)
5731               (setq p (point))
5732               (narrow-to-region (point)
5733                                 (or (and (search-forward "\n.\n" nil t)
5734                                          (- (point) 2))
5735                                     (point))))
5736             ;; Subject.
5737             (progn
5738               (goto-char p)
5739               (if (search-forward "\nsubject:" nil t)
5740                   (funcall gnus-decode-encoded-word-function
5741                            (nnheader-header-value))
5742                 "(none)"))
5743             ;; From.
5744             (progn
5745               (goto-char p)
5746               (if (search-forward "\nfrom:" nil t)
5747                   (funcall gnus-decode-encoded-word-function
5748                            (nnheader-header-value))
5749                 "(nobody)"))
5750             ;; Date.
5751             (progn
5752               (goto-char p)
5753               (if (search-forward "\ndate:" nil t)
5754                   (nnheader-header-value) ""))
5755             ;; Message-ID.
5756             (progn
5757               (goto-char p)
5758               (setq id (if (re-search-forward
5759                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5760                            ;; We do it this way to make sure the Message-ID
5761                            ;; is (somewhat) syntactically valid.
5762                            (buffer-substring (match-beginning 1)
5763                                              (match-end 1))
5764                          ;; If there was no message-id, we just fake one
5765                          ;; to make subsequent routines simpler.
5766                          (nnheader-generate-fake-message-id))))
5767             ;; References.
5768             (progn
5769               (goto-char p)
5770               (if (search-forward "\nreferences:" nil t)
5771                   (progn
5772                     (setq end (point))
5773                     (prog1
5774                         (nnheader-header-value)
5775                       (setq ref
5776                             (buffer-substring
5777                              (progn
5778                                (end-of-line)
5779                                (search-backward ">" end t)
5780                                (1+ (point)))
5781                              (progn
5782                                (search-backward "<" end t)
5783                                (point))))))
5784                 ;; Get the references from the in-reply-to header if there
5785                 ;; were no references and the in-reply-to header looks
5786                 ;; promising.
5787                 (if (and (search-forward "\nin-reply-to:" nil t)
5788                          (setq in-reply-to (nnheader-header-value))
5789                          (string-match "<[^>]+>" in-reply-to))
5790                     (let (ref2)
5791                       (setq ref (substring in-reply-to (match-beginning 0)
5792                                            (match-end 0)))
5793                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5794                         (setq ref2 (substring in-reply-to (match-beginning 0)
5795                                               (match-end 0)))
5796                         (when (> (length ref2) (length ref))
5797                           (setq ref ref2)))
5798                       ref)
5799                   (setq ref nil))))
5800             ;; Chars.
5801             (progn
5802               (goto-char p)
5803               (if (search-forward "\nchars: " nil t)
5804                   (if (numberp (setq chars (ignore-errors (read cur))))
5805                       chars -1)
5806                 -1))
5807             ;; Lines.
5808             (progn
5809               (goto-char p)
5810               (if (search-forward "\nlines: " nil t)
5811                   (if (numberp (setq lines (ignore-errors (read cur))))
5812                       lines -1)
5813                 -1))
5814             ;; Xref.
5815             (progn
5816               (goto-char p)
5817               (and (search-forward "\nxref:" nil t)
5818                    (nnheader-header-value)))
5819             ;; Extra.
5820             (when gnus-extra-headers
5821               (let ((extra gnus-extra-headers)
5822                     out)
5823                 (while extra
5824                   (goto-char p)
5825                   (when (search-forward
5826                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5827                     (push (cons (car extra) (nnheader-header-value))
5828                           out))
5829                   (pop extra))
5830                 out))))
5831           (when (equal id ref)
5832             (setq ref nil))
5833
5834           (when gnus-alter-header-function
5835             (funcall gnus-alter-header-function header)
5836             (setq id (mail-header-id header)
5837                   ref (gnus-parent-id (mail-header-references header))))
5838
5839           (when (setq header
5840                       (gnus-dependencies-add-header
5841                        header dependencies force-new))
5842             (push header headers))
5843           (goto-char (point-max))
5844           (widen))
5845         (nreverse headers)))))
5846
5847 ;; Goes through the xover lines and returns a list of vectors
5848 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5849                                                   force-new dependencies
5850                                                   group also-fetch-heads)
5851   "Parse the news overview data in the server buffer.
5852 Return a list of headers that match SEQUENCE (see
5853 `nntp-retrieve-headers')."
5854   ;; Get the Xref when the users reads the articles since most/some
5855   ;; NNTP servers do not include Xrefs when using XOVER.
5856   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5857   (let ((mail-parse-charset gnus-newsgroup-charset)
5858         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5859         (cur nntp-server-buffer)
5860         (dependencies (or dependencies gnus-newsgroup-dependencies))
5861         (allp (cond
5862                ((eq gnus-read-all-available-headers t)
5863                 t)
5864                ((stringp gnus-read-all-available-headers)
5865                 (string-match gnus-read-all-available-headers group))
5866                (t
5867                 nil)))
5868         number headers header)
5869     (save-excursion
5870       (set-buffer nntp-server-buffer)
5871       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5872       ;; Allow the user to mangle the headers before parsing them.
5873       (gnus-run-hooks 'gnus-parse-headers-hook)
5874       (goto-char (point-min))
5875       (gnus-parse-without-error
5876         (while (and (or sequence allp)
5877                     (not (eobp)))
5878           (setq number (read cur))
5879           (when (not allp)
5880             (while (and sequence
5881                         (< (car sequence) number))
5882               (setq sequence (cdr sequence))))
5883           (when (and (or allp
5884                          (and sequence
5885                               (eq number (car sequence))))
5886                      (progn
5887                        (setq sequence (cdr sequence))
5888                        (setq header (inline
5889                                       (gnus-nov-parse-line
5890                                        number dependencies force-new)))))
5891             (push header headers))
5892           (forward-line 1)))
5893       ;; A common bug in inn is that if you have posted an article and
5894       ;; then retrieves the active file, it will answer correctly --
5895       ;; the new article is included.  However, a NOV entry for the
5896       ;; article may not have been generated yet, so this may fail.
5897       ;; We work around this problem by retrieving the last few
5898       ;; headers using HEAD.
5899       (if (or (not also-fetch-heads)
5900               (not sequence))
5901           ;; We (probably) got all the headers.
5902           (nreverse headers)
5903         (let ((gnus-nov-is-evil t))
5904           (nconc
5905            (nreverse headers)
5906            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5907              (gnus-get-newsgroup-headers))))))))
5908
5909 (defun gnus-article-get-xrefs ()
5910   "Fill in the Xref value in `gnus-current-headers', if necessary.
5911 This is meant to be called in `gnus-article-internal-prepare-hook'."
5912   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5913                                  gnus-current-headers)))
5914     (or (not gnus-use-cross-reference)
5915         (not headers)
5916         (and (mail-header-xref headers)
5917              (not (string= (mail-header-xref headers) "")))
5918         (let ((case-fold-search t)
5919               xref)
5920           (save-restriction
5921             (nnheader-narrow-to-headers)
5922             (goto-char (point-min))
5923             (when (or (and (not (eobp))
5924                            (eq (downcase (char-after)) ?x)
5925                            (looking-at "Xref:"))
5926                       (search-forward "\nXref:" nil t))
5927               (goto-char (1+ (match-end 0)))
5928               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5929               (mail-header-set-xref headers xref)))))))
5930
5931 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5932   "Find article ID and insert the summary line for that article.
5933 OLD-HEADER can either be a header or a line number to insert
5934 the subject line on."
5935   (let* ((line (and (numberp old-header) old-header))
5936          (old-header (and (vectorp old-header) old-header))
5937          (header (cond ((and old-header use-old-header)
5938                         old-header)
5939                        ((and (numberp id)
5940                              (gnus-number-to-header id))
5941                         (gnus-number-to-header id))
5942                        (t
5943                         (gnus-read-header id))))
5944          (number (and (numberp id) id))
5945          d)
5946     (when header
5947       ;; Rebuild the thread that this article is part of and go to the
5948       ;; article we have fetched.
5949       (when (and (not gnus-show-threads)
5950                  old-header)
5951         (when (and number
5952                    (setq d (gnus-data-find (mail-header-number old-header))))
5953           (goto-char (gnus-data-pos d))
5954           (gnus-data-remove
5955            number
5956            (- (gnus-point-at-bol)
5957               (prog1
5958                   (1+ (gnus-point-at-eol))
5959                 (gnus-delete-line))))))
5960       (when old-header
5961         (mail-header-set-number header (mail-header-number old-header)))
5962       (setq gnus-newsgroup-sparse
5963             (delq (setq number (mail-header-number header))
5964                   gnus-newsgroup-sparse))
5965       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5966       (push number gnus-newsgroup-limit)
5967       (gnus-rebuild-thread (mail-header-id header) line)
5968       (gnus-summary-goto-subject number nil t))
5969     (when (and (numberp number)
5970                (> number 0))
5971       ;; We have to update the boundaries even if we can't fetch the
5972       ;; article if ID is a number -- so that the next `P' or `N'
5973       ;; command will fetch the previous (or next) article even
5974       ;; if the one we tried to fetch this time has been canceled.
5975       (when (> number gnus-newsgroup-end)
5976         (setq gnus-newsgroup-end number))
5977       (when (< number gnus-newsgroup-begin)
5978         (setq gnus-newsgroup-begin number))
5979       (setq gnus-newsgroup-unselected
5980             (delq number gnus-newsgroup-unselected)))
5981     ;; Report back a success?
5982     (and header (mail-header-number header))))
5983
5984 ;;; Process/prefix in the summary buffer
5985
5986 (defun gnus-summary-work-articles (n)
5987   "Return a list of articles to be worked upon.
5988 The prefix argument, the list of process marked articles, and the
5989 current article will be taken into consideration."
5990   (save-excursion
5991     (set-buffer gnus-summary-buffer)
5992     (cond
5993      (n
5994       ;; A numerical prefix has been given.
5995       (setq n (prefix-numeric-value n))
5996       (let ((backward (< n 0))
5997             (n (abs (prefix-numeric-value n)))
5998             articles article)
5999         (save-excursion
6000           (while
6001               (and (> n 0)
6002                    (push (setq article (gnus-summary-article-number))
6003                          articles)
6004                    (if backward
6005                        (gnus-summary-find-prev nil article)
6006                      (gnus-summary-find-next nil article)))
6007             (decf n)))
6008         (nreverse articles)))
6009      ((and (gnus-region-active-p) (mark))
6010       (message "region active")
6011       ;; Work on the region between point and mark.
6012       (let ((max (max (point) (mark)))
6013             articles article)
6014         (save-excursion
6015           (goto-char (min (min (point) (mark))))
6016           (while
6017               (and
6018                (push (setq article (gnus-summary-article-number)) articles)
6019                (gnus-summary-find-next nil article)
6020                (< (point) max)))
6021           (nreverse articles))))
6022      (gnus-newsgroup-processable
6023       ;; There are process-marked articles present.
6024       ;; Save current state.
6025       (gnus-summary-save-process-mark)
6026       ;; Return the list.
6027       (reverse gnus-newsgroup-processable))
6028      (t
6029       ;; Just return the current article.
6030       (list (gnus-summary-article-number))))))
6031
6032 (defmacro gnus-summary-iterate (arg &rest forms)
6033   "Iterate over the process/prefixed articles and do FORMS.
6034 ARG is the interactive prefix given to the command.  FORMS will be
6035 executed with point over the summary line of the articles."
6036   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6037     `(let ((,articles (gnus-summary-work-articles ,arg)))
6038        (while ,articles
6039          (gnus-summary-goto-subject (car ,articles))
6040          ,@forms
6041          (pop ,articles)))))
6042
6043 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6044 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6045
6046 (defun gnus-summary-save-process-mark ()
6047   "Push the current set of process marked articles on the stack."
6048   (interactive)
6049   (push (copy-sequence gnus-newsgroup-processable)
6050         gnus-newsgroup-process-stack))
6051
6052 (defun gnus-summary-kill-process-mark ()
6053   "Push the current set of process marked articles on the stack and unmark."
6054   (interactive)
6055   (gnus-summary-save-process-mark)
6056   (gnus-summary-unmark-all-processable))
6057
6058 (defun gnus-summary-yank-process-mark ()
6059   "Pop the last process mark state off the stack and restore it."
6060   (interactive)
6061   (unless gnus-newsgroup-process-stack
6062     (error "Empty mark stack"))
6063   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6064
6065 (defun gnus-summary-process-mark-set (set)
6066   "Make SET into the current process marked articles."
6067   (gnus-summary-unmark-all-processable)
6068   (while set
6069     (gnus-summary-set-process-mark (pop set))))
6070
6071 ;;; Searching and stuff
6072
6073 (defun gnus-summary-search-group (&optional backward use-level)
6074   "Search for next unread newsgroup.
6075 If optional argument BACKWARD is non-nil, search backward instead."
6076   (save-excursion
6077     (set-buffer gnus-group-buffer)
6078     (when (gnus-group-search-forward
6079            backward nil (if use-level (gnus-group-group-level) nil))
6080       (gnus-group-group-name))))
6081
6082 (defun gnus-summary-best-group (&optional exclude-group)
6083   "Find the name of the best unread group.
6084 If EXCLUDE-GROUP, do not go to this group."
6085   (save-excursion
6086     (set-buffer gnus-group-buffer)
6087     (save-excursion
6088       (gnus-group-best-unread-group exclude-group))))
6089
6090 (defun gnus-summary-find-next (&optional unread article backward)
6091   (if backward (gnus-summary-find-prev unread article)
6092     (let* ((dummy (gnus-summary-article-intangible-p))
6093            (article (or article (gnus-summary-article-number)))
6094            (data (gnus-data-find-list article))
6095            result)
6096       (when (and (not dummy)
6097                  (or (not gnus-summary-check-current)
6098                      (not unread)
6099                      (not (gnus-data-unread-p (car data)))))
6100         (setq data (cdr data)))
6101       (when (setq result
6102                   (if unread
6103                       (progn
6104                         (while data
6105                           (unless (memq (gnus-data-number (car data)) 
6106                                         (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6107                                                gnus-newsgroup-undownloaded)
6108                                               (gnus-plugged
6109                                                nil)
6110                                               ((eq gnus-auto-goto-ignores 'unfetched)
6111                                                gnus-newsgroup-unfetched)
6112                                               ((eq gnus-auto-goto-ignores 'undownloaded)
6113                                                gnus-newsgroup-undownloaded)))
6114                             (when (gnus-data-unread-p (car data))
6115                               (setq result (car data)
6116                                     data nil)))
6117                           (setq data (cdr data)))
6118                         result)
6119                     (car data)))
6120         (goto-char (gnus-data-pos result))
6121         (gnus-data-number result)))))
6122
6123 (defun gnus-summary-find-prev (&optional unread article)
6124   (let* ((eobp (eobp))
6125          (article (or article (gnus-summary-article-number)))
6126          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6127          result)
6128     (when (and (not eobp)
6129                (or (not gnus-summary-check-current)
6130                    (not unread)
6131                    (not (gnus-data-unread-p (car data)))))
6132       (setq data (cdr data)))
6133     (when (setq result
6134                 (if unread
6135                     (progn
6136                       (while data
6137                         (unless (memq (gnus-data-number (car data))
6138                                       (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6139                                              gnus-newsgroup-undownloaded)
6140                                             (gnus-plugged
6141                                              nil)
6142                                             ((eq gnus-auto-goto-ignores 'unfetched)
6143                                              gnus-newsgroup-unfetched)
6144                                             ((eq gnus-auto-goto-ignores 'undownloaded)
6145                                              gnus-newsgroup-undownloaded)))
6146                           (when (gnus-data-unread-p (car data))
6147                             (setq result (car data)
6148                                   data nil)))
6149                         (setq data (cdr data)))
6150                       result)
6151                   (car data)))
6152       (goto-char (gnus-data-pos result))
6153       (gnus-data-number result))))
6154
6155 (defun gnus-summary-find-subject (subject &optional unread backward article)
6156   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6157          (article (or article (gnus-summary-article-number)))
6158          (articles (gnus-data-list backward))
6159          (arts (gnus-data-find-list article articles))
6160          result)
6161     (when (or (not gnus-summary-check-current)
6162               (not unread)
6163               (not (gnus-data-unread-p (car arts))))
6164       (setq arts (cdr arts)))
6165     (while arts
6166       (and (or (not unread)
6167                (gnus-data-unread-p (car arts)))
6168            (vectorp (gnus-data-header (car arts)))
6169            (gnus-subject-equal
6170             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6171            (setq result (car arts)
6172                  arts nil))
6173       (setq arts (cdr arts)))
6174     (and result
6175          (goto-char (gnus-data-pos result))
6176          (gnus-data-number result))))
6177
6178 (defun gnus-summary-search-forward (&optional unread subject backward)
6179   "Search forward for an article.
6180 If UNREAD, look for unread articles.  If SUBJECT, look for
6181 articles with that subject.  If BACKWARD, search backward instead."
6182   (cond (subject (gnus-summary-find-subject subject unread backward))
6183         (backward (gnus-summary-find-prev unread))
6184         (t (gnus-summary-find-next unread))))
6185
6186 (defun gnus-recenter (&optional n)
6187   "Center point in window and redisplay frame.
6188 Also do horizontal recentering."
6189   (interactive "P")
6190   (when (and gnus-auto-center-summary
6191              (not (eq gnus-auto-center-summary 'vertical)))
6192     (gnus-horizontal-recenter))
6193   (recenter n))
6194
6195 (defun gnus-summary-recenter ()
6196   "Center point in the summary window.
6197 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6198 displayed, no centering will be performed."
6199   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6200   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6201   (interactive)
6202   ;; The user has to want it.
6203   (when gnus-auto-center-summary
6204     (let* ((top (cond ((< (window-height) 4) 0)
6205                       ((< (window-height) 7) 1)
6206                       (t (if (numberp gnus-auto-center-summary)
6207                              gnus-auto-center-summary
6208                            2))))
6209            (height (1- (window-height)))
6210            (bottom (save-excursion (goto-char (point-max))
6211                                    (forward-line (- height))
6212                                    (point)))
6213            (window (get-buffer-window (current-buffer))))
6214       (when (get-buffer-window gnus-article-buffer)
6215         ;; Only do recentering when the article buffer is displayed,
6216         ;; Set the window start to either `bottom', which is the biggest
6217         ;; possible valid number, or the second line from the top,
6218         ;; whichever is the least.
6219         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6220           (if (> bottom top-pos)
6221               ;; Keep the second line from the top visible
6222               (set-window-start window top-pos t)
6223             ;; Try to keep the bottom line visible; if it's partially
6224             ;; obscured, either scroll one more line to make it fully
6225             ;; visible, or revert to using TOP-POS.
6226             (save-excursion
6227               (goto-char (point-max))
6228               (forward-line -1)
6229               (let ((last-line-start (point)))
6230                 (goto-char bottom)
6231                 (set-window-start window (point) t)
6232                 (when (not (pos-visible-in-window-p last-line-start window))
6233                   (forward-line 1)
6234                   (set-window-start window (min (point) top-pos) t)))))))
6235       ;; Do horizontal recentering while we're at it.
6236       (when (and (get-buffer-window (current-buffer) t)
6237                  (not (eq gnus-auto-center-summary 'vertical)))
6238         (let ((selected (selected-window)))
6239           (select-window (get-buffer-window (current-buffer) t))
6240           (gnus-summary-position-point)
6241           (gnus-horizontal-recenter)
6242           (select-window selected))))))
6243
6244 (defun gnus-summary-jump-to-group (newsgroup)
6245   "Move point to NEWSGROUP in group mode buffer."
6246   ;; Keep update point of group mode buffer if visible.
6247   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6248       (save-window-excursion
6249         ;; Take care of tree window mode.
6250         (when (get-buffer-window gnus-group-buffer)
6251           (pop-to-buffer gnus-group-buffer))
6252         (gnus-group-jump-to-group newsgroup))
6253     (save-excursion
6254       ;; Take care of tree window mode.
6255       (if (get-buffer-window gnus-group-buffer)
6256           (pop-to-buffer gnus-group-buffer)
6257         (set-buffer gnus-group-buffer))
6258       (gnus-group-jump-to-group newsgroup))))
6259
6260 ;; This function returns a list of article numbers based on the
6261 ;; difference between the ranges of read articles in this group and
6262 ;; the range of active articles.
6263 (defun gnus-list-of-unread-articles (group)
6264   (let* ((read (gnus-info-read (gnus-get-info group)))
6265          (active (or (gnus-active group) (gnus-activate-group group)))
6266          (last (cdr active))
6267          first nlast unread)
6268     ;; If none are read, then all are unread.
6269     (if (not read)
6270         (setq first (car active))
6271       ;; If the range of read articles is a single range, then the
6272       ;; first unread article is the article after the last read
6273       ;; article.  Sounds logical, doesn't it?
6274       (if (and (not (listp (cdr read)))
6275                (or (< (car read) (car active))
6276                    (progn (setq read (list read))
6277                           nil)))
6278           (setq first (max (car active) (1+ (cdr read))))
6279         ;; `read' is a list of ranges.
6280         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6281                                   (caar read)))
6282                   1)
6283           (setq first (car active)))
6284         (while read
6285           (when first
6286             (while (< first nlast)
6287               (push first unread)
6288               (setq first (1+ first))))
6289           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6290           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6291           (setq read (cdr read)))))
6292     ;; And add the last unread articles.
6293     (while (<= first last)
6294       (push first unread)
6295       (setq first (1+ first)))
6296     ;; Return the list of unread articles.
6297     (delq 0 (nreverse unread))))
6298
6299 (defun gnus-list-of-read-articles (group)
6300   "Return a list of unread, unticked and non-dormant articles."
6301   (let* ((info (gnus-get-info group))
6302          (marked (gnus-info-marks info))
6303          (active (gnus-active group)))
6304     (and info active
6305          (gnus-list-range-difference
6306           (gnus-list-range-difference
6307            (gnus-sorted-complement
6308             (gnus-uncompress-range active)
6309             (gnus-list-of-unread-articles group))
6310            (cdr (assq 'dormant marked)))
6311           (cdr (assq 'tick marked))))))
6312
6313 ;; Various summary commands
6314
6315 (defun gnus-summary-select-article-buffer ()
6316   "Reconfigure windows to show article buffer."
6317   (interactive)
6318   (if (not (gnus-buffer-live-p gnus-article-buffer))
6319       (error "There is no article buffer for this summary buffer")
6320     (gnus-configure-windows 'article)
6321     (select-window (get-buffer-window gnus-article-buffer))))
6322
6323 (defun gnus-summary-universal-argument (arg)
6324   "Perform any operation on all articles that are process/prefixed."
6325   (interactive "P")
6326   (let ((articles (gnus-summary-work-articles arg))
6327         func article)
6328     (if (eq
6329          (setq
6330           func
6331           (key-binding
6332            (read-key-sequence
6333             (substitute-command-keys
6334              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6335          'undefined)
6336         (gnus-error 1 "Undefined key")
6337       (save-excursion
6338         (while articles
6339           (gnus-summary-goto-subject (setq article (pop articles)))
6340           (let (gnus-newsgroup-processable)
6341             (command-execute func))
6342           (gnus-summary-remove-process-mark article)))))
6343   (gnus-summary-position-point))
6344
6345 (defun gnus-summary-toggle-truncation (&optional arg)
6346   "Toggle truncation of summary lines.
6347 With arg, turn line truncation on if arg is positive."
6348   (interactive "P")
6349   (setq truncate-lines
6350         (if (null arg) (not truncate-lines)
6351           (> (prefix-numeric-value arg) 0)))
6352   (redraw-display))
6353
6354 (defun gnus-summary-find-for-reselect ()
6355   "Return the number of an article to stay on across a reselect.
6356 The current article is considered, then following articles, then previous
6357 articles.  An article is sought which is not cancelled and isn't a temporary
6358 insertion from another group.  If there's no such then return a dummy 0."
6359   (let (found)
6360     (dolist (rev '(nil t))
6361       (unless found      ; don't demand the reverse list if we don't need it
6362         (let ((data (gnus-data-find-list
6363                      (gnus-summary-article-number) (gnus-data-list rev))))
6364           (while (and data (not found))
6365             (if (and (< 0 (gnus-data-number (car data)))
6366                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6367                 (setq found (gnus-data-number (car data))))
6368             (setq data (cdr data))))))
6369     (or found 0)))
6370
6371 (defun gnus-summary-reselect-current-group (&optional all rescan)
6372   "Exit and then reselect the current newsgroup.
6373 The prefix argument ALL means to select all articles."
6374   (interactive "P")
6375   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6376     (error "Ephemeral groups can't be reselected"))
6377   (let ((current-subject (gnus-summary-find-for-reselect))
6378         (group gnus-newsgroup-name))
6379     (setq gnus-newsgroup-begin nil)
6380     (gnus-summary-exit)
6381     ;; We have to adjust the point of group mode buffer because
6382     ;; point was moved to the next unread newsgroup by exiting.
6383     (gnus-summary-jump-to-group group)
6384     (when rescan
6385       (save-excursion
6386         (gnus-group-get-new-news-this-group 1)))
6387     (gnus-group-read-group all t)
6388     (gnus-summary-goto-subject current-subject nil t)))
6389
6390 (defun gnus-summary-rescan-group (&optional all)
6391   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6392   (interactive "P")
6393   (gnus-summary-reselect-current-group all t))
6394
6395 (defun gnus-summary-update-info (&optional non-destructive)
6396   (save-excursion
6397     (let ((group gnus-newsgroup-name))
6398       (when group
6399         (when gnus-newsgroup-kill-headers
6400           (setq gnus-newsgroup-killed
6401                 (gnus-compress-sequence
6402                  (gnus-sorted-union
6403                   (gnus-list-range-intersection
6404                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6405                   gnus-newsgroup-unreads)
6406                  t)))
6407         (unless (listp (cdr gnus-newsgroup-killed))
6408           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6409         (let ((headers gnus-newsgroup-headers))
6410           ;; Set the new ranges of read articles.
6411           (save-excursion
6412             (set-buffer gnus-group-buffer)
6413             (gnus-undo-force-boundary))
6414           (gnus-update-read-articles
6415            group (gnus-sorted-union
6416                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6417           ;; Set the current article marks.
6418           (let ((gnus-newsgroup-scored
6419                  (if (and (not gnus-save-score)
6420                           (not non-destructive))
6421                      nil
6422                    gnus-newsgroup-scored)))
6423             (save-excursion
6424               (gnus-update-marks)))
6425           ;; Do the cross-ref thing.
6426           (when gnus-use-cross-reference
6427             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6428           ;; Do not switch windows but change the buffer to work.
6429           (set-buffer gnus-group-buffer)
6430           (unless (gnus-ephemeral-group-p group)
6431             (gnus-group-update-group group)))))))
6432
6433 (defun gnus-summary-save-newsrc (&optional force)
6434   "Save the current number of read/marked articles in the dribble buffer.
6435 The dribble buffer will then be saved.
6436 If FORCE (the prefix), also save the .newsrc file(s)."
6437   (interactive "P")
6438   (gnus-summary-update-info t)
6439   (if force
6440       (gnus-save-newsrc-file)
6441     (gnus-dribble-save)))
6442
6443 (defun gnus-summary-exit (&optional temporary)
6444   "Exit reading current newsgroup, and then return to group selection mode.
6445 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6446   (interactive)
6447   (gnus-set-global-variables)
6448   (when (gnus-buffer-live-p gnus-article-buffer)
6449     (save-excursion
6450       (set-buffer gnus-article-buffer)
6451       (mm-destroy-parts gnus-article-mime-handles)
6452       ;; Set it to nil for safety reason.
6453       (setq gnus-article-mime-handle-alist nil)
6454       (setq gnus-article-mime-handles nil)))
6455   (gnus-kill-save-kill-buffer)
6456   (gnus-async-halt-prefetch)
6457   (let* ((group gnus-newsgroup-name)
6458          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6459          (gnus-group-is-exiting-p t)
6460          (mode major-mode)
6461          (group-point nil)
6462          (buf (current-buffer)))
6463     (unless quit-config
6464       ;; Do adaptive scoring, and possibly save score files.
6465       (when gnus-newsgroup-adaptive
6466         (gnus-score-adaptive))
6467       (when gnus-use-scoring
6468         (gnus-score-save)))
6469     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6470     ;; If we have several article buffers, we kill them at exit.
6471     (unless gnus-single-article-buffer
6472       (gnus-kill-buffer gnus-original-article-buffer)
6473       (setq gnus-article-current nil))
6474     (when gnus-use-cache
6475       (gnus-cache-possibly-remove-articles)
6476       (gnus-cache-save-buffers))
6477     (gnus-async-prefetch-remove-group group)
6478     (when gnus-suppress-duplicates
6479       (gnus-dup-enter-articles))
6480     (when gnus-use-trees
6481       (gnus-tree-close group))
6482     (when gnus-use-cache
6483       (gnus-cache-write-active))
6484     ;; Remove entries for this group.
6485     (nnmail-purge-split-history (gnus-group-real-name group))
6486     ;; Make all changes in this group permanent.
6487     (unless quit-config
6488       (gnus-run-hooks 'gnus-exit-group-hook)
6489       (gnus-summary-update-info))
6490     (gnus-close-group group)
6491     ;; Make sure where we were, and go to next newsgroup.
6492     (set-buffer gnus-group-buffer)
6493     (unless quit-config
6494       (gnus-group-jump-to-group group))
6495     (gnus-run-hooks 'gnus-summary-exit-hook)
6496     (unless (or quit-config
6497                 ;; If this group has disappeared from the summary
6498                 ;; buffer, don't skip forwards.
6499                 (not (string= group (gnus-group-group-name))))
6500       (gnus-group-next-unread-group 1))
6501     (setq group-point (point))
6502     (if temporary
6503         nil                             ;Nothing to do.
6504       ;; If we have several article buffers, we kill them at exit.
6505       (unless gnus-single-article-buffer
6506         (gnus-kill-buffer gnus-article-buffer)
6507         (gnus-kill-buffer gnus-original-article-buffer)
6508         (setq gnus-article-current nil))
6509       (set-buffer buf)
6510       (if (not gnus-kill-summary-on-exit)
6511           (progn
6512             (gnus-deaden-summary)
6513             (setq mode nil))
6514         ;; We set all buffer-local variables to nil.  It is unclear why
6515         ;; this is needed, but if we don't, buffer-local variables are
6516         ;; not garbage-collected, it seems.  This would the lead to en
6517         ;; ever-growing Emacs.
6518         (gnus-summary-clear-local-variables)
6519         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6520           (gnus-summary-clear-local-variables))
6521         (when (get-buffer gnus-article-buffer)
6522           (bury-buffer gnus-article-buffer))
6523         ;; We clear the global counterparts of the buffer-local
6524         ;; variables as well, just to be on the safe side.
6525         (set-buffer gnus-group-buffer)
6526         (gnus-summary-clear-local-variables)
6527         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6528           (gnus-summary-clear-local-variables))
6529         ;; Return to group mode buffer.
6530         (when (eq mode 'gnus-summary-mode)
6531           (gnus-kill-buffer buf)))
6532       (setq gnus-current-select-method gnus-select-method)
6533       (pop-to-buffer gnus-group-buffer)
6534       (if (not quit-config)
6535           (progn
6536             (goto-char group-point)
6537             (gnus-configure-windows 'group 'force))
6538         (gnus-handle-ephemeral-exit quit-config))
6539       ;; Clear the current group name.
6540       (unless quit-config
6541         (setq gnus-newsgroup-name nil)))))
6542
6543 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6544 (defun gnus-summary-exit-no-update (&optional no-questions)
6545   "Quit reading current newsgroup without updating read article info."
6546   (interactive)
6547   (let* ((group gnus-newsgroup-name)
6548          (gnus-group-is-exiting-p t)
6549          (gnus-group-is-exiting-without-update-p t)
6550          (quit-config (gnus-group-quit-config group)))
6551     (when (or no-questions
6552               gnus-expert-user
6553               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6554       (gnus-async-halt-prefetch)
6555       (run-hooks 'gnus-summary-prepare-exit-hook)
6556       (when (gnus-buffer-live-p gnus-article-buffer)
6557         (save-excursion
6558           (set-buffer gnus-article-buffer)
6559           (mm-destroy-parts gnus-article-mime-handles)
6560           ;; Set it to nil for safety reason.
6561           (setq gnus-article-mime-handle-alist nil)
6562           (setq gnus-article-mime-handles nil)))
6563       ;; If we have several article buffers, we kill them at exit.
6564       (unless gnus-single-article-buffer
6565         (gnus-kill-buffer gnus-article-buffer)
6566         (gnus-kill-buffer gnus-original-article-buffer)
6567         (setq gnus-article-current nil))
6568       (if (not gnus-kill-summary-on-exit)
6569           (gnus-deaden-summary)
6570         (gnus-close-group group)
6571         (gnus-summary-clear-local-variables)
6572         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6573           (gnus-summary-clear-local-variables))
6574         (set-buffer gnus-group-buffer)
6575         (gnus-summary-clear-local-variables)
6576         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6577           (gnus-summary-clear-local-variables))
6578         (gnus-kill-buffer gnus-summary-buffer))
6579       (unless gnus-single-article-buffer
6580         (setq gnus-article-current nil))
6581       (when gnus-use-trees
6582         (gnus-tree-close group))
6583       (gnus-async-prefetch-remove-group group)
6584       (when (get-buffer gnus-article-buffer)
6585         (bury-buffer gnus-article-buffer))
6586       ;; Return to the group buffer.
6587       (gnus-configure-windows 'group 'force)
6588       ;; Clear the current group name.
6589       (setq gnus-newsgroup-name nil)
6590       (unless (gnus-ephemeral-group-p group)
6591         (gnus-group-update-group group))
6592       (when (equal (gnus-group-group-name) group)
6593         (gnus-group-next-unread-group 1))
6594       (when quit-config
6595         (gnus-handle-ephemeral-exit quit-config)))))
6596
6597 (defun gnus-handle-ephemeral-exit (quit-config)
6598   "Handle movement when leaving an ephemeral group.
6599 The state which existed when entering the ephemeral is reset."
6600   (if (not (buffer-name (car quit-config)))
6601       (gnus-configure-windows 'group 'force)
6602     (set-buffer (car quit-config))
6603     (cond ((eq major-mode 'gnus-summary-mode)
6604            (gnus-set-global-variables))
6605           ((eq major-mode 'gnus-article-mode)
6606            (save-excursion
6607              ;; The `gnus-summary-buffer' variable may point
6608              ;; to the old summary buffer when using a single
6609              ;; article buffer.
6610              (unless (gnus-buffer-live-p gnus-summary-buffer)
6611                (set-buffer gnus-group-buffer))
6612              (set-buffer gnus-summary-buffer)
6613              (gnus-set-global-variables))))
6614     (if (or (eq (cdr quit-config) 'article)
6615             (eq (cdr quit-config) 'pick))
6616         (progn
6617           ;; The current article may be from the ephemeral group
6618           ;; thus it is best that we reload this article
6619           (gnus-summary-show-article)
6620           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6621               (gnus-configure-windows 'pick 'force)
6622             (gnus-configure-windows (cdr quit-config) 'force)))
6623       (gnus-configure-windows (cdr quit-config) 'force))
6624     (when (eq major-mode 'gnus-summary-mode)
6625       (gnus-summary-next-subject 1 nil t)
6626       (gnus-summary-recenter)
6627       (gnus-summary-position-point))))
6628
6629 ;;; Dead summaries.
6630
6631 (defvar gnus-dead-summary-mode-map nil)
6632
6633 (unless gnus-dead-summary-mode-map
6634   (setq gnus-dead-summary-mode-map (make-keymap))
6635   (suppress-keymap gnus-dead-summary-mode-map)
6636   (substitute-key-definition
6637    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6638   (dolist (key '("\C-d" "\r" "\177" [delete]))
6639     (define-key gnus-dead-summary-mode-map
6640       key 'gnus-summary-wake-up-the-dead))
6641   (dolist (key '("q" "Q"))
6642     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6643
6644 (defvar gnus-dead-summary-mode nil
6645   "Minor mode for Gnus summary buffers.")
6646
6647 (defun gnus-dead-summary-mode (&optional arg)
6648   "Minor mode for Gnus summary buffers."
6649   (interactive "P")
6650   (when (eq major-mode 'gnus-summary-mode)
6651     (make-local-variable 'gnus-dead-summary-mode)
6652     (setq gnus-dead-summary-mode
6653           (if (null arg) (not gnus-dead-summary-mode)
6654             (> (prefix-numeric-value arg) 0)))
6655     (when gnus-dead-summary-mode
6656       (gnus-add-minor-mode
6657        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6658
6659 (defun gnus-deaden-summary ()
6660   "Make the current summary buffer into a dead summary buffer."
6661   ;; Kill any previous dead summary buffer.
6662   (when (and gnus-dead-summary
6663              (buffer-name gnus-dead-summary))
6664     (save-excursion
6665       (set-buffer gnus-dead-summary)
6666       (when gnus-dead-summary-mode
6667         (kill-buffer (current-buffer)))))
6668   ;; Make this the current dead summary.
6669   (setq gnus-dead-summary (current-buffer))
6670   (gnus-dead-summary-mode 1)
6671   (let ((name (buffer-name)))
6672     (when (string-match "Summary" name)
6673       (rename-buffer
6674        (concat (substring name 0 (match-beginning 0)) "Dead "
6675                (substring name (match-beginning 0)))
6676        t)
6677       (bury-buffer))))
6678
6679 (defun gnus-kill-or-deaden-summary (buffer)
6680   "Kill or deaden the summary BUFFER."
6681   (save-excursion
6682     (when (and (buffer-name buffer)
6683                (not gnus-single-article-buffer))
6684       (save-excursion
6685         (set-buffer buffer)
6686         (gnus-kill-buffer gnus-article-buffer)
6687         (gnus-kill-buffer gnus-original-article-buffer)))
6688     (cond
6689      ;; Kill the buffer.
6690      (gnus-kill-summary-on-exit
6691       (when (and gnus-use-trees
6692                  (gnus-buffer-exists-p buffer))
6693         (save-excursion
6694           (set-buffer buffer)
6695           (gnus-tree-close gnus-newsgroup-name)))
6696       (gnus-kill-buffer buffer))
6697      ;; Deaden the buffer.
6698      ((gnus-buffer-exists-p buffer)
6699       (save-excursion
6700         (set-buffer buffer)
6701         (gnus-deaden-summary))))))
6702
6703 (defun gnus-summary-wake-up-the-dead (&rest args)
6704   "Wake up the dead summary buffer."
6705   (interactive)
6706   (gnus-dead-summary-mode -1)
6707   (let ((name (buffer-name)))
6708     (when (string-match "Dead " name)
6709       (rename-buffer
6710        (concat (substring name 0 (match-beginning 0))
6711                (substring name (match-end 0)))
6712        t)))
6713   (gnus-message 3 "This dead summary is now alive again"))
6714
6715 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6716 (defun gnus-summary-fetch-faq (&optional faq-dir)
6717   "Fetch the FAQ for the current group.
6718 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6719 in."
6720   (interactive
6721    (list
6722     (when current-prefix-arg
6723       (completing-read
6724        "FAQ dir: " (and (listp gnus-group-faq-directory)
6725                         (mapcar (lambda (file) (list file))
6726                                 gnus-group-faq-directory))))))
6727   (let (gnus-faq-buffer)
6728     (when (setq gnus-faq-buffer
6729                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6730       (gnus-configure-windows 'summary-faq))))
6731
6732 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6733 (defun gnus-summary-describe-group (&optional force)
6734   "Describe the current newsgroup."
6735   (interactive "P")
6736   (gnus-group-describe-group force gnus-newsgroup-name))
6737
6738 (defun gnus-summary-describe-briefly ()
6739   "Describe summary mode commands briefly."
6740   (interactive)
6741   (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")))
6742
6743 ;; Walking around group mode buffer from summary mode.
6744
6745 (defun gnus-summary-next-group (&optional no-article target-group backward)
6746   "Exit current newsgroup and then select next unread newsgroup.
6747 If prefix argument NO-ARTICLE is non-nil, no article is selected
6748 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6749 previous group instead."
6750   (interactive "P")
6751   ;; Stop pre-fetching.
6752   (gnus-async-halt-prefetch)
6753   (let ((current-group gnus-newsgroup-name)
6754         (current-buffer (current-buffer))
6755         entered)
6756     ;; First we semi-exit this group to update Xrefs and all variables.
6757     ;; We can't do a real exit, because the window conf must remain
6758     ;; the same in case the user is prompted for info, and we don't
6759     ;; want the window conf to change before that...
6760     (gnus-summary-exit t)
6761     (while (not entered)
6762       ;; Then we find what group we are supposed to enter.
6763       (set-buffer gnus-group-buffer)
6764       (gnus-group-jump-to-group current-group)
6765       (setq target-group
6766             (or target-group
6767                 (if (eq gnus-keep-same-level 'best)
6768                     (gnus-summary-best-group gnus-newsgroup-name)
6769                   (gnus-summary-search-group backward gnus-keep-same-level))))
6770       (if (not target-group)
6771           ;; There are no further groups, so we return to the group
6772           ;; buffer.
6773           (progn
6774             (gnus-message 5 "Returning to the group buffer")
6775             (setq entered t)
6776             (when (gnus-buffer-live-p current-buffer)
6777               (set-buffer current-buffer)
6778               (gnus-summary-exit))
6779             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6780         ;; We try to enter the target group.
6781         (gnus-group-jump-to-group target-group)
6782         (let ((unreads (gnus-group-group-unread)))
6783           (if (and (or (eq t unreads)
6784                        (and unreads (not (zerop unreads))))
6785                    (gnus-summary-read-group
6786                     target-group nil no-article
6787                     (and (buffer-name current-buffer) current-buffer)
6788                     nil backward))
6789               (setq entered t)
6790             (setq current-group target-group
6791                   target-group nil)))))))
6792
6793 (defun gnus-summary-prev-group (&optional no-article)
6794   "Exit current newsgroup and then select previous unread newsgroup.
6795 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6796   (interactive "P")
6797   (gnus-summary-next-group no-article nil t))
6798
6799 ;; Walking around summary lines.
6800
6801 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6802   "Go to the first subject satisfying any non-nil constraint.
6803 If UNREAD is non-nil, the article should be unread.
6804 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6805 If UNSEED is non-nil, the article should be unseen.
6806 Returns the article selected or nil if there are no matching articles."
6807   (interactive "P")
6808   (cond
6809    ;; Empty summary.
6810    ((null gnus-newsgroup-data)
6811     (gnus-message 3 "No articles in the group")
6812     nil)
6813    ;; Pick the first article.
6814    ((not (or unread undownloaded unseen))
6815     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6816     (gnus-data-number (car gnus-newsgroup-data)))
6817    ;; Find the first unread article.
6818    (t
6819     (let ((data gnus-newsgroup-data))
6820       (while (and data
6821                   (let ((num (gnus-data-number (car data))))
6822                     (or (memq num gnus-newsgroup-unfetched)
6823                         (not (or (and unread
6824                                       (memq num gnus-newsgroup-unreads))
6825                                  (and undownloaded
6826                                       (memq num gnus-newsgroup-undownloaded))
6827                                  (and unseen
6828                                       (memq num gnus-newsgroup-unseen)))))))
6829         (setq data (cdr data)))
6830       (prog1 
6831           (if data
6832               (progn
6833                 (goto-char (gnus-data-pos (car data)))
6834                 (gnus-data-number (car data)))
6835             (gnus-message 3 "No more%s articles"
6836                           (let* ((r (when unread " unread"))
6837                                  (d (when undownloaded " undownloaded"))
6838                                  (s (when unseen " unseen"))
6839                                  (l (delq nil (list r d s))))
6840                             (cond ((= 3 (length l))
6841                                    (concat r "," d ", or" s))
6842                                   ((= 2 (length l))
6843                                    (concat (car l) ", or" (cadr l)))
6844                                   ((= 1 (length l))
6845                                    (car l))
6846                                   (t
6847                                    ""))))
6848             nil
6849             )
6850         (gnus-summary-position-point))))))
6851
6852 (defun gnus-summary-next-subject (n &optional unread dont-display)
6853   "Go to next N'th summary line.
6854 If N is negative, go to the previous N'th subject line.
6855 If UNREAD is non-nil, only unread articles are selected.
6856 The difference between N and the actual number of steps taken is
6857 returned."
6858   (interactive "p")
6859   (let ((backward (< n 0))
6860         (n (abs n)))
6861     (while (and (> n 0)
6862                 (if backward
6863                     (gnus-summary-find-prev unread)
6864                   (gnus-summary-find-next unread)))
6865       (unless (zerop (setq n (1- n)))
6866         (gnus-summary-show-thread)))
6867     (when (/= 0 n)
6868       (gnus-message 7 "No more%s articles"
6869                     (if unread " unread" "")))
6870     (unless dont-display
6871       (gnus-summary-recenter)
6872       (gnus-summary-position-point))
6873     n))
6874
6875 (defun gnus-summary-next-unread-subject (n)
6876   "Go to next N'th unread summary line."
6877   (interactive "p")
6878   (gnus-summary-next-subject n t))
6879
6880 (defun gnus-summary-prev-subject (n &optional unread)
6881   "Go to previous N'th summary line.
6882 If optional argument UNREAD is non-nil, only unread article is selected."
6883   (interactive "p")
6884   (gnus-summary-next-subject (- n) unread))
6885
6886 (defun gnus-summary-prev-unread-subject (n)
6887   "Go to previous N'th unread summary line."
6888   (interactive "p")
6889   (gnus-summary-next-subject (- n) t))
6890
6891 (defun gnus-summary-goto-subjects (articles)
6892   "Insert the subject header for ARTICLES in the current buffer."
6893   (save-excursion
6894     (dolist (article articles)
6895       (gnus-summary-goto-subject article t)))
6896   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6897   (gnus-summary-position-point))
6898  
6899 (defun gnus-summary-goto-subject (article &optional force silent)
6900   "Go the subject line of ARTICLE.
6901 If FORCE, also allow jumping to articles not currently shown."
6902   (interactive "nArticle number: ")
6903   (unless (numberp article)
6904     (error "Article %s is not a number" article))
6905   (let ((b (point))
6906         (data (gnus-data-find article)))
6907     ;; We read in the article if we have to.
6908     (and (not data)
6909          force
6910          (gnus-summary-insert-subject
6911           article
6912           (if (or (numberp force) (vectorp force)) force)
6913           t)
6914          (setq data (gnus-data-find article)))
6915     (goto-char b)
6916     (if (not data)
6917         (progn
6918           (unless silent
6919             (gnus-message 3 "Can't find article %d" article))
6920           nil)
6921       (let ((pt (gnus-data-pos data)))
6922         (goto-char pt)
6923         (gnus-summary-set-article-display-arrow pt))
6924       (gnus-summary-position-point)
6925       article)))
6926
6927 ;; Walking around summary lines with displaying articles.
6928
6929 (defun gnus-summary-expand-window (&optional arg)
6930   "Make the summary buffer take up the entire Emacs frame.
6931 Given a prefix, will force an `article' buffer configuration."
6932   (interactive "P")
6933   (if arg
6934       (gnus-configure-windows 'article 'force)
6935     (gnus-configure-windows 'summary 'force)))
6936
6937 (defun gnus-summary-display-article (article &optional all-header)
6938   "Display ARTICLE in article buffer."
6939   (when (gnus-buffer-live-p gnus-article-buffer)
6940     (with-current-buffer gnus-article-buffer
6941       (mm-enable-multibyte)))
6942   (gnus-set-global-variables)
6943   (when (gnus-buffer-live-p gnus-article-buffer)
6944     (with-current-buffer gnus-article-buffer
6945       (setq gnus-article-charset gnus-newsgroup-charset)
6946       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6947       (mm-enable-multibyte)))
6948   (if (null article)
6949       nil
6950     (prog1
6951         (if gnus-summary-display-article-function
6952             (funcall gnus-summary-display-article-function article all-header)
6953           (gnus-article-prepare article all-header))
6954       (gnus-run-hooks 'gnus-select-article-hook)
6955       (when (and gnus-current-article
6956                  (not (zerop gnus-current-article)))
6957         (gnus-summary-goto-subject gnus-current-article))
6958       (gnus-summary-recenter)
6959       (when (and gnus-use-trees gnus-show-threads)
6960         (gnus-possibly-generate-tree article)
6961         (gnus-highlight-selected-tree article))
6962       ;; Successfully display article.
6963       (gnus-article-set-window-start
6964        (cdr (assq article gnus-newsgroup-bookmarks))))))
6965
6966 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6967   "Select the current article.
6968 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6969 non-nil, the article will be re-fetched even if it already present in
6970 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6971 be displayed."
6972   ;; Make sure we are in the summary buffer to work around bbdb bug.
6973   (unless (eq major-mode 'gnus-summary-mode)
6974     (set-buffer gnus-summary-buffer))
6975   (let ((article (or article (gnus-summary-article-number)))
6976         (all-headers (not (not all-headers))) ;Must be t or nil.
6977         gnus-summary-display-article-function)
6978     (and (not pseudo)
6979          (gnus-summary-article-pseudo-p article)
6980          (error "This is a pseudo-article"))
6981     (save-excursion
6982       (set-buffer gnus-summary-buffer)
6983       (if (or (and gnus-single-article-buffer
6984                    (or (null gnus-current-article)
6985                        (null gnus-article-current)
6986                        (null (get-buffer gnus-article-buffer))
6987                        (not (eq article (cdr gnus-article-current)))
6988                        (not (equal (car gnus-article-current)
6989                                    gnus-newsgroup-name))))
6990               (and (not gnus-single-article-buffer)
6991                    (or (null gnus-current-article)
6992                        (not (eq gnus-current-article article))))
6993               force)
6994           ;; The requested article is different from the current article.
6995           (progn
6996             (gnus-summary-display-article article all-headers)
6997             (when (gnus-buffer-live-p gnus-article-buffer)
6998               (with-current-buffer gnus-article-buffer
6999                 (if (not gnus-article-decoded-p) ;; a local variable
7000                     (mm-disable-multibyte))))
7001             (gnus-article-set-window-start
7002              (cdr (assq article gnus-newsgroup-bookmarks)))
7003             article)
7004         'old))))
7005
7006 (defun gnus-summary-force-verify-and-decrypt ()
7007   "Display buttons for signed/encrypted parts and verify/decrypt them."
7008   (interactive)
7009   (let ((mm-verify-option 'known)
7010         (mm-decrypt-option 'known)
7011         (gnus-buttonized-mime-types (append (list "multipart/signed"
7012                                                   "multipart/encrypted")
7013                                             gnus-buttonized-mime-types)))
7014     (gnus-summary-select-article nil 'force)))
7015
7016 (defun gnus-summary-set-current-mark (&optional current-mark)
7017   "Obsolete function."
7018   nil)
7019
7020 (defun gnus-summary-next-article (&optional unread subject backward push)
7021   "Select the next article.
7022 If UNREAD, only unread articles are selected.
7023 If SUBJECT, only articles with SUBJECT are selected.
7024 If BACKWARD, the previous article is selected instead of the next."
7025   (interactive "P")
7026   (cond
7027    ;; Is there such an article?
7028    ((and (gnus-summary-search-forward unread subject backward)
7029          (or (gnus-summary-display-article (gnus-summary-article-number))
7030              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7031     (gnus-summary-position-point))
7032    ;; If not, we try the first unread, if that is wanted.
7033    ((and subject
7034          gnus-auto-select-same
7035          (gnus-summary-first-unread-article))
7036     (gnus-summary-position-point)
7037     (gnus-message 6 "Wrapped"))
7038    ;; Try to get next/previous article not displayed in this group.
7039    ((and gnus-auto-extend-newsgroup
7040          (not unread) (not subject))
7041     (gnus-summary-goto-article
7042      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7043      nil (count-lines (point-min) (point))))
7044    ;; Go to next/previous group.
7045    (t
7046     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7047       (gnus-summary-jump-to-group gnus-newsgroup-name))
7048     (let ((cmd last-command-char)
7049           (point
7050            (save-excursion
7051              (set-buffer gnus-group-buffer)
7052              (point)))
7053           (group
7054            (if (eq gnus-keep-same-level 'best)
7055                (gnus-summary-best-group gnus-newsgroup-name)
7056              (gnus-summary-search-group backward gnus-keep-same-level))))
7057       ;; For some reason, the group window gets selected.  We change
7058       ;; it back.
7059       (select-window (get-buffer-window (current-buffer)))
7060       ;; Select next unread newsgroup automagically.
7061       (cond
7062        ((or (not gnus-auto-select-next)
7063             (not cmd))
7064         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7065        ((or (eq gnus-auto-select-next 'quietly)
7066             (and (eq gnus-auto-select-next 'slightly-quietly)
7067                  push)
7068             (and (eq gnus-auto-select-next 'almost-quietly)
7069                  (gnus-summary-last-article-p)))
7070         ;; Select quietly.
7071         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7072             (gnus-summary-exit)
7073           (gnus-message 7 "No more%s articles (%s)..."
7074                         (if unread " unread" "")
7075                         (if group (concat "selecting " group)
7076                           "exiting"))
7077           (gnus-summary-next-group nil group backward)))
7078        (t
7079         (when (gnus-key-press-event-p last-input-event)
7080           (gnus-summary-walk-group-buffer
7081            gnus-newsgroup-name cmd unread backward point))))))))
7082
7083 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7084   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7085                       (?\C-p (gnus-group-prev-unread-group 1))))
7086         (cursor-in-echo-area t)
7087         keve key group ended prompt)
7088     (save-excursion
7089       (set-buffer gnus-group-buffer)
7090       (goto-char start)
7091       (setq group
7092             (if (eq gnus-keep-same-level 'best)
7093                 (gnus-summary-best-group gnus-newsgroup-name)
7094               (gnus-summary-search-group backward gnus-keep-same-level))))
7095     (while (not ended)
7096       (setq prompt
7097             (format
7098              "No more%s articles%s " (if unread " unread" "")
7099              (if (and group
7100                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7101                  (format " (Type %s for %s [%s])"
7102                          (single-key-description cmd) group
7103                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7104                (format " (Type %s to exit %s)"
7105                        (single-key-description cmd)
7106                        gnus-newsgroup-name))))
7107       ;; Confirm auto selection.
7108       (setq key (car (setq keve (gnus-read-event-char prompt)))
7109             ended t)
7110       (cond
7111        ((assq key keystrokes)
7112         (let ((obuf (current-buffer)))
7113           (switch-to-buffer gnus-group-buffer)
7114           (when group
7115             (gnus-group-jump-to-group group))
7116           (eval (cadr (assq key keystrokes)))
7117           (setq group (gnus-group-group-name))
7118           (switch-to-buffer obuf))
7119         (setq ended nil))
7120        ((equal key cmd)
7121         (if (or (not group)
7122                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7123             (gnus-summary-exit)
7124           (gnus-summary-next-group nil group backward)))
7125        (t
7126         (push (cdr keve) unread-command-events))))))
7127
7128 (defun gnus-summary-next-unread-article ()
7129   "Select unread article after current one."
7130   (interactive)
7131   (gnus-summary-next-article
7132    (or (not (eq gnus-summary-goto-unread 'never))
7133        (gnus-summary-last-article-p (gnus-summary-article-number)))
7134    (and gnus-auto-select-same
7135         (gnus-summary-article-subject))))
7136
7137 (defun gnus-summary-prev-article (&optional unread subject)
7138   "Select the article after the current one.
7139 If UNREAD is non-nil, only unread articles are selected."
7140   (interactive "P")
7141   (gnus-summary-next-article unread subject t))
7142
7143 (defun gnus-summary-prev-unread-article ()
7144   "Select unread article before current one."
7145   (interactive)
7146   (gnus-summary-prev-article
7147    (or (not (eq gnus-summary-goto-unread 'never))
7148        (gnus-summary-first-article-p (gnus-summary-article-number)))
7149    (and gnus-auto-select-same
7150         (gnus-summary-article-subject))))
7151
7152 (defun gnus-summary-next-page (&optional lines circular stop)
7153   "Show next page of the selected article.
7154 If at the end of the current article, select the next article.
7155 LINES says how many lines should be scrolled up.
7156
7157 If CIRCULAR is non-nil, go to the start of the article instead of
7158 selecting the next article when reaching the end of the current
7159 article.
7160
7161 If STOP is non-nil, just stop when reaching the end of the message."
7162   (interactive "P")
7163   (setq gnus-summary-buffer (current-buffer))
7164   (gnus-set-global-variables)
7165   (let ((article (gnus-summary-article-number))
7166         (article-window (get-buffer-window gnus-article-buffer t))
7167         endp)
7168     ;; If the buffer is empty, we have no article.
7169     (unless article
7170       (error "No article to select"))
7171     (gnus-configure-windows 'article)
7172     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7173         (if (and (eq gnus-summary-goto-unread 'never)
7174                  (not (gnus-summary-last-article-p article)))
7175             (gnus-summary-next-article)
7176           (gnus-summary-next-unread-article))
7177       (if (or (null gnus-current-article)
7178               (null gnus-article-current)
7179               (/= article (cdr gnus-article-current))
7180               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7181           ;; Selected subject is different from current article's.
7182           (gnus-summary-display-article article)
7183         (when article-window
7184           (gnus-eval-in-buffer-window gnus-article-buffer
7185             (setq endp (or (gnus-article-next-page lines)
7186                            (gnus-article-only-boring-p))))
7187           (when endp
7188             (cond (stop
7189                    (gnus-message 3 "End of message"))
7190                   (circular
7191                    (gnus-summary-beginning-of-article))
7192                   (lines
7193                    (gnus-message 3 "End of message"))
7194                   ((null lines)
7195                    (if (and (eq gnus-summary-goto-unread 'never)
7196                             (not (gnus-summary-last-article-p article)))
7197                        (gnus-summary-next-article)
7198                      (gnus-summary-next-unread-article))))))))
7199     (gnus-summary-recenter)
7200     (gnus-summary-position-point)))
7201
7202 (defun gnus-summary-prev-page (&optional lines move)
7203   "Show previous page of selected article.
7204 Argument LINES specifies lines to be scrolled down.
7205 If MOVE, move to the previous unread article if point is at
7206 the beginning of the buffer."
7207   (interactive "P")
7208   (let ((article (gnus-summary-article-number))
7209         (article-window (get-buffer-window gnus-article-buffer t))
7210         endp)
7211     (gnus-configure-windows 'article)
7212     (if (or (null gnus-current-article)
7213             (null gnus-article-current)
7214             (/= article (cdr gnus-article-current))
7215             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7216         ;; Selected subject is different from current article's.
7217         (gnus-summary-display-article article)
7218       (gnus-summary-recenter)
7219       (when article-window
7220         (gnus-eval-in-buffer-window gnus-article-buffer
7221           (setq endp (gnus-article-prev-page lines)))
7222         (when (and move endp)
7223           (cond (lines
7224                  (gnus-message 3 "Beginning of message"))
7225                 ((null lines)
7226                  (if (and (eq gnus-summary-goto-unread 'never)
7227                           (not (gnus-summary-first-article-p article)))
7228                      (gnus-summary-prev-article)
7229                    (gnus-summary-prev-unread-article))))))))
7230   (gnus-summary-position-point))
7231
7232 (defun gnus-summary-prev-page-or-article (&optional lines)
7233   "Show previous page of selected article.
7234 Argument LINES specifies lines to be scrolled down.
7235 If at the beginning of the article, go to the next article."
7236   (interactive "P")
7237   (gnus-summary-prev-page lines t))
7238
7239 (defun gnus-summary-scroll-up (lines)
7240   "Scroll up (or down) one line current article.
7241 Argument LINES specifies lines to be scrolled up (or down if negative)."
7242   (interactive "p")
7243   (gnus-configure-windows 'article)
7244   (gnus-summary-show-thread)
7245   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7246     (gnus-eval-in-buffer-window gnus-article-buffer
7247       (cond ((> lines 0)
7248              (when (gnus-article-next-page lines)
7249                (gnus-message 3 "End of message")))
7250             ((< lines 0)
7251              (gnus-article-prev-page (- lines))))))
7252   (gnus-summary-recenter)
7253   (gnus-summary-position-point))
7254
7255 (defun gnus-summary-scroll-down (lines)
7256   "Scroll down (or up) one line current article.
7257 Argument LINES specifies lines to be scrolled down (or up if negative)."
7258   (interactive "p")
7259   (gnus-summary-scroll-up (- lines)))
7260
7261 (defun gnus-summary-next-same-subject ()
7262   "Select next article which has the same subject as current one."
7263   (interactive)
7264   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7265
7266 (defun gnus-summary-prev-same-subject ()
7267   "Select previous article which has the same subject as current one."
7268   (interactive)
7269   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7270
7271 (defun gnus-summary-next-unread-same-subject ()
7272   "Select next unread article which has the same subject as current one."
7273   (interactive)
7274   (gnus-summary-next-article t (gnus-summary-article-subject)))
7275
7276 (defun gnus-summary-prev-unread-same-subject ()
7277   "Select previous unread article which has the same subject as current one."
7278   (interactive)
7279   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7280
7281 (defun gnus-summary-first-unread-article ()
7282   "Select the first unread article.
7283 Return nil if there are no unread articles."
7284   (interactive)
7285   (prog1
7286       (when (gnus-summary-first-subject t)
7287         (gnus-summary-show-thread)
7288         (gnus-summary-first-subject t)
7289         (gnus-summary-display-article (gnus-summary-article-number)))
7290     (gnus-summary-position-point)))
7291
7292 (defun gnus-summary-first-unread-subject ()
7293   "Place the point on the subject line of the first unread article.
7294 Return nil if there are no unread articles."
7295   (interactive)
7296   (prog1
7297       (when (gnus-summary-first-subject t)
7298         (gnus-summary-show-thread)
7299         (gnus-summary-first-subject t))
7300     (gnus-summary-position-point)))
7301
7302 (defun gnus-summary-first-unseen-subject ()
7303   "Place the point on the subject line of the first unseen article.
7304 Return nil if there are no unseen articles."
7305   (interactive)
7306   (prog1
7307       (when (gnus-summary-first-subject nil nil t)
7308         (gnus-summary-show-thread)
7309         (gnus-summary-first-subject nil nil t))
7310     (gnus-summary-position-point)))
7311
7312 (defun gnus-summary-first-unseen-or-unread-subject ()
7313   "Place the point on the subject line of the first unseen article or,
7314 if all article have been seen, on the subject line of the first unread
7315 article."
7316   (interactive)
7317   (prog1
7318       (unless (when (gnus-summary-first-subject nil nil t)
7319                 (gnus-summary-show-thread)
7320                 (gnus-summary-first-subject nil nil t))
7321         (when (gnus-summary-first-subject t)
7322           (gnus-summary-show-thread)
7323           (gnus-summary-first-subject t)))
7324     (gnus-summary-position-point)))
7325
7326 (defun gnus-summary-first-article ()
7327   "Select the first article.
7328 Return nil if there are no articles."
7329   (interactive)
7330   (prog1
7331       (when (gnus-summary-first-subject)
7332         (gnus-summary-show-thread)
7333         (gnus-summary-first-subject)
7334         (gnus-summary-display-article (gnus-summary-article-number)))
7335     (gnus-summary-position-point)))
7336
7337 (defun gnus-summary-best-unread-article (&optional arg)
7338   "Select the unread article with the highest score.
7339 If given a prefix argument, select the next unread article that has a
7340 score higher than the default score."
7341   (interactive "P")
7342   (let ((article (if arg
7343                      (gnus-summary-better-unread-subject)
7344                    (gnus-summary-best-unread-subject))))
7345     (if article
7346         (gnus-summary-goto-article article)
7347       (error "No unread articles"))))
7348
7349 (defun gnus-summary-best-unread-subject ()
7350   "Select the unread subject with the highest score."
7351   (interactive)
7352   (let ((best -1000000)
7353         (data gnus-newsgroup-data)
7354         article score)
7355     (while data
7356       (and (gnus-data-unread-p (car data))
7357            (> (setq score
7358                     (gnus-summary-article-score (gnus-data-number (car data))))
7359               best)
7360            (setq best score
7361                  article (gnus-data-number (car data))))
7362       (setq data (cdr data)))
7363     (when article
7364       (gnus-summary-goto-subject article))
7365     (gnus-summary-position-point)
7366     article))
7367
7368 (defun gnus-summary-better-unread-subject ()
7369   "Select the first unread subject that has a score over the default score."
7370   (interactive)
7371   (let ((data gnus-newsgroup-data)
7372         article score)
7373     (while (and (setq article (gnus-data-number (car data)))
7374                 (or (gnus-data-read-p (car data))
7375                     (not (> (gnus-summary-article-score article)
7376                             gnus-summary-default-score))))
7377       (setq data (cdr data)))
7378     (when article
7379       (gnus-summary-goto-subject article))
7380     (gnus-summary-position-point)
7381     article))
7382
7383 (defun gnus-summary-last-subject ()
7384   "Go to the last displayed subject line in the group."
7385   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7386     (when article
7387       (gnus-summary-goto-subject article))))
7388
7389 (defun gnus-summary-goto-article (article &optional all-headers force)
7390   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7391 If ALL-HEADERS is non-nil, no header lines are hidden.
7392 If FORCE, go to the article even if it isn't displayed.  If FORCE
7393 is a number, it is the line the article is to be displayed on."
7394   (interactive
7395    (list
7396     (completing-read
7397      "Article number or Message-ID: "
7398      (mapcar (lambda (number) (list (int-to-string number)))
7399              gnus-newsgroup-limit))
7400     current-prefix-arg
7401     t))
7402   (prog1
7403       (if (and (stringp article)
7404                (string-match "@" article))
7405           (gnus-summary-refer-article article)
7406         (when (stringp article)
7407           (setq article (string-to-number article)))
7408         (if (gnus-summary-goto-subject article force)
7409             (gnus-summary-display-article article all-headers)
7410           (gnus-message 4 "Couldn't go to article %s" article) nil))
7411     (gnus-summary-position-point)))
7412
7413 (defun gnus-summary-goto-last-article ()
7414   "Go to the previously read article."
7415   (interactive)
7416   (prog1
7417       (when gnus-last-article
7418         (gnus-summary-goto-article gnus-last-article nil t))
7419     (gnus-summary-position-point)))
7420
7421 (defun gnus-summary-pop-article (number)
7422   "Pop one article off the history and go to the previous.
7423 NUMBER articles will be popped off."
7424   (interactive "p")
7425   (let (to)
7426     (setq gnus-newsgroup-history
7427           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7428     (if to
7429         (gnus-summary-goto-article (car to) nil t)
7430       (error "Article history empty")))
7431   (gnus-summary-position-point))
7432
7433 ;; Summary commands and functions for limiting the summary buffer.
7434
7435 (defun gnus-summary-limit-to-articles (n)
7436   "Limit the summary buffer to the next N articles.
7437 If not given a prefix, use the process marked articles instead."
7438   (interactive "P")
7439   (prog1
7440       (let ((articles (gnus-summary-work-articles n)))
7441         (setq gnus-newsgroup-processable nil)
7442         (gnus-summary-limit articles))
7443     (gnus-summary-position-point)))
7444
7445 (defun gnus-summary-pop-limit (&optional total)
7446   "Restore the previous limit.
7447 If given a prefix, remove all limits."
7448   (interactive "P")
7449   (when total
7450     (setq gnus-newsgroup-limits
7451           (list (mapcar (lambda (h) (mail-header-number h))
7452                         gnus-newsgroup-headers))))
7453   (unless gnus-newsgroup-limits
7454     (error "No limit to pop"))
7455   (prog1
7456       (gnus-summary-limit nil 'pop)
7457     (gnus-summary-position-point)))
7458
7459 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7460   "Limit the summary buffer to articles that have subjects that match a regexp.
7461 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7462   (interactive
7463    (list (read-string (if current-prefix-arg
7464                           "Exclude subject (regexp): "
7465                         "Limit to subject (regexp): "))
7466          nil current-prefix-arg))
7467   (unless header
7468     (setq header "subject"))
7469   (when (not (equal "" subject))
7470     (prog1
7471         (let ((articles (gnus-summary-find-matching
7472                          (or header "subject") subject 'all nil nil
7473                          not-matching)))
7474           (unless articles
7475             (error "Found no matches for \"%s\"" subject))
7476           (gnus-summary-limit articles))
7477       (gnus-summary-position-point))))
7478
7479 (defun gnus-summary-limit-to-author (from &optional not-matching)
7480   "Limit the summary buffer to articles that have authors that match a regexp.
7481 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7482   (interactive
7483    (list (read-string (if current-prefix-arg
7484                           "Exclude author (regexp): "
7485                         "Limit to author (regexp): "))
7486          current-prefix-arg))
7487   (gnus-summary-limit-to-subject from "from" not-matching))
7488
7489 (defun gnus-summary-limit-to-age (age &optional younger-p)
7490   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7491 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7492 articles that are younger than AGE days."
7493   (interactive
7494    (let ((younger current-prefix-arg)
7495          (days-got nil)
7496          days)
7497      (while (not days-got)
7498        (setq days (if younger
7499                       (read-string "Limit to articles younger than (in days, older when negative): ")
7500                     (read-string
7501                      "Limit to articles older than (in days, younger when negative): ")))
7502        (when (> (length days) 0)
7503          (setq days (read days)))
7504        (if (numberp days)
7505            (progn
7506              (setq days-got t)
7507              (if (< days 0)
7508                  (progn
7509                    (setq younger (not younger))
7510                    (setq days (* days -1)))))
7511          (message "Please enter a number.")
7512          (sleep-for 1)))
7513      (list days younger)))
7514   (prog1
7515       (let ((data gnus-newsgroup-data)
7516             (cutoff (days-to-time age))
7517             articles d date is-younger)
7518         (while (setq d (pop data))
7519           (when (and (vectorp (gnus-data-header d))
7520                      (setq date (mail-header-date (gnus-data-header d))))
7521             (setq is-younger (time-less-p
7522                               (time-since (condition-case ()
7523                                               (date-to-time date)
7524                                             (error '(0 0))))
7525                               cutoff))
7526             (when (if younger-p
7527                       is-younger
7528                     (not is-younger))
7529               (push (gnus-data-number d) articles))))
7530         (gnus-summary-limit (nreverse articles)))
7531     (gnus-summary-position-point)))
7532
7533 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7534   "Limit the summary buffer to articles that match an 'extra' header."
7535   (interactive
7536    (let ((header
7537           (intern
7538            (gnus-completing-read-with-default
7539             (symbol-name (car gnus-extra-headers))
7540             (if current-prefix-arg
7541                 "Exclude extra header:"
7542               "Limit extra header:")
7543             (mapcar (lambda (x)
7544                       (cons (symbol-name x) x))
7545                     gnus-extra-headers)
7546             nil
7547             t))))
7548      (list header
7549            (read-string (format "%s header %s (regexp): "
7550                                 (if current-prefix-arg "Exclude" "Limit to")
7551                                 header))
7552            current-prefix-arg)))
7553   (when (not (equal "" regexp))
7554     (prog1
7555         (let ((articles (gnus-summary-find-matching
7556                          (cons 'extra header) regexp 'all nil nil
7557                          not-matching)))
7558           (unless articles
7559             (error "Found no matches for \"%s\"" regexp))
7560           (gnus-summary-limit articles))
7561       (gnus-summary-position-point))))
7562
7563 (defun gnus-summary-limit-to-display-predicate ()
7564   "Limit the summary buffer to the predicated in the `display' group parameter."
7565   (interactive)
7566   (unless gnus-newsgroup-display
7567     (error "There is no `display' group parameter"))
7568   (let (articles)
7569     (dolist (number gnus-newsgroup-articles)
7570       (when (funcall gnus-newsgroup-display)
7571         (push number articles)))
7572     (gnus-summary-limit articles))
7573   (gnus-summary-position-point))
7574
7575 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7576 (make-obsolete
7577  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7578
7579 (defun gnus-summary-limit-to-unread (&optional all)
7580   "Limit the summary buffer to articles that are not marked as read.
7581 If ALL is non-nil, limit strictly to unread articles."
7582   (interactive "P")
7583   (if all
7584       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7585     (gnus-summary-limit-to-marks
7586      ;; Concat all the marks that say that an article is read and have
7587      ;; those removed.
7588      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7589            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7590            gnus-low-score-mark gnus-expirable-mark
7591            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7592            gnus-duplicate-mark gnus-souped-mark)
7593      'reverse)))
7594
7595 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7596 (make-obsolete 'gnus-summary-delete-marked-with
7597                'gnus-summary-limit-exclude-marks)
7598
7599 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7600   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7601 If REVERSE, limit the summary buffer to articles that are marked
7602 with MARKS.  MARKS can either be a string of marks or a list of marks.
7603 Returns how many articles were removed."
7604   (interactive "sMarks: ")
7605   (gnus-summary-limit-to-marks marks t))
7606
7607 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7608   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7609 If REVERSE (the prefix), limit the summary buffer to articles that are
7610 not marked with MARKS.  MARKS can either be a string of marks or a
7611 list of marks.
7612 Returns how many articles were removed."
7613   (interactive "sMarks: \nP")
7614   (prog1
7615       (let ((data gnus-newsgroup-data)
7616             (marks (if (listp marks) marks
7617                      (append marks nil))) ; Transform to list.
7618             articles)
7619         (while data
7620           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7621                   (memq (gnus-data-mark (car data)) marks))
7622             (push (gnus-data-number (car data)) articles))
7623           (setq data (cdr data)))
7624         (gnus-summary-limit articles))
7625     (gnus-summary-position-point)))
7626
7627 (defun gnus-summary-limit-to-score (score)
7628   "Limit to articles with score at or above SCORE."
7629   (interactive "NLimit to articles with score of at least: ")
7630   (let ((data gnus-newsgroup-data)
7631         articles)
7632     (while data
7633       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7634                 score)
7635         (push (gnus-data-number (car data)) articles))
7636       (setq data (cdr data)))
7637     (prog1
7638         (gnus-summary-limit articles)
7639       (gnus-summary-position-point))))
7640
7641 (defun gnus-summary-limit-to-unseen ()
7642   "Limit to unseen articles."
7643   (interactive)
7644   (prog1
7645       (gnus-summary-limit gnus-newsgroup-unseen)
7646     (gnus-summary-position-point)))
7647
7648 (defun gnus-summary-limit-include-thread (id)
7649   "Display all the hidden articles that is in the thread with ID in it.
7650 When called interactively, ID is the Message-ID of the current
7651 article."
7652   (interactive (list (mail-header-id (gnus-summary-article-header))))
7653   (let ((articles (gnus-articles-in-thread
7654                    (gnus-id-to-thread (gnus-root-id id)))))
7655     (prog1
7656         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7657       (gnus-summary-limit-include-matching-articles
7658        "subject"
7659        (regexp-quote (gnus-simplify-subject-re
7660                       (mail-header-subject (gnus-id-to-header id)))))
7661       (gnus-summary-position-point))))
7662
7663 (defun gnus-summary-limit-include-matching-articles (header regexp)
7664   "Display all the hidden articles that have HEADERs that match REGEXP."
7665   (interactive (list (read-string "Match on header: ")
7666                      (read-string "Regexp: ")))
7667   (let ((articles (gnus-find-matching-articles header regexp)))
7668     (prog1
7669         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7670       (gnus-summary-position-point))))
7671
7672 (defun gnus-summary-insert-dormant-articles ()
7673   "Insert all the dormat articles for this group into the current buffer."
7674   (interactive)
7675   (let ((gnus-verbose (max 6 gnus-verbose)))
7676     (if (not gnus-newsgroup-dormant)
7677         (gnus-message 3 "No cached articles for this group")
7678       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7679
7680 (defun gnus-summary-limit-include-dormant ()
7681   "Display all the hidden articles that are marked as dormant.
7682 Note that this command only works on a subset of the articles currently
7683 fetched for this group."
7684   (interactive)
7685   (unless gnus-newsgroup-dormant
7686     (error "There are no dormant articles in this group"))
7687   (prog1
7688       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7689     (gnus-summary-position-point)))
7690
7691 (defun gnus-summary-limit-exclude-dormant ()
7692   "Hide all dormant articles."
7693   (interactive)
7694   (prog1
7695       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7696     (gnus-summary-position-point)))
7697
7698 (defun gnus-summary-limit-exclude-childless-dormant ()
7699   "Hide all dormant articles that have no children."
7700   (interactive)
7701   (let ((data (gnus-data-list t))
7702         articles d children)
7703     ;; Find all articles that are either not dormant or have
7704     ;; children.
7705     (while (setq d (pop data))
7706       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7707                 (and (setq children
7708                            (gnus-article-children (gnus-data-number d)))
7709                      (let (found)
7710                        (while children
7711                          (when (memq (car children) articles)
7712                            (setq children nil
7713                                  found t))
7714                          (pop children))
7715                        found)))
7716         (push (gnus-data-number d) articles)))
7717     ;; Do the limiting.
7718     (prog1
7719         (gnus-summary-limit articles)
7720       (gnus-summary-position-point))))
7721
7722 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7723   "Mark all unread excluded articles as read.
7724 If ALL, mark even excluded ticked and dormants as read."
7725   (interactive "P")
7726   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7727   (let ((articles (gnus-sorted-ndifference
7728                    (sort
7729                     (mapcar (lambda (h) (mail-header-number h))
7730                             gnus-newsgroup-headers)
7731                     '<)
7732                    gnus-newsgroup-limit))
7733         article)
7734     (setq gnus-newsgroup-unreads
7735           (gnus-sorted-intersection gnus-newsgroup-unreads
7736                                     gnus-newsgroup-limit))
7737     (if all
7738         (setq gnus-newsgroup-dormant nil
7739               gnus-newsgroup-marked nil
7740               gnus-newsgroup-reads
7741               (nconc
7742                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7743                gnus-newsgroup-reads))
7744       (while (setq article (pop articles))
7745         (unless (or (memq article gnus-newsgroup-dormant)
7746                     (memq article gnus-newsgroup-marked))
7747           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7748
7749 (defun gnus-summary-limit (articles &optional pop)
7750   (if pop
7751       ;; We pop the previous limit off the stack and use that.
7752       (setq articles (car gnus-newsgroup-limits)
7753             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7754     ;; We use the new limit, so we push the old limit on the stack.
7755     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7756   ;; Set the limit.
7757   (setq gnus-newsgroup-limit articles)
7758   (let ((total (length gnus-newsgroup-data))
7759         (data (gnus-data-find-list (gnus-summary-article-number)))
7760         (gnus-summary-mark-below nil)   ; Inhibit this.
7761         found)
7762     ;; This will do all the work of generating the new summary buffer
7763     ;; according to the new limit.
7764     (gnus-summary-prepare)
7765     ;; Hide any threads, possibly.
7766     (gnus-summary-maybe-hide-threads)
7767     ;; Try to return to the article you were at, or one in the
7768     ;; neighborhood.
7769     (when data
7770       ;; We try to find some article after the current one.
7771       (while data
7772         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7773           (setq data nil
7774                 found t))
7775         (setq data (cdr data))))
7776     (unless found
7777       ;; If there is no data, that means that we were after the last
7778       ;; article.  The same goes when we can't find any articles
7779       ;; after the current one.
7780       (goto-char (point-max))
7781       (gnus-summary-find-prev))
7782     (gnus-set-mode-line 'summary)
7783     ;; We return how many articles were removed from the summary
7784     ;; buffer as a result of the new limit.
7785     (- total (length gnus-newsgroup-data))))
7786
7787 (defsubst gnus-invisible-cut-children (threads)
7788   (let ((num 0))
7789     (while threads
7790       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7791         (incf num))
7792       (pop threads))
7793     (< num 2)))
7794
7795 (defsubst gnus-cut-thread (thread)
7796   "Go forwards in the thread until we find an article that we want to display."
7797   (when (or (eq gnus-fetch-old-headers 'some)
7798             (eq gnus-fetch-old-headers 'invisible)
7799             (numberp gnus-fetch-old-headers)
7800             (eq gnus-build-sparse-threads 'some)
7801             (eq gnus-build-sparse-threads 'more))
7802     ;; Deal with old-fetched headers and sparse threads.
7803     (while (and
7804             thread
7805             (or
7806              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7807              (gnus-summary-article-ancient-p
7808               (mail-header-number (car thread))))
7809             (if (or (<= (length (cdr thread)) 1)
7810                     (eq gnus-fetch-old-headers 'invisible))
7811                 (setq gnus-newsgroup-limit
7812                       (delq (mail-header-number (car thread))
7813                             gnus-newsgroup-limit)
7814                       thread (cadr thread))
7815               (when (gnus-invisible-cut-children (cdr thread))
7816                 (let ((th (cdr thread)))
7817                   (while th
7818                     (if (memq (mail-header-number (caar th))
7819                               gnus-newsgroup-limit)
7820                         (setq thread (car th)
7821                               th nil)
7822                       (setq th (cdr th))))))))))
7823   thread)
7824
7825 (defun gnus-cut-threads (threads)
7826   "Cut off all uninteresting articles from the beginning of threads."
7827   (when (or (eq gnus-fetch-old-headers 'some)
7828             (eq gnus-fetch-old-headers 'invisible)
7829             (numberp gnus-fetch-old-headers)
7830             (eq gnus-build-sparse-threads 'some)
7831             (eq gnus-build-sparse-threads 'more))
7832     (let ((th threads))
7833       (while th
7834         (setcar th (gnus-cut-thread (car th)))
7835         (setq th (cdr th)))))
7836   ;; Remove nixed out threads.
7837   (delq nil threads))
7838
7839 (defun gnus-summary-initial-limit (&optional show-if-empty)
7840   "Figure out what the initial limit is supposed to be on group entry.
7841 This entails weeding out unwanted dormants, low-scored articles,
7842 fetch-old-headers verbiage, and so on."
7843   ;; Most groups have nothing to remove.
7844   (if (or gnus-inhibit-limiting
7845           (and (null gnus-newsgroup-dormant)
7846                (eq gnus-newsgroup-display 'gnus-not-ignore)
7847                (not (eq gnus-fetch-old-headers 'some))
7848                (not (numberp gnus-fetch-old-headers))
7849                (not (eq gnus-fetch-old-headers 'invisible))
7850                (null gnus-summary-expunge-below)
7851                (not (eq gnus-build-sparse-threads 'some))
7852                (not (eq gnus-build-sparse-threads 'more))
7853                (null gnus-thread-expunge-below)
7854                (not gnus-use-nocem)))
7855       ()                                ; Do nothing.
7856     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7857     (setq gnus-newsgroup-limit nil)
7858     (mapatoms
7859      (lambda (node)
7860        (unless (car (symbol-value node))
7861          ;; These threads have no parents -- they are roots.
7862          (let ((nodes (cdr (symbol-value node)))
7863                thread)
7864            (while nodes
7865              (if (and gnus-thread-expunge-below
7866                       (< (gnus-thread-total-score (car nodes))
7867                          gnus-thread-expunge-below))
7868                  (gnus-expunge-thread (pop nodes))
7869                (setq thread (pop nodes))
7870                (gnus-summary-limit-children thread))))))
7871      gnus-newsgroup-dependencies)
7872     ;; If this limitation resulted in an empty group, we might
7873     ;; pop the previous limit and use it instead.
7874     (when (and (not gnus-newsgroup-limit)
7875                show-if-empty)
7876       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7877     gnus-newsgroup-limit))
7878
7879 (defun gnus-summary-limit-children (thread)
7880   "Return 1 if this subthread is visible and 0 if it is not."
7881   ;; First we get the number of visible children to this thread.  This
7882   ;; is done by recursing down the thread using this function, so this
7883   ;; will really go down to a leaf article first, before slowly
7884   ;; working its way up towards the root.
7885   (when thread
7886     (let* ((max-lisp-eval-depth 5000)
7887            (children
7888            (if (cdr thread)
7889                (apply '+ (mapcar 'gnus-summary-limit-children
7890                                  (cdr thread)))
7891              0))
7892           (number (mail-header-number (car thread)))
7893           score)
7894       (if (and
7895            (not (memq number gnus-newsgroup-marked))
7896            (or
7897             ;; If this article is dormant and has absolutely no visible
7898             ;; children, then this article isn't visible.
7899             (and (memq number gnus-newsgroup-dormant)
7900                  (zerop children))
7901             ;; If this is "fetch-old-headered" and there is no
7902             ;; visible children, then we don't want this article.
7903             (and (or (eq gnus-fetch-old-headers 'some)
7904                      (numberp gnus-fetch-old-headers))
7905                  (gnus-summary-article-ancient-p number)
7906                  (zerop children))
7907             ;; If this is "fetch-old-headered" and `invisible', then
7908             ;; we don't want this article.
7909             (and (eq gnus-fetch-old-headers 'invisible)
7910                  (gnus-summary-article-ancient-p number))
7911             ;; If this is a sparsely inserted article with no children,
7912             ;; we don't want it.
7913             (and (eq gnus-build-sparse-threads 'some)
7914                  (gnus-summary-article-sparse-p number)
7915                  (zerop children))
7916             ;; If we use expunging, and this article is really
7917             ;; low-scored, then we don't want this article.
7918             (when (and gnus-summary-expunge-below
7919                        (< (setq score
7920                                 (or (cdr (assq number gnus-newsgroup-scored))
7921                                     gnus-summary-default-score))
7922                           gnus-summary-expunge-below))
7923               ;; We increase the expunge-tally here, but that has
7924               ;; nothing to do with the limits, really.
7925               (incf gnus-newsgroup-expunged-tally)
7926               ;; We also mark as read here, if that's wanted.
7927               (when (and gnus-summary-mark-below
7928                          (< score gnus-summary-mark-below))
7929                 (setq gnus-newsgroup-unreads
7930                       (delq number gnus-newsgroup-unreads))
7931                 (if gnus-newsgroup-auto-expire
7932                     (push number gnus-newsgroup-expirable)
7933                   (push (cons number gnus-low-score-mark)
7934                         gnus-newsgroup-reads)))
7935               t)
7936             ;; Do the `display' group parameter.
7937             (and gnus-newsgroup-display
7938                  (not (funcall gnus-newsgroup-display)))
7939             ;; Check NoCeM things.
7940             (if (and gnus-use-nocem
7941                      (gnus-nocem-unwanted-article-p
7942                       (mail-header-id (car thread))))
7943                 (progn
7944                   (setq gnus-newsgroup-unreads
7945                         (delq number gnus-newsgroup-unreads))
7946                   t))))
7947           ;; Nope, invisible article.
7948           0
7949         ;; Ok, this article is to be visible, so we add it to the limit
7950         ;; and return 1.
7951         (push number gnus-newsgroup-limit)
7952         1))))
7953
7954 (defun gnus-expunge-thread (thread)
7955   "Mark all articles in THREAD as read."
7956   (let* ((number (mail-header-number (car thread))))
7957     (incf gnus-newsgroup-expunged-tally)
7958     ;; We also mark as read here, if that's wanted.
7959     (setq gnus-newsgroup-unreads
7960           (delq number gnus-newsgroup-unreads))
7961     (if gnus-newsgroup-auto-expire
7962         (push number gnus-newsgroup-expirable)
7963       (push (cons number gnus-low-score-mark)
7964             gnus-newsgroup-reads)))
7965   ;; Go recursively through all subthreads.
7966   (mapcar 'gnus-expunge-thread (cdr thread)))
7967
7968 ;; Summary article oriented commands
7969
7970 (defun gnus-summary-refer-parent-article (n)
7971   "Refer parent article N times.
7972 If N is negative, go to ancestor -N instead.
7973 The difference between N and the number of articles fetched is returned."
7974   (interactive "p")
7975   (let ((skip 1)
7976         error header ref)
7977     (when (not (natnump n))
7978       (setq skip (abs n)
7979             n 1))
7980     (while (and (> n 0)
7981                 (not error))
7982       (setq header (gnus-summary-article-header))
7983       (if (and (eq (mail-header-number header)
7984                    (cdr gnus-article-current))
7985                (equal gnus-newsgroup-name
7986                       (car gnus-article-current)))
7987           ;; If we try to find the parent of the currently
7988           ;; displayed article, then we take a look at the actual
7989           ;; References header, since this is slightly more
7990           ;; reliable than the References field we got from the
7991           ;; server.
7992           (save-excursion
7993             (set-buffer gnus-original-article-buffer)
7994             (nnheader-narrow-to-headers)
7995             (unless (setq ref (message-fetch-field "references"))
7996               (setq ref (message-fetch-field "in-reply-to")))
7997             (widen))
7998         (setq ref
7999               ;; It's not the current article, so we take a bet on
8000               ;; the value we got from the server.
8001               (mail-header-references header)))
8002       (if (and ref
8003                (not (equal ref "")))
8004           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8005             (gnus-message 1 "Couldn't find parent"))
8006         (gnus-message 1 "No references in article %d"
8007                       (gnus-summary-article-number))
8008         (setq error t))
8009       (decf n))
8010     (gnus-summary-position-point)
8011     n))
8012
8013 (defun gnus-summary-refer-references ()
8014   "Fetch all articles mentioned in the References header.
8015 Return the number of articles fetched."
8016   (interactive)
8017   (let ((ref (mail-header-references (gnus-summary-article-header)))
8018         (current (gnus-summary-article-number))
8019         (n 0))
8020     (if (or (not ref)
8021             (equal ref ""))
8022         (error "No References in the current article")
8023       ;; For each Message-ID in the References header...
8024       (while (string-match "<[^>]*>" ref)
8025         (incf n)
8026         ;; ... fetch that article.
8027         (gnus-summary-refer-article
8028          (prog1 (match-string 0 ref)
8029            (setq ref (substring ref (match-end 0))))))
8030       (gnus-summary-goto-subject current)
8031       (gnus-summary-position-point)
8032       n)))
8033
8034 (defun gnus-summary-refer-thread (&optional limit)
8035   "Fetch all articles in the current thread.
8036 If LIMIT (the numerical prefix), fetch that many old headers instead
8037 of what's specified by the `gnus-refer-thread-limit' variable."
8038   (interactive "P")
8039   (let ((id (mail-header-id (gnus-summary-article-header)))
8040         (limit (if limit (prefix-numeric-value limit)
8041                  gnus-refer-thread-limit)))
8042     (unless (eq gnus-fetch-old-headers 'invisible)
8043       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8044       ;; Retrieve the headers and read them in.
8045       (if (eq (if (numberp limit)
8046                   (gnus-retrieve-headers
8047                    (list (min
8048                           (+ (mail-header-number
8049                               (gnus-summary-article-header))
8050                              limit)
8051                           gnus-newsgroup-end))
8052                    gnus-newsgroup-name (* limit 2))
8053                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8054                 ;; headers.
8055                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8056                                        gnus-newsgroup-name limit))
8057               'nov)
8058           (gnus-build-all-threads)
8059         (error "Can't fetch thread from backends that don't support NOV"))
8060       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8061     (gnus-summary-limit-include-thread id)))
8062
8063 (defun gnus-summary-refer-article (message-id)
8064   "Fetch an article specified by MESSAGE-ID."
8065   (interactive "sMessage-ID: ")
8066   (when (and (stringp message-id)
8067              (not (zerop (length message-id))))
8068     ;; Construct the correct Message-ID if necessary.
8069     ;; Suggested by tale@pawl.rpi.edu.
8070     (unless (string-match "^<" message-id)
8071       (setq message-id (concat "<" message-id)))
8072     (unless (string-match ">$" message-id)
8073       (setq message-id (concat message-id ">")))
8074     (let* ((header (gnus-id-to-header message-id))
8075            (sparse (and header
8076                         (gnus-summary-article-sparse-p
8077                          (mail-header-number header))
8078                         (memq (mail-header-number header)
8079                               gnus-newsgroup-limit)))
8080            number)
8081       (cond
8082        ;; If the article is present in the buffer we just go to it.
8083        ((and header
8084              (or (not (gnus-summary-article-sparse-p
8085                        (mail-header-number header)))
8086                  sparse))
8087         (prog1
8088             (gnus-summary-goto-article
8089              (mail-header-number header) nil t)
8090           (when sparse
8091             (gnus-summary-update-article (mail-header-number header)))))
8092        (t
8093         ;; We fetch the article.
8094         (catch 'found
8095           (dolist (gnus-override-method (gnus-refer-article-methods))
8096             (when (and (gnus-check-server gnus-override-method)
8097                        ;; Fetch the header,
8098                        (setq number (gnus-summary-insert-subject message-id)))
8099               ;; and display the article.
8100               (gnus-summary-select-article nil nil nil number)
8101               (throw 'found t)))
8102           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8103
8104 (defun gnus-refer-article-methods ()
8105   "Return a list of referable methods."
8106   (cond
8107    ;; No method, so we default to current and native.
8108    ((null gnus-refer-article-method)
8109     (list gnus-current-select-method gnus-select-method))
8110    ;; Current.
8111    ((eq 'current gnus-refer-article-method)
8112     (list gnus-current-select-method))
8113    ;; List of select methods.
8114    ((not (and (symbolp (car gnus-refer-article-method))
8115               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8116     (let (out)
8117       (dolist (method gnus-refer-article-method)
8118         (push (if (eq 'current method)
8119                   gnus-current-select-method
8120                 method)
8121               out))
8122       (nreverse out)))
8123    ;; One single select method.
8124    (t
8125     (list gnus-refer-article-method))))
8126
8127 (defun gnus-summary-edit-parameters ()
8128   "Edit the group parameters of the current group."
8129   (interactive)
8130   (gnus-group-edit-group gnus-newsgroup-name 'params))
8131
8132 (defun gnus-summary-customize-parameters ()
8133   "Customize the group parameters of the current group."
8134   (interactive)
8135   (gnus-group-customize gnus-newsgroup-name))
8136
8137 (defun gnus-summary-enter-digest-group (&optional force)
8138   "Enter an nndoc group based on the current article.
8139 If FORCE, force a digest interpretation.  If not, try
8140 to guess what the document format is."
8141   (interactive "P")
8142   (let ((conf gnus-current-window-configuration))
8143     (save-excursion
8144       (gnus-summary-select-article))
8145     (setq gnus-current-window-configuration conf)
8146     (let* ((name (format "%s-%d"
8147                          (gnus-group-prefixed-name
8148                           gnus-newsgroup-name (list 'nndoc ""))
8149                          (save-excursion
8150                            (set-buffer gnus-summary-buffer)
8151                            gnus-current-article)))
8152            (ogroup gnus-newsgroup-name)
8153            (params (append (gnus-info-params (gnus-get-info ogroup))
8154                            (list (cons 'to-group ogroup))
8155                            (list (cons 'save-article-group ogroup))))
8156            (case-fold-search t)
8157            (buf (current-buffer))
8158            dig to-address)
8159       (save-excursion
8160         (set-buffer gnus-original-article-buffer)
8161         ;; Have the digest group inherit the main mail address of
8162         ;; the parent article.
8163         (when (setq to-address (or (gnus-fetch-field "reply-to")
8164                                    (gnus-fetch-field "from")))
8165           (setq params (append
8166                         (list (cons 'to-address
8167                                     (funcall gnus-decode-encoded-word-function
8168                                              to-address))))))
8169         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8170         (insert-buffer-substring gnus-original-article-buffer)
8171         ;; Remove lines that may lead nndoc to misinterpret the
8172         ;; document type.
8173         (narrow-to-region
8174          (goto-char (point-min))
8175          (or (search-forward "\n\n" nil t) (point)))
8176         (goto-char (point-min))
8177         (delete-matching-lines "^Path:\\|^From ")
8178         (widen))
8179       (unwind-protect
8180           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8181                     (gnus-newsgroup-ephemeral-ignored-charsets
8182                      gnus-newsgroup-ignored-charsets))
8183                 (gnus-group-read-ephemeral-group
8184                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8185                               (nndoc-article-type
8186                                ,(if force 'mbox 'guess)))
8187                  t nil nil nil
8188                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8189                                                         "ADAPT")))))
8190               ;; Make all postings to this group go to the parent group.
8191               (nconc (gnus-info-params (gnus-get-info name))
8192                      params)
8193             ;; Couldn't select this doc group.
8194             (switch-to-buffer buf)
8195             (gnus-set-global-variables)
8196             (gnus-configure-windows 'summary)
8197             (gnus-message 3 "Article couldn't be entered?"))
8198         (kill-buffer dig)))))
8199
8200 (defun gnus-summary-read-document (n)
8201   "Open a new group based on the current article(s).
8202 This will allow you to read digests and other similar
8203 documents as newsgroups.
8204 Obeys the standard process/prefix convention."
8205   (interactive "P")
8206   (let* ((articles (gnus-summary-work-articles n))
8207          (ogroup gnus-newsgroup-name)
8208          (params (append (gnus-info-params (gnus-get-info ogroup))
8209                          (list (cons 'to-group ogroup))))
8210          article group egroup groups vgroup)
8211     (while (setq article (pop articles))
8212       (setq group (format "%s-%d" gnus-newsgroup-name article))
8213       (gnus-summary-remove-process-mark article)
8214       (when (gnus-summary-display-article article)
8215         (save-excursion
8216           (with-temp-buffer
8217             (insert-buffer-substring gnus-original-article-buffer)
8218             ;; Remove some headers that may lead nndoc to make
8219             ;; the wrong guess.
8220             (message-narrow-to-head)
8221             (goto-char (point-min))
8222             (delete-matching-lines "^\\(Path\\):\\|^From ")
8223             (widen)
8224             (if (setq egroup
8225                       (gnus-group-read-ephemeral-group
8226                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8227                                      (nndoc-article-type guess))
8228                        t nil t))
8229                 (progn
8230             ;; Make all postings to this group go to the parent group.
8231                   (nconc (gnus-info-params (gnus-get-info egroup))
8232                          params)
8233                   (push egroup groups))
8234               ;; Couldn't select this doc group.
8235               (gnus-error 3 "Article couldn't be entered"))))))
8236     ;; Now we have selected all the documents.
8237     (cond
8238      ((not groups)
8239       (error "None of the articles could be interpreted as documents"))
8240      ((gnus-group-read-ephemeral-group
8241        (setq vgroup (format
8242                      "nnvirtual:%s-%s" gnus-newsgroup-name
8243                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8244        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8245        t
8246        (cons (current-buffer) 'summary)))
8247      (t
8248       (error "Couldn't select virtual nndoc group")))))
8249
8250 (defun gnus-summary-isearch-article (&optional regexp-p)
8251   "Do incremental search forward on the current article.
8252 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8253   (interactive "P")
8254   (gnus-summary-select-article)
8255   (gnus-configure-windows 'article)
8256   (gnus-eval-in-buffer-window gnus-article-buffer
8257     (save-restriction
8258       (widen)
8259       (isearch-forward regexp-p))))
8260
8261 (defun gnus-summary-search-article-forward (regexp &optional backward)
8262   "Search for an article containing REGEXP forward.
8263 If BACKWARD, search backward instead."
8264   (interactive
8265    (list (read-string
8266           (format "Search article %s (regexp%s): "
8267                   (if current-prefix-arg "backward" "forward")
8268                   (if gnus-last-search-regexp
8269                       (concat ", default " gnus-last-search-regexp)
8270                     "")))
8271          current-prefix-arg))
8272   (if (string-equal regexp "")
8273       (setq regexp (or gnus-last-search-regexp ""))
8274     (setq gnus-last-search-regexp regexp)
8275     (setq gnus-article-before-search gnus-current-article))
8276   ;; Intentionally set gnus-last-article.
8277   (setq gnus-last-article gnus-article-before-search)
8278   (let ((gnus-last-article gnus-last-article))
8279     (if (gnus-summary-search-article regexp backward)
8280         (gnus-summary-show-thread)
8281       (error "Search failed: \"%s\"" regexp))))
8282
8283 (defun gnus-summary-search-article-backward (regexp)
8284   "Search for an article containing REGEXP backward."
8285   (interactive
8286    (list (read-string
8287           (format "Search article backward (regexp%s): "
8288                   (if gnus-last-search-regexp
8289                       (concat ", default " gnus-last-search-regexp)
8290                     "")))))
8291   (gnus-summary-search-article-forward regexp 'backward))
8292
8293 (defun gnus-summary-search-article (regexp &optional backward)
8294   "Search for an article containing REGEXP.
8295 Optional argument BACKWARD means do search for backward.
8296 `gnus-select-article-hook' is not called during the search."
8297   ;; We have to require this here to make sure that the following
8298   ;; dynamic binding isn't shadowed by autoloading.
8299   (require 'gnus-async)
8300   (require 'gnus-art)
8301   (let ((gnus-select-article-hook nil)  ;Disable hook.
8302         (gnus-article-prepare-hook nil)
8303         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8304         (gnus-use-article-prefetch nil)
8305         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8306         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8307         (gnus-visual nil)
8308         (gnus-keep-backlog nil)
8309         (gnus-break-pages nil)
8310         (gnus-summary-display-arrow nil)
8311         (gnus-updated-mode-lines nil)
8312         (gnus-auto-center-summary nil)
8313         (sum (current-buffer))
8314         (gnus-display-mime-function nil)
8315         (found nil)
8316         point)
8317     (gnus-save-hidden-threads
8318       (gnus-summary-select-article)
8319       (set-buffer gnus-article-buffer)
8320       (goto-char (window-point (get-buffer-window (current-buffer))))
8321       (when backward
8322         (forward-line -1))
8323       (while (not found)
8324         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8325         (if (if backward
8326                 (re-search-backward regexp nil t)
8327               (re-search-forward regexp nil t))
8328             ;; We found the regexp.
8329             (progn
8330               (setq found 'found)
8331               (beginning-of-line)
8332               (set-window-start
8333                (get-buffer-window (current-buffer))
8334                (point))
8335               (forward-line 1)
8336               (set-window-point
8337                (get-buffer-window (current-buffer))
8338                (point))
8339               (set-buffer sum)
8340               (setq point (point)))
8341           ;; We didn't find it, so we go to the next article.
8342           (set-buffer sum)
8343           (setq found 'not)
8344           (while (eq found 'not)
8345             (if (not (if backward (gnus-summary-find-prev)
8346                        (gnus-summary-find-next)))
8347                 ;; No more articles.
8348                 (setq found t)
8349               ;; Select the next article and adjust point.
8350               (unless (gnus-summary-article-sparse-p
8351                        (gnus-summary-article-number))
8352                 (setq found nil)
8353                 (gnus-summary-select-article)
8354                 (set-buffer gnus-article-buffer)
8355                 (widen)
8356                 (goto-char (if backward (point-max) (point-min))))))))
8357       (gnus-message 7 ""))
8358     ;; Return whether we found the regexp.
8359     (when (eq found 'found)
8360       (goto-char point)
8361       (gnus-summary-show-thread)
8362       (gnus-summary-goto-subject gnus-current-article)
8363       (gnus-summary-position-point)
8364       t)))
8365
8366 (defun gnus-find-matching-articles (header regexp)
8367   "Return a list of all articles that match REGEXP on HEADER.
8368 This search includes all articles in the current group that Gnus has
8369 fetched headers for, whether they are displayed or not."
8370   (let ((articles nil)
8371         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8372         (case-fold-search t))
8373     (dolist (header gnus-newsgroup-headers)
8374       (when (string-match regexp (funcall func header))
8375         (push (mail-header-number header) articles)))
8376     (nreverse articles)))
8377
8378 (defun gnus-summary-find-matching (header regexp &optional backward unread
8379                                           not-case-fold not-matching)
8380   "Return a list of all articles that match REGEXP on HEADER.
8381 The search stars on the current article and goes forwards unless
8382 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8383 If UNREAD is non-nil, only unread articles will
8384 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8385 in the comparisons. If NOT-MATCHING, return a list of all articles that
8386 not match REGEXP on HEADER."
8387   (let ((case-fold-search (not not-case-fold))
8388         articles d func)
8389     (if (consp header)
8390         (if (eq (car header) 'extra)
8391             (setq func
8392                   `(lambda (h)
8393                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8394                          "")))
8395           (error "%s is an invalid header" header))
8396       (unless (fboundp (intern (concat "mail-header-" header)))
8397         (error "%s is not a valid header" header))
8398       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8399     (dolist (d (if (eq backward 'all)
8400                    gnus-newsgroup-data
8401                  (gnus-data-find-list
8402                   (gnus-summary-article-number)
8403                   (gnus-data-list backward))))
8404       (when (and (or (not unread)       ; We want all articles...
8405                      (gnus-data-unread-p d)) ; Or just unreads.
8406                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8407                  (if not-matching
8408                      (not (string-match
8409                            regexp
8410                            (funcall func (gnus-data-header d))))
8411                    (string-match regexp
8412                                  (funcall func (gnus-data-header d)))))
8413         (push (gnus-data-number d) articles))) ; Success!
8414     (nreverse articles)))
8415
8416 (defun gnus-summary-execute-command (header regexp command &optional backward)
8417   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8418 If HEADER is an empty string (or nil), the match is done on the entire
8419 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8420   (interactive
8421    (list (let ((completion-ignore-case t))
8422            (completing-read
8423             "Header name: "
8424             (mapcar (lambda (header) (list (format "%s" header)))
8425                     (append
8426                      '("Number" "Subject" "From" "Lines" "Date"
8427                        "Message-ID" "Xref" "References" "Body")
8428                      gnus-extra-headers))
8429             nil 'require-match))
8430          (read-string "Regexp: ")
8431          (read-key-sequence "Command: ")
8432          current-prefix-arg))
8433   (when (equal header "Body")
8434     (setq header ""))
8435   ;; Hidden thread subtrees must be searched as well.
8436   (gnus-summary-show-all-threads)
8437   ;; We don't want to change current point nor window configuration.
8438   (save-excursion
8439     (save-window-excursion
8440       (let (gnus-visual
8441             gnus-treat-strip-trailing-blank-lines
8442             gnus-treat-strip-leading-blank-lines
8443             gnus-treat-strip-multiple-blank-lines
8444             gnus-treat-hide-boring-headers
8445             gnus-treat-fold-newsgroups
8446             gnus-article-prepare-hook)
8447         (gnus-message 6 "Executing %s..." (key-description command))
8448         ;; We'd like to execute COMMAND interactively so as to give arguments.
8449         (gnus-execute header regexp
8450                       `(call-interactively ',(key-binding command))
8451                       backward)
8452         (gnus-message 6 "Executing %s...done" (key-description command))))))
8453
8454 (defun gnus-summary-beginning-of-article ()
8455   "Scroll the article back to the beginning."
8456   (interactive)
8457   (gnus-summary-select-article)
8458   (gnus-configure-windows 'article)
8459   (gnus-eval-in-buffer-window gnus-article-buffer
8460     (widen)
8461     (goto-char (point-min))
8462     (when gnus-page-broken
8463       (gnus-narrow-to-page))))
8464
8465 (defun gnus-summary-end-of-article ()
8466   "Scroll to the end of the article."
8467   (interactive)
8468   (gnus-summary-select-article)
8469   (gnus-configure-windows 'article)
8470   (gnus-eval-in-buffer-window gnus-article-buffer
8471     (widen)
8472     (goto-char (point-max))
8473     (recenter -3)
8474     (when gnus-page-broken
8475       (gnus-narrow-to-page))))
8476
8477 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8478   "Truncate to LEN and quote all \"(\"'s in STRING."
8479   (gnus-replace-in-string (if (and len (> (length string) len))
8480                               (substring string 0 len)
8481                             string)
8482                           "[()]" "\\\\\\&"))
8483
8484 (defun gnus-summary-print-article (&optional filename n)
8485   "Generate and print a PostScript image of the N next (mail) articles.
8486
8487 If N is negative, print the N previous articles.  If N is nil and articles
8488 have been marked with the process mark, print these instead.
8489
8490 If the optional first argument FILENAME is nil, send the image to the
8491 printer.  If FILENAME is a string, save the PostScript image in a file with
8492 that name.  If FILENAME is a number, prompt the user for the name of the file
8493 to save in."
8494   (interactive (list (ps-print-preprint current-prefix-arg)))
8495   (dolist (article (gnus-summary-work-articles n))
8496     (gnus-summary-select-article nil nil 'pseudo article)
8497     (gnus-eval-in-buffer-window gnus-article-buffer
8498       (gnus-print-buffer))
8499     (gnus-summary-remove-process-mark article))
8500   (ps-despool filename))
8501
8502 (defun gnus-print-buffer ()
8503   (let ((buffer (generate-new-buffer " *print*")))
8504     (unwind-protect
8505         (progn
8506           (copy-to-buffer buffer (point-min) (point-max))
8507           (set-buffer buffer)
8508           (gnus-article-delete-invisible-text)
8509           (gnus-remove-text-with-property 'gnus-decoration)
8510           (when (gnus-visual-p 'article-highlight 'highlight)
8511             ;; Copy-to-buffer doesn't copy overlay.  So redo
8512             ;; highlight.
8513             (let ((gnus-article-buffer buffer))
8514               (gnus-article-highlight-citation t)
8515               (gnus-article-highlight-signature)))
8516           (let ((ps-left-header
8517                  (list
8518                   (concat "("
8519                           (gnus-summary-print-truncate-and-quote
8520                            (mail-header-subject gnus-current-headers)
8521                            66) ")")
8522                   (concat "("
8523                           (gnus-summary-print-truncate-and-quote
8524                            (mail-header-from gnus-current-headers)
8525                            45) ")")))
8526                 (ps-right-header
8527                  (list
8528                   "/pagenumberstring load"
8529                   (concat "("
8530                           (mail-header-date gnus-current-headers) ")"))))
8531             (gnus-run-hooks 'gnus-ps-print-hook)
8532             (save-excursion
8533               (if window-system
8534                   (ps-spool-buffer-with-faces)
8535                 (ps-spool-buffer)))))
8536       (kill-buffer buffer))))
8537
8538 (defun gnus-summary-show-article (&optional arg)
8539   "Force redisplaying of the current article.
8540 If ARG (the prefix) is a number, show the article with the charset
8541 defined in `gnus-summary-show-article-charset-alist', or the charset
8542 input.
8543 If ARG (the prefix) is non-nil and not a number, show the raw article
8544 without any article massaging functions being run.  Normally, the key
8545 strokes are `C-u g'."
8546   (interactive "P")
8547   (cond
8548    ((numberp arg)
8549     (gnus-summary-show-article t)
8550     (let ((gnus-newsgroup-charset
8551            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8552                (mm-read-coding-system
8553                 "View as charset: " ;; actually it is coding system.
8554                 (save-excursion
8555                   (set-buffer gnus-article-buffer)
8556                   (mm-detect-coding-region (point) (point-max))))))
8557           (gnus-newsgroup-ignored-charsets 'gnus-all))
8558       (gnus-summary-select-article nil 'force)
8559       (let ((deps gnus-newsgroup-dependencies)
8560             head header lines)
8561         (save-excursion
8562           (set-buffer gnus-original-article-buffer)
8563           (save-restriction
8564             (message-narrow-to-head)
8565             (setq head (buffer-string))
8566             (goto-char (point-min))
8567             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8568               (goto-char (point-max))
8569               (widen)
8570               (setq lines (1- (count-lines (point) (point-max))))))
8571           (with-temp-buffer
8572             (insert (format "211 %d Article retrieved.\n"
8573                             (cdr gnus-article-current)))
8574             (insert head)
8575             (if lines (insert (format "Lines: %d\n" lines)))
8576             (insert ".\n")
8577             (let ((nntp-server-buffer (current-buffer)))
8578               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8579         (gnus-data-set-header
8580          (gnus-data-find (cdr gnus-article-current))
8581          header)
8582         (gnus-summary-update-article-line
8583          (cdr gnus-article-current) header)
8584         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8585           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8586    ((not arg)
8587     ;; Select the article the normal way.
8588     (gnus-summary-select-article nil 'force))
8589    (t
8590     ;; We have to require this here to make sure that the following
8591     ;; dynamic binding isn't shadowed by autoloading.
8592     (require 'gnus-async)
8593     (require 'gnus-art)
8594     ;; Bind the article treatment functions to nil.
8595     (let ((gnus-have-all-headers t)
8596           gnus-article-prepare-hook
8597           gnus-article-decode-hook
8598           gnus-display-mime-function
8599           gnus-break-pages)
8600       ;; Destroy any MIME parts.
8601       (when (gnus-buffer-live-p gnus-article-buffer)
8602         (save-excursion
8603           (set-buffer gnus-article-buffer)
8604           (mm-destroy-parts gnus-article-mime-handles)
8605           ;; Set it to nil for safety reason.
8606           (setq gnus-article-mime-handle-alist nil)
8607           (setq gnus-article-mime-handles nil)))
8608       (gnus-summary-select-article nil 'force))))
8609   (gnus-summary-goto-subject gnus-current-article)
8610   (gnus-summary-position-point))
8611
8612 (defun gnus-summary-show-raw-article ()
8613   "Show the raw article without any article massaging functions being run."
8614   (interactive)
8615   (gnus-summary-show-article t))
8616
8617 (defun gnus-summary-verbose-headers (&optional arg)
8618   "Toggle permanent full header display.
8619 If ARG is a positive number, turn header display on.
8620 If ARG is a negative number, turn header display off."
8621   (interactive "P")
8622   (setq gnus-show-all-headers
8623         (cond ((or (not (numberp arg))
8624                    (zerop arg))
8625                (not gnus-show-all-headers))
8626               ((natnump arg)
8627                t)))
8628   (gnus-summary-show-article))
8629
8630 (defun gnus-summary-toggle-header (&optional arg)
8631   "Show the headers if they are hidden, or hide them if they are shown.
8632 If ARG is a positive number, show the entire header.
8633 If ARG is a negative number, hide the unwanted header lines."
8634   (interactive "P")
8635   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8636                      (get-buffer-window gnus-article-buffer t))))
8637     (with-current-buffer gnus-article-buffer
8638       (widen)
8639       (article-narrow-to-head)
8640       (let* ((buffer-read-only nil)
8641              (inhibit-point-motion-hooks t)
8642              (hidden (if (numberp arg)
8643                          (>= arg 0)
8644                        (gnus-article-hidden-text-p 'headers)))
8645              s e)
8646         (delete-region (point-min) (point-max))
8647         (with-current-buffer gnus-original-article-buffer
8648           (goto-char (setq s (point-min)))
8649           (setq e (if (search-forward "\n\n" nil t)
8650                       (1- (point))
8651                     (point-max))))
8652         (insert-buffer-substring gnus-original-article-buffer s e)
8653         (run-hooks 'gnus-article-decode-hook)
8654         (if hidden
8655             (let ((gnus-treat-hide-headers nil)
8656                   (gnus-treat-hide-boring-headers nil))
8657               (gnus-delete-wash-type 'headers)
8658               (gnus-treat-article 'head))
8659           (gnus-treat-article 'head))
8660         (widen)
8661         (if window
8662             (set-window-start window (goto-char (point-min))))
8663         (setq gnus-page-broken
8664               (when gnus-break-pages
8665                 (gnus-narrow-to-page)
8666                 t))
8667         (gnus-set-mode-line 'article)))))
8668
8669 (defun gnus-summary-show-all-headers ()
8670   "Make all header lines visible."
8671   (interactive)
8672   (gnus-summary-toggle-header 1))
8673
8674 (defun gnus-summary-caesar-message (&optional arg)
8675   "Caesar rotate the current article by 13.
8676 The numerical prefix specifies how many places to rotate each letter
8677 forward."
8678   (interactive "P")
8679   (gnus-summary-select-article)
8680   (let ((mail-header-separator ""))
8681     (gnus-eval-in-buffer-window gnus-article-buffer
8682       (save-restriction
8683         (widen)
8684         (let ((start (window-start))
8685               buffer-read-only)
8686           (message-caesar-buffer-body arg)
8687           (set-window-start (get-buffer-window (current-buffer)) start))))))
8688
8689 (autoload 'unmorse-region "morse"
8690   "Convert morse coded text in region to ordinary ASCII text."
8691   t)
8692
8693 (defun gnus-summary-morse-message (&optional arg)
8694   "Morse decode the current article."
8695   (interactive "P")
8696   (gnus-summary-select-article)
8697   (let ((mail-header-separator ""))
8698     (gnus-eval-in-buffer-window gnus-article-buffer
8699       (save-excursion
8700         (save-restriction
8701           (widen)
8702           (let ((pos (window-start))
8703                 buffer-read-only)
8704             (goto-char (point-min))
8705             (when (message-goto-body)
8706               (gnus-narrow-to-body))
8707             (goto-char (point-min))
8708             (while (re-search-forward "·" (point-max) t)
8709               (replace-match "."))
8710             (unmorse-region (point-min) (point-max))
8711             (widen)
8712             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8713
8714 (defun gnus-summary-stop-page-breaking ()
8715   "Stop page breaking in the current article."
8716   (interactive)
8717   (gnus-summary-select-article)
8718   (gnus-eval-in-buffer-window gnus-article-buffer
8719     (widen)
8720     (when (gnus-visual-p 'page-marker)
8721       (let ((buffer-read-only nil))
8722         (gnus-remove-text-with-property 'gnus-prev)
8723         (gnus-remove-text-with-property 'gnus-next))
8724       (setq gnus-page-broken nil))))
8725
8726 (defun gnus-summary-move-article (&optional n to-newsgroup
8727                                             select-method action)
8728   "Move the current article to a different newsgroup.
8729 If N is a positive number, move the N next articles.
8730 If N is a negative number, move the N previous articles.
8731 If N is nil and any articles have been marked with the process mark,
8732 move those articles instead.
8733 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8734 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8735 re-spool using this method.
8736
8737 When called interactively with TO-NEWSGROUP being nil, the value of
8738 the variable `gnus-move-split-methods' is used for finding a default
8739 for the target newsgroup.
8740
8741 For this function to work, both the current newsgroup and the
8742 newsgroup that you want to move to have to support the `request-move'
8743 and `request-accept' functions.
8744
8745 ACTION can be either `move' (the default), `crosspost' or `copy'."
8746   (interactive "P")
8747   (unless action
8748     (setq action 'move))
8749   ;; Check whether the source group supports the required functions.
8750   (cond ((and (eq action 'move)
8751               (not (gnus-check-backend-function
8752                     'request-move-article gnus-newsgroup-name)))
8753          (error "The current group does not support article moving"))
8754         ((and (eq action 'crosspost)
8755               (not (gnus-check-backend-function
8756                     'request-replace-article gnus-newsgroup-name)))
8757          (error "The current group does not support article editing")))
8758   (let ((articles (gnus-summary-work-articles n))
8759         (prefix (if (gnus-check-backend-function
8760                      'request-move-article gnus-newsgroup-name)
8761                     (gnus-group-real-prefix gnus-newsgroup-name)
8762                   ""))
8763         (names '((move "Move" "Moving")
8764                  (copy "Copy" "Copying")
8765                  (crosspost "Crosspost" "Crossposting")))
8766         (copy-buf (save-excursion
8767                     (nnheader-set-temp-buffer " *copy article*")))
8768         art-group to-method new-xref article to-groups)
8769     (unless (assq action names)
8770       (error "Unknown action %s" action))
8771     ;; Read the newsgroup name.
8772     (when (and (not to-newsgroup)
8773                (not select-method))
8774       (if (and gnus-move-split-methods
8775                (not
8776                 (and (memq gnus-current-article articles)
8777                      (gnus-buffer-live-p gnus-original-article-buffer))))
8778           ;; When `gnus-move-split-methods' is non-nil, we have to
8779           ;; select an article to give `gnus-read-move-group-name' an
8780           ;; opportunity to suggest an appropriate default.  However,
8781           ;; we needn't render or mark the article.
8782           (let ((gnus-display-mime-function nil)
8783                 (gnus-article-prepare-hook nil)
8784                 (gnus-mark-article-hook nil))
8785             (gnus-summary-select-article nil nil nil (car articles))))
8786       (setq to-newsgroup
8787             (gnus-read-move-group-name
8788              (cadr (assq action names))
8789              (symbol-value (intern (format "gnus-current-%s-group" action)))
8790              articles prefix))
8791       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8792     (setq to-method (or select-method
8793                         (gnus-server-to-method
8794                          (gnus-group-method to-newsgroup))))
8795     ;; Check the method we are to move this article to...
8796     (unless (gnus-check-backend-function
8797              'request-accept-article (car to-method))
8798       (error "%s does not support article copying" (car to-method)))
8799     (unless (gnus-check-server to-method)
8800       (error "Can't open server %s" (car to-method)))
8801     (gnus-message 6 "%s to %s: %s..."
8802                   (caddr (assq action names))
8803                   (or (car select-method) to-newsgroup) articles)
8804     (while articles
8805       (setq article (pop articles))
8806       (setq
8807        art-group
8808        (cond
8809         ;; Move the article.
8810         ((eq action 'move)
8811          ;; Remove this article from future suppression.
8812          (gnus-dup-unsuppress-article article)
8813          (gnus-request-move-article
8814           article                       ; Article to move
8815           gnus-newsgroup-name           ; From newsgroup
8816           (nth 1 (gnus-find-method-for-group
8817                   gnus-newsgroup-name)) ; Server
8818           (list 'gnus-request-accept-article
8819                 to-newsgroup (list 'quote select-method)
8820                 (not articles) t)       ; Accept form
8821           (not articles)))              ; Only save nov last time
8822         ;; Copy the article.
8823         ((eq action 'copy)
8824          (save-excursion
8825            (set-buffer copy-buf)
8826            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8827              (gnus-request-accept-article
8828               to-newsgroup select-method (not articles) t))))
8829         ;; Crosspost the article.
8830         ((eq action 'crosspost)
8831          (let ((xref (message-tokenize-header
8832                       (mail-header-xref (gnus-summary-article-header article))
8833                       " ")))
8834            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8835                                   ":" (number-to-string article)))
8836            (unless xref
8837              (setq xref (list (system-name))))
8838            (setq new-xref
8839                  (concat
8840                   (mapconcat 'identity
8841                              (delete "Xref:" (delete new-xref xref))
8842                              " ")
8843                   " " new-xref))
8844            (save-excursion
8845              (set-buffer copy-buf)
8846              ;; First put the article in the destination group.
8847              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8848              (when (consp (setq art-group
8849                                 (gnus-request-accept-article
8850                                  to-newsgroup select-method (not articles))))
8851                (setq new-xref (concat new-xref " " (car art-group)
8852                                       ":"
8853                                       (number-to-string (cdr art-group))))
8854                ;; Now we have the new Xrefs header, so we insert
8855                ;; it and replace the new article.
8856                (nnheader-replace-header "Xref" new-xref)
8857                (gnus-request-replace-article
8858                 (cdr art-group) to-newsgroup (current-buffer))
8859                art-group))))))
8860       (cond
8861        ((not art-group)
8862         (gnus-message 1 "Couldn't %s article %s: %s"
8863                       (cadr (assq action names)) article
8864                       (nnheader-get-report (car to-method))))
8865        ((eq art-group 'junk)
8866         (when (eq action 'move)
8867           (gnus-summary-mark-article article gnus-canceled-mark)
8868           (gnus-message 4 "Deleted article %s" article)
8869           ;; run the delete hook
8870           (run-hook-with-args 'gnus-summary-article-delete-hook
8871                               action
8872                               (gnus-data-header
8873                                (assoc article (gnus-data-list nil)))
8874                               gnus-newsgroup-name nil
8875                               select-method)))
8876        (t
8877         (let* ((pto-group (gnus-group-prefixed-name
8878                            (car art-group) to-method))
8879                (entry
8880                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8881                (info (nth 2 entry))
8882                (to-group (gnus-info-group info))
8883                to-marks)
8884           ;; Update the group that has been moved to.
8885           (when (and info
8886                      (memq action '(move copy)))
8887             (unless (member to-group to-groups)
8888               (push to-group to-groups))
8889
8890             (unless (memq article gnus-newsgroup-unreads)
8891               (push 'read to-marks)
8892               (gnus-info-set-read
8893                info (gnus-add-to-range (gnus-info-read info)
8894                                        (list (cdr art-group)))))
8895
8896             ;; See whether the article is to be put in the cache.
8897             (let ((marks gnus-article-mark-lists)
8898                   (to-article (cdr art-group)))
8899
8900               ;; Enter the article into the cache in the new group,
8901               ;; if that is required.
8902               (when gnus-use-cache
8903                 (gnus-cache-possibly-enter-article
8904                  to-group to-article
8905                  (memq article gnus-newsgroup-marked)
8906                  (memq article gnus-newsgroup-dormant)
8907                  (memq article gnus-newsgroup-unreads)))
8908
8909               (when gnus-preserve-marks
8910                 ;; Copy any marks over to the new group.
8911                 (when (and (equal to-group gnus-newsgroup-name)
8912                            (not (memq article gnus-newsgroup-unreads)))
8913                   ;; Mark this article as read in this group.
8914                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8915                   (setcdr (gnus-active to-group) to-article)
8916                   (setcdr gnus-newsgroup-active to-article))
8917
8918                 (while marks
8919                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8920                     (when (memq article (symbol-value
8921                                          (intern (format "gnus-newsgroup-%s"
8922                                                          (caar marks)))))
8923                       (push (cdar marks) to-marks)
8924                       ;; If the other group is the same as this group,
8925                       ;; then we have to add the mark to the list.
8926                       (when (equal to-group gnus-newsgroup-name)
8927                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8928                              (cons to-article
8929                                    (symbol-value
8930                                     (intern (format "gnus-newsgroup-%s"
8931                                                     (caar marks)))))))
8932                       ;; Copy the marks to other group.
8933                       (gnus-add-marked-articles
8934                        to-group (cdar marks) (list to-article) info)))
8935                   (setq marks (cdr marks)))
8936
8937                 (gnus-request-set-mark
8938                  to-group (list (list (list to-article) 'add to-marks))))
8939
8940               (gnus-dribble-enter
8941                (concat "(gnus-group-set-info '"
8942                        (gnus-prin1-to-string (gnus-get-info to-group))
8943                        ")"))))
8944
8945           ;; Update the Xref header in this article to point to
8946           ;; the new crossposted article we have just created.
8947           (when (eq action 'crosspost)
8948             (save-excursion
8949               (set-buffer copy-buf)
8950               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8951               (nnheader-replace-header "Xref" new-xref)
8952               (gnus-request-replace-article
8953                article gnus-newsgroup-name (current-buffer))))
8954
8955           ;; run the move/copy/crosspost/respool hook
8956           (run-hook-with-args 'gnus-summary-article-move-hook 
8957                               action
8958                               (gnus-data-header 
8959                                (assoc article (gnus-data-list nil)))
8960                               gnus-newsgroup-name
8961                               to-newsgroup
8962                               select-method))
8963
8964         ;;;!!!Why is this necessary?
8965         (set-buffer gnus-summary-buffer)
8966         
8967         (gnus-summary-goto-subject article)
8968         (when (eq action 'move)
8969           (gnus-summary-mark-article article gnus-canceled-mark))))
8970       (gnus-summary-remove-process-mark article))
8971     ;; Re-activate all groups that have been moved to.
8972     (save-excursion
8973       (set-buffer gnus-group-buffer)
8974       (let ((gnus-group-marked to-groups))
8975         (gnus-group-get-new-news-this-group nil t)))
8976
8977     (gnus-kill-buffer copy-buf)
8978     (gnus-summary-position-point)
8979     (gnus-set-mode-line 'summary)))
8980
8981 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8982   "Move the current article to a different newsgroup.
8983 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8984 When called interactively, if TO-NEWSGROUP is nil, use the value of
8985 the variable `gnus-move-split-methods' for finding a default target
8986 newsgroup.
8987 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8988 re-spool using this method."
8989   (interactive "P")
8990   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8991
8992 (defun gnus-summary-crosspost-article (&optional n)
8993   "Crosspost the current article to some other group."
8994   (interactive "P")
8995   (gnus-summary-move-article n nil nil 'crosspost))
8996
8997 (defcustom gnus-summary-respool-default-method nil
8998   "Default method type for respooling an article.
8999 If nil, use to the current newsgroup method."
9000   :type 'symbol
9001   :group 'gnus-summary-mail)
9002
9003 (defcustom gnus-summary-display-while-building nil
9004   "If non-nil, show and update the summary buffer as it's being built.
9005 If the value is t, update the buffer after every line is inserted.  If
9006 the value is an integer (N), update the display every N lines."
9007   :group 'gnus-thread
9008   :type '(choice (const :tag "off" nil)
9009                  number
9010                  (const :tag "frequently" t)))
9011
9012 (defun gnus-summary-respool-article (&optional n method)
9013   "Respool the current article.
9014 The article will be squeezed through the mail spooling process again,
9015 which means that it will be put in some mail newsgroup or other
9016 depending on `nnmail-split-methods'.
9017 If N is a positive number, respool the N next articles.
9018 If N is a negative number, respool the N previous articles.
9019 If N is nil and any articles have been marked with the process mark,
9020 respool those articles instead.
9021
9022 Respooling can be done both from mail groups and \"real\" newsgroups.
9023 In the former case, the articles in question will be moved from the
9024 current group into whatever groups they are destined to.  In the
9025 latter case, they will be copied into the relevant groups."
9026   (interactive
9027    (list current-prefix-arg
9028          (let* ((methods (gnus-methods-using 'respool))
9029                 (methname
9030                  (symbol-name (or gnus-summary-respool-default-method
9031                                   (car (gnus-find-method-for-group
9032                                         gnus-newsgroup-name)))))
9033                 (method
9034                  (gnus-completing-read-with-default
9035                   methname "What backend do you want to use when respooling?"
9036                   methods nil t nil 'gnus-mail-method-history))
9037                 ms)
9038            (cond
9039             ((zerop (length (setq ms (gnus-servers-using-backend
9040                                       (intern method)))))
9041              (list (intern method) ""))
9042             ((= 1 (length ms))
9043              (car ms))
9044             (t
9045              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9046                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9047                            ms-alist))))))))
9048   (unless method
9049     (error "No method given for respooling"))
9050   (if (assoc (symbol-name
9051               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9052              (gnus-methods-using 'respool))
9053       (gnus-summary-move-article n nil method)
9054     (gnus-summary-copy-article n nil method)))
9055
9056 (defun gnus-summary-import-article (file &optional edit)
9057   "Import an arbitrary file into a mail newsgroup."
9058   (interactive "fImport file: \nP")
9059   (let ((group gnus-newsgroup-name)
9060         (now (current-time))
9061         atts lines group-art)
9062     (unless (gnus-check-backend-function 'request-accept-article group)
9063       (error "%s does not support article importing" group))
9064     (or (file-readable-p file)
9065         (not (file-regular-p file))
9066         (error "Can't read %s" file))
9067     (save-excursion
9068       (set-buffer (gnus-get-buffer-create " *import file*"))
9069       (erase-buffer)
9070       (nnheader-insert-file-contents file)
9071       (goto-char (point-min))
9072       (if (nnheader-article-p)
9073           (save-restriction
9074             (goto-char (point-min))
9075             (search-forward "\n\n" nil t)
9076             (narrow-to-region (point-min) (1- (point)))
9077             (goto-char (point-min))
9078             (unless (re-search-forward "^date:" nil t)
9079               (goto-char (point-max))
9080               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9081        ;; This doesn't look like an article, so we fudge some headers.
9082         (setq atts (file-attributes file)
9083               lines (count-lines (point-min) (point-max)))
9084         (insert "From: " (read-string "From: ") "\n"
9085                 "Subject: " (read-string "Subject: ") "\n"
9086                 "Date: " (message-make-date (nth 5 atts)) "\n"
9087                 "Message-ID: " (message-make-message-id) "\n"
9088                 "Lines: " (int-to-string lines) "\n"
9089                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9090       (setq group-art (gnus-request-accept-article group nil t))
9091       (kill-buffer (current-buffer)))
9092     (setq gnus-newsgroup-active (gnus-activate-group group))
9093     (forward-line 1)
9094     (gnus-summary-goto-article (cdr group-art) nil t)
9095     (when edit
9096       (gnus-summary-edit-article))))
9097
9098 (defun gnus-summary-create-article ()
9099   "Create an article in a mail newsgroup."
9100   (interactive)
9101   (let ((group gnus-newsgroup-name)
9102         (now (current-time))
9103         group-art)
9104     (unless (gnus-check-backend-function 'request-accept-article group)
9105       (error "%s does not support article importing" group))
9106     (save-excursion
9107       (set-buffer (gnus-get-buffer-create " *import file*"))
9108       (erase-buffer)
9109       (goto-char (point-min))
9110       ;; This doesn't look like an article, so we fudge some headers.
9111       (insert "From: " (read-string "From: ") "\n"
9112               "Subject: " (read-string "Subject: ") "\n"
9113               "Date: " (message-make-date now) "\n"
9114               "Message-ID: " (message-make-message-id) "\n")
9115       (setq group-art (gnus-request-accept-article group nil t))
9116       (kill-buffer (current-buffer)))
9117     (setq gnus-newsgroup-active (gnus-activate-group group))
9118     (forward-line 1)
9119     (gnus-summary-goto-article (cdr group-art) nil t)
9120     (gnus-summary-edit-article)))
9121
9122 (defun gnus-summary-article-posted-p ()
9123   "Say whether the current (mail) article is available from news as well.
9124 This will be the case if the article has both been mailed and posted."
9125   (interactive)
9126   (let ((id (mail-header-references (gnus-summary-article-header)))
9127         (gnus-override-method (car (gnus-refer-article-methods))))
9128     (if (gnus-request-head id "")
9129         (gnus-message 2 "The current message was found on %s"
9130                       gnus-override-method)
9131       (gnus-message 2 "The current message couldn't be found on %s"
9132                     gnus-override-method)
9133       nil)))
9134
9135 (defun gnus-summary-expire-articles (&optional now)
9136   "Expire all articles that are marked as expirable in the current group."
9137   (interactive)
9138   (when (and (not gnus-group-is-exiting-without-update-p)
9139              (gnus-check-backend-function
9140               'request-expire-articles gnus-newsgroup-name))
9141     ;; This backend supports expiry.
9142     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9143            (expirable (if total
9144                           (progn
9145                             ;; We need to update the info for
9146                             ;; this group for `gnus-list-of-read-articles'
9147                             ;; to give us the right answer.
9148                             (gnus-run-hooks 'gnus-exit-group-hook)
9149                             (gnus-summary-update-info)
9150                             (gnus-list-of-read-articles gnus-newsgroup-name))
9151                         (setq gnus-newsgroup-expirable
9152                               (sort gnus-newsgroup-expirable '<))))
9153            (expiry-wait (if now 'immediate
9154                           (gnus-group-find-parameter
9155                            gnus-newsgroup-name 'expiry-wait)))
9156            (nnmail-expiry-target
9157             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9158                 nnmail-expiry-target))
9159            es)
9160       (when expirable
9161         ;; There are expirable articles in this group, so we run them
9162         ;; through the expiry process.
9163         (gnus-message 6 "Expiring articles...")
9164         (unless (gnus-check-group gnus-newsgroup-name)
9165           (error "Can't open server for %s" gnus-newsgroup-name))
9166         ;; The list of articles that weren't expired is returned.
9167         (save-excursion
9168           (if expiry-wait
9169               (let ((nnmail-expiry-wait-function nil)
9170                     (nnmail-expiry-wait expiry-wait))
9171                 (setq es (gnus-request-expire-articles
9172                           expirable gnus-newsgroup-name)))
9173             (setq es (gnus-request-expire-articles
9174                       expirable gnus-newsgroup-name)))
9175           (unless total
9176             (setq gnus-newsgroup-expirable es))
9177           ;; We go through the old list of expirable, and mark all
9178           ;; really expired articles as nonexistent.
9179           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9180             (let ((gnus-use-cache nil))
9181               (dolist (article expirable)
9182                 (when (and (not (memq article es))
9183                            (gnus-data-find article))
9184                   (gnus-summary-mark-article article gnus-canceled-mark)
9185                   (run-hook-with-args 'gnus-summary-article-expire-hook
9186                                       'delete
9187                                       (gnus-data-header
9188                                        (assoc article (gnus-data-list nil)))
9189                                       gnus-newsgroup-name
9190                                       nil
9191                                       nil))))))
9192         (gnus-message 6 "Expiring articles...done")))))
9193
9194 (defun gnus-summary-expire-articles-now ()
9195   "Expunge all expirable articles in the current group.
9196 This means that *all* articles that are marked as expirable will be
9197 deleted forever, right now."
9198   (interactive)
9199   (or gnus-expert-user
9200       (gnus-yes-or-no-p
9201        "Are you really, really, really sure you want to delete all these messages? ")
9202       (error "Phew!"))
9203   (gnus-summary-expire-articles t))
9204
9205 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9206 (defun gnus-summary-delete-article (&optional n)
9207   "Delete the N next (mail) articles.
9208 This command actually deletes articles.  This is not a marking
9209 command.  The article will disappear forever from your life, never to
9210 return.
9211
9212 If N is negative, delete backwards.
9213 If N is nil and articles have been marked with the process mark,
9214 delete these instead.
9215
9216 If `gnus-novice-user' is non-nil you will be asked for
9217 confirmation before the articles are deleted."
9218   (interactive "P")
9219   (unless (gnus-check-backend-function 'request-expire-articles
9220                                        gnus-newsgroup-name)
9221     (error "The current newsgroup does not support article deletion"))
9222   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9223     (error "Couldn't open server"))
9224   ;; Compute the list of articles to delete.
9225   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9226         (nnmail-expiry-target 'delete)
9227         not-deleted)
9228     (if (and gnus-novice-user
9229              (not (gnus-yes-or-no-p
9230                    (format "Do you really want to delete %s forever? "
9231                            (if (> (length articles) 1)
9232                                (format "these %s articles" (length articles))
9233                              "this article")))))
9234         ()
9235       ;; Delete the articles.
9236       (setq not-deleted (gnus-request-expire-articles
9237                          articles gnus-newsgroup-name 'force))
9238       (while articles
9239         (gnus-summary-remove-process-mark (car articles))
9240         ;; The backend might not have been able to delete the article
9241         ;; after all.
9242         (unless (memq (car articles) not-deleted)
9243           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9244         (let* ((article (car articles))
9245                (id (mail-header-id (gnus-data-header 
9246                                     (assoc article (gnus-data-list nil))))))
9247           (run-hook-with-args 'gnus-summary-article-delete-hook
9248                               'delete id gnus-newsgroup-name nil
9249                               nil))
9250         (setq articles (cdr articles)))
9251       (when not-deleted
9252         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9253     (gnus-summary-position-point)
9254     (gnus-set-mode-line 'summary)
9255     not-deleted))
9256
9257 (defun gnus-summary-edit-article (&optional arg)
9258   "Edit the current article.
9259 This will have permanent effect only in mail groups.
9260 If ARG is nil, edit the decoded articles.
9261 If ARG is 1, edit the raw articles.
9262 If ARG is 2, edit the raw articles even in read-only groups.
9263 If ARG is 3, edit the articles with the current handles.
9264 Otherwise, allow editing of articles even in read-only
9265 groups."
9266   (interactive "P")
9267   (let (force raw current-handles)
9268     (cond
9269      ((null arg))
9270      ((eq arg 1)
9271       (setq raw t))
9272      ((eq arg 2)
9273       (setq raw t
9274             force t))
9275      ((eq arg 3)
9276       (setq current-handles
9277             (and (gnus-buffer-live-p gnus-article-buffer)
9278                  (with-current-buffer gnus-article-buffer
9279                    (prog1
9280                        gnus-article-mime-handles
9281                      (setq gnus-article-mime-handles nil))))))
9282      (t
9283       (setq force t)))
9284     (when (and raw (not force)
9285                (member gnus-newsgroup-name '("nndraft:delayed"
9286                                              "nndraft:drafts"
9287                                              "nndraft:queue")))
9288       (error "Can't edit the raw article in group %s"
9289              gnus-newsgroup-name))
9290     (save-excursion
9291       (set-buffer gnus-summary-buffer)
9292       (let ((mail-parse-charset gnus-newsgroup-charset)
9293             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9294         (gnus-set-global-variables)
9295         (when (and (not force)
9296                    (gnus-group-read-only-p))
9297           (error "The current newsgroup does not support article editing"))
9298         (gnus-summary-show-article t)
9299         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9300           (with-current-buffer gnus-article-buffer
9301             (mm-enable-multibyte)))
9302         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9303             (setq raw t))
9304         (gnus-article-edit-article
9305          (if raw 'ignore
9306            `(lambda ()
9307               (let ((mbl mml-buffer-list))
9308                 (setq mml-buffer-list nil)
9309                 (mime-to-mml ,'current-handles)
9310                 (let ((mbl1 mml-buffer-list))
9311                   (setq mml-buffer-list mbl)
9312                   (set (make-local-variable 'mml-buffer-list) mbl1))
9313                 (gnus-make-local-hook 'kill-buffer-hook)
9314                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9315          `(lambda (no-highlight)
9316             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9317                   (message-options message-options)
9318                   (message-options-set-recipient)
9319                   (mail-parse-ignored-charsets
9320                    ',gnus-newsgroup-ignored-charsets))
9321               ,(if (not raw) '(progn
9322                                 (mml-to-mime)
9323                                 (mml-destroy-buffers)
9324                                 (remove-hook 'kill-buffer-hook
9325                                              'mml-destroy-buffers t)
9326                                 (kill-local-variable 'mml-buffer-list)))
9327               (gnus-summary-edit-article-done
9328                ,(or (mail-header-references gnus-current-headers) "")
9329                ,(gnus-group-read-only-p)
9330                ,gnus-summary-buffer no-highlight))))))))
9331
9332 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9333
9334 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9335                                                  no-highlight)
9336   "Make edits to the current article permanent."
9337   (interactive)
9338   (save-excursion
9339    ;; The buffer restriction contains the entire article if it exists.
9340     (when (article-goto-body)
9341       (let ((lines (count-lines (point) (point-max)))
9342             (length (- (point-max) (point)))
9343             (case-fold-search t)
9344             (body (copy-marker (point))))
9345         (goto-char (point-min))
9346         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9347           (delete-region (match-beginning 1) (match-end 1))
9348           (insert (number-to-string length)))
9349         (goto-char (point-min))
9350         (when (re-search-forward
9351                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9352           (delete-region (match-beginning 1) (match-end 1))
9353           (insert (number-to-string length)))
9354         (goto-char (point-min))
9355         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9356           (delete-region (match-beginning 1) (match-end 1))
9357           (insert (number-to-string lines))))))
9358   ;; Replace the article.
9359   (let ((buf (current-buffer)))
9360     (with-temp-buffer
9361       (insert-buffer-substring buf)
9362
9363       (if (and (not read-only)
9364                (not (gnus-request-replace-article
9365                      (cdr gnus-article-current) (car gnus-article-current)
9366                      (current-buffer) t)))
9367           (error "Couldn't replace article")
9368         ;; Update the summary buffer.
9369         (if (and references
9370                  (equal (message-tokenize-header references " ")
9371                         (message-tokenize-header
9372                          (or (message-fetch-field "references") "") " ")))
9373             ;; We only have to update this line.
9374             (save-excursion
9375               (save-restriction
9376                 (message-narrow-to-head)
9377                 (let ((head (buffer-string))
9378                       header)
9379                   (with-temp-buffer
9380                     (insert (format "211 %d Article retrieved.\n"
9381                                     (cdr gnus-article-current)))
9382                     (insert head)
9383                     (insert ".\n")
9384                     (let ((nntp-server-buffer (current-buffer)))
9385                       (setq header (car (gnus-get-newsgroup-headers
9386                                          nil t))))
9387                     (save-excursion
9388                       (set-buffer gnus-summary-buffer)
9389                       (gnus-data-set-header
9390                        (gnus-data-find (cdr gnus-article-current))
9391                        header)
9392                       (gnus-summary-update-article-line
9393                        (cdr gnus-article-current) header)
9394                       (if (gnus-summary-goto-subject
9395                            (cdr gnus-article-current) nil t)
9396                           (gnus-summary-update-secondary-mark
9397                            (cdr gnus-article-current))))))))
9398           ;; Update threads.
9399           (set-buffer (or buffer gnus-summary-buffer))
9400           (gnus-summary-update-article (cdr gnus-article-current))
9401           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9402               (gnus-summary-update-secondary-mark
9403                (cdr gnus-article-current))))
9404         ;; Prettify the article buffer again.
9405         (unless no-highlight
9406           (save-excursion
9407             (set-buffer gnus-article-buffer)
9408             ;;;!!! Fix this -- article should be rehighlighted.
9409             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9410             (set-buffer gnus-original-article-buffer)
9411             (gnus-request-article
9412              (cdr gnus-article-current)
9413              (car gnus-article-current) (current-buffer))))
9414         ;; Prettify the summary buffer line.
9415         (when (gnus-visual-p 'summary-highlight 'highlight)
9416           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9417
9418 (defun gnus-summary-edit-wash (key)
9419   "Perform editing command KEY in the article buffer."
9420   (interactive
9421    (list
9422     (progn
9423       (message "%s" (concat (this-command-keys) "- "))
9424       (read-char))))
9425   (message "")
9426   (gnus-summary-edit-article)
9427   (execute-kbd-macro (concat (this-command-keys) key))
9428   (gnus-article-edit-done))
9429
9430 ;;; Respooling
9431
9432 (defun gnus-summary-respool-query (&optional silent trace)
9433   "Query where the respool algorithm would put this article."
9434   (interactive)
9435   (let (gnus-mark-article-hook)
9436     (gnus-summary-select-article)
9437     (save-excursion
9438       (set-buffer gnus-original-article-buffer)
9439       (save-restriction
9440         (message-narrow-to-head)
9441         (let ((groups (nnmail-article-group 'identity trace)))
9442           (unless silent
9443             (if groups
9444                 (message "This message would go to %s"
9445                          (mapconcat 'car groups ", "))
9446               (message "This message would go to no groups"))
9447             groups))))))
9448
9449 (defun gnus-summary-respool-trace ()
9450   "Trace where the respool algorithm would put this article.
9451 Display a buffer showing all fancy splitting patterns which matched."
9452   (interactive)
9453   (gnus-summary-respool-query nil t))
9454
9455 ;; Summary marking commands.
9456
9457 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9458   "Mark articles which has the same subject as read, and then select the next.
9459 If UNMARK is positive, remove any kind of mark.
9460 If UNMARK is negative, tick articles."
9461   (interactive "P")
9462   (when unmark
9463     (setq unmark (prefix-numeric-value unmark)))
9464   (let ((count
9465          (gnus-summary-mark-same-subject
9466           (gnus-summary-article-subject) unmark)))
9467     ;; Select next unread article.  If auto-select-same mode, should
9468     ;; select the first unread article.
9469     (gnus-summary-next-article t (and gnus-auto-select-same
9470                                       (gnus-summary-article-subject)))
9471     (gnus-message 7 "%d article%s marked as %s"
9472                   count (if (= count 1) " is" "s are")
9473                   (if unmark "unread" "read"))))
9474
9475 (defun gnus-summary-kill-same-subject (&optional unmark)
9476   "Mark articles which has the same subject as read.
9477 If UNMARK is positive, remove any kind of mark.
9478 If UNMARK is negative, tick articles."
9479   (interactive "P")
9480   (when unmark
9481     (setq unmark (prefix-numeric-value unmark)))
9482   (let ((count
9483          (gnus-summary-mark-same-subject
9484           (gnus-summary-article-subject) unmark)))
9485     ;; If marked as read, go to next unread subject.
9486     (when (null unmark)
9487       ;; Go to next unread subject.
9488       (gnus-summary-next-subject 1 t))
9489     (gnus-message 7 "%d articles are marked as %s"
9490                   count (if unmark "unread" "read"))))
9491
9492 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9493   "Mark articles with same SUBJECT as read, and return marked number.
9494 If optional argument UNMARK is positive, remove any kinds of marks.
9495 If optional argument UNMARK is negative, mark articles as unread instead."
9496   (let ((count 1))
9497     (save-excursion
9498       (cond
9499        ((null unmark)                   ; Mark as read.
9500         (while (and
9501                 (progn
9502                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9503                   (gnus-summary-show-thread) t)
9504                 (gnus-summary-find-subject subject))
9505           (setq count (1+ count))))
9506        ((> unmark 0)                    ; Tick.
9507         (while (and
9508                 (progn
9509                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9510                   (gnus-summary-show-thread) t)
9511                 (gnus-summary-find-subject subject))
9512           (setq count (1+ count))))
9513        (t                               ; Mark as unread.
9514         (while (and
9515                 (progn
9516                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9517                   (gnus-summary-show-thread) t)
9518                 (gnus-summary-find-subject subject))
9519           (setq count (1+ count)))))
9520       (gnus-set-mode-line 'summary)
9521       ;; Return the number of marked articles.
9522       count)))
9523
9524 (defun gnus-summary-mark-as-processable (n &optional unmark)
9525   "Set the process mark on the next N articles.
9526 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9527 the process mark instead.  The difference between N and the actual
9528 number of articles marked is returned."
9529   (interactive "P")
9530   (if (and (null n) (gnus-region-active-p))
9531       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9532     (setq n (prefix-numeric-value n))
9533     (let ((backward (< n 0))
9534           (n (abs n)))
9535       (while (and
9536               (> n 0)
9537               (if unmark
9538                   (gnus-summary-remove-process-mark
9539                    (gnus-summary-article-number))
9540                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9541               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9542         (setq n (1- n)))
9543       (when (/= 0 n)
9544         (gnus-message 7 "No more articles"))
9545       (gnus-summary-recenter)
9546       (gnus-summary-position-point)
9547       n)))
9548
9549 (defun gnus-summary-unmark-as-processable (n)
9550   "Remove the process mark from the next N articles.
9551 If N is negative, unmark backward instead.  The difference between N and
9552 the actual number of articles unmarked is returned."
9553   (interactive "P")
9554   (gnus-summary-mark-as-processable n t))
9555
9556 (defun gnus-summary-unmark-all-processable ()
9557   "Remove the process mark from all articles."
9558   (interactive)
9559   (save-excursion
9560     (while gnus-newsgroup-processable
9561       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9562   (gnus-summary-position-point))
9563
9564 (defun gnus-summary-add-mark (article type)
9565   "Mark ARTICLE with a mark of TYPE."
9566   (let ((vtype (car (assq type gnus-article-mark-lists)))
9567         var)
9568     (if (not vtype)
9569         (error "No such mark type: %s" type)
9570       (setq var (intern (format "gnus-newsgroup-%s" type)))
9571       (set var (cons article (symbol-value var)))
9572       (if (memq type '(processable cached replied forwarded recent saved))
9573           (gnus-summary-update-secondary-mark article)
9574         ;;; !!! This is bogus.  We should find out what primary
9575         ;;; !!! mark we want to set.
9576         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9577
9578 (defun gnus-summary-mark-as-expirable (n)
9579   "Mark N articles forward as expirable.
9580 If N is negative, mark backward instead.  The difference between N and
9581 the actual number of articles marked is returned."
9582   (interactive "p")
9583   (gnus-summary-mark-forward n gnus-expirable-mark))
9584
9585 (defun gnus-summary-mark-as-spam (n)
9586   "Mark N articles forward as spam.
9587 If N is negative, mark backward instead.  The difference between N and
9588 the actual number of articles marked is returned."
9589   (interactive "p")
9590   (gnus-summary-mark-forward n gnus-spam-mark))
9591
9592 (defun gnus-summary-mark-article-as-replied (article)
9593   "Mark ARTICLE as replied to and update the summary line.
9594 ARTICLE can also be a list of articles."
9595   (interactive (list (gnus-summary-article-number)))
9596   (let ((articles (if (listp article) article (list article))))
9597     (dolist (article articles)
9598       (unless (numberp article)
9599         (error "%s is not a number" article))
9600       (push article gnus-newsgroup-replied)
9601       (let ((buffer-read-only nil))
9602         (when (gnus-summary-goto-subject article nil t)
9603           (gnus-summary-update-secondary-mark article))))))
9604
9605 (defun gnus-summary-mark-article-as-forwarded (article)
9606   "Mark ARTICLE as forwarded and update the summary line.
9607 ARTICLE can also be a list of articles."
9608   (let ((articles (if (listp article) article (list article))))
9609     (dolist (article articles)
9610       (push article gnus-newsgroup-forwarded)
9611       (let ((buffer-read-only nil))
9612         (when (gnus-summary-goto-subject article nil t)
9613           (gnus-summary-update-secondary-mark article))))))
9614
9615 (defun gnus-summary-set-bookmark (article)
9616   "Set a bookmark in current article."
9617   (interactive (list (gnus-summary-article-number)))
9618   (when (or (not (get-buffer gnus-article-buffer))
9619             (not gnus-current-article)
9620             (not gnus-article-current)
9621             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9622     (error "No current article selected"))
9623   ;; Remove old bookmark, if one exists.
9624   (gnus-pull article gnus-newsgroup-bookmarks)
9625   ;; Set the new bookmark, which is on the form
9626   ;; (article-number . line-number-in-body).
9627   (push
9628    (cons article
9629          (save-excursion
9630            (set-buffer gnus-article-buffer)
9631            (count-lines
9632             (min (point)
9633                  (save-excursion
9634                    (article-goto-body)
9635                    (point)))
9636             (point))))
9637    gnus-newsgroup-bookmarks)
9638   (gnus-message 6 "A bookmark has been added to the current article."))
9639
9640 (defun gnus-summary-remove-bookmark (article)
9641   "Remove the bookmark from the current article."
9642   (interactive (list (gnus-summary-article-number)))
9643   ;; Remove old bookmark, if one exists.
9644   (if (not (assq article gnus-newsgroup-bookmarks))
9645       (gnus-message 6 "No bookmark in current article.")
9646     (gnus-pull article gnus-newsgroup-bookmarks)
9647     (gnus-message 6 "Removed bookmark.")))
9648
9649 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9650 (defun gnus-summary-mark-as-dormant (n)
9651   "Mark N articles forward as dormant.
9652 If N is negative, mark backward instead.  The difference between N and
9653 the actual number of articles marked is returned."
9654   (interactive "p")
9655   (gnus-summary-mark-forward n gnus-dormant-mark))
9656
9657 (defun gnus-summary-set-process-mark (article)
9658   "Set the process mark on ARTICLE and update the summary line."
9659   (setq gnus-newsgroup-processable
9660         (cons article
9661               (delq article gnus-newsgroup-processable)))
9662   (when (gnus-summary-goto-subject article)
9663     (gnus-summary-show-thread)
9664     (gnus-summary-goto-subject article)
9665     (gnus-summary-update-secondary-mark article)))
9666
9667 (defun gnus-summary-remove-process-mark (article)
9668   "Remove the process mark from ARTICLE and update the summary line."
9669   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9670   (when (gnus-summary-goto-subject article)
9671     (gnus-summary-show-thread)
9672     (gnus-summary-goto-subject article)
9673     (gnus-summary-update-secondary-mark article)))
9674
9675 (defun gnus-summary-set-saved-mark (article)
9676   "Set the process mark on ARTICLE and update the summary line."
9677   (push article gnus-newsgroup-saved)
9678   (when (gnus-summary-goto-subject article)
9679     (gnus-summary-update-secondary-mark article)))
9680
9681 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9682   "Mark N articles as read forwards.
9683 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9684 The difference between N and the actual number of articles marked is
9685 returned.
9686 If NO-EXPIRE, auto-expiry will be inhibited."
9687   (interactive "p")
9688   (gnus-summary-show-thread)
9689   (let ((backward (< n 0))
9690         (gnus-summary-goto-unread
9691          (and gnus-summary-goto-unread
9692               (not (eq gnus-summary-goto-unread 'never))
9693               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9694                                     gnus-ticked-mark gnus-dormant-mark)))))
9695         (n (abs n))
9696         (mark (or mark gnus-del-mark)))
9697     (while (and (> n 0)
9698                 (gnus-summary-mark-article nil mark no-expire)
9699                 (zerop (gnus-summary-next-subject
9700                         (if backward -1 1)
9701                         (and gnus-summary-goto-unread
9702                              (not (eq gnus-summary-goto-unread 'never)))
9703                         t)))
9704       (setq n (1- n)))
9705     (when (/= 0 n)
9706       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9707     (gnus-summary-recenter)
9708     (gnus-summary-position-point)
9709     (gnus-set-mode-line 'summary)
9710     n))
9711
9712 (defun gnus-summary-mark-article-as-read (mark)
9713   "Mark the current article quickly as read with MARK."
9714   (let ((article (gnus-summary-article-number)))
9715     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9716     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9717     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9718     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9719     (push (cons article mark) gnus-newsgroup-reads)
9720     ;; Possibly remove from cache, if that is used.
9721     (when gnus-use-cache
9722       (gnus-cache-enter-remove-article article))
9723     ;; Allow the backend to change the mark.
9724     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9725     ;; Check for auto-expiry.
9726     (when (and gnus-newsgroup-auto-expire
9727                (memq mark gnus-auto-expirable-marks))
9728       (setq mark gnus-expirable-mark)
9729       ;; Let the backend know about the mark change.
9730       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9731       (push article gnus-newsgroup-expirable))
9732     ;; Set the mark in the buffer.
9733     (gnus-summary-update-mark mark 'unread)
9734     t))
9735
9736 (defun gnus-summary-mark-article-as-unread (mark)
9737   "Mark the current article quickly as unread with MARK."
9738   (let* ((article (gnus-summary-article-number))
9739          (old-mark (gnus-summary-article-mark article)))
9740     ;; Allow the backend to change the mark.
9741     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9742     (if (eq mark old-mark)
9743         t
9744       (if (<= article 0)
9745           (progn
9746             (gnus-error 1 "Can't mark negative article numbers")
9747             nil)
9748         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9749         (setq gnus-newsgroup-spam-marked
9750               (delq article gnus-newsgroup-spam-marked))
9751         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9752         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9753         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9754         (cond ((= mark gnus-ticked-mark)
9755                (setq gnus-newsgroup-marked
9756                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9757                                               article)))
9758               ((= mark gnus-spam-mark)
9759                (setq gnus-newsgroup-spam-marked
9760                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9761                                               article)))
9762               ((= mark gnus-dormant-mark)
9763                (setq gnus-newsgroup-dormant
9764                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9765                                               article)))
9766               (t
9767                (setq gnus-newsgroup-unreads
9768                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9769                                               article))))
9770         (gnus-pull article gnus-newsgroup-reads)
9771
9772         ;; See whether the article is to be put in the cache.
9773         (and gnus-use-cache
9774              (vectorp (gnus-summary-article-header article))
9775              (save-excursion
9776                (gnus-cache-possibly-enter-article
9777                 gnus-newsgroup-name article
9778                 (= mark gnus-ticked-mark)
9779                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9780
9781         ;; Fix the mark.
9782         (gnus-summary-update-mark mark 'unread)
9783         t))))
9784
9785 (defun gnus-summary-mark-article (&optional article mark no-expire)
9786   "Mark ARTICLE with MARK.  MARK can be any character.
9787 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9788 `??' (dormant) and `?E' (expirable).
9789 If MARK is nil, then the default character `?r' is used.
9790 If ARTICLE is nil, then the article on the current line will be
9791 marked.
9792 If NO-EXPIRE, auto-expiry will be inhibited."
9793   ;; The mark might be a string.
9794   (when (stringp mark)
9795     (setq mark (aref mark 0)))
9796   ;; If no mark is given, then we check auto-expiring.
9797   (when (null mark)
9798     (setq mark gnus-del-mark))
9799   (when (and (not no-expire)
9800              gnus-newsgroup-auto-expire
9801              (memq mark gnus-auto-expirable-marks))
9802     (setq mark gnus-expirable-mark))
9803   (let ((article (or article (gnus-summary-article-number)))
9804         (old-mark (gnus-summary-article-mark article)))
9805     ;; Allow the backend to change the mark.
9806     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9807     (if (eq mark old-mark)
9808         t
9809       (unless article
9810         (error "No article on current line"))
9811       (if (not (if (or (= mark gnus-unread-mark)
9812                        (= mark gnus-ticked-mark)
9813                        (= mark gnus-spam-mark)
9814                        (= mark gnus-dormant-mark))
9815                    (gnus-mark-article-as-unread article mark)
9816                  (gnus-mark-article-as-read article mark)))
9817           t
9818         ;; See whether the article is to be put in the cache.
9819         (and gnus-use-cache
9820              (not (= mark gnus-canceled-mark))
9821              (vectorp (gnus-summary-article-header article))
9822              (save-excursion
9823                (gnus-cache-possibly-enter-article
9824                 gnus-newsgroup-name article
9825                 (= mark gnus-ticked-mark)
9826                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9827
9828         (when (gnus-summary-goto-subject article nil t)
9829           (let ((buffer-read-only nil))
9830             (gnus-summary-show-thread)
9831             ;; Fix the mark.
9832             (gnus-summary-update-mark mark 'unread)
9833             t))))))
9834
9835 (defun gnus-summary-update-secondary-mark (article)
9836   "Update the secondary (read, process, cache) mark."
9837   (gnus-summary-update-mark
9838    (cond ((memq article gnus-newsgroup-processable)
9839           gnus-process-mark)
9840          ((memq article gnus-newsgroup-cached)
9841           gnus-cached-mark)
9842          ((memq article gnus-newsgroup-replied)
9843           gnus-replied-mark)
9844          ((memq article gnus-newsgroup-forwarded)
9845           gnus-forwarded-mark)
9846          ((memq article gnus-newsgroup-saved)
9847           gnus-saved-mark)
9848          ((memq article gnus-newsgroup-recent)
9849           gnus-recent-mark)
9850          ((memq article gnus-newsgroup-unseen)
9851           gnus-unseen-mark)
9852          (t gnus-no-mark))
9853    'replied)
9854   (when (gnus-visual-p 'summary-highlight 'highlight)
9855     (gnus-run-hooks 'gnus-summary-update-hook))
9856   t)
9857
9858 (defun gnus-summary-update-download-mark (article)
9859   "Update the download mark."
9860   (gnus-summary-update-mark
9861    (cond ((memq article gnus-newsgroup-undownloaded) 
9862           gnus-undownloaded-mark)
9863          (gnus-newsgroup-agentized
9864           gnus-downloaded-mark)
9865          (t
9866           gnus-no-mark))
9867    'download)
9868   (gnus-summary-update-line t)
9869   t)
9870
9871 (defun gnus-summary-update-mark (mark type)
9872   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9873         (buffer-read-only nil))
9874     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9875     (when forward
9876       (when (looking-at "\r")
9877         (incf forward))
9878       (when (<= (+ forward (point)) (point-max))
9879         ;; Go to the right position on the line.
9880         (goto-char (+ forward (point)))
9881         ;; Replace the old mark with the new mark.
9882         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9883         ;; Optionally update the marks by some user rule.
9884         (when (eq type 'unread)
9885           (gnus-data-set-mark
9886            (gnus-data-find (gnus-summary-article-number)) mark)
9887           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9888
9889 (defun gnus-mark-article-as-read (article &optional mark)
9890   "Enter ARTICLE in the pertinent lists and remove it from others."
9891   ;; Make the article expirable.
9892   (let ((mark (or mark gnus-del-mark)))
9893     (setq gnus-newsgroup-expirable
9894           (if (= mark gnus-expirable-mark)
9895               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9896             (delq article gnus-newsgroup-expirable)))
9897     ;; Remove from unread and marked lists.
9898     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9899     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9900     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9901     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9902     (push (cons article mark) gnus-newsgroup-reads)
9903     ;; Possibly remove from cache, if that is used.
9904     (when gnus-use-cache
9905       (gnus-cache-enter-remove-article article))
9906     t))
9907
9908 (defun gnus-mark-article-as-unread (article &optional mark)
9909   "Enter ARTICLE in the pertinent lists and remove it from others."
9910   (let ((mark (or mark gnus-ticked-mark)))
9911     (if (<= article 0)
9912         (progn
9913           (gnus-error 1 "Can't mark negative article numbers")
9914           nil)
9915       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9916             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9917             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9918             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9919             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9920
9921       ;; Unsuppress duplicates?
9922       (when gnus-suppress-duplicates
9923         (gnus-dup-unsuppress-article article))
9924
9925       (cond ((= mark gnus-ticked-mark)
9926              (setq gnus-newsgroup-marked
9927                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9928             ((= mark gnus-spam-mark)
9929              (setq gnus-newsgroup-spam-marked
9930                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9931                                             article)))
9932             ((= mark gnus-dormant-mark)
9933              (setq gnus-newsgroup-dormant
9934                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9935             (t
9936              (setq gnus-newsgroup-unreads
9937                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9938       (gnus-pull article gnus-newsgroup-reads)
9939       t)))
9940
9941 (defalias 'gnus-summary-mark-as-unread-forward
9942   'gnus-summary-tick-article-forward)
9943 (make-obsolete 'gnus-summary-mark-as-unread-forward
9944                'gnus-summary-tick-article-forward)
9945 (defun gnus-summary-tick-article-forward (n)
9946   "Tick N articles forwards.
9947 If N is negative, tick backwards instead.
9948 The difference between N and the number of articles ticked is returned."
9949   (interactive "p")
9950   (gnus-summary-mark-forward n gnus-ticked-mark))
9951
9952 (defalias 'gnus-summary-mark-as-unread-backward
9953   'gnus-summary-tick-article-backward)
9954 (make-obsolete 'gnus-summary-mark-as-unread-backward
9955                'gnus-summary-tick-article-backward)
9956 (defun gnus-summary-tick-article-backward (n)
9957   "Tick N articles backwards.
9958 The difference between N and the number of articles ticked is returned."
9959   (interactive "p")
9960   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9961
9962 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9963 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9964 (defun gnus-summary-tick-article (&optional article clear-mark)
9965   "Mark current article as unread.
9966 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9967 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9968   (interactive)
9969   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9970                                        gnus-ticked-mark)))
9971
9972 (defun gnus-summary-mark-as-read-forward (n)
9973   "Mark N articles as read forwards.
9974 If N is negative, mark backwards instead.
9975 The difference between N and the actual number of articles marked is
9976 returned."
9977   (interactive "p")
9978   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9979
9980 (defun gnus-summary-mark-as-read-backward (n)
9981   "Mark the N articles as read backwards.
9982 The difference between N and the actual number of articles marked is
9983 returned."
9984   (interactive "p")
9985   (gnus-summary-mark-forward
9986    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9987
9988 (defun gnus-summary-mark-as-read (&optional article mark)
9989   "Mark current article as read.
9990 ARTICLE specifies the article to be marked as read.
9991 MARK specifies a string to be inserted at the beginning of the line."
9992   (gnus-summary-mark-article article mark))
9993
9994 (defun gnus-summary-clear-mark-forward (n)
9995   "Clear marks from N articles forward.
9996 If N is negative, clear backward instead.
9997 The difference between N and the number of marks cleared is returned."
9998   (interactive "p")
9999   (gnus-summary-mark-forward n gnus-unread-mark))
10000
10001 (defun gnus-summary-clear-mark-backward (n)
10002   "Clear marks from N articles backward.
10003 The difference between N and the number of marks cleared is returned."
10004   (interactive "p")
10005   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10006
10007 (defun gnus-summary-mark-unread-as-read ()
10008   "Intended to be used by `gnus-summary-mark-article-hook'."
10009   (when (memq gnus-current-article gnus-newsgroup-unreads)
10010     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10011
10012 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10013   "Intended to be used by `gnus-summary-mark-article-hook'."
10014   (let ((mark (gnus-summary-article-mark)))
10015     (when (or (gnus-unread-mark-p mark)
10016               (gnus-read-mark-p mark))
10017       (gnus-summary-mark-article gnus-current-article
10018                                  (or new-mark gnus-read-mark)))))
10019
10020 (defun gnus-summary-mark-unread-as-ticked ()
10021   "Intended to be used by `gnus-summary-mark-article-hook'."
10022   (when (memq gnus-current-article gnus-newsgroup-unreads)
10023     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10024
10025 (defun gnus-summary-mark-region-as-read (point mark all)
10026   "Mark all unread articles between point and mark as read.
10027 If given a prefix, mark all articles between point and mark as read,
10028 even ticked and dormant ones."
10029   (interactive "r\nP")
10030   (save-excursion
10031     (let (article)
10032       (goto-char point)
10033       (beginning-of-line)
10034       (while (and
10035               (< (point) mark)
10036               (progn
10037                 (when (or all
10038                           (memq (setq article (gnus-summary-article-number))
10039                                 gnus-newsgroup-unreads))
10040                   (gnus-summary-mark-article article gnus-del-mark))
10041                 t)
10042               (gnus-summary-find-next))))))
10043
10044 (defun gnus-summary-mark-below (score mark)
10045   "Mark articles with score less than SCORE with MARK."
10046   (interactive "P\ncMark: ")
10047   (setq score (if score
10048                   (prefix-numeric-value score)
10049                 (or gnus-summary-default-score 0)))
10050   (save-excursion
10051     (set-buffer gnus-summary-buffer)
10052     (goto-char (point-min))
10053     (while
10054         (progn
10055           (and (< (gnus-summary-article-score) score)
10056                (gnus-summary-mark-article nil mark))
10057           (gnus-summary-find-next)))))
10058
10059 (defun gnus-summary-kill-below (&optional score)
10060   "Mark articles with score below SCORE as read."
10061   (interactive "P")
10062   (gnus-summary-mark-below score gnus-killed-mark))
10063
10064 (defun gnus-summary-clear-above (&optional score)
10065   "Clear all marks from articles with score above SCORE."
10066   (interactive "P")
10067   (gnus-summary-mark-above score gnus-unread-mark))
10068
10069 (defun gnus-summary-tick-above (&optional score)
10070   "Tick all articles with score above SCORE."
10071   (interactive "P")
10072   (gnus-summary-mark-above score gnus-ticked-mark))
10073
10074 (defun gnus-summary-mark-above (score mark)
10075   "Mark articles with score over SCORE with MARK."
10076   (interactive "P\ncMark: ")
10077   (setq score (if score
10078                   (prefix-numeric-value score)
10079                 (or gnus-summary-default-score 0)))
10080   (save-excursion
10081     (set-buffer gnus-summary-buffer)
10082     (goto-char (point-min))
10083     (while (and (progn
10084                   (when (> (gnus-summary-article-score) score)
10085                     (gnus-summary-mark-article nil mark))
10086                   t)
10087                 (gnus-summary-find-next)))))
10088
10089 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10090 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10091 (defun gnus-summary-limit-include-expunged (&optional no-error)
10092   "Display all the hidden articles that were expunged for low scores."
10093   (interactive)
10094   (let ((buffer-read-only nil))
10095     (let ((scored gnus-newsgroup-scored)
10096           headers h)
10097       (while scored
10098         (unless (gnus-summary-article-header (caar scored))
10099           (and (setq h (gnus-number-to-header (caar scored)))
10100                (< (cdar scored) gnus-summary-expunge-below)
10101                (push h headers)))
10102         (setq scored (cdr scored)))
10103       (if (not headers)
10104           (when (not no-error)
10105             (error "No expunged articles hidden"))
10106         (goto-char (point-min))
10107         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10108         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10109         (mapcar (lambda (x) (push (mail-header-number x)
10110                                   gnus-newsgroup-limit))
10111                 headers)
10112         (gnus-summary-prepare-unthreaded (nreverse headers))
10113         (goto-char (point-min))
10114         (gnus-summary-position-point)
10115         t))))
10116
10117 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10118   "Mark all unread articles in this newsgroup as read.
10119 If prefix argument ALL is non-nil, ticked and dormant articles will
10120 also be marked as read.
10121 If QUIETLY is non-nil, no questions will be asked.
10122 If TO-HERE is non-nil, it should be a point in the buffer.  All
10123 articles before (after, if REVERSE is set) this point will be marked as read.
10124 Note that this function will only catch up the unread article
10125 in the current summary buffer limitation.
10126 The number of articles marked as read is returned."
10127   (interactive "P")
10128   (prog1
10129       (save-excursion
10130         (when (or quietly
10131                   (not gnus-interactive-catchup) ;Without confirmation?
10132                   gnus-expert-user
10133                   (gnus-y-or-n-p
10134                    (if all
10135                        "Mark absolutely all articles as read? "
10136                      "Mark all unread articles as read? ")))
10137           (if (and not-mark
10138                    (not gnus-newsgroup-adaptive)
10139                    (not gnus-newsgroup-auto-expire)
10140                    (not gnus-suppress-duplicates)
10141                    (or (not gnus-use-cache)
10142                        (eq gnus-use-cache 'passive)))
10143               (progn
10144                 (when all
10145                   (setq gnus-newsgroup-marked nil
10146                         gnus-newsgroup-spam-marked nil
10147                         gnus-newsgroup-dormant nil))
10148                 (setq gnus-newsgroup-unreads
10149                       (gnus-sorted-nunion
10150                        (gnus-intersection gnus-newsgroup-unreads
10151                                           gnus-newsgroup-downloadable)
10152                        gnus-newsgroup-unfetched)))
10153             ;; We actually mark all articles as canceled, which we
10154             ;; have to do when using auto-expiry or adaptive scoring.
10155             (gnus-summary-show-all-threads)
10156             (if (and to-here reverse)
10157                 (progn
10158                   (goto-char to-here)
10159                   (gnus-summary-mark-read-and-unread-as-read gnus-catchup-mark)
10160                   (while (gnus-summary-find-next (not all))
10161                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10162               (when (gnus-summary-first-subject (not all))
10163                 (while (and
10164                         (if to-here (< (point) to-here) t)
10165                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10166                         (gnus-summary-find-next (not all))))))
10167             (gnus-set-mode-line 'summary))
10168           t))
10169     (gnus-summary-position-point)))
10170
10171 (defun gnus-summary-catchup-to-here (&optional all)
10172   "Mark all unticked articles before the current one as read.
10173 If ALL is non-nil, also mark ticked and dormant articles as read."
10174   (interactive "P")
10175   (save-excursion
10176     (gnus-save-hidden-threads
10177       (let ((beg (point)))
10178         ;; We check that there are unread articles.
10179         (when (or all (gnus-summary-find-prev))
10180           (gnus-summary-catchup all t beg)))))
10181   (gnus-summary-position-point))
10182
10183 (defun gnus-summary-catchup-from-here (&optional all)
10184   "Mark all unticked articles after (and including) the current one as read.
10185 If ALL is non-nil, also mark ticked and dormant articles as read."
10186   (interactive "P")
10187   (save-excursion
10188     (gnus-save-hidden-threads
10189       (let ((beg (point)))
10190         ;; We check that there are unread articles.
10191         (when (or all (gnus-summary-find-next))
10192           (gnus-summary-catchup all t beg nil t)))))
10193
10194   (gnus-summary-position-point))
10195 (defun gnus-summary-catchup-all (&optional quietly)
10196   "Mark all articles in this newsgroup as read.
10197 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10198 instead, which marks only unread articles as read."
10199   (interactive "P")
10200   (gnus-summary-catchup t quietly))
10201
10202 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10203   "Mark all unread articles in this group as read, then exit.
10204 If prefix argument ALL is non-nil, all articles are marked as read.
10205 If QUIETLY is non-nil, no questions will be asked."
10206   (interactive "P")
10207   (when (gnus-summary-catchup all quietly nil 'fast)
10208     ;; Select next newsgroup or exit.
10209     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10210              (eq gnus-auto-select-next 'quietly))
10211         (gnus-summary-next-group nil)
10212       (gnus-summary-exit))))
10213
10214 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10215   "Mark all articles in this newsgroup as read, and then exit.
10216 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10217 instead, which marks only unread articles as read."
10218   (interactive "P")
10219   (gnus-summary-catchup-and-exit t quietly))
10220
10221 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10222   "Mark all articles in this group as read and select the next group.
10223 If given a prefix, mark all articles, unread as well as ticked, as
10224 read."
10225   (interactive "P")
10226   (save-excursion
10227     (gnus-summary-catchup all))
10228   (gnus-summary-next-group))
10229
10230 ;;;
10231 ;;; with article
10232 ;;;
10233
10234 (defmacro gnus-with-article (article &rest forms)
10235   "Select ARTICLE and perform FORMS in the original article buffer.
10236 Then replace the article with the result."
10237   `(progn
10238      ;; We don't want the article to be marked as read.
10239      (let (gnus-mark-article-hook)
10240        (gnus-summary-select-article t t nil ,article))
10241      (set-buffer gnus-original-article-buffer)
10242      ,@forms
10243      (if (not (gnus-check-backend-function
10244                'request-replace-article (car gnus-article-current)))
10245          (gnus-message 5 "Read-only group; not replacing")
10246        (unless (gnus-request-replace-article
10247                 ,article (car gnus-article-current)
10248                 (current-buffer) t)
10249          (error "Couldn't replace article")))
10250      ;; The cache and backlog have to be flushed somewhat.
10251      (when gnus-keep-backlog
10252        (gnus-backlog-remove-article
10253         (car gnus-article-current) (cdr gnus-article-current)))
10254      (when gnus-use-cache
10255        (gnus-cache-update-article
10256         (car gnus-article-current) (cdr gnus-article-current)))))
10257
10258 (put 'gnus-with-article 'lisp-indent-function 1)
10259 (put 'gnus-with-article 'edebug-form-spec '(form body))
10260
10261 ;; Thread-based commands.
10262
10263 (defun gnus-summary-articles-in-thread (&optional article)
10264   "Return a list of all articles in the current thread.
10265 If ARTICLE is non-nil, return all articles in the thread that starts
10266 with that article."
10267   (let* ((article (or article (gnus-summary-article-number)))
10268          (data (gnus-data-find-list article))
10269          (top-level (gnus-data-level (car data)))
10270          (top-subject
10271           (cond ((null gnus-thread-operation-ignore-subject)
10272                  (gnus-simplify-subject-re
10273                   (mail-header-subject (gnus-data-header (car data)))))
10274                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10275                  (gnus-simplify-subject-fuzzy
10276                   (mail-header-subject (gnus-data-header (car data)))))
10277                 (t nil)))
10278          (end-point (save-excursion
10279                       (if (gnus-summary-go-to-next-thread)
10280                           (point) (point-max))))
10281          articles)
10282     (while (and data
10283                 (< (gnus-data-pos (car data)) end-point))
10284       (when (or (not top-subject)
10285                 (string= top-subject
10286                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10287                              (gnus-simplify-subject-fuzzy
10288                               (mail-header-subject
10289                                (gnus-data-header (car data))))
10290                            (gnus-simplify-subject-re
10291                             (mail-header-subject
10292                              (gnus-data-header (car data)))))))
10293         (push (gnus-data-number (car data)) articles))
10294       (unless (and (setq data (cdr data))
10295                    (> (gnus-data-level (car data)) top-level))
10296         (setq data nil)))
10297     ;; Return the list of articles.
10298     (nreverse articles)))
10299
10300 (defun gnus-summary-rethread-current ()
10301   "Rethread the thread the current article is part of."
10302   (interactive)
10303   (let* ((gnus-show-threads t)
10304          (article (gnus-summary-article-number))
10305          (id (mail-header-id (gnus-summary-article-header)))
10306          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10307     (unless id
10308       (error "No article on the current line"))
10309     (gnus-rebuild-thread id)
10310     (gnus-summary-goto-subject article)))
10311
10312 (defun gnus-summary-reparent-thread ()
10313   "Make the current article child of the marked (or previous) article.
10314
10315 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10316 is non-nil or the Subject: of both articles are the same."
10317   (interactive)
10318   (unless (not (gnus-group-read-only-p))
10319     (error "The current newsgroup does not support article editing"))
10320   (unless (<= (length gnus-newsgroup-processable) 1)
10321     (error "No more than one article may be marked"))
10322   (save-window-excursion
10323     (let ((gnus-article-buffer " *reparent*")
10324           (current-article (gnus-summary-article-number))
10325           ;; First grab the marked article, otherwise one line up.
10326           (parent-article (if (not (null gnus-newsgroup-processable))
10327                               (car gnus-newsgroup-processable)
10328                             (save-excursion
10329                               (if (eq (forward-line -1) 0)
10330                                   (gnus-summary-article-number)
10331                                 (error "Beginning of summary buffer"))))))
10332       (unless (not (eq current-article parent-article))
10333         (error "An article may not be self-referential"))
10334       (let ((message-id (mail-header-id
10335                          (gnus-summary-article-header parent-article))))
10336         (unless (and message-id (not (equal message-id "")))
10337           (error "No message-id in desired parent"))
10338         (gnus-with-article current-article
10339           (save-restriction
10340             (goto-char (point-min))
10341             (message-narrow-to-head)
10342             (if (re-search-forward "^References: " nil t)
10343                 (progn
10344                   (re-search-forward "^[^ \t]" nil t)
10345                   (forward-line -1)
10346                   (end-of-line)
10347                   (insert " " message-id))
10348               (insert "References: " message-id "\n"))))
10349         (set-buffer gnus-summary-buffer)
10350         (gnus-summary-unmark-all-processable)
10351         (gnus-summary-update-article current-article)
10352         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10353             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10354         (gnus-summary-rethread-current)
10355         (gnus-message 3 "Article %d is now the child of article %d"
10356                       current-article parent-article)))))
10357
10358 (defun gnus-summary-toggle-threads (&optional arg)
10359   "Toggle showing conversation threads.
10360 If ARG is positive number, turn showing conversation threads on."
10361   (interactive "P")
10362   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10363     (setq gnus-show-threads
10364           (if (null arg) (not gnus-show-threads)
10365             (> (prefix-numeric-value arg) 0)))
10366     (gnus-summary-prepare)
10367     (gnus-summary-goto-subject current)
10368     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10369     (gnus-summary-position-point)))
10370
10371 (defun gnus-summary-show-all-threads ()
10372   "Show all threads."
10373   (interactive)
10374   (save-excursion
10375     (let ((buffer-read-only nil))
10376       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10377   (gnus-summary-position-point))
10378
10379 (defun gnus-summary-show-thread ()
10380   "Show thread subtrees.
10381 Returns nil if no thread was there to be shown."
10382   (interactive)
10383   (let ((buffer-read-only nil)
10384         (orig (point))
10385         (end (gnus-point-at-eol))
10386         ;; Leave point at bol
10387         (beg (progn (beginning-of-line) (point))))
10388     (prog1
10389         ;; Any hidden lines here?
10390         (search-forward "\r" end t)
10391       (subst-char-in-region beg end ?\^M ?\n t)
10392       (goto-char orig)
10393       (gnus-summary-position-point))))
10394
10395 (defun gnus-summary-maybe-hide-threads ()
10396   "If requested, hide the threads that should be hidden."
10397   (when (and gnus-show-threads
10398              gnus-thread-hide-subtree)
10399     (gnus-summary-hide-all-threads
10400      (if (or (consp gnus-thread-hide-subtree)
10401              (functionp gnus-thread-hide-subtree))
10402          (gnus-make-predicate gnus-thread-hide-subtree)
10403        nil))))
10404
10405 ;;; Hiding predicates.
10406
10407 (defun gnus-article-unread-p (header)
10408   (memq (mail-header-number header) gnus-newsgroup-unreads))
10409
10410 (defun gnus-article-unseen-p (header)
10411   (memq (mail-header-number header) gnus-newsgroup-unseen))
10412
10413 (defun gnus-map-articles (predicate articles)
10414   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10415   (apply 'gnus-or (mapcar predicate
10416                           (mapcar 'gnus-summary-article-header articles))))
10417
10418 (defun gnus-summary-hide-all-threads (&optional predicate)
10419   "Hide all thread subtrees.
10420 If PREDICATE is supplied, threads that satisfy this predicate
10421 will not be hidden."
10422   (interactive)
10423   (save-excursion
10424     (goto-char (point-min))
10425     (let ((end nil))
10426       (while (not end)
10427         (when (or (not predicate)
10428                   (gnus-map-articles
10429                    predicate (gnus-summary-article-children)))
10430             (gnus-summary-hide-thread))
10431         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10432   (gnus-summary-position-point))
10433
10434 (defun gnus-summary-hide-thread ()
10435   "Hide thread subtrees.
10436 If PREDICATE is supplied, threads that satisfy this predicate
10437 will not be hidden.
10438 Returns nil if no threads were there to be hidden."
10439   (interactive)
10440   (let ((buffer-read-only nil)
10441         (start (point))
10442         (article (gnus-summary-article-number)))
10443     (goto-char start)
10444     ;; Go forward until either the buffer ends or the subthread
10445     ;; ends.
10446     (when (and (not (eobp))
10447                (or (zerop (gnus-summary-next-thread 1 t))
10448                    (goto-char (point-max))))
10449       (prog1
10450           (if (and (> (point) start)
10451                    (search-backward "\n" start t))
10452               (progn
10453                 (subst-char-in-region start (point) ?\n ?\^M)
10454                 (gnus-summary-goto-subject article))
10455             (goto-char start)
10456             nil)))))
10457
10458 (defun gnus-summary-go-to-next-thread (&optional previous)
10459   "Go to the same level (or less) next thread.
10460 If PREVIOUS is non-nil, go to previous thread instead.
10461 Return the article number moved to, or nil if moving was impossible."
10462   (let ((level (gnus-summary-thread-level))
10463         (way (if previous -1 1))
10464         (beg (point)))
10465     (forward-line way)
10466     (while (and (not (eobp))
10467                 (< level (gnus-summary-thread-level)))
10468       (forward-line way))
10469     (if (eobp)
10470         (progn
10471           (goto-char beg)
10472           nil)
10473       (setq beg (point))
10474       (prog1
10475           (gnus-summary-article-number)
10476         (goto-char beg)))))
10477
10478 (defun gnus-summary-next-thread (n &optional silent)
10479   "Go to the same level next N'th thread.
10480 If N is negative, search backward instead.
10481 Returns the difference between N and the number of skips actually
10482 done.
10483
10484 If SILENT, don't output messages."
10485   (interactive "p")
10486   (let ((backward (< n 0))
10487         (n (abs n)))
10488     (while (and (> n 0)
10489                 (gnus-summary-go-to-next-thread backward))
10490       (decf n))
10491     (unless silent
10492       (gnus-summary-position-point))
10493     (when (and (not silent) (/= 0 n))
10494       (gnus-message 7 "No more threads"))
10495     n))
10496
10497 (defun gnus-summary-prev-thread (n)
10498   "Go to the same level previous N'th thread.
10499 Returns the difference between N and the number of skips actually
10500 done."
10501   (interactive "p")
10502   (gnus-summary-next-thread (- n)))
10503
10504 (defun gnus-summary-go-down-thread ()
10505   "Go down one level in the current thread."
10506   (let ((children (gnus-summary-article-children)))
10507     (when children
10508       (gnus-summary-goto-subject (car children)))))
10509
10510 (defun gnus-summary-go-up-thread ()
10511   "Go up one level in the current thread."
10512   (let ((parent (gnus-summary-article-parent)))
10513     (when parent
10514       (gnus-summary-goto-subject parent))))
10515
10516 (defun gnus-summary-down-thread (n)
10517   "Go down thread N steps.
10518 If N is negative, go up instead.
10519 Returns the difference between N and how many steps down that were
10520 taken."
10521   (interactive "p")
10522   (let ((up (< n 0))
10523         (n (abs n)))
10524     (while (and (> n 0)
10525                 (if up (gnus-summary-go-up-thread)
10526                   (gnus-summary-go-down-thread)))
10527       (setq n (1- n)))
10528     (gnus-summary-position-point)
10529     (when (/= 0 n)
10530       (gnus-message 7 "Can't go further"))
10531     n))
10532
10533 (defun gnus-summary-up-thread (n)
10534   "Go up thread N steps.
10535 If N is negative, go down instead.
10536 Returns the difference between N and how many steps down that were
10537 taken."
10538   (interactive "p")
10539   (gnus-summary-down-thread (- n)))
10540
10541 (defun gnus-summary-top-thread ()
10542   "Go to the top of the thread."
10543   (interactive)
10544   (while (gnus-summary-go-up-thread))
10545   (gnus-summary-article-number))
10546
10547 (defun gnus-summary-kill-thread (&optional unmark)
10548   "Mark articles under current thread as read.
10549 If the prefix argument is positive, remove any kinds of marks.
10550 If the prefix argument is negative, tick articles instead."
10551   (interactive "P")
10552   (when unmark
10553     (setq unmark (prefix-numeric-value unmark)))
10554   (let ((articles (gnus-summary-articles-in-thread)))
10555     (save-excursion
10556       ;; Expand the thread.
10557       (gnus-summary-show-thread)
10558       ;; Mark all the articles.
10559       (while articles
10560         (gnus-summary-goto-subject (car articles))
10561         (cond ((null unmark)
10562                (gnus-summary-mark-article-as-read gnus-killed-mark))
10563               ((> unmark 0)
10564                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10565               (t
10566                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10567         (setq articles (cdr articles))))
10568     ;; Hide killed subtrees.
10569     (and (null unmark)
10570          gnus-thread-hide-killed
10571          (gnus-summary-hide-thread))
10572     ;; If marked as read, go to next unread subject.
10573     (when (null unmark)
10574       ;; Go to next unread subject.
10575       (gnus-summary-next-subject 1 t)))
10576   (gnus-set-mode-line 'summary))
10577
10578 ;; Summary sorting commands
10579
10580 (defun gnus-summary-sort-by-number (&optional reverse)
10581   "Sort the summary buffer by article number.
10582 Argument REVERSE means reverse order."
10583   (interactive "P")
10584   (gnus-summary-sort 'number reverse))
10585
10586 (defun gnus-summary-sort-by-random (&optional reverse)
10587   "Randomize the order in the summary buffer.
10588 Argument REVERSE means to randomize in reverse order."
10589   (interactive "P")
10590   (gnus-summary-sort 'random reverse))
10591
10592 (defun gnus-summary-sort-by-author (&optional reverse)
10593   "Sort the summary buffer by author name alphabetically.
10594 If `case-fold-search' is non-nil, case of letters is ignored.
10595 Argument REVERSE means reverse order."
10596   (interactive "P")
10597   (gnus-summary-sort 'author reverse))
10598
10599 (defun gnus-summary-sort-by-subject (&optional reverse)
10600   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10601 If `case-fold-search' is non-nil, case of letters is ignored.
10602 Argument REVERSE means reverse order."
10603   (interactive "P")
10604   (gnus-summary-sort 'subject reverse))
10605
10606 (defun gnus-summary-sort-by-date (&optional reverse)
10607   "Sort the summary buffer by date.
10608 Argument REVERSE means reverse order."
10609   (interactive "P")
10610   (gnus-summary-sort 'date reverse))
10611
10612 (defun gnus-summary-sort-by-score (&optional reverse)
10613   "Sort the summary buffer by score.
10614 Argument REVERSE means reverse order."
10615   (interactive "P")
10616   (gnus-summary-sort 'score reverse))
10617
10618 (defun gnus-summary-sort-by-lines (&optional reverse)
10619   "Sort the summary buffer by the number of lines.
10620 Argument REVERSE means reverse order."
10621   (interactive "P")
10622   (gnus-summary-sort 'lines reverse))
10623
10624 (defun gnus-summary-sort-by-chars (&optional reverse)
10625   "Sort the summary buffer by article length.
10626 Argument REVERSE means reverse order."
10627   (interactive "P")
10628   (gnus-summary-sort 'chars reverse))
10629
10630 (defun gnus-summary-sort-by-original (&optional reverse)
10631   "Sort the summary buffer using the default sorting method.
10632 Argument REVERSE means reverse order."
10633   (interactive "P")
10634   (let* ((buffer-read-only)
10635          (gnus-summary-prepare-hook nil))
10636     ;; We do the sorting by regenerating the threads.
10637     (gnus-summary-prepare)
10638     ;; Hide subthreads if needed.
10639     (gnus-summary-maybe-hide-threads)))
10640
10641 (defun gnus-summary-sort (predicate reverse)
10642   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10643   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10644          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10645          (gnus-thread-sort-functions
10646           (if (not reverse)
10647               thread
10648             `(lambda (t1 t2)
10649                (,thread t2 t1))))
10650          (gnus-sort-gathered-threads-function
10651           gnus-thread-sort-functions)
10652          (gnus-article-sort-functions
10653           (if (not reverse)
10654               article
10655             `(lambda (t1 t2)
10656                (,article t2 t1))))
10657          (buffer-read-only)
10658          (gnus-summary-prepare-hook nil))
10659     ;; We do the sorting by regenerating the threads.
10660     (gnus-summary-prepare)
10661     ;; Hide subthreads if needed.
10662     (gnus-summary-maybe-hide-threads)))
10663
10664 ;; Summary saving commands.
10665
10666 (defun gnus-summary-save-article (&optional n not-saved)
10667   "Save the current article using the default saver function.
10668 If N is a positive number, save the N next articles.
10669 If N is a negative number, save the N previous articles.
10670 If N is nil and any articles have been marked with the process mark,
10671 save those articles instead.
10672 The variable `gnus-default-article-saver' specifies the saver function."
10673   (interactive "P")
10674   (let* ((articles (gnus-summary-work-articles n))
10675          (save-buffer (save-excursion
10676                         (nnheader-set-temp-buffer " *Gnus Save*")))
10677          (num (length articles))
10678          header file)
10679     (dolist (article articles)
10680       (setq header (gnus-summary-article-header article))
10681       (if (not (vectorp header))
10682           ;; This is a pseudo-article.
10683           (if (assq 'name header)
10684               (gnus-copy-file (cdr (assq 'name header)))
10685             (gnus-message 1 "Article %d is unsaveable" article))
10686         ;; This is a real article.
10687         (save-window-excursion
10688           (let ((gnus-display-mime-function nil)
10689                 (gnus-article-prepare-hook nil))
10690             (gnus-summary-select-article t nil nil article)))
10691         (save-excursion
10692           (set-buffer save-buffer)
10693           (erase-buffer)
10694           (insert-buffer-substring gnus-original-article-buffer))
10695         (setq file (gnus-article-save save-buffer file num))
10696         (gnus-summary-remove-process-mark article)
10697         (unless not-saved
10698           (gnus-summary-set-saved-mark article))))
10699     (gnus-kill-buffer save-buffer)
10700     (gnus-summary-position-point)
10701     (gnus-set-mode-line 'summary)
10702     n))
10703
10704 (defun gnus-summary-pipe-output (&optional arg headers)
10705   "Pipe the current article to a subprocess.
10706 If N is a positive number, pipe the N next articles.
10707 If N is a negative number, pipe the N previous articles.
10708 If N is nil and any articles have been marked with the process mark,
10709 pipe those articles instead.
10710 If HEADERS (the symbolic prefix), include the headers, too."
10711   (interactive (gnus-interactive "P\ny"))
10712   (require 'gnus-art)
10713   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10714         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10715     (gnus-summary-save-article arg t))
10716   (let ((buffer (get-buffer "*Shell Command Output*")))
10717     (when (and buffer
10718                (not (zerop (buffer-size buffer))))
10719       (gnus-configure-windows 'pipe))))
10720
10721 (defun gnus-summary-save-article-mail (&optional arg)
10722   "Append the current article to an mail file.
10723 If N is a positive number, save the N next articles.
10724 If N is a negative number, save the N previous articles.
10725 If N is nil and any articles have been marked with the process mark,
10726 save those articles instead."
10727   (interactive "P")
10728   (require 'gnus-art)
10729   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10730     (gnus-summary-save-article arg)))
10731
10732 (defun gnus-summary-save-article-rmail (&optional arg)
10733   "Append the current article to an rmail file.
10734 If N is a positive number, save the N next articles.
10735 If N is a negative number, save the N previous articles.
10736 If N is nil and any articles have been marked with the process mark,
10737 save those articles instead."
10738   (interactive "P")
10739   (require 'gnus-art)
10740   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10741     (gnus-summary-save-article arg)))
10742
10743 (defun gnus-summary-save-article-file (&optional arg)
10744   "Append the current article to a file.
10745 If N is a positive number, save the N next articles.
10746 If N is a negative number, save the N previous articles.
10747 If N is nil and any articles have been marked with the process mark,
10748 save those articles instead."
10749   (interactive "P")
10750   (require 'gnus-art)
10751   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10752     (gnus-summary-save-article arg)))
10753
10754 (defun gnus-summary-write-article-file (&optional arg)
10755   "Write the current article to a file, deleting the previous file.
10756 If N is a positive number, save the N next articles.
10757 If N is a negative number, save the N previous articles.
10758 If N is nil and any articles have been marked with the process mark,
10759 save those articles instead."
10760   (interactive "P")
10761   (require 'gnus-art)
10762   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10763     (gnus-summary-save-article arg)))
10764
10765 (defun gnus-summary-save-article-body-file (&optional arg)
10766   "Append the current article body to a file.
10767 If N is a positive number, save the N next articles.
10768 If N is a negative number, save the N previous articles.
10769 If N is nil and any articles have been marked with the process mark,
10770 save those articles instead."
10771   (interactive "P")
10772   (require 'gnus-art)
10773   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10774     (gnus-summary-save-article arg)))
10775
10776 (defun gnus-summary-muttprint (&optional arg)
10777   "Print the current article using Muttprint.
10778 If N is a positive number, save the N next articles.
10779 If N is a negative number, save the N previous articles.
10780 If N is nil and any articles have been marked with the process mark,
10781 save those articles instead."
10782   (interactive "P")
10783   (require 'gnus-art)
10784   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10785     (gnus-summary-save-article arg t)))
10786
10787 (defun gnus-summary-pipe-message (program)
10788   "Pipe the current article through PROGRAM."
10789   (interactive "sProgram: ")
10790   (gnus-summary-select-article)
10791   (let ((mail-header-separator ""))
10792     (gnus-eval-in-buffer-window gnus-article-buffer
10793       (save-restriction
10794         (widen)
10795         (let ((start (window-start))
10796               buffer-read-only)
10797           (message-pipe-buffer-body program)
10798           (set-window-start (get-buffer-window (current-buffer)) start))))))
10799
10800 (defun gnus-get-split-value (methods)
10801   "Return a value based on the split METHODS."
10802   (let (split-name method result match)
10803     (when methods
10804       (save-excursion
10805         (set-buffer gnus-original-article-buffer)
10806         (save-restriction
10807           (nnheader-narrow-to-headers)
10808           (while (and methods (not split-name))
10809             (goto-char (point-min))
10810             (setq method (pop methods))
10811             (setq match (car method))
10812             (when (cond
10813                    ((stringp match)
10814                     ;; Regular expression.
10815                     (ignore-errors
10816                       (re-search-forward match nil t)))
10817                    ((functionp match)
10818                     ;; Function.
10819                     (save-restriction
10820                       (widen)
10821                       (setq result (funcall match gnus-newsgroup-name))))
10822                    ((consp match)
10823                     ;; Form.
10824                     (save-restriction
10825                       (widen)
10826                       (setq result (eval match)))))
10827               (setq split-name (cdr method))
10828               (cond ((stringp result)
10829                      (push (expand-file-name
10830                             result gnus-article-save-directory)
10831                            split-name))
10832                     ((consp result)
10833                      (setq split-name (append result split-name)))))))))
10834     (nreverse split-name)))
10835
10836 (defun gnus-valid-move-group-p (group)
10837   (and (boundp group)
10838        (symbol-name group)
10839        (symbol-value group)
10840        (gnus-get-function (gnus-find-method-for-group
10841                            (symbol-name group)) 'request-accept-article t)))
10842
10843 (defun gnus-read-move-group-name (prompt default articles prefix)
10844   "Read a group name."
10845   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10846          (minibuffer-confirm-incomplete nil) ; XEmacs
10847          (prom
10848           (format "%s %s to:"
10849                   prompt
10850                   (if (> (length articles) 1)
10851                       (format "these %d articles" (length articles))
10852                     "this article")))
10853          (to-newsgroup
10854           (cond
10855            ((null split-name)
10856             (gnus-completing-read-with-default
10857              default prom
10858              gnus-active-hashtb
10859              'gnus-valid-move-group-p
10860              nil prefix
10861              'gnus-group-history))
10862            ((= 1 (length split-name))
10863             (gnus-completing-read-with-default
10864              (car split-name) prom
10865              gnus-active-hashtb
10866              'gnus-valid-move-group-p
10867              nil nil
10868              'gnus-group-history))
10869            (t
10870             (gnus-completing-read-with-default
10871              nil prom
10872              (mapcar (lambda (el) (list el))
10873                      (nreverse split-name))
10874              nil nil nil
10875              'gnus-group-history))))
10876          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10877     (when to-newsgroup
10878       (if (or (string= to-newsgroup "")
10879               (string= to-newsgroup prefix))
10880           (setq to-newsgroup default))
10881       (unless to-newsgroup
10882         (error "No group name entered"))
10883       (or (gnus-active to-newsgroup)
10884           (gnus-activate-group to-newsgroup nil nil to-method)
10885           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10886                                      to-newsgroup))
10887               (or (and (gnus-request-create-group to-newsgroup to-method)
10888                        (gnus-activate-group
10889                         to-newsgroup nil nil to-method)
10890                        (gnus-subscribe-group to-newsgroup))
10891                   (error "Couldn't create group %s" to-newsgroup)))
10892           (error "No such group: %s" to-newsgroup)))
10893     to-newsgroup))
10894
10895 (defun gnus-summary-save-parts (type dir n &optional reverse)
10896   "Save parts matching TYPE to DIR.
10897 If REVERSE, save parts that do not match TYPE."
10898   (interactive
10899    (list (read-string "Save parts of type: "
10900                       (or (car gnus-summary-save-parts-type-history)
10901                           gnus-summary-save-parts-default-mime)
10902                       'gnus-summary-save-parts-type-history)
10903          (setq gnus-summary-save-parts-last-directory
10904                (read-file-name "Save to directory: "
10905                                gnus-summary-save-parts-last-directory
10906                                nil t))
10907          current-prefix-arg))
10908   (gnus-summary-iterate n
10909     (let ((gnus-display-mime-function nil)
10910           (gnus-inhibit-treatment t))
10911       (gnus-summary-select-article))
10912     (save-excursion
10913       (set-buffer gnus-article-buffer)
10914       (let ((handles (or gnus-article-mime-handles
10915                          (mm-dissect-buffer nil gnus-article-loose-mime)
10916                          (and gnus-article-emulate-mime
10917                               (mm-uu-dissect)))))
10918         (when handles
10919           (gnus-summary-save-parts-1 type dir handles reverse)
10920           (unless gnus-article-mime-handles ;; Don't destroy this case.
10921             (mm-destroy-parts handles)))))))
10922
10923 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10924   (if (stringp (car handle))
10925       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10926               (cdr handle))
10927     (when (if reverse
10928               (not (string-match type (mm-handle-media-type handle)))
10929             (string-match type (mm-handle-media-type handle)))
10930       (let ((file (expand-file-name
10931                    (file-name-nondirectory
10932                     (or
10933                      (mail-content-type-get
10934                       (mm-handle-disposition handle) 'filename)
10935                      (concat gnus-newsgroup-name
10936                              "." (number-to-string
10937                                   (cdr gnus-article-current)))))
10938                    dir)))
10939         (unless (file-exists-p file)
10940           (mm-save-part-to-file handle file))))))
10941
10942 ;; Summary extract commands
10943
10944 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10945   (let ((buffer-read-only nil)
10946         (article (gnus-summary-article-number))
10947         after-article b e)
10948     (unless (gnus-summary-goto-subject article)
10949       (error "No such article: %d" article))
10950     (gnus-summary-position-point)
10951     ;; If all commands are to be bunched up on one line, we collect
10952     ;; them here.
10953     (unless gnus-view-pseudos-separately
10954       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10955             files action)
10956         (while ps
10957           (setq action (cdr (assq 'action (car ps))))
10958           (setq files (list (cdr (assq 'name (car ps)))))
10959           (while (and ps (cdr ps)
10960                       (string= (or action "1")
10961                                (or (cdr (assq 'action (cadr ps))) "2")))
10962             (push (cdr (assq 'name (cadr ps))) files)
10963             (setcdr ps (cddr ps)))
10964           (when files
10965             (when (not (string-match "%s" action))
10966               (push " " files))
10967             (push " " files)
10968             (when (assq 'execute (car ps))
10969               (setcdr (assq 'execute (car ps))
10970                       (funcall (if (string-match "%s" action)
10971                                    'format 'concat)
10972                                action
10973                                (mapconcat
10974                                 (lambda (f)
10975                                   (if (equal f " ")
10976                                       f
10977                                     (mm-quote-arg f)))
10978                                 files " ")))))
10979           (setq ps (cdr ps)))))
10980     (if (and gnus-view-pseudos (not not-view))
10981         (while pslist
10982           (when (assq 'execute (car pslist))
10983             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10984                                   (eq gnus-view-pseudos 'not-confirm)))
10985           (setq pslist (cdr pslist)))
10986       (save-excursion
10987         (while pslist
10988           (setq after-article (or (cdr (assq 'article (car pslist)))
10989                                   (gnus-summary-article-number)))
10990           (gnus-summary-goto-subject after-article)
10991           (forward-line 1)
10992           (setq b (point))
10993           (insert "    " (file-name-nondirectory
10994                           (cdr (assq 'name (car pslist))))
10995                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10996           (setq e (point))
10997           (forward-line -1)             ; back to `b'
10998           (gnus-add-text-properties
10999            b (1- e) (list 'gnus-number gnus-reffed-article-number
11000                           gnus-mouse-face-prop gnus-mouse-face))
11001           (gnus-data-enter
11002            after-article gnus-reffed-article-number
11003            gnus-unread-mark b (car pslist) 0 (- e b))
11004           (setq gnus-newsgroup-unreads
11005                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11006                                          gnus-reffed-article-number))
11007           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11008           (setq pslist (cdr pslist)))))))
11009
11010 (defun gnus-pseudos< (p1 p2)
11011   (let ((c1 (cdr (assq 'action p1)))
11012         (c2 (cdr (assq 'action p2))))
11013     (and c1 c2 (string< c1 c2))))
11014
11015 (defun gnus-request-pseudo-article (props)
11016   (cond ((assq 'execute props)
11017          (gnus-execute-command (cdr (assq 'execute props)))))
11018   (let ((gnus-current-article (gnus-summary-article-number)))
11019     (gnus-run-hooks 'gnus-mark-article-hook)))
11020
11021 (defun gnus-execute-command (command &optional automatic)
11022   (save-excursion
11023     (gnus-article-setup-buffer)
11024     (set-buffer gnus-article-buffer)
11025     (setq buffer-read-only nil)
11026     (let ((command (if automatic command
11027                      (read-string "Command: " (cons command 0)))))
11028       (erase-buffer)
11029       (insert "$ " command "\n\n")
11030       (if gnus-view-pseudo-asynchronously
11031           (start-process "gnus-execute" (current-buffer) shell-file-name
11032                          shell-command-switch command)
11033         (call-process shell-file-name nil t nil
11034                       shell-command-switch command)))))
11035
11036 ;; Summary kill commands.
11037
11038 (defun gnus-summary-edit-global-kill (article)
11039   "Edit the \"global\" kill file."
11040   (interactive (list (gnus-summary-article-number)))
11041   (gnus-group-edit-global-kill article))
11042
11043 (defun gnus-summary-edit-local-kill ()
11044   "Edit a local kill file applied to the current newsgroup."
11045   (interactive)
11046   (setq gnus-current-headers (gnus-summary-article-header))
11047   (gnus-group-edit-local-kill
11048    (gnus-summary-article-number) gnus-newsgroup-name))
11049
11050 ;;; Header reading.
11051
11052 (defun gnus-read-header (id &optional header)
11053   "Read the headers of article ID and enter them into the Gnus system."
11054   (let ((group gnus-newsgroup-name)
11055         (gnus-override-method
11056          (or
11057           gnus-override-method
11058           (and (gnus-news-group-p gnus-newsgroup-name)
11059                (car (gnus-refer-article-methods)))))
11060         where)
11061     ;; First we check to see whether the header in question is already
11062     ;; fetched.
11063     (if (stringp id)
11064         ;; This is a Message-ID.
11065         (setq header (or header (gnus-id-to-header id)))
11066       ;; This is an article number.
11067       (setq header (or header (gnus-summary-article-header id))))
11068     (if (and header
11069              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11070         ;; We have found the header.
11071         header
11072       ;; If this is a sparse article, we have to nix out its
11073       ;; previous entry in the thread hashtb.
11074       (when (and header
11075                  (gnus-summary-article-sparse-p (mail-header-number header)))
11076         (let* ((parent (gnus-parent-id (mail-header-references header)))
11077                (thread (and parent (gnus-id-to-thread parent))))
11078           (when thread
11079             (delq (assq header thread) thread))))
11080       ;; We have to really fetch the header to this article.
11081       (save-excursion
11082         (set-buffer nntp-server-buffer)
11083         (when (setq where (gnus-request-head id group))
11084           (nnheader-fold-continuation-lines)
11085           (goto-char (point-max))
11086           (insert ".\n")
11087           (goto-char (point-min))
11088           (insert "211 ")
11089           (princ (cond
11090                   ((numberp id) id)
11091                   ((cdr where) (cdr where))
11092                   (header (mail-header-number header))
11093                   (t gnus-reffed-article-number))
11094                  (current-buffer))
11095           (insert " Article retrieved.\n"))
11096         (if (or (not where)
11097                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11098             ()                          ; Malformed head.
11099           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11100             (when (and (stringp id)
11101                        (not (string= (gnus-group-real-name group)
11102                                      (car where))))
11103               ;; If we fetched by Message-ID and the article came
11104               ;; from a different group, we fudge some bogus article
11105               ;; numbers for this article.
11106               (mail-header-set-number header gnus-reffed-article-number))
11107             (save-excursion
11108               (set-buffer gnus-summary-buffer)
11109               (decf gnus-reffed-article-number)
11110               (gnus-remove-header (mail-header-number header))
11111               (push header gnus-newsgroup-headers)
11112               (setq gnus-current-headers header)
11113               (push (mail-header-number header) gnus-newsgroup-limit)))
11114           header)))))
11115
11116 (defun gnus-remove-header (number)
11117   "Remove header NUMBER from `gnus-newsgroup-headers'."
11118   (if (and gnus-newsgroup-headers
11119            (= number (mail-header-number (car gnus-newsgroup-headers))))
11120       (pop gnus-newsgroup-headers)
11121     (let ((headers gnus-newsgroup-headers))
11122       (while (and (cdr headers)
11123                   (not (= number (mail-header-number (cadr headers)))))
11124         (pop headers))
11125       (when (cdr headers)
11126         (setcdr headers (cddr headers))))))
11127
11128 ;;;
11129 ;;; summary highlights
11130 ;;;
11131
11132 (defun gnus-highlight-selected-summary ()
11133   "Highlight selected article in summary buffer."
11134   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11135   (when gnus-summary-selected-face
11136     (save-excursion
11137       (let* ((beg (gnus-point-at-bol))
11138              (end (gnus-point-at-eol))
11139              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11140              (from (if (get-text-property beg gnus-mouse-face-prop)
11141                        beg
11142                      (or (next-single-property-change
11143                           beg gnus-mouse-face-prop nil end)
11144                          beg)))
11145              (to
11146               (if (= from end)
11147                   (- from 2)
11148                 (or (next-single-property-change
11149                      from gnus-mouse-face-prop nil end)
11150                     end))))
11151         ;; If no mouse-face prop on line we will have to = from = end,
11152         ;; so we highlight the entire line instead.
11153         (when (= (+ to 2) from)
11154           (setq from beg)
11155           (setq to end))
11156         (if gnus-newsgroup-selected-overlay
11157             ;; Move old overlay.
11158             (gnus-move-overlay
11159              gnus-newsgroup-selected-overlay from to (current-buffer))
11160           ;; Create new overlay.
11161           (gnus-overlay-put
11162            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11163            'face gnus-summary-selected-face))))))
11164
11165 (defvar gnus-summary-highlight-line-cached nil)
11166 (defvar gnus-summary-highlight-line-trigger nil)
11167
11168 (defun gnus-summary-highlight-line-0 ()
11169   (if (and (eq gnus-summary-highlight-line-trigger 
11170                gnus-summary-highlight)
11171            gnus-summary-highlight-line-cached)
11172       gnus-summary-highlight-line-cached
11173     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11174           gnus-summary-highlight-line-cached
11175           (let* ((cond (list 'cond))
11176                  (c cond)
11177                  (list gnus-summary-highlight))
11178             (while list
11179               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11180                               nil))
11181               (setq c (cdr c)
11182                     list (cdr list)))
11183             (gnus-byte-compile (list 'lambda nil cond))))))
11184
11185 (defun gnus-summary-highlight-line ()
11186   "Highlight current line according to `gnus-summary-highlight'."
11187   (let* ((beg (gnus-point-at-bol))
11188          (article (or (gnus-summary-article-number) gnus-current-article))
11189          (score (or (cdr (assq article
11190                                gnus-newsgroup-scored))
11191                     gnus-summary-default-score 0))
11192          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11193          (inhibit-read-only t)
11194          (default gnus-summary-default-score)
11195          (default-high gnus-summary-default-high-score)
11196          (default-low gnus-summary-default-low-score)
11197          (uncached (memq article gnus-newsgroup-undownloaded))
11198          (downloaded (not uncached)))
11199     (let ((face (funcall (gnus-summary-highlight-line-0))))
11200       (unless (eq face (get-text-property beg 'face))
11201         (gnus-put-text-property-excluding-characters-with-faces
11202          beg (gnus-point-at-eol) 'face
11203          (setq face (if (boundp face) (symbol-value face) face)))
11204         (when gnus-summary-highlight-line-function
11205           (funcall gnus-summary-highlight-line-function article face))))))
11206
11207 (defun gnus-update-read-articles (group unread &optional compute)
11208   "Update the list of read articles in GROUP.
11209 UNREAD is a sorted list."
11210   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11211          (entry (gnus-gethash group gnus-newsrc-hashtb))
11212          (info (nth 2 entry))
11213          (prev 1)
11214          read)
11215     (if (or (not info) (not active))
11216         ;; There is no info on this group if it was, in fact,
11217         ;; killed.  Gnus stores no information on killed groups, so
11218         ;; there's nothing to be done.
11219         ;; One could store the information somewhere temporarily,
11220         ;; perhaps...  Hmmm...
11221         ()
11222       ;; Remove any negative articles numbers.
11223       (while (and unread (< (car unread) 0))
11224         (setq unread (cdr unread)))
11225       ;; Remove any expired article numbers
11226       (while (and unread (< (car unread) (car active)))
11227         (setq unread (cdr unread)))
11228       ;; Compute the ranges of read articles by looking at the list of
11229       ;; unread articles.
11230       (while unread
11231         (when (/= (car unread) prev)
11232           (push (if (= prev (1- (car unread))) prev
11233                   (cons prev (1- (car unread))))
11234                 read))
11235         (setq prev (1+ (car unread)))
11236         (setq unread (cdr unread)))
11237       (when (<= prev (cdr active))
11238         (push (cons prev (cdr active)) read))
11239       (setq read (if (> (length read) 1) (nreverse read) read))
11240       (if compute
11241           read
11242         (save-excursion
11243           (let (setmarkundo)
11244             ;; Propagate the read marks to the backend.
11245             (when (gnus-check-backend-function 'request-set-mark group)
11246               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11247                     (add (gnus-remove-from-range read (gnus-info-read info))))
11248                 (when (or add del)
11249                   (unless (gnus-check-group group)
11250                     (error "Can't open server for %s" group))
11251                   (gnus-request-set-mark
11252                    group (delq nil (list (if add (list add 'add '(read)))
11253                                          (if del (list del 'del '(read))))))
11254                   (setq setmarkundo
11255                         `(gnus-request-set-mark
11256                           ,group
11257                           ',(delq nil (list
11258                                        (if del (list del 'add '(read)))
11259                                        (if add (list add 'del '(read))))))))))
11260             (set-buffer gnus-group-buffer)
11261             (gnus-undo-register
11262               `(progn
11263                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11264                  (gnus-info-set-read ',info ',(gnus-info-read info))
11265                  (gnus-get-unread-articles-in-group ',info
11266                                                     (gnus-active ,group))
11267                  (gnus-group-update-group ,group t)
11268                  ,setmarkundo))))
11269         ;; Enter this list into the group info.
11270         (gnus-info-set-read info read)
11271         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11272         (gnus-get-unread-articles-in-group info (gnus-active group))
11273         t))))
11274
11275 (defun gnus-offer-save-summaries ()
11276   "Offer to save all active summary buffers."
11277   (let (buffers)
11278     ;; Go through all buffers and find all summaries.
11279     (dolist (buffer (buffer-list))
11280       (when (and (setq buffer (buffer-name buffer))
11281                  (string-match "Summary" buffer)
11282                  (save-excursion
11283                    (set-buffer buffer)
11284                    ;; We check that this is, indeed, a summary buffer.
11285                    (and (eq major-mode 'gnus-summary-mode)
11286                         ;; Also make sure this isn't bogus.
11287                         gnus-newsgroup-prepared
11288                         ;; Also make sure that this isn't a
11289                         ;; dead summary buffer.
11290                         (not gnus-dead-summary-mode))))
11291         (push buffer buffers)))
11292     ;; Go through all these summary buffers and offer to save them.
11293     (when buffers
11294       (save-excursion
11295         (map-y-or-n-p
11296          "Update summary buffer %s? "
11297          (lambda (buf)
11298            (switch-to-buffer buf)
11299            (gnus-summary-exit))
11300          buffers)))))
11301
11302 (defun gnus-summary-setup-default-charset ()
11303   "Setup newsgroup default charset."
11304   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11305       (setq gnus-newsgroup-charset nil)
11306     (let* ((ignored-charsets
11307             (or gnus-newsgroup-ephemeral-ignored-charsets
11308                 (append
11309                  (and gnus-newsgroup-name
11310                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11311                  gnus-newsgroup-ignored-charsets))))
11312       (setq gnus-newsgroup-charset
11313             (or gnus-newsgroup-ephemeral-charset
11314                 (and gnus-newsgroup-name
11315                      (gnus-parameter-charset gnus-newsgroup-name))
11316                 gnus-default-charset))
11317       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11318            ignored-charsets))))
11319
11320 ;;;
11321 ;;; Mime Commands
11322 ;;;
11323
11324 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11325   "Display the current article buffer fully MIME-buttonized.
11326 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11327 treated as multipart/mixed."
11328   (interactive "P")
11329   (require 'gnus-art)
11330   (let ((gnus-unbuttonized-mime-types nil)
11331         (gnus-mime-display-multipart-as-mixed show-all-parts))
11332     (gnus-summary-show-article)))
11333
11334 (defun gnus-summary-repair-multipart (article)
11335   "Add a Content-Type header to a multipart article without one."
11336   (interactive (list (gnus-summary-article-number)))
11337   (gnus-with-article article
11338     (message-narrow-to-head)
11339     (message-remove-header "Mime-Version")
11340     (goto-char (point-max))
11341     (insert "Mime-Version: 1.0\n")
11342     (widen)
11343     (when (search-forward "\n--" nil t)
11344       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11345         (message-narrow-to-head)
11346         (message-remove-header "Content-Type")
11347         (goto-char (point-max))
11348         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11349                         separator))
11350         (widen))))
11351   (let (gnus-mark-article-hook)
11352     (gnus-summary-select-article t t nil article)))
11353
11354 (defun gnus-summary-toggle-display-buttonized ()
11355   "Toggle the buttonizing of the article buffer."
11356   (interactive)
11357   (require 'gnus-art)
11358   (if (setq gnus-inhibit-mime-unbuttonizing
11359             (not gnus-inhibit-mime-unbuttonizing))
11360       (let ((gnus-unbuttonized-mime-types nil))
11361         (gnus-summary-show-article))
11362     (gnus-summary-show-article)))
11363
11364 ;;;
11365 ;;; Generic summary marking commands
11366 ;;;
11367
11368 (defvar gnus-summary-marking-alist
11369   '((read gnus-del-mark "d")
11370     (unread gnus-unread-mark "u")
11371     (ticked gnus-ticked-mark "!")
11372     (dormant gnus-dormant-mark "?")
11373     (expirable gnus-expirable-mark "e"))
11374   "An alist of names/marks/keystrokes.")
11375
11376 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11377 (defvar gnus-summary-mark-map)
11378
11379 (defun gnus-summary-make-all-marking-commands ()
11380   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11381   (dolist (elem gnus-summary-marking-alist)
11382     (apply 'gnus-summary-make-marking-command elem)))
11383
11384 (defun gnus-summary-make-marking-command (name mark keystroke)
11385   (let ((map (make-sparse-keymap)))
11386     (define-key gnus-summary-generic-mark-map keystroke map)
11387     (dolist (lway `((next "next" next nil "n")
11388                     (next-unread "next unread" next t "N")
11389                     (prev "previous" prev nil "p")
11390                     (prev-unread "previous unread" prev t "P")
11391                     (nomove "" nil nil ,keystroke)))
11392       (let ((func (gnus-summary-make-marking-command-1
11393                    mark (car lway) lway name)))
11394         (setq func (eval func))
11395         (define-key map (nth 4 lway) func)))))
11396
11397 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11398   `(defun ,(intern
11399             (format "gnus-summary-put-mark-as-%s%s"
11400                     name (if (eq way 'nomove)
11401                              ""
11402                            (concat "-" (symbol-name way)))))
11403      (n)
11404      ,(format
11405        "Mark the current article as %s%s.
11406 If N, the prefix, then repeat N times.
11407 If N is negative, move in reverse order.
11408 The difference between N and the actual number of articles marked is
11409 returned."
11410        name (cadr lway))
11411      (interactive "p")
11412      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11413
11414 (defun gnus-summary-generic-mark (n mark move unread)
11415   "Mark N articles with MARK."
11416   (unless (eq major-mode 'gnus-summary-mode)
11417     (error "This command can only be used in the summary buffer"))
11418   (gnus-summary-show-thread)
11419   (let ((nummove
11420          (cond
11421           ((eq move 'next) 1)
11422           ((eq move 'prev) -1)
11423           (t 0))))
11424     (if (zerop nummove)
11425         (setq n 1)
11426       (when (< n 0)
11427         (setq n (abs n)
11428               nummove (* -1 nummove))))
11429     (while (and (> n 0)
11430                 (gnus-summary-mark-article nil mark)
11431                 (zerop (gnus-summary-next-subject nummove unread t)))
11432       (setq n (1- n)))
11433     (when (/= 0 n)
11434       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11435     (gnus-summary-recenter)
11436     (gnus-summary-position-point)
11437     (gnus-set-mode-line 'summary)
11438     n))
11439
11440 (defun gnus-summary-insert-articles (articles)
11441   (when (setq articles
11442               (gnus-sorted-difference articles
11443                                       (mapcar (lambda (h)
11444                                                 (mail-header-number h))
11445                                               gnus-newsgroup-headers)))
11446     (setq gnus-newsgroup-headers
11447           (gnus-merge 'list
11448                       gnus-newsgroup-headers
11449                       (gnus-fetch-headers articles)
11450                       'gnus-article-sort-by-number))
11451     ;; Suppress duplicates?
11452     (when gnus-suppress-duplicates
11453       (gnus-dup-suppress-articles))
11454
11455     ;; We might want to build some more threads first.
11456     (when (and gnus-fetch-old-headers
11457                (eq gnus-headers-retrieved-by 'nov))
11458       (if (eq gnus-fetch-old-headers 'invisible)
11459           (gnus-build-all-threads)
11460         (gnus-build-old-threads)))
11461     ;; Let the Gnus agent mark articles as read.
11462     (when gnus-agent
11463       (gnus-agent-get-undownloaded-list))
11464     ;; Remove list identifiers from subject
11465     (when gnus-list-identifiers
11466       (gnus-summary-remove-list-identifiers))
11467     ;; First and last article in this newsgroup.
11468     (when gnus-newsgroup-headers
11469       (setq gnus-newsgroup-begin
11470             (mail-header-number (car gnus-newsgroup-headers))
11471             gnus-newsgroup-end
11472             (mail-header-number
11473              (gnus-last-element gnus-newsgroup-headers))))
11474     (when gnus-use-scoring
11475       (gnus-possibly-score-headers))))
11476
11477 (defun gnus-summary-insert-old-articles (&optional all)
11478   "Insert all old articles in this group.
11479 If ALL is non-nil, already read articles become readable.
11480 If ALL is a number, fetch this number of articles."
11481   (interactive "P")
11482   (prog1
11483       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11484             older len)
11485         (setq older
11486               ;; Some nntp servers lie about their active range.  When
11487               ;; this happens, the active range can be in the millions.
11488               ;; Use a compressed range to avoid creating a huge list.
11489               (gnus-range-difference (list gnus-newsgroup-active) old))
11490         (setq len (gnus-range-length older))
11491         (cond
11492          ((null older) nil)
11493          ((numberp all)
11494           (if (< all len)
11495               (let ((older-range (nreverse older)))
11496                 (setq older nil)
11497
11498                 (while (> all 0)
11499                   (let* ((r (pop older-range))
11500                          (min (if (numberp r) r (car r)))
11501                          (max (if (numberp r) r (cdr r))))
11502                     (while (and (<= min max)
11503                                 (> all 0))
11504                       (push max older)
11505                       (setq all (1- all)
11506                             max (1- max))))))
11507             (setq older (gnus-uncompress-range older))))
11508          (all
11509           (setq older (gnus-uncompress-range older)))
11510          (t
11511           (when (and (numberp gnus-large-newsgroup)
11512                    (> len gnus-large-newsgroup))
11513               (let* ((cursor-in-echo-area nil)
11514                      (initial (gnus-parameter-large-newsgroup-initial
11515                                gnus-newsgroup-name))
11516                      (input
11517                       (read-string
11518                        (format
11519                         "How many articles from %s (%s %d): "
11520                         (gnus-limit-string
11521                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11522                         (if initial "max" "default")
11523                         len)
11524                        (if initial
11525                            (cons (number-to-string initial)
11526                                  0)))))
11527                 (unless (string-match "^[ \t]*$" input)
11528                   (setq all (string-to-number input))
11529                   (if (< all len)
11530                       (let ((older-range (nreverse older)))
11531                         (setq older nil)
11532
11533                         (while (> all 0)
11534                           (let* ((r (pop older-range))
11535                                  (min (if (numberp r) r (car r)))
11536                                  (max (if (numberp r) r (cdr r))))
11537                             (while (and (<= min max)
11538                                         (> all 0))
11539                               (push max older)
11540                               (setq all (1- all)
11541                                     max (1- max))))))))))
11542           (setq older (gnus-uncompress-range older))))
11543         (if (not older)
11544             (message "No old news.")
11545           (gnus-summary-insert-articles older)
11546           (gnus-summary-limit (gnus-sorted-nunion old older))))
11547     (gnus-summary-position-point)))
11548
11549 (defun gnus-summary-insert-new-articles ()
11550   "Insert all new articles in this group."
11551   (interactive)
11552   (prog1
11553       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11554             (old-active gnus-newsgroup-active)
11555             (nnmail-fetched-sources (list t))
11556             i new)
11557         (setq gnus-newsgroup-active
11558               (gnus-activate-group gnus-newsgroup-name 'scan))
11559         (setq i (cdr gnus-newsgroup-active))
11560         (while (> i (cdr old-active))
11561           (push i new)
11562           (decf i))
11563         (if (not new)
11564             (message "No gnus is bad news.")
11565           (gnus-summary-insert-articles new)
11566           (setq gnus-newsgroup-unreads
11567                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11568           (gnus-summary-limit (gnus-sorted-nunion old new))))
11569     (gnus-summary-position-point)))
11570
11571 (gnus-summary-make-all-marking-commands)
11572
11573 (gnus-ems-redefine)
11574
11575 (provide 'gnus-sum)
11576
11577 (run-hooks 'gnus-sum-load-hook)
11578
11579 ;; Local Variables:
11580 ;; coding: iso-8859-1
11581 ;; End:
11582
11583 ;;; gnus-sum.el ends here