Import Oort Gnus v0.17.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
50 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
51 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
52
53 (defcustom gnus-kill-summary-on-exit t
54   "*If non-nil, kill the summary buffer when you exit from it.
55 If nil, the summary will become a \"*Dead Summary*\" buffer, and
56 it will be killed sometime later."
57   :group 'gnus-summary-exit
58   :type 'boolean)
59
60 (defcustom gnus-fetch-old-headers nil
61   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
62 If an unread article in the group refers to an older, already read (or
63 just marked as read) article, the old article will not normally be
64 displayed in the Summary buffer.  If this variable is non-nil, Gnus
65 will attempt to grab the headers to the old articles, and thereby
66 build complete threads.  If it has the value `some', only enough
67 headers to connect otherwise loose threads will be displayed.  This
68 variable can also be a number.  In that case, no more than that number
69 of old headers will be fetched.  If it has the value `invisible', all
70 old headers will be fetched, but none will be displayed.
71
72 The server has to support NOV for any of this to work."
73   :group 'gnus-thread
74   :type '(choice (const :tag "off" nil)
75                  (const some)
76                  number
77                  (sexp :menu-tag "other" t)))
78
79 (defcustom gnus-refer-thread-limit 200
80   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
81 If t, fetch all the available old headers."
82   :group 'gnus-thread
83   :type '(choice number
84                  (sexp :menu-tag "other" t)))
85
86 (defcustom gnus-summary-make-false-root 'adopt
87   "*nil means that Gnus won't gather loose threads.
88 If the root of a thread has expired or been read in a previous
89 session, the information necessary to build a complete thread has been
90 lost.  Instead of having many small sub-threads from this original thread
91 scattered all over the summary buffer, Gnus can gather them.
92
93 If non-nil, Gnus will try to gather all loose sub-threads from an
94 original thread into one large thread.
95
96 If this variable is non-nil, it should be one of `none', `adopt',
97 `dummy' or `empty'.
98
99 If this variable is `none', Gnus will not make a false root, but just
100 present the sub-threads after another.
101 If this variable is `dummy', Gnus will create a dummy root that will
102 have all the sub-threads as children.
103 If this variable is `adopt', Gnus will make one of the \"children\"
104 the parent and mark all the step-children as such.
105 If this variable is `empty', the \"children\" are printed with empty
106 subject fields.  (Or rather, they will be printed with a string
107 given by the `gnus-summary-same-subject' variable.)"
108   :group 'gnus-thread
109   :type '(choice (const :tag "off" nil)
110                  (const none)
111                  (const dummy)
112                  (const adopt)
113                  (const empty)))
114
115 (defcustom gnus-summary-make-false-root-always nil
116   "Always make a false dummy root."
117   :group 'gnus-thread
118   :type 'boolean)
119
120 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
121   "*A regexp to match subjects to be excluded from loose thread gathering.
122 As loose thread gathering is done on subjects only, that means that
123 there can be many false gatherings performed.  By rooting out certain
124 common subjects, gathering might become saner."
125   :group 'gnus-thread
126   :type 'regexp)
127
128 (defcustom gnus-summary-gather-subject-limit nil
129   "*Maximum length of subject comparisons when gathering loose threads.
130 Use nil to compare full subjects.  Setting this variable to a low
131 number will help gather threads that have been corrupted by
132 newsreaders chopping off subject lines, but it might also mean that
133 unrelated articles that have subject that happen to begin with the
134 same few characters will be incorrectly gathered.
135
136 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
137 comparing subjects."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  (const fuzzy)
141                  (sexp :menu-tag "on" t)))
142
143 (defcustom gnus-simplify-subject-functions nil
144   "List of functions taking a string argument that simplify subjects.
145 The functions are applied recursively.
146
147 Useful functions to put in this list include:
148 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
149 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
150   :group 'gnus-thread
151   :type '(repeat function))
152
153 (defcustom gnus-simplify-ignored-prefixes nil
154   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  regexp))
158
159 (defcustom gnus-build-sparse-threads nil
160   "*If non-nil, fill in the gaps in threads.
161 If `some', only fill in the gaps that are needed to tie loose threads
162 together.  If `more', fill in all leaf nodes that Gnus can find.  If
163 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
164   :group 'gnus-thread
165   :type '(choice (const :tag "off" nil)
166                  (const some)
167                  (const more)
168                  (sexp :menu-tag "all" t)))
169
170 (defcustom gnus-summary-thread-gathering-function
171   'gnus-gather-threads-by-subject
172   "*Function used for gathering loose threads.
173 There are two pre-defined functions: `gnus-gather-threads-by-subject',
174 which only takes Subjects into consideration; and
175 `gnus-gather-threads-by-references', which compared the References
176 headers of the articles to find matches."
177   :group 'gnus-thread
178   :type '(radio (function-item gnus-gather-threads-by-subject)
179                 (function-item gnus-gather-threads-by-references)
180                 (function :tag "other")))
181
182 (defcustom gnus-summary-same-subject ""
183   "*String indicating that the current article has the same subject as the previous.
184 This variable will only be used if the value of
185 `gnus-summary-make-false-root' is `empty'."
186   :group 'gnus-summary-format
187   :type 'string)
188
189 (defcustom gnus-summary-goto-unread t
190   "*If t, many commands will go to the next unread article.
191 This applies to marking commands as well as other commands that
192 \"naturally\" select the next article, like, for instance, `SPC' at
193 the end of an article.
194
195 If nil, the marking commands do NOT go to the next unread article
196 \(they go to the next article instead).  If `never', commands that
197 usually go to the next unread article, will go to the next article,
198 whether it is read or not."
199   :group 'gnus-summary-marks
200   :link '(custom-manual "(gnus)Setting Marks")
201   :type '(choice (const :tag "off" nil)
202                  (const never)
203                  (sexp :menu-tag "on" t)))
204
205 (defcustom gnus-summary-default-score 0
206   "*Default article score level.
207 All scores generated by the score files will be added to this score.
208 If this variable is nil, scoring will be disabled."
209   :group 'gnus-score-default
210   :type '(choice (const :tag "disable")
211                  integer))
212
213 (defcustom gnus-summary-default-high-score 0
214   "*Default threshold for a high scored article.
215 An article will be highlighted as high scored if its score is greater
216 than this score."
217   :group 'gnus-score-default
218   :type 'integer)
219
220 (defcustom gnus-summary-default-low-score 0
221   "*Default threshold for a low scored article.
222 An article will be highlighted as low scored if its score is smaller
223 than this score."
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-zcore-fuzz 0
228   "*Fuzziness factor for the zcore in the summary buffer.
229 Articles with scores closer than this to `gnus-summary-default-score'
230 will not be marked."
231   :group 'gnus-summary-format
232   :type 'integer)
233
234 (defcustom gnus-simplify-subject-fuzzy-regexp nil
235   "*Strings to be removed when doing fuzzy matches.
236 This can either be a regular expression or list of regular expressions
237 that will be removed from subject strings if fuzzy subject
238 simplification is selected."
239   :group 'gnus-thread
240   :type '(repeat regexp))
241
242 (defcustom gnus-show-threads t
243   "*If non-nil, display threads in summary mode."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-subtree nil
248   "*If non-nil, hide all threads initially.
249 This can be a predicate specifier which says which threads to hide.
250 If threads are hidden, you have to run the command
251 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
252 to expose hidden threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-killed t
257   "*If non-nil, hide killed threads automatically."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-ignore-subject t
262   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
263 If nil, articles that have different subjects from their parents will
264 start separate threads."
265   :group 'gnus-thread
266   :type 'boolean)
267
268 (defcustom gnus-thread-operation-ignore-subject t
269   "*If non-nil, subjects will be ignored when doing thread commands.
270 This affects commands like `gnus-summary-kill-thread' and
271 `gnus-summary-lower-thread'.
272
273 If this variable is nil, articles in the same thread with different
274 subjects will not be included in the operation in question.  If this
275 variable is `fuzzy', only articles that have subjects that are fuzzily
276 equal will be included."
277   :group 'gnus-thread
278   :type '(choice (const :tag "off" nil)
279                  (const fuzzy)
280                  (sexp :tag "on" t)))
281
282 (defcustom gnus-thread-indent-level 4
283   "*Number that says how much each sub-thread should be indented."
284   :group 'gnus-thread
285   :type 'integer)
286
287 (defcustom gnus-auto-extend-newsgroup t
288   "*If non-nil, extend newsgroup forward and backward when requested."
289   :group 'gnus-summary-choose
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-first t
293   "*If non-nil, select the article under point.
294 Which article this is is controlled by the `gnus-auto-select-subject'
295 variable.
296
297 If you want to prevent automatic selection of articles in some
298 newsgroups, set the variable to nil in `gnus-select-group-hook'."
299   :group 'gnus-group-select
300   :type '(choice (const :tag "none" nil)
301                  (sexp :menu-tag "first" t)))
302
303 (defcustom gnus-auto-select-subject 'unread
304   "*Says what subject to place under point when entering a group.
305
306 This variable can either be the symbols `first' (place point on the
307 first subject), `unread' (place point on the subject line of the first
308 unread article), `best' (place point on the subject line of the
309 higest-scored article), `unseen' (place point on the subject line of
310 the first unseen article), 'unseen-or-unread' (place point on the subject
311 line of the first unseen article or, if all article have been seen, on the
312 subject line of the first unread article), or a function to be called to
313 place point on some subject line."
314   :group 'gnus-group-select
315   :type '(choice (const best)
316                  (const unread)
317                  (const first)
318                  (const unseen)
319                  (const unseen-or-unread)))
320
321 (defcustom gnus-auto-select-next t
322   "*If non-nil, offer to go to the next group from the end of the previous.
323 If the value is t and the next newsgroup is empty, Gnus will exit
324 summary mode and go back to group mode.  If the value is neither nil
325 nor t, Gnus will select the following unread newsgroup.  In
326 particular, if the value is the symbol `quietly', the next unread
327 newsgroup will be selected without any confirmation, and if it is
328 `almost-quietly', the next group will be selected without any
329 confirmation if you are located on the last article in the group.
330 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
331 will go to the next group without confirmation."
332   :group 'gnus-summary-maneuvering
333   :type '(choice (const :tag "off" nil)
334                  (const quietly)
335                  (const almost-quietly)
336                  (const slightly-quietly)
337                  (sexp :menu-tag "on" t)))
338
339 (defcustom gnus-auto-select-same nil
340   "*If non-nil, select the next article with the same subject.
341 If there are no more articles with the same subject, go to
342 the first unread article."
343   :group 'gnus-summary-maneuvering
344   :type 'boolean)
345
346 (defcustom gnus-auto-goto-ignores 'unfetched
347   "*Says how to handle unfetched articles when maneuvering.
348
349 This variable can either be the symbols `nil' (maneuver to any
350 article), `undownloaded' (maneuvering while unplugged ignores articles
351 that have not been fetched), `always-undownloaded' (maneuvering always
352 ignores articles that have not been fetched), `unfetched' (maneuvering
353 ignores articles whose headers have not been fetched).
354
355 NOTE: The list of unfetched articles will always be nil when plugged
356 and, when unplugged, a subset of the undownloaded article list."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "None" nil)
359                  (const :tag "Undownloaded when unplugged" undownloaded)
360                  (const :tag "Undownloaded" always-undownloaded)
361                  (const :tag "Unfetched" unfetched)))
362
363 (defcustom gnus-summary-check-current nil
364   "*If non-nil, consider the current article when moving.
365 The \"unread\" movement commands will stay on the same line if the
366 current article is unread."
367   :group 'gnus-summary-maneuvering
368   :type 'boolean)
369
370 (defcustom gnus-auto-center-summary t
371   "*If non-nil, always center the current summary buffer.
372 In particular, if `vertical' do only vertical recentering.  If non-nil
373 and non-`vertical', do both horizontal and vertical recentering."
374   :group 'gnus-summary-maneuvering
375   :type '(choice (const :tag "none" nil)
376                  (const vertical)
377                  (integer :tag "height")
378                  (sexp :menu-tag "both" t)))
379
380 (defvar gnus-auto-center-group t
381   "*If non-nil, always center the group buffer.")
382
383 (defcustom gnus-show-all-headers nil
384   "*If non-nil, don't hide any headers."
385   :group 'gnus-article-hiding
386   :group 'gnus-article-headers
387   :type 'boolean)
388
389 (defcustom gnus-summary-ignore-duplicates nil
390   "*If non-nil, ignore articles with identical Message-ID headers."
391   :group 'gnus-summary
392   :type 'boolean)
393
394 (defcustom gnus-single-article-buffer t
395   "*If non-nil, display all articles in the same buffer.
396 If nil, each group will get its own article buffer."
397   :group 'gnus-article-various
398   :type 'boolean)
399
400 (defcustom gnus-break-pages t
401   "*If non-nil, do page breaking on articles.
402 The page delimiter is specified by the `gnus-page-delimiter'
403 variable."
404   :group 'gnus-article-various
405   :type 'boolean)
406
407 (defcustom gnus-move-split-methods nil
408   "*Variable used to suggest where articles are to be moved to.
409 It uses the same syntax as the `gnus-split-methods' variable.
410 However, whereas `gnus-split-methods' specifies file names as targets,
411 this variable specifies group names."
412   :group 'gnus-summary-mail
413   :type '(repeat (choice (list :value (fun) function)
414                          (cons :value ("" "") regexp (repeat string))
415                          (sexp :value nil))))
416
417 (defcustom gnus-unread-mark ?           ;Whitespace
418   "*Mark used for unread articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-ticked-mark ?!
423   "*Mark used for ticked articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-dormant-mark ??
428   "*Mark used for dormant articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-del-mark ?r
433   "*Mark used for del'd articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-read-mark ?R
438   "*Mark used for read articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-expirable-mark ?E
443   "*Mark used for expirable articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-killed-mark ?K
448   "*Mark used for killed articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-spam-mark ?$
453   "*Mark used for spam articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-souped-mark ?F
458   "*Mark used for souped articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-kill-file-mark ?X
463   "*Mark used for articles killed by kill files."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-low-score-mark ?Y
468   "*Mark used for articles with a low score."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-catchup-mark ?C
473   "*Mark used for articles that are caught up."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-replied-mark ?A
478   "*Mark used for articles that have been replied to."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-forwarded-mark ?F
483   "*Mark used for articles that have been forwarded."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-recent-mark ?N
488   "*Mark used for articles that are recent."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-cached-mark ?*
493   "*Mark used for articles that are in the cache."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-saved-mark ?S
498   "*Mark used for articles that have been saved."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-unseen-mark ?.
503   "*Mark used for articles that haven't been seen."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-no-mark ?               ;Whitespace
508   "*Mark used for articles that have no other secondary mark."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-ancient-mark ?O
513   "*Mark used for ancient articles."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-sparse-mark ?Q
518   "*Mark used for sparsely reffed articles."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-canceled-mark ?G
523   "*Mark used for canceled articles."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-duplicate-mark ?M
528   "*Mark used for duplicate articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-undownloaded-mark ?-
533   "*Mark used for articles that weren't downloaded."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-downloaded-mark ?+
538   "*Mark used for articles that were downloaded."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-downloadable-mark ?%
543   "*Mark used for articles that are to be downloaded."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-unsendable-mark ?=
548   "*Mark used for articles that won't be sent."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-score-over-mark ?+
553   "*Score mark used for articles with high scores."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-score-below-mark ?-
558   "*Score mark used for articles with low scores."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-empty-thread-mark ?     ;Whitespace
563   "*There is no thread under the article."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-not-empty-thread-mark ?=
568   "*There is a thread under the article."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-view-pseudo-asynchronously nil
573   "*If non-nil, Gnus will view pseudo-articles asynchronously."
574   :group 'gnus-extract-view
575   :type 'boolean)
576
577 (defcustom gnus-auto-expirable-marks
578   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
579         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
580         gnus-souped-mark gnus-duplicate-mark)
581   "*The list of marks converted into expiration if a group is auto-expirable."
582   :version "21.1"
583   :group 'gnus-summary
584   :type '(repeat character))
585
586 (defcustom gnus-inhibit-user-auto-expire t
587   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
588   :version "21.1"
589   :group 'gnus-summary
590   :type 'boolean)
591
592 (defcustom gnus-view-pseudos nil
593   "*If `automatic', pseudo-articles will be viewed automatically.
594 If `not-confirm', pseudos will be viewed automatically, and the user
595 will not be asked to confirm the command."
596   :group 'gnus-extract-view
597   :type '(choice (const :tag "off" nil)
598                  (const automatic)
599                  (const not-confirm)))
600
601 (defcustom gnus-view-pseudos-separately t
602   "*If non-nil, one pseudo-article will be created for each file to be viewed.
603 If nil, all files that use the same viewing command will be given as a
604 list of parameters to that command."
605   :group 'gnus-extract-view
606   :type 'boolean)
607
608 (defcustom gnus-insert-pseudo-articles t
609   "*If non-nil, insert pseudo-articles when decoding articles."
610   :group 'gnus-extract-view
611   :type 'boolean)
612
613 (defcustom gnus-summary-dummy-line-format
614   "   %(:                             :%) %S\n"
615   "*The format specification for the dummy roots in the summary buffer.
616 It works along the same lines as a normal formatting string,
617 with some simple extensions.
618
619 %S  The subject
620
621 General format specifiers can also be used.
622 See `(gnus)Formatting Variables'."
623   :link '(custom-manual "(gnus)Formatting Variables")
624   :group 'gnus-threading
625   :type 'string)
626
627 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
628   "*The format specification for the summary mode line.
629 It works along the same lines as a normal formatting string,
630 with some simple extensions:
631
632 %G  Group name
633 %p  Unprefixed group name
634 %A  Current article number
635 %z  Current article score
636 %V  Gnus version
637 %U  Number of unread articles in the group
638 %e  Number of unselected articles in the group
639 %Z  A string with unread/unselected article counts
640 %g  Shortish group name
641 %S  Subject of the current article
642 %u  User-defined spec
643 %s  Current score file name
644 %d  Number of dormant articles
645 %r  Number of articles that have been marked as read in this session
646 %E  Number of articles expunged by the score files"
647   :group 'gnus-summary-format
648   :type 'string)
649
650 (defcustom gnus-list-identifiers nil
651   "Regexp that matches list identifiers to be removed from subject.
652 This can also be a list of regexps."
653   :version "21.1"
654   :group 'gnus-summary-format
655   :group 'gnus-article-hiding
656   :type '(choice (const :tag "none" nil)
657                  (regexp :value ".*")
658                  (repeat :value (".*") regexp)))
659
660 (defcustom gnus-summary-mark-below 0
661   "*Mark all articles with a score below this variable as read.
662 This variable is local to each summary buffer and usually set by the
663 score file."
664   :group 'gnus-score-default
665   :type 'integer)
666
667 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
668   "*List of functions used for sorting articles in the summary buffer.
669
670 Each function takes two articles and returns non-nil if the first
671 article should be sorted before the other.  If you use more than one
672 function, the primary sort function should be the last.  You should
673 probably always include `gnus-article-sort-by-number' in the list of
674 sorting functions -- preferably first.  Also note that sorting by date
675 is often much slower than sorting by number, and the sorting order is
676 very similar.  (Sorting by date means sorting by the time the message
677 was sent, sorting by number means sorting by arrival time.)
678
679 Ready-made functions include `gnus-article-sort-by-number',
680 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
681 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
682 and `gnus-article-sort-by-score'.
683
684 When threading is turned on, the variable `gnus-thread-sort-functions'
685 controls how articles are sorted."
686   :group 'gnus-summary-sort
687   :type '(repeat (choice (function-item gnus-article-sort-by-number)
688                          (function-item gnus-article-sort-by-author)
689                          (function-item gnus-article-sort-by-subject)
690                          (function-item gnus-article-sort-by-date)
691                          (function-item gnus-article-sort-by-score)
692                          (function-item gnus-article-sort-by-random)
693                          (function :tag "other"))))
694
695 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
696   "*List of functions used for sorting threads in the summary buffer.
697 By default, threads are sorted by article number.
698
699 Each function takes two threads and returns non-nil if the first
700 thread should be sorted before the other.  If you use more than one
701 function, the primary sort function should be the last.  You should
702 probably always include `gnus-thread-sort-by-number' in the list of
703 sorting functions -- preferably first.  Also note that sorting by date
704 is often much slower than sorting by number, and the sorting order is
705 very similar.  (Sorting by date means sorting by the time the message
706 was sent, sorting by number means sorting by arrival time.)
707
708 Ready-made functions include `gnus-thread-sort-by-number',
709 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
710 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
711 `gnus-thread-sort-by-most-recent-number',
712 `gnus-thread-sort-by-most-recent-date',
713 `gnus-thread-sort-by-random', and
714 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
715
716 When threading is turned off, the variable
717 `gnus-article-sort-functions' controls how articles are sorted."
718   :group 'gnus-summary-sort
719   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
720                          (function-item gnus-thread-sort-by-author)
721                          (function-item gnus-thread-sort-by-subject)
722                          (function-item gnus-thread-sort-by-date)
723                          (function-item gnus-thread-sort-by-score)
724                          (function-item gnus-thread-sort-by-total-score)
725                          (function-item gnus-thread-sort-by-random)
726                          (function :tag "other"))))
727
728 (defcustom gnus-thread-score-function '+
729   "*Function used for calculating the total score of a thread.
730
731 The function is called with the scores of the article and each
732 subthread and should then return the score of the thread.
733
734 Some functions you can use are `+', `max', or `min'."
735   :group 'gnus-summary-sort
736   :type 'function)
737
738 (defcustom gnus-summary-expunge-below nil
739   "All articles that have a score less than this variable will be expunged.
740 This variable is local to the summary buffers."
741   :group 'gnus-score-default
742   :type '(choice (const :tag "off" nil)
743                  integer))
744
745 (defcustom gnus-thread-expunge-below nil
746   "All threads that have a total score less than this variable will be expunged.
747 See `gnus-thread-score-function' for en explanation of what a
748 \"thread score\" is.
749
750 This variable is local to the summary buffers."
751   :group 'gnus-threading
752   :group 'gnus-score-default
753   :type '(choice (const :tag "off" nil)
754                  integer))
755
756 (defcustom gnus-summary-mode-hook nil
757   "*A hook for Gnus summary mode.
758 This hook is run before any variables are set in the summary buffer."
759   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
760   :group 'gnus-summary-various
761   :type 'hook)
762
763 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
764 (when (featurep 'xemacs)
765   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
766   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
767   (add-hook 'gnus-summary-mode-hook
768             'gnus-xmas-switch-horizontal-scrollbar-off))
769
770 (defcustom gnus-summary-menu-hook nil
771   "*Hook run after the creation of the summary mode menu."
772   :group 'gnus-summary-visual
773   :type 'hook)
774
775 (defcustom gnus-summary-exit-hook nil
776   "*A hook called on exit from the summary buffer.
777 It will be called with point in the group buffer."
778   :group 'gnus-summary-exit
779   :type 'hook)
780
781 (defcustom gnus-summary-prepare-hook nil
782   "*A hook called after the summary buffer has been generated.
783 If you want to modify the summary buffer, you can use this hook."
784   :group 'gnus-summary-various
785   :type 'hook)
786
787 (defcustom gnus-summary-prepared-hook nil
788   "*A hook called as the last thing after the summary buffer has been generated."
789   :group 'gnus-summary-various
790   :type 'hook)
791
792 (defcustom gnus-summary-generate-hook nil
793   "*A hook run just before generating the summary buffer.
794 This hook is commonly used to customize threading variables and the
795 like."
796   :group 'gnus-summary-various
797   :type 'hook)
798
799 (defcustom gnus-select-group-hook nil
800   "*A hook called when a newsgroup is selected.
801
802 If you'd like to simplify subjects like the
803 `gnus-summary-next-same-subject' command does, you can use the
804 following hook:
805
806  (add-hook gnus-select-group-hook
807            (lambda ()
808              (mapcar (lambda (header)
809                        (mail-header-set-subject
810                         header
811                         (gnus-simplify-subject
812                          (mail-header-subject header) 're-only)))
813                      gnus-newsgroup-headers)))"
814   :group 'gnus-group-select
815   :type 'hook)
816
817 (defcustom gnus-select-article-hook nil
818   "*A hook called when an article is selected."
819   :group 'gnus-summary-choose
820   :options '(gnus-agent-fetch-selected-article)
821   :type 'hook)
822
823 (defcustom gnus-visual-mark-article-hook
824   (list 'gnus-highlight-selected-summary)
825   "*Hook run after selecting an article in the summary buffer.
826 It is meant to be used for highlighting the article in some way.  It
827 is not run if `gnus-visual' is nil."
828   :group 'gnus-summary-visual
829   :type 'hook)
830
831 (defcustom gnus-parse-headers-hook nil
832   "*A hook called before parsing the headers."
833   :group 'gnus-various
834   :type 'hook)
835
836 (defcustom gnus-exit-group-hook nil
837   "*A hook called when exiting summary mode.
838 This hook is not called from the non-updating exit commands like `Q'."
839   :group 'gnus-various
840   :type 'hook)
841
842 (defcustom gnus-summary-update-hook
843   (list 'gnus-summary-highlight-line)
844   "*A hook called when a summary line is changed.
845 The hook will not be called if `gnus-visual' is nil.
846
847 The default function `gnus-summary-highlight-line' will
848 highlight the line according to the `gnus-summary-highlight'
849 variable."
850   :group 'gnus-summary-visual
851   :type 'hook)
852
853 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
854   "*A hook called when an article is selected for the first time.
855 The hook is intended to mark an article as read (or unread)
856 automatically when it is selected."
857   :group 'gnus-summary-choose
858   :type 'hook)
859
860 (defcustom gnus-group-no-more-groups-hook nil
861   "*A hook run when returning to group mode having no more (unread) groups."
862   :group 'gnus-group-select
863   :type 'hook)
864
865 (defcustom gnus-ps-print-hook nil
866   "*A hook run before ps-printing something from Gnus."
867   :group 'gnus-summary
868   :type 'hook)
869
870 (defcustom gnus-summary-article-move-hook nil
871   "*A hook called after an article is moved, copied, respooled, or crossposted."
872   :group 'gnus-summary
873   :type 'hook)
874
875 (defcustom gnus-summary-article-delete-hook nil
876   "*A hook called after an article is deleted."
877   :group 'gnus-summary
878   :type 'hook)
879
880 (defcustom gnus-summary-article-expire-hook nil
881   "*A hook called after an article is expired."
882   :group 'gnus-summary
883   :type 'hook)
884
885 (defcustom gnus-summary-display-arrow
886   (and (fboundp 'display-graphic-p)
887        (display-graphic-p))
888   "*If non-nil, display an arrow highlighting the current article."
889   :version "21.1"
890   :group 'gnus-summary
891   :type 'boolean)
892
893 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
894   "Face used for highlighting the current article in the summary buffer."
895   :group 'gnus-summary-visual
896   :type 'face)
897
898 (defvar gnus-tmp-downloaded nil)
899
900 (defcustom gnus-summary-highlight
901   '(((eq mark gnus-canceled-mark)
902      . gnus-summary-cancelled-face)
903     ((and uncached (> score default-high))
904      . gnus-summary-high-undownloaded-face)
905     ((and uncached (< score default-low))
906      . gnus-summary-low-undownloaded-face)
907     (uncached
908      . gnus-summary-normal-undownloaded-face)
909     ((and (> score default-high)
910           (or (eq mark gnus-dormant-mark)
911               (eq mark gnus-ticked-mark)))
912      . gnus-summary-high-ticked-face)
913     ((and (< score default-low)
914           (or (eq mark gnus-dormant-mark)
915               (eq mark gnus-ticked-mark)))
916      . gnus-summary-low-ticked-face)
917     ((or (eq mark gnus-dormant-mark)
918          (eq mark gnus-ticked-mark))
919      . gnus-summary-normal-ticked-face)
920     ((and (> score default-high) (eq mark gnus-ancient-mark))
921      . gnus-summary-high-ancient-face)
922     ((and (< score default-low) (eq mark gnus-ancient-mark))
923      . gnus-summary-low-ancient-face)
924     ((eq mark gnus-ancient-mark)
925      . gnus-summary-normal-ancient-face)
926     ((and (> score default-high) (eq mark gnus-unread-mark))
927      . gnus-summary-high-unread-face)
928     ((and (< score default-low) (eq mark gnus-unread-mark))
929      . gnus-summary-low-unread-face)
930     ((eq mark gnus-unread-mark)
931      . gnus-summary-normal-unread-face)
932     ((> score default-high)
933      . gnus-summary-high-read-face)
934     ((< score default-low)
935      . gnus-summary-low-read-face)
936     (t
937      . gnus-summary-normal-read-face))
938   "*Controls the highlighting of summary buffer lines.
939
940 A list of (FORM . FACE) pairs.  When deciding how a a particular
941 summary line should be displayed, each form is evaluated.  The content
942 of the face field after the first true form is used.  You can change
943 how those summary lines are displayed, by editing the face field.
944
945 You can use the following variables in the FORM field.
946
947 score:        The article's score
948 default:      The default article score.
949 default-high: The default score for high scored articles.
950 default-low:  The default score for low scored articles.
951 below:        The score below which articles are automatically marked as read.
952 mark:         The articles mark."
953   :group 'gnus-summary-visual
954   :type '(repeat (cons (sexp :tag "Form" nil)
955                        face)))
956
957 (defcustom gnus-alter-header-function nil
958   "Function called to allow alteration of article header structures.
959 The function is called with one parameter, the article header vector,
960 which it may alter in any way."
961   :type '(choice (const :tag "None" nil)
962                  function)
963   :group 'gnus-summary)
964
965 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
966   "Variable that says which function should be used to decode a string with encoded words.")
967
968 (defcustom gnus-extra-headers '(To Newsgroups)
969   "*Extra headers to parse."
970   :version "21.1"
971   :group 'gnus-summary
972   :type '(repeat symbol))
973
974 (defcustom gnus-ignored-from-addresses
975   (and user-mail-address (regexp-quote user-mail-address))
976   "*Regexp of From headers that may be suppressed in favor of To headers."
977   :version "21.1"
978   :group 'gnus-summary
979   :type 'regexp)
980
981 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
982   "List of charsets that should be ignored.
983 When these charsets are used in the \"charset\" parameter, the
984 default charset will be used instead."
985   :version "21.1"
986   :type '(repeat symbol)
987   :group 'gnus-charset)
988
989 (gnus-define-group-parameter
990  ignored-charsets
991  :type list
992  :function-document
993  "Return the ignored charsets of GROUP."
994  :variable gnus-group-ignored-charsets-alist
995  :variable-default
996  '(("alt\\.chinese\\.text" iso-8859-1))
997  :variable-document
998  "Alist of regexps (to match group names) and charsets that should be ignored.
999 When these charsets are used in the \"charset\" parameter, the
1000 default charset will be used instead."
1001  :variable-group gnus-charset
1002  :variable-type '(repeat (cons (regexp :tag "Group")
1003                                (repeat symbol)))
1004  :parameter-type '(choice :tag "Ignored charsets"
1005                           :value nil
1006                           (repeat (symbol)))
1007  :parameter-document       "\
1008 List of charsets that should be ignored.
1009
1010 When these charsets are used in the \"charset\" parameter, the
1011 default charset will be used instead.")
1012
1013 (defcustom gnus-group-highlight-words-alist nil
1014   "Alist of group regexps and highlight regexps.
1015 This variable uses the same syntax as `gnus-emphasis-alist'."
1016   :version "21.1"
1017   :type '(repeat (cons (regexp :tag "Group")
1018                        (repeat (list (regexp :tag "Highlight regexp")
1019                                      (number :tag "Group for entire word" 0)
1020                                      (number :tag "Group for displayed part" 0)
1021                                      (symbol :tag "Face"
1022                                              gnus-emphasis-highlight-words)))))
1023   :group 'gnus-summary-visual)
1024
1025 (defcustom gnus-summary-show-article-charset-alist
1026   nil
1027   "Alist of number and charset.
1028 The article will be shown with the charset corresponding to the
1029 numbered argument.
1030 For example: ((1 . cn-gb-2312) (2 . big5))."
1031   :version "21.1"
1032   :type '(repeat (cons (number :tag "Argument" 1)
1033                        (symbol :tag "Charset")))
1034   :group 'gnus-charset)
1035
1036 (defcustom gnus-preserve-marks t
1037   "Whether marks are preserved when moving, copying and respooling messages."
1038   :version "21.1"
1039   :type 'boolean
1040   :group 'gnus-summary-marks)
1041
1042 (defcustom gnus-alter-articles-to-read-function nil
1043   "Function to be called to alter the list of articles to be selected."
1044   :type '(choice (const nil) function)
1045   :group 'gnus-summary)
1046
1047 (defcustom gnus-orphan-score nil
1048   "*All orphans get this score added.  Set in the score file."
1049   :group 'gnus-score-default
1050   :type '(choice (const nil)
1051                  integer))
1052
1053 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1054   "*A regexp to match MIME parts when saving multiple parts of a
1055 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1056 This regexp will be used by default when prompting the user for which
1057 type of files to save."
1058   :group 'gnus-summary
1059   :type 'regexp)
1060
1061 (defcustom gnus-read-all-available-headers nil
1062   "Whether Gnus should parse all headers made available to it.
1063 This is mostly relevant for slow backends where the user may
1064 wish to widen the summary buffer to include all headers
1065 that were fetched.  Say, for nnultimate groups."
1066   :group 'gnus-summary
1067   :type '(choice boolean regexp))
1068
1069 (defcustom gnus-summary-muttprint-program "muttprint"
1070   "Command (and optional arguments) used to run Muttprint."
1071   :version "21.3"
1072   :group 'gnus-summary
1073   :type 'string)
1074
1075 (defcustom gnus-article-loose-mime nil
1076   "If non-nil, don't require MIME-Version header.
1077 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1078 supply the MIME-Version header or deliberately strip it From the mail.
1079 Set it to non-nil, Gnus will treat some articles as MIME even if
1080 the MIME-Version header is missed."
1081   :version "21.3"
1082   :type 'boolean
1083   :group 'gnus-article-mime)
1084
1085 (defcustom gnus-article-emulate-mime t
1086   "If non-nil, use MIME emulation for uuencode and the like.
1087 This means that Gnus will search message bodies for text that look
1088 like uuencoded bits, yEncoded bits, and so on, and present that using
1089 the normal Gnus MIME machinery."
1090   :type 'boolean
1091   :group 'gnus-article-mime)
1092
1093 ;;; Internal variables
1094
1095 (defvar gnus-summary-display-cache nil)
1096 (defvar gnus-article-mime-handles nil)
1097 (defvar gnus-article-decoded-p nil)
1098 (defvar gnus-article-charset nil)
1099 (defvar gnus-article-ignored-charsets nil)
1100 (defvar gnus-scores-exclude-files nil)
1101 (defvar gnus-page-broken nil)
1102
1103 (defvar gnus-original-article nil)
1104 (defvar gnus-article-internal-prepare-hook nil)
1105 (defvar gnus-newsgroup-process-stack nil)
1106
1107 (defvar gnus-thread-indent-array nil)
1108 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1109 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1110   "Function called to sort the articles within a thread after it has been gathered together.")
1111
1112 (defvar gnus-summary-save-parts-type-history nil)
1113 (defvar gnus-summary-save-parts-last-directory nil)
1114
1115 ;; Avoid highlighting in kill files.
1116 (defvar gnus-summary-inhibit-highlight nil)
1117 (defvar gnus-newsgroup-selected-overlay nil)
1118 (defvar gnus-inhibit-limiting nil)
1119 (defvar gnus-newsgroup-adaptive-score-file nil)
1120 (defvar gnus-current-score-file nil)
1121 (defvar gnus-current-move-group nil)
1122 (defvar gnus-current-copy-group nil)
1123 (defvar gnus-current-crosspost-group nil)
1124 (defvar gnus-newsgroup-display nil)
1125
1126 (defvar gnus-newsgroup-dependencies nil)
1127 (defvar gnus-newsgroup-adaptive nil)
1128 (defvar gnus-summary-display-article-function nil)
1129 (defvar gnus-summary-highlight-line-function nil
1130   "Function called after highlighting a summary line.")
1131
1132 (defvar gnus-summary-line-format-alist
1133   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1134     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1135     (?s gnus-tmp-subject-or-nil ?s)
1136     (?n gnus-tmp-name ?s)
1137     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1138         ?s)
1139     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1140             gnus-tmp-from) ?s)
1141     (?F gnus-tmp-from ?s)
1142     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1143     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1144     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1145     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1146     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1147     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1148     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1149     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1150     (?L gnus-tmp-lines ?s)
1151     (?O gnus-tmp-downloaded ?c)
1152     (?I gnus-tmp-indentation ?s)
1153     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1154     (?R gnus-tmp-replied ?c)
1155     (?\[ gnus-tmp-opening-bracket ?c)
1156     (?\] gnus-tmp-closing-bracket ?c)
1157     (?\> (make-string gnus-tmp-level ? ) ?s)
1158     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1159     (?i gnus-tmp-score ?d)
1160     (?z gnus-tmp-score-char ?c)
1161     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1162     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1163     (?U gnus-tmp-unread ?c)
1164     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1165         ?s)
1166     (?t (gnus-summary-number-of-articles-in-thread
1167          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1168         ?d)
1169     (?e (gnus-summary-number-of-articles-in-thread
1170          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1171         ?c)
1172     (?u gnus-tmp-user-defined ?s)
1173     (?P (gnus-pick-line-number) ?d)
1174     (?B gnus-tmp-thread-tree-header-string ?s)
1175     (user-date (gnus-user-date
1176                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1177   "An alist of format specifications that can appear in summary lines.
1178 These are paired with what variables they correspond with, along with
1179 the type of the variable (string, integer, character, etc).")
1180
1181 (defvar gnus-summary-dummy-line-format-alist
1182   `((?S gnus-tmp-subject ?s)
1183     (?N gnus-tmp-number ?d)
1184     (?u gnus-tmp-user-defined ?s)))
1185
1186 (defvar gnus-summary-mode-line-format-alist
1187   `((?G gnus-tmp-group-name ?s)
1188     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1189     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1190     (?A gnus-tmp-article-number ?d)
1191     (?Z gnus-tmp-unread-and-unselected ?s)
1192     (?V gnus-version ?s)
1193     (?U gnus-tmp-unread-and-unticked ?d)
1194     (?S gnus-tmp-subject ?s)
1195     (?e gnus-tmp-unselected ?d)
1196     (?u gnus-tmp-user-defined ?s)
1197     (?d (length gnus-newsgroup-dormant) ?d)
1198     (?t (length gnus-newsgroup-marked) ?d)
1199     (?h (length gnus-newsgroup-spam-marked) ?d)
1200     (?r (length gnus-newsgroup-reads) ?d)
1201     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1202     (?E gnus-newsgroup-expunged-tally ?d)
1203     (?s (gnus-current-score-file-nondirectory) ?s)))
1204
1205 (defvar gnus-last-search-regexp nil
1206   "Default regexp for article search command.")
1207
1208 (defvar gnus-last-shell-command nil
1209   "Default shell command on article.")
1210
1211 (defvar gnus-newsgroup-agentized nil
1212   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1213 (defvar gnus-newsgroup-begin nil)
1214 (defvar gnus-newsgroup-end nil)
1215 (defvar gnus-newsgroup-last-rmail nil)
1216 (defvar gnus-newsgroup-last-mail nil)
1217 (defvar gnus-newsgroup-last-folder nil)
1218 (defvar gnus-newsgroup-last-file nil)
1219 (defvar gnus-newsgroup-auto-expire nil)
1220 (defvar gnus-newsgroup-active nil)
1221
1222 (defvar gnus-newsgroup-data nil)
1223 (defvar gnus-newsgroup-data-reverse nil)
1224 (defvar gnus-newsgroup-limit nil)
1225 (defvar gnus-newsgroup-limits nil)
1226
1227 (defvar gnus-newsgroup-unreads nil
1228   "Sorted list of unread articles in the current newsgroup.")
1229
1230 (defvar gnus-newsgroup-unselected nil
1231   "Sorted list of unselected unread articles in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-reads nil
1234   "Alist of read articles and article marks in the current newsgroup.")
1235
1236 (defvar gnus-newsgroup-expunged-tally nil)
1237
1238 (defvar gnus-newsgroup-marked nil
1239   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1240
1241 (defvar gnus-newsgroup-spam-marked nil
1242   "List of ranges of articles that have been marked as spam.")
1243
1244 (defvar gnus-newsgroup-killed nil
1245   "List of ranges of articles that have been through the scoring process.")
1246
1247 (defvar gnus-newsgroup-cached nil
1248   "Sorted list of articles that come from the article cache.")
1249
1250 (defvar gnus-newsgroup-saved nil
1251   "List of articles that have been saved.")
1252
1253 (defvar gnus-newsgroup-kill-headers nil)
1254
1255 (defvar gnus-newsgroup-replied nil
1256   "List of articles that have been replied to in the current newsgroup.")
1257
1258 (defvar gnus-newsgroup-forwarded nil
1259   "List of articles that have been forwarded in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-recent nil
1262   "List of articles that have are recent in the current newsgroup.")
1263
1264 (defvar gnus-newsgroup-expirable nil
1265   "Sorted list of articles in the current newsgroup that can be expired.")
1266
1267 (defvar gnus-newsgroup-processable nil
1268   "List of articles in the current newsgroup that can be processed.")
1269
1270 (defvar gnus-newsgroup-downloadable nil
1271   "Sorted list of articles in the current newsgroup that can be processed.")
1272
1273 (defvar gnus-newsgroup-unfetched nil
1274   "Sorted list of articles in the current newsgroup whose headers have
1275 not been fetched into the agent.
1276
1277 This list will always be a subset of gnus-newsgroup-undownloaded.")
1278
1279 (defvar gnus-newsgroup-undownloaded nil
1280   "List of articles in the current newsgroup that haven't been downloaded.")
1281
1282 (defvar gnus-newsgroup-unsendable nil
1283   "List of articles in the current newsgroup that won't be sent.")
1284
1285 (defvar gnus-newsgroup-bookmarks nil
1286   "List of articles in the current newsgroup that have bookmarks.")
1287
1288 (defvar gnus-newsgroup-dormant nil
1289   "Sorted list of dormant articles in the current newsgroup.")
1290
1291 (defvar gnus-newsgroup-unseen nil
1292   "List of unseen articles in the current newsgroup.")
1293
1294 (defvar gnus-newsgroup-seen nil
1295   "Range of seen articles in the current newsgroup.")
1296
1297 (defvar gnus-newsgroup-articles nil
1298   "List of articles in the current newsgroup.")
1299
1300 (defvar gnus-newsgroup-scored nil
1301   "List of scored articles in the current newsgroup.")
1302
1303 (defvar gnus-newsgroup-headers nil
1304   "List of article headers in the current newsgroup.")
1305
1306 (defvar gnus-newsgroup-threads nil)
1307
1308 (defvar gnus-newsgroup-prepared nil
1309   "Whether the current group has been prepared properly.")
1310
1311 (defvar gnus-newsgroup-ancient nil
1312   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1313
1314 (defvar gnus-newsgroup-sparse nil)
1315
1316 (defvar gnus-current-article nil)
1317 (defvar gnus-article-current nil)
1318 (defvar gnus-current-headers nil)
1319 (defvar gnus-have-all-headers nil)
1320 (defvar gnus-last-article nil)
1321 (defvar gnus-newsgroup-history nil)
1322 (defvar gnus-newsgroup-charset nil)
1323 (defvar gnus-newsgroup-ephemeral-charset nil)
1324 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1325
1326 (defvar gnus-article-before-search nil)
1327
1328 (defconst gnus-summary-local-variables
1329   '(gnus-newsgroup-name
1330     gnus-newsgroup-begin gnus-newsgroup-end
1331     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1332     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1333     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1334     gnus-newsgroup-unselected gnus-newsgroup-marked
1335     gnus-newsgroup-spam-marked
1336     gnus-newsgroup-reads gnus-newsgroup-saved
1337     gnus-newsgroup-replied gnus-newsgroup-forwarded
1338     gnus-newsgroup-recent
1339     gnus-newsgroup-expirable
1340     gnus-newsgroup-processable gnus-newsgroup-killed
1341     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1342     gnus-newsgroup-unfetched
1343     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1344     gnus-newsgroup-seen gnus-newsgroup-articles
1345     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1346     gnus-newsgroup-headers gnus-newsgroup-threads
1347     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1348     gnus-current-article gnus-current-headers gnus-have-all-headers
1349     gnus-last-article gnus-article-internal-prepare-hook
1350     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1351     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1352     gnus-thread-expunge-below
1353     gnus-score-alist gnus-current-score-file
1354     (gnus-summary-expunge-below . global)
1355     (gnus-summary-mark-below . global)
1356     (gnus-orphan-score . global)
1357     gnus-newsgroup-active gnus-scores-exclude-files
1358     gnus-newsgroup-history gnus-newsgroup-ancient
1359     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1360     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1361     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1362     (gnus-newsgroup-expunged-tally . 0)
1363     gnus-cache-removable-articles gnus-newsgroup-cached
1364     gnus-newsgroup-data gnus-newsgroup-data-reverse
1365     gnus-newsgroup-limit gnus-newsgroup-limits
1366     gnus-newsgroup-charset gnus-newsgroup-display)
1367   "Variables that are buffer-local to the summary buffers.")
1368
1369 (defvar gnus-newsgroup-variables nil
1370   "A list of variables that have separate values in different newsgroups.
1371 A list of newsgroup (summary buffer) local variables, or cons of
1372 variables and their default values (when the default values are not
1373 nil), that should be made global while the summary buffer is active.
1374 These variables can be used to set variables in the group parameters
1375 while still allowing them to affect operations done in other
1376 buffers. For example:
1377
1378 \(setq gnus-newsgroup-variables
1379      '(message-use-followup-to
1380        (gnus-visible-headers .
1381          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1382 ")
1383
1384 ;; Byte-compiler warning.
1385 (eval-when-compile
1386   ;; Bind features so that require will believe that gnus-sum has
1387   ;; already been loaded (avoids infinite recursion)
1388   (let ((features (cons 'gnus-sum features)))
1389     ;; Several of the declarations in gnus-sum are needed to load the
1390     ;; following files. Right now, these definitions have been
1391     ;; compiled but not defined (evaluated).  We could either do a
1392     ;; eval-and-compile about all of the declarations or evaluate the
1393     ;; source file.
1394     (if (boundp 'gnus-newsgroup-variables)
1395         nil
1396       (load "gnus-sum.el" t t t t))
1397     (require 'gnus)
1398     (require 'gnus-agent)
1399     (require 'gnus-art)))
1400
1401 ;; MIME stuff.
1402
1403 (defvar gnus-decode-encoded-word-methods
1404   '(mail-decode-encoded-word-string)
1405   "List of methods used to decode encoded words.
1406
1407 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1408 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1409 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1410 whose names match REGEXP.
1411
1412 For example:
1413 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1414  mail-decode-encoded-word-string
1415  (\"chinese\" . rfc1843-decode-string))")
1416
1417 (defvar gnus-decode-encoded-word-methods-cache nil)
1418
1419 (defun gnus-multi-decode-encoded-word-string (string)
1420   "Apply the functions from `gnus-encoded-word-methods' that match."
1421   (unless (and gnus-decode-encoded-word-methods-cache
1422                (eq gnus-newsgroup-name
1423                    (car gnus-decode-encoded-word-methods-cache)))
1424     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1425     (mapcar (lambda (x)
1426               (if (symbolp x)
1427                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1428                 (if (and gnus-newsgroup-name
1429                          (string-match (car x) gnus-newsgroup-name))
1430                     (nconc gnus-decode-encoded-word-methods-cache
1431                            (list (cdr x))))))
1432             gnus-decode-encoded-word-methods))
1433   (let ((xlist gnus-decode-encoded-word-methods-cache))
1434     (pop xlist)
1435     (while xlist
1436       (setq string (funcall (pop xlist) string))))
1437   string)
1438
1439 ;; Subject simplification.
1440
1441 (defun gnus-simplify-whitespace (str)
1442   "Remove excessive whitespace from STR."
1443   ;; Multiple spaces.
1444   (while (string-match "[ \t][ \t]+" str)
1445     (setq str (concat (substring str 0 (match-beginning 0))
1446                         " "
1447                         (substring str (match-end 0)))))
1448   ;; Leading spaces.
1449   (when (string-match "^[ \t]+" str)
1450     (setq str (substring str (match-end 0))))
1451   ;; Trailing spaces.
1452   (when (string-match "[ \t]+$" str)
1453     (setq str (substring str 0 (match-beginning 0))))
1454   str)
1455
1456 (defun gnus-simplify-all-whitespace (str)
1457   "Remove all whitespace from STR."
1458   (while (string-match "[ \t\n]+" str)
1459     (setq str (replace-match "" nil nil str)))
1460   str)
1461
1462 (defsubst gnus-simplify-subject-re (subject)
1463   "Remove \"Re:\" from subject lines."
1464   (if (string-match message-subject-re-regexp subject)
1465       (substring subject (match-end 0))
1466     subject))
1467
1468 (defun gnus-simplify-subject (subject &optional re-only)
1469   "Remove `Re:' and words in parentheses.
1470 If RE-ONLY is non-nil, strip leading `Re:'s only."
1471   (let ((case-fold-search t))           ;Ignore case.
1472     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1473     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1474       (setq subject (substring subject (match-end 0))))
1475     ;; Remove uninteresting prefixes.
1476     (when (and (not re-only)
1477                gnus-simplify-ignored-prefixes
1478                (string-match gnus-simplify-ignored-prefixes subject))
1479       (setq subject (substring subject (match-end 0))))
1480     ;; Remove words in parentheses from end.
1481     (unless re-only
1482       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1483         (setq subject (substring subject 0 (match-beginning 0)))))
1484     ;; Return subject string.
1485     subject))
1486
1487 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1488 ;; all whitespace.
1489 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1490   (goto-char (point-min))
1491   (while (re-search-forward regexp nil t)
1492     (replace-match (or newtext ""))))
1493
1494 (defun gnus-simplify-buffer-fuzzy ()
1495   "Simplify string in the buffer fuzzily.
1496 The string in the accessible portion of the current buffer is simplified.
1497 It is assumed to be a single-line subject.
1498 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1499 matter is removed.  Additional things can be deleted by setting
1500 `gnus-simplify-subject-fuzzy-regexp'."
1501   (let ((case-fold-search t)
1502         (modified-tick))
1503     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1504
1505     (while (not (eq modified-tick (buffer-modified-tick)))
1506       (setq modified-tick (buffer-modified-tick))
1507       (cond
1508        ((listp gnus-simplify-subject-fuzzy-regexp)
1509         (mapcar 'gnus-simplify-buffer-fuzzy-step
1510                 gnus-simplify-subject-fuzzy-regexp))
1511        (gnus-simplify-subject-fuzzy-regexp
1512         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1513       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1514       (gnus-simplify-buffer-fuzzy-step
1515        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1516       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1517
1518     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1519     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1520     (gnus-simplify-buffer-fuzzy-step " $")
1521     (gnus-simplify-buffer-fuzzy-step "^ +")))
1522
1523 (defun gnus-simplify-subject-fuzzy (subject)
1524   "Simplify a subject string fuzzily.
1525 See `gnus-simplify-buffer-fuzzy' for details."
1526   (save-excursion
1527     (gnus-set-work-buffer)
1528     (let ((case-fold-search t))
1529       ;; Remove uninteresting prefixes.
1530       (when (and gnus-simplify-ignored-prefixes
1531                  (string-match gnus-simplify-ignored-prefixes subject))
1532         (setq subject (substring subject (match-end 0))))
1533       (insert subject)
1534       (inline (gnus-simplify-buffer-fuzzy))
1535       (buffer-string))))
1536
1537 (defsubst gnus-simplify-subject-fully (subject)
1538   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1539   (cond
1540    (gnus-simplify-subject-functions
1541     (gnus-map-function gnus-simplify-subject-functions subject))
1542    ((null gnus-summary-gather-subject-limit)
1543     (gnus-simplify-subject-re subject))
1544    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1545     (gnus-simplify-subject-fuzzy subject))
1546    ((numberp gnus-summary-gather-subject-limit)
1547     (gnus-limit-string (gnus-simplify-subject-re subject)
1548                        gnus-summary-gather-subject-limit))
1549    (t
1550     subject)))
1551
1552 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1553   "Check whether two subjects are equal.
1554 If optional argument SIMPLE-FIRST is t, first argument is already
1555 simplified."
1556   (cond
1557    ((null simple-first)
1558     (equal (gnus-simplify-subject-fully s1)
1559            (gnus-simplify-subject-fully s2)))
1560    (t
1561     (equal s1
1562            (gnus-simplify-subject-fully s2)))))
1563
1564 (defun gnus-summary-bubble-group ()
1565   "Increase the score of the current group.
1566 This is a handy function to add to `gnus-summary-exit-hook' to
1567 increase the score of each group you read."
1568   (gnus-group-add-score gnus-newsgroup-name))
1569
1570 \f
1571 ;;;
1572 ;;; Gnus summary mode
1573 ;;;
1574
1575 (put 'gnus-summary-mode 'mode-class 'special)
1576
1577 (defvar gnus-article-commands-menu)
1578
1579 ;; Non-orthogonal keys
1580
1581 (gnus-define-keys gnus-summary-mode-map
1582   " " gnus-summary-next-page
1583   "\177" gnus-summary-prev-page
1584   [delete] gnus-summary-prev-page
1585   [backspace] gnus-summary-prev-page
1586   "\r" gnus-summary-scroll-up
1587   "\M-\r" gnus-summary-scroll-down
1588   "n" gnus-summary-next-unread-article
1589   "p" gnus-summary-prev-unread-article
1590   "N" gnus-summary-next-article
1591   "P" gnus-summary-prev-article
1592   "\M-\C-n" gnus-summary-next-same-subject
1593   "\M-\C-p" gnus-summary-prev-same-subject
1594   "\M-n" gnus-summary-next-unread-subject
1595   "\M-p" gnus-summary-prev-unread-subject
1596   "." gnus-summary-first-unread-article
1597   "," gnus-summary-best-unread-article
1598   "\M-s" gnus-summary-search-article-forward
1599   "\M-r" gnus-summary-search-article-backward
1600   "<" gnus-summary-beginning-of-article
1601   ">" gnus-summary-end-of-article
1602   "j" gnus-summary-goto-article
1603   "^" gnus-summary-refer-parent-article
1604   "\M-^" gnus-summary-refer-article
1605   "u" gnus-summary-tick-article-forward
1606   "!" gnus-summary-tick-article-forward
1607   "U" gnus-summary-tick-article-backward
1608   "d" gnus-summary-mark-as-read-forward
1609   "D" gnus-summary-mark-as-read-backward
1610   "E" gnus-summary-mark-as-expirable
1611   "\M-u" gnus-summary-clear-mark-forward
1612   "\M-U" gnus-summary-clear-mark-backward
1613   "k" gnus-summary-kill-same-subject-and-select
1614   "\C-k" gnus-summary-kill-same-subject
1615   "\M-\C-k" gnus-summary-kill-thread
1616   "\M-\C-l" gnus-summary-lower-thread
1617   "e" gnus-summary-edit-article
1618   "#" gnus-summary-mark-as-processable
1619   "\M-#" gnus-summary-unmark-as-processable
1620   "\M-\C-t" gnus-summary-toggle-threads
1621   "\M-\C-s" gnus-summary-show-thread
1622   "\M-\C-h" gnus-summary-hide-thread
1623   "\M-\C-f" gnus-summary-next-thread
1624   "\M-\C-b" gnus-summary-prev-thread
1625   [(meta down)] gnus-summary-next-thread
1626   [(meta up)] gnus-summary-prev-thread
1627   "\M-\C-u" gnus-summary-up-thread
1628   "\M-\C-d" gnus-summary-down-thread
1629   "&" gnus-summary-execute-command
1630   "c" gnus-summary-catchup-and-exit
1631   "\C-w" gnus-summary-mark-region-as-read
1632   "\C-t" gnus-summary-toggle-truncation
1633   "?" gnus-summary-mark-as-dormant
1634   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1635   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1636   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1637   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1638   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1639   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1640   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1641   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1642   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1643   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1644   "=" gnus-summary-expand-window
1645   "\C-x\C-s" gnus-summary-reselect-current-group
1646   "\M-g" gnus-summary-rescan-group
1647   "w" gnus-summary-stop-page-breaking
1648   "\C-c\C-r" gnus-summary-caesar-message
1649   "f" gnus-summary-followup
1650   "F" gnus-summary-followup-with-original
1651   "C" gnus-summary-cancel-article
1652   "r" gnus-summary-reply
1653   "R" gnus-summary-reply-with-original
1654   "\C-c\C-f" gnus-summary-mail-forward
1655   "o" gnus-summary-save-article
1656   "\C-o" gnus-summary-save-article-mail
1657   "|" gnus-summary-pipe-output
1658   "\M-k" gnus-summary-edit-local-kill
1659   "\M-K" gnus-summary-edit-global-kill
1660   ;; "V" gnus-version
1661   "\C-c\C-d" gnus-summary-describe-group
1662   "q" gnus-summary-exit
1663   "Q" gnus-summary-exit-no-update
1664   "\C-c\C-i" gnus-info-find-node
1665   gnus-mouse-2 gnus-mouse-pick-article
1666   "m" gnus-summary-mail-other-window
1667   "a" gnus-summary-post-news
1668   "i" gnus-summary-news-other-window
1669   "x" gnus-summary-limit-to-unread
1670   "s" gnus-summary-isearch-article
1671   "t" gnus-summary-toggle-header
1672   "g" gnus-summary-show-article
1673   "l" gnus-summary-goto-last-article
1674   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1675   "\C-d" gnus-summary-enter-digest-group
1676   "\M-\C-d" gnus-summary-read-document
1677   "\M-\C-e" gnus-summary-edit-parameters
1678   "\M-\C-a" gnus-summary-customize-parameters
1679   "\C-c\C-b" gnus-bug
1680   "*" gnus-cache-enter-article
1681   "\M-*" gnus-cache-remove-article
1682   "\M-&" gnus-summary-universal-argument
1683   "\C-l" gnus-recenter
1684   "I" gnus-summary-increase-score
1685   "L" gnus-summary-lower-score
1686   "\M-i" gnus-symbolic-argument
1687   "h" gnus-summary-select-article-buffer
1688
1689   "b" gnus-article-view-part
1690   "\M-t" gnus-summary-toggle-display-buttonized
1691
1692   "V" gnus-summary-score-map
1693   "X" gnus-uu-extract-map
1694   "S" gnus-summary-send-map)
1695
1696 ;; Sort of orthogonal keymap
1697 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1698   "t" gnus-summary-tick-article-forward
1699   "!" gnus-summary-tick-article-forward
1700   "d" gnus-summary-mark-as-read-forward
1701   "r" gnus-summary-mark-as-read-forward
1702   "c" gnus-summary-clear-mark-forward
1703   " " gnus-summary-clear-mark-forward
1704   "e" gnus-summary-mark-as-expirable
1705   "x" gnus-summary-mark-as-expirable
1706   "?" gnus-summary-mark-as-dormant
1707   "b" gnus-summary-set-bookmark
1708   "B" gnus-summary-remove-bookmark
1709   "#" gnus-summary-mark-as-processable
1710   "\M-#" gnus-summary-unmark-as-processable
1711   "S" gnus-summary-limit-include-expunged
1712   "C" gnus-summary-catchup
1713   "H" gnus-summary-catchup-to-here
1714   "h" gnus-summary-catchup-from-here
1715   "\C-c" gnus-summary-catchup-all
1716   "k" gnus-summary-kill-same-subject-and-select
1717   "K" gnus-summary-kill-same-subject
1718   "P" gnus-uu-mark-map)
1719
1720 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1721   "c" gnus-summary-clear-above
1722   "u" gnus-summary-tick-above
1723   "m" gnus-summary-mark-above
1724   "k" gnus-summary-kill-below)
1725
1726 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1727   "/" gnus-summary-limit-to-subject
1728   "n" gnus-summary-limit-to-articles
1729   "w" gnus-summary-pop-limit
1730   "s" gnus-summary-limit-to-subject
1731   "a" gnus-summary-limit-to-author
1732   "u" gnus-summary-limit-to-unread
1733   "m" gnus-summary-limit-to-marks
1734   "M" gnus-summary-limit-exclude-marks
1735   "v" gnus-summary-limit-to-score
1736   "*" gnus-summary-limit-include-cached
1737   "D" gnus-summary-limit-include-dormant
1738   "T" gnus-summary-limit-include-thread
1739   "d" gnus-summary-limit-exclude-dormant
1740   "t" gnus-summary-limit-to-age
1741   "." gnus-summary-limit-to-unseen
1742   "x" gnus-summary-limit-to-extra
1743   "p" gnus-summary-limit-to-display-predicate
1744   "E" gnus-summary-limit-include-expunged
1745   "c" gnus-summary-limit-exclude-childless-dormant
1746   "C" gnus-summary-limit-mark-excluded-as-read
1747   "o" gnus-summary-insert-old-articles
1748   "N" gnus-summary-insert-new-articles)
1749
1750 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1751   "n" gnus-summary-next-unread-article
1752   "p" gnus-summary-prev-unread-article
1753   "N" gnus-summary-next-article
1754   "P" gnus-summary-prev-article
1755   "\C-n" gnus-summary-next-same-subject
1756   "\C-p" gnus-summary-prev-same-subject
1757   "\M-n" gnus-summary-next-unread-subject
1758   "\M-p" gnus-summary-prev-unread-subject
1759   "f" gnus-summary-first-unread-article
1760   "b" gnus-summary-best-unread-article
1761   "j" gnus-summary-goto-article
1762   "g" gnus-summary-goto-subject
1763   "l" gnus-summary-goto-last-article
1764   "o" gnus-summary-pop-article)
1765
1766 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1767   "k" gnus-summary-kill-thread
1768   "l" gnus-summary-lower-thread
1769   "i" gnus-summary-raise-thread
1770   "T" gnus-summary-toggle-threads
1771   "t" gnus-summary-rethread-current
1772   "^" gnus-summary-reparent-thread
1773   "s" gnus-summary-show-thread
1774   "S" gnus-summary-show-all-threads
1775   "h" gnus-summary-hide-thread
1776   "H" gnus-summary-hide-all-threads
1777   "n" gnus-summary-next-thread
1778   "p" gnus-summary-prev-thread
1779   "u" gnus-summary-up-thread
1780   "o" gnus-summary-top-thread
1781   "d" gnus-summary-down-thread
1782   "#" gnus-uu-mark-thread
1783   "\M-#" gnus-uu-unmark-thread)
1784
1785 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1786   "g" gnus-summary-prepare
1787   "c" gnus-summary-insert-cached-articles
1788   "d" gnus-summary-insert-dormant-articles)
1789
1790 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1791   "c" gnus-summary-catchup-and-exit
1792   "C" gnus-summary-catchup-all-and-exit
1793   "E" gnus-summary-exit-no-update
1794   "Q" gnus-summary-exit
1795   "Z" gnus-summary-exit
1796   "n" gnus-summary-catchup-and-goto-next-group
1797   "R" gnus-summary-reselect-current-group
1798   "G" gnus-summary-rescan-group
1799   "N" gnus-summary-next-group
1800   "s" gnus-summary-save-newsrc
1801   "P" gnus-summary-prev-group)
1802
1803 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1804   " " gnus-summary-next-page
1805   "n" gnus-summary-next-page
1806   "\177" gnus-summary-prev-page
1807   [delete] gnus-summary-prev-page
1808   "p" gnus-summary-prev-page
1809   "\r" gnus-summary-scroll-up
1810   "\M-\r" gnus-summary-scroll-down
1811   "<" gnus-summary-beginning-of-article
1812   ">" gnus-summary-end-of-article
1813   "b" gnus-summary-beginning-of-article
1814   "e" gnus-summary-end-of-article
1815   "^" gnus-summary-refer-parent-article
1816   "r" gnus-summary-refer-parent-article
1817   "D" gnus-summary-enter-digest-group
1818   "R" gnus-summary-refer-references
1819   "T" gnus-summary-refer-thread
1820   "g" gnus-summary-show-article
1821   "s" gnus-summary-isearch-article
1822   "P" gnus-summary-print-article
1823   "M" gnus-mailing-list-insinuate
1824   "t" gnus-article-babel)
1825
1826 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1827   "b" gnus-article-add-buttons
1828   "B" gnus-article-add-buttons-to-head
1829   "o" gnus-article-treat-overstrike
1830   "e" gnus-article-emphasize
1831   "w" gnus-article-fill-cited-article
1832   "Q" gnus-article-fill-long-lines
1833   "C" gnus-article-capitalize-sentences
1834   "c" gnus-article-remove-cr
1835   "q" gnus-article-de-quoted-unreadable
1836   "6" gnus-article-de-base64-unreadable
1837   "Z" gnus-article-decode-HZ
1838   "h" gnus-article-wash-html
1839   "u" gnus-article-unsplit-urls
1840   "s" gnus-summary-force-verify-and-decrypt
1841   "f" gnus-article-display-x-face
1842   "l" gnus-summary-stop-page-breaking
1843   "r" gnus-summary-caesar-message
1844   "m" gnus-summary-morse-message
1845   "t" gnus-summary-toggle-header
1846   "g" gnus-treat-smiley
1847   "v" gnus-summary-verbose-headers
1848   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1849   "p" gnus-article-verify-x-pgp-sig
1850   "d" gnus-article-treat-dumbquotes)
1851
1852 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1853   ;; mnemonic: deuglif*Y*
1854   "u" gnus-article-outlook-unwrap-lines
1855   "a" gnus-article-outlook-repair-attribution
1856   "c" gnus-article-outlook-rearrange-citation
1857   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1858
1859 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1860   "a" gnus-article-hide
1861   "h" gnus-article-hide-headers
1862   "b" gnus-article-hide-boring-headers
1863   "s" gnus-article-hide-signature
1864   "c" gnus-article-hide-citation
1865   "C" gnus-article-hide-citation-in-followups
1866   "l" gnus-article-hide-list-identifiers
1867   "B" gnus-article-strip-banner
1868   "P" gnus-article-hide-pem
1869   "\C-c" gnus-article-hide-citation-maybe)
1870
1871 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1872   "a" gnus-article-highlight
1873   "h" gnus-article-highlight-headers
1874   "c" gnus-article-highlight-citation
1875   "s" gnus-article-highlight-signature)
1876
1877 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1878   "f" gnus-article-treat-fold-headers
1879   "u" gnus-article-treat-unfold-headers
1880   "n" gnus-article-treat-fold-newsgroups)
1881
1882 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1883   "x" gnus-article-display-x-face
1884   "d" gnus-article-display-face
1885   "s" gnus-treat-smiley
1886   "D" gnus-article-remove-images
1887   "f" gnus-treat-from-picon
1888   "m" gnus-treat-mail-picon
1889   "n" gnus-treat-newsgroups-picon)
1890
1891 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1892   "w" gnus-article-decode-mime-words
1893   "c" gnus-article-decode-charset
1894   "v" gnus-mime-view-all-parts
1895   "b" gnus-article-view-part)
1896
1897 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1898   "z" gnus-article-date-ut
1899   "u" gnus-article-date-ut
1900   "l" gnus-article-date-local
1901   "p" gnus-article-date-english
1902   "e" gnus-article-date-lapsed
1903   "o" gnus-article-date-original
1904   "i" gnus-article-date-iso8601
1905   "s" gnus-article-date-user)
1906
1907 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1908   "t" gnus-article-remove-trailing-blank-lines
1909   "l" gnus-article-strip-leading-blank-lines
1910   "m" gnus-article-strip-multiple-blank-lines
1911   "a" gnus-article-strip-blank-lines
1912   "A" gnus-article-strip-all-blank-lines
1913   "s" gnus-article-strip-leading-space
1914   "e" gnus-article-strip-trailing-space
1915   "w" gnus-article-remove-leading-whitespace)
1916
1917 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1918   "v" gnus-version
1919   "f" gnus-summary-fetch-faq
1920   "d" gnus-summary-describe-group
1921   "h" gnus-summary-describe-briefly
1922   "i" gnus-info-find-node
1923   "c" gnus-group-fetch-charter
1924   "C" gnus-group-fetch-control)
1925
1926 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1927   "e" gnus-summary-expire-articles
1928   "\M-\C-e" gnus-summary-expire-articles-now
1929   "\177" gnus-summary-delete-article
1930   [delete] gnus-summary-delete-article
1931   [backspace] gnus-summary-delete-article
1932   "m" gnus-summary-move-article
1933   "r" gnus-summary-respool-article
1934   "w" gnus-summary-edit-article
1935   "c" gnus-summary-copy-article
1936   "B" gnus-summary-crosspost-article
1937   "q" gnus-summary-respool-query
1938   "t" gnus-summary-respool-trace
1939   "i" gnus-summary-import-article
1940   "I" gnus-summary-create-article
1941   "p" gnus-summary-article-posted-p)
1942
1943 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1944   "o" gnus-summary-save-article
1945   "m" gnus-summary-save-article-mail
1946   "F" gnus-summary-write-article-file
1947   "r" gnus-summary-save-article-rmail
1948   "f" gnus-summary-save-article-file
1949   "b" gnus-summary-save-article-body-file
1950   "h" gnus-summary-save-article-folder
1951   "v" gnus-summary-save-article-vm
1952   "p" gnus-summary-pipe-output
1953   "P" gnus-summary-muttprint
1954   "s" gnus-soup-add-article)
1955
1956 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1957   "b" gnus-summary-display-buttonized
1958   "m" gnus-summary-repair-multipart
1959   "v" gnus-article-view-part
1960   "o" gnus-article-save-part
1961   "c" gnus-article-copy-part
1962   "C" gnus-article-view-part-as-charset
1963   "e" gnus-article-view-part-externally
1964   "E" gnus-article-encrypt-body
1965   "i" gnus-article-inline-part
1966   "|" gnus-article-pipe-part)
1967
1968 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1969   "p" gnus-summary-mark-as-processable
1970   "u" gnus-summary-unmark-as-processable
1971   "U" gnus-summary-unmark-all-processable
1972   "v" gnus-uu-mark-over
1973   "s" gnus-uu-mark-series
1974   "r" gnus-uu-mark-region
1975   "g" gnus-uu-unmark-region
1976   "R" gnus-uu-mark-by-regexp
1977   "G" gnus-uu-unmark-by-regexp
1978   "t" gnus-uu-mark-thread
1979   "T" gnus-uu-unmark-thread
1980   "a" gnus-uu-mark-all
1981   "b" gnus-uu-mark-buffer
1982   "S" gnus-uu-mark-sparse
1983   "k" gnus-summary-kill-process-mark
1984   "y" gnus-summary-yank-process-mark
1985   "w" gnus-summary-save-process-mark
1986   "i" gnus-uu-invert-processable)
1987
1988 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1989   ;;"x" gnus-uu-extract-any
1990   "m" gnus-summary-save-parts
1991   "u" gnus-uu-decode-uu
1992   "U" gnus-uu-decode-uu-and-save
1993   "s" gnus-uu-decode-unshar
1994   "S" gnus-uu-decode-unshar-and-save
1995   "o" gnus-uu-decode-save
1996   "O" gnus-uu-decode-save
1997   "b" gnus-uu-decode-binhex
1998   "B" gnus-uu-decode-binhex
1999   "p" gnus-uu-decode-postscript
2000   "P" gnus-uu-decode-postscript-and-save)
2001
2002 (gnus-define-keys
2003     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2004   "u" gnus-uu-decode-uu-view
2005   "U" gnus-uu-decode-uu-and-save-view
2006   "s" gnus-uu-decode-unshar-view
2007   "S" gnus-uu-decode-unshar-and-save-view
2008   "o" gnus-uu-decode-save-view
2009   "O" gnus-uu-decode-save-view
2010   "b" gnus-uu-decode-binhex-view
2011   "B" gnus-uu-decode-binhex-view
2012   "p" gnus-uu-decode-postscript-view
2013   "P" gnus-uu-decode-postscript-and-save-view)
2014
2015 (defvar gnus-article-post-menu nil)
2016
2017 (defconst gnus-summary-menu-maxlen 20)
2018
2019 (defun gnus-summary-menu-split (menu)
2020   ;; If we have lots of elements, divide them into groups of 20
2021   ;; and make a pane (or submenu) for each one.
2022   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2023       (let ((menu menu) sublists next
2024             (i 1))
2025         (while menu
2026           ;; Pull off the next gnus-summary-menu-maxlen elements
2027           ;; and make them the next element of sublist.
2028           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2029           (if next
2030               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2031                       nil))
2032           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2033                                              (aref (car (last menu)) 0)) menu)
2034                                sublists))
2035           (setq i (1+ i))
2036           (setq menu next))
2037         (nreverse sublists))
2038     ;; Few elements--put them all in one pane.
2039     menu))
2040
2041 (defun gnus-summary-make-menu-bar ()
2042   (gnus-turn-off-edit-menu 'summary)
2043
2044   (unless (boundp 'gnus-summary-misc-menu)
2045
2046     (easy-menu-define
2047       gnus-summary-kill-menu gnus-summary-mode-map ""
2048       (cons
2049        "Score"
2050        (nconc
2051         (list
2052          ["Customize" gnus-score-customize t])
2053         (gnus-make-score-map 'increase)
2054         (gnus-make-score-map 'lower)
2055         '(("Mark"
2056            ["Kill below" gnus-summary-kill-below t]
2057            ["Mark above" gnus-summary-mark-above t]
2058            ["Tick above" gnus-summary-tick-above t]
2059            ["Clear above" gnus-summary-clear-above t])
2060           ["Current score" gnus-summary-current-score t]
2061           ["Set score" gnus-summary-set-score t]
2062           ["Switch current score file..." gnus-score-change-score-file t]
2063           ["Set mark below..." gnus-score-set-mark-below t]
2064           ["Set expunge below..." gnus-score-set-expunge-below t]
2065           ["Edit current score file" gnus-score-edit-current-scores t]
2066           ["Edit score file" gnus-score-edit-file t]
2067           ["Trace score" gnus-score-find-trace t]
2068           ["Find words" gnus-score-find-favourite-words t]
2069           ["Rescore buffer" gnus-summary-rescore t]
2070           ["Increase score..." gnus-summary-increase-score t]
2071           ["Lower score..." gnus-summary-lower-score t]))))
2072
2073     ;; Define both the Article menu in the summary buffer and the
2074     ;; equivalent Commands menu in the article buffer here for
2075     ;; consistency.
2076     (let ((innards
2077            `(("Hide"
2078               ["All" gnus-article-hide t]
2079               ["Headers" gnus-article-hide-headers t]
2080               ["Signature" gnus-article-hide-signature t]
2081               ["Citation" gnus-article-hide-citation t]
2082               ["List identifiers" gnus-article-hide-list-identifiers t]
2083               ["Banner" gnus-article-strip-banner t]
2084               ["Boring headers" gnus-article-hide-boring-headers t])
2085              ("Highlight"
2086               ["All" gnus-article-highlight t]
2087               ["Headers" gnus-article-highlight-headers t]
2088               ["Signature" gnus-article-highlight-signature t]
2089               ["Citation" gnus-article-highlight-citation t])
2090              ("MIME"
2091               ["Words" gnus-article-decode-mime-words t]
2092               ["Charset" gnus-article-decode-charset t]
2093               ["QP" gnus-article-de-quoted-unreadable t]
2094               ["Base64" gnus-article-de-base64-unreadable t]
2095               ["View MIME buttons" gnus-summary-display-buttonized t]
2096               ["View all" gnus-mime-view-all-parts t]
2097               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2098               ["Encrypt body" gnus-article-encrypt-body t]
2099               ["Extract all parts" gnus-summary-save-parts t]
2100               ("Multipart"
2101                ["Repair multipart" gnus-summary-repair-multipart t]
2102                ["Add buttons" gnus-summary-display-buttonized t]
2103                ["Pipe part" gnus-article-pipe-part t]
2104                ["Inline part" gnus-article-inline-part t]
2105                ["Encrypt body" gnus-article-encrypt-body t]
2106                ["View part externally" gnus-article-view-part-externally t]
2107                ["View part with charset" gnus-article-view-part-as-charset t]
2108                ["Copy part" gnus-article-copy-part t]
2109                ["Save part" gnus-article-save-part t]
2110                ["View part" gnus-article-view-part t]))
2111              ("Date"
2112               ["Local" gnus-article-date-local t]
2113               ["ISO8601" gnus-article-date-iso8601 t]
2114               ["UT" gnus-article-date-ut t]
2115               ["Original" gnus-article-date-original t]
2116               ["Lapsed" gnus-article-date-lapsed t]
2117               ["User-defined" gnus-article-date-user t])
2118              ("Display"
2119               ["Remove images" gnus-article-remove-images t]
2120               ["Toggle smiley" gnus-treat-smiley t]
2121               ["Show X-Face" gnus-article-display-x-face t]
2122               ["Show picons in From" gnus-treat-from-picon t]
2123               ["Show picons in mail headers" gnus-treat-mail-picon t]
2124               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2125               ("View as different encoding"
2126                ,@(gnus-summary-menu-split
2127                   (mapcar
2128                    (lambda (cs)
2129                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2130                      ;; forms for menu commands, we should provide intern'ed
2131                      ;; function symbols.
2132                      (let ((command (intern (format "\
2133 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2134                        (fset command
2135                              `(lambda ()
2136                                 (interactive)
2137                                 (let ((gnus-summary-show-article-charset-alist
2138                                        '((1 . ,cs))))
2139                                   (gnus-summary-show-article 1))))
2140                        `[,(symbol-name cs) ,command t]))
2141                    (sort (if (fboundp 'coding-system-list)
2142                              (coding-system-list)
2143                            (mapcar 'car mm-mime-mule-charset-alist))
2144                          'string<)))))
2145              ("Washing"
2146               ("Remove Blanks"
2147                ["Leading" gnus-article-strip-leading-blank-lines t]
2148                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2149                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2150                ["All of the above" gnus-article-strip-blank-lines t]
2151                ["All" gnus-article-strip-all-blank-lines t]
2152                ["Leading space" gnus-article-strip-leading-space t]
2153                ["Trailing space" gnus-article-strip-trailing-space t]
2154                ["Leading space in headers"
2155                 gnus-article-remove-leading-whitespace t])
2156               ["Overstrike" gnus-article-treat-overstrike t]
2157               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2158               ["Emphasis" gnus-article-emphasize t]
2159               ["Word wrap" gnus-article-fill-cited-article t]
2160               ["Fill long lines" gnus-article-fill-long-lines t]
2161               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2162               ["CR" gnus-article-remove-cr t]
2163               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2164               ["Base64" gnus-article-de-base64-unreadable t]
2165               ["Rot 13" gnus-summary-caesar-message
2166                ,@(if (featurep 'xemacs) '(t)
2167                    '(:help "\"Caesar rotate\" article by 13"))]
2168               ["Morse decode" gnus-summary-morse-message t]
2169               ["Unix pipe..." gnus-summary-pipe-message t]
2170               ["Add buttons" gnus-article-add-buttons t]
2171               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2172               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2173               ["Verbose header" gnus-summary-verbose-headers t]
2174               ["Toggle header" gnus-summary-toggle-header t]
2175               ["Unfold headers" gnus-article-treat-unfold-headers t]
2176               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2177               ["Html" gnus-article-wash-html t]
2178               ["URLs" gnus-article-unsplit-urls t]
2179               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2180               ["HZ" gnus-article-decode-HZ t]
2181               ("(Outlook) Deuglify"
2182                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2183                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2184                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2185                ["Full (Outlook) deuglify"
2186                 gnus-article-outlook-deuglify-article t])
2187               )
2188              ("Output"
2189               ["Save in default format" gnus-summary-save-article
2190                ,@(if (featurep 'xemacs) '(t)
2191                    '(:help "Save article using default method"))]
2192               ["Save in file" gnus-summary-save-article-file
2193                ,@(if (featurep 'xemacs) '(t)
2194                    '(:help "Save article in file"))]
2195               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2196               ["Save in MH folder" gnus-summary-save-article-folder t]
2197               ["Save in VM folder" gnus-summary-save-article-vm t]
2198               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2199               ["Save body in file" gnus-summary-save-article-body-file t]
2200               ["Pipe through a filter" gnus-summary-pipe-output t]
2201               ["Add to SOUP packet" gnus-soup-add-article t]
2202               ["Print with Muttprint" gnus-summary-muttprint t]
2203               ["Print" gnus-summary-print-article t])
2204              ("Backend"
2205               ["Respool article..." gnus-summary-respool-article t]
2206               ["Move article..." gnus-summary-move-article
2207                (gnus-check-backend-function
2208                 'request-move-article gnus-newsgroup-name)]
2209               ["Copy article..." gnus-summary-copy-article t]
2210               ["Crosspost article..." gnus-summary-crosspost-article
2211                (gnus-check-backend-function
2212                 'request-replace-article gnus-newsgroup-name)]
2213               ["Import file..." gnus-summary-import-article t]
2214               ["Create article..." gnus-summary-create-article t]
2215               ["Check if posted" gnus-summary-article-posted-p t]
2216               ["Edit article" gnus-summary-edit-article
2217                (not (gnus-group-read-only-p))]
2218               ["Delete article" gnus-summary-delete-article
2219                (gnus-check-backend-function
2220                 'request-expire-articles gnus-newsgroup-name)]
2221               ["Query respool" gnus-summary-respool-query t]
2222               ["Trace respool" gnus-summary-respool-trace t]
2223               ["Delete expirable articles" gnus-summary-expire-articles-now
2224                (gnus-check-backend-function
2225                 'request-expire-articles gnus-newsgroup-name)])
2226              ("Extract"
2227               ["Uudecode" gnus-uu-decode-uu
2228                ,@(if (featurep 'xemacs) '(t)
2229                    '(:help "Decode uuencoded article(s)"))]
2230               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2231               ["Unshar" gnus-uu-decode-unshar t]
2232               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2233               ["Save" gnus-uu-decode-save t]
2234               ["Binhex" gnus-uu-decode-binhex t]
2235               ["Postscript" gnus-uu-decode-postscript t]
2236               ["All MIME parts" gnus-summary-save-parts t])
2237              ("Cache"
2238               ["Enter article" gnus-cache-enter-article t]
2239               ["Remove article" gnus-cache-remove-article t])
2240              ["Translate" gnus-article-babel t]
2241              ["Select article buffer" gnus-summary-select-article-buffer t]
2242              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2243              ["Isearch article..." gnus-summary-isearch-article t]
2244              ["Beginning of the article" gnus-summary-beginning-of-article t]
2245              ["End of the article" gnus-summary-end-of-article t]
2246              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2247              ["Fetch referenced articles" gnus-summary-refer-references t]
2248              ["Fetch current thread" gnus-summary-refer-thread t]
2249              ["Fetch article with id..." gnus-summary-refer-article t]
2250              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2251              ["Redisplay" gnus-summary-show-article t]
2252              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2253       (easy-menu-define
2254         gnus-summary-article-menu gnus-summary-mode-map ""
2255         (cons "Article" innards))
2256
2257       (if (not (keymapp gnus-summary-article-menu))
2258           (easy-menu-define
2259             gnus-article-commands-menu gnus-article-mode-map ""
2260             (cons "Commands" innards))
2261         ;; in Emacs, don't share menu.
2262         (setq gnus-article-commands-menu
2263               (copy-keymap gnus-summary-article-menu))
2264         (define-key gnus-article-mode-map [menu-bar commands]
2265           (cons "Commands" gnus-article-commands-menu))))
2266
2267     (easy-menu-define
2268       gnus-summary-thread-menu gnus-summary-mode-map ""
2269       '("Threads"
2270         ["Find all messages in thread" gnus-summary-refer-thread t]
2271         ["Toggle threading" gnus-summary-toggle-threads t]
2272         ["Hide threads" gnus-summary-hide-all-threads t]
2273         ["Show threads" gnus-summary-show-all-threads t]
2274         ["Hide thread" gnus-summary-hide-thread t]
2275         ["Show thread" gnus-summary-show-thread t]
2276         ["Go to next thread" gnus-summary-next-thread t]
2277         ["Go to previous thread" gnus-summary-prev-thread t]
2278         ["Go down thread" gnus-summary-down-thread t]
2279         ["Go up thread" gnus-summary-up-thread t]
2280         ["Top of thread" gnus-summary-top-thread t]
2281         ["Mark thread as read" gnus-summary-kill-thread t]
2282         ["Lower thread score" gnus-summary-lower-thread t]
2283         ["Raise thread score" gnus-summary-raise-thread t]
2284         ["Rethread current" gnus-summary-rethread-current t]))
2285
2286     (easy-menu-define
2287       gnus-summary-post-menu gnus-summary-mode-map ""
2288       `("Post"
2289         ["Send a message (mail or news)" gnus-summary-post-news
2290          ,@(if (featurep 'xemacs) '(t)
2291              '(:help "Post an article"))]
2292         ["Followup" gnus-summary-followup
2293          ,@(if (featurep 'xemacs) '(t)
2294              '(:help "Post followup to this article"))]
2295         ["Followup and yank" gnus-summary-followup-with-original
2296          ,@(if (featurep 'xemacs) '(t)
2297              '(:help "Post followup to this article, quoting its contents"))]
2298         ["Supersede article" gnus-summary-supersede-article t]
2299         ["Cancel article" gnus-summary-cancel-article
2300          ,@(if (featurep 'xemacs) '(t)
2301              '(:help "Cancel an article you posted"))]
2302         ["Reply" gnus-summary-reply t]
2303         ["Reply and yank" gnus-summary-reply-with-original t]
2304         ["Wide reply" gnus-summary-wide-reply t]
2305         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2306          ,@(if (featurep 'xemacs) '(t)
2307              '(:help "Mail a reply, quoting this article"))]
2308         ["Very wide reply" gnus-summary-very-wide-reply t]
2309         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2310          ,@(if (featurep 'xemacs) '(t)
2311              '(:help "Mail a very wide reply, quoting this article"))]
2312         ["Mail forward" gnus-summary-mail-forward t]
2313         ["Post forward" gnus-summary-post-forward t]
2314         ["Digest and mail" gnus-uu-digest-mail-forward t]
2315         ["Digest and post" gnus-uu-digest-post-forward t]
2316         ["Resend message" gnus-summary-resend-message t]
2317         ["Resend message edit" gnus-summary-resend-message-edit t]
2318         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2319         ["Send a mail" gnus-summary-mail-other-window t]
2320         ["Create a local message" gnus-summary-news-other-window t]
2321         ["Uuencode and post" gnus-uu-post-news
2322          ,@(if (featurep 'xemacs) '(t)
2323              '(:help "Post a uuencoded article"))]
2324         ["Followup via news" gnus-summary-followup-to-mail t]
2325         ["Followup via news and yank"
2326          gnus-summary-followup-to-mail-with-original t]
2327         ;;("Draft"
2328         ;;["Send" gnus-summary-send-draft t]
2329         ;;["Send bounced" gnus-resend-bounced-mail t])
2330         ))
2331
2332     (cond
2333      ((not (keymapp gnus-summary-post-menu))
2334       (setq gnus-article-post-menu gnus-summary-post-menu))
2335      ((not gnus-article-post-menu)
2336       ;; Don't share post menu.
2337       (setq gnus-article-post-menu
2338             (copy-keymap gnus-summary-post-menu))))
2339     (define-key gnus-article-mode-map [menu-bar post]
2340       (cons "Post" gnus-article-post-menu))
2341
2342     (easy-menu-define
2343       gnus-summary-misc-menu gnus-summary-mode-map ""
2344       `("Gnus"
2345         ("Mark Read"
2346          ["Mark as read" gnus-summary-mark-as-read-forward t]
2347          ["Mark same subject and select"
2348           gnus-summary-kill-same-subject-and-select t]
2349          ["Mark same subject" gnus-summary-kill-same-subject t]
2350          ["Catchup" gnus-summary-catchup
2351           ,@(if (featurep 'xemacs) '(t)
2352               '(:help "Mark unread articles in this group as read"))]
2353          ["Catchup all" gnus-summary-catchup-all t]
2354          ["Catchup to here" gnus-summary-catchup-to-here t]
2355          ["Catchup from here" gnus-summary-catchup-from-here t]
2356          ["Catchup region" gnus-summary-mark-region-as-read 
2357           (gnus-mark-active-p)]
2358          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2359         ("Mark Various"
2360          ["Tick" gnus-summary-tick-article-forward t]
2361          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2362          ["Remove marks" gnus-summary-clear-mark-forward t]
2363          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2364          ["Set bookmark" gnus-summary-set-bookmark t]
2365          ["Remove bookmark" gnus-summary-remove-bookmark t])
2366         ("Limit to"
2367          ["Marks..." gnus-summary-limit-to-marks t]
2368          ["Subject..." gnus-summary-limit-to-subject t]
2369          ["Author..." gnus-summary-limit-to-author t]
2370          ["Age..." gnus-summary-limit-to-age t]
2371          ["Extra..." gnus-summary-limit-to-extra t]
2372          ["Score..." gnus-summary-limit-to-score t]
2373          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2374          ["Unread" gnus-summary-limit-to-unread t]
2375          ["Unseen" gnus-summary-limit-to-unseen t]
2376          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2377          ["Next articles" gnus-summary-limit-to-articles t]
2378          ["Pop limit" gnus-summary-pop-limit t]
2379          ["Show dormant" gnus-summary-limit-include-dormant t]
2380          ["Hide childless dormant"
2381           gnus-summary-limit-exclude-childless-dormant t]
2382          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2383          ["Hide marked" gnus-summary-limit-exclude-marks t]
2384          ["Show expunged" gnus-summary-limit-include-expunged t])
2385         ("Process Mark"
2386          ["Set mark" gnus-summary-mark-as-processable t]
2387          ["Remove mark" gnus-summary-unmark-as-processable t]
2388          ["Remove all marks" gnus-summary-unmark-all-processable t]
2389          ["Mark above" gnus-uu-mark-over t]
2390          ["Mark series" gnus-uu-mark-series t]
2391          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2392          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2393          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2394          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2395          ["Mark all" gnus-uu-mark-all t]
2396          ["Mark buffer" gnus-uu-mark-buffer t]
2397          ["Mark sparse" gnus-uu-mark-sparse t]
2398          ["Mark thread" gnus-uu-mark-thread t]
2399          ["Unmark thread" gnus-uu-unmark-thread t]
2400          ("Process Mark Sets"
2401           ["Kill" gnus-summary-kill-process-mark t]
2402           ["Yank" gnus-summary-yank-process-mark
2403            gnus-newsgroup-process-stack]
2404           ["Save" gnus-summary-save-process-mark t]
2405           ["Run command on marked..." gnus-summary-universal-argument t]))
2406         ("Scroll article"
2407          ["Page forward" gnus-summary-next-page
2408           ,@(if (featurep 'xemacs) '(t)
2409               '(:help "Show next page of article"))]
2410          ["Page backward" gnus-summary-prev-page
2411           ,@(if (featurep 'xemacs) '(t)
2412               '(:help "Show previous page of article"))]
2413          ["Line forward" gnus-summary-scroll-up t])
2414         ("Move"
2415          ["Next unread article" gnus-summary-next-unread-article t]
2416          ["Previous unread article" gnus-summary-prev-unread-article t]
2417          ["Next article" gnus-summary-next-article t]
2418          ["Previous article" gnus-summary-prev-article t]
2419          ["Next unread subject" gnus-summary-next-unread-subject t]
2420          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2421          ["Next article same subject" gnus-summary-next-same-subject t]
2422          ["Previous article same subject" gnus-summary-prev-same-subject t]
2423          ["First unread article" gnus-summary-first-unread-article t]
2424          ["Best unread article" gnus-summary-best-unread-article t]
2425          ["Go to subject number..." gnus-summary-goto-subject t]
2426          ["Go to article number..." gnus-summary-goto-article t]
2427          ["Go to the last article" gnus-summary-goto-last-article t]
2428          ["Pop article off history" gnus-summary-pop-article t])
2429         ("Sort"
2430          ["Sort by number" gnus-summary-sort-by-number t]
2431          ["Sort by author" gnus-summary-sort-by-author t]
2432          ["Sort by subject" gnus-summary-sort-by-subject t]
2433          ["Sort by date" gnus-summary-sort-by-date t]
2434          ["Sort by score" gnus-summary-sort-by-score t]
2435          ["Sort by lines" gnus-summary-sort-by-lines t]
2436          ["Sort by characters" gnus-summary-sort-by-chars t]
2437          ["Randomize" gnus-summary-sort-by-random t]
2438          ["Original sort" gnus-summary-sort-by-original t])
2439         ("Help"
2440          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2441          ["Describe group" gnus-summary-describe-group t]
2442          ["Fetch charter" gnus-group-fetch-charter
2443           ,@(if (featurep 'xemacs) nil
2444               '(:help "Display the charter of the current group"))]
2445          ["Fetch control message" gnus-group-fetch-control
2446           ,@(if (featurep 'xemacs) nil
2447               '(:help "Display the archived control message for the current group"))]
2448          ["Read manual" gnus-info-find-node t])
2449         ("Modes"
2450          ["Pick and read" gnus-pick-mode t]
2451          ["Binary" gnus-binary-mode t])
2452         ("Regeneration"
2453          ["Regenerate" gnus-summary-prepare t]
2454          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2455          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2456          ["Toggle threading" gnus-summary-toggle-threads t])
2457         ["See old articles" gnus-summary-insert-old-articles t]
2458         ["See new articles" gnus-summary-insert-new-articles t]
2459         ["Filter articles..." gnus-summary-execute-command t]
2460         ["Run command on articles..." gnus-summary-universal-argument t]
2461         ["Search articles forward..." gnus-summary-search-article-forward t]
2462         ["Search articles backward..." gnus-summary-search-article-backward t]
2463         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2464         ["Expand window" gnus-summary-expand-window t]
2465         ["Expire expirable articles" gnus-summary-expire-articles
2466          (gnus-check-backend-function
2467           'request-expire-articles gnus-newsgroup-name)]
2468         ["Edit local kill file" gnus-summary-edit-local-kill t]
2469         ["Edit main kill file" gnus-summary-edit-global-kill t]
2470         ["Edit group parameters" gnus-summary-edit-parameters t]
2471         ["Customize group parameters" gnus-summary-customize-parameters t]
2472         ["Send a bug report" gnus-bug t]
2473         ("Exit"
2474          ["Catchup and exit" gnus-summary-catchup-and-exit
2475           ,@(if (featurep 'xemacs) '(t)
2476               '(:help "Mark unread articles in this group as read, then exit"))]
2477          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2478          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2479          ["Exit group" gnus-summary-exit
2480           ,@(if (featurep 'xemacs) '(t)
2481               '(:help "Exit current group, return to group selection mode"))]
2482          ["Exit group without updating" gnus-summary-exit-no-update t]
2483          ["Exit and goto next group" gnus-summary-next-group t]
2484          ["Exit and goto prev group" gnus-summary-prev-group t]
2485          ["Reselect group" gnus-summary-reselect-current-group t]
2486          ["Rescan group" gnus-summary-rescan-group t]
2487          ["Update dribble" gnus-summary-save-newsrc t])))
2488
2489     (gnus-run-hooks 'gnus-summary-menu-hook)))
2490
2491 (defvar gnus-summary-tool-bar-map nil)
2492
2493 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2494 (defun gnus-summary-make-tool-bar ()
2495   (if (and (fboundp 'tool-bar-add-item-from-menu)
2496            (default-value 'tool-bar-mode)
2497            (not gnus-summary-tool-bar-map))
2498       (setq gnus-summary-tool-bar-map
2499             (let ((tool-bar-map (make-sparse-keymap))
2500                   (load-path (mm-image-load-path)))
2501               (tool-bar-add-item-from-menu
2502                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2503               (tool-bar-add-item-from-menu
2504                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2505               (tool-bar-add-item-from-menu
2506                'gnus-summary-post-news "post" gnus-summary-mode-map)
2507               (tool-bar-add-item-from-menu
2508                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2509               (tool-bar-add-item-from-menu
2510                'gnus-summary-followup "followup" gnus-summary-mode-map)
2511               (tool-bar-add-item-from-menu
2512                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2513               (tool-bar-add-item-from-menu
2514                'gnus-summary-reply "reply" gnus-summary-mode-map)
2515               (tool-bar-add-item-from-menu
2516                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2517               (tool-bar-add-item-from-menu
2518                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2519               (tool-bar-add-item-from-menu
2520                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2531               tool-bar-map)))
2532   (if gnus-summary-tool-bar-map
2533       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2534
2535 (defun gnus-score-set-default (var value)
2536   "A version of set that updates the GNU Emacs menu-bar."
2537   (set var value)
2538   ;; It is the message that forces the active status to be updated.
2539   (message ""))
2540
2541 (defun gnus-make-score-map (type)
2542   "Make a summary score map of type TYPE."
2543   (if t
2544       nil
2545     (let ((headers '(("author" "from" string)
2546                      ("subject" "subject" string)
2547                      ("article body" "body" string)
2548                      ("article head" "head" string)
2549                      ("xref" "xref" string)
2550                      ("extra header" "extra" string)
2551                      ("lines" "lines" number)
2552                      ("followups to author" "followup" string)))
2553           (types '((number ("less than" <)
2554                            ("greater than" >)
2555                            ("equal" =))
2556                    (string ("substring" s)
2557                            ("exact string" e)
2558                            ("fuzzy string" f)
2559                            ("regexp" r))))
2560           (perms '(("temporary" (current-time-string))
2561                    ("permanent" nil)
2562                    ("immediate" now)))
2563           header)
2564       (list
2565        (apply
2566         'nconc
2567         (list
2568          (if (eq type 'lower)
2569              "Lower score"
2570            "Increase score"))
2571         (let (outh)
2572           (while headers
2573             (setq header (car headers))
2574             (setq outh
2575                   (cons
2576                    (apply
2577                     'nconc
2578                     (list (car header))
2579                     (let ((ts (cdr (assoc (nth 2 header) types)))
2580                           outt)
2581                       (while ts
2582                         (setq outt
2583                               (cons
2584                                (apply
2585                                 'nconc
2586                                 (list (caar ts))
2587                                 (let ((ps perms)
2588                                       outp)
2589                                   (while ps
2590                                     (setq outp
2591                                           (cons
2592                                            (vector
2593                                             (caar ps)
2594                                             (list
2595                                              'gnus-summary-score-entry
2596                                              (nth 1 header)
2597                                              (if (or (string= (nth 1 header)
2598                                                               "head")
2599                                                      (string= (nth 1 header)
2600                                                               "body"))
2601                                                  ""
2602                                                (list 'gnus-summary-header
2603                                                      (nth 1 header)))
2604                                              (list 'quote (nth 1 (car ts)))
2605                                              (list 'gnus-score-delta-default
2606                                                    nil)
2607                                              (nth 1 (car ps))
2608                                              t)
2609                                             t)
2610                                            outp))
2611                                     (setq ps (cdr ps)))
2612                                   (list (nreverse outp))))
2613                                outt))
2614                         (setq ts (cdr ts)))
2615                       (list (nreverse outt))))
2616                    outh))
2617             (setq headers (cdr headers)))
2618           (list (nreverse outh))))))))
2619
2620 \f
2621
2622 (defun gnus-summary-mode (&optional group)
2623   "Major mode for reading articles.
2624
2625 All normal editing commands are switched off.
2626 \\<gnus-summary-mode-map>
2627 Each line in this buffer represents one article.  To read an
2628 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2629 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2630 respectively.
2631
2632 You can also post articles and send mail from this buffer.  To
2633 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2634 of an article, type `\\[gnus-summary-reply]'.
2635
2636 There are approx. one gazillion commands you can execute in this
2637 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2638
2639 The following commands are available:
2640
2641 \\{gnus-summary-mode-map}"
2642   (interactive)
2643   (kill-all-local-variables)
2644   (when (gnus-visual-p 'summary-menu 'menu)
2645     (gnus-summary-make-menu-bar)
2646     (gnus-summary-make-tool-bar))
2647   (gnus-summary-make-local-variables)
2648   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2649     (gnus-summary-make-local-variables))
2650   (gnus-make-thread-indent-array)
2651   (gnus-simplify-mode-line)
2652   (setq major-mode 'gnus-summary-mode)
2653   (setq mode-name "Summary")
2654   (make-local-variable 'minor-mode-alist)
2655   (use-local-map gnus-summary-mode-map)
2656   (buffer-disable-undo)
2657   (setq buffer-read-only t)             ;Disable modification
2658   (setq truncate-lines t)
2659   (setq selective-display t)
2660   (setq selective-display-ellipses t)   ;Display `...'
2661   (gnus-summary-set-display-table)
2662   (gnus-set-default-directory)
2663   (setq gnus-newsgroup-name group)
2664   (make-local-variable 'gnus-summary-line-format)
2665   (make-local-variable 'gnus-summary-line-format-spec)
2666   (make-local-variable 'gnus-summary-dummy-line-format)
2667   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2668   (make-local-variable 'gnus-summary-mark-positions)
2669   (make-local-hook 'pre-command-hook)
2670   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2671   (gnus-run-hooks 'gnus-summary-mode-hook)
2672   (turn-on-gnus-mailing-list-mode)
2673   (mm-enable-multibyte)
2674   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2675   (gnus-update-summary-mark-positions))
2676
2677 (defun gnus-summary-make-local-variables ()
2678   "Make all the local summary buffer variables."
2679   (let (global)
2680     (dolist (local gnus-summary-local-variables)
2681       (if (consp local)
2682           (progn
2683             (if (eq (cdr local) 'global)
2684                 ;; Copy the global value of the variable.
2685                 (setq global (symbol-value (car local)))
2686               ;; Use the value from the list.
2687               (setq global (eval (cdr local))))
2688             (set (make-local-variable (car local)) global))
2689         ;; Simple nil-valued local variable.
2690         (set (make-local-variable local) nil)))))
2691
2692 (defun gnus-summary-clear-local-variables ()
2693   (let ((locals gnus-summary-local-variables))
2694     (while locals
2695       (if (consp (car locals))
2696           (and (vectorp (caar locals))
2697                (set (caar locals) nil))
2698         (and (vectorp (car locals))
2699              (set (car locals) nil)))
2700       (setq locals (cdr locals)))))
2701
2702 ;; Summary data functions.
2703
2704 (defmacro gnus-data-number (data)
2705   `(car ,data))
2706
2707 (defmacro gnus-data-set-number (data number)
2708   `(setcar ,data ,number))
2709
2710 (defmacro gnus-data-mark (data)
2711   `(nth 1 ,data))
2712
2713 (defmacro gnus-data-set-mark (data mark)
2714   `(setcar (nthcdr 1 ,data) ,mark))
2715
2716 (defmacro gnus-data-pos (data)
2717   `(nth 2 ,data))
2718
2719 (defmacro gnus-data-set-pos (data pos)
2720   `(setcar (nthcdr 2 ,data) ,pos))
2721
2722 (defmacro gnus-data-header (data)
2723   `(nth 3 ,data))
2724
2725 (defmacro gnus-data-set-header (data header)
2726   `(setf (nth 3 ,data) ,header))
2727
2728 (defmacro gnus-data-level (data)
2729   `(nth 4 ,data))
2730
2731 (defmacro gnus-data-unread-p (data)
2732   `(= (nth 1 ,data) gnus-unread-mark))
2733
2734 (defmacro gnus-data-read-p (data)
2735   `(/= (nth 1 ,data) gnus-unread-mark))
2736
2737 (defmacro gnus-data-pseudo-p (data)
2738   `(consp (nth 3 ,data)))
2739
2740 (defmacro gnus-data-find (number)
2741   `(assq ,number gnus-newsgroup-data))
2742
2743 (defmacro gnus-data-find-list (number &optional data)
2744   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2745      (memq (assq ,number bdata)
2746            bdata)))
2747
2748 (defmacro gnus-data-make (number mark pos header level)
2749   `(list ,number ,mark ,pos ,header ,level))
2750
2751 (defun gnus-data-enter (after-article number mark pos header level offset)
2752   (let ((data (gnus-data-find-list after-article)))
2753     (unless data
2754       (error "No such article: %d" after-article))
2755     (setcdr data (cons (gnus-data-make number mark pos header level)
2756                        (cdr data)))
2757     (setq gnus-newsgroup-data-reverse nil)
2758     (gnus-data-update-list (cddr data) offset)))
2759
2760 (defun gnus-data-enter-list (after-article list &optional offset)
2761   (when list
2762     (let ((data (and after-article (gnus-data-find-list after-article)))
2763           (ilist list))
2764       (if (not (or data
2765                    after-article))
2766           (let ((odata gnus-newsgroup-data))
2767             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2768             (when offset
2769               (gnus-data-update-list odata offset)))
2770       ;; Find the last element in the list to be spliced into the main
2771         ;; list.
2772         (while (cdr list)
2773           (setq list (cdr list)))
2774         (if (not data)
2775             (progn
2776               (setcdr list gnus-newsgroup-data)
2777               (setq gnus-newsgroup-data ilist)
2778               (when offset
2779                 (gnus-data-update-list (cdr list) offset)))
2780           (setcdr list (cdr data))
2781           (setcdr data ilist)
2782           (when offset
2783             (gnus-data-update-list (cdr list) offset))))
2784       (setq gnus-newsgroup-data-reverse nil))))
2785
2786 (defun gnus-data-remove (article &optional offset)
2787   (let ((data gnus-newsgroup-data))
2788     (if (= (gnus-data-number (car data)) article)
2789         (progn
2790           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2791                 gnus-newsgroup-data-reverse nil)
2792           (when offset
2793             (gnus-data-update-list gnus-newsgroup-data offset)))
2794       (while (cdr data)
2795         (when (= (gnus-data-number (cadr data)) article)
2796           (setcdr data (cddr data))
2797           (when offset
2798             (gnus-data-update-list (cdr data) offset))
2799           (setq data nil
2800                 gnus-newsgroup-data-reverse nil))
2801         (setq data (cdr data))))))
2802
2803 (defmacro gnus-data-list (backward)
2804   `(if ,backward
2805        (or gnus-newsgroup-data-reverse
2806            (setq gnus-newsgroup-data-reverse
2807                  (reverse gnus-newsgroup-data)))
2808      gnus-newsgroup-data))
2809
2810 (defun gnus-data-update-list (data offset)
2811   "Add OFFSET to the POS of all data entries in DATA."
2812   (setq gnus-newsgroup-data-reverse nil)
2813   (while data
2814     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2815     (setq data (cdr data))))
2816
2817 (defun gnus-summary-article-pseudo-p (article)
2818   "Say whether this article is a pseudo article or not."
2819   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2820
2821 (defmacro gnus-summary-article-sparse-p (article)
2822   "Say whether this article is a sparse article or not."
2823   `(memq ,article gnus-newsgroup-sparse))
2824
2825 (defmacro gnus-summary-article-ancient-p (article)
2826   "Say whether this article is a sparse article or not."
2827   `(memq ,article gnus-newsgroup-ancient))
2828
2829 (defun gnus-article-parent-p (number)
2830   "Say whether this article is a parent or not."
2831   (let ((data (gnus-data-find-list number)))
2832     (and (cdr data)              ; There has to be an article after...
2833          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2834             (gnus-data-level (nth 1 data))))))
2835
2836 (defun gnus-article-children (number)
2837   "Return a list of all children to NUMBER."
2838   (let* ((data (gnus-data-find-list number))
2839          (level (gnus-data-level (car data)))
2840          children)
2841     (setq data (cdr data))
2842     (while (and data
2843                 (= (gnus-data-level (car data)) (1+ level)))
2844       (push (gnus-data-number (car data)) children)
2845       (setq data (cdr data)))
2846     children))
2847
2848 (defmacro gnus-summary-skip-intangible ()
2849   "If the current article is intangible, then jump to a different article."
2850   '(let ((to (get-text-property (point) 'gnus-intangible)))
2851      (and to (gnus-summary-goto-subject to))))
2852
2853 (defmacro gnus-summary-article-intangible-p ()
2854   "Say whether this article is intangible or not."
2855   '(get-text-property (point) 'gnus-intangible))
2856
2857 (defun gnus-article-read-p (article)
2858   "Say whether ARTICLE is read or not."
2859   (not (or (memq article gnus-newsgroup-marked)
2860            (memq article gnus-newsgroup-spam-marked)
2861            (memq article gnus-newsgroup-unreads)
2862            (memq article gnus-newsgroup-unselected)
2863            (memq article gnus-newsgroup-dormant))))
2864
2865 ;; Some summary mode macros.
2866
2867 (defmacro gnus-summary-article-number ()
2868   "The article number of the article on the current line.
2869 If there isn't an article number here, then we return the current
2870 article number."
2871   '(progn
2872      (gnus-summary-skip-intangible)
2873      (or (get-text-property (point) 'gnus-number)
2874          (gnus-summary-last-subject))))
2875
2876 (defmacro gnus-summary-article-header (&optional number)
2877   "Return the header of article NUMBER."
2878   `(gnus-data-header (gnus-data-find
2879                       ,(or number '(gnus-summary-article-number)))))
2880
2881 (defmacro gnus-summary-thread-level (&optional number)
2882   "Return the level of thread that starts with article NUMBER."
2883   `(if (and (eq gnus-summary-make-false-root 'dummy)
2884             (get-text-property (point) 'gnus-intangible))
2885        0
2886      (gnus-data-level (gnus-data-find
2887                        ,(or number '(gnus-summary-article-number))))))
2888
2889 (defmacro gnus-summary-article-mark (&optional number)
2890   "Return the mark of article NUMBER."
2891   `(gnus-data-mark (gnus-data-find
2892                     ,(or number '(gnus-summary-article-number)))))
2893
2894 (defmacro gnus-summary-article-pos (&optional number)
2895   "Return the position of the line of article NUMBER."
2896   `(gnus-data-pos (gnus-data-find
2897                    ,(or number '(gnus-summary-article-number)))))
2898
2899 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2900 (defmacro gnus-summary-article-subject (&optional number)
2901   "Return current subject string or nil if nothing."
2902   `(let ((headers
2903           ,(if number
2904                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2905              '(gnus-data-header (assq (gnus-summary-article-number)
2906                                       gnus-newsgroup-data)))))
2907      (and headers
2908           (vectorp headers)
2909           (mail-header-subject headers))))
2910
2911 (defmacro gnus-summary-article-score (&optional number)
2912   "Return current article score."
2913   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2914                   gnus-newsgroup-scored))
2915        gnus-summary-default-score 0))
2916
2917 (defun gnus-summary-article-children (&optional number)
2918   "Return a list of article numbers that are children of article NUMBER."
2919   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2920          (level (gnus-data-level (car data)))
2921          l children)
2922     (while (and (setq data (cdr data))
2923                 (> (setq l (gnus-data-level (car data))) level))
2924       (and (= (1+ level) l)
2925            (push (gnus-data-number (car data))
2926                  children)))
2927     (nreverse children)))
2928
2929 (defun gnus-summary-article-parent (&optional number)
2930   "Return the article number of the parent of article NUMBER."
2931   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2932                                     (gnus-data-list t)))
2933          (level (gnus-data-level (car data))))
2934     (if (zerop level)
2935         ()                              ; This is a root.
2936       ;; We search until we find an article with a level less than
2937       ;; this one.  That function has to be the parent.
2938       (while (and (setq data (cdr data))
2939                   (not (< (gnus-data-level (car data)) level))))
2940       (and data (gnus-data-number (car data))))))
2941
2942 (defun gnus-unread-mark-p (mark)
2943   "Say whether MARK is the unread mark."
2944   (= mark gnus-unread-mark))
2945
2946 (defun gnus-read-mark-p (mark)
2947   "Say whether MARK is one of the marks that mark as read.
2948 This is all marks except unread, ticked, dormant, and expirable."
2949   (not (or (= mark gnus-unread-mark)
2950            (= mark gnus-ticked-mark)
2951            (= mark gnus-spam-mark)
2952            (= mark gnus-dormant-mark)
2953            (= mark gnus-expirable-mark))))
2954
2955 (defmacro gnus-article-mark (number)
2956   "Return the MARK of article NUMBER.
2957 This macro should only be used when computing the mark the \"first\"
2958 time; i.e., when generating the summary lines.  After that,
2959 `gnus-summary-article-mark' should be used to examine the
2960 marks of articles."
2961   `(cond
2962     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2963     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2964     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2965     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2966     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2967     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2968     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2969     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2970            gnus-ancient-mark))))
2971
2972 ;; Saving hidden threads.
2973
2974 (defmacro gnus-save-hidden-threads (&rest forms)
2975   "Save hidden threads, eval FORMS, and restore the hidden threads."
2976   (let ((config (make-symbol "config")))
2977     `(let ((,config (gnus-hidden-threads-configuration)))
2978        (unwind-protect
2979            (save-excursion
2980              ,@forms)
2981          (gnus-restore-hidden-threads-configuration ,config)))))
2982 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2983 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2984
2985 (defun gnus-data-compute-positions ()
2986   "Compute the positions of all articles."
2987   (setq gnus-newsgroup-data-reverse nil)
2988   (let ((data gnus-newsgroup-data))
2989     (save-excursion
2990       (gnus-save-hidden-threads
2991         (gnus-summary-show-all-threads)
2992         (goto-char (point-min))
2993         (while data
2994           (while (get-text-property (point) 'gnus-intangible)
2995             (forward-line 1))
2996           (gnus-data-set-pos (car data) (+ (point) 3))
2997           (setq data (cdr data))
2998           (forward-line 1))))))
2999
3000 (defun gnus-hidden-threads-configuration ()
3001   "Return the current hidden threads configuration."
3002   (save-excursion
3003     (let (config)
3004       (goto-char (point-min))
3005       (while (search-forward "\r" nil t)
3006         (push (1- (point)) config))
3007       config)))
3008
3009 (defun gnus-restore-hidden-threads-configuration (config)
3010   "Restore hidden threads configuration from CONFIG."
3011   (save-excursion
3012     (let (point buffer-read-only)
3013       (while (setq point (pop config))
3014         (when (and (< point (point-max))
3015                    (goto-char point)
3016                    (eq (char-after) ?\n))
3017           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3018
3019 ;; Various summary mode internalish functions.
3020
3021 (defun gnus-mouse-pick-article (e)
3022   (interactive "e")
3023   (mouse-set-point e)
3024   (gnus-summary-next-page nil t))
3025
3026 (defun gnus-summary-set-display-table ()
3027   "Change the display table.
3028 Odd characters have a tendency to mess
3029 up nicely formatted displays - we make all possible glyphs
3030 display only a single character."
3031
3032   ;; We start from the standard display table, if any.
3033   (let ((table (or (copy-sequence standard-display-table)
3034                    (make-display-table)))
3035         (i 32))
3036     ;; Nix out all the control chars...
3037     (while (>= (setq i (1- i)) 0)
3038       (aset table i [??]))
3039    ;; ... but not newline and cr, of course.  (cr is necessary for the
3040     ;; selective display).
3041     (aset table ?\n nil)
3042     (aset table ?\r nil)
3043     ;; We keep TAB as well.
3044     (aset table ?\t nil)
3045     ;; We nix out any glyphs over 126 that are not set already.
3046     (let ((i 256))
3047       (while (>= (setq i (1- i)) 127)
3048         ;; Only modify if the entry is nil.
3049         (unless (aref table i)
3050           (aset table i [??]))))
3051     (setq buffer-display-table table)))
3052
3053 (defun gnus-summary-set-article-display-arrow (pos)
3054   "Update the overlay arrow to point to line at position POS."
3055   (when (and gnus-summary-display-arrow
3056              (boundp 'overlay-arrow-position)
3057              (boundp 'overlay-arrow-string))
3058     (save-excursion
3059       (goto-char pos)
3060       (beginning-of-line)
3061       (unless overlay-arrow-position
3062         (setq overlay-arrow-position (make-marker)))
3063       (setq overlay-arrow-string "=>"
3064             overlay-arrow-position (set-marker overlay-arrow-position
3065                                                (point)
3066                                                (current-buffer))))))
3067
3068 (defun gnus-summary-setup-buffer (group)
3069   "Initialize summary buffer."
3070   (let ((buffer (gnus-summary-buffer-name group))
3071         (dead-name (concat "*Dead Summary "
3072                            (gnus-group-decoded-name group) "*")))
3073     ;; If a dead summary buffer exists, we kill it.
3074     (when (gnus-buffer-live-p dead-name)
3075       (gnus-kill-buffer dead-name))
3076     (if (get-buffer buffer)
3077         (progn
3078           (set-buffer buffer)
3079           (setq gnus-summary-buffer (current-buffer))
3080           (not gnus-newsgroup-prepared))
3081       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3082       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3083       (gnus-summary-mode group)
3084       (when gnus-carpal
3085         (gnus-carpal-setup-buffer 'summary))
3086       (unless gnus-single-article-buffer
3087         (make-local-variable 'gnus-article-buffer)
3088         (make-local-variable 'gnus-article-current)
3089         (make-local-variable 'gnus-original-article-buffer))
3090       (setq gnus-newsgroup-name group)
3091       ;; Set any local variables in the group parameters.
3092       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3093       t)))
3094
3095 (defun gnus-set-global-variables ()
3096   "Set the global equivalents of the buffer-local variables.
3097 They are set to the latest values they had.  These reflect the summary
3098 buffer that was in action when the last article was fetched."
3099   (when (eq major-mode 'gnus-summary-mode)
3100     (setq gnus-summary-buffer (current-buffer))
3101     (let ((name gnus-newsgroup-name)
3102           (marked gnus-newsgroup-marked)
3103           (spam gnus-newsgroup-spam-marked)
3104           (unread gnus-newsgroup-unreads)
3105           (headers gnus-current-headers)
3106           (data gnus-newsgroup-data)
3107           (summary gnus-summary-buffer)
3108           (article-buffer gnus-article-buffer)
3109           (original gnus-original-article-buffer)
3110           (gac gnus-article-current)
3111           (reffed gnus-reffed-article-number)
3112           (score-file gnus-current-score-file)
3113           (default-charset gnus-newsgroup-charset)
3114           vlist)
3115       (let ((locals gnus-newsgroup-variables))
3116         (while locals
3117           (if (consp (car locals))
3118               (push (eval (caar locals)) vlist)
3119             (push (eval (car locals)) vlist))
3120           (setq locals (cdr locals)))
3121         (setq vlist (nreverse vlist)))
3122       (save-excursion
3123         (set-buffer gnus-group-buffer)
3124         (setq gnus-newsgroup-name name
3125               gnus-newsgroup-marked marked
3126               gnus-newsgroup-spam-marked spam
3127               gnus-newsgroup-unreads unread
3128               gnus-current-headers headers
3129               gnus-newsgroup-data data
3130               gnus-article-current gac
3131               gnus-summary-buffer summary
3132               gnus-article-buffer article-buffer
3133               gnus-original-article-buffer original
3134               gnus-reffed-article-number reffed
3135               gnus-current-score-file score-file
3136               gnus-newsgroup-charset default-charset)
3137         (let ((locals gnus-newsgroup-variables))
3138           (while locals
3139             (if (consp (car locals))
3140                 (set (caar locals) (pop vlist))
3141               (set (car locals) (pop vlist)))
3142             (setq locals (cdr locals))))
3143         ;; The article buffer also has local variables.
3144         (when (gnus-buffer-live-p gnus-article-buffer)
3145           (set-buffer gnus-article-buffer)
3146           (setq gnus-summary-buffer summary))))))
3147
3148 (defun gnus-summary-article-unread-p (article)
3149   "Say whether ARTICLE is unread or not."
3150   (memq article gnus-newsgroup-unreads))
3151
3152 (defun gnus-summary-first-article-p (&optional article)
3153   "Return whether ARTICLE is the first article in the buffer."
3154   (if (not (setq article (or article (gnus-summary-article-number))))
3155       nil
3156     (eq article (caar gnus-newsgroup-data))))
3157
3158 (defun gnus-summary-last-article-p (&optional article)
3159   "Return whether ARTICLE is the last article in the buffer."
3160   (if (not (setq article (or article (gnus-summary-article-number))))
3161       ;; All non-existent numbers are the last article.  :-)
3162       t
3163     (not (cdr (gnus-data-find-list article)))))
3164
3165 (defun gnus-make-thread-indent-array ()
3166   (let ((n 200))
3167     (unless (and gnus-thread-indent-array
3168                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3169       (setq gnus-thread-indent-array (make-vector 201 "")
3170             gnus-thread-indent-array-level gnus-thread-indent-level)
3171       (while (>= n 0)
3172         (aset gnus-thread-indent-array n
3173               (make-string (* n gnus-thread-indent-level) ? ))
3174         (setq n (1- n))))))
3175
3176 (defun gnus-update-summary-mark-positions ()
3177   "Compute where the summary marks are to go."
3178   (save-excursion
3179     (when (gnus-buffer-exists-p gnus-summary-buffer)
3180       (set-buffer gnus-summary-buffer))
3181     (let ((gnus-replied-mark 129)
3182           (gnus-score-below-mark 130)
3183           (gnus-score-over-mark 130)
3184           (gnus-undownloaded-mark 131)
3185           (spec gnus-summary-line-format-spec)
3186           gnus-visual pos)
3187       (save-excursion
3188         (gnus-set-work-buffer)
3189         (let ((gnus-summary-line-format-spec spec)
3190               (gnus-newsgroup-downloadable '(0)))
3191           (gnus-summary-insert-line
3192            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3193            0 nil t 128 t nil "" nil 1)
3194           (goto-char (point-min))
3195           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3196                                              (- (point) (point-min) 1)))))
3197           (goto-char (point-min))
3198           (push (cons 'replied (and (search-forward "\201" nil t)
3199                                     (- (point) (point-min) 1)))
3200                 pos)
3201           (goto-char (point-min))
3202           (push (cons 'score (and (search-forward "\202" nil t)
3203                                   (- (point) (point-min) 1)))
3204                 pos)
3205           (goto-char (point-min))
3206           (push (cons 'download
3207                       (and (search-forward "\203" nil t)
3208                            (- (point) (point-min) 1)))
3209                 pos)))
3210       (setq gnus-summary-mark-positions pos))))
3211
3212 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3213   "Insert a dummy root in the summary buffer."
3214   (beginning-of-line)
3215   (gnus-add-text-properties
3216    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3217    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3218
3219 (defun gnus-summary-extract-address-component (from)
3220   (or (car (funcall gnus-extract-address-components from))
3221       from))
3222
3223 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3224   (let ((mail-parse-charset gnus-newsgroup-charset)
3225         ; Is it really necessary to do this next part for each summary line?
3226         ; Luckily, doesn't seem to slow things down much.
3227         (mail-parse-ignored-charsets
3228          (save-excursion (set-buffer gnus-summary-buffer)
3229                          gnus-newsgroup-ignored-charsets)))
3230     (or
3231      (and gnus-ignored-from-addresses
3232           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3233           (let ((extra-headers (mail-header-extra header))
3234                 to
3235                 newsgroups)
3236             (cond
3237              ((setq to (cdr (assq 'To extra-headers)))
3238               (concat "-> "
3239                       (inline
3240                         (gnus-summary-extract-address-component
3241                          (funcall gnus-decode-encoded-word-function to)))))
3242              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3243               (concat "=> " newsgroups)))))
3244      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3245
3246 (defun gnus-summary-insert-line (gnus-tmp-header
3247                                  gnus-tmp-level gnus-tmp-current
3248                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3249                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3250                                  &optional gnus-tmp-dummy gnus-tmp-score
3251                                  gnus-tmp-process)
3252   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3253          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3254          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3255          (gnus-tmp-score-char
3256           (if (or (null gnus-summary-default-score)
3257                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3258                       gnus-summary-zcore-fuzz))
3259               ?                         ;Whitespace
3260             (if (< gnus-tmp-score gnus-summary-default-score)
3261                 gnus-score-below-mark gnus-score-over-mark)))
3262          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3263          (gnus-tmp-replied
3264           (cond (gnus-tmp-process gnus-process-mark)
3265                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3266                  gnus-cached-mark)
3267                 (gnus-tmp-replied gnus-replied-mark)
3268                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3269                  gnus-forwarded-mark)
3270                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3271                  gnus-saved-mark)
3272                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3273                  gnus-recent-mark)
3274                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3275                  gnus-unseen-mark)
3276                 (t gnus-no-mark)))
3277          (gnus-tmp-downloaded
3278           (cond (undownloaded 
3279                  gnus-undownloaded-mark)
3280                 (gnus-newsgroup-agentized
3281                  gnus-downloaded-mark)
3282                 (t
3283                  gnus-no-mark)))
3284          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3285          (gnus-tmp-name
3286           (cond
3287            ((string-match "<[^>]+> *$" gnus-tmp-from)
3288             (let ((beg (match-beginning 0)))
3289               (or (and (string-match "^\".+\"" gnus-tmp-from)
3290                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3291                   (substring gnus-tmp-from 0 beg))))
3292            ((string-match "(.+)" gnus-tmp-from)
3293             (substring gnus-tmp-from
3294                        (1+ (match-beginning 0)) (1- (match-end 0))))
3295            (t gnus-tmp-from)))
3296          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3297          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3298          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3299          (buffer-read-only nil))
3300     (when (string= gnus-tmp-name "")
3301       (setq gnus-tmp-name gnus-tmp-from))
3302     (unless (numberp gnus-tmp-lines)
3303       (setq gnus-tmp-lines -1))
3304     (if (= gnus-tmp-lines -1)
3305         (setq gnus-tmp-lines "?")
3306       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3307       (gnus-put-text-property
3308      (point)
3309      (progn (eval gnus-summary-line-format-spec) (point))
3310        'gnus-number gnus-tmp-number)
3311     (when (gnus-visual-p 'summary-highlight 'highlight)
3312       (forward-line -1)
3313       (gnus-run-hooks 'gnus-summary-update-hook)
3314       (forward-line 1))))
3315
3316 (defun gnus-summary-update-line (&optional dont-update)
3317   "Update summary line after change."
3318   (when (and gnus-summary-default-score
3319              (not gnus-summary-inhibit-highlight))
3320     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3321            (article (gnus-summary-article-number))
3322            (score (gnus-summary-article-score article)))
3323       (unless dont-update
3324         (if (and gnus-summary-mark-below
3325                  (< (gnus-summary-article-score)
3326                     gnus-summary-mark-below))
3327             ;; This article has a low score, so we mark it as read.
3328             (when (memq article gnus-newsgroup-unreads)
3329               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3330           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3331             ;; This article was previously marked as read on account
3332             ;; of a low score, but now it has risen, so we mark it as
3333             ;; unread.
3334             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3335         (gnus-summary-update-mark
3336          (if (or (null gnus-summary-default-score)
3337                  (<= (abs (- score gnus-summary-default-score))
3338                      gnus-summary-zcore-fuzz))
3339              ?                          ;Whitespace
3340            (if (< score gnus-summary-default-score)
3341                gnus-score-below-mark gnus-score-over-mark))
3342          'score))
3343       ;; Do visual highlighting.
3344       (when (gnus-visual-p 'summary-highlight 'highlight)
3345         (gnus-run-hooks 'gnus-summary-update-hook)))))
3346
3347 (defvar gnus-tmp-new-adopts nil)
3348
3349 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3350   "Return the number of articles in THREAD.
3351 This may be 0 in some cases -- if none of the articles in
3352 the thread are to be displayed."
3353   (let* ((number
3354          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3355           (cond
3356            ((not (listp thread))
3357             1)
3358            ((and (consp thread) (cdr thread))
3359             (apply
3360              '+ 1 (mapcar
3361                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3362            ((null thread)
3363             1)
3364            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3365             1)
3366            (t 0))))
3367     (when (and level (zerop level) gnus-tmp-new-adopts)
3368       (incf number
3369             (apply '+ (mapcar
3370                        'gnus-summary-number-of-articles-in-thread
3371                        gnus-tmp-new-adopts))))
3372     (if char
3373         (if (> number 1) gnus-not-empty-thread-mark
3374           gnus-empty-thread-mark)
3375       number)))
3376
3377 (defsubst gnus-summary-line-message-size (head)
3378   "Return pretty-printed version of message size.
3379 This function is intended to be used in
3380 `gnus-summary-line-format-alist', which see."
3381   (let ((c (or (mail-header-chars head) -1)))
3382     (cond ((< c 0) "n/a")               ; chars not available
3383           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3384           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3385           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3386           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3387
3388
3389 (defun gnus-summary-set-local-parameters (group)
3390   "Go through the local params of GROUP and set all variable specs in that list."
3391   (let ((params (gnus-group-find-parameter group))
3392         (vars '(quit-config))           ; Ignore quit-config.
3393         elem)
3394     (while params
3395       (setq elem (car params)
3396             params (cdr params))
3397       (and (consp elem)                 ; Has to be a cons.
3398            (consp (cdr elem))           ; The cdr has to be a list.
3399            (symbolp (car elem))         ; Has to be a symbol in there.
3400            (not (memq (car elem) vars))
3401            (ignore-errors               ; So we set it.
3402              (push (car elem) vars)
3403              (make-local-variable (car elem))
3404              (set (car elem) (eval (nth 1 elem))))))))
3405
3406 (defun gnus-summary-read-group (group &optional show-all no-article
3407                                       kill-buffer no-display backward
3408                                       select-articles)
3409   "Start reading news in newsgroup GROUP.
3410 If SHOW-ALL is non-nil, already read articles are also listed.
3411 If NO-ARTICLE is non-nil, no article is selected initially.
3412 If NO-DISPLAY, don't generate a summary buffer."
3413   (let (result)
3414     (while (and group
3415                 (null (setq result
3416                             (let ((gnus-auto-select-next nil))
3417                               (or (gnus-summary-read-group-1
3418                                    group show-all no-article
3419                                    kill-buffer no-display
3420                                    select-articles)
3421                                   (setq show-all nil
3422                                         select-articles nil)))))
3423                 (eq gnus-auto-select-next 'quietly))
3424       (set-buffer gnus-group-buffer)
3425       ;; The entry function called above goes to the next
3426       ;; group automatically, so we go two groups back
3427       ;; if we are searching for the previous group.
3428       (when backward
3429         (gnus-group-prev-unread-group 2))
3430       (if (not (equal group (gnus-group-group-name)))
3431           (setq group (gnus-group-group-name))
3432         (setq group nil)))
3433     result))
3434
3435 (defun gnus-summary-read-group-1 (group show-all no-article
3436                                         kill-buffer no-display
3437                                         &optional select-articles)
3438   ;; Killed foreign groups can't be entered.
3439   ;;  (when (and (not (gnus-group-native-p group))
3440   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3441   ;;    (error "Dead non-native groups can't be entered"))
3442   (gnus-message 5 "Retrieving newsgroup: %s..."
3443                 (gnus-group-decoded-name group))
3444   (let* ((new-group (gnus-summary-setup-buffer group))
3445          (quit-config (gnus-group-quit-config group))
3446          (did-select (and new-group (gnus-select-newsgroup
3447                                      group show-all select-articles))))
3448     (cond
3449      ;; This summary buffer exists already, so we just select it.
3450      ((not new-group)
3451       (gnus-set-global-variables)
3452       (when kill-buffer
3453         (gnus-kill-or-deaden-summary kill-buffer))
3454       (gnus-configure-windows 'summary 'force)
3455       (gnus-set-mode-line 'summary)
3456       (gnus-summary-position-point)
3457       (message "")
3458       t)
3459      ;; We couldn't select this group.
3460      ((null did-select)
3461       (when (and (eq major-mode 'gnus-summary-mode)
3462                  (not (equal (current-buffer) kill-buffer)))
3463         (kill-buffer (current-buffer))
3464         (if (not quit-config)
3465             (progn
3466               ;; Update the info -- marks might need to be removed,
3467               ;; for instance.
3468               (gnus-summary-update-info)
3469               (set-buffer gnus-group-buffer)
3470               (gnus-group-jump-to-group group)
3471               (gnus-group-next-unread-group 1))
3472           (gnus-handle-ephemeral-exit quit-config)))
3473       (let ((grpinfo (gnus-get-info group)))
3474         (if (null (gnus-info-read grpinfo))
3475             (gnus-message 3 "Group %s contains no messages"
3476                           (gnus-group-decoded-name group))
3477           (gnus-message 3 "Can't select group")))
3478       nil)
3479      ;; The user did a `C-g' while prompting for number of articles,
3480      ;; so we exit this group.
3481      ((eq did-select 'quit)
3482       (and (eq major-mode 'gnus-summary-mode)
3483            (not (equal (current-buffer) kill-buffer))
3484            (kill-buffer (current-buffer)))
3485       (when kill-buffer
3486         (gnus-kill-or-deaden-summary kill-buffer))
3487       (if (not quit-config)
3488           (progn
3489             (set-buffer gnus-group-buffer)
3490             (gnus-group-jump-to-group group)
3491             (gnus-group-next-unread-group 1)
3492             (gnus-configure-windows 'group 'force))
3493         (gnus-handle-ephemeral-exit quit-config))
3494       ;; Finally signal the quit.
3495       (signal 'quit nil))
3496      ;; The group was successfully selected.
3497      (t
3498       (gnus-set-global-variables)
3499       ;; Save the active value in effect when the group was entered.
3500       (setq gnus-newsgroup-active
3501             (gnus-copy-sequence
3502              (gnus-active gnus-newsgroup-name)))
3503       ;; You can change the summary buffer in some way with this hook.
3504       (gnus-run-hooks 'gnus-select-group-hook)
3505       (gnus-update-format-specifications
3506        nil 'summary 'summary-mode 'summary-dummy)
3507       (gnus-update-summary-mark-positions)
3508       ;; Do score processing.
3509       (when gnus-use-scoring
3510         (gnus-possibly-score-headers))
3511       ;; Check whether to fill in the gaps in the threads.
3512       (when gnus-build-sparse-threads
3513         (gnus-build-sparse-threads))
3514       ;; Find the initial limit.
3515       (if gnus-show-threads
3516           (if show-all
3517               (let ((gnus-newsgroup-dormant nil))
3518                 (gnus-summary-initial-limit show-all))
3519             (gnus-summary-initial-limit show-all))
3520         ;; When unthreaded, all articles are always shown.
3521         (setq gnus-newsgroup-limit
3522               (mapcar
3523                (lambda (header) (mail-header-number header))
3524                gnus-newsgroup-headers)))
3525       ;; Generate the summary buffer.
3526       (unless no-display
3527         (gnus-summary-prepare))
3528       (when gnus-use-trees
3529         (gnus-tree-open group)
3530         (setq gnus-summary-highlight-line-function
3531               'gnus-tree-highlight-article))
3532       ;; If the summary buffer is empty, but there are some low-scored
3533       ;; articles or some excluded dormants, we include these in the
3534       ;; buffer.
3535       (when (and (zerop (buffer-size))
3536                  (not no-display))
3537         (cond (gnus-newsgroup-dormant
3538                (gnus-summary-limit-include-dormant))
3539               ((and gnus-newsgroup-scored show-all)
3540                (gnus-summary-limit-include-expunged t))))
3541       ;; Function `gnus-apply-kill-file' must be called in this hook.
3542       (gnus-run-hooks 'gnus-apply-kill-hook)
3543       (if (and (zerop (buffer-size))
3544                (not no-display))
3545           (progn
3546             ;; This newsgroup is empty.
3547             (gnus-summary-catchup-and-exit nil t)
3548             (gnus-message 6 "No unread news")
3549             (when kill-buffer
3550               (gnus-kill-or-deaden-summary kill-buffer))
3551             ;; Return nil from this function.
3552             nil)
3553         ;; Hide conversation thread subtrees.  We cannot do this in
3554         ;; gnus-summary-prepare-hook since kill processing may not
3555         ;; work with hidden articles.
3556         (gnus-summary-maybe-hide-threads)
3557         (when kill-buffer
3558           (gnus-kill-or-deaden-summary kill-buffer))
3559         (gnus-summary-auto-select-subject)
3560         ;; Show first unread article if requested.
3561         (if (and (not no-article)
3562                  (not no-display)
3563                  gnus-newsgroup-unreads
3564                  gnus-auto-select-first)
3565             (progn
3566               (gnus-configure-windows 'summary)
3567               (let ((art (gnus-summary-article-number)))
3568                 (unless (and (not gnus-plugged)
3569                              (or (memq art gnus-newsgroup-undownloaded)
3570                                  (memq art gnus-newsgroup-downloadable)))
3571                   (gnus-summary-goto-article art))))
3572           ;; Don't select any articles.
3573           (gnus-summary-position-point)
3574           (gnus-configure-windows 'summary 'force)
3575           (gnus-set-mode-line 'summary))
3576         (when (and gnus-auto-center-group
3577                    (get-buffer-window gnus-group-buffer t))
3578           ;; Gotta use windows, because recenter does weird stuff if
3579           ;; the current buffer ain't the displayed window.
3580           (let ((owin (selected-window)))
3581             (select-window (get-buffer-window gnus-group-buffer t))
3582             (when (gnus-group-goto-group group)
3583               (recenter))
3584             (select-window owin)))
3585         ;; Mark this buffer as "prepared".
3586         (setq gnus-newsgroup-prepared t)
3587         (gnus-run-hooks 'gnus-summary-prepared-hook)
3588         (unless (gnus-ephemeral-group-p group)
3589           (gnus-group-update-group group))
3590         t)))))
3591
3592 (defun gnus-summary-auto-select-subject ()
3593   "Select the subject line on initial group entry."
3594   (goto-char (point-min))
3595   (cond
3596    ((eq gnus-auto-select-subject 'best)
3597     (gnus-summary-best-unread-subject))
3598    ((eq gnus-auto-select-subject 'unread)
3599     (gnus-summary-first-unread-subject))
3600    ((eq gnus-auto-select-subject 'unseen)
3601     (gnus-summary-first-unseen-subject))
3602    ((eq gnus-auto-select-subject 'unseen-or-unread)
3603     (gnus-summary-first-unseen-or-unread-subject))
3604    ((eq gnus-auto-select-subject 'first)
3605     ;; Do nothing.
3606     )
3607    ((gnus-functionp gnus-auto-select-subject)
3608     (funcall gnus-auto-select-subject))))
3609
3610 (defun gnus-summary-prepare ()
3611   "Generate the summary buffer."
3612   (interactive)
3613   (let ((buffer-read-only nil))
3614     (erase-buffer)
3615     (setq gnus-newsgroup-data nil
3616           gnus-newsgroup-data-reverse nil)
3617     (gnus-run-hooks 'gnus-summary-generate-hook)
3618     ;; Generate the buffer, either with threads or without.
3619     (when gnus-newsgroup-headers
3620       (gnus-summary-prepare-threads
3621        (if gnus-show-threads
3622            (gnus-sort-gathered-threads
3623             (funcall gnus-summary-thread-gathering-function
3624                      (gnus-sort-threads
3625                       (gnus-cut-threads (gnus-make-threads)))))
3626          ;; Unthreaded display.
3627          (gnus-sort-articles gnus-newsgroup-headers))))
3628     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3629     ;; Call hooks for modifying summary buffer.
3630     (goto-char (point-min))
3631     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3632
3633 (defsubst gnus-general-simplify-subject (subject)
3634   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3635   (setq subject
3636         (cond
3637          ;; Truncate the subject.
3638          (gnus-simplify-subject-functions
3639           (gnus-map-function gnus-simplify-subject-functions subject))
3640          ((numberp gnus-summary-gather-subject-limit)
3641           (setq subject (gnus-simplify-subject-re subject))
3642           (if (> (length subject) gnus-summary-gather-subject-limit)
3643               (substring subject 0 gnus-summary-gather-subject-limit)
3644             subject))
3645          ;; Fuzzily simplify it.
3646          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3647           (gnus-simplify-subject-fuzzy subject))
3648          ;; Just remove the leading "Re:".
3649          (t
3650           (gnus-simplify-subject-re subject))))
3651
3652   (if (and gnus-summary-gather-exclude-subject
3653            (string-match gnus-summary-gather-exclude-subject subject))
3654       nil                         ; This article shouldn't be gathered
3655     subject))
3656
3657 (defun gnus-summary-simplify-subject-query ()
3658   "Query where the respool algorithm would put this article."
3659   (interactive)
3660   (gnus-summary-select-article)
3661   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3662
3663 (defun gnus-gather-threads-by-subject (threads)
3664   "Gather threads by looking at Subject headers."
3665   (if (not gnus-summary-make-false-root)
3666       threads
3667     (let ((hashtb (gnus-make-hashtable 1024))
3668           (prev threads)
3669           (result threads)
3670           subject hthread whole-subject)
3671       (while threads
3672         (setq subject (gnus-general-simplify-subject
3673                        (setq whole-subject (mail-header-subject
3674                                             (caar threads)))))
3675         (when subject
3676           (if (setq hthread (gnus-gethash subject hashtb))
3677               (progn
3678                 ;; We enter a dummy root into the thread, if we
3679                 ;; haven't done that already.
3680                 (unless (stringp (caar hthread))
3681                   (setcar hthread (list whole-subject (car hthread))))
3682                 ;; We add this new gathered thread to this gathered
3683                 ;; thread.
3684                 (setcdr (car hthread)
3685                         (nconc (cdar hthread) (list (car threads))))
3686                 ;; Remove it from the list of threads.
3687                 (setcdr prev (cdr threads))
3688                 (setq threads prev))
3689             ;; Enter this thread into the hash table.
3690             (gnus-sethash subject
3691                           (if gnus-summary-make-false-root-always
3692                               (progn
3693                                 ;; If you want a dummy root above all
3694                                 ;; threads...
3695                                 (setcar threads (list whole-subject
3696                                                       (car threads)))
3697                                 threads)
3698                             threads)
3699                           hashtb)))
3700         (setq prev threads)
3701         (setq threads (cdr threads)))
3702       result)))
3703
3704 (defun gnus-gather-threads-by-references (threads)
3705   "Gather threads by looking at References headers."
3706   (let ((idhashtb (gnus-make-hashtable 1024))
3707         (thhashtb (gnus-make-hashtable 1024))
3708         (prev threads)
3709         (result threads)
3710         ids references id gthread gid entered ref)
3711     (while threads
3712       (when (setq references (mail-header-references (caar threads)))
3713         (setq id (mail-header-id (caar threads))
3714               ids (inline (gnus-split-references references))
3715               entered nil)
3716         (while (setq ref (pop ids))
3717           (setq ids (delete ref ids))
3718           (if (not (setq gid (gnus-gethash ref idhashtb)))
3719               (progn
3720                 (gnus-sethash ref id idhashtb)
3721                 (gnus-sethash id threads thhashtb))
3722             (setq gthread (gnus-gethash gid thhashtb))
3723             (unless entered
3724               ;; We enter a dummy root into the thread, if we
3725               ;; haven't done that already.
3726               (unless (stringp (caar gthread))
3727                 (setcar gthread (list (mail-header-subject (caar gthread))
3728                                       (car gthread))))
3729               ;; We add this new gathered thread to this gathered
3730               ;; thread.
3731               (setcdr (car gthread)
3732                       (nconc (cdar gthread) (list (car threads)))))
3733             ;; Add it into the thread hash table.
3734             (gnus-sethash id gthread thhashtb)
3735             (setq entered t)
3736             ;; Remove it from the list of threads.
3737             (setcdr prev (cdr threads))
3738             (setq threads prev))))
3739       (setq prev threads)
3740       (setq threads (cdr threads)))
3741     result))
3742
3743 (defun gnus-sort-gathered-threads (threads)
3744   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3745   (let ((result threads))
3746     (while threads
3747       (when (stringp (caar threads))
3748         (setcdr (car threads)
3749                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3750       (setq threads (cdr threads)))
3751     result))
3752
3753 (defun gnus-thread-loop-p (root thread)
3754   "Say whether ROOT is in THREAD."
3755   (let ((stack (list thread))
3756         (infloop 0)
3757         th)
3758     (while (setq thread (pop stack))
3759       (setq th (cdr thread))
3760       (while (and th
3761                   (not (eq (caar th) root)))
3762         (pop th))
3763       (if th
3764           ;; We have found a loop.
3765           (let (ref-dep)
3766             (setcdr thread (delq (car th) (cdr thread)))
3767             (if (boundp (setq ref-dep (intern "none"
3768                                               gnus-newsgroup-dependencies)))
3769                 (setcdr (symbol-value ref-dep)
3770                         (nconc (cdr (symbol-value ref-dep))
3771                                (list (car th))))
3772               (set ref-dep (list nil (car th))))
3773             (setq infloop 1
3774                   stack nil))
3775         ;; Push all the subthreads onto the stack.
3776         (push (cdr thread) stack)))
3777     infloop))
3778
3779 (defun gnus-make-threads ()
3780   "Go through the dependency hashtb and find the roots.  Return all threads."
3781   (let (threads)
3782     (while (catch 'infloop
3783              (mapatoms
3784               (lambda (refs)
3785                 ;; Deal with self-referencing References loops.
3786                 (when (and (car (symbol-value refs))
3787                            (not (zerop
3788                                  (apply
3789                                   '+
3790                                   (mapcar
3791                                    (lambda (thread)
3792                                      (gnus-thread-loop-p
3793                                       (car (symbol-value refs)) thread))
3794                                    (cdr (symbol-value refs)))))))
3795                   (setq threads nil)
3796                   (throw 'infloop t))
3797                 (unless (car (symbol-value refs))
3798                   ;; These threads do not refer back to any other
3799                   ;; articles, so they're roots.
3800                   (setq threads (append (cdr (symbol-value refs)) threads))))
3801               gnus-newsgroup-dependencies)))
3802     threads))
3803
3804 ;; Build the thread tree.
3805 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3806   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3807
3808 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3809 if it was already present.
3810
3811 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3812 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3813 Message-IDs will be renamed to a unique Message-ID before being
3814 entered.
3815
3816 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3817   (let* ((id (mail-header-id header))
3818          (id-dep (and id (intern id dependencies)))
3819          parent-id ref ref-dep ref-header replaced)
3820     ;; Enter this `header' in the `dependencies' table.
3821     (cond
3822      ((not id-dep)
3823       (setq header nil))
3824      ;; The first two cases do the normal part: enter a new `header'
3825      ;; in the `dependencies' table.
3826      ((not (boundp id-dep))
3827       (set id-dep (list header)))
3828      ((null (car (symbol-value id-dep)))
3829       (setcar (symbol-value id-dep) header))
3830
3831      ;; From here the `header' was already present in the
3832      ;; `dependencies' table.
3833      (force-new
3834       ;; Overrides an existing entry;
3835       ;; just set the header part of the entry.
3836       (setcar (symbol-value id-dep) header)
3837       (setq replaced t))
3838
3839      ;; Renames the existing `header' to a unique Message-ID.
3840      ((not gnus-summary-ignore-duplicates)
3841       ;; An article with this Message-ID has already been seen.
3842       ;; We rename the Message-ID.
3843       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3844            (list header))
3845       (mail-header-set-id header id))
3846
3847      ;; The last case ignores an existing entry, except it adds any
3848      ;; additional Xrefs (in case the two articles came from different
3849      ;; servers.
3850      ;; Also sets `header' to `nil' meaning that the `dependencies'
3851      ;; table was *not* modified.
3852      (t
3853       (mail-header-set-xref
3854        (car (symbol-value id-dep))
3855        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3856                    "")
3857                (or (mail-header-xref header) "")))
3858       (setq header nil)))
3859
3860     (when (and header (not replaced))
3861       ;; First check that we are not creating a References loop.
3862       (setq parent-id (gnus-parent-id (mail-header-references header)))
3863       (setq ref parent-id)
3864       (while (and ref
3865                   (setq ref-dep (intern-soft ref dependencies))
3866                   (boundp ref-dep)
3867                   (setq ref-header (car (symbol-value ref-dep))))
3868         (if (string= id ref)
3869             ;; Yuk!  This is a reference loop.  Make the article be a
3870             ;; root article.
3871             (progn
3872               (mail-header-set-references (car (symbol-value id-dep)) "none")
3873               (setq ref nil)
3874               (setq parent-id nil))
3875           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3876       (setq ref-dep (intern (or parent-id "none") dependencies))
3877       (if (boundp ref-dep)
3878           (setcdr (symbol-value ref-dep)
3879                   (nconc (cdr (symbol-value ref-dep))
3880                          (list (symbol-value id-dep))))
3881         (set ref-dep (list nil (symbol-value id-dep)))))
3882     header))
3883
3884 (defun gnus-extract-message-id-from-in-reply-to (string)
3885   (if (string-match "<[^>]+>" string)
3886       (substring string (match-beginning 0) (match-end 0))
3887     nil))
3888
3889 (defun gnus-build-sparse-threads ()
3890   (let ((headers gnus-newsgroup-headers)
3891         (mail-parse-charset gnus-newsgroup-charset)
3892         (gnus-summary-ignore-duplicates t)
3893         header references generation relations
3894         subject child end new-child date)
3895     ;; First we create an alist of generations/relations, where
3896     ;; generations is how much we trust the relation, and the relation
3897     ;; is parent/child.
3898     (gnus-message 7 "Making sparse threads...")
3899     (save-excursion
3900       (nnheader-set-temp-buffer " *gnus sparse threads*")
3901       (while (setq header (pop headers))
3902         (when (and (setq references (mail-header-references header))
3903                    (not (string= references "")))
3904           (insert references)
3905           (setq child (mail-header-id header)
3906                 subject (mail-header-subject header)
3907                 date (mail-header-date header)
3908                 generation 0)
3909           (while (search-backward ">" nil t)
3910             (setq end (1+ (point)))
3911             (when (search-backward "<" nil t)
3912               (setq new-child (buffer-substring (point) end))
3913               (push (list (incf generation)
3914                           child (setq child new-child)
3915                           subject date)
3916                     relations)))
3917           (when child
3918             (push (list (1+ generation) child nil subject) relations))
3919           (erase-buffer)))
3920       (kill-buffer (current-buffer)))
3921     ;; Sort over trustworthiness.
3922     (mapcar
3923      (lambda (relation)
3924        (when (gnus-dependencies-add-header
3925               (make-full-mail-header
3926                gnus-reffed-article-number
3927                (nth 3 relation) "" (or (nth 4 relation) "")
3928                (nth 1 relation)
3929                (or (nth 2 relation) "") 0 0 "")
3930               gnus-newsgroup-dependencies nil)
3931          (push gnus-reffed-article-number gnus-newsgroup-limit)
3932          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3933          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3934                gnus-newsgroup-reads)
3935          (decf gnus-reffed-article-number)))
3936      (sort relations 'car-less-than-car))
3937     (gnus-message 7 "Making sparse threads...done")))
3938
3939 (defun gnus-build-old-threads ()
3940   ;; Look at all the articles that refer back to old articles, and
3941   ;; fetch the headers for the articles that aren't there.  This will
3942   ;; build complete threads - if the roots haven't been expired by the
3943   ;; server, that is.
3944   (let ((mail-parse-charset gnus-newsgroup-charset)
3945         id heads)
3946     (mapatoms
3947      (lambda (refs)
3948        (when (not (car (symbol-value refs)))
3949          (setq heads (cdr (symbol-value refs)))
3950          (while heads
3951            (if (memq (mail-header-number (caar heads))
3952                      gnus-newsgroup-dormant)
3953                (setq heads (cdr heads))
3954              (setq id (symbol-name refs))
3955              (while (and (setq id (gnus-build-get-header id))
3956                          (not (car (gnus-id-to-thread id)))))
3957              (setq heads nil)))))
3958      gnus-newsgroup-dependencies)))
3959
3960 ;; This function has to be called with point after the article number
3961 ;; on the beginning of the line.
3962 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3963   (let ((eol (gnus-point-at-eol))
3964         (buffer (current-buffer))
3965         header references in-reply-to)
3966
3967     ;; overview: [num subject from date id refs chars lines misc]
3968     (unwind-protect
3969         (let (x)
3970           (narrow-to-region (point) eol)
3971           (unless (eobp)
3972             (forward-char))
3973
3974           (setq header
3975                 (make-full-mail-header
3976                  number                 ; number
3977                  (condition-case ()     ; subject
3978                      (funcall gnus-decode-encoded-word-function
3979                               (setq x (nnheader-nov-field)))
3980                    (error x))
3981                  (condition-case ()     ; from
3982                      (funcall gnus-decode-encoded-word-function
3983                               (setq x (nnheader-nov-field)))
3984                    (error x))
3985                  (nnheader-nov-field)   ; date
3986                  (nnheader-nov-read-message-id) ; id
3987                  (setq references (nnheader-nov-field)) ; refs
3988                  (nnheader-nov-read-integer) ; chars
3989                  (nnheader-nov-read-integer) ; lines
3990                  (unless (eobp)
3991                    (if (looking-at "Xref: ")
3992                        (goto-char (match-end 0)))
3993                    (nnheader-nov-field)) ; Xref
3994                  (nnheader-nov-parse-extra)))) ; extra
3995
3996       (widen))
3997
3998     (when (and (string= references "")
3999                (setq in-reply-to (mail-header-extra header))
4000                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4001       (mail-header-set-references
4002        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4003
4004     (when gnus-alter-header-function
4005       (funcall gnus-alter-header-function header))
4006     (gnus-dependencies-add-header header dependencies force-new)))
4007
4008 (defun gnus-build-get-header (id)
4009   "Look through the buffer of NOV lines and find the header to ID.
4010 Enter this line into the dependencies hash table, and return
4011 the id of the parent article (if any)."
4012   (let ((deps gnus-newsgroup-dependencies)
4013         found header)
4014     (prog1
4015         (save-excursion
4016           (set-buffer nntp-server-buffer)
4017           (let ((case-fold-search nil))
4018             (goto-char (point-min))
4019             (while (and (not found)
4020                         (search-forward id nil t))
4021               (beginning-of-line)
4022               (setq found (looking-at
4023                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4024                                    (regexp-quote id))))
4025               (or found (beginning-of-line 2)))
4026             (when found
4027               (beginning-of-line)
4028               (and
4029                (setq header (gnus-nov-parse-line
4030                              (read (current-buffer)) deps))
4031                (gnus-parent-id (mail-header-references header))))))
4032       (when header
4033         (let ((number (mail-header-number header)))
4034           (push number gnus-newsgroup-limit)
4035           (push header gnus-newsgroup-headers)
4036           (if (memq number gnus-newsgroup-unselected)
4037               (progn
4038                 (setq gnus-newsgroup-unreads
4039                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4040                                                number))
4041                 (setq gnus-newsgroup-unselected
4042                       (delq number gnus-newsgroup-unselected)))
4043             (push number gnus-newsgroup-ancient)))))))
4044
4045 (defun gnus-build-all-threads ()
4046   "Read all the headers."
4047   (let ((gnus-summary-ignore-duplicates t)
4048         (mail-parse-charset gnus-newsgroup-charset)
4049         (dependencies gnus-newsgroup-dependencies)
4050         header article)
4051     (save-excursion
4052       (set-buffer nntp-server-buffer)
4053       (let ((case-fold-search nil))
4054         (goto-char (point-min))
4055         (while (not (eobp))
4056           (ignore-errors
4057             (setq article (read (current-buffer))
4058                   header (gnus-nov-parse-line article dependencies)))
4059           (when header
4060             (save-excursion
4061               (set-buffer gnus-summary-buffer)
4062               (push header gnus-newsgroup-headers)
4063               (if (memq (setq article (mail-header-number header))
4064                         gnus-newsgroup-unselected)
4065                   (progn
4066                     (setq gnus-newsgroup-unreads
4067                           (gnus-add-to-sorted-list
4068                            gnus-newsgroup-unreads article))
4069                     (setq gnus-newsgroup-unselected
4070                           (delq article gnus-newsgroup-unselected)))
4071                 (push article gnus-newsgroup-ancient)))
4072             (forward-line 1)))))))
4073
4074 (defun gnus-summary-update-article-line (article header)
4075   "Update the line for ARTICLE using HEADERS."
4076   (let* ((id (mail-header-id header))
4077          (thread (gnus-id-to-thread id)))
4078     (unless thread
4079       (error "Article in no thread"))
4080     ;; Update the thread.
4081     (setcar thread header)
4082     (gnus-summary-goto-subject article)
4083     (let* ((datal (gnus-data-find-list article))
4084            (data (car datal))
4085            (buffer-read-only nil)
4086            (level (gnus-summary-thread-level)))
4087       (gnus-delete-line)
4088       (let ((inserted (- (point)
4089                          (progn
4090                            (gnus-summary-insert-line
4091                             header level nil 
4092                             (memq article gnus-newsgroup-undownloaded)
4093                             (gnus-article-mark article)
4094                             (memq article gnus-newsgroup-replied)
4095                             (memq article gnus-newsgroup-expirable)
4096                             ;; Only insert the Subject string when it's different
4097                             ;; from the previous Subject string.
4098                             (if (and
4099                                  gnus-show-threads
4100                                  (gnus-subject-equal
4101                                   (condition-case ()
4102                                       (mail-header-subject
4103                                        (gnus-data-header
4104                                         (cadr
4105                                          (gnus-data-find-list
4106                                           article
4107                                           (gnus-data-list t)))))
4108                                     ;; Error on the side of excessive subjects.
4109                                     (error ""))
4110                                   (mail-header-subject header)))
4111                                 ""
4112                               (mail-header-subject header))
4113                             nil (cdr (assq article gnus-newsgroup-scored))
4114                             (memq article gnus-newsgroup-processable))
4115                            (point)))))
4116         (when (cdr datal)
4117           (gnus-data-update-list
4118            (cdr datal) 
4119            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4120
4121 (defun gnus-summary-update-article (article &optional iheader)
4122   "Update ARTICLE in the summary buffer."
4123   (set-buffer gnus-summary-buffer)
4124   (let* ((header (gnus-summary-article-header article))
4125          (id (mail-header-id header))
4126          (data (gnus-data-find article))
4127          (thread (gnus-id-to-thread id))
4128          (references (mail-header-references header))
4129          (parent
4130           (gnus-id-to-thread
4131            (or (gnus-parent-id
4132                 (when (and references
4133                            (not (equal "" references)))
4134                   references))
4135                "none")))
4136          (buffer-read-only nil)
4137          (old (car thread)))
4138     (when thread
4139       (unless iheader
4140         (setcar thread nil)
4141         (when parent
4142           (delq thread parent)))
4143       (if (gnus-summary-insert-subject id header)
4144           ;; Set the (possibly) new article number in the data structure.
4145           (gnus-data-set-number data (gnus-id-to-article id))
4146         (setcar thread old)
4147         nil))))
4148
4149 (defun gnus-rebuild-thread (id &optional line)
4150   "Rebuild the thread containing ID.
4151 If LINE, insert the rebuilt thread starting on line LINE."
4152   (let ((buffer-read-only nil)
4153         old-pos current thread data)
4154     (if (not gnus-show-threads)
4155         (setq thread (list (car (gnus-id-to-thread id))))
4156       ;; Get the thread this article is part of.
4157       (setq thread (gnus-remove-thread id)))
4158     (setq old-pos (gnus-point-at-bol))
4159     (setq current (save-excursion
4160                     (and (re-search-backward "[\r\n]" nil t)
4161                          (gnus-summary-article-number))))
4162     ;; If this is a gathered thread, we have to go some re-gathering.
4163     (when (stringp (car thread))
4164       (let ((subject (car thread))
4165             roots thr)
4166         (setq thread (cdr thread))
4167         (while thread
4168           (unless (memq (setq thr (gnus-id-to-thread
4169                                    (gnus-root-id
4170                                     (mail-header-id (caar thread)))))
4171                         roots)
4172             (push thr roots))
4173           (setq thread (cdr thread)))
4174         ;; We now have all (unique) roots.
4175         (if (= (length roots) 1)
4176             ;; All the loose roots are now one solid root.
4177             (setq thread (car roots))
4178           (setq thread (cons subject (gnus-sort-threads roots))))))
4179     (let (threads)
4180       ;; We then insert this thread into the summary buffer.
4181       (when line
4182         (goto-char (point-min))
4183         (forward-line (1- line)))
4184       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4185         (if gnus-show-threads
4186             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4187           (gnus-summary-prepare-unthreaded thread))
4188         (setq data (nreverse gnus-newsgroup-data))
4189         (setq threads gnus-newsgroup-threads))
4190       ;; We splice the new data into the data structure.
4191       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4192       ;;!!! then we want to insert at the beginning of the buffer.
4193       ;;!!! That happens to be true with Gnus now, but that may
4194       ;;!!! change in the future.  Perhaps.
4195       (gnus-data-enter-list
4196        (if line nil current) data (- (point) old-pos))
4197       (setq gnus-newsgroup-threads
4198             (nconc threads gnus-newsgroup-threads))
4199       (gnus-data-compute-positions))))
4200
4201 (defun gnus-number-to-header (number)
4202   "Return the header for article NUMBER."
4203   (let ((headers gnus-newsgroup-headers))
4204     (while (and headers
4205                 (not (= number (mail-header-number (car headers)))))
4206       (pop headers))
4207     (when headers
4208       (car headers))))
4209
4210 (defun gnus-parent-headers (in-headers &optional generation)
4211   "Return the headers of the GENERATIONeth parent of HEADERS."
4212   (unless generation
4213     (setq generation 1))
4214   (let ((parent t)
4215         (headers in-headers)
4216         references)
4217     (while (and parent
4218                 (not (zerop generation))
4219                 (setq references (mail-header-references headers)))
4220       (setq headers (if (and references
4221                              (setq parent (gnus-parent-id references)))
4222                         (car (gnus-id-to-thread parent))
4223                       nil))
4224       (decf generation))
4225     (and (not (eq headers in-headers))
4226          headers)))
4227
4228 (defun gnus-id-to-thread (id)
4229   "Return the (sub-)thread where ID appears."
4230   (gnus-gethash id gnus-newsgroup-dependencies))
4231
4232 (defun gnus-id-to-article (id)
4233   "Return the article number of ID."
4234   (let ((thread (gnus-id-to-thread id)))
4235     (when (and thread
4236                (car thread))
4237       (mail-header-number (car thread)))))
4238
4239 (defun gnus-id-to-header (id)
4240   "Return the article headers of ID."
4241   (car (gnus-id-to-thread id)))
4242
4243 (defun gnus-article-displayed-root-p (article)
4244   "Say whether ARTICLE is a root(ish) article."
4245   (let ((level (gnus-summary-thread-level article))
4246         (refs (mail-header-references  (gnus-summary-article-header article)))
4247         particle)
4248     (cond
4249      ((null level) nil)
4250      ((zerop level) t)
4251      ((null refs) t)
4252      ((null (gnus-parent-id refs)) t)
4253      ((and (= 1 level)
4254            (null (setq particle (gnus-id-to-article
4255                                  (gnus-parent-id refs))))
4256            (null (gnus-summary-thread-level particle)))))))
4257
4258 (defun gnus-root-id (id)
4259   "Return the id of the root of the thread where ID appears."
4260   (let (last-id prev)
4261     (while (and id (setq prev (car (gnus-id-to-thread id))))
4262       (setq last-id id
4263             id (gnus-parent-id (mail-header-references prev))))
4264     last-id))
4265
4266 (defun gnus-articles-in-thread (thread)
4267   "Return the list of articles in THREAD."
4268   (cons (mail-header-number (car thread))
4269         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4270
4271 (defun gnus-remove-thread (id &optional dont-remove)
4272   "Remove the thread that has ID in it."
4273   (let (headers thread last-id)
4274     ;; First go up in this thread until we find the root.
4275     (setq last-id (gnus-root-id id)
4276           headers (message-flatten-list (gnus-id-to-thread last-id)))
4277     ;; We have now found the real root of this thread.  It might have
4278     ;; been gathered into some loose thread, so we have to search
4279     ;; through the threads to find the thread we wanted.
4280     (let ((threads gnus-newsgroup-threads)
4281           sub)
4282       (while threads
4283         (setq sub (car threads))
4284         (if (stringp (car sub))
4285             ;; This is a gathered thread, so we look at the roots
4286             ;; below it to find whether this article is in this
4287             ;; gathered root.
4288             (progn
4289               (setq sub (cdr sub))
4290               (while sub
4291                 (when (member (caar sub) headers)
4292                   (setq thread (car threads)
4293                         threads nil
4294                         sub nil))
4295                 (setq sub (cdr sub))))
4296           ;; It's an ordinary thread, so we check it.
4297           (when (eq (car sub) (car headers))
4298             (setq thread sub
4299                   threads nil)))
4300         (setq threads (cdr threads)))
4301       ;; If this article is in no thread, then it's a root.
4302       (if thread
4303           (unless dont-remove
4304             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4305         (setq thread (gnus-id-to-thread last-id)))
4306       (when thread
4307         (prog1
4308             thread                      ; We return this thread.
4309           (unless dont-remove
4310             (if (stringp (car thread))
4311                 (progn
4312                   ;; If we use dummy roots, then we have to remove the
4313                   ;; dummy root as well.
4314                   (when (eq gnus-summary-make-false-root 'dummy)
4315                     ;; We go to the dummy root by going to
4316                     ;; the first sub-"thread", and then one line up.
4317                     (gnus-summary-goto-article
4318                      (mail-header-number (caadr thread)))
4319                     (forward-line -1)
4320                     (gnus-delete-line)
4321                     (gnus-data-compute-positions))
4322                   (setq thread (cdr thread))
4323                   (while thread
4324                     (gnus-remove-thread-1 (car thread))
4325                     (setq thread (cdr thread))))
4326               (gnus-remove-thread-1 thread))))))))
4327
4328 (defun gnus-remove-thread-1 (thread)
4329   "Remove the thread THREAD recursively."
4330   (let ((number (mail-header-number (pop thread)))
4331         d)
4332     (setq thread (reverse thread))
4333     (while thread
4334       (gnus-remove-thread-1 (pop thread)))
4335     (when (setq d (gnus-data-find number))
4336       (goto-char (gnus-data-pos d))
4337       (gnus-summary-show-thread)
4338       (gnus-data-remove
4339        number
4340        (- (gnus-point-at-bol)
4341           (prog1
4342               (1+ (gnus-point-at-eol))
4343             (gnus-delete-line)))))))
4344
4345 (defun gnus-sort-threads-1 (threads func)
4346   (sort (mapcar (lambda (thread)
4347                   (cons (car thread)
4348                         (and (cdr thread)
4349                              (gnus-sort-threads-1 (cdr thread) func))))
4350                 threads) func))
4351
4352 (defun gnus-sort-threads (threads)
4353   "Sort THREADS."
4354   (if (not gnus-thread-sort-functions)
4355       threads
4356     (gnus-message 8 "Sorting threads...")
4357     (let ((max-lisp-eval-depth 5000))
4358       (prog1 (gnus-sort-threads-1
4359          threads
4360          (gnus-make-sort-function gnus-thread-sort-functions))
4361         (gnus-message 8 "Sorting threads...done")))))
4362
4363 (defun gnus-sort-articles (articles)
4364   "Sort ARTICLES."
4365   (when gnus-article-sort-functions
4366     (gnus-message 7 "Sorting articles...")
4367     (prog1
4368         (setq gnus-newsgroup-headers
4369               (sort articles (gnus-make-sort-function
4370                               gnus-article-sort-functions)))
4371       (gnus-message 7 "Sorting articles...done"))))
4372
4373 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4374 (defmacro gnus-thread-header (thread)
4375   "Return header of first article in THREAD.
4376 Note that THREAD must never, ever be anything else than a variable -
4377 using some other form will lead to serious barfage."
4378   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4379   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4380   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4381         (vector thread) 2))
4382
4383 (defsubst gnus-article-sort-by-number (h1 h2)
4384   "Sort articles by article number."
4385   (< (mail-header-number h1)
4386      (mail-header-number h2)))
4387
4388 (defun gnus-thread-sort-by-number (h1 h2)
4389   "Sort threads by root article number."
4390   (gnus-article-sort-by-number
4391    (gnus-thread-header h1) (gnus-thread-header h2)))
4392
4393 (defsubst gnus-article-sort-by-random (h1 h2)
4394   "Sort articles by article number."
4395   (zerop (random 2)))
4396
4397 (defun gnus-thread-sort-by-random (h1 h2)
4398   "Sort threads by root article number."
4399   (gnus-article-sort-by-random
4400    (gnus-thread-header h1) (gnus-thread-header h2)))
4401
4402 (defsubst gnus-article-sort-by-lines (h1 h2)
4403   "Sort articles by article Lines header."
4404   (< (mail-header-lines h1)
4405      (mail-header-lines h2)))
4406
4407 (defun gnus-thread-sort-by-lines (h1 h2)
4408   "Sort threads by root article Lines header."
4409   (gnus-article-sort-by-lines
4410    (gnus-thread-header h1) (gnus-thread-header h2)))
4411
4412 (defsubst gnus-article-sort-by-chars (h1 h2)
4413   "Sort articles by octet length."
4414   (< (mail-header-chars h1)
4415      (mail-header-chars h2)))
4416
4417 (defun gnus-thread-sort-by-chars (h1 h2)
4418   "Sort threads by root article octet length."
4419   (gnus-article-sort-by-chars
4420    (gnus-thread-header h1) (gnus-thread-header h2)))
4421
4422 (defsubst gnus-article-sort-by-author (h1 h2)
4423   "Sort articles by root author."
4424   (string-lessp
4425    (let ((extract (funcall
4426                    gnus-extract-address-components
4427                    (mail-header-from h1))))
4428      (or (car extract) (cadr extract) ""))
4429    (let ((extract (funcall
4430                    gnus-extract-address-components
4431                    (mail-header-from h2))))
4432      (or (car extract) (cadr extract) ""))))
4433
4434 (defun gnus-thread-sort-by-author (h1 h2)
4435   "Sort threads by root author."
4436   (gnus-article-sort-by-author
4437    (gnus-thread-header h1)  (gnus-thread-header h2)))
4438
4439 (defsubst gnus-article-sort-by-subject (h1 h2)
4440   "Sort articles by root subject."
4441   (string-lessp
4442    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4443    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4444
4445 (defun gnus-thread-sort-by-subject (h1 h2)
4446   "Sort threads by root subject."
4447   (gnus-article-sort-by-subject
4448    (gnus-thread-header h1) (gnus-thread-header h2)))
4449
4450 (defsubst gnus-article-sort-by-date (h1 h2)
4451   "Sort articles by root article date."
4452   (time-less-p
4453    (gnus-date-get-time (mail-header-date h1))
4454    (gnus-date-get-time (mail-header-date h2))))
4455
4456 (defun gnus-thread-sort-by-date (h1 h2)
4457   "Sort threads by root article date."
4458   (gnus-article-sort-by-date
4459    (gnus-thread-header h1) (gnus-thread-header h2)))
4460
4461 (defsubst gnus-article-sort-by-score (h1 h2)
4462   "Sort articles by root article score.
4463 Unscored articles will be counted as having a score of zero."
4464   (> (or (cdr (assq (mail-header-number h1)
4465                     gnus-newsgroup-scored))
4466          gnus-summary-default-score 0)
4467      (or (cdr (assq (mail-header-number h2)
4468                     gnus-newsgroup-scored))
4469          gnus-summary-default-score 0)))
4470
4471 (defun gnus-thread-sort-by-score (h1 h2)
4472   "Sort threads by root article score."
4473   (gnus-article-sort-by-score
4474    (gnus-thread-header h1) (gnus-thread-header h2)))
4475
4476 (defun gnus-thread-sort-by-total-score (h1 h2)
4477   "Sort threads by the sum of all scores in the thread.
4478 Unscored articles will be counted as having a score of zero."
4479   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4480
4481 (defun gnus-thread-total-score (thread)
4482   ;; This function find the total score of THREAD.
4483   (cond
4484    ((null thread)
4485     0)
4486    ((consp thread)
4487     (if (stringp (car thread))
4488         (apply gnus-thread-score-function 0
4489                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4490       (gnus-thread-total-score-1 thread)))
4491    (t
4492     (gnus-thread-total-score-1 (list thread)))))
4493
4494 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4495   "Sort threads such that the thread with the most recently arrived article comes first."
4496   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4497
4498 (defun gnus-thread-highest-number (thread)
4499   "Return the highest article number in THREAD."
4500   (apply 'max (mapcar (lambda (header)
4501                         (mail-header-number header))
4502                       (message-flatten-list thread))))
4503
4504 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4505   "Sort threads such that the thread with the most recently dated article comes first."
4506   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4507
4508 (defun gnus-thread-latest-date (thread)
4509   "Return the highest article date in THREAD."
4510   (let ((previous-time 0))
4511     (apply 'max
4512            (mapcar
4513             (lambda (header)
4514               (setq previous-time
4515                     (time-to-seconds
4516                      (condition-case ()
4517                          (mail-header-parse-date (mail-header-date header))
4518                        (error previous-time)))))
4519             (sort
4520              (message-flatten-list thread)
4521              (lambda (h1 h2)
4522                (< (mail-header-number h1)
4523                   (mail-header-number h2))))))))
4524
4525 (defun gnus-thread-total-score-1 (root)
4526   ;; This function find the total score of the thread below ROOT.
4527   (setq root (car root))
4528   (apply gnus-thread-score-function
4529          (or (append
4530               (mapcar 'gnus-thread-total-score
4531                       (cdr (gnus-id-to-thread (mail-header-id root))))
4532               (when (> (mail-header-number root) 0)
4533                 (list (or (cdr (assq (mail-header-number root)
4534                                      gnus-newsgroup-scored))
4535                           gnus-summary-default-score 0))))
4536              (list gnus-summary-default-score)
4537              '(0))))
4538
4539 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4540 (defvar gnus-tmp-prev-subject nil)
4541 (defvar gnus-tmp-false-parent nil)
4542 (defvar gnus-tmp-root-expunged nil)
4543 (defvar gnus-tmp-dummy-line nil)
4544
4545 (eval-when-compile (defvar gnus-tmp-header))
4546 (defun gnus-extra-header (type &optional header)
4547   "Return the extra header of TYPE."
4548   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4549       ""))
4550
4551 (defvar gnus-tmp-thread-tree-header-string "")
4552
4553 (defcustom gnus-sum-thread-tree-root "> "
4554   "With %B spec, used for the root of a thread.
4555 If nil, use subject instead."
4556   :type 'string
4557   :group 'gnus-thread)
4558 (defcustom gnus-sum-thread-tree-single-indent ""
4559   "With %B spec, used for a thread with just one message.
4560 If nil, use subject instead."
4561   :type 'string
4562   :group 'gnus-thread)
4563 (defcustom gnus-sum-thread-tree-vertical "| "
4564   "With %B spec, used for drawing a vertical line."
4565   :type 'string
4566   :group 'gnus-thread)
4567 (defcustom gnus-sum-thread-tree-indent "  "
4568   "With %B spec, used for indenting."
4569   :type 'string
4570   :group 'gnus-thread)
4571 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4572   "With %B spec, used for a leaf with brothers."
4573   :type 'string
4574   :group 'gnus-thread)
4575 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4576   "With %B spec, used for a leaf without brothers."
4577   :type 'string
4578   :group 'gnus-thread)
4579
4580 (defun gnus-summary-prepare-threads (threads)
4581   "Prepare summary buffer from THREADS and indentation LEVEL.
4582 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4583 or a straight list of headers."
4584   (gnus-message 7 "Generating summary...")
4585
4586   (setq gnus-newsgroup-threads threads)
4587   (beginning-of-line)
4588
4589   (let ((gnus-tmp-level 0)
4590         (default-score (or gnus-summary-default-score 0))
4591         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4592         (building-line-count gnus-summary-display-while-building)
4593         (building-count (integerp gnus-summary-display-while-building))
4594         thread number subject stack state gnus-tmp-gathered beg-match
4595         new-roots gnus-tmp-new-adopts thread-end simp-subject
4596         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4597         gnus-tmp-replied gnus-tmp-subject-or-nil
4598         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4599         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4600         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4601         tree-stack)
4602
4603     (setq gnus-tmp-prev-subject nil
4604           gnus-tmp-thread-tree-header-string "")
4605
4606     (if (vectorp (car threads))
4607         ;; If this is a straight (sic) list of headers, then a
4608         ;; threaded summary display isn't required, so we just create
4609         ;; an unthreaded one.
4610         (gnus-summary-prepare-unthreaded threads)
4611
4612       ;; Do the threaded display.
4613
4614       (if gnus-summary-display-while-building
4615           (switch-to-buffer (buffer-name)))
4616       (while (or threads stack gnus-tmp-new-adopts new-roots)
4617
4618         (if (and (= gnus-tmp-level 0)
4619                  (or (not stack)
4620                      (= (caar stack) 0))
4621                  (not gnus-tmp-false-parent)
4622                  (or gnus-tmp-new-adopts new-roots))
4623             (if gnus-tmp-new-adopts
4624                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4625                       thread (list (car gnus-tmp-new-adopts))
4626                       gnus-tmp-header (caar thread)
4627                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4628               (when new-roots
4629                 (setq thread (list (car new-roots))
4630                       gnus-tmp-header (caar thread)
4631                       new-roots (cdr new-roots))))
4632
4633           (if threads
4634               ;; If there are some threads, we do them before the
4635               ;; threads on the stack.
4636               (setq thread threads
4637                     gnus-tmp-header (caar thread))
4638             ;; There were no current threads, so we pop something off
4639             ;; the stack.
4640             (setq state (car stack)
4641                   gnus-tmp-level (car state)
4642                   tree-stack (cadr state)
4643                   thread (caddr state)
4644                   stack (cdr stack)
4645                   gnus-tmp-header (caar thread))))
4646
4647         (setq gnus-tmp-false-parent nil)
4648         (setq gnus-tmp-root-expunged nil)
4649         (setq thread-end nil)
4650
4651         (if (stringp gnus-tmp-header)
4652             ;; The header is a dummy root.
4653             (cond
4654              ((eq gnus-summary-make-false-root 'adopt)
4655               ;; We let the first article adopt the rest.
4656               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4657                                                (cddar thread)))
4658               (setq gnus-tmp-gathered
4659                     (nconc (mapcar
4660                             (lambda (h) (mail-header-number (car h)))
4661                             (cddar thread))
4662                            gnus-tmp-gathered))
4663               (setq thread (cons (list (caar thread)
4664                                        (cadar thread))
4665                                  (cdr thread)))
4666               (setq gnus-tmp-level -1
4667                     gnus-tmp-false-parent t))
4668              ((eq gnus-summary-make-false-root 'empty)
4669               ;; We print adopted articles with empty subject fields.
4670               (setq gnus-tmp-gathered
4671                     (nconc (mapcar
4672                             (lambda (h) (mail-header-number (car h)))
4673                             (cddar thread))
4674                            gnus-tmp-gathered))
4675               (setq gnus-tmp-level -1))
4676              ((eq gnus-summary-make-false-root 'dummy)
4677               ;; We remember that we probably want to output a dummy
4678               ;; root.
4679               (setq gnus-tmp-dummy-line gnus-tmp-header)
4680               (setq gnus-tmp-prev-subject gnus-tmp-header))
4681              (t
4682               ;; We do not make a root for the gathered
4683               ;; sub-threads at all.
4684               (setq gnus-tmp-level -1)))
4685
4686           (setq number (mail-header-number gnus-tmp-header)
4687                 subject (mail-header-subject gnus-tmp-header)
4688                 simp-subject (gnus-simplify-subject-fully subject))
4689
4690           (cond
4691            ;; If the thread has changed subject, we might want to make
4692            ;; this subthread into a root.
4693            ((and (null gnus-thread-ignore-subject)
4694                  (not (zerop gnus-tmp-level))
4695                  gnus-tmp-prev-subject
4696                  (not (string= gnus-tmp-prev-subject simp-subject)))
4697             (setq new-roots (nconc new-roots (list (car thread)))
4698                   thread-end t
4699                   gnus-tmp-header nil))
4700            ;; If the article lies outside the current limit,
4701            ;; then we do not display it.
4702            ((not (memq number gnus-newsgroup-limit))
4703             (setq gnus-tmp-gathered
4704                   (nconc (mapcar
4705                           (lambda (h) (mail-header-number (car h)))
4706                           (cdar thread))
4707                          gnus-tmp-gathered))
4708             (setq gnus-tmp-new-adopts (if (cdar thread)
4709                                           (append gnus-tmp-new-adopts
4710                                                   (cdar thread))
4711                                         gnus-tmp-new-adopts)
4712                   thread-end t
4713                   gnus-tmp-header nil)
4714             (when (zerop gnus-tmp-level)
4715               (setq gnus-tmp-root-expunged t)))
4716            ;; Perhaps this article is to be marked as read?
4717            ((and gnus-summary-mark-below
4718                  (< (or (cdr (assq number gnus-newsgroup-scored))
4719                         default-score)
4720                     gnus-summary-mark-below)
4721                  ;; Don't touch sparse articles.
4722                  (not (gnus-summary-article-sparse-p number))
4723                  (not (gnus-summary-article-ancient-p number)))
4724             (setq gnus-newsgroup-unreads
4725                   (delq number gnus-newsgroup-unreads))
4726             (if gnus-newsgroup-auto-expire
4727                 (setq gnus-newsgroup-expirable
4728                       (gnus-add-to-sorted-list
4729                        gnus-newsgroup-expirable number))
4730               (push (cons number gnus-low-score-mark)
4731                     gnus-newsgroup-reads))))
4732
4733           (when gnus-tmp-header
4734             ;; We may have an old dummy line to output before this
4735             ;; article.
4736             (when (and gnus-tmp-dummy-line
4737                        (gnus-subject-equal
4738                         gnus-tmp-dummy-line
4739                         (mail-header-subject gnus-tmp-header)))
4740               (gnus-summary-insert-dummy-line
4741                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4742               (setq gnus-tmp-dummy-line nil))
4743
4744             ;; Compute the mark.
4745             (setq gnus-tmp-unread (gnus-article-mark number))
4746
4747             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4748                                   gnus-tmp-header gnus-tmp-level)
4749                   gnus-newsgroup-data)
4750
4751             ;; Actually insert the line.
4752             (setq
4753              gnus-tmp-subject-or-nil
4754              (cond
4755               ((and gnus-thread-ignore-subject
4756                     gnus-tmp-prev-subject
4757                     (not (string= gnus-tmp-prev-subject simp-subject)))
4758                subject)
4759               ((zerop gnus-tmp-level)
4760                (if (and (eq gnus-summary-make-false-root 'empty)
4761                         (memq number gnus-tmp-gathered)
4762                         gnus-tmp-prev-subject
4763                         (string= gnus-tmp-prev-subject simp-subject))
4764                    gnus-summary-same-subject
4765                  subject))
4766               (t gnus-summary-same-subject)))
4767             (if (and (eq gnus-summary-make-false-root 'adopt)
4768                      (= gnus-tmp-level 1)
4769                      (memq number gnus-tmp-gathered))
4770                 (setq gnus-tmp-opening-bracket ?\<
4771                       gnus-tmp-closing-bracket ?\>)
4772               (setq gnus-tmp-opening-bracket ?\[
4773                     gnus-tmp-closing-bracket ?\]))
4774             (setq
4775              gnus-tmp-indentation
4776              (aref gnus-thread-indent-array gnus-tmp-level)
4777              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4778              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4779                                 gnus-summary-default-score 0)
4780              gnus-tmp-score-char
4781              (if (or (null gnus-summary-default-score)
4782                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4783                          gnus-summary-zcore-fuzz))
4784                  ?                      ;Whitespace
4785                (if (< gnus-tmp-score gnus-summary-default-score)
4786                    gnus-score-below-mark gnus-score-over-mark))
4787              gnus-tmp-replied
4788              (cond ((memq number gnus-newsgroup-processable)
4789                     gnus-process-mark)
4790                    ((memq number gnus-newsgroup-cached)
4791                     gnus-cached-mark)
4792                    ((memq number gnus-newsgroup-replied)
4793                     gnus-replied-mark)
4794                    ((memq number gnus-newsgroup-forwarded)
4795                     gnus-forwarded-mark)
4796                    ((memq number gnus-newsgroup-saved)
4797                     gnus-saved-mark)
4798                    ((memq number gnus-newsgroup-recent)
4799                     gnus-recent-mark)
4800                    ((memq number gnus-newsgroup-unseen)
4801                     gnus-unseen-mark)
4802                    (t gnus-no-mark))
4803              gnus-tmp-downloaded
4804              (cond ((memq number gnus-newsgroup-undownloaded) 
4805                     gnus-undownloaded-mark)
4806                    (gnus-newsgroup-agentized
4807                     gnus-downloaded-mark)
4808                    (t
4809                     gnus-no-mark))
4810              gnus-tmp-from (mail-header-from gnus-tmp-header)
4811              gnus-tmp-name
4812              (cond
4813               ((string-match "<[^>]+> *$" gnus-tmp-from)
4814                (setq beg-match (match-beginning 0))
4815                (or (and (string-match "^\".+\"" gnus-tmp-from)
4816                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4817                    (substring gnus-tmp-from 0 beg-match)))
4818               ((string-match "(.+)" gnus-tmp-from)
4819                (substring gnus-tmp-from
4820                           (1+ (match-beginning 0)) (1- (match-end 0))))
4821               (t gnus-tmp-from))
4822
4823              ;; Do the %B string
4824              gnus-tmp-thread-tree-header-string
4825              (cond
4826               ((not gnus-show-threads) "")
4827               ((zerop gnus-tmp-level)
4828                (if (cdar thread)
4829                    (or gnus-sum-thread-tree-root subject)
4830                  (or gnus-sum-thread-tree-single-indent subject)))
4831               (t
4832                (concat (apply 'concat
4833                               (mapcar (lambda (item)
4834                                         (if (= item 1)
4835                                             gnus-sum-thread-tree-vertical
4836                                           gnus-sum-thread-tree-indent))
4837                                       (cdr (reverse tree-stack))))
4838                        (if (nth 1 thread)
4839                            gnus-sum-thread-tree-leaf-with-other
4840                          gnus-sum-thread-tree-single-leaf)))))
4841             (when (string= gnus-tmp-name "")
4842               (setq gnus-tmp-name gnus-tmp-from))
4843             (unless (numberp gnus-tmp-lines)
4844               (setq gnus-tmp-lines -1))
4845             (if (= gnus-tmp-lines -1)
4846                 (setq gnus-tmp-lines "?")
4847               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4848               (gnus-put-text-property
4849              (point)
4850              (progn (eval gnus-summary-line-format-spec) (point))
4851                'gnus-number number)
4852             (when gnus-visual-p
4853               (forward-line -1)
4854               (gnus-run-hooks 'gnus-summary-update-hook)
4855               (forward-line 1))
4856
4857             (setq gnus-tmp-prev-subject simp-subject)))
4858
4859         (when (nth 1 thread)
4860           (push (list (max 0 gnus-tmp-level)
4861                       (copy-list tree-stack)
4862                       (nthcdr 1 thread))
4863                 stack))
4864         (push (if (nth 1 thread) 1 0) tree-stack)
4865         (incf gnus-tmp-level)
4866         (setq threads (if thread-end nil (cdar thread)))
4867         (if gnus-summary-display-while-building
4868             (if building-count
4869                 (progn
4870                   ;; use a set frequency
4871                   (setq building-line-count (1- building-line-count))
4872                   (when (= building-line-count 0)
4873                     (sit-for 0)
4874                     (setq building-line-count
4875                           gnus-summary-display-while-building)))
4876               ;; always
4877               (sit-for 0)))
4878         (unless threads
4879           (setq gnus-tmp-level 0)))))
4880   (gnus-message 7 "Generating summary...done"))
4881
4882 (defun gnus-summary-prepare-unthreaded (headers)
4883   "Generate an unthreaded summary buffer based on HEADERS."
4884   (let (header number mark)
4885
4886     (beginning-of-line)
4887
4888     (while headers
4889       ;; We may have to root out some bad articles...
4890       (when (memq (setq number (mail-header-number
4891                                 (setq header (pop headers))))
4892                   gnus-newsgroup-limit)
4893         ;; Mark article as read when it has a low score.
4894         (when (and gnus-summary-mark-below
4895                    (< (or (cdr (assq number gnus-newsgroup-scored))
4896                           gnus-summary-default-score 0)
4897                       gnus-summary-mark-below)
4898                    (not (gnus-summary-article-ancient-p number)))
4899           (setq gnus-newsgroup-unreads
4900                 (delq number gnus-newsgroup-unreads))
4901           (if gnus-newsgroup-auto-expire
4902               (push number gnus-newsgroup-expirable)
4903             (push (cons number gnus-low-score-mark)
4904                   gnus-newsgroup-reads)))
4905
4906         (setq mark (gnus-article-mark number))
4907         (push (gnus-data-make number mark (1+ (point)) header 0)
4908               gnus-newsgroup-data)
4909         (gnus-summary-insert-line
4910          header 0 number
4911          (memq number gnus-newsgroup-undownloaded)
4912          mark (memq number gnus-newsgroup-replied)
4913          (memq number gnus-newsgroup-expirable)
4914          (mail-header-subject header) nil
4915          (cdr (assq number gnus-newsgroup-scored))
4916          (memq number gnus-newsgroup-processable))))))
4917
4918 (defun gnus-summary-remove-list-identifiers ()
4919   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4920   (let ((regexp (if (consp gnus-list-identifiers)
4921                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4922                   gnus-list-identifiers))
4923         changed subject)
4924     (when regexp
4925       (dolist (header gnus-newsgroup-headers)
4926         (setq subject (mail-header-subject header)
4927               changed nil)
4928         (while (string-match
4929                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4930                 subject)
4931           (setq subject
4932                 (concat (substring subject 0 (match-beginning 2))
4933                         (substring subject (match-end 0)))
4934                 changed t))
4935         (when (and changed
4936                    (string-match
4937                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4938           (setq subject
4939                 (concat (substring subject 0 (match-beginning 1))
4940                         (substring subject (match-end 1)))))
4941         (when changed
4942           (mail-header-set-subject header subject))))))
4943
4944 (defun gnus-fetch-headers (articles)
4945   "Fetch headers of ARTICLES."
4946   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4947     (gnus-message 5 "Fetching headers for %s..." name)
4948     (prog1
4949         (if (eq 'nov
4950                 (setq gnus-headers-retrieved-by
4951                       (gnus-retrieve-headers
4952                        articles gnus-newsgroup-name
4953                        ;; We might want to fetch old headers, but
4954                        ;; not if there is only 1 article.
4955                        (and (or (and
4956                                  (not (eq gnus-fetch-old-headers 'some))
4957                                  (not (numberp gnus-fetch-old-headers)))
4958                                 (> (length articles) 1))
4959                             gnus-fetch-old-headers))))
4960             (gnus-get-newsgroup-headers-xover
4961              articles nil nil gnus-newsgroup-name t)
4962           (gnus-get-newsgroup-headers))
4963       (gnus-message 5 "Fetching headers for %s...done" name))))
4964
4965 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4966   "Select newsgroup GROUP.
4967 If READ-ALL is non-nil, all articles in the group are selected.
4968 If SELECT-ARTICLES, only select those articles from GROUP."
4969   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4970          ;;!!! Dirty hack; should be removed.
4971          (gnus-summary-ignore-duplicates
4972           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4973               t
4974             gnus-summary-ignore-duplicates))
4975          (info (nth 2 entry))
4976          articles fetched-articles cached)
4977
4978     (unless (gnus-check-server
4979              (set (make-local-variable 'gnus-current-select-method)
4980                   (gnus-find-method-for-group group)))
4981       (error "Couldn't open server"))
4982
4983     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4984         (gnus-activate-group group)     ; Or we can activate it...
4985         (progn                          ; Or we bug out.
4986           (when (equal major-mode 'gnus-summary-mode)
4987             (gnus-kill-buffer (current-buffer)))
4988           (error "Couldn't activate group %s: %s"
4989                  group (gnus-status-message group))))
4990
4991     (unless (gnus-request-group group t)
4992       (when (equal major-mode 'gnus-summary-mode)
4993         (gnus-kill-buffer (current-buffer)))
4994       (error "Couldn't request group %s: %s"
4995              group (gnus-status-message group)))
4996
4997     (setq gnus-newsgroup-name group
4998           gnus-newsgroup-unselected nil
4999           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5000
5001     (let ((display (gnus-group-find-parameter group 'display)))
5002       (setq gnus-newsgroup-display
5003             (cond
5004              ((not (zerop (or (car-safe read-all) 0)))
5005               ;; The user entered the group with C-u SPC/RET, let's show
5006               ;; all articles.
5007               'gnus-not-ignore)
5008              ((eq display 'all)
5009               'gnus-not-ignore)
5010              ((arrayp display)
5011               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5012              ((numberp display)
5013               ;; The following is probably the "correct" solution, but
5014               ;; it makes Gnus fetch all headers and then limit the
5015               ;; articles (which is slow), so instead we hack the
5016               ;; select-articles parameter instead. -- Simon Josefsson
5017               ;; <jas@kth.se>
5018               ;;
5019               ;; (gnus-byte-compile
5020               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5021               ;;                         display)))))
5022               (setq select-articles
5023                     (gnus-uncompress-range
5024                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5025                              (if (> tmp 0)
5026                                  tmp
5027                                1))
5028                            (cdr (gnus-active group)))))
5029               nil)
5030              (t
5031               nil))))
5032
5033     (gnus-summary-setup-default-charset)
5034
5035     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5036     (when (gnus-virtual-group-p group)
5037       (setq cached gnus-newsgroup-cached))
5038
5039     (setq gnus-newsgroup-unreads
5040           (gnus-sorted-ndifference
5041            (gnus-sorted-ndifference gnus-newsgroup-unreads
5042                                     gnus-newsgroup-marked)
5043            gnus-newsgroup-dormant))
5044
5045     (setq gnus-newsgroup-processable nil)
5046
5047     (gnus-update-read-articles group gnus-newsgroup-unreads)
5048
5049     ;; Adjust and set lists of article marks.
5050     (when info
5051       (gnus-adjust-marked-articles info))
5052     (if (setq articles select-articles)
5053         (setq gnus-newsgroup-unselected
5054               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5055       (setq articles (gnus-articles-to-read group read-all)))
5056
5057     (cond
5058      ((null articles)
5059       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5060       'quit)
5061      ((eq articles 0) nil)
5062      (t
5063       ;; Init the dependencies hash table.
5064       (setq gnus-newsgroup-dependencies
5065             (gnus-make-hashtable (length articles)))
5066       (gnus-set-global-variables)
5067       ;; Retrieve the headers and read them in.
5068
5069       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5070
5071       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5072       (when cached
5073         (setq gnus-newsgroup-cached cached))
5074
5075       ;; Suppress duplicates?
5076       (when gnus-suppress-duplicates
5077         (gnus-dup-suppress-articles))
5078
5079       ;; Set the initial limit.
5080       (setq gnus-newsgroup-limit (copy-sequence articles))
5081       ;; Remove canceled articles from the list of unread articles.
5082       (setq fetched-articles
5083             (mapcar (lambda (headers) (mail-header-number headers))
5084                     gnus-newsgroup-headers))
5085       (setq gnus-newsgroup-articles fetched-articles)
5086       (setq gnus-newsgroup-unreads
5087             (gnus-sorted-nintersection
5088              gnus-newsgroup-unreads fetched-articles))
5089       (gnus-compute-unseen-list)
5090
5091       ;; Removed marked articles that do not exist.
5092       (gnus-update-missing-marks
5093        (gnus-sorted-difference articles fetched-articles))
5094       ;; We might want to build some more threads first.
5095       (when (and gnus-fetch-old-headers
5096                  (eq gnus-headers-retrieved-by 'nov))
5097         (if (eq gnus-fetch-old-headers 'invisible)
5098             (gnus-build-all-threads)
5099           (gnus-build-old-threads)))
5100       ;; Let the Gnus agent mark articles as read.
5101       (when gnus-agent
5102         (gnus-agent-get-undownloaded-list))
5103       ;; Remove list identifiers from subject
5104       (when gnus-list-identifiers
5105         (gnus-summary-remove-list-identifiers))
5106       ;; Check whether auto-expire is to be done in this group.
5107       (setq gnus-newsgroup-auto-expire
5108             (gnus-group-auto-expirable-p group))
5109       ;; Set up the article buffer now, if necessary.
5110       (unless gnus-single-article-buffer
5111         (gnus-article-setup-buffer))
5112       ;; First and last article in this newsgroup.
5113       (when gnus-newsgroup-headers
5114         (setq gnus-newsgroup-begin
5115               (mail-header-number (car gnus-newsgroup-headers))
5116               gnus-newsgroup-end
5117               (mail-header-number
5118                (gnus-last-element gnus-newsgroup-headers))))
5119       ;; GROUP is successfully selected.
5120       (or gnus-newsgroup-headers t)))))
5121
5122 (defun gnus-compute-unseen-list ()
5123   ;; The `seen' marks are treated specially.
5124   (if (not gnus-newsgroup-seen)
5125       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5126     (setq gnus-newsgroup-unseen
5127           (gnus-inverse-list-range-intersection
5128            gnus-newsgroup-articles gnus-newsgroup-seen))))
5129
5130 (defun gnus-summary-display-make-predicate (display)
5131   (require 'gnus-agent)
5132   (when (= (length display) 1)
5133     (setq display (car display)))
5134   (unless gnus-summary-display-cache
5135     (dolist (elem (append '((unread . unread)
5136                             (read . read)
5137                             (unseen . unseen))
5138                           gnus-article-mark-lists))
5139       (push (cons (cdr elem)
5140                   (gnus-byte-compile
5141                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5142             gnus-summary-display-cache)))
5143   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5144         (gnus-category-predicate-cache gnus-summary-display-cache))
5145     (gnus-get-predicate display)))
5146
5147 ;; Uses the dynamically bound `number' variable.
5148 (eval-when-compile
5149   (defvar number))
5150 (defun gnus-article-marked-p (type &optional article)
5151   (let ((article (or article number)))
5152     (cond
5153      ((eq type 'tick)
5154       (memq article gnus-newsgroup-marked))
5155      ((eq type 'spam)
5156       (memq article gnus-newsgroup-spam-marked))
5157      ((eq type 'unsend)
5158       (memq article gnus-newsgroup-unsendable))
5159      ((eq type 'undownload)
5160       (memq article gnus-newsgroup-undownloaded))
5161      ((eq type 'download)
5162       (memq article gnus-newsgroup-downloadable))
5163      ((eq type 'unread)
5164       (memq article gnus-newsgroup-unreads))
5165      ((eq type 'read)
5166       (memq article gnus-newsgroup-reads))
5167      ((eq type 'dormant)
5168       (memq article gnus-newsgroup-dormant) )
5169      ((eq type 'expire)
5170       (memq article gnus-newsgroup-expirable))
5171      ((eq type 'reply)
5172       (memq article gnus-newsgroup-replied))
5173      ((eq type 'killed)
5174       (memq article gnus-newsgroup-killed))
5175      ((eq type 'bookmark)
5176       (assq article gnus-newsgroup-bookmarks))
5177      ((eq type 'score)
5178       (assq article gnus-newsgroup-scored))
5179      ((eq type 'save)
5180       (memq article gnus-newsgroup-saved))
5181      ((eq type 'cache)
5182       (memq article gnus-newsgroup-cached))
5183      ((eq type 'forward)
5184       (memq article gnus-newsgroup-forwarded))
5185      ((eq type 'seen)
5186       (not (memq article gnus-newsgroup-unseen)))
5187      ((eq type 'recent)
5188       (memq article gnus-newsgroup-recent))
5189      (t t))))
5190
5191 (defun gnus-articles-to-read (group &optional read-all)
5192   "Find out what articles the user wants to read."
5193   (let* ((display (gnus-group-find-parameter group 'display))
5194          (articles
5195           ;; Select all articles if `read-all' is non-nil, or if there
5196           ;; are no unread articles.
5197           (if (or read-all
5198                   (and (zerop (length gnus-newsgroup-marked))
5199                        (zerop (length gnus-newsgroup-unreads)))
5200                   ;; Fetch all if the predicate is non-nil.
5201                   gnus-newsgroup-display)
5202               ;; We want to select the headers for all the articles in
5203               ;; the group, so we select either all the active
5204               ;; articles in the group, or (if that's nil), the
5205               ;; articles in the cache.
5206               (or
5207                (gnus-uncompress-range (gnus-active group))
5208                (gnus-cache-articles-in-group group))
5209             ;; Select only the "normal" subset of articles.
5210             (gnus-sorted-nunion
5211              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5212              gnus-newsgroup-unreads)))
5213          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5214          (scored (length scored-list))
5215          (number (length articles))
5216          (marked (+ (length gnus-newsgroup-marked)
5217                     (length gnus-newsgroup-dormant)))
5218          (select
5219           (cond
5220            ((numberp read-all)
5221             read-all)
5222            ((numberp gnus-newsgroup-display)
5223             gnus-newsgroup-display)
5224            (t
5225             (condition-case ()
5226                 (cond
5227                  ((and (or (<= scored marked) (= scored number))
5228                        (numberp gnus-large-newsgroup)
5229                        (> number gnus-large-newsgroup))
5230                   (let* ((cursor-in-echo-area nil)
5231                          (initial (gnus-parameter-large-newsgroup-initial
5232                                    gnus-newsgroup-name))
5233                          (input
5234                           (read-string
5235                            (format
5236                             "How many articles from %s (%s %d): "
5237                             (gnus-limit-string
5238                              (gnus-group-decoded-name gnus-newsgroup-name)
5239                              35)
5240                             (if initial "max" "default")
5241                             number)
5242                            (if initial
5243                                (cons (number-to-string initial)
5244                                      0)))))
5245                     (if (string-match "^[ \t]*$" input) number input)))
5246                  ((and (> scored marked) (< scored number)
5247                        (> (- scored number) 20))
5248                   (let ((input
5249                          (read-string
5250                           (format "%s %s (%d scored, %d total): "
5251                                   "How many articles from"
5252                                   (gnus-group-decoded-name group)
5253                                   scored number))))
5254                     (if (string-match "^[ \t]*$" input)
5255                         number input)))
5256                  (t number))
5257               (quit
5258                (message "Quit getting the articles to read")
5259                nil))))))
5260     (setq select (if (stringp select) (string-to-number select) select))
5261     (if (or (null select) (zerop select))
5262         select
5263       (if (and (not (zerop scored)) (<= (abs select) scored))
5264           (progn
5265             (setq articles (sort scored-list '<))
5266             (setq number (length articles)))
5267         (setq articles (copy-sequence articles)))
5268
5269       (when (< (abs select) number)
5270         (if (< select 0)
5271             ;; Select the N oldest articles.
5272             (setcdr (nthcdr (1- (abs select)) articles) nil)
5273           ;; Select the N most recent articles.
5274           (setq articles (nthcdr (- number select) articles))))
5275       (setq gnus-newsgroup-unselected
5276             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5277       (when gnus-alter-articles-to-read-function
5278         (setq articles
5279               (sort
5280                (funcall gnus-alter-articles-to-read-function
5281                         gnus-newsgroup-name articles)
5282                '<)))
5283       articles)))
5284
5285 (defun gnus-killed-articles (killed articles)
5286   (let (out)
5287     (while articles
5288       (when (inline (gnus-member-of-range (car articles) killed))
5289         (push (car articles) out))
5290       (setq articles (cdr articles)))
5291     out))
5292
5293 (defun gnus-uncompress-marks (marks)
5294   "Uncompress the mark ranges in MARKS."
5295   (let ((uncompressed '(score bookmark))
5296         out)
5297     (while marks
5298       (if (memq (caar marks) uncompressed)
5299           (push (car marks) out)
5300         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5301       (setq marks (cdr marks)))
5302     out))
5303
5304 (defun gnus-article-mark-to-type (mark)
5305   "Return the type of MARK."
5306   (or (cadr (assq mark gnus-article-special-mark-lists))
5307       'list))
5308
5309 (defun gnus-article-unpropagatable-p (mark)
5310   "Return whether MARK should be propagated to backend."
5311   (memq mark gnus-article-unpropagated-mark-lists))
5312
5313 (defun gnus-adjust-marked-articles (info)
5314   "Set all article lists and remove all marks that are no longer valid."
5315   (let* ((marked-lists (gnus-info-marks info))
5316          (active (gnus-active (gnus-info-group info)))
5317          (min (car active))
5318          (max (cdr active))
5319          (types gnus-article-mark-lists)
5320          marks var articles article mark mark-type)
5321
5322     (dolist (marks marked-lists)
5323       (setq mark (car marks)
5324             mark-type (gnus-article-mark-to-type mark)
5325             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5326
5327       ;; We set the variable according to the type of the marks list,
5328       ;; and then adjust the marks to a subset of the active articles.
5329       (cond
5330        ;; Adjust "simple" lists.
5331        ((eq mark-type 'list)
5332         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5333         (when (memq mark '(tick dormant expire reply save))
5334           (while articles
5335             (when (or (< (setq article (pop articles)) min) (> article max))
5336               (set var (delq article (symbol-value var)))))))
5337        ;; Adjust assocs.
5338        ((eq mark-type 'tuple)
5339         (set var (setq articles (cdr marks)))
5340         (when (not (listp (cdr (symbol-value var))))
5341           (set var (list (symbol-value var))))
5342         (when (not (listp (cdr articles)))
5343           (setq articles (list articles)))
5344         (while articles
5345           (when (or (not (consp (setq article (pop articles))))
5346                     (< (car article) min)
5347                     (> (car article) max))
5348             (set var (delq article (symbol-value var))))))
5349        ;; Adjust ranges (sloppily).
5350        ((eq mark-type 'range)
5351         (cond
5352          ((eq mark 'seen)
5353           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5354           ;; It should be (seen (NUM1 . NUM2)).
5355           (when (numberp (cddr marks))
5356             (setcdr marks (list (cdr marks))))
5357           (setq articles (cdr marks))
5358           (while (and articles
5359                       (or (and (consp (car articles))
5360                                (> min (cdar articles)))
5361                           (and (numberp (car articles))
5362                                (> min (car articles)))))
5363             (pop articles))
5364           (set var articles))))))))
5365
5366 (defun gnus-update-missing-marks (missing)
5367   "Go through the list of MISSING articles and remove them from the mark lists."
5368   (when missing
5369     (let (var m)
5370       ;; Go through all types.
5371       (dolist (elem gnus-article-mark-lists)
5372         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5373           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5374           (when (symbol-value var)
5375             ;; This list has articles.  So we delete all missing
5376             ;; articles from it.
5377             (setq m missing)
5378             (while m
5379               (set var (delq (pop m) (symbol-value var))))))))))
5380
5381 (defun gnus-update-marks ()
5382   "Enter the various lists of marked articles into the newsgroup info list."
5383   (let ((types gnus-article-mark-lists)
5384         (info (gnus-get-info gnus-newsgroup-name))
5385         type list newmarked symbol delta-marks)
5386     (when info
5387       ;; Add all marks lists to the list of marks lists.
5388       (while (setq type (pop types))
5389         (setq list (symbol-value
5390                     (setq symbol
5391                           (intern (format "gnus-newsgroup-%s" (car type))))))
5392
5393         (when list
5394           ;; Get rid of the entries of the articles that have the
5395           ;; default score.
5396           (when (and (eq (cdr type) 'score)
5397                      gnus-save-score
5398                      list)
5399             (let* ((arts list)
5400                    (prev (cons nil list))
5401                    (all prev))
5402               (while arts
5403                 (if (or (not (consp (car arts)))
5404                         (= (cdar arts) gnus-summary-default-score))
5405                     (setcdr prev (cdr arts))
5406                   (setq prev arts))
5407                 (setq arts (cdr arts)))
5408               (setq list (cdr all)))))
5409
5410         (when (eq (cdr type) 'seen)
5411           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5412
5413         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5414           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5415
5416         (when (and (gnus-check-backend-function
5417                     'request-set-mark gnus-newsgroup-name)
5418                    (not (gnus-article-unpropagatable-p (cdr type))))
5419           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5420                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5421                  (add (gnus-remove-from-range
5422                        (gnus-copy-sequence list) old)))
5423             (when add
5424               (push (list add 'add (list (cdr type))) delta-marks))
5425             (when del
5426               (push (list del 'del (list (cdr type))) delta-marks))))
5427
5428         (when list
5429           (push (cons (cdr type) list) newmarked)))
5430
5431       (when delta-marks
5432         (unless (gnus-check-group gnus-newsgroup-name)
5433           (error "Can't open server for %s" gnus-newsgroup-name))
5434         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5435
5436       ;; Enter these new marks into the info of the group.
5437       (if (nthcdr 3 info)
5438           (setcar (nthcdr 3 info) newmarked)
5439         ;; Add the marks lists to the end of the info.
5440         (when newmarked
5441           (setcdr (nthcdr 2 info) (list newmarked))))
5442
5443       ;; Cut off the end of the info if there's nothing else there.
5444       (let ((i 5))
5445         (while (and (> i 2)
5446                     (not (nth i info)))
5447           (when (nthcdr (decf i) info)
5448             (setcdr (nthcdr i info) nil)))))))
5449
5450 (defun gnus-set-mode-line (where)
5451   "Set the mode line of the article or summary buffers.
5452 If WHERE is `summary', the summary mode line format will be used."
5453   ;; Is this mode line one we keep updated?
5454   (when (and (memq where gnus-updated-mode-lines)
5455              (symbol-value
5456               (intern (format "gnus-%s-mode-line-format-spec" where))))
5457     (let (mode-string)
5458       (save-excursion
5459         ;; We evaluate this in the summary buffer since these
5460         ;; variables are buffer-local to that buffer.
5461         (set-buffer gnus-summary-buffer)
5462        ;; We bind all these variables that are used in the `eval' form
5463         ;; below.
5464         (let* ((mformat (symbol-value
5465                          (intern
5466                           (format "gnus-%s-mode-line-format-spec" where))))
5467                (gnus-tmp-group-name (gnus-group-decoded-name
5468                                      gnus-newsgroup-name))
5469                (gnus-tmp-article-number (or gnus-current-article 0))
5470                (gnus-tmp-unread gnus-newsgroup-unreads)
5471                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5472                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5473                (gnus-tmp-unread-and-unselected
5474                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5475                             (zerop gnus-tmp-unselected))
5476                        "")
5477                       ((zerop gnus-tmp-unselected)
5478                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5479                       (t (format "{%d(+%d) more}"
5480                                  gnus-tmp-unread-and-unticked
5481                                  gnus-tmp-unselected))))
5482                (gnus-tmp-subject
5483                 (if (and gnus-current-headers
5484                          (vectorp gnus-current-headers))
5485                     (gnus-mode-string-quote
5486                      (mail-header-subject gnus-current-headers))
5487                   ""))
5488                bufname-length max-len
5489                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5490           (setq mode-string (eval mformat))
5491           (setq bufname-length (if (string-match "%b" mode-string)
5492                                    (- (length
5493                                        (buffer-name
5494                                         (if (eq where 'summary)
5495                                             nil
5496                                           (get-buffer gnus-article-buffer))))
5497                                       2)
5498                                  0))
5499           (setq max-len (max 4 (if gnus-mode-non-string-length
5500                                    (- (window-width)
5501                                       gnus-mode-non-string-length
5502                                       bufname-length)
5503                                  (length mode-string))))
5504           ;; We might have to chop a bit of the string off...
5505           (when (> (length mode-string) max-len)
5506             (setq mode-string
5507                   (concat (truncate-string-to-width mode-string (- max-len 3))
5508                           "...")))
5509           ;; Pad the mode string a bit.
5510           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5511       ;; Update the mode line.
5512       (setq mode-line-buffer-identification
5513             (gnus-mode-line-buffer-identification (list mode-string)))
5514       (set-buffer-modified-p t))))
5515
5516 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5517   "Go through the HEADERS list and add all Xrefs to a hash table.
5518 The resulting hash table is returned, or nil if no Xrefs were found."
5519   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5520          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5521          (xref-hashtb (gnus-make-hashtable))
5522          start group entry number xrefs header)
5523     (while headers
5524       (setq header (pop headers))
5525       (when (and (setq xrefs (mail-header-xref header))
5526                  (not (memq (setq number (mail-header-number header))
5527                             unreads)))
5528         (setq start 0)
5529         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5530           (setq start (match-end 0))
5531           (setq group (if prefix
5532                           (concat prefix (substring xrefs (match-beginning 1)
5533                                                     (match-end 1)))
5534                         (substring xrefs (match-beginning 1) (match-end 1))))
5535           (setq number
5536                 (string-to-int (substring xrefs (match-beginning 2)
5537                                           (match-end 2))))
5538           (if (setq entry (gnus-gethash group xref-hashtb))
5539               (setcdr entry (cons number (cdr entry)))
5540             (gnus-sethash group (cons number nil) xref-hashtb)))))
5541     (and start xref-hashtb)))
5542
5543 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5544   "Look through all the headers and mark the Xrefs as read."
5545   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5546         name entry info xref-hashtb idlist method nth4)
5547     (save-excursion
5548       (set-buffer gnus-group-buffer)
5549       (when (setq xref-hashtb
5550                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5551         (mapatoms
5552          (lambda (group)
5553            (unless (string= from-newsgroup (setq name (symbol-name group)))
5554              (setq idlist (symbol-value group))
5555              ;; Dead groups are not updated.
5556              (and (prog1
5557                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5558                             info (nth 2 entry))
5559                     (when (stringp (setq nth4 (gnus-info-method info)))
5560                       (setq nth4 (gnus-server-to-method nth4))))
5561                   ;; Only do the xrefs if the group has the same
5562                   ;; select method as the group we have just read.
5563                   (or (gnus-methods-equal-p
5564                        nth4 (gnus-find-method-for-group from-newsgroup))
5565                       virtual
5566                       (equal nth4 (setq method (gnus-find-method-for-group
5567                                                 from-newsgroup)))
5568                       (and (equal (car nth4) (car method))
5569                            (equal (nth 1 nth4) (nth 1 method))))
5570                   gnus-use-cross-reference
5571                   (or (not (eq gnus-use-cross-reference t))
5572                       virtual
5573                       ;; Only do cross-references on subscribed
5574                       ;; groups, if that is what is wanted.
5575                       (<= (gnus-info-level info) gnus-level-subscribed))
5576                   (gnus-group-make-articles-read name idlist))))
5577          xref-hashtb)))))
5578
5579 (defun gnus-compute-read-articles (group articles)
5580   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5581          (info (nth 2 entry))
5582          (active (gnus-active group))
5583          ninfo)
5584     (when entry
5585       ;; First peel off all invalid article numbers.
5586       (when active
5587         (let ((ids articles)
5588               id first)
5589           (while (setq id (pop ids))
5590             (when (and first (> id (cdr active)))
5591               ;; We'll end up in this situation in one particular
5592               ;; obscure situation.  If you re-scan a group and get
5593               ;; a new article that is cross-posted to a different
5594               ;; group that has not been re-scanned, you might get
5595               ;; crossposted article that has a higher number than
5596               ;; Gnus believes possible.  So we re-activate this
5597               ;; group as well.  This might mean doing the
5598               ;; crossposting thingy will *increase* the number
5599               ;; of articles in some groups.  Tsk, tsk.
5600               (setq active (or (gnus-activate-group group) active)))
5601             (when (or (> id (cdr active))
5602                       (< id (car active)))
5603               (setq articles (delq id articles))))))
5604       ;; If the read list is nil, we init it.
5605       (if (and active
5606                (null (gnus-info-read info))
5607                (> (car active) 1))
5608           (setq ninfo (cons 1 (1- (car active))))
5609         (setq ninfo (gnus-info-read info)))
5610       ;; Then we add the read articles to the range.
5611       (gnus-add-to-range
5612        ninfo (setq articles (sort articles '<))))))
5613
5614 (defun gnus-group-make-articles-read (group articles)
5615   "Update the info of GROUP to say that ARTICLES are read."
5616   (let* ((num 0)
5617          (entry (gnus-gethash group gnus-newsrc-hashtb))
5618          (info (nth 2 entry))
5619          (active (gnus-active group))
5620          range)
5621     (when entry
5622       (setq range (gnus-compute-read-articles group articles))
5623       (save-excursion
5624         (set-buffer gnus-group-buffer)
5625         (gnus-undo-register
5626           `(progn
5627              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5628              (gnus-info-set-read ',info ',(gnus-info-read info))
5629              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5630              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5631              (gnus-group-update-group ,group t))))
5632       ;; Add the read articles to the range.
5633       (gnus-info-set-read info range)
5634       (gnus-request-set-mark group (list (list range 'add '(read))))
5635       ;; Then we have to re-compute how many unread
5636       ;; articles there are in this group.
5637       (when active
5638         (cond
5639          ((not range)
5640           (setq num (- (1+ (cdr active)) (car active))))
5641          ((not (listp (cdr range)))
5642           (setq num (- (cdr active) (- (1+ (cdr range))
5643                                        (car range)))))
5644          (t
5645           (while range
5646             (if (numberp (car range))
5647                 (setq num (1+ num))
5648               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5649             (setq range (cdr range)))
5650           (setq num (- (cdr active) num))))
5651         ;; Update the number of unread articles.
5652         (setcar entry num)
5653         ;; Update the group buffer.
5654         (unless (gnus-ephemeral-group-p group)
5655           (gnus-group-update-group group t))))))
5656
5657 (defvar gnus-newsgroup-none-id 0)
5658
5659 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5660   (let ((cur nntp-server-buffer)
5661         (dependencies
5662          (or dependencies
5663              (save-excursion (set-buffer gnus-summary-buffer)
5664                              gnus-newsgroup-dependencies)))
5665         headers id end ref
5666         (mail-parse-charset gnus-newsgroup-charset)
5667         (mail-parse-ignored-charsets
5668          (save-excursion (condition-case nil
5669                              (set-buffer gnus-summary-buffer)
5670                            (error))
5671                          gnus-newsgroup-ignored-charsets)))
5672     (save-excursion
5673       (set-buffer nntp-server-buffer)
5674       ;; Translate all TAB characters into SPACE characters.
5675       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5676       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5677       (gnus-run-hooks 'gnus-parse-headers-hook)
5678       (let ((case-fold-search t)
5679             in-reply-to header p lines chars)
5680         (goto-char (point-min))
5681         ;; Search to the beginning of the next header.  Error messages
5682         ;; do not begin with 2 or 3.
5683         (while (re-search-forward "^[23][0-9]+ " nil t)
5684           (setq id nil
5685                 ref nil)
5686           ;; This implementation of this function, with nine
5687           ;; search-forwards instead of the one re-search-forward and
5688           ;; a case (which basically was the old function) is actually
5689           ;; about twice as fast, even though it looks messier.  You
5690           ;; can't have everything, I guess.  Speed and elegance
5691           ;; doesn't always go hand in hand.
5692           (setq
5693            header
5694            (vector
5695             ;; Number.
5696             (prog1
5697                 (read cur)
5698               (end-of-line)
5699               (setq p (point))
5700               (narrow-to-region (point)
5701                                 (or (and (search-forward "\n.\n" nil t)
5702                                          (- (point) 2))
5703                                     (point))))
5704             ;; Subject.
5705             (progn
5706               (goto-char p)
5707               (if (search-forward "\nsubject:" nil t)
5708                   (funcall gnus-decode-encoded-word-function
5709                            (nnheader-header-value))
5710                 "(none)"))
5711             ;; From.
5712             (progn
5713               (goto-char p)
5714               (if (search-forward "\nfrom:" nil t)
5715                   (funcall gnus-decode-encoded-word-function
5716                            (nnheader-header-value))
5717                 "(nobody)"))
5718             ;; Date.
5719             (progn
5720               (goto-char p)
5721               (if (search-forward "\ndate:" nil t)
5722                   (nnheader-header-value) ""))
5723             ;; Message-ID.
5724             (progn
5725               (goto-char p)
5726               (setq id (if (re-search-forward
5727                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5728                            ;; We do it this way to make sure the Message-ID
5729                            ;; is (somewhat) syntactically valid.
5730                            (buffer-substring (match-beginning 1)
5731                                              (match-end 1))
5732                          ;; If there was no message-id, we just fake one
5733                          ;; to make subsequent routines simpler.
5734                          (nnheader-generate-fake-message-id))))
5735             ;; References.
5736             (progn
5737               (goto-char p)
5738               (if (search-forward "\nreferences:" nil t)
5739                   (progn
5740                     (setq end (point))
5741                     (prog1
5742                         (nnheader-header-value)
5743                       (setq ref
5744                             (buffer-substring
5745                              (progn
5746                                (end-of-line)
5747                                (search-backward ">" end t)
5748                                (1+ (point)))
5749                              (progn
5750                                (search-backward "<" end t)
5751                                (point))))))
5752                 ;; Get the references from the in-reply-to header if there
5753                 ;; were no references and the in-reply-to header looks
5754                 ;; promising.
5755                 (if (and (search-forward "\nin-reply-to:" nil t)
5756                          (setq in-reply-to (nnheader-header-value))
5757                          (string-match "<[^>]+>" in-reply-to))
5758                     (let (ref2)
5759                       (setq ref (substring in-reply-to (match-beginning 0)
5760                                            (match-end 0)))
5761                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5762                         (setq ref2 (substring in-reply-to (match-beginning 0)
5763                                               (match-end 0)))
5764                         (when (> (length ref2) (length ref))
5765                           (setq ref ref2)))
5766                       ref)
5767                   (setq ref nil))))
5768             ;; Chars.
5769             (progn
5770               (goto-char p)
5771               (if (search-forward "\nchars: " nil t)
5772                   (if (numberp (setq chars (ignore-errors (read cur))))
5773                       chars -1)
5774                 -1))
5775             ;; Lines.
5776             (progn
5777               (goto-char p)
5778               (if (search-forward "\nlines: " nil t)
5779                   (if (numberp (setq lines (ignore-errors (read cur))))
5780                       lines -1)
5781                 -1))
5782             ;; Xref.
5783             (progn
5784               (goto-char p)
5785               (and (search-forward "\nxref:" nil t)
5786                    (nnheader-header-value)))
5787             ;; Extra.
5788             (when gnus-extra-headers
5789               (let ((extra gnus-extra-headers)
5790                     out)
5791                 (while extra
5792                   (goto-char p)
5793                   (when (search-forward
5794                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5795                     (push (cons (car extra) (nnheader-header-value))
5796                           out))
5797                   (pop extra))
5798                 out))))
5799           (when (equal id ref)
5800             (setq ref nil))
5801
5802           (when gnus-alter-header-function
5803             (funcall gnus-alter-header-function header)
5804             (setq id (mail-header-id header)
5805                   ref (gnus-parent-id (mail-header-references header))))
5806
5807           (when (setq header
5808                       (gnus-dependencies-add-header
5809                        header dependencies force-new))
5810             (push header headers))
5811           (goto-char (point-max))
5812           (widen))
5813         (nreverse headers)))))
5814
5815 ;; Goes through the xover lines and returns a list of vectors
5816 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5817                                                   force-new dependencies
5818                                                   group also-fetch-heads)
5819   "Parse the news overview data in the server buffer.
5820 Return a list of headers that match SEQUENCE (see
5821 `nntp-retrieve-headers')."
5822   ;; Get the Xref when the users reads the articles since most/some
5823   ;; NNTP servers do not include Xrefs when using XOVER.
5824   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5825   (let ((mail-parse-charset gnus-newsgroup-charset)
5826         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5827         (cur nntp-server-buffer)
5828         (dependencies (or dependencies gnus-newsgroup-dependencies))
5829         (allp (cond
5830                ((eq gnus-read-all-available-headers t)
5831                 t)
5832                ((stringp gnus-read-all-available-headers)
5833                 (string-match gnus-read-all-available-headers group))
5834                (t
5835                 nil)))
5836         number headers header)
5837     (save-excursion
5838       (set-buffer nntp-server-buffer)
5839       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5840       ;; Allow the user to mangle the headers before parsing them.
5841       (gnus-run-hooks 'gnus-parse-headers-hook)
5842       (goto-char (point-min))
5843       (gnus-parse-without-error
5844         (while (and (or sequence allp)
5845                     (not (eobp)))
5846           (setq number (read cur))
5847           (when (not allp)
5848             (while (and sequence
5849                         (< (car sequence) number))
5850               (setq sequence (cdr sequence))))
5851           (when (and (or allp
5852                          (and sequence
5853                               (eq number (car sequence))))
5854                      (progn
5855                        (setq sequence (cdr sequence))
5856                        (setq header (inline
5857                                       (gnus-nov-parse-line
5858                                        number dependencies force-new)))))
5859             (push header headers))
5860           (forward-line 1)))
5861       ;; A common bug in inn is that if you have posted an article and
5862       ;; then retrieves the active file, it will answer correctly --
5863       ;; the new article is included.  However, a NOV entry for the
5864       ;; article may not have been generated yet, so this may fail.
5865       ;; We work around this problem by retrieving the last few
5866       ;; headers using HEAD.
5867       (if (or (not also-fetch-heads)
5868               (not sequence))
5869           ;; We (probably) got all the headers.
5870           (nreverse headers)
5871         (let ((gnus-nov-is-evil t))
5872           (nconc
5873            (nreverse headers)
5874            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5875              (gnus-get-newsgroup-headers))))))))
5876
5877 (defun gnus-article-get-xrefs ()
5878   "Fill in the Xref value in `gnus-current-headers', if necessary.
5879 This is meant to be called in `gnus-article-internal-prepare-hook'."
5880   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5881                                  gnus-current-headers)))
5882     (or (not gnus-use-cross-reference)
5883         (not headers)
5884         (and (mail-header-xref headers)
5885              (not (string= (mail-header-xref headers) "")))
5886         (let ((case-fold-search t)
5887               xref)
5888           (save-restriction
5889             (nnheader-narrow-to-headers)
5890             (goto-char (point-min))
5891             (when (or (and (not (eobp))
5892                            (eq (downcase (char-after)) ?x)
5893                            (looking-at "Xref:"))
5894                       (search-forward "\nXref:" nil t))
5895               (goto-char (1+ (match-end 0)))
5896               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5897               (mail-header-set-xref headers xref)))))))
5898
5899 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5900   "Find article ID and insert the summary line for that article.
5901 OLD-HEADER can either be a header or a line number to insert
5902 the subject line on."
5903   (let* ((line (and (numberp old-header) old-header))
5904          (old-header (and (vectorp old-header) old-header))
5905          (header (cond ((and old-header use-old-header)
5906                         old-header)
5907                        ((and (numberp id)
5908                              (gnus-number-to-header id))
5909                         (gnus-number-to-header id))
5910                        (t
5911                         (gnus-read-header id))))
5912          (number (and (numberp id) id))
5913          d)
5914     (when header
5915       ;; Rebuild the thread that this article is part of and go to the
5916       ;; article we have fetched.
5917       (when (and (not gnus-show-threads)
5918                  old-header)
5919         (when (and number
5920                    (setq d (gnus-data-find (mail-header-number old-header))))
5921           (goto-char (gnus-data-pos d))
5922           (gnus-data-remove
5923            number
5924            (- (gnus-point-at-bol)
5925               (prog1
5926                   (1+ (gnus-point-at-eol))
5927                 (gnus-delete-line))))))
5928       (when old-header
5929         (mail-header-set-number header (mail-header-number old-header)))
5930       (setq gnus-newsgroup-sparse
5931             (delq (setq number (mail-header-number header))
5932                   gnus-newsgroup-sparse))
5933       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5934       (push number gnus-newsgroup-limit)
5935       (gnus-rebuild-thread (mail-header-id header) line)
5936       (gnus-summary-goto-subject number nil t))
5937     (when (and (numberp number)
5938                (> number 0))
5939       ;; We have to update the boundaries even if we can't fetch the
5940       ;; article if ID is a number -- so that the next `P' or `N'
5941       ;; command will fetch the previous (or next) article even
5942       ;; if the one we tried to fetch this time has been canceled.
5943       (when (> number gnus-newsgroup-end)
5944         (setq gnus-newsgroup-end number))
5945       (when (< number gnus-newsgroup-begin)
5946         (setq gnus-newsgroup-begin number))
5947       (setq gnus-newsgroup-unselected
5948             (delq number gnus-newsgroup-unselected)))
5949     ;; Report back a success?
5950     (and header (mail-header-number header))))
5951
5952 ;;; Process/prefix in the summary buffer
5953
5954 (defun gnus-summary-work-articles (n)
5955   "Return a list of articles to be worked upon.
5956 The prefix argument, the list of process marked articles, and the
5957 current article will be taken into consideration."
5958   (save-excursion
5959     (set-buffer gnus-summary-buffer)
5960     (cond
5961      (n
5962       ;; A numerical prefix has been given.
5963       (setq n (prefix-numeric-value n))
5964       (let ((backward (< n 0))
5965             (n (abs (prefix-numeric-value n)))
5966             articles article)
5967         (save-excursion
5968           (while
5969               (and (> n 0)
5970                    (push (setq article (gnus-summary-article-number))
5971                          articles)
5972                    (if backward
5973                        (gnus-summary-find-prev nil article)
5974                      (gnus-summary-find-next nil article)))
5975             (decf n)))
5976         (nreverse articles)))
5977      ((and (gnus-region-active-p) (mark))
5978       (message "region active")
5979       ;; Work on the region between point and mark.
5980       (let ((max (max (point) (mark)))
5981             articles article)
5982         (save-excursion
5983           (goto-char (min (min (point) (mark))))
5984           (while
5985               (and
5986                (push (setq article (gnus-summary-article-number)) articles)
5987                (gnus-summary-find-next nil article)
5988                (< (point) max)))
5989           (nreverse articles))))
5990      (gnus-newsgroup-processable
5991       ;; There are process-marked articles present.
5992       ;; Save current state.
5993       (gnus-summary-save-process-mark)
5994       ;; Return the list.
5995       (reverse gnus-newsgroup-processable))
5996      (t
5997       ;; Just return the current article.
5998       (list (gnus-summary-article-number))))))
5999
6000 (defmacro gnus-summary-iterate (arg &rest forms)
6001   "Iterate over the process/prefixed articles and do FORMS.
6002 ARG is the interactive prefix given to the command.  FORMS will be
6003 executed with point over the summary line of the articles."
6004   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6005     `(let ((,articles (gnus-summary-work-articles ,arg)))
6006        (while ,articles
6007          (gnus-summary-goto-subject (car ,articles))
6008          ,@forms
6009          (pop ,articles)))))
6010
6011 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6012 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6013
6014 (defun gnus-summary-save-process-mark ()
6015   "Push the current set of process marked articles on the stack."
6016   (interactive)
6017   (push (copy-sequence gnus-newsgroup-processable)
6018         gnus-newsgroup-process-stack))
6019
6020 (defun gnus-summary-kill-process-mark ()
6021   "Push the current set of process marked articles on the stack and unmark."
6022   (interactive)
6023   (gnus-summary-save-process-mark)
6024   (gnus-summary-unmark-all-processable))
6025
6026 (defun gnus-summary-yank-process-mark ()
6027   "Pop the last process mark state off the stack and restore it."
6028   (interactive)
6029   (unless gnus-newsgroup-process-stack
6030     (error "Empty mark stack"))
6031   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6032
6033 (defun gnus-summary-process-mark-set (set)
6034   "Make SET into the current process marked articles."
6035   (gnus-summary-unmark-all-processable)
6036   (while set
6037     (gnus-summary-set-process-mark (pop set))))
6038
6039 ;;; Searching and stuff
6040
6041 (defun gnus-summary-search-group (&optional backward use-level)
6042   "Search for next unread newsgroup.
6043 If optional argument BACKWARD is non-nil, search backward instead."
6044   (save-excursion
6045     (set-buffer gnus-group-buffer)
6046     (when (gnus-group-search-forward
6047            backward nil (if use-level (gnus-group-group-level) nil))
6048       (gnus-group-group-name))))
6049
6050 (defun gnus-summary-best-group (&optional exclude-group)
6051   "Find the name of the best unread group.
6052 If EXCLUDE-GROUP, do not go to this group."
6053   (save-excursion
6054     (set-buffer gnus-group-buffer)
6055     (save-excursion
6056       (gnus-group-best-unread-group exclude-group))))
6057
6058 (defun gnus-summary-find-next (&optional unread article backward)
6059   (if backward (gnus-summary-find-prev unread article)
6060     (let* ((dummy (gnus-summary-article-intangible-p))
6061            (article (or article (gnus-summary-article-number)))
6062            (data (gnus-data-find-list article))
6063            result)
6064       (when (and (not dummy)
6065                  (or (not gnus-summary-check-current)
6066                      (not unread)
6067                      (not (gnus-data-unread-p (car data)))))
6068         (setq data (cdr data)))
6069       (when (setq result
6070                   (if unread
6071                       (progn
6072                         (while data
6073                           (unless (memq (gnus-data-number (car data)) 
6074                                         (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6075                                                gnus-newsgroup-undownloaded)
6076                                               (gnus-plugged
6077                                                nil)
6078                                               ((eq gnus-auto-goto-ignores 'unfetched)
6079                                                gnus-newsgroup-unfetched)
6080                                               ((eq gnus-auto-goto-ignores 'undownloaded)
6081                                                gnus-newsgroup-undownloaded)))
6082                             (when (gnus-data-unread-p (car data))
6083                               (setq result (car data)
6084                                     data nil)))
6085                           (setq data (cdr data)))
6086                         result)
6087                     (car data)))
6088         (goto-char (gnus-data-pos result))
6089         (gnus-data-number result)))))
6090
6091 (defun gnus-summary-find-prev (&optional unread article)
6092   (let* ((eobp (eobp))
6093          (article (or article (gnus-summary-article-number)))
6094          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6095          result)
6096     (when (and (not eobp)
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-subject (subject &optional unread backward article)
6124   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6125          (article (or article (gnus-summary-article-number)))
6126          (articles (gnus-data-list backward))
6127          (arts (gnus-data-find-list article articles))
6128          result)
6129     (when (or (not gnus-summary-check-current)
6130               (not unread)
6131               (not (gnus-data-unread-p (car arts))))
6132       (setq arts (cdr arts)))
6133     (while arts
6134       (and (or (not unread)
6135                (gnus-data-unread-p (car arts)))
6136            (vectorp (gnus-data-header (car arts)))
6137            (gnus-subject-equal
6138             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6139            (setq result (car arts)
6140                  arts nil))
6141       (setq arts (cdr arts)))
6142     (and result
6143          (goto-char (gnus-data-pos result))
6144          (gnus-data-number result))))
6145
6146 (defun gnus-summary-search-forward (&optional unread subject backward)
6147   "Search forward for an article.
6148 If UNREAD, look for unread articles.  If SUBJECT, look for
6149 articles with that subject.  If BACKWARD, search backward instead."
6150   (cond (subject (gnus-summary-find-subject subject unread backward))
6151         (backward (gnus-summary-find-prev unread))
6152         (t (gnus-summary-find-next unread))))
6153
6154 (defun gnus-recenter (&optional n)
6155   "Center point in window and redisplay frame.
6156 Also do horizontal recentering."
6157   (interactive "P")
6158   (when (and gnus-auto-center-summary
6159              (not (eq gnus-auto-center-summary 'vertical)))
6160     (gnus-horizontal-recenter))
6161   (recenter n))
6162
6163 (defun gnus-summary-recenter ()
6164   "Center point in the summary window.
6165 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6166 displayed, no centering will be performed."
6167   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6168   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6169   (interactive)
6170   ;; The user has to want it.
6171   (when gnus-auto-center-summary
6172     (let* ((top (cond ((< (window-height) 4) 0)
6173                       ((< (window-height) 7) 1)
6174                       (t (if (numberp gnus-auto-center-summary)
6175                              gnus-auto-center-summary
6176                            2))))
6177            (height (1- (window-height)))
6178            (bottom (save-excursion (goto-char (point-max))
6179                                    (forward-line (- height))
6180                                    (point)))
6181            (window (get-buffer-window (current-buffer))))
6182       (when (get-buffer-window gnus-article-buffer)
6183         ;; Only do recentering when the article buffer is displayed,
6184         ;; Set the window start to either `bottom', which is the biggest
6185         ;; possible valid number, or the second line from the top,
6186         ;; whichever is the least.
6187         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6188           (if (> bottom top-pos)
6189               ;; Keep the second line from the top visible
6190               (set-window-start window top-pos t)
6191             ;; Try to keep the bottom line visible; if it's partially
6192             ;; obscured, either scroll one more line to make it fully
6193             ;; visible, or revert to using TOP-POS.
6194             (save-excursion
6195               (goto-char (point-max))
6196               (forward-line -1)
6197               (let ((last-line-start (point)))
6198                 (goto-char bottom)
6199                 (set-window-start window (point) t)
6200                 (when (not (pos-visible-in-window-p last-line-start window))
6201                   (forward-line 1)
6202                   (set-window-start window (min (point) top-pos) t)))))))
6203       ;; Do horizontal recentering while we're at it.
6204       (when (and (get-buffer-window (current-buffer) t)
6205                  (not (eq gnus-auto-center-summary 'vertical)))
6206         (let ((selected (selected-window)))
6207           (select-window (get-buffer-window (current-buffer) t))
6208           (gnus-summary-position-point)
6209           (gnus-horizontal-recenter)
6210           (select-window selected))))))
6211
6212 (defun gnus-summary-jump-to-group (newsgroup)
6213   "Move point to NEWSGROUP in group mode buffer."
6214   ;; Keep update point of group mode buffer if visible.
6215   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6216       (save-window-excursion
6217         ;; Take care of tree window mode.
6218         (when (get-buffer-window gnus-group-buffer)
6219           (pop-to-buffer gnus-group-buffer))
6220         (gnus-group-jump-to-group newsgroup))
6221     (save-excursion
6222       ;; Take care of tree window mode.
6223       (if (get-buffer-window gnus-group-buffer)
6224           (pop-to-buffer gnus-group-buffer)
6225         (set-buffer gnus-group-buffer))
6226       (gnus-group-jump-to-group newsgroup))))
6227
6228 ;; This function returns a list of article numbers based on the
6229 ;; difference between the ranges of read articles in this group and
6230 ;; the range of active articles.
6231 (defun gnus-list-of-unread-articles (group)
6232   (let* ((read (gnus-info-read (gnus-get-info group)))
6233          (active (or (gnus-active group) (gnus-activate-group group)))
6234          (last (cdr active))
6235          first nlast unread)
6236     ;; If none are read, then all are unread.
6237     (if (not read)
6238         (setq first (car active))
6239       ;; If the range of read articles is a single range, then the
6240       ;; first unread article is the article after the last read
6241       ;; article.  Sounds logical, doesn't it?
6242       (if (and (not (listp (cdr read)))
6243                (or (< (car read) (car active))
6244                    (progn (setq read (list read))
6245                           nil)))
6246           (setq first (max (car active) (1+ (cdr read))))
6247         ;; `read' is a list of ranges.
6248         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6249                                   (caar read)))
6250                   1)
6251           (setq first (car active)))
6252         (while read
6253           (when first
6254             (while (< first nlast)
6255               (push first unread)
6256               (setq first (1+ first))))
6257           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6258           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6259           (setq read (cdr read)))))
6260     ;; And add the last unread articles.
6261     (while (<= first last)
6262       (push first unread)
6263       (setq first (1+ first)))
6264     ;; Return the list of unread articles.
6265     (delq 0 (nreverse unread))))
6266
6267 (defun gnus-list-of-read-articles (group)
6268   "Return a list of unread, unticked and non-dormant articles."
6269   (let* ((info (gnus-get-info group))
6270          (marked (gnus-info-marks info))
6271          (active (gnus-active group)))
6272     (and info active
6273          (gnus-list-range-difference
6274           (gnus-list-range-difference
6275            (gnus-sorted-complement
6276             (gnus-uncompress-range active)
6277             (gnus-list-of-unread-articles group))
6278            (cdr (assq 'dormant marked)))
6279           (cdr (assq 'tick marked))))))
6280
6281 ;; Various summary commands
6282
6283 (defun gnus-summary-select-article-buffer ()
6284   "Reconfigure windows to show article buffer."
6285   (interactive)
6286   (if (not (gnus-buffer-live-p gnus-article-buffer))
6287       (error "There is no article buffer for this summary buffer")
6288     (gnus-configure-windows 'article)
6289     (select-window (get-buffer-window gnus-article-buffer))))
6290
6291 (defun gnus-summary-universal-argument (arg)
6292   "Perform any operation on all articles that are process/prefixed."
6293   (interactive "P")
6294   (let ((articles (gnus-summary-work-articles arg))
6295         func article)
6296     (if (eq
6297          (setq
6298           func
6299           (key-binding
6300            (read-key-sequence
6301             (substitute-command-keys
6302              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6303          'undefined)
6304         (gnus-error 1 "Undefined key")
6305       (save-excursion
6306         (while articles
6307           (gnus-summary-goto-subject (setq article (pop articles)))
6308           (let (gnus-newsgroup-processable)
6309             (command-execute func))
6310           (gnus-summary-remove-process-mark article)))))
6311   (gnus-summary-position-point))
6312
6313 (defun gnus-summary-toggle-truncation (&optional arg)
6314   "Toggle truncation of summary lines.
6315 With arg, turn line truncation on if arg is positive."
6316   (interactive "P")
6317   (setq truncate-lines
6318         (if (null arg) (not truncate-lines)
6319           (> (prefix-numeric-value arg) 0)))
6320   (redraw-display))
6321
6322 (defun gnus-summary-find-for-reselect ()
6323   "Return the number of an article to stay on across a reselect.
6324 The current article is considered, then following articles, then previous
6325 articles.  An article is sought which is not cancelled and isn't a temporary
6326 insertion from another group.  If there's no such then return a dummy 0."
6327   (let (found)
6328     (dolist (rev '(nil t))
6329       (unless found      ; don't demand the reverse list if we don't need it
6330         (let ((data (gnus-data-find-list
6331                      (gnus-summary-article-number) (gnus-data-list rev))))
6332           (while (and data (not found))
6333             (if (and (< 0 (gnus-data-number (car data)))
6334                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6335                 (setq found (gnus-data-number (car data))))
6336             (setq data (cdr data))))))
6337     (or found 0)))
6338
6339 (defun gnus-summary-reselect-current-group (&optional all rescan)
6340   "Exit and then reselect the current newsgroup.
6341 The prefix argument ALL means to select all articles."
6342   (interactive "P")
6343   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6344     (error "Ephemeral groups can't be reselected"))
6345   (let ((current-subject (gnus-summary-find-for-reselect))
6346         (group gnus-newsgroup-name))
6347     (setq gnus-newsgroup-begin nil)
6348     (gnus-summary-exit)
6349     ;; We have to adjust the point of group mode buffer because
6350     ;; point was moved to the next unread newsgroup by exiting.
6351     (gnus-summary-jump-to-group group)
6352     (when rescan
6353       (save-excursion
6354         (gnus-group-get-new-news-this-group 1)))
6355     (gnus-group-read-group all t)
6356     (gnus-summary-goto-subject current-subject nil t)))
6357
6358 (defun gnus-summary-rescan-group (&optional all)
6359   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6360   (interactive "P")
6361   (gnus-summary-reselect-current-group all t))
6362
6363 (defun gnus-summary-update-info (&optional non-destructive)
6364   (save-excursion
6365     (let ((group gnus-newsgroup-name))
6366       (when group
6367         (when gnus-newsgroup-kill-headers
6368           (setq gnus-newsgroup-killed
6369                 (gnus-compress-sequence
6370                  (gnus-sorted-union
6371                   (gnus-list-range-intersection
6372                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6373                   gnus-newsgroup-unreads)
6374                  t)))
6375         (unless (listp (cdr gnus-newsgroup-killed))
6376           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6377         (let ((headers gnus-newsgroup-headers))
6378           ;; Set the new ranges of read articles.
6379           (save-excursion
6380             (set-buffer gnus-group-buffer)
6381             (gnus-undo-force-boundary))
6382           (gnus-update-read-articles
6383            group (gnus-sorted-union
6384                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6385           ;; Set the current article marks.
6386           (let ((gnus-newsgroup-scored
6387                  (if (and (not gnus-save-score)
6388                           (not non-destructive))
6389                      nil
6390                    gnus-newsgroup-scored)))
6391             (save-excursion
6392               (gnus-update-marks)))
6393           ;; Do the cross-ref thing.
6394           (when gnus-use-cross-reference
6395             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6396           ;; Do not switch windows but change the buffer to work.
6397           (set-buffer gnus-group-buffer)
6398           (unless (gnus-ephemeral-group-p group)
6399             (gnus-group-update-group group)))))))
6400
6401 (defun gnus-summary-save-newsrc (&optional force)
6402   "Save the current number of read/marked articles in the dribble buffer.
6403 The dribble buffer will then be saved.
6404 If FORCE (the prefix), also save the .newsrc file(s)."
6405   (interactive "P")
6406   (gnus-summary-update-info t)
6407   (if force
6408       (gnus-save-newsrc-file)
6409     (gnus-dribble-save)))
6410
6411 (defun gnus-summary-exit (&optional temporary)
6412   "Exit reading current newsgroup, and then return to group selection mode.
6413 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6414   (interactive)
6415   (gnus-set-global-variables)
6416   (when (gnus-buffer-live-p gnus-article-buffer)
6417     (save-excursion
6418       (set-buffer gnus-article-buffer)
6419       (mm-destroy-parts gnus-article-mime-handles)
6420       ;; Set it to nil for safety reason.
6421       (setq gnus-article-mime-handle-alist nil)
6422       (setq gnus-article-mime-handles nil)))
6423   (gnus-kill-save-kill-buffer)
6424   (gnus-async-halt-prefetch)
6425   (let* ((group gnus-newsgroup-name)
6426          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6427          (gnus-group-is-exiting-p t)
6428          (mode major-mode)
6429          (group-point nil)
6430          (buf (current-buffer)))
6431     (unless quit-config
6432       ;; Do adaptive scoring, and possibly save score files.
6433       (when gnus-newsgroup-adaptive
6434         (gnus-score-adaptive))
6435       (when gnus-use-scoring
6436         (gnus-score-save)))
6437     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6438     ;; If we have several article buffers, we kill them at exit.
6439     (unless gnus-single-article-buffer
6440       (gnus-kill-buffer gnus-original-article-buffer)
6441       (setq gnus-article-current nil))
6442     (when gnus-use-cache
6443       (gnus-cache-possibly-remove-articles)
6444       (gnus-cache-save-buffers))
6445     (gnus-async-prefetch-remove-group group)
6446     (when gnus-suppress-duplicates
6447       (gnus-dup-enter-articles))
6448     (when gnus-use-trees
6449       (gnus-tree-close group))
6450     (when gnus-use-cache
6451       (gnus-cache-write-active))
6452     ;; Remove entries for this group.
6453     (nnmail-purge-split-history (gnus-group-real-name group))
6454     ;; Make all changes in this group permanent.
6455     (unless quit-config
6456       (gnus-run-hooks 'gnus-exit-group-hook)
6457       (gnus-summary-update-info))
6458     (gnus-close-group group)
6459     ;; Make sure where we were, and go to next newsgroup.
6460     (set-buffer gnus-group-buffer)
6461     (unless quit-config
6462       (gnus-group-jump-to-group group))
6463     (gnus-run-hooks 'gnus-summary-exit-hook)
6464     (unless (or quit-config
6465                 ;; If this group has disappeared from the summary
6466                 ;; buffer, don't skip forwards.
6467                 (not (string= group (gnus-group-group-name))))
6468       (gnus-group-next-unread-group 1))
6469     (setq group-point (point))
6470     (if temporary
6471         nil                             ;Nothing to do.
6472       ;; If we have several article buffers, we kill them at exit.
6473       (unless gnus-single-article-buffer
6474         (gnus-kill-buffer gnus-article-buffer)
6475         (gnus-kill-buffer gnus-original-article-buffer)
6476         (setq gnus-article-current nil))
6477       (set-buffer buf)
6478       (if (not gnus-kill-summary-on-exit)
6479           (progn
6480             (gnus-deaden-summary)
6481             (setq mode nil))
6482         ;; We set all buffer-local variables to nil.  It is unclear why
6483         ;; this is needed, but if we don't, buffer-local variables are
6484         ;; not garbage-collected, it seems.  This would the lead to en
6485         ;; ever-growing Emacs.
6486         (gnus-summary-clear-local-variables)
6487         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6488           (gnus-summary-clear-local-variables))
6489         (when (get-buffer gnus-article-buffer)
6490           (bury-buffer gnus-article-buffer))
6491         ;; We clear the global counterparts of the buffer-local
6492         ;; variables as well, just to be on the safe side.
6493         (set-buffer gnus-group-buffer)
6494         (gnus-summary-clear-local-variables)
6495         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6496           (gnus-summary-clear-local-variables))
6497         ;; Return to group mode buffer.
6498         (when (eq mode 'gnus-summary-mode)
6499           (gnus-kill-buffer buf)))
6500       (setq gnus-current-select-method gnus-select-method)
6501       (pop-to-buffer gnus-group-buffer)
6502       (if (not quit-config)
6503           (progn
6504             (goto-char group-point)
6505             (gnus-configure-windows 'group 'force))
6506         (gnus-handle-ephemeral-exit quit-config))
6507       ;; Clear the current group name.
6508       (unless quit-config
6509         (setq gnus-newsgroup-name nil)))))
6510
6511 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6512 (defun gnus-summary-exit-no-update (&optional no-questions)
6513   "Quit reading current newsgroup without updating read article info."
6514   (interactive)
6515   (let* ((group gnus-newsgroup-name)
6516          (gnus-group-is-exiting-p t)
6517          (gnus-group-is-exiting-without-update-p t)
6518          (quit-config (gnus-group-quit-config group)))
6519     (when (or no-questions
6520               gnus-expert-user
6521               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6522       (gnus-async-halt-prefetch)
6523       (run-hooks 'gnus-summary-prepare-exit-hook)
6524       (when (gnus-buffer-live-p gnus-article-buffer)
6525         (save-excursion
6526           (set-buffer gnus-article-buffer)
6527           (mm-destroy-parts gnus-article-mime-handles)
6528           ;; Set it to nil for safety reason.
6529           (setq gnus-article-mime-handle-alist nil)
6530           (setq gnus-article-mime-handles nil)))
6531       ;; If we have several article buffers, we kill them at exit.
6532       (unless gnus-single-article-buffer
6533         (gnus-kill-buffer gnus-article-buffer)
6534         (gnus-kill-buffer gnus-original-article-buffer)
6535         (setq gnus-article-current nil))
6536       (if (not gnus-kill-summary-on-exit)
6537           (gnus-deaden-summary)
6538         (gnus-close-group group)
6539         (gnus-summary-clear-local-variables)
6540         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6541           (gnus-summary-clear-local-variables))
6542         (set-buffer gnus-group-buffer)
6543         (gnus-summary-clear-local-variables)
6544         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6545           (gnus-summary-clear-local-variables))
6546         (gnus-kill-buffer gnus-summary-buffer))
6547       (unless gnus-single-article-buffer
6548         (setq gnus-article-current nil))
6549       (when gnus-use-trees
6550         (gnus-tree-close group))
6551       (gnus-async-prefetch-remove-group group)
6552       (when (get-buffer gnus-article-buffer)
6553         (bury-buffer gnus-article-buffer))
6554       ;; Return to the group buffer.
6555       (gnus-configure-windows 'group 'force)
6556       ;; Clear the current group name.
6557       (setq gnus-newsgroup-name nil)
6558       (unless (gnus-ephemeral-group-p group)
6559         (gnus-group-update-group group))
6560       (when (equal (gnus-group-group-name) group)
6561         (gnus-group-next-unread-group 1))
6562       (when quit-config
6563         (gnus-handle-ephemeral-exit quit-config)))))
6564
6565 (defun gnus-handle-ephemeral-exit (quit-config)
6566   "Handle movement when leaving an ephemeral group.
6567 The state which existed when entering the ephemeral is reset."
6568   (if (not (buffer-name (car quit-config)))
6569       (gnus-configure-windows 'group 'force)
6570     (set-buffer (car quit-config))
6571     (cond ((eq major-mode 'gnus-summary-mode)
6572            (gnus-set-global-variables))
6573           ((eq major-mode 'gnus-article-mode)
6574            (save-excursion
6575              ;; The `gnus-summary-buffer' variable may point
6576              ;; to the old summary buffer when using a single
6577              ;; article buffer.
6578              (unless (gnus-buffer-live-p gnus-summary-buffer)
6579                (set-buffer gnus-group-buffer))
6580              (set-buffer gnus-summary-buffer)
6581              (gnus-set-global-variables))))
6582     (if (or (eq (cdr quit-config) 'article)
6583             (eq (cdr quit-config) 'pick))
6584         (progn
6585           ;; The current article may be from the ephemeral group
6586           ;; thus it is best that we reload this article
6587           (gnus-summary-show-article)
6588           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6589               (gnus-configure-windows 'pick 'force)
6590             (gnus-configure-windows (cdr quit-config) 'force)))
6591       (gnus-configure-windows (cdr quit-config) 'force))
6592     (when (eq major-mode 'gnus-summary-mode)
6593       (gnus-summary-next-subject 1 nil t)
6594       (gnus-summary-recenter)
6595       (gnus-summary-position-point))))
6596
6597 ;;; Dead summaries.
6598
6599 (defvar gnus-dead-summary-mode-map nil)
6600
6601 (unless gnus-dead-summary-mode-map
6602   (setq gnus-dead-summary-mode-map (make-keymap))
6603   (suppress-keymap gnus-dead-summary-mode-map)
6604   (substitute-key-definition
6605    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6606   (dolist (key '("\C-d" "\r" "\177" [delete]))
6607     (define-key gnus-dead-summary-mode-map
6608       key 'gnus-summary-wake-up-the-dead))
6609   (dolist (key '("q" "Q"))
6610     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6611
6612 (defvar gnus-dead-summary-mode nil
6613   "Minor mode for Gnus summary buffers.")
6614
6615 (defun gnus-dead-summary-mode (&optional arg)
6616   "Minor mode for Gnus summary buffers."
6617   (interactive "P")
6618   (when (eq major-mode 'gnus-summary-mode)
6619     (make-local-variable 'gnus-dead-summary-mode)
6620     (setq gnus-dead-summary-mode
6621           (if (null arg) (not gnus-dead-summary-mode)
6622             (> (prefix-numeric-value arg) 0)))
6623     (when gnus-dead-summary-mode
6624       (gnus-add-minor-mode
6625        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6626
6627 (defun gnus-deaden-summary ()
6628   "Make the current summary buffer into a dead summary buffer."
6629   ;; Kill any previous dead summary buffer.
6630   (when (and gnus-dead-summary
6631              (buffer-name gnus-dead-summary))
6632     (save-excursion
6633       (set-buffer gnus-dead-summary)
6634       (when gnus-dead-summary-mode
6635         (kill-buffer (current-buffer)))))
6636   ;; Make this the current dead summary.
6637   (setq gnus-dead-summary (current-buffer))
6638   (gnus-dead-summary-mode 1)
6639   (let ((name (buffer-name)))
6640     (when (string-match "Summary" name)
6641       (rename-buffer
6642        (concat (substring name 0 (match-beginning 0)) "Dead "
6643                (substring name (match-beginning 0)))
6644        t)
6645       (bury-buffer))))
6646
6647 (defun gnus-kill-or-deaden-summary (buffer)
6648   "Kill or deaden the summary BUFFER."
6649   (save-excursion
6650     (when (and (buffer-name buffer)
6651                (not gnus-single-article-buffer))
6652       (save-excursion
6653         (set-buffer buffer)
6654         (gnus-kill-buffer gnus-article-buffer)
6655         (gnus-kill-buffer gnus-original-article-buffer)))
6656     (cond
6657      ;; Kill the buffer.
6658      (gnus-kill-summary-on-exit
6659       (when (and gnus-use-trees
6660                  (gnus-buffer-exists-p buffer))
6661         (save-excursion
6662           (set-buffer buffer)
6663           (gnus-tree-close gnus-newsgroup-name)))
6664       (gnus-kill-buffer buffer))
6665      ;; Deaden the buffer.
6666      ((gnus-buffer-exists-p buffer)
6667       (save-excursion
6668         (set-buffer buffer)
6669         (gnus-deaden-summary))))))
6670
6671 (defun gnus-summary-wake-up-the-dead (&rest args)
6672   "Wake up the dead summary buffer."
6673   (interactive)
6674   (gnus-dead-summary-mode -1)
6675   (let ((name (buffer-name)))
6676     (when (string-match "Dead " name)
6677       (rename-buffer
6678        (concat (substring name 0 (match-beginning 0))
6679                (substring name (match-end 0)))
6680        t)))
6681   (gnus-message 3 "This dead summary is now alive again"))
6682
6683 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6684 (defun gnus-summary-fetch-faq (&optional faq-dir)
6685   "Fetch the FAQ for the current group.
6686 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6687 in."
6688   (interactive
6689    (list
6690     (when current-prefix-arg
6691       (completing-read
6692        "FAQ dir: " (and (listp gnus-group-faq-directory)
6693                         (mapcar (lambda (file) (list file))
6694                                 gnus-group-faq-directory))))))
6695   (let (gnus-faq-buffer)
6696     (when (setq gnus-faq-buffer
6697                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6698       (gnus-configure-windows 'summary-faq))))
6699
6700 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6701 (defun gnus-summary-describe-group (&optional force)
6702   "Describe the current newsgroup."
6703   (interactive "P")
6704   (gnus-group-describe-group force gnus-newsgroup-name))
6705
6706 (defun gnus-summary-describe-briefly ()
6707   "Describe summary mode commands briefly."
6708   (interactive)
6709   (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")))
6710
6711 ;; Walking around group mode buffer from summary mode.
6712
6713 (defun gnus-summary-next-group (&optional no-article target-group backward)
6714   "Exit current newsgroup and then select next unread newsgroup.
6715 If prefix argument NO-ARTICLE is non-nil, no article is selected
6716 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6717 previous group instead."
6718   (interactive "P")
6719   ;; Stop pre-fetching.
6720   (gnus-async-halt-prefetch)
6721   (let ((current-group gnus-newsgroup-name)
6722         (current-buffer (current-buffer))
6723         entered)
6724     ;; First we semi-exit this group to update Xrefs and all variables.
6725     ;; We can't do a real exit, because the window conf must remain
6726     ;; the same in case the user is prompted for info, and we don't
6727     ;; want the window conf to change before that...
6728     (gnus-summary-exit t)
6729     (while (not entered)
6730       ;; Then we find what group we are supposed to enter.
6731       (set-buffer gnus-group-buffer)
6732       (gnus-group-jump-to-group current-group)
6733       (setq target-group
6734             (or target-group
6735                 (if (eq gnus-keep-same-level 'best)
6736                     (gnus-summary-best-group gnus-newsgroup-name)
6737                   (gnus-summary-search-group backward gnus-keep-same-level))))
6738       (if (not target-group)
6739           ;; There are no further groups, so we return to the group
6740           ;; buffer.
6741           (progn
6742             (gnus-message 5 "Returning to the group buffer")
6743             (setq entered t)
6744             (when (gnus-buffer-live-p current-buffer)
6745               (set-buffer current-buffer)
6746               (gnus-summary-exit))
6747             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6748         ;; We try to enter the target group.
6749         (gnus-group-jump-to-group target-group)
6750         (let ((unreads (gnus-group-group-unread)))
6751           (if (and (or (eq t unreads)
6752                        (and unreads (not (zerop unreads))))
6753                    (gnus-summary-read-group
6754                     target-group nil no-article
6755                     (and (buffer-name current-buffer) current-buffer)
6756                     nil backward))
6757               (setq entered t)
6758             (setq current-group target-group
6759                   target-group nil)))))))
6760
6761 (defun gnus-summary-prev-group (&optional no-article)
6762   "Exit current newsgroup and then select previous unread newsgroup.
6763 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6764   (interactive "P")
6765   (gnus-summary-next-group no-article nil t))
6766
6767 ;; Walking around summary lines.
6768
6769 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6770   "Go to the first subject satisfying any non-nil constraint.
6771 If UNREAD is non-nil, the article should be unread.
6772 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6773 If UNSEED is non-nil, the article should be unseen.
6774 Returns the article selected or nil if there are no matching articles."
6775   (interactive "P")
6776   (cond
6777    ;; Empty summary.
6778    ((null gnus-newsgroup-data)
6779     (gnus-message 3 "No articles in the group")
6780     nil)
6781    ;; Pick the first article.
6782    ((not (or unread undownloaded unseen))
6783     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6784     (gnus-data-number (car gnus-newsgroup-data)))
6785    ;; Find the first unread article.
6786    (t
6787     (let ((data gnus-newsgroup-data))
6788       (while (and data
6789                   (let ((num (gnus-data-number (car data))))
6790                     (or (memq num gnus-newsgroup-unfetched)
6791                         (not (or (and unread
6792                                       (memq num gnus-newsgroup-unreads))
6793                                  (and undownloaded
6794                                       (memq num gnus-newsgroup-undownloaded))
6795                                  (and unseen
6796                                       (memq num gnus-newsgroup-unseen)))))))
6797         (setq data (cdr data)))
6798       (prog1 
6799           (if data
6800               (progn
6801                 (goto-char (gnus-data-pos (car data)))
6802                 (gnus-data-number (car data)))
6803             (gnus-message 3 "No more%s articles"
6804                           (let* ((r (when unread " unread"))
6805                                  (d (when undownloaded " undownloaded"))
6806                                  (s (when unseen " unseen"))
6807                                  (l (delq nil (list r d s))))
6808                             (cond ((= 3 (length l))
6809                                    (concat r "," d ", or" s))
6810                                   ((= 2 (length l))
6811                                    (concat (car l) ", or" (cadr l)))
6812                                   ((= 1 (length l))
6813                                    (car l))
6814                                   (t
6815                                    ""))))
6816             nil
6817             )
6818         (gnus-summary-position-point))))))
6819
6820 (defun gnus-summary-next-subject (n &optional unread dont-display)
6821   "Go to next N'th summary line.
6822 If N is negative, go to the previous N'th subject line.
6823 If UNREAD is non-nil, only unread articles are selected.
6824 The difference between N and the actual number of steps taken is
6825 returned."
6826   (interactive "p")
6827   (let ((backward (< n 0))
6828         (n (abs n)))
6829     (while (and (> n 0)
6830                 (if backward
6831                     (gnus-summary-find-prev unread)
6832                   (gnus-summary-find-next unread)))
6833       (unless (zerop (setq n (1- n)))
6834         (gnus-summary-show-thread)))
6835     (when (/= 0 n)
6836       (gnus-message 7 "No more%s articles"
6837                     (if unread " unread" "")))
6838     (unless dont-display
6839       (gnus-summary-recenter)
6840       (gnus-summary-position-point))
6841     n))
6842
6843 (defun gnus-summary-next-unread-subject (n)
6844   "Go to next N'th unread summary line."
6845   (interactive "p")
6846   (gnus-summary-next-subject n t))
6847
6848 (defun gnus-summary-prev-subject (n &optional unread)
6849   "Go to previous N'th summary line.
6850 If optional argument UNREAD is non-nil, only unread article is selected."
6851   (interactive "p")
6852   (gnus-summary-next-subject (- n) unread))
6853
6854 (defun gnus-summary-prev-unread-subject (n)
6855   "Go to previous N'th unread summary line."
6856   (interactive "p")
6857   (gnus-summary-next-subject (- n) t))
6858
6859 (defun gnus-summary-goto-subjects (articles)
6860   "Insert the subject header for ARTICLES in the current buffer."
6861   (save-excursion
6862     (dolist (article articles)
6863       (gnus-summary-goto-subject article t)))
6864   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6865   (gnus-summary-position-point))
6866  
6867 (defun gnus-summary-goto-subject (article &optional force silent)
6868   "Go the subject line of ARTICLE.
6869 If FORCE, also allow jumping to articles not currently shown."
6870   (interactive "nArticle number: ")
6871   (unless (numberp article)
6872     (error "Article %s is not a number" article))
6873   (let ((b (point))
6874         (data (gnus-data-find article)))
6875     ;; We read in the article if we have to.
6876     (and (not data)
6877          force
6878          (gnus-summary-insert-subject
6879           article
6880           (if (or (numberp force) (vectorp force)) force)
6881           t)
6882          (setq data (gnus-data-find article)))
6883     (goto-char b)
6884     (if (not data)
6885         (progn
6886           (unless silent
6887             (gnus-message 3 "Can't find article %d" article))
6888           nil)
6889       (let ((pt (gnus-data-pos data)))
6890         (goto-char pt)
6891         (gnus-summary-set-article-display-arrow pt))
6892       (gnus-summary-position-point)
6893       article)))
6894
6895 ;; Walking around summary lines with displaying articles.
6896
6897 (defun gnus-summary-expand-window (&optional arg)
6898   "Make the summary buffer take up the entire Emacs frame.
6899 Given a prefix, will force an `article' buffer configuration."
6900   (interactive "P")
6901   (if arg
6902       (gnus-configure-windows 'article 'force)
6903     (gnus-configure-windows 'summary 'force)))
6904
6905 (defun gnus-summary-display-article (article &optional all-header)
6906   "Display ARTICLE in article buffer."
6907   (when (gnus-buffer-live-p gnus-article-buffer)
6908     (with-current-buffer gnus-article-buffer
6909       (mm-enable-multibyte)))
6910   (gnus-set-global-variables)
6911   (when (gnus-buffer-live-p gnus-article-buffer)
6912     (with-current-buffer gnus-article-buffer
6913       (setq gnus-article-charset gnus-newsgroup-charset)
6914       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6915       (mm-enable-multibyte)))
6916   (if (null article)
6917       nil
6918     (prog1
6919         (if gnus-summary-display-article-function
6920             (funcall gnus-summary-display-article-function article all-header)
6921           (gnus-article-prepare article all-header))
6922       (gnus-run-hooks 'gnus-select-article-hook)
6923       (when (and gnus-current-article
6924                  (not (zerop gnus-current-article)))
6925         (gnus-summary-goto-subject gnus-current-article))
6926       (gnus-summary-recenter)
6927       (when (and gnus-use-trees gnus-show-threads)
6928         (gnus-possibly-generate-tree article)
6929         (gnus-highlight-selected-tree article))
6930       ;; Successfully display article.
6931       (gnus-article-set-window-start
6932        (cdr (assq article gnus-newsgroup-bookmarks))))))
6933
6934 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6935   "Select the current article.
6936 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6937 non-nil, the article will be re-fetched even if it already present in
6938 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6939 be displayed."
6940   ;; Make sure we are in the summary buffer to work around bbdb bug.
6941   (unless (eq major-mode 'gnus-summary-mode)
6942     (set-buffer gnus-summary-buffer))
6943   (let ((article (or article (gnus-summary-article-number)))
6944         (all-headers (not (not all-headers))) ;Must be t or nil.
6945         gnus-summary-display-article-function)
6946     (and (not pseudo)
6947          (gnus-summary-article-pseudo-p article)
6948          (error "This is a pseudo-article"))
6949     (save-excursion
6950       (set-buffer gnus-summary-buffer)
6951       (if (or (and gnus-single-article-buffer
6952                    (or (null gnus-current-article)
6953                        (null gnus-article-current)
6954                        (null (get-buffer gnus-article-buffer))
6955                        (not (eq article (cdr gnus-article-current)))
6956                        (not (equal (car gnus-article-current)
6957                                    gnus-newsgroup-name))))
6958               (and (not gnus-single-article-buffer)
6959                    (or (null gnus-current-article)
6960                        (not (eq gnus-current-article article))))
6961               force)
6962           ;; The requested article is different from the current article.
6963           (progn
6964             (gnus-summary-display-article article all-headers)
6965             (when (gnus-buffer-live-p gnus-article-buffer)
6966               (with-current-buffer gnus-article-buffer
6967                 (if (not gnus-article-decoded-p) ;; a local variable
6968                     (mm-disable-multibyte))))
6969             (gnus-article-set-window-start
6970              (cdr (assq article gnus-newsgroup-bookmarks)))
6971             article)
6972         'old))))
6973
6974 (defun gnus-summary-force-verify-and-decrypt ()
6975   "Display buttons for signed/encrypted parts and verify/decrypt them."
6976   (interactive)
6977   (let ((mm-verify-option 'known)
6978         (mm-decrypt-option 'known)
6979         (gnus-buttonized-mime-types (append (list "multipart/signed"
6980                                                   "multipart/encrypted")
6981                                             gnus-buttonized-mime-types)))
6982     (gnus-summary-select-article nil 'force)))
6983
6984 (defun gnus-summary-set-current-mark (&optional current-mark)
6985   "Obsolete function."
6986   nil)
6987
6988 (defun gnus-summary-next-article (&optional unread subject backward push)
6989   "Select the next article.
6990 If UNREAD, only unread articles are selected.
6991 If SUBJECT, only articles with SUBJECT are selected.
6992 If BACKWARD, the previous article is selected instead of the next."
6993   (interactive "P")
6994   (cond
6995    ;; Is there such an article?
6996    ((and (gnus-summary-search-forward unread subject backward)
6997          (or (gnus-summary-display-article (gnus-summary-article-number))
6998              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6999     (gnus-summary-position-point))
7000    ;; If not, we try the first unread, if that is wanted.
7001    ((and subject
7002          gnus-auto-select-same
7003          (gnus-summary-first-unread-article))
7004     (gnus-summary-position-point)
7005     (gnus-message 6 "Wrapped"))
7006    ;; Try to get next/previous article not displayed in this group.
7007    ((and gnus-auto-extend-newsgroup
7008          (not unread) (not subject))
7009     (gnus-summary-goto-article
7010      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7011      nil (count-lines (point-min) (point))))
7012    ;; Go to next/previous group.
7013    (t
7014     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7015       (gnus-summary-jump-to-group gnus-newsgroup-name))
7016     (let ((cmd last-command-char)
7017           (point
7018            (save-excursion
7019              (set-buffer gnus-group-buffer)
7020              (point)))
7021           (group
7022            (if (eq gnus-keep-same-level 'best)
7023                (gnus-summary-best-group gnus-newsgroup-name)
7024              (gnus-summary-search-group backward gnus-keep-same-level))))
7025       ;; For some reason, the group window gets selected.  We change
7026       ;; it back.
7027       (select-window (get-buffer-window (current-buffer)))
7028       ;; Select next unread newsgroup automagically.
7029       (cond
7030        ((or (not gnus-auto-select-next)
7031             (not cmd))
7032         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7033        ((or (eq gnus-auto-select-next 'quietly)
7034             (and (eq gnus-auto-select-next 'slightly-quietly)
7035                  push)
7036             (and (eq gnus-auto-select-next 'almost-quietly)
7037                  (gnus-summary-last-article-p)))
7038         ;; Select quietly.
7039         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7040             (gnus-summary-exit)
7041           (gnus-message 7 "No more%s articles (%s)..."
7042                         (if unread " unread" "")
7043                         (if group (concat "selecting " group)
7044                           "exiting"))
7045           (gnus-summary-next-group nil group backward)))
7046        (t
7047         (when (gnus-key-press-event-p last-input-event)
7048           (gnus-summary-walk-group-buffer
7049            gnus-newsgroup-name cmd unread backward point))))))))
7050
7051 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7052   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7053                       (?\C-p (gnus-group-prev-unread-group 1))))
7054         (cursor-in-echo-area t)
7055         keve key group ended prompt)
7056     (save-excursion
7057       (set-buffer gnus-group-buffer)
7058       (goto-char start)
7059       (setq group
7060             (if (eq gnus-keep-same-level 'best)
7061                 (gnus-summary-best-group gnus-newsgroup-name)
7062               (gnus-summary-search-group backward gnus-keep-same-level))))
7063     (while (not ended)
7064       (setq prompt
7065             (format
7066              "No more%s articles%s " (if unread " unread" "")
7067              (if (and group
7068                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7069                  (format " (Type %s for %s [%s])"
7070                          (single-key-description cmd) group
7071                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7072                (format " (Type %s to exit %s)"
7073                        (single-key-description cmd)
7074                        gnus-newsgroup-name))))
7075       ;; Confirm auto selection.
7076       (setq key (car (setq keve (gnus-read-event-char prompt)))
7077             ended t)
7078       (cond
7079        ((assq key keystrokes)
7080         (let ((obuf (current-buffer)))
7081           (switch-to-buffer gnus-group-buffer)
7082           (when group
7083             (gnus-group-jump-to-group group))
7084           (eval (cadr (assq key keystrokes)))
7085           (setq group (gnus-group-group-name))
7086           (switch-to-buffer obuf))
7087         (setq ended nil))
7088        ((equal key cmd)
7089         (if (or (not group)
7090                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7091             (gnus-summary-exit)
7092           (gnus-summary-next-group nil group backward)))
7093        (t
7094         (push (cdr keve) unread-command-events))))))
7095
7096 (defun gnus-summary-next-unread-article ()
7097   "Select unread article after current one."
7098   (interactive)
7099   (gnus-summary-next-article
7100    (or (not (eq gnus-summary-goto-unread 'never))
7101        (gnus-summary-last-article-p (gnus-summary-article-number)))
7102    (and gnus-auto-select-same
7103         (gnus-summary-article-subject))))
7104
7105 (defun gnus-summary-prev-article (&optional unread subject)
7106   "Select the article after the current one.
7107 If UNREAD is non-nil, only unread articles are selected."
7108   (interactive "P")
7109   (gnus-summary-next-article unread subject t))
7110
7111 (defun gnus-summary-prev-unread-article ()
7112   "Select unread article before current one."
7113   (interactive)
7114   (gnus-summary-prev-article
7115    (or (not (eq gnus-summary-goto-unread 'never))
7116        (gnus-summary-first-article-p (gnus-summary-article-number)))
7117    (and gnus-auto-select-same
7118         (gnus-summary-article-subject))))
7119
7120 (defun gnus-summary-next-page (&optional lines circular stop)
7121   "Show next page of the selected article.
7122 If at the end of the current article, select the next article.
7123 LINES says how many lines should be scrolled up.
7124
7125 If CIRCULAR is non-nil, go to the start of the article instead of
7126 selecting the next article when reaching the end of the current
7127 article.
7128
7129 If STOP is non-nil, just stop when reaching the end of the message."
7130   (interactive "P")
7131   (setq gnus-summary-buffer (current-buffer))
7132   (gnus-set-global-variables)
7133   (let ((article (gnus-summary-article-number))
7134         (article-window (get-buffer-window gnus-article-buffer t))
7135         endp)
7136     ;; If the buffer is empty, we have no article.
7137     (unless article
7138       (error "No article to select"))
7139     (gnus-configure-windows 'article)
7140     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7141         (if (and (eq gnus-summary-goto-unread 'never)
7142                  (not (gnus-summary-last-article-p article)))
7143             (gnus-summary-next-article)
7144           (gnus-summary-next-unread-article))
7145       (if (or (null gnus-current-article)
7146               (null gnus-article-current)
7147               (/= article (cdr gnus-article-current))
7148               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7149           ;; Selected subject is different from current article's.
7150           (gnus-summary-display-article article)
7151         (when article-window
7152           (gnus-eval-in-buffer-window gnus-article-buffer
7153             (setq endp (or (gnus-article-next-page lines)
7154                            (gnus-article-only-boring-p))))
7155           (when endp
7156             (cond (stop
7157                    (gnus-message 3 "End of message"))
7158                   (circular
7159                    (gnus-summary-beginning-of-article))
7160                   (lines
7161                    (gnus-message 3 "End of message"))
7162                   ((null lines)
7163                    (if (and (eq gnus-summary-goto-unread 'never)
7164                             (not (gnus-summary-last-article-p article)))
7165                        (gnus-summary-next-article)
7166                      (gnus-summary-next-unread-article))))))))
7167     (gnus-summary-recenter)
7168     (gnus-summary-position-point)))
7169
7170 (defun gnus-summary-prev-page (&optional lines move)
7171   "Show previous page of selected article.
7172 Argument LINES specifies lines to be scrolled down.
7173 If MOVE, move to the previous unread article if point is at
7174 the beginning of the buffer."
7175   (interactive "P")
7176   (let ((article (gnus-summary-article-number))
7177         (article-window (get-buffer-window gnus-article-buffer t))
7178         endp)
7179     (gnus-configure-windows 'article)
7180     (if (or (null gnus-current-article)
7181             (null gnus-article-current)
7182             (/= article (cdr gnus-article-current))
7183             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7184         ;; Selected subject is different from current article's.
7185         (gnus-summary-display-article article)
7186       (gnus-summary-recenter)
7187       (when article-window
7188         (gnus-eval-in-buffer-window gnus-article-buffer
7189           (setq endp (gnus-article-prev-page lines)))
7190         (when (and move endp)
7191           (cond (lines
7192                  (gnus-message 3 "Beginning of message"))
7193                 ((null lines)
7194                  (if (and (eq gnus-summary-goto-unread 'never)
7195                           (not (gnus-summary-first-article-p article)))
7196                      (gnus-summary-prev-article)
7197                    (gnus-summary-prev-unread-article))))))))
7198   (gnus-summary-position-point))
7199
7200 (defun gnus-summary-prev-page-or-article (&optional lines)
7201   "Show previous page of selected article.
7202 Argument LINES specifies lines to be scrolled down.
7203 If at the beginning of the article, go to the next article."
7204   (interactive "P")
7205   (gnus-summary-prev-page lines t))
7206
7207 (defun gnus-summary-scroll-up (lines)
7208   "Scroll up (or down) one line current article.
7209 Argument LINES specifies lines to be scrolled up (or down if negative)."
7210   (interactive "p")
7211   (gnus-configure-windows 'article)
7212   (gnus-summary-show-thread)
7213   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7214     (gnus-eval-in-buffer-window gnus-article-buffer
7215       (cond ((> lines 0)
7216              (when (gnus-article-next-page lines)
7217                (gnus-message 3 "End of message")))
7218             ((< lines 0)
7219              (gnus-article-prev-page (- lines))))))
7220   (gnus-summary-recenter)
7221   (gnus-summary-position-point))
7222
7223 (defun gnus-summary-scroll-down (lines)
7224   "Scroll down (or up) one line current article.
7225 Argument LINES specifies lines to be scrolled down (or up if negative)."
7226   (interactive "p")
7227   (gnus-summary-scroll-up (- lines)))
7228
7229 (defun gnus-summary-next-same-subject ()
7230   "Select next article which has the same subject as current one."
7231   (interactive)
7232   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7233
7234 (defun gnus-summary-prev-same-subject ()
7235   "Select previous article which has the same subject as current one."
7236   (interactive)
7237   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7238
7239 (defun gnus-summary-next-unread-same-subject ()
7240   "Select next unread article which has the same subject as current one."
7241   (interactive)
7242   (gnus-summary-next-article t (gnus-summary-article-subject)))
7243
7244 (defun gnus-summary-prev-unread-same-subject ()
7245   "Select previous unread article which has the same subject as current one."
7246   (interactive)
7247   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7248
7249 (defun gnus-summary-first-unread-article ()
7250   "Select the first unread article.
7251 Return nil if there are no unread articles."
7252   (interactive)
7253   (prog1
7254       (when (gnus-summary-first-subject t)
7255         (gnus-summary-show-thread)
7256         (gnus-summary-first-subject t)
7257         (gnus-summary-display-article (gnus-summary-article-number)))
7258     (gnus-summary-position-point)))
7259
7260 (defun gnus-summary-first-unread-subject ()
7261   "Place the point on the subject line of the first unread article.
7262 Return nil if there are no unread articles."
7263   (interactive)
7264   (prog1
7265       (when (gnus-summary-first-subject t)
7266         (gnus-summary-show-thread)
7267         (gnus-summary-first-subject t))
7268     (gnus-summary-position-point)))
7269
7270 (defun gnus-summary-first-unseen-subject ()
7271   "Place the point on the subject line of the first unseen article.
7272 Return nil if there are no unseen articles."
7273   (interactive)
7274   (prog1
7275       (when (gnus-summary-first-subject nil nil t)
7276         (gnus-summary-show-thread)
7277         (gnus-summary-first-subject nil nil t))
7278     (gnus-summary-position-point)))
7279
7280 (defun gnus-summary-first-unseen-or-unread-subject ()
7281   "Place the point on the subject line of the first unseen article or,
7282 if all article have been seen, on the subject line of the first unread
7283 article."
7284   (interactive)
7285   (prog1
7286       (unless (when (gnus-summary-first-subject nil nil t)
7287                 (gnus-summary-show-thread)
7288                 (gnus-summary-first-subject nil nil t))
7289         (when (gnus-summary-first-subject t)
7290           (gnus-summary-show-thread)
7291           (gnus-summary-first-subject t)))
7292     (gnus-summary-position-point)))
7293
7294 (defun gnus-summary-first-article ()
7295   "Select the first article.
7296 Return nil if there are no articles."
7297   (interactive)
7298   (prog1
7299       (when (gnus-summary-first-subject)
7300         (gnus-summary-show-thread)
7301         (gnus-summary-first-subject)
7302         (gnus-summary-display-article (gnus-summary-article-number)))
7303     (gnus-summary-position-point)))
7304
7305 (defun gnus-summary-best-unread-article (&optional arg)
7306   "Select the unread article with the highest score.
7307 If given a prefix argument, select the next unread article that has a
7308 score higher than the default score."
7309   (interactive "P")
7310   (let ((article (if arg
7311                      (gnus-summary-better-unread-subject)
7312                    (gnus-summary-best-unread-subject))))
7313     (if article
7314         (gnus-summary-goto-article article)
7315       (error "No unread articles"))))
7316
7317 (defun gnus-summary-best-unread-subject ()
7318   "Select the unread subject with the highest score."
7319   (interactive)
7320   (let ((best -1000000)
7321         (data gnus-newsgroup-data)
7322         article score)
7323     (while data
7324       (and (gnus-data-unread-p (car data))
7325            (> (setq score
7326                     (gnus-summary-article-score (gnus-data-number (car data))))
7327               best)
7328            (setq best score
7329                  article (gnus-data-number (car data))))
7330       (setq data (cdr data)))
7331     (when article
7332       (gnus-summary-goto-subject article))
7333     (gnus-summary-position-point)
7334     article))
7335
7336 (defun gnus-summary-better-unread-subject ()
7337   "Select the first unread subject that has a score over the default score."
7338   (interactive)
7339   (let ((data gnus-newsgroup-data)
7340         article score)
7341     (while (and (setq article (gnus-data-number (car data)))
7342                 (or (gnus-data-read-p (car data))
7343                     (not (> (gnus-summary-article-score article)
7344                             gnus-summary-default-score))))
7345       (setq data (cdr data)))
7346     (when article
7347       (gnus-summary-goto-subject article))
7348     (gnus-summary-position-point)
7349     article))
7350
7351 (defun gnus-summary-last-subject ()
7352   "Go to the last displayed subject line in the group."
7353   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7354     (when article
7355       (gnus-summary-goto-subject article))))
7356
7357 (defun gnus-summary-goto-article (article &optional all-headers force)
7358   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7359 If ALL-HEADERS is non-nil, no header lines are hidden.
7360 If FORCE, go to the article even if it isn't displayed.  If FORCE
7361 is a number, it is the line the article is to be displayed on."
7362   (interactive
7363    (list
7364     (completing-read
7365      "Article number or Message-ID: "
7366      (mapcar (lambda (number) (list (int-to-string number)))
7367              gnus-newsgroup-limit))
7368     current-prefix-arg
7369     t))
7370   (prog1
7371       (if (and (stringp article)
7372                (string-match "@" article))
7373           (gnus-summary-refer-article article)
7374         (when (stringp article)
7375           (setq article (string-to-number article)))
7376         (if (gnus-summary-goto-subject article force)
7377             (gnus-summary-display-article article all-headers)
7378           (gnus-message 4 "Couldn't go to article %s" article) nil))
7379     (gnus-summary-position-point)))
7380
7381 (defun gnus-summary-goto-last-article ()
7382   "Go to the previously read article."
7383   (interactive)
7384   (prog1
7385       (when gnus-last-article
7386         (gnus-summary-goto-article gnus-last-article nil t))
7387     (gnus-summary-position-point)))
7388
7389 (defun gnus-summary-pop-article (number)
7390   "Pop one article off the history and go to the previous.
7391 NUMBER articles will be popped off."
7392   (interactive "p")
7393   (let (to)
7394     (setq gnus-newsgroup-history
7395           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7396     (if to
7397         (gnus-summary-goto-article (car to) nil t)
7398       (error "Article history empty")))
7399   (gnus-summary-position-point))
7400
7401 ;; Summary commands and functions for limiting the summary buffer.
7402
7403 (defun gnus-summary-limit-to-articles (n)
7404   "Limit the summary buffer to the next N articles.
7405 If not given a prefix, use the process marked articles instead."
7406   (interactive "P")
7407   (prog1
7408       (let ((articles (gnus-summary-work-articles n)))
7409         (setq gnus-newsgroup-processable nil)
7410         (gnus-summary-limit articles))
7411     (gnus-summary-position-point)))
7412
7413 (defun gnus-summary-pop-limit (&optional total)
7414   "Restore the previous limit.
7415 If given a prefix, remove all limits."
7416   (interactive "P")
7417   (when total
7418     (setq gnus-newsgroup-limits
7419           (list (mapcar (lambda (h) (mail-header-number h))
7420                         gnus-newsgroup-headers))))
7421   (unless gnus-newsgroup-limits
7422     (error "No limit to pop"))
7423   (prog1
7424       (gnus-summary-limit nil 'pop)
7425     (gnus-summary-position-point)))
7426
7427 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7428   "Limit the summary buffer to articles that have subjects that match a regexp.
7429 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7430   (interactive
7431    (list (read-string (if current-prefix-arg
7432                           "Exclude subject (regexp): "
7433                         "Limit to subject (regexp): "))
7434          nil current-prefix-arg))
7435   (unless header
7436     (setq header "subject"))
7437   (when (not (equal "" subject))
7438     (prog1
7439         (let ((articles (gnus-summary-find-matching
7440                          (or header "subject") subject 'all nil nil
7441                          not-matching)))
7442           (unless articles
7443             (error "Found no matches for \"%s\"" subject))
7444           (gnus-summary-limit articles))
7445       (gnus-summary-position-point))))
7446
7447 (defun gnus-summary-limit-to-author (from &optional not-matching)
7448   "Limit the summary buffer to articles that have authors that match a regexp.
7449 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7450   (interactive
7451    (list (read-string (if current-prefix-arg
7452                           "Exclude author (regexp): "
7453                         "Limit to author (regexp): "))
7454          current-prefix-arg))
7455   (gnus-summary-limit-to-subject from "from" not-matching))
7456
7457 (defun gnus-summary-limit-to-age (age &optional younger-p)
7458   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7459 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7460 articles that are younger than AGE days."
7461   (interactive
7462    (let ((younger current-prefix-arg)
7463          (days-got nil)
7464          days)
7465      (while (not days-got)
7466        (setq days (if younger
7467                       (read-string "Limit to articles younger than (in days, older when negative): ")
7468                     (read-string
7469                      "Limit to articles older than (in days, younger when negative): ")))
7470        (when (> (length days) 0)
7471          (setq days (read days)))
7472        (if (numberp days)
7473            (progn
7474              (setq days-got t)
7475              (if (< days 0)
7476                  (progn
7477                    (setq younger (not younger))
7478                    (setq days (* days -1)))))
7479          (message "Please enter a number.")
7480          (sleep-for 1)))
7481      (list days younger)))
7482   (prog1
7483       (let ((data gnus-newsgroup-data)
7484             (cutoff (days-to-time age))
7485             articles d date is-younger)
7486         (while (setq d (pop data))
7487           (when (and (vectorp (gnus-data-header d))
7488                      (setq date (mail-header-date (gnus-data-header d))))
7489             (setq is-younger (time-less-p
7490                               (time-since (condition-case ()
7491                                               (date-to-time date)
7492                                             (error '(0 0))))
7493                               cutoff))
7494             (when (if younger-p
7495                       is-younger
7496                     (not is-younger))
7497               (push (gnus-data-number d) articles))))
7498         (gnus-summary-limit (nreverse articles)))
7499     (gnus-summary-position-point)))
7500
7501 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7502   "Limit the summary buffer to articles that match an 'extra' header."
7503   (interactive
7504    (let ((header
7505           (intern
7506            (gnus-completing-read-with-default
7507             (symbol-name (car gnus-extra-headers))
7508             (if current-prefix-arg
7509                 "Exclude extra header:"
7510               "Limit extra header:")
7511             (mapcar (lambda (x)
7512                       (cons (symbol-name x) x))
7513                     gnus-extra-headers)
7514             nil
7515             t))))
7516      (list header
7517            (read-string (format "%s header %s (regexp): "
7518                                 (if current-prefix-arg "Exclude" "Limit to")
7519                                 header))
7520            current-prefix-arg)))
7521   (when (not (equal "" regexp))
7522     (prog1
7523         (let ((articles (gnus-summary-find-matching
7524                          (cons 'extra header) regexp 'all nil nil
7525                          not-matching)))
7526           (unless articles
7527             (error "Found no matches for \"%s\"" regexp))
7528           (gnus-summary-limit articles))
7529       (gnus-summary-position-point))))
7530
7531 (defun gnus-summary-limit-to-display-predicate ()
7532   "Limit the summary buffer to the predicated in the `display' group parameter."
7533   (interactive)
7534   (unless gnus-newsgroup-display
7535     (error "There is no `display' group parameter"))
7536   (let (articles)
7537     (dolist (number gnus-newsgroup-articles)
7538       (when (funcall gnus-newsgroup-display)
7539         (push number articles)))
7540     (gnus-summary-limit articles))
7541   (gnus-summary-position-point))
7542
7543 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7544 (make-obsolete
7545  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7546
7547 (defun gnus-summary-limit-to-unread (&optional all)
7548   "Limit the summary buffer to articles that are not marked as read.
7549 If ALL is non-nil, limit strictly to unread articles."
7550   (interactive "P")
7551   (if all
7552       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7553     (gnus-summary-limit-to-marks
7554      ;; Concat all the marks that say that an article is read and have
7555      ;; those removed.
7556      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7557            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7558            gnus-low-score-mark gnus-expirable-mark
7559            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7560            gnus-duplicate-mark gnus-souped-mark)
7561      'reverse)))
7562
7563 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7564 (make-obsolete 'gnus-summary-delete-marked-with
7565                'gnus-summary-limit-exclude-marks)
7566
7567 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7568   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7569 If REVERSE, limit the summary buffer to articles that are marked
7570 with MARKS.  MARKS can either be a string of marks or a list of marks.
7571 Returns how many articles were removed."
7572   (interactive "sMarks: ")
7573   (gnus-summary-limit-to-marks marks t))
7574
7575 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7576   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7577 If REVERSE (the prefix), limit the summary buffer to articles that are
7578 not marked with MARKS.  MARKS can either be a string of marks or a
7579 list of marks.
7580 Returns how many articles were removed."
7581   (interactive "sMarks: \nP")
7582   (prog1
7583       (let ((data gnus-newsgroup-data)
7584             (marks (if (listp marks) marks
7585                      (append marks nil))) ; Transform to list.
7586             articles)
7587         (while data
7588           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7589                   (memq (gnus-data-mark (car data)) marks))
7590             (push (gnus-data-number (car data)) articles))
7591           (setq data (cdr data)))
7592         (gnus-summary-limit articles))
7593     (gnus-summary-position-point)))
7594
7595 (defun gnus-summary-limit-to-score (score)
7596   "Limit to articles with score at or above SCORE."
7597   (interactive "NLimit to articles with score of at least: ")
7598   (let ((data gnus-newsgroup-data)
7599         articles)
7600     (while data
7601       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7602                 score)
7603         (push (gnus-data-number (car data)) articles))
7604       (setq data (cdr data)))
7605     (prog1
7606         (gnus-summary-limit articles)
7607       (gnus-summary-position-point))))
7608
7609 (defun gnus-summary-limit-to-unseen ()
7610   "Limit to unseen articles."
7611   (interactive)
7612   (prog1
7613       (gnus-summary-limit gnus-newsgroup-unseen)
7614     (gnus-summary-position-point)))
7615
7616 (defun gnus-summary-limit-include-thread (id)
7617   "Display all the hidden articles that is in the thread with ID in it.
7618 When called interactively, ID is the Message-ID of the current
7619 article."
7620   (interactive (list (mail-header-id (gnus-summary-article-header))))
7621   (let ((articles (gnus-articles-in-thread
7622                    (gnus-id-to-thread (gnus-root-id id)))))
7623     (prog1
7624         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7625       (gnus-summary-limit-include-matching-articles
7626        "subject"
7627        (regexp-quote (gnus-simplify-subject-re
7628                       (mail-header-subject (gnus-id-to-header id)))))
7629       (gnus-summary-position-point))))
7630
7631 (defun gnus-summary-limit-include-matching-articles (header regexp)
7632   "Display all the hidden articles that have HEADERs that match REGEXP."
7633   (interactive (list (read-string "Match on header: ")
7634                      (read-string "Regexp: ")))
7635   (let ((articles (gnus-find-matching-articles header regexp)))
7636     (prog1
7637         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7638       (gnus-summary-position-point))))
7639
7640 (defun gnus-summary-insert-dormant-articles ()
7641   "Insert all the dormat articles for this group into the current buffer."
7642   (interactive)
7643   (let ((gnus-verbose (max 6 gnus-verbose)))
7644     (if (not gnus-newsgroup-dormant)
7645         (gnus-message 3 "No cached articles for this group")
7646       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7647
7648 (defun gnus-summary-limit-include-dormant ()
7649   "Display all the hidden articles that are marked as dormant.
7650 Note that this command only works on a subset of the articles currently
7651 fetched for this group."
7652   (interactive)
7653   (unless gnus-newsgroup-dormant
7654     (error "There are no dormant articles in this group"))
7655   (prog1
7656       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7657     (gnus-summary-position-point)))
7658
7659 (defun gnus-summary-limit-exclude-dormant ()
7660   "Hide all dormant articles."
7661   (interactive)
7662   (prog1
7663       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7664     (gnus-summary-position-point)))
7665
7666 (defun gnus-summary-limit-exclude-childless-dormant ()
7667   "Hide all dormant articles that have no children."
7668   (interactive)
7669   (let ((data (gnus-data-list t))
7670         articles d children)
7671     ;; Find all articles that are either not dormant or have
7672     ;; children.
7673     (while (setq d (pop data))
7674       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7675                 (and (setq children
7676                            (gnus-article-children (gnus-data-number d)))
7677                      (let (found)
7678                        (while children
7679                          (when (memq (car children) articles)
7680                            (setq children nil
7681                                  found t))
7682                          (pop children))
7683                        found)))
7684         (push (gnus-data-number d) articles)))
7685     ;; Do the limiting.
7686     (prog1
7687         (gnus-summary-limit articles)
7688       (gnus-summary-position-point))))
7689
7690 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7691   "Mark all unread excluded articles as read.
7692 If ALL, mark even excluded ticked and dormants as read."
7693   (interactive "P")
7694   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7695   (let ((articles (gnus-sorted-ndifference
7696                    (sort
7697                     (mapcar (lambda (h) (mail-header-number h))
7698                             gnus-newsgroup-headers)
7699                     '<)
7700                    gnus-newsgroup-limit))
7701         article)
7702     (setq gnus-newsgroup-unreads
7703           (gnus-sorted-intersection gnus-newsgroup-unreads
7704                                     gnus-newsgroup-limit))
7705     (if all
7706         (setq gnus-newsgroup-dormant nil
7707               gnus-newsgroup-marked nil
7708               gnus-newsgroup-reads
7709               (nconc
7710                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7711                gnus-newsgroup-reads))
7712       (while (setq article (pop articles))
7713         (unless (or (memq article gnus-newsgroup-dormant)
7714                     (memq article gnus-newsgroup-marked))
7715           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7716
7717 (defun gnus-summary-limit (articles &optional pop)
7718   (if pop
7719       ;; We pop the previous limit off the stack and use that.
7720       (setq articles (car gnus-newsgroup-limits)
7721             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7722     ;; We use the new limit, so we push the old limit on the stack.
7723     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7724   ;; Set the limit.
7725   (setq gnus-newsgroup-limit articles)
7726   (let ((total (length gnus-newsgroup-data))
7727         (data (gnus-data-find-list (gnus-summary-article-number)))
7728         (gnus-summary-mark-below nil)   ; Inhibit this.
7729         found)
7730     ;; This will do all the work of generating the new summary buffer
7731     ;; according to the new limit.
7732     (gnus-summary-prepare)
7733     ;; Hide any threads, possibly.
7734     (gnus-summary-maybe-hide-threads)
7735     ;; Try to return to the article you were at, or one in the
7736     ;; neighborhood.
7737     (when data
7738       ;; We try to find some article after the current one.
7739       (while data
7740         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7741           (setq data nil
7742                 found t))
7743         (setq data (cdr data))))
7744     (unless found
7745       ;; If there is no data, that means that we were after the last
7746       ;; article.  The same goes when we can't find any articles
7747       ;; after the current one.
7748       (goto-char (point-max))
7749       (gnus-summary-find-prev))
7750     (gnus-set-mode-line 'summary)
7751     ;; We return how many articles were removed from the summary
7752     ;; buffer as a result of the new limit.
7753     (- total (length gnus-newsgroup-data))))
7754
7755 (defsubst gnus-invisible-cut-children (threads)
7756   (let ((num 0))
7757     (while threads
7758       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7759         (incf num))
7760       (pop threads))
7761     (< num 2)))
7762
7763 (defsubst gnus-cut-thread (thread)
7764   "Go forwards in the thread until we find an article that we want to display."
7765   (when (or (eq gnus-fetch-old-headers 'some)
7766             (eq gnus-fetch-old-headers 'invisible)
7767             (numberp gnus-fetch-old-headers)
7768             (eq gnus-build-sparse-threads 'some)
7769             (eq gnus-build-sparse-threads 'more))
7770     ;; Deal with old-fetched headers and sparse threads.
7771     (while (and
7772             thread
7773             (or
7774              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7775              (gnus-summary-article-ancient-p
7776               (mail-header-number (car thread))))
7777             (if (or (<= (length (cdr thread)) 1)
7778                     (eq gnus-fetch-old-headers 'invisible))
7779                 (setq gnus-newsgroup-limit
7780                       (delq (mail-header-number (car thread))
7781                             gnus-newsgroup-limit)
7782                       thread (cadr thread))
7783               (when (gnus-invisible-cut-children (cdr thread))
7784                 (let ((th (cdr thread)))
7785                   (while th
7786                     (if (memq (mail-header-number (caar th))
7787                               gnus-newsgroup-limit)
7788                         (setq thread (car th)
7789                               th nil)
7790                       (setq th (cdr th))))))))))
7791   thread)
7792
7793 (defun gnus-cut-threads (threads)
7794   "Cut off all uninteresting articles from the beginning of threads."
7795   (when (or (eq gnus-fetch-old-headers 'some)
7796             (eq gnus-fetch-old-headers 'invisible)
7797             (numberp gnus-fetch-old-headers)
7798             (eq gnus-build-sparse-threads 'some)
7799             (eq gnus-build-sparse-threads 'more))
7800     (let ((th threads))
7801       (while th
7802         (setcar th (gnus-cut-thread (car th)))
7803         (setq th (cdr th)))))
7804   ;; Remove nixed out threads.
7805   (delq nil threads))
7806
7807 (defun gnus-summary-initial-limit (&optional show-if-empty)
7808   "Figure out what the initial limit is supposed to be on group entry.
7809 This entails weeding out unwanted dormants, low-scored articles,
7810 fetch-old-headers verbiage, and so on."
7811   ;; Most groups have nothing to remove.
7812   (if (or gnus-inhibit-limiting
7813           (and (null gnus-newsgroup-dormant)
7814                (eq gnus-newsgroup-display 'gnus-not-ignore)
7815                (not (eq gnus-fetch-old-headers 'some))
7816                (not (numberp gnus-fetch-old-headers))
7817                (not (eq gnus-fetch-old-headers 'invisible))
7818                (null gnus-summary-expunge-below)
7819                (not (eq gnus-build-sparse-threads 'some))
7820                (not (eq gnus-build-sparse-threads 'more))
7821                (null gnus-thread-expunge-below)
7822                (not gnus-use-nocem)))
7823       ()                                ; Do nothing.
7824     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7825     (setq gnus-newsgroup-limit nil)
7826     (mapatoms
7827      (lambda (node)
7828        (unless (car (symbol-value node))
7829          ;; These threads have no parents -- they are roots.
7830          (let ((nodes (cdr (symbol-value node)))
7831                thread)
7832            (while nodes
7833              (if (and gnus-thread-expunge-below
7834                       (< (gnus-thread-total-score (car nodes))
7835                          gnus-thread-expunge-below))
7836                  (gnus-expunge-thread (pop nodes))
7837                (setq thread (pop nodes))
7838                (gnus-summary-limit-children thread))))))
7839      gnus-newsgroup-dependencies)
7840     ;; If this limitation resulted in an empty group, we might
7841     ;; pop the previous limit and use it instead.
7842     (when (and (not gnus-newsgroup-limit)
7843                show-if-empty)
7844       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7845     gnus-newsgroup-limit))
7846
7847 (defun gnus-summary-limit-children (thread)
7848   "Return 1 if this subthread is visible and 0 if it is not."
7849   ;; First we get the number of visible children to this thread.  This
7850   ;; is done by recursing down the thread using this function, so this
7851   ;; will really go down to a leaf article first, before slowly
7852   ;; working its way up towards the root.
7853   (when thread
7854     (let* ((max-lisp-eval-depth 5000)
7855            (children
7856            (if (cdr thread)
7857                (apply '+ (mapcar 'gnus-summary-limit-children
7858                                  (cdr thread)))
7859              0))
7860           (number (mail-header-number (car thread)))
7861           score)
7862       (if (and
7863            (not (memq number gnus-newsgroup-marked))
7864            (or
7865             ;; If this article is dormant and has absolutely no visible
7866             ;; children, then this article isn't visible.
7867             (and (memq number gnus-newsgroup-dormant)
7868                  (zerop children))
7869             ;; If this is "fetch-old-headered" and there is no
7870             ;; visible children, then we don't want this article.
7871             (and (or (eq gnus-fetch-old-headers 'some)
7872                      (numberp gnus-fetch-old-headers))
7873                  (gnus-summary-article-ancient-p number)
7874                  (zerop children))
7875             ;; If this is "fetch-old-headered" and `invisible', then
7876             ;; we don't want this article.
7877             (and (eq gnus-fetch-old-headers 'invisible)
7878                  (gnus-summary-article-ancient-p number))
7879             ;; If this is a sparsely inserted article with no children,
7880             ;; we don't want it.
7881             (and (eq gnus-build-sparse-threads 'some)
7882                  (gnus-summary-article-sparse-p number)
7883                  (zerop children))
7884             ;; If we use expunging, and this article is really
7885             ;; low-scored, then we don't want this article.
7886             (when (and gnus-summary-expunge-below
7887                        (< (setq score
7888                                 (or (cdr (assq number gnus-newsgroup-scored))
7889                                     gnus-summary-default-score))
7890                           gnus-summary-expunge-below))
7891               ;; We increase the expunge-tally here, but that has
7892               ;; nothing to do with the limits, really.
7893               (incf gnus-newsgroup-expunged-tally)
7894               ;; We also mark as read here, if that's wanted.
7895               (when (and gnus-summary-mark-below
7896                          (< score gnus-summary-mark-below))
7897                 (setq gnus-newsgroup-unreads
7898                       (delq number gnus-newsgroup-unreads))
7899                 (if gnus-newsgroup-auto-expire
7900                     (push number gnus-newsgroup-expirable)
7901                   (push (cons number gnus-low-score-mark)
7902                         gnus-newsgroup-reads)))
7903               t)
7904             ;; Do the `display' group parameter.
7905             (and gnus-newsgroup-display
7906                  (not (funcall gnus-newsgroup-display)))
7907             ;; Check NoCeM things.
7908             (if (and gnus-use-nocem
7909                      (gnus-nocem-unwanted-article-p
7910                       (mail-header-id (car thread))))
7911                 (progn
7912                   (setq gnus-newsgroup-unreads
7913                         (delq number gnus-newsgroup-unreads))
7914                   t))))
7915           ;; Nope, invisible article.
7916           0
7917         ;; Ok, this article is to be visible, so we add it to the limit
7918         ;; and return 1.
7919         (push number gnus-newsgroup-limit)
7920         1))))
7921
7922 (defun gnus-expunge-thread (thread)
7923   "Mark all articles in THREAD as read."
7924   (let* ((number (mail-header-number (car thread))))
7925     (incf gnus-newsgroup-expunged-tally)
7926     ;; We also mark as read here, if that's wanted.
7927     (setq gnus-newsgroup-unreads
7928           (delq number gnus-newsgroup-unreads))
7929     (if gnus-newsgroup-auto-expire
7930         (push number gnus-newsgroup-expirable)
7931       (push (cons number gnus-low-score-mark)
7932             gnus-newsgroup-reads)))
7933   ;; Go recursively through all subthreads.
7934   (mapcar 'gnus-expunge-thread (cdr thread)))
7935
7936 ;; Summary article oriented commands
7937
7938 (defun gnus-summary-refer-parent-article (n)
7939   "Refer parent article N times.
7940 If N is negative, go to ancestor -N instead.
7941 The difference between N and the number of articles fetched is returned."
7942   (interactive "p")
7943   (let ((skip 1)
7944         error header ref)
7945     (when (not (natnump n))
7946       (setq skip (abs n)
7947             n 1))
7948     (while (and (> n 0)
7949                 (not error))
7950       (setq header (gnus-summary-article-header))
7951       (if (and (eq (mail-header-number header)
7952                    (cdr gnus-article-current))
7953                (equal gnus-newsgroup-name
7954                       (car gnus-article-current)))
7955           ;; If we try to find the parent of the currently
7956           ;; displayed article, then we take a look at the actual
7957           ;; References header, since this is slightly more
7958           ;; reliable than the References field we got from the
7959           ;; server.
7960           (save-excursion
7961             (set-buffer gnus-original-article-buffer)
7962             (nnheader-narrow-to-headers)
7963             (unless (setq ref (message-fetch-field "references"))
7964               (setq ref (message-fetch-field "in-reply-to")))
7965             (widen))
7966         (setq ref
7967               ;; It's not the current article, so we take a bet on
7968               ;; the value we got from the server.
7969               (mail-header-references header)))
7970       (if (and ref
7971                (not (equal ref "")))
7972           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7973             (gnus-message 1 "Couldn't find parent"))
7974         (gnus-message 1 "No references in article %d"
7975                       (gnus-summary-article-number))
7976         (setq error t))
7977       (decf n))
7978     (gnus-summary-position-point)
7979     n))
7980
7981 (defun gnus-summary-refer-references ()
7982   "Fetch all articles mentioned in the References header.
7983 Return the number of articles fetched."
7984   (interactive)
7985   (let ((ref (mail-header-references (gnus-summary-article-header)))
7986         (current (gnus-summary-article-number))
7987         (n 0))
7988     (if (or (not ref)
7989             (equal ref ""))
7990         (error "No References in the current article")
7991       ;; For each Message-ID in the References header...
7992       (while (string-match "<[^>]*>" ref)
7993         (incf n)
7994         ;; ... fetch that article.
7995         (gnus-summary-refer-article
7996          (prog1 (match-string 0 ref)
7997            (setq ref (substring ref (match-end 0))))))
7998       (gnus-summary-goto-subject current)
7999       (gnus-summary-position-point)
8000       n)))
8001
8002 (defun gnus-summary-refer-thread (&optional limit)
8003   "Fetch all articles in the current thread.
8004 If LIMIT (the numerical prefix), fetch that many old headers instead
8005 of what's specified by the `gnus-refer-thread-limit' variable."
8006   (interactive "P")
8007   (let ((id (mail-header-id (gnus-summary-article-header)))
8008         (limit (if limit (prefix-numeric-value limit)
8009                  gnus-refer-thread-limit)))
8010     (unless (eq gnus-fetch-old-headers 'invisible)
8011       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8012       ;; Retrieve the headers and read them in.
8013       (if (eq (if (numberp limit)
8014                   (gnus-retrieve-headers
8015                    (list (min
8016                           (+ (mail-header-number
8017                               (gnus-summary-article-header))
8018                              limit)
8019                           gnus-newsgroup-end))
8020                    gnus-newsgroup-name (* limit 2))
8021                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8022                 ;; headers.
8023                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8024                                        gnus-newsgroup-name limit))
8025               'nov)
8026           (gnus-build-all-threads)
8027         (error "Can't fetch thread from backends that don't support NOV"))
8028       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8029     (gnus-summary-limit-include-thread id)))
8030
8031 (defun gnus-summary-refer-article (message-id)
8032   "Fetch an article specified by MESSAGE-ID."
8033   (interactive "sMessage-ID: ")
8034   (when (and (stringp message-id)
8035              (not (zerop (length message-id))))
8036     ;; Construct the correct Message-ID if necessary.
8037     ;; Suggested by tale@pawl.rpi.edu.
8038     (unless (string-match "^<" message-id)
8039       (setq message-id (concat "<" message-id)))
8040     (unless (string-match ">$" message-id)
8041       (setq message-id (concat message-id ">")))
8042     (let* ((header (gnus-id-to-header message-id))
8043            (sparse (and header
8044                         (gnus-summary-article-sparse-p
8045                          (mail-header-number header))
8046                         (memq (mail-header-number header)
8047                               gnus-newsgroup-limit)))
8048            number)
8049       (cond
8050        ;; If the article is present in the buffer we just go to it.
8051        ((and header
8052              (or (not (gnus-summary-article-sparse-p
8053                        (mail-header-number header)))
8054                  sparse))
8055         (prog1
8056             (gnus-summary-goto-article
8057              (mail-header-number header) nil t)
8058           (when sparse
8059             (gnus-summary-update-article (mail-header-number header)))))
8060        (t
8061         ;; We fetch the article.
8062         (catch 'found
8063           (dolist (gnus-override-method (gnus-refer-article-methods))
8064             (when (and (gnus-check-server gnus-override-method)
8065                        ;; Fetch the header,
8066                        (setq number (gnus-summary-insert-subject message-id)))
8067               ;; and display the article.
8068               (gnus-summary-select-article nil nil nil number)
8069               (throw 'found t)))
8070           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8071
8072 (defun gnus-refer-article-methods ()
8073   "Return a list of referable methods."
8074   (cond
8075    ;; No method, so we default to current and native.
8076    ((null gnus-refer-article-method)
8077     (list gnus-current-select-method gnus-select-method))
8078    ;; Current.
8079    ((eq 'current gnus-refer-article-method)
8080     (list gnus-current-select-method))
8081    ;; List of select methods.
8082    ((not (and (symbolp (car gnus-refer-article-method))
8083               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8084     (let (out)
8085       (dolist (method gnus-refer-article-method)
8086         (push (if (eq 'current method)
8087                   gnus-current-select-method
8088                 method)
8089               out))
8090       (nreverse out)))
8091    ;; One single select method.
8092    (t
8093     (list gnus-refer-article-method))))
8094
8095 (defun gnus-summary-edit-parameters ()
8096   "Edit the group parameters of the current group."
8097   (interactive)
8098   (gnus-group-edit-group gnus-newsgroup-name 'params))
8099
8100 (defun gnus-summary-customize-parameters ()
8101   "Customize the group parameters of the current group."
8102   (interactive)
8103   (gnus-group-customize gnus-newsgroup-name))
8104
8105 (defun gnus-summary-enter-digest-group (&optional force)
8106   "Enter an nndoc group based on the current article.
8107 If FORCE, force a digest interpretation.  If not, try
8108 to guess what the document format is."
8109   (interactive "P")
8110   (let ((conf gnus-current-window-configuration))
8111     (save-excursion
8112       (gnus-summary-select-article))
8113     (setq gnus-current-window-configuration conf)
8114     (let* ((name (format "%s-%d"
8115                          (gnus-group-prefixed-name
8116                           gnus-newsgroup-name (list 'nndoc ""))
8117                          (save-excursion
8118                            (set-buffer gnus-summary-buffer)
8119                            gnus-current-article)))
8120            (ogroup gnus-newsgroup-name)
8121            (params (append (gnus-info-params (gnus-get-info ogroup))
8122                            (list (cons 'to-group ogroup))
8123                            (list (cons 'save-article-group ogroup))))
8124            (case-fold-search t)
8125            (buf (current-buffer))
8126            dig to-address)
8127       (save-excursion
8128         (set-buffer gnus-original-article-buffer)
8129         ;; Have the digest group inherit the main mail address of
8130         ;; the parent article.
8131         (when (setq to-address (or (gnus-fetch-field "reply-to")
8132                                    (gnus-fetch-field "from")))
8133           (setq params (append
8134                         (list (cons 'to-address
8135                                     (funcall gnus-decode-encoded-word-function
8136                                              to-address))))))
8137         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8138         (insert-buffer-substring gnus-original-article-buffer)
8139         ;; Remove lines that may lead nndoc to misinterpret the
8140         ;; document type.
8141         (narrow-to-region
8142          (goto-char (point-min))
8143          (or (search-forward "\n\n" nil t) (point)))
8144         (goto-char (point-min))
8145         (delete-matching-lines "^Path:\\|^From ")
8146         (widen))
8147       (unwind-protect
8148           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8149                     (gnus-newsgroup-ephemeral-ignored-charsets
8150                      gnus-newsgroup-ignored-charsets))
8151                 (gnus-group-read-ephemeral-group
8152                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8153                               (nndoc-article-type
8154                                ,(if force 'mbox 'guess)))
8155                  t nil nil nil
8156                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8157                                                         "ADAPT")))))
8158               ;; Make all postings to this group go to the parent group.
8159               (nconc (gnus-info-params (gnus-get-info name))
8160                      params)
8161             ;; Couldn't select this doc group.
8162             (switch-to-buffer buf)
8163             (gnus-set-global-variables)
8164             (gnus-configure-windows 'summary)
8165             (gnus-message 3 "Article couldn't be entered?"))
8166         (kill-buffer dig)))))
8167
8168 (defun gnus-summary-read-document (n)
8169   "Open a new group based on the current article(s).
8170 This will allow you to read digests and other similar
8171 documents as newsgroups.
8172 Obeys the standard process/prefix convention."
8173   (interactive "P")
8174   (let* ((articles (gnus-summary-work-articles n))
8175          (ogroup gnus-newsgroup-name)
8176          (params (append (gnus-info-params (gnus-get-info ogroup))
8177                          (list (cons 'to-group ogroup))))
8178          article group egroup groups vgroup)
8179     (while (setq article (pop articles))
8180       (setq group (format "%s-%d" gnus-newsgroup-name article))
8181       (gnus-summary-remove-process-mark article)
8182       (when (gnus-summary-display-article article)
8183         (save-excursion
8184           (with-temp-buffer
8185             (insert-buffer-substring gnus-original-article-buffer)
8186             ;; Remove some headers that may lead nndoc to make
8187             ;; the wrong guess.
8188             (message-narrow-to-head)
8189             (goto-char (point-min))
8190             (delete-matching-lines "^\\(Path\\):\\|^From ")
8191             (widen)
8192             (if (setq egroup
8193                       (gnus-group-read-ephemeral-group
8194                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8195                                      (nndoc-article-type guess))
8196                        t nil t))
8197                 (progn
8198             ;; Make all postings to this group go to the parent group.
8199                   (nconc (gnus-info-params (gnus-get-info egroup))
8200                          params)
8201                   (push egroup groups))
8202               ;; Couldn't select this doc group.
8203               (gnus-error 3 "Article couldn't be entered"))))))
8204     ;; Now we have selected all the documents.
8205     (cond
8206      ((not groups)
8207       (error "None of the articles could be interpreted as documents"))
8208      ((gnus-group-read-ephemeral-group
8209        (setq vgroup (format
8210                      "nnvirtual:%s-%s" gnus-newsgroup-name
8211                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8212        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8213        t
8214        (cons (current-buffer) 'summary)))
8215      (t
8216       (error "Couldn't select virtual nndoc group")))))
8217
8218 (defun gnus-summary-isearch-article (&optional regexp-p)
8219   "Do incremental search forward on the current article.
8220 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8221   (interactive "P")
8222   (gnus-summary-select-article)
8223   (gnus-configure-windows 'article)
8224   (gnus-eval-in-buffer-window gnus-article-buffer
8225     (save-restriction
8226       (widen)
8227       (isearch-forward regexp-p))))
8228
8229 (defun gnus-summary-search-article-forward (regexp &optional backward)
8230   "Search for an article containing REGEXP forward.
8231 If BACKWARD, search backward instead."
8232   (interactive
8233    (list (read-string
8234           (format "Search article %s (regexp%s): "
8235                   (if current-prefix-arg "backward" "forward")
8236                   (if gnus-last-search-regexp
8237                       (concat ", default " gnus-last-search-regexp)
8238                     "")))
8239          current-prefix-arg))
8240   (if (string-equal regexp "")
8241       (setq regexp (or gnus-last-search-regexp ""))
8242     (setq gnus-last-search-regexp regexp)
8243     (setq gnus-article-before-search gnus-current-article))
8244   ;; Intentionally set gnus-last-article.
8245   (setq gnus-last-article gnus-article-before-search)
8246   (let ((gnus-last-article gnus-last-article))
8247     (if (gnus-summary-search-article regexp backward)
8248         (gnus-summary-show-thread)
8249       (error "Search failed: \"%s\"" regexp))))
8250
8251 (defun gnus-summary-search-article-backward (regexp)
8252   "Search for an article containing REGEXP backward."
8253   (interactive
8254    (list (read-string
8255           (format "Search article backward (regexp%s): "
8256                   (if gnus-last-search-regexp
8257                       (concat ", default " gnus-last-search-regexp)
8258                     "")))))
8259   (gnus-summary-search-article-forward regexp 'backward))
8260
8261 (defun gnus-summary-search-article (regexp &optional backward)
8262   "Search for an article containing REGEXP.
8263 Optional argument BACKWARD means do search for backward.
8264 `gnus-select-article-hook' is not called during the search."
8265   ;; We have to require this here to make sure that the following
8266   ;; dynamic binding isn't shadowed by autoloading.
8267   (require 'gnus-async)
8268   (require 'gnus-art)
8269   (let ((gnus-select-article-hook nil)  ;Disable hook.
8270         (gnus-article-prepare-hook nil)
8271         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8272         (gnus-use-article-prefetch nil)
8273         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8274         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8275         (gnus-visual nil)
8276         (gnus-keep-backlog nil)
8277         (gnus-break-pages nil)
8278         (gnus-summary-display-arrow nil)
8279         (gnus-updated-mode-lines nil)
8280         (gnus-auto-center-summary nil)
8281         (sum (current-buffer))
8282         (gnus-display-mime-function nil)
8283         (found nil)
8284         point)
8285     (gnus-save-hidden-threads
8286       (gnus-summary-select-article)
8287       (set-buffer gnus-article-buffer)
8288       (goto-char (window-point (get-buffer-window (current-buffer))))
8289       (when backward
8290         (forward-line -1))
8291       (while (not found)
8292         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8293         (if (if backward
8294                 (re-search-backward regexp nil t)
8295               (re-search-forward regexp nil t))
8296             ;; We found the regexp.
8297             (progn
8298               (setq found 'found)
8299               (beginning-of-line)
8300               (set-window-start
8301                (get-buffer-window (current-buffer))
8302                (point))
8303               (forward-line 1)
8304               (set-window-point
8305                (get-buffer-window (current-buffer))
8306                (point))
8307               (set-buffer sum)
8308               (setq point (point)))
8309           ;; We didn't find it, so we go to the next article.
8310           (set-buffer sum)
8311           (setq found 'not)
8312           (while (eq found 'not)
8313             (if (not (if backward (gnus-summary-find-prev)
8314                        (gnus-summary-find-next)))
8315                 ;; No more articles.
8316                 (setq found t)
8317               ;; Select the next article and adjust point.
8318               (unless (gnus-summary-article-sparse-p
8319                        (gnus-summary-article-number))
8320                 (setq found nil)
8321                 (gnus-summary-select-article)
8322                 (set-buffer gnus-article-buffer)
8323                 (widen)
8324                 (goto-char (if backward (point-max) (point-min))))))))
8325       (gnus-message 7 ""))
8326     ;; Return whether we found the regexp.
8327     (when (eq found 'found)
8328       (goto-char point)
8329       (gnus-summary-show-thread)
8330       (gnus-summary-goto-subject gnus-current-article)
8331       (gnus-summary-position-point)
8332       t)))
8333
8334 (defun gnus-find-matching-articles (header regexp)
8335   "Return a list of all articles that match REGEXP on HEADER.
8336 This search includes all articles in the current group that Gnus has
8337 fetched headers for, whether they are displayed or not."
8338   (let ((articles nil)
8339         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8340         (case-fold-search t))
8341     (dolist (header gnus-newsgroup-headers)
8342       (when (string-match regexp (funcall func header))
8343         (push (mail-header-number header) articles)))
8344     (nreverse articles)))
8345
8346 (defun gnus-summary-find-matching (header regexp &optional backward unread
8347                                           not-case-fold not-matching)
8348   "Return a list of all articles that match REGEXP on HEADER.
8349 The search stars on the current article and goes forwards unless
8350 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8351 If UNREAD is non-nil, only unread articles will
8352 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8353 in the comparisons. If NOT-MATCHING, return a list of all articles that
8354 not match REGEXP on HEADER."
8355   (let ((case-fold-search (not not-case-fold))
8356         articles d func)
8357     (if (consp header)
8358         (if (eq (car header) 'extra)
8359             (setq func
8360                   `(lambda (h)
8361                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8362                          "")))
8363           (error "%s is an invalid header" header))
8364       (unless (fboundp (intern (concat "mail-header-" header)))
8365         (error "%s is not a valid header" header))
8366       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8367     (dolist (d (if (eq backward 'all)
8368                    gnus-newsgroup-data
8369                  (gnus-data-find-list
8370                   (gnus-summary-article-number)
8371                   (gnus-data-list backward))))
8372       (when (and (or (not unread)       ; We want all articles...
8373                      (gnus-data-unread-p d)) ; Or just unreads.
8374                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8375                  (if not-matching
8376                      (not (string-match
8377                            regexp
8378                            (funcall func (gnus-data-header d))))
8379                    (string-match regexp
8380                                  (funcall func (gnus-data-header d)))))
8381         (push (gnus-data-number d) articles))) ; Success!
8382     (nreverse articles)))
8383
8384 (defun gnus-summary-execute-command (header regexp command &optional backward)
8385   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8386 If HEADER is an empty string (or nil), the match is done on the entire
8387 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8388   (interactive
8389    (list (let ((completion-ignore-case t))
8390            (completing-read
8391             "Header name: "
8392             (mapcar (lambda (header) (list (format "%s" header)))
8393                     (append
8394                      '("Number" "Subject" "From" "Lines" "Date"
8395                        "Message-ID" "Xref" "References" "Body")
8396                      gnus-extra-headers))
8397             nil 'require-match))
8398          (read-string "Regexp: ")
8399          (read-key-sequence "Command: ")
8400          current-prefix-arg))
8401   (when (equal header "Body")
8402     (setq header ""))
8403   ;; Hidden thread subtrees must be searched as well.
8404   (gnus-summary-show-all-threads)
8405   ;; We don't want to change current point nor window configuration.
8406   (save-excursion
8407     (save-window-excursion
8408       (let (gnus-visual
8409             gnus-treat-strip-trailing-blank-lines
8410             gnus-treat-strip-leading-blank-lines
8411             gnus-treat-strip-multiple-blank-lines
8412             gnus-treat-hide-boring-headers
8413             gnus-treat-fold-newsgroups
8414             gnus-article-prepare-hook)
8415         (gnus-message 6 "Executing %s..." (key-description command))
8416         ;; We'd like to execute COMMAND interactively so as to give arguments.
8417         (gnus-execute header regexp
8418                       `(call-interactively ',(key-binding command))
8419                       backward)
8420         (gnus-message 6 "Executing %s...done" (key-description command))))))
8421
8422 (defun gnus-summary-beginning-of-article ()
8423   "Scroll the article back to the beginning."
8424   (interactive)
8425   (gnus-summary-select-article)
8426   (gnus-configure-windows 'article)
8427   (gnus-eval-in-buffer-window gnus-article-buffer
8428     (widen)
8429     (goto-char (point-min))
8430     (when gnus-page-broken
8431       (gnus-narrow-to-page))))
8432
8433 (defun gnus-summary-end-of-article ()
8434   "Scroll to the end of the article."
8435   (interactive)
8436   (gnus-summary-select-article)
8437   (gnus-configure-windows 'article)
8438   (gnus-eval-in-buffer-window gnus-article-buffer
8439     (widen)
8440     (goto-char (point-max))
8441     (recenter -3)
8442     (when gnus-page-broken
8443       (gnus-narrow-to-page))))
8444
8445 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8446   "Truncate to LEN and quote all \"(\"'s in STRING."
8447   (gnus-replace-in-string (if (and len (> (length string) len))
8448                               (substring string 0 len)
8449                             string)
8450                           "[()]" "\\\\\\&"))
8451
8452 (defun gnus-summary-print-article (&optional filename n)
8453   "Generate and print a PostScript image of the N next (mail) articles.
8454
8455 If N is negative, print the N previous articles.  If N is nil and articles
8456 have been marked with the process mark, print these instead.
8457
8458 If the optional first argument FILENAME is nil, send the image to the
8459 printer.  If FILENAME is a string, save the PostScript image in a file with
8460 that name.  If FILENAME is a number, prompt the user for the name of the file
8461 to save in."
8462   (interactive (list (ps-print-preprint current-prefix-arg)))
8463   (dolist (article (gnus-summary-work-articles n))
8464     (gnus-summary-select-article nil nil 'pseudo article)
8465     (gnus-eval-in-buffer-window gnus-article-buffer
8466       (gnus-print-buffer))
8467     (gnus-summary-remove-process-mark article))
8468   (ps-despool filename))
8469
8470 (defun gnus-print-buffer ()
8471   (let ((buffer (generate-new-buffer " *print*")))
8472     (unwind-protect
8473         (progn
8474           (copy-to-buffer buffer (point-min) (point-max))
8475           (set-buffer buffer)
8476           (gnus-article-delete-invisible-text)
8477           (gnus-remove-text-with-property 'gnus-decoration)
8478           (when (gnus-visual-p 'article-highlight 'highlight)
8479             ;; Copy-to-buffer doesn't copy overlay.  So redo
8480             ;; highlight.
8481             (let ((gnus-article-buffer buffer))
8482               (gnus-article-highlight-citation t)
8483               (gnus-article-highlight-signature)))
8484           (let ((ps-left-header
8485                  (list
8486                   (concat "("
8487                           (gnus-summary-print-truncate-and-quote
8488                            (mail-header-subject gnus-current-headers)
8489                            66) ")")
8490                   (concat "("
8491                           (gnus-summary-print-truncate-and-quote
8492                            (mail-header-from gnus-current-headers)
8493                            45) ")")))
8494                 (ps-right-header
8495                  (list
8496                   "/pagenumberstring load"
8497                   (concat "("
8498                           (mail-header-date gnus-current-headers) ")"))))
8499             (gnus-run-hooks 'gnus-ps-print-hook)
8500             (save-excursion
8501               (if window-system
8502                   (ps-spool-buffer-with-faces)
8503                 (ps-spool-buffer)))))
8504       (kill-buffer buffer))))
8505
8506 (defun gnus-summary-show-article (&optional arg)
8507   "Force redisplaying of the current article.
8508 If ARG (the prefix) is a number, show the article with the charset
8509 defined in `gnus-summary-show-article-charset-alist', or the charset
8510 input.
8511 If ARG (the prefix) is non-nil and not a number, show the raw article
8512 without any article massaging functions being run.  Normally, the key strokes
8513 are `C-u g'."
8514   (interactive "P")
8515   (cond
8516    ((numberp arg)
8517     (gnus-summary-show-article t)
8518     (let ((gnus-newsgroup-charset
8519            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8520                (mm-read-coding-system
8521                 "View as charset: " ;; actually it is coding system.
8522                 (save-excursion
8523                   (set-buffer gnus-article-buffer)
8524                   (mm-detect-coding-region (point) (point-max))))))
8525           (gnus-newsgroup-ignored-charsets 'gnus-all))
8526       (gnus-summary-select-article nil 'force)
8527       (let ((deps gnus-newsgroup-dependencies)
8528             head header lines)
8529         (save-excursion
8530           (set-buffer gnus-original-article-buffer)
8531           (save-restriction
8532             (message-narrow-to-head)
8533             (setq head (buffer-string))
8534             (goto-char (point-min))
8535             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8536               (goto-char (point-max))
8537               (widen)
8538               (setq lines (1- (count-lines (point) (point-max))))))
8539           (with-temp-buffer
8540             (insert (format "211 %d Article retrieved.\n"
8541                             (cdr gnus-article-current)))
8542             (insert head)
8543             (if lines (insert (format "Lines: %d\n" lines)))
8544             (insert ".\n")
8545             (let ((nntp-server-buffer (current-buffer)))
8546               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8547         (gnus-data-set-header
8548          (gnus-data-find (cdr gnus-article-current))
8549          header)
8550         (gnus-summary-update-article-line
8551          (cdr gnus-article-current) header)
8552         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8553           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8554    ((not arg)
8555     ;; Select the article the normal way.
8556     (gnus-summary-select-article nil 'force))
8557    (t
8558     ;; We have to require this here to make sure that the following
8559     ;; dynamic binding isn't shadowed by autoloading.
8560     (require 'gnus-async)
8561     (require 'gnus-art)
8562     ;; Bind the article treatment functions to nil.
8563     (let ((gnus-have-all-headers t)
8564           gnus-article-prepare-hook
8565           gnus-article-decode-hook
8566           gnus-display-mime-function
8567           gnus-break-pages)
8568       ;; Destroy any MIME parts.
8569       (when (gnus-buffer-live-p gnus-article-buffer)
8570         (save-excursion
8571           (set-buffer gnus-article-buffer)
8572           (mm-destroy-parts gnus-article-mime-handles)
8573           ;; Set it to nil for safety reason.
8574           (setq gnus-article-mime-handle-alist nil)
8575           (setq gnus-article-mime-handles nil)))
8576       (gnus-summary-select-article nil 'force))))
8577   (gnus-summary-goto-subject gnus-current-article)
8578   (gnus-summary-position-point))
8579
8580 (defun gnus-summary-show-raw-article ()
8581   "Show the raw article without any article massaging functions being run."
8582   (interactive)
8583   (gnus-summary-show-article t))
8584
8585 (defun gnus-summary-verbose-headers (&optional arg)
8586   "Toggle permanent full header display.
8587 If ARG is a positive number, turn header display on.
8588 If ARG is a negative number, turn header display off."
8589   (interactive "P")
8590   (setq gnus-show-all-headers
8591         (cond ((or (not (numberp arg))
8592                    (zerop arg))
8593                (not gnus-show-all-headers))
8594               ((natnump arg)
8595                t)))
8596   (gnus-summary-show-article))
8597
8598 (defun gnus-summary-toggle-header (&optional arg)
8599   "Show the headers if they are hidden, or hide them if they are shown.
8600 If ARG is a positive number, show the entire header.
8601 If ARG is a negative number, hide the unwanted header lines."
8602   (interactive "P")
8603   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8604                      (get-buffer-window gnus-article-buffer t))))
8605     (with-current-buffer gnus-article-buffer
8606       (widen)
8607       (article-narrow-to-head)
8608       (let* ((buffer-read-only nil)
8609              (inhibit-point-motion-hooks t)
8610              (hidden (if (numberp arg)
8611                          (>= arg 0)
8612                        (gnus-article-hidden-text-p 'headers)))
8613              s e)
8614         (delete-region (point-min) (point-max))
8615         (with-current-buffer gnus-original-article-buffer
8616           (goto-char (setq s (point-min)))
8617           (setq e (if (search-forward "\n\n" nil t)
8618                       (1- (point))
8619                     (point-max))))
8620         (insert-buffer-substring gnus-original-article-buffer s e)
8621         (run-hooks 'gnus-article-decode-hook)
8622         (if hidden
8623             (let ((gnus-treat-hide-headers nil)
8624                   (gnus-treat-hide-boring-headers nil))
8625               (gnus-delete-wash-type 'headers)
8626               (gnus-treat-article 'head))
8627           (gnus-treat-article 'head))
8628         (widen)
8629         (if window
8630             (set-window-start window (goto-char (point-min))))
8631         (setq gnus-page-broken
8632               (when gnus-break-pages
8633                 (gnus-narrow-to-page)
8634                 t))
8635         (gnus-set-mode-line 'article)))))
8636
8637 (defun gnus-summary-show-all-headers ()
8638   "Make all header lines visible."
8639   (interactive)
8640   (gnus-summary-toggle-header 1))
8641
8642 (defun gnus-summary-caesar-message (&optional arg)
8643   "Caesar rotate the current article by 13.
8644 The numerical prefix specifies how many places to rotate each letter
8645 forward."
8646   (interactive "P")
8647   (gnus-summary-select-article)
8648   (let ((mail-header-separator ""))
8649     (gnus-eval-in-buffer-window gnus-article-buffer
8650       (save-restriction
8651         (widen)
8652         (let ((start (window-start))
8653               buffer-read-only)
8654           (message-caesar-buffer-body arg)
8655           (set-window-start (get-buffer-window (current-buffer)) start))))))
8656
8657 (autoload 'unmorse-region "morse"
8658   "Convert morse coded text in region to ordinary ASCII text."
8659   t)
8660
8661 (defun gnus-summary-morse-message (&optional arg)
8662   "Morse decode the current article."
8663   (interactive "P")
8664   (gnus-summary-select-article)
8665   (let ((mail-header-separator ""))
8666     (gnus-eval-in-buffer-window gnus-article-buffer
8667       (save-excursion
8668         (save-restriction
8669           (widen)
8670           (let ((pos (window-start))
8671                 buffer-read-only)
8672             (goto-char (point-min))
8673             (when (message-goto-body)
8674               (gnus-narrow-to-body))
8675             (goto-char (point-min))
8676             (while (re-search-forward "·" (point-max) t)
8677               (replace-match "."))
8678             (unmorse-region (point-min) (point-max))
8679             (widen)
8680             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8681
8682 (defun gnus-summary-stop-page-breaking ()
8683   "Stop page breaking in the current article."
8684   (interactive)
8685   (gnus-summary-select-article)
8686   (gnus-eval-in-buffer-window gnus-article-buffer
8687     (widen)
8688     (when (gnus-visual-p 'page-marker)
8689       (let ((buffer-read-only nil))
8690         (gnus-remove-text-with-property 'gnus-prev)
8691         (gnus-remove-text-with-property 'gnus-next))
8692       (setq gnus-page-broken nil))))
8693
8694 (defun gnus-summary-move-article (&optional n to-newsgroup
8695                                             select-method action)
8696   "Move the current article to a different newsgroup.
8697 If N is a positive number, move the N next articles.
8698 If N is a negative number, move the N previous articles.
8699 If N is nil and any articles have been marked with the process mark,
8700 move those articles instead.
8701 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8702 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8703 re-spool using this method.
8704
8705 When called interactively with TO-NEWSGROUP being nil, the value of
8706 the variable `gnus-move-split-methods' is used for finding a default
8707 for the target newsgroup.
8708
8709 For this function to work, both the current newsgroup and the
8710 newsgroup that you want to move to have to support the `request-move'
8711 and `request-accept' functions.
8712
8713 ACTION can be either `move' (the default), `crosspost' or `copy'."
8714   (interactive "P")
8715   (unless action
8716     (setq action 'move))
8717   ;; Check whether the source group supports the required functions.
8718   (cond ((and (eq action 'move)
8719               (not (gnus-check-backend-function
8720                     'request-move-article gnus-newsgroup-name)))
8721          (error "The current group does not support article moving"))
8722         ((and (eq action 'crosspost)
8723               (not (gnus-check-backend-function
8724                     'request-replace-article gnus-newsgroup-name)))
8725          (error "The current group does not support article editing")))
8726   (let ((articles (gnus-summary-work-articles n))
8727         (prefix (if (gnus-check-backend-function
8728                      'request-move-article gnus-newsgroup-name)
8729                     (gnus-group-real-prefix gnus-newsgroup-name)
8730                   ""))
8731         (names '((move "Move" "Moving")
8732                  (copy "Copy" "Copying")
8733                  (crosspost "Crosspost" "Crossposting")))
8734         (copy-buf (save-excursion
8735                     (nnheader-set-temp-buffer " *copy article*")))
8736         art-group to-method new-xref article to-groups)
8737     (unless (assq action names)
8738       (error "Unknown action %s" action))
8739     ;; Read the newsgroup name.
8740     (when (and (not to-newsgroup)
8741                (not select-method))
8742       (if (and gnus-move-split-methods
8743                (not
8744                 (and (memq gnus-current-article articles)
8745                      (gnus-buffer-live-p gnus-original-article-buffer))))
8746           ;; When `gnus-move-split-methods' is non-nil, we have to
8747           ;; select an article to give `gnus-read-move-group-name' an
8748           ;; opportunity to suggest an appropriate default.  However,
8749           ;; we needn't render or mark the article.
8750           (let ((gnus-display-mime-function nil)
8751                 (gnus-article-prepare-hook nil)
8752                 (gnus-mark-article-hook nil))
8753             (gnus-summary-select-article nil nil nil (car articles))))
8754       (setq to-newsgroup
8755             (gnus-read-move-group-name
8756              (cadr (assq action names))
8757              (symbol-value (intern (format "gnus-current-%s-group" action)))
8758              articles prefix))
8759       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8760     (setq to-method (or select-method
8761                         (gnus-server-to-method
8762                          (gnus-group-method to-newsgroup))))
8763     ;; Check the method we are to move this article to...
8764     (unless (gnus-check-backend-function
8765              'request-accept-article (car to-method))
8766       (error "%s does not support article copying" (car to-method)))
8767     (unless (gnus-check-server to-method)
8768       (error "Can't open server %s" (car to-method)))
8769     (gnus-message 6 "%s to %s: %s..."
8770                   (caddr (assq action names))
8771                   (or (car select-method) to-newsgroup) articles)
8772     (while articles
8773       (setq article (pop articles))
8774       (setq
8775        art-group
8776        (cond
8777         ;; Move the article.
8778         ((eq action 'move)
8779          ;; Remove this article from future suppression.
8780          (gnus-dup-unsuppress-article article)
8781          (gnus-request-move-article
8782           article                       ; Article to move
8783           gnus-newsgroup-name           ; From newsgroup
8784           (nth 1 (gnus-find-method-for-group
8785                   gnus-newsgroup-name)) ; Server
8786           (list 'gnus-request-accept-article
8787                 to-newsgroup (list 'quote select-method)
8788                 (not articles) t)       ; Accept form
8789           (not articles)))              ; Only save nov last time
8790         ;; Copy the article.
8791         ((eq action 'copy)
8792          (save-excursion
8793            (set-buffer copy-buf)
8794            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8795              (gnus-request-accept-article
8796               to-newsgroup select-method (not articles) t))))
8797         ;; Crosspost the article.
8798         ((eq action 'crosspost)
8799          (let ((xref (message-tokenize-header
8800                       (mail-header-xref (gnus-summary-article-header article))
8801                       " ")))
8802            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8803                                   ":" (number-to-string article)))
8804            (unless xref
8805              (setq xref (list (system-name))))
8806            (setq new-xref
8807                  (concat
8808                   (mapconcat 'identity
8809                              (delete "Xref:" (delete new-xref xref))
8810                              " ")
8811                   " " new-xref))
8812            (save-excursion
8813              (set-buffer copy-buf)
8814              ;; First put the article in the destination group.
8815              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8816              (when (consp (setq art-group
8817                                 (gnus-request-accept-article
8818                                  to-newsgroup select-method (not articles))))
8819                (setq new-xref (concat new-xref " " (car art-group)
8820                                       ":"
8821                                       (number-to-string (cdr art-group))))
8822                ;; Now we have the new Xrefs header, so we insert
8823                ;; it and replace the new article.
8824                (nnheader-replace-header "Xref" new-xref)
8825                (gnus-request-replace-article
8826                 (cdr art-group) to-newsgroup (current-buffer))
8827                art-group))))))
8828       (cond
8829        ((not art-group)
8830         (gnus-message 1 "Couldn't %s article %s: %s"
8831                       (cadr (assq action names)) article
8832                       (nnheader-get-report (car to-method))))
8833        ((eq art-group 'junk)
8834         (when (eq action 'move)
8835           (gnus-summary-mark-article article gnus-canceled-mark)
8836           (gnus-message 4 "Deleted article %s" article)
8837           ;; run the delete hook
8838           (run-hook-with-args 'gnus-summary-article-delete-hook
8839                               action
8840                               (gnus-data-header
8841                                (assoc article (gnus-data-list nil)))
8842                               gnus-newsgroup-name nil
8843                               select-method)))
8844        (t
8845         (let* ((pto-group (gnus-group-prefixed-name
8846                            (car art-group) to-method))
8847                (entry
8848                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8849                (info (nth 2 entry))
8850                (to-group (gnus-info-group info))
8851                to-marks)
8852           ;; Update the group that has been moved to.
8853           (when (and info
8854                      (memq action '(move copy)))
8855             (unless (member to-group to-groups)
8856               (push to-group to-groups))
8857
8858             (unless (memq article gnus-newsgroup-unreads)
8859               (push 'read to-marks)
8860               (gnus-info-set-read
8861                info (gnus-add-to-range (gnus-info-read info)
8862                                        (list (cdr art-group)))))
8863
8864             ;; See whether the article is to be put in the cache.
8865             (let ((marks gnus-article-mark-lists)
8866                   (to-article (cdr art-group)))
8867
8868               ;; Enter the article into the cache in the new group,
8869               ;; if that is required.
8870               (when gnus-use-cache
8871                 (gnus-cache-possibly-enter-article
8872                  to-group to-article
8873                  (memq article gnus-newsgroup-marked)
8874                  (memq article gnus-newsgroup-dormant)
8875                  (memq article gnus-newsgroup-unreads)))
8876
8877               (when gnus-preserve-marks
8878                 ;; Copy any marks over to the new group.
8879                 (when (and (equal to-group gnus-newsgroup-name)
8880                            (not (memq article gnus-newsgroup-unreads)))
8881                   ;; Mark this article as read in this group.
8882                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8883                   (setcdr (gnus-active to-group) to-article)
8884                   (setcdr gnus-newsgroup-active to-article))
8885
8886                 (while marks
8887                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8888                     (when (memq article (symbol-value
8889                                          (intern (format "gnus-newsgroup-%s"
8890                                                          (caar marks)))))
8891                       (push (cdar marks) to-marks)
8892                       ;; If the other group is the same as this group,
8893                       ;; then we have to add the mark to the list.
8894                       (when (equal to-group gnus-newsgroup-name)
8895                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8896                              (cons to-article
8897                                    (symbol-value
8898                                     (intern (format "gnus-newsgroup-%s"
8899                                                     (caar marks)))))))
8900                       ;; Copy the marks to other group.
8901                       (gnus-add-marked-articles
8902                        to-group (cdar marks) (list to-article) info)))
8903                   (setq marks (cdr marks)))
8904
8905                 (gnus-request-set-mark
8906                  to-group (list (list (list to-article) 'add to-marks))))
8907
8908               (gnus-dribble-enter
8909                (concat "(gnus-group-set-info '"
8910                        (gnus-prin1-to-string (gnus-get-info to-group))
8911                        ")"))))
8912
8913           ;; Update the Xref header in this article to point to
8914           ;; the new crossposted article we have just created.
8915           (when (eq action 'crosspost)
8916             (save-excursion
8917               (set-buffer copy-buf)
8918               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8919               (nnheader-replace-header "Xref" new-xref)
8920               (gnus-request-replace-article
8921                article gnus-newsgroup-name (current-buffer))))
8922
8923           ;; run the move/copy/crosspost/respool hook
8924           (run-hook-with-args 'gnus-summary-article-move-hook 
8925                               action
8926                               (gnus-data-header 
8927                                (assoc article (gnus-data-list nil)))
8928                               gnus-newsgroup-name
8929                               to-newsgroup
8930                               select-method))
8931
8932         ;;;!!!Why is this necessary?
8933         (set-buffer gnus-summary-buffer)
8934         
8935         (gnus-summary-goto-subject article)
8936         (when (eq action 'move)
8937           (gnus-summary-mark-article article gnus-canceled-mark))))
8938       (gnus-summary-remove-process-mark article))
8939     ;; Re-activate all groups that have been moved to.
8940     (save-excursion
8941       (set-buffer gnus-group-buffer)
8942       (let ((gnus-group-marked to-groups))
8943         (gnus-group-get-new-news-this-group nil t)))
8944
8945     (gnus-kill-buffer copy-buf)
8946     (gnus-summary-position-point)
8947     (gnus-set-mode-line 'summary)))
8948
8949 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8950   "Move the current article to a different newsgroup.
8951 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8952 When called interactively, if TO-NEWSGROUP is nil, use the value of
8953 the variable `gnus-move-split-methods' for finding a default target
8954 newsgroup.
8955 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8956 re-spool using this method."
8957   (interactive "P")
8958   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8959
8960 (defun gnus-summary-crosspost-article (&optional n)
8961   "Crosspost the current article to some other group."
8962   (interactive "P")
8963   (gnus-summary-move-article n nil nil 'crosspost))
8964
8965 (defcustom gnus-summary-respool-default-method nil
8966   "Default method type for respooling an article.
8967 If nil, use to the current newsgroup method."
8968   :type 'symbol
8969   :group 'gnus-summary-mail)
8970
8971 (defcustom gnus-summary-display-while-building nil
8972   "If not-nil, show and update the summary buffer as it's being built.
8973 If the value is t, update the buffer after every line is inserted.  If
8974 the value is an integer (N), update the display every N lines."
8975   :group 'gnus-thread
8976   :type '(choice (const :tag "off" nil)
8977                  number
8978                  (const :tag "frequently" t)))
8979
8980 (defun gnus-summary-respool-article (&optional n method)
8981   "Respool the current article.
8982 The article will be squeezed through the mail spooling process again,
8983 which means that it will be put in some mail newsgroup or other
8984 depending on `nnmail-split-methods'.
8985 If N is a positive number, respool the N next articles.
8986 If N is a negative number, respool the N previous articles.
8987 If N is nil and any articles have been marked with the process mark,
8988 respool those articles instead.
8989
8990 Respooling can be done both from mail groups and \"real\" newsgroups.
8991 In the former case, the articles in question will be moved from the
8992 current group into whatever groups they are destined to.  In the
8993 latter case, they will be copied into the relevant groups."
8994   (interactive
8995    (list current-prefix-arg
8996          (let* ((methods (gnus-methods-using 'respool))
8997                 (methname
8998                  (symbol-name (or gnus-summary-respool-default-method
8999                                   (car (gnus-find-method-for-group
9000                                         gnus-newsgroup-name)))))
9001                 (method
9002                  (gnus-completing-read-with-default
9003                   methname "What backend do you want to use when respooling?"
9004                   methods nil t nil 'gnus-mail-method-history))
9005                 ms)
9006            (cond
9007             ((zerop (length (setq ms (gnus-servers-using-backend
9008                                       (intern method)))))
9009              (list (intern method) ""))
9010             ((= 1 (length ms))
9011              (car ms))
9012             (t
9013              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9014                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9015                            ms-alist))))))))
9016   (unless method
9017     (error "No method given for respooling"))
9018   (if (assoc (symbol-name
9019               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9020              (gnus-methods-using 'respool))
9021       (gnus-summary-move-article n nil method)
9022     (gnus-summary-copy-article n nil method)))
9023
9024 (defun gnus-summary-import-article (file &optional edit)
9025   "Import an arbitrary file into a mail newsgroup."
9026   (interactive "fImport file: \nP")
9027   (let ((group gnus-newsgroup-name)
9028         (now (current-time))
9029         atts lines group-art)
9030     (unless (gnus-check-backend-function 'request-accept-article group)
9031       (error "%s does not support article importing" group))
9032     (or (file-readable-p file)
9033         (not (file-regular-p file))
9034         (error "Can't read %s" file))
9035     (save-excursion
9036       (set-buffer (gnus-get-buffer-create " *import file*"))
9037       (erase-buffer)
9038       (nnheader-insert-file-contents file)
9039       (goto-char (point-min))
9040       (if (nnheader-article-p)
9041           (save-restriction
9042             (goto-char (point-min))
9043             (search-forward "\n\n" nil t)
9044             (narrow-to-region (point-min) (1- (point)))
9045             (goto-char (point-min))
9046             (unless (re-search-forward "^date:" nil t)
9047               (goto-char (point-max))
9048               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9049        ;; This doesn't look like an article, so we fudge some headers.
9050         (setq atts (file-attributes file)
9051               lines (count-lines (point-min) (point-max)))
9052         (insert "From: " (read-string "From: ") "\n"
9053                 "Subject: " (read-string "Subject: ") "\n"
9054                 "Date: " (message-make-date (nth 5 atts)) "\n"
9055                 "Message-ID: " (message-make-message-id) "\n"
9056                 "Lines: " (int-to-string lines) "\n"
9057                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9058       (setq group-art (gnus-request-accept-article group nil t))
9059       (kill-buffer (current-buffer)))
9060     (setq gnus-newsgroup-active (gnus-activate-group group))
9061     (forward-line 1)
9062     (gnus-summary-goto-article (cdr group-art) nil t)
9063     (when edit
9064       (gnus-summary-edit-article))))
9065
9066 (defun gnus-summary-create-article ()
9067   "Create an article in a mail newsgroup."
9068   (interactive)
9069   (let ((group gnus-newsgroup-name)
9070         (now (current-time))
9071         group-art)
9072     (unless (gnus-check-backend-function 'request-accept-article group)
9073       (error "%s does not support article importing" group))
9074     (save-excursion
9075       (set-buffer (gnus-get-buffer-create " *import file*"))
9076       (erase-buffer)
9077       (goto-char (point-min))
9078       ;; This doesn't look like an article, so we fudge some headers.
9079       (insert "From: " (read-string "From: ") "\n"
9080               "Subject: " (read-string "Subject: ") "\n"
9081               "Date: " (message-make-date now) "\n"
9082               "Message-ID: " (message-make-message-id) "\n")
9083       (setq group-art (gnus-request-accept-article group nil t))
9084       (kill-buffer (current-buffer)))
9085     (setq gnus-newsgroup-active (gnus-activate-group group))
9086     (forward-line 1)
9087     (gnus-summary-goto-article (cdr group-art) nil t)
9088     (gnus-summary-edit-article)))
9089
9090 (defun gnus-summary-article-posted-p ()
9091   "Say whether the current (mail) article is available from news as well.
9092 This will be the case if the article has both been mailed and posted."
9093   (interactive)
9094   (let ((id (mail-header-references (gnus-summary-article-header)))
9095         (gnus-override-method (car (gnus-refer-article-methods))))
9096     (if (gnus-request-head id "")
9097         (gnus-message 2 "The current message was found on %s"
9098                       gnus-override-method)
9099       (gnus-message 2 "The current message couldn't be found on %s"
9100                     gnus-override-method)
9101       nil)))
9102
9103 (defun gnus-summary-expire-articles (&optional now)
9104   "Expire all articles that are marked as expirable in the current group."
9105   (interactive)
9106   (when (and (not gnus-group-is-exiting-without-update-p)
9107              (gnus-check-backend-function
9108               'request-expire-articles gnus-newsgroup-name))
9109     ;; This backend supports expiry.
9110     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9111            (expirable (if total
9112                           (progn
9113                             ;; We need to update the info for
9114                             ;; this group for `gnus-list-of-read-articles'
9115                             ;; to give us the right answer.
9116                             (gnus-run-hooks 'gnus-exit-group-hook)
9117                             (gnus-summary-update-info)
9118                             (gnus-list-of-read-articles gnus-newsgroup-name))
9119                         (setq gnus-newsgroup-expirable
9120                               (sort gnus-newsgroup-expirable '<))))
9121            (expiry-wait (if now 'immediate
9122                           (gnus-group-find-parameter
9123                            gnus-newsgroup-name 'expiry-wait)))
9124            (nnmail-expiry-target
9125             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9126                 nnmail-expiry-target))
9127            es)
9128       (when expirable
9129         ;; There are expirable articles in this group, so we run them
9130         ;; through the expiry process.
9131         (gnus-message 6 "Expiring articles...")
9132         (unless (gnus-check-group gnus-newsgroup-name)
9133           (error "Can't open server for %s" gnus-newsgroup-name))
9134         ;; The list of articles that weren't expired is returned.
9135         (save-excursion
9136           (if expiry-wait
9137               (let ((nnmail-expiry-wait-function nil)
9138                     (nnmail-expiry-wait expiry-wait))
9139                 (setq es (gnus-request-expire-articles
9140                           expirable gnus-newsgroup-name)))
9141             (setq es (gnus-request-expire-articles
9142                       expirable gnus-newsgroup-name)))
9143           (unless total
9144             (setq gnus-newsgroup-expirable es))
9145           ;; We go through the old list of expirable, and mark all
9146           ;; really expired articles as nonexistent.
9147           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9148             (let ((gnus-use-cache nil))
9149               (dolist (article expirable)
9150                 (when (and (not (memq article es))
9151                            (gnus-data-find article))
9152                   (gnus-summary-mark-article article gnus-canceled-mark)
9153                   (run-hook-with-args 'gnus-summary-article-expire-hook
9154                                       'delete
9155                                       (gnus-data-header
9156                                        (assoc article (gnus-data-list nil)))
9157                                       gnus-newsgroup-name
9158                                       nil
9159                                       nil))))))
9160         (gnus-message 6 "Expiring articles...done")))))
9161
9162 (defun gnus-summary-expire-articles-now ()
9163   "Expunge all expirable articles in the current group.
9164 This means that *all* articles that are marked as expirable will be
9165 deleted forever, right now."
9166   (interactive)
9167   (or gnus-expert-user
9168       (gnus-yes-or-no-p
9169        "Are you really, really, really sure you want to delete all these messages? ")
9170       (error "Phew!"))
9171   (gnus-summary-expire-articles t))
9172
9173 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9174 (defun gnus-summary-delete-article (&optional n)
9175   "Delete the N next (mail) articles.
9176 This command actually deletes articles.  This is not a marking
9177 command.  The article will disappear forever from your life, never to
9178 return.
9179 If N is negative, delete backwards.
9180 If N is nil and articles have been marked with the process mark,
9181 delete these instead."
9182   (interactive "P")
9183   (unless (gnus-check-backend-function 'request-expire-articles
9184                                        gnus-newsgroup-name)
9185     (error "The current newsgroup does not support article deletion"))
9186   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9187     (error "Couldn't open server"))
9188   ;; Compute the list of articles to delete.
9189   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9190         (nnmail-expiry-target 'delete)
9191         not-deleted)
9192     (if (and gnus-novice-user
9193              (not (gnus-yes-or-no-p
9194                    (format "Do you really want to delete %s forever? "
9195                            (if (> (length articles) 1)
9196                                (format "these %s articles" (length articles))
9197                              "this article")))))
9198         ()
9199       ;; Delete the articles.
9200       (setq not-deleted (gnus-request-expire-articles
9201                          articles gnus-newsgroup-name 'force))
9202       (while articles
9203         (gnus-summary-remove-process-mark (car articles))
9204         ;; The backend might not have been able to delete the article
9205         ;; after all.
9206         (unless (memq (car articles) not-deleted)
9207           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9208         (let* ((article (car articles))
9209                (id (mail-header-id (gnus-data-header 
9210                                     (assoc article (gnus-data-list nil))))))
9211           (run-hook-with-args 'gnus-summary-article-delete-hook
9212                               'delete id gnus-newsgroup-name nil
9213                               nil))
9214         (setq articles (cdr articles)))
9215       (when not-deleted
9216         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9217     (gnus-summary-position-point)
9218     (gnus-set-mode-line 'summary)
9219     not-deleted))
9220
9221 (defun gnus-summary-edit-article (&optional arg)
9222   "Edit the current article.
9223 This will have permanent effect only in mail groups.
9224 If ARG is nil, edit the decoded articles.
9225 If ARG is 1, edit the raw articles.
9226 If ARG is 2, edit the raw articles even in read-only groups.
9227 If ARG is 3, edit the articles with the current handles.
9228 Otherwise, allow editing of articles even in read-only
9229 groups."
9230   (interactive "P")
9231   (let (force raw current-handles)
9232     (cond
9233      ((null arg))
9234      ((eq arg 1)
9235       (setq raw t))
9236      ((eq arg 2)
9237       (setq raw t
9238             force t))
9239      ((eq arg 3)
9240       (setq current-handles
9241             (and (gnus-buffer-live-p gnus-article-buffer)
9242                  (with-current-buffer gnus-article-buffer
9243                    (prog1
9244                        gnus-article-mime-handles
9245                      (setq gnus-article-mime-handles nil))))))
9246      (t
9247       (setq force t)))
9248     (when (and raw (not force)
9249                (member gnus-newsgroup-name '("nndraft:delayed"
9250                                              "nndraft:drafts"
9251                                              "nndraft:queue")))
9252       (error "Can't edit the raw article in group %s"
9253              gnus-newsgroup-name))
9254     (save-excursion
9255       (set-buffer gnus-summary-buffer)
9256       (let ((mail-parse-charset gnus-newsgroup-charset)
9257             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9258         (gnus-set-global-variables)
9259         (when (and (not force)
9260                    (gnus-group-read-only-p))
9261           (error "The current newsgroup does not support article editing"))
9262         (gnus-summary-show-article t)
9263         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9264           (with-current-buffer gnus-article-buffer
9265             (mm-enable-multibyte)))
9266         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9267             (setq raw t))
9268         (gnus-article-edit-article
9269          (if raw 'ignore
9270            `(lambda ()
9271               (let ((mbl mml-buffer-list))
9272                 (setq mml-buffer-list nil)
9273                 (mime-to-mml ,'current-handles)
9274                 (let ((mbl1 mml-buffer-list))
9275                   (setq mml-buffer-list mbl)
9276                   (set (make-local-variable 'mml-buffer-list) mbl1))
9277                 (make-local-hook 'kill-buffer-hook)
9278                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9279          `(lambda (no-highlight)
9280             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9281                   (message-options message-options)
9282                   (message-options-set-recipient)
9283                   (mail-parse-ignored-charsets
9284                    ',gnus-newsgroup-ignored-charsets))
9285               ,(if (not raw) '(progn
9286                                 (mml-to-mime)
9287                                 (mml-destroy-buffers)
9288                                 (remove-hook 'kill-buffer-hook
9289                                              'mml-destroy-buffers t)
9290                                 (kill-local-variable 'mml-buffer-list)))
9291               (gnus-summary-edit-article-done
9292                ,(or (mail-header-references gnus-current-headers) "")
9293                ,(gnus-group-read-only-p)
9294                ,gnus-summary-buffer no-highlight))))))))
9295
9296 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9297
9298 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9299                                                  no-highlight)
9300   "Make edits to the current article permanent."
9301   (interactive)
9302   (save-excursion
9303    ;; The buffer restriction contains the entire article if it exists.
9304     (when (article-goto-body)
9305       (let ((lines (count-lines (point) (point-max)))
9306             (length (- (point-max) (point)))
9307             (case-fold-search t)
9308             (body (copy-marker (point))))
9309         (goto-char (point-min))
9310         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9311           (delete-region (match-beginning 1) (match-end 1))
9312           (insert (number-to-string length)))
9313         (goto-char (point-min))
9314         (when (re-search-forward
9315                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9316           (delete-region (match-beginning 1) (match-end 1))
9317           (insert (number-to-string length)))
9318         (goto-char (point-min))
9319         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9320           (delete-region (match-beginning 1) (match-end 1))
9321           (insert (number-to-string lines))))))
9322   ;; Replace the article.
9323   (let ((buf (current-buffer)))
9324     (with-temp-buffer
9325       (insert-buffer-substring buf)
9326
9327       (if (and (not read-only)
9328                (not (gnus-request-replace-article
9329                      (cdr gnus-article-current) (car gnus-article-current)
9330                      (current-buffer) t)))
9331           (error "Couldn't replace article")
9332         ;; Update the summary buffer.
9333         (if (and references
9334                  (equal (message-tokenize-header references " ")
9335                         (message-tokenize-header
9336                          (or (message-fetch-field "references") "") " ")))
9337             ;; We only have to update this line.
9338             (save-excursion
9339               (save-restriction
9340                 (message-narrow-to-head)
9341                 (let ((head (buffer-string))
9342                       header)
9343                   (with-temp-buffer
9344                     (insert (format "211 %d Article retrieved.\n"
9345                                     (cdr gnus-article-current)))
9346                     (insert head)
9347                     (insert ".\n")
9348                     (let ((nntp-server-buffer (current-buffer)))
9349                       (setq header (car (gnus-get-newsgroup-headers
9350                                          nil t))))
9351                     (save-excursion
9352                       (set-buffer gnus-summary-buffer)
9353                       (gnus-data-set-header
9354                        (gnus-data-find (cdr gnus-article-current))
9355                        header)
9356                       (gnus-summary-update-article-line
9357                        (cdr gnus-article-current) header)
9358                       (if (gnus-summary-goto-subject
9359                            (cdr gnus-article-current) nil t)
9360                           (gnus-summary-update-secondary-mark
9361                            (cdr gnus-article-current))))))))
9362           ;; Update threads.
9363           (set-buffer (or buffer gnus-summary-buffer))
9364           (gnus-summary-update-article (cdr gnus-article-current))
9365           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9366               (gnus-summary-update-secondary-mark
9367                (cdr gnus-article-current))))
9368         ;; Prettify the article buffer again.
9369         (unless no-highlight
9370           (save-excursion
9371             (set-buffer gnus-article-buffer)
9372             ;;;!!! Fix this -- article should be rehighlighted.
9373             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9374             (set-buffer gnus-original-article-buffer)
9375             (gnus-request-article
9376              (cdr gnus-article-current)
9377              (car gnus-article-current) (current-buffer))))
9378         ;; Prettify the summary buffer line.
9379         (when (gnus-visual-p 'summary-highlight 'highlight)
9380           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9381
9382 (defun gnus-summary-edit-wash (key)
9383   "Perform editing command KEY in the article buffer."
9384   (interactive
9385    (list
9386     (progn
9387       (message "%s" (concat (this-command-keys) "- "))
9388       (read-char))))
9389   (message "")
9390   (gnus-summary-edit-article)
9391   (execute-kbd-macro (concat (this-command-keys) key))
9392   (gnus-article-edit-done))
9393
9394 ;;; Respooling
9395
9396 (defun gnus-summary-respool-query (&optional silent trace)
9397   "Query where the respool algorithm would put this article."
9398   (interactive)
9399   (let (gnus-mark-article-hook)
9400     (gnus-summary-select-article)
9401     (save-excursion
9402       (set-buffer gnus-original-article-buffer)
9403       (save-restriction
9404         (message-narrow-to-head)
9405         (let ((groups (nnmail-article-group 'identity trace)))
9406           (unless silent
9407             (if groups
9408                 (message "This message would go to %s"
9409                          (mapconcat 'car groups ", "))
9410               (message "This message would go to no groups"))
9411             groups))))))
9412
9413 (defun gnus-summary-respool-trace ()
9414   "Trace where the respool algorithm would put this article.
9415 Display a buffer showing all fancy splitting patterns which matched."
9416   (interactive)
9417   (gnus-summary-respool-query nil t))
9418
9419 ;; Summary marking commands.
9420
9421 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9422   "Mark articles which has the same subject as read, and then select the next.
9423 If UNMARK is positive, remove any kind of mark.
9424 If UNMARK is negative, tick articles."
9425   (interactive "P")
9426   (when unmark
9427     (setq unmark (prefix-numeric-value unmark)))
9428   (let ((count
9429          (gnus-summary-mark-same-subject
9430           (gnus-summary-article-subject) unmark)))
9431     ;; Select next unread article.  If auto-select-same mode, should
9432     ;; select the first unread article.
9433     (gnus-summary-next-article t (and gnus-auto-select-same
9434                                       (gnus-summary-article-subject)))
9435     (gnus-message 7 "%d article%s marked as %s"
9436                   count (if (= count 1) " is" "s are")
9437                   (if unmark "unread" "read"))))
9438
9439 (defun gnus-summary-kill-same-subject (&optional unmark)
9440   "Mark articles which has the same subject as read.
9441 If UNMARK is positive, remove any kind of mark.
9442 If UNMARK is negative, tick articles."
9443   (interactive "P")
9444   (when unmark
9445     (setq unmark (prefix-numeric-value unmark)))
9446   (let ((count
9447          (gnus-summary-mark-same-subject
9448           (gnus-summary-article-subject) unmark)))
9449     ;; If marked as read, go to next unread subject.
9450     (when (null unmark)
9451       ;; Go to next unread subject.
9452       (gnus-summary-next-subject 1 t))
9453     (gnus-message 7 "%d articles are marked as %s"
9454                   count (if unmark "unread" "read"))))
9455
9456 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9457   "Mark articles with same SUBJECT as read, and return marked number.
9458 If optional argument UNMARK is positive, remove any kinds of marks.
9459 If optional argument UNMARK is negative, mark articles as unread instead."
9460   (let ((count 1))
9461     (save-excursion
9462       (cond
9463        ((null unmark)                   ; Mark as read.
9464         (while (and
9465                 (progn
9466                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9467                   (gnus-summary-show-thread) t)
9468                 (gnus-summary-find-subject subject))
9469           (setq count (1+ count))))
9470        ((> unmark 0)                    ; Tick.
9471         (while (and
9472                 (progn
9473                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9474                   (gnus-summary-show-thread) t)
9475                 (gnus-summary-find-subject subject))
9476           (setq count (1+ count))))
9477        (t                               ; Mark as unread.
9478         (while (and
9479                 (progn
9480                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9481                   (gnus-summary-show-thread) t)
9482                 (gnus-summary-find-subject subject))
9483           (setq count (1+ count)))))
9484       (gnus-set-mode-line 'summary)
9485       ;; Return the number of marked articles.
9486       count)))
9487
9488 (defun gnus-summary-mark-as-processable (n &optional unmark)
9489   "Set the process mark on the next N articles.
9490 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9491 the process mark instead.  The difference between N and the actual
9492 number of articles marked is returned."
9493   (interactive "P")
9494   (if (and (null n) (gnus-region-active-p))
9495       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9496     (setq n (prefix-numeric-value n))
9497     (let ((backward (< n 0))
9498           (n (abs n)))
9499       (while (and
9500               (> n 0)
9501               (if unmark
9502                   (gnus-summary-remove-process-mark
9503                    (gnus-summary-article-number))
9504                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9505               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9506         (setq n (1- n)))
9507       (when (/= 0 n)
9508         (gnus-message 7 "No more articles"))
9509       (gnus-summary-recenter)
9510       (gnus-summary-position-point)
9511       n)))
9512
9513 (defun gnus-summary-unmark-as-processable (n)
9514   "Remove the process mark from the next N articles.
9515 If N is negative, unmark backward instead.  The difference between N and
9516 the actual number of articles unmarked is returned."
9517   (interactive "P")
9518   (gnus-summary-mark-as-processable n t))
9519
9520 (defun gnus-summary-unmark-all-processable ()
9521   "Remove the process mark from all articles."
9522   (interactive)
9523   (save-excursion
9524     (while gnus-newsgroup-processable
9525       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9526   (gnus-summary-position-point))
9527
9528 (defun gnus-summary-add-mark (article type)
9529   "Mark ARTICLE with a mark of TYPE."
9530   (let ((vtype (car (assq type gnus-article-mark-lists)))
9531         var)
9532     (if (not vtype)
9533         (error "No such mark type: %s" type)
9534       (setq var (intern (format "gnus-newsgroup-%s" type)))
9535       (set var (cons article (symbol-value var)))
9536       (if (memq type '(processable cached replied forwarded recent saved))
9537           (gnus-summary-update-secondary-mark article)
9538         ;;; !!! This is bogus.  We should find out what primary
9539         ;;; !!! mark we want to set.
9540         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9541
9542 (defun gnus-summary-mark-as-expirable (n)
9543   "Mark N articles forward as expirable.
9544 If N is negative, mark backward instead.  The difference between N and
9545 the actual number of articles marked is returned."
9546   (interactive "p")
9547   (gnus-summary-mark-forward n gnus-expirable-mark))
9548
9549 (defun gnus-summary-mark-as-spam (n)
9550   "Mark N articles forward as spam.
9551 If N is negative, mark backward instead.  The difference between N and
9552 the actual number of articles marked is returned."
9553   (interactive "p")
9554   (gnus-summary-mark-forward n gnus-spam-mark))
9555
9556 (defun gnus-summary-mark-article-as-replied (article)
9557   "Mark ARTICLE as replied to and update the summary line.
9558 ARTICLE can also be a list of articles."
9559   (interactive (list (gnus-summary-article-number)))
9560   (let ((articles (if (listp article) article (list article))))
9561     (dolist (article articles)
9562       (unless (numberp article)
9563         (error "%s is not a number" article))
9564       (push article gnus-newsgroup-replied)
9565       (let ((buffer-read-only nil))
9566         (when (gnus-summary-goto-subject article nil t)
9567           (gnus-summary-update-secondary-mark article))))))
9568
9569 (defun gnus-summary-mark-article-as-forwarded (article)
9570   "Mark ARTICLE as forwarded and update the summary line.
9571 ARTICLE can also be a list of articles."
9572   (let ((articles (if (listp article) article (list article))))
9573     (dolist (article articles)
9574       (push article gnus-newsgroup-forwarded)
9575       (let ((buffer-read-only nil))
9576         (when (gnus-summary-goto-subject article nil t)
9577           (gnus-summary-update-secondary-mark article))))))
9578
9579 (defun gnus-summary-set-bookmark (article)
9580   "Set a bookmark in current article."
9581   (interactive (list (gnus-summary-article-number)))
9582   (when (or (not (get-buffer gnus-article-buffer))
9583             (not gnus-current-article)
9584             (not gnus-article-current)
9585             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9586     (error "No current article selected"))
9587   ;; Remove old bookmark, if one exists.
9588   (gnus-pull article gnus-newsgroup-bookmarks)
9589   ;; Set the new bookmark, which is on the form
9590   ;; (article-number . line-number-in-body).
9591   (push
9592    (cons article
9593          (save-excursion
9594            (set-buffer gnus-article-buffer)
9595            (count-lines
9596             (min (point)
9597                  (save-excursion
9598                    (article-goto-body)
9599                    (point)))
9600             (point))))
9601    gnus-newsgroup-bookmarks)
9602   (gnus-message 6 "A bookmark has been added to the current article."))
9603
9604 (defun gnus-summary-remove-bookmark (article)
9605   "Remove the bookmark from the current article."
9606   (interactive (list (gnus-summary-article-number)))
9607   ;; Remove old bookmark, if one exists.
9608   (if (not (assq article gnus-newsgroup-bookmarks))
9609       (gnus-message 6 "No bookmark in current article.")
9610     (gnus-pull article gnus-newsgroup-bookmarks)
9611     (gnus-message 6 "Removed bookmark.")))
9612
9613 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9614 (defun gnus-summary-mark-as-dormant (n)
9615   "Mark N articles forward as dormant.
9616 If N is negative, mark backward instead.  The difference between N and
9617 the actual number of articles marked is returned."
9618   (interactive "p")
9619   (gnus-summary-mark-forward n gnus-dormant-mark))
9620
9621 (defun gnus-summary-set-process-mark (article)
9622   "Set the process mark on ARTICLE and update the summary line."
9623   (setq gnus-newsgroup-processable
9624         (cons article
9625               (delq article gnus-newsgroup-processable)))
9626   (when (gnus-summary-goto-subject article)
9627     (gnus-summary-show-thread)
9628     (gnus-summary-goto-subject article)
9629     (gnus-summary-update-secondary-mark article)))
9630
9631 (defun gnus-summary-remove-process-mark (article)
9632   "Remove the process mark from ARTICLE and update the summary line."
9633   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9634   (when (gnus-summary-goto-subject article)
9635     (gnus-summary-show-thread)
9636     (gnus-summary-goto-subject article)
9637     (gnus-summary-update-secondary-mark article)))
9638
9639 (defun gnus-summary-set-saved-mark (article)
9640   "Set the process mark on ARTICLE and update the summary line."
9641   (push article gnus-newsgroup-saved)
9642   (when (gnus-summary-goto-subject article)
9643     (gnus-summary-update-secondary-mark article)))
9644
9645 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9646   "Mark N articles as read forwards.
9647 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9648 The difference between N and the actual number of articles marked is
9649 returned.
9650 If NO-EXPIRE, auto-expiry will be inhibited."
9651   (interactive "p")
9652   (gnus-summary-show-thread)
9653   (let ((backward (< n 0))
9654         (gnus-summary-goto-unread
9655          (and gnus-summary-goto-unread
9656               (not (eq gnus-summary-goto-unread 'never))
9657               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9658                                     gnus-ticked-mark gnus-dormant-mark)))))
9659         (n (abs n))
9660         (mark (or mark gnus-del-mark)))
9661     (while (and (> n 0)
9662                 (gnus-summary-mark-article nil mark no-expire)
9663                 (zerop (gnus-summary-next-subject
9664                         (if backward -1 1)
9665                         (and gnus-summary-goto-unread
9666                              (not (eq gnus-summary-goto-unread 'never)))
9667                         t)))
9668       (setq n (1- n)))
9669     (when (/= 0 n)
9670       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9671     (gnus-summary-recenter)
9672     (gnus-summary-position-point)
9673     (gnus-set-mode-line 'summary)
9674     n))
9675
9676 (defun gnus-summary-mark-article-as-read (mark)
9677   "Mark the current article quickly as read with MARK."
9678   (let ((article (gnus-summary-article-number)))
9679     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9680     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9681     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9682     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9683     (push (cons article mark) gnus-newsgroup-reads)
9684     ;; Possibly remove from cache, if that is used.
9685     (when gnus-use-cache
9686       (gnus-cache-enter-remove-article article))
9687     ;; Allow the backend to change the mark.
9688     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9689     ;; Check for auto-expiry.
9690     (when (and gnus-newsgroup-auto-expire
9691                (memq mark gnus-auto-expirable-marks))
9692       (setq mark gnus-expirable-mark)
9693       ;; Let the backend know about the mark change.
9694       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9695       (push article gnus-newsgroup-expirable))
9696     ;; Set the mark in the buffer.
9697     (gnus-summary-update-mark mark 'unread)
9698     t))
9699
9700 (defun gnus-summary-mark-article-as-unread (mark)
9701   "Mark the current article quickly as unread with MARK."
9702   (let* ((article (gnus-summary-article-number))
9703          (old-mark (gnus-summary-article-mark article)))
9704     ;; Allow the backend to change the mark.
9705     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9706     (if (eq mark old-mark)
9707         t
9708       (if (<= article 0)
9709           (progn
9710             (gnus-error 1 "Can't mark negative article numbers")
9711             nil)
9712         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9713         (setq gnus-newsgroup-spam-marked
9714               (delq article gnus-newsgroup-spam-marked))
9715         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9716         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9717         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9718         (cond ((= mark gnus-ticked-mark)
9719                (setq gnus-newsgroup-marked
9720                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9721                                               article)))
9722               ((= mark gnus-spam-mark)
9723                (setq gnus-newsgroup-spam-marked
9724                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9725                                               article)))
9726               ((= mark gnus-dormant-mark)
9727                (setq gnus-newsgroup-dormant
9728                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9729                                               article)))
9730               (t
9731                (setq gnus-newsgroup-unreads
9732                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9733                                               article))))
9734         (gnus-pull article gnus-newsgroup-reads)
9735
9736         ;; See whether the article is to be put in the cache.
9737         (and gnus-use-cache
9738              (vectorp (gnus-summary-article-header article))
9739              (save-excursion
9740                (gnus-cache-possibly-enter-article
9741                 gnus-newsgroup-name article
9742                 (= mark gnus-ticked-mark)
9743                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9744
9745         ;; Fix the mark.
9746         (gnus-summary-update-mark mark 'unread)
9747         t))))
9748
9749 (defun gnus-summary-mark-article (&optional article mark no-expire)
9750   "Mark ARTICLE with MARK.  MARK can be any character.
9751 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9752 `??' (dormant) and `?E' (expirable).
9753 If MARK is nil, then the default character `?r' is used.
9754 If ARTICLE is nil, then the article on the current line will be
9755 marked.
9756 If NO-EXPIRE, auto-expiry will be inhibited."
9757   ;; The mark might be a string.
9758   (when (stringp mark)
9759     (setq mark (aref mark 0)))
9760   ;; If no mark is given, then we check auto-expiring.
9761   (when (null mark)
9762     (setq mark gnus-del-mark))
9763   (when (and (not no-expire)
9764              gnus-newsgroup-auto-expire
9765              (memq mark gnus-auto-expirable-marks))
9766     (setq mark gnus-expirable-mark))
9767   (let ((article (or article (gnus-summary-article-number)))
9768         (old-mark (gnus-summary-article-mark article)))
9769     ;; Allow the backend to change the mark.
9770     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9771     (if (eq mark old-mark)
9772         t
9773       (unless article
9774         (error "No article on current line"))
9775       (if (not (if (or (= mark gnus-unread-mark)
9776                        (= mark gnus-ticked-mark)
9777                        (= mark gnus-spam-mark)
9778                        (= mark gnus-dormant-mark))
9779                    (gnus-mark-article-as-unread article mark)
9780                  (gnus-mark-article-as-read article mark)))
9781           t
9782         ;; See whether the article is to be put in the cache.
9783         (and gnus-use-cache
9784              (not (= mark gnus-canceled-mark))
9785              (vectorp (gnus-summary-article-header article))
9786              (save-excursion
9787                (gnus-cache-possibly-enter-article
9788                 gnus-newsgroup-name article
9789                 (= mark gnus-ticked-mark)
9790                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9791
9792         (when (gnus-summary-goto-subject article nil t)
9793           (let ((buffer-read-only nil))
9794             (gnus-summary-show-thread)
9795             ;; Fix the mark.
9796             (gnus-summary-update-mark mark 'unread)
9797             t))))))
9798
9799 (defun gnus-summary-update-secondary-mark (article)
9800   "Update the secondary (read, process, cache) mark."
9801   (gnus-summary-update-mark
9802    (cond ((memq article gnus-newsgroup-processable)
9803           gnus-process-mark)
9804          ((memq article gnus-newsgroup-cached)
9805           gnus-cached-mark)
9806          ((memq article gnus-newsgroup-replied)
9807           gnus-replied-mark)
9808          ((memq article gnus-newsgroup-forwarded)
9809           gnus-forwarded-mark)
9810          ((memq article gnus-newsgroup-saved)
9811           gnus-saved-mark)
9812          ((memq article gnus-newsgroup-recent)
9813           gnus-recent-mark)
9814          ((memq article gnus-newsgroup-unseen)
9815           gnus-unseen-mark)
9816          (t gnus-no-mark))
9817    'replied)
9818   (when (gnus-visual-p 'summary-highlight 'highlight)
9819     (gnus-run-hooks 'gnus-summary-update-hook))
9820   t)
9821
9822 (defun gnus-summary-update-download-mark (article)
9823   "Update the download mark."
9824   (gnus-summary-update-mark
9825    (cond ((memq article gnus-newsgroup-undownloaded) 
9826           gnus-undownloaded-mark)
9827          (gnus-newsgroup-agentized
9828           gnus-downloaded-mark)
9829          (t
9830           gnus-no-mark))
9831    'download)
9832   (gnus-summary-update-line t)
9833   t)
9834
9835 (defun gnus-summary-update-mark (mark type)
9836   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9837         (buffer-read-only nil))
9838     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9839     (when forward
9840       (when (looking-at "\r")
9841         (incf forward))
9842       (when (<= (+ forward (point)) (point-max))
9843         ;; Go to the right position on the line.
9844         (goto-char (+ forward (point)))
9845         ;; Replace the old mark with the new mark.
9846         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9847         ;; Optionally update the marks by some user rule.
9848         (when (eq type 'unread)
9849           (gnus-data-set-mark
9850            (gnus-data-find (gnus-summary-article-number)) mark)
9851           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9852
9853 (defun gnus-mark-article-as-read (article &optional mark)
9854   "Enter ARTICLE in the pertinent lists and remove it from others."
9855   ;; Make the article expirable.
9856   (let ((mark (or mark gnus-del-mark)))
9857     (setq gnus-newsgroup-expirable
9858           (if (= mark gnus-expirable-mark)
9859               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9860             (delq article gnus-newsgroup-expirable)))
9861     ;; Remove from unread and marked lists.
9862     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9863     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9864     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9865     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9866     (push (cons article mark) gnus-newsgroup-reads)
9867     ;; Possibly remove from cache, if that is used.
9868     (when gnus-use-cache
9869       (gnus-cache-enter-remove-article article))
9870     t))
9871
9872 (defun gnus-mark-article-as-unread (article &optional mark)
9873   "Enter ARTICLE in the pertinent lists and remove it from others."
9874   (let ((mark (or mark gnus-ticked-mark)))
9875     (if (<= article 0)
9876         (progn
9877           (gnus-error 1 "Can't mark negative article numbers")
9878           nil)
9879       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9880             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9881             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9882             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9883             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9884
9885       ;; Unsuppress duplicates?
9886       (when gnus-suppress-duplicates
9887         (gnus-dup-unsuppress-article article))
9888
9889       (cond ((= mark gnus-ticked-mark)
9890              (setq gnus-newsgroup-marked
9891                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9892             ((= mark gnus-spam-mark)
9893              (setq gnus-newsgroup-spam-marked
9894                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9895                                             article)))
9896             ((= mark gnus-dormant-mark)
9897              (setq gnus-newsgroup-dormant
9898                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9899             (t
9900              (setq gnus-newsgroup-unreads
9901                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9902       (gnus-pull article gnus-newsgroup-reads)
9903       t)))
9904
9905 (defalias 'gnus-summary-mark-as-unread-forward
9906   'gnus-summary-tick-article-forward)
9907 (make-obsolete 'gnus-summary-mark-as-unread-forward
9908                'gnus-summary-tick-article-forward)
9909 (defun gnus-summary-tick-article-forward (n)
9910   "Tick N articles forwards.
9911 If N is negative, tick backwards instead.
9912 The difference between N and the number of articles ticked is returned."
9913   (interactive "p")
9914   (gnus-summary-mark-forward n gnus-ticked-mark))
9915
9916 (defalias 'gnus-summary-mark-as-unread-backward
9917   'gnus-summary-tick-article-backward)
9918 (make-obsolete 'gnus-summary-mark-as-unread-backward
9919                'gnus-summary-tick-article-backward)
9920 (defun gnus-summary-tick-article-backward (n)
9921   "Tick N articles backwards.
9922 The difference between N and the number of articles ticked is returned."
9923   (interactive "p")
9924   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9925
9926 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9927 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9928 (defun gnus-summary-tick-article (&optional article clear-mark)
9929   "Mark current article as unread.
9930 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9931 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9932   (interactive)
9933   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9934                                        gnus-ticked-mark)))
9935
9936 (defun gnus-summary-mark-as-read-forward (n)
9937   "Mark N articles as read forwards.
9938 If N is negative, mark backwards instead.
9939 The difference between N and the actual number of articles marked is
9940 returned."
9941   (interactive "p")
9942   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9943
9944 (defun gnus-summary-mark-as-read-backward (n)
9945   "Mark the N articles as read backwards.
9946 The difference between N and the actual number of articles marked is
9947 returned."
9948   (interactive "p")
9949   (gnus-summary-mark-forward
9950    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9951
9952 (defun gnus-summary-mark-as-read (&optional article mark)
9953   "Mark current article as read.
9954 ARTICLE specifies the article to be marked as read.
9955 MARK specifies a string to be inserted at the beginning of the line."
9956   (gnus-summary-mark-article article mark))
9957
9958 (defun gnus-summary-clear-mark-forward (n)
9959   "Clear marks from N articles forward.
9960 If N is negative, clear backward instead.
9961 The difference between N and the number of marks cleared is returned."
9962   (interactive "p")
9963   (gnus-summary-mark-forward n gnus-unread-mark))
9964
9965 (defun gnus-summary-clear-mark-backward (n)
9966   "Clear marks from N articles backward.
9967 The difference between N and the number of marks cleared is returned."
9968   (interactive "p")
9969   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9970
9971 (defun gnus-summary-mark-unread-as-read ()
9972   "Intended to be used by `gnus-summary-mark-article-hook'."
9973   (when (memq gnus-current-article gnus-newsgroup-unreads)
9974     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9975
9976 (defun gnus-summary-mark-read-and-unread-as-read ()
9977   "Intended to be used by `gnus-summary-mark-article-hook'."
9978   (let ((mark (gnus-summary-article-mark)))
9979     (when (or (gnus-unread-mark-p mark)
9980               (gnus-read-mark-p mark))
9981       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9982
9983 (defun gnus-summary-mark-unread-as-ticked ()
9984   "Intended to be used by `gnus-summary-mark-article-hook'."
9985   (when (memq gnus-current-article gnus-newsgroup-unreads)
9986     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9987
9988 (defun gnus-summary-mark-region-as-read (point mark all)
9989   "Mark all unread articles between point and mark as read.
9990 If given a prefix, mark all articles between point and mark as read,
9991 even ticked and dormant ones."
9992   (interactive "r\nP")
9993   (save-excursion
9994     (let (article)
9995       (goto-char point)
9996       (beginning-of-line)
9997       (while (and
9998               (< (point) mark)
9999               (progn
10000                 (when (or all
10001                           (memq (setq article (gnus-summary-article-number))
10002                                 gnus-newsgroup-unreads))
10003                   (gnus-summary-mark-article article gnus-del-mark))
10004                 t)
10005               (gnus-summary-find-next))))))
10006
10007 (defun gnus-summary-mark-below (score mark)
10008   "Mark articles with score less than SCORE with MARK."
10009   (interactive "P\ncMark: ")
10010   (setq score (if score
10011                   (prefix-numeric-value score)
10012                 (or gnus-summary-default-score 0)))
10013   (save-excursion
10014     (set-buffer gnus-summary-buffer)
10015     (goto-char (point-min))
10016     (while
10017         (progn
10018           (and (< (gnus-summary-article-score) score)
10019                (gnus-summary-mark-article nil mark))
10020           (gnus-summary-find-next)))))
10021
10022 (defun gnus-summary-kill-below (&optional score)
10023   "Mark articles with score below SCORE as read."
10024   (interactive "P")
10025   (gnus-summary-mark-below score gnus-killed-mark))
10026
10027 (defun gnus-summary-clear-above (&optional score)
10028   "Clear all marks from articles with score above SCORE."
10029   (interactive "P")
10030   (gnus-summary-mark-above score gnus-unread-mark))
10031
10032 (defun gnus-summary-tick-above (&optional score)
10033   "Tick all articles with score above SCORE."
10034   (interactive "P")
10035   (gnus-summary-mark-above score gnus-ticked-mark))
10036
10037 (defun gnus-summary-mark-above (score mark)
10038   "Mark articles with score over SCORE with MARK."
10039   (interactive "P\ncMark: ")
10040   (setq score (if score
10041                   (prefix-numeric-value score)
10042                 (or gnus-summary-default-score 0)))
10043   (save-excursion
10044     (set-buffer gnus-summary-buffer)
10045     (goto-char (point-min))
10046     (while (and (progn
10047                   (when (> (gnus-summary-article-score) score)
10048                     (gnus-summary-mark-article nil mark))
10049                   t)
10050                 (gnus-summary-find-next)))))
10051
10052 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10053 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10054 (defun gnus-summary-limit-include-expunged (&optional no-error)
10055   "Display all the hidden articles that were expunged for low scores."
10056   (interactive)
10057   (let ((buffer-read-only nil))
10058     (let ((scored gnus-newsgroup-scored)
10059           headers h)
10060       (while scored
10061         (unless (gnus-summary-article-header (caar scored))
10062           (and (setq h (gnus-number-to-header (caar scored)))
10063                (< (cdar scored) gnus-summary-expunge-below)
10064                (push h headers)))
10065         (setq scored (cdr scored)))
10066       (if (not headers)
10067           (when (not no-error)
10068             (error "No expunged articles hidden"))
10069         (goto-char (point-min))
10070         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10071         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10072         (mapcar (lambda (x) (push (mail-header-number x)
10073                                   gnus-newsgroup-limit))
10074                 headers)
10075         (gnus-summary-prepare-unthreaded (nreverse headers))
10076         (goto-char (point-min))
10077         (gnus-summary-position-point)
10078         t))))
10079
10080 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10081   "Mark all unread articles in this newsgroup as read.
10082 If prefix argument ALL is non-nil, ticked and dormant articles will
10083 also be marked as read.
10084 If QUIETLY is non-nil, no questions will be asked.
10085 If TO-HERE is non-nil, it should be a point in the buffer.  All
10086 articles before (after, if REVERSE is set) this point will be marked as read.
10087 Note that this function will only catch up the unread article
10088 in the current summary buffer limitation.
10089 The number of articles marked as read is returned."
10090   (interactive "P")
10091   (prog1
10092       (save-excursion
10093         (when (or quietly
10094                   (not gnus-interactive-catchup) ;Without confirmation?
10095                   gnus-expert-user
10096                   (gnus-y-or-n-p
10097                    (if all
10098                        "Mark absolutely all articles as read? "
10099                      "Mark all unread articles as read? ")))
10100           (if (and not-mark
10101                    (not gnus-newsgroup-adaptive)
10102                    (not gnus-newsgroup-auto-expire)
10103                    (not gnus-suppress-duplicates)
10104                    (or (not gnus-use-cache)
10105                        (eq gnus-use-cache 'passive)))
10106               (progn
10107                 (when all
10108                   (setq gnus-newsgroup-marked nil
10109                         gnus-newsgroup-spam-marked nil
10110                         gnus-newsgroup-dormant nil))
10111                 (setq gnus-newsgroup-unreads
10112                       (gnus-sorted-nunion
10113                        (gnus-intersection gnus-newsgroup-unreads
10114                                           gnus-newsgroup-downloadable)
10115                        gnus-newsgroup-unfetched)))
10116             ;; We actually mark all articles as canceled, which we
10117             ;; have to do when using auto-expiry or adaptive scoring.
10118             (gnus-summary-show-all-threads)
10119             (if (and to-here reverse)
10120                 (progn
10121                   (goto-char to-here)
10122                   (while (and
10123                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10124                           (gnus-summary-find-next (not all)))))
10125               (when (gnus-summary-first-subject (not all))
10126                 (while (and
10127                         (if to-here (< (point) to-here) t)
10128                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10129                         (gnus-summary-find-next (not all))))))
10130             (gnus-set-mode-line 'summary))
10131           t))
10132     (gnus-summary-position-point)))
10133
10134 (defun gnus-summary-catchup-to-here (&optional all)
10135   "Mark all unticked articles before the current one as read.
10136 If ALL is non-nil, also mark ticked and dormant articles as read."
10137   (interactive "P")
10138   (save-excursion
10139     (gnus-save-hidden-threads
10140       (let ((beg (point)))
10141         ;; We check that there are unread articles.
10142         (when (or all (gnus-summary-find-prev))
10143           (gnus-summary-catchup all t beg)))))
10144   (gnus-summary-position-point))
10145
10146 (defun gnus-summary-catchup-from-here (&optional all)
10147   "Mark all unticked articles after the current one as read.
10148 If ALL is non-nil, also mark ticked and dormant articles as read."
10149   (interactive "P")
10150   (save-excursion
10151     (gnus-save-hidden-threads
10152       (let ((beg (point)))
10153         ;; We check that there are unread articles.
10154         (when (or all (gnus-summary-find-next))
10155           (gnus-summary-catchup all t beg nil t)))))
10156
10157   (gnus-summary-position-point))
10158 (defun gnus-summary-catchup-all (&optional quietly)
10159   "Mark all articles in this newsgroup as read.
10160 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10161 instead, which marks only unread articles as read."
10162   (interactive "P")
10163   (gnus-summary-catchup t quietly))
10164
10165 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10166   "Mark all unread articles in this group as read, then exit.
10167 If prefix argument ALL is non-nil, all articles are marked as read.
10168 If QUIETLY is non-nil, no questions will be asked."
10169   (interactive "P")
10170   (when (gnus-summary-catchup all quietly nil 'fast)
10171     ;; Select next newsgroup or exit.
10172     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10173              (eq gnus-auto-select-next 'quietly))
10174         (gnus-summary-next-group nil)
10175       (gnus-summary-exit))))
10176
10177 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10178   "Mark all articles in this newsgroup as read, and then exit.
10179 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10180 instead, which marks only unread articles as read."
10181   (interactive "P")
10182   (gnus-summary-catchup-and-exit t quietly))
10183
10184 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10185   "Mark all articles in this group as read and select the next group.
10186 If given a prefix, mark all articles, unread as well as ticked, as
10187 read."
10188   (interactive "P")
10189   (save-excursion
10190     (gnus-summary-catchup all))
10191   (gnus-summary-next-group))
10192
10193 ;;;
10194 ;;; with article
10195 ;;;
10196
10197 (defmacro gnus-with-article (article &rest forms)
10198   "Select ARTICLE and perform FORMS in the original article buffer.
10199 Then replace the article with the result."
10200   `(progn
10201      ;; We don't want the article to be marked as read.
10202      (let (gnus-mark-article-hook)
10203        (gnus-summary-select-article t t nil ,article))
10204      (set-buffer gnus-original-article-buffer)
10205      ,@forms
10206      (if (not (gnus-check-backend-function
10207                'request-replace-article (car gnus-article-current)))
10208          (gnus-message 5 "Read-only group; not replacing")
10209        (unless (gnus-request-replace-article
10210                 ,article (car gnus-article-current)
10211                 (current-buffer) t)
10212          (error "Couldn't replace article")))
10213      ;; The cache and backlog have to be flushed somewhat.
10214      (when gnus-keep-backlog
10215        (gnus-backlog-remove-article
10216         (car gnus-article-current) (cdr gnus-article-current)))
10217      (when gnus-use-cache
10218        (gnus-cache-update-article
10219         (car gnus-article-current) (cdr gnus-article-current)))))
10220
10221 (put 'gnus-with-article 'lisp-indent-function 1)
10222 (put 'gnus-with-article 'edebug-form-spec '(form body))
10223
10224 ;; Thread-based commands.
10225
10226 (defun gnus-summary-articles-in-thread (&optional article)
10227   "Return a list of all articles in the current thread.
10228 If ARTICLE is non-nil, return all articles in the thread that starts
10229 with that article."
10230   (let* ((article (or article (gnus-summary-article-number)))
10231          (data (gnus-data-find-list article))
10232          (top-level (gnus-data-level (car data)))
10233          (top-subject
10234           (cond ((null gnus-thread-operation-ignore-subject)
10235                  (gnus-simplify-subject-re
10236                   (mail-header-subject (gnus-data-header (car data)))))
10237                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10238                  (gnus-simplify-subject-fuzzy
10239                   (mail-header-subject (gnus-data-header (car data)))))
10240                 (t nil)))
10241          (end-point (save-excursion
10242                       (if (gnus-summary-go-to-next-thread)
10243                           (point) (point-max))))
10244          articles)
10245     (while (and data
10246                 (< (gnus-data-pos (car data)) end-point))
10247       (when (or (not top-subject)
10248                 (string= top-subject
10249                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10250                              (gnus-simplify-subject-fuzzy
10251                               (mail-header-subject
10252                                (gnus-data-header (car data))))
10253                            (gnus-simplify-subject-re
10254                             (mail-header-subject
10255                              (gnus-data-header (car data)))))))
10256         (push (gnus-data-number (car data)) articles))
10257       (unless (and (setq data (cdr data))
10258                    (> (gnus-data-level (car data)) top-level))
10259         (setq data nil)))
10260     ;; Return the list of articles.
10261     (nreverse articles)))
10262
10263 (defun gnus-summary-rethread-current ()
10264   "Rethread the thread the current article is part of."
10265   (interactive)
10266   (let* ((gnus-show-threads t)
10267          (article (gnus-summary-article-number))
10268          (id (mail-header-id (gnus-summary-article-header)))
10269          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10270     (unless id
10271       (error "No article on the current line"))
10272     (gnus-rebuild-thread id)
10273     (gnus-summary-goto-subject article)))
10274
10275 (defun gnus-summary-reparent-thread ()
10276   "Make the current article child of the marked (or previous) article.
10277
10278 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10279 is non-nil or the Subject: of both articles are the same."
10280   (interactive)
10281   (unless (not (gnus-group-read-only-p))
10282     (error "The current newsgroup does not support article editing"))
10283   (unless (<= (length gnus-newsgroup-processable) 1)
10284     (error "No more than one article may be marked"))
10285   (save-window-excursion
10286     (let ((gnus-article-buffer " *reparent*")
10287           (current-article (gnus-summary-article-number))
10288           ;; First grab the marked article, otherwise one line up.
10289           (parent-article (if (not (null gnus-newsgroup-processable))
10290                               (car gnus-newsgroup-processable)
10291                             (save-excursion
10292                               (if (eq (forward-line -1) 0)
10293                                   (gnus-summary-article-number)
10294                                 (error "Beginning of summary buffer"))))))
10295       (unless (not (eq current-article parent-article))
10296         (error "An article may not be self-referential"))
10297       (let ((message-id (mail-header-id
10298                          (gnus-summary-article-header parent-article))))
10299         (unless (and message-id (not (equal message-id "")))
10300           (error "No message-id in desired parent"))
10301         (gnus-with-article current-article
10302           (save-restriction
10303             (goto-char (point-min))
10304             (message-narrow-to-head)
10305             (if (re-search-forward "^References: " nil t)
10306                 (progn
10307                   (re-search-forward "^[^ \t]" nil t)
10308                   (forward-line -1)
10309                   (end-of-line)
10310                   (insert " " message-id))
10311               (insert "References: " message-id "\n"))))
10312         (set-buffer gnus-summary-buffer)
10313         (gnus-summary-unmark-all-processable)
10314         (gnus-summary-update-article current-article)
10315         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10316             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10317         (gnus-summary-rethread-current)
10318         (gnus-message 3 "Article %d is now the child of article %d"
10319                       current-article parent-article)))))
10320
10321 (defun gnus-summary-toggle-threads (&optional arg)
10322   "Toggle showing conversation threads.
10323 If ARG is positive number, turn showing conversation threads on."
10324   (interactive "P")
10325   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10326     (setq gnus-show-threads
10327           (if (null arg) (not gnus-show-threads)
10328             (> (prefix-numeric-value arg) 0)))
10329     (gnus-summary-prepare)
10330     (gnus-summary-goto-subject current)
10331     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10332     (gnus-summary-position-point)))
10333
10334 (defun gnus-summary-show-all-threads ()
10335   "Show all threads."
10336   (interactive)
10337   (save-excursion
10338     (let ((buffer-read-only nil))
10339       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10340   (gnus-summary-position-point))
10341
10342 (defun gnus-summary-show-thread ()
10343   "Show thread subtrees.
10344 Returns nil if no thread was there to be shown."
10345   (interactive)
10346   (let ((buffer-read-only nil)
10347         (orig (point))
10348         (end (gnus-point-at-eol))
10349         ;; Leave point at bol
10350         (beg (progn (beginning-of-line) (point))))
10351     (prog1
10352         ;; Any hidden lines here?
10353         (search-forward "\r" end t)
10354       (subst-char-in-region beg end ?\^M ?\n t)
10355       (goto-char orig)
10356       (gnus-summary-position-point))))
10357
10358 (defun gnus-summary-maybe-hide-threads ()
10359   "If requested, hide the threads that should be hidden."
10360   (when (and gnus-show-threads
10361              gnus-thread-hide-subtree)
10362     (gnus-summary-hide-all-threads
10363      (if (or (consp gnus-thread-hide-subtree)
10364              (gnus-functionp gnus-thread-hide-subtree))
10365          (gnus-make-predicate gnus-thread-hide-subtree)
10366        nil))))
10367
10368 ;;; Hiding predicates.
10369
10370 (defun gnus-article-unread-p (header)
10371   (memq (mail-header-number header) gnus-newsgroup-unreads))
10372
10373 (defun gnus-article-unseen-p (header)
10374   (memq (mail-header-number header) gnus-newsgroup-unseen))
10375
10376 (defun gnus-map-articles (predicate articles)
10377   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10378   (apply 'gnus-or (mapcar predicate
10379                           (mapcar 'gnus-summary-article-header articles))))
10380
10381 (defun gnus-summary-hide-all-threads (&optional predicate)
10382   "Hide all thread subtrees.
10383 If PREDICATE is supplied, threads that satisfy this predicate
10384 will not be hidden."
10385   (interactive)
10386   (save-excursion
10387     (goto-char (point-min))
10388     (let ((end nil))
10389       (while (not end)
10390         (when (or (not predicate)
10391                   (gnus-map-articles
10392                    predicate (gnus-summary-article-children)))
10393             (gnus-summary-hide-thread))
10394         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10395   (gnus-summary-position-point))
10396
10397 (defun gnus-summary-hide-thread ()
10398   "Hide thread subtrees.
10399 If PREDICATE is supplied, threads that satisfy this predicate
10400 will not be hidden.
10401 Returns nil if no threads were there to be hidden."
10402   (interactive)
10403   (let ((buffer-read-only nil)
10404         (start (point))
10405         (article (gnus-summary-article-number)))
10406     (goto-char start)
10407     ;; Go forward until either the buffer ends or the subthread
10408     ;; ends.
10409     (when (and (not (eobp))
10410                (or (zerop (gnus-summary-next-thread 1 t))
10411                    (goto-char (point-max))))
10412       (prog1
10413           (if (and (> (point) start)
10414                    (search-backward "\n" start t))
10415               (progn
10416                 (subst-char-in-region start (point) ?\n ?\^M)
10417                 (gnus-summary-goto-subject article))
10418             (goto-char start)
10419             nil)))))
10420
10421 (defun gnus-summary-go-to-next-thread (&optional previous)
10422   "Go to the same level (or less) next thread.
10423 If PREVIOUS is non-nil, go to previous thread instead.
10424 Return the article number moved to, or nil if moving was impossible."
10425   (let ((level (gnus-summary-thread-level))
10426         (way (if previous -1 1))
10427         (beg (point)))
10428     (forward-line way)
10429     (while (and (not (eobp))
10430                 (< level (gnus-summary-thread-level)))
10431       (forward-line way))
10432     (if (eobp)
10433         (progn
10434           (goto-char beg)
10435           nil)
10436       (setq beg (point))
10437       (prog1
10438           (gnus-summary-article-number)
10439         (goto-char beg)))))
10440
10441 (defun gnus-summary-next-thread (n &optional silent)
10442   "Go to the same level next N'th thread.
10443 If N is negative, search backward instead.
10444 Returns the difference between N and the number of skips actually
10445 done.
10446
10447 If SILENT, don't output messages."
10448   (interactive "p")
10449   (let ((backward (< n 0))
10450         (n (abs n)))
10451     (while (and (> n 0)
10452                 (gnus-summary-go-to-next-thread backward))
10453       (decf n))
10454     (unless silent
10455       (gnus-summary-position-point))
10456     (when (and (not silent) (/= 0 n))
10457       (gnus-message 7 "No more threads"))
10458     n))
10459
10460 (defun gnus-summary-prev-thread (n)
10461   "Go to the same level previous N'th thread.
10462 Returns the difference between N and the number of skips actually
10463 done."
10464   (interactive "p")
10465   (gnus-summary-next-thread (- n)))
10466
10467 (defun gnus-summary-go-down-thread ()
10468   "Go down one level in the current thread."
10469   (let ((children (gnus-summary-article-children)))
10470     (when children
10471       (gnus-summary-goto-subject (car children)))))
10472
10473 (defun gnus-summary-go-up-thread ()
10474   "Go up one level in the current thread."
10475   (let ((parent (gnus-summary-article-parent)))
10476     (when parent
10477       (gnus-summary-goto-subject parent))))
10478
10479 (defun gnus-summary-down-thread (n)
10480   "Go down thread N steps.
10481 If N is negative, go up instead.
10482 Returns the difference between N and how many steps down that were
10483 taken."
10484   (interactive "p")
10485   (let ((up (< n 0))
10486         (n (abs n)))
10487     (while (and (> n 0)
10488                 (if up (gnus-summary-go-up-thread)
10489                   (gnus-summary-go-down-thread)))
10490       (setq n (1- n)))
10491     (gnus-summary-position-point)
10492     (when (/= 0 n)
10493       (gnus-message 7 "Can't go further"))
10494     n))
10495
10496 (defun gnus-summary-up-thread (n)
10497   "Go up thread N steps.
10498 If N is negative, go down instead.
10499 Returns the difference between N and how many steps down that were
10500 taken."
10501   (interactive "p")
10502   (gnus-summary-down-thread (- n)))
10503
10504 (defun gnus-summary-top-thread ()
10505   "Go to the top of the thread."
10506   (interactive)
10507   (while (gnus-summary-go-up-thread))
10508   (gnus-summary-article-number))
10509
10510 (defun gnus-summary-kill-thread (&optional unmark)
10511   "Mark articles under current thread as read.
10512 If the prefix argument is positive, remove any kinds of marks.
10513 If the prefix argument is negative, tick articles instead."
10514   (interactive "P")
10515   (when unmark
10516     (setq unmark (prefix-numeric-value unmark)))
10517   (let ((articles (gnus-summary-articles-in-thread)))
10518     (save-excursion
10519       ;; Expand the thread.
10520       (gnus-summary-show-thread)
10521       ;; Mark all the articles.
10522       (while articles
10523         (gnus-summary-goto-subject (car articles))
10524         (cond ((null unmark)
10525                (gnus-summary-mark-article-as-read gnus-killed-mark))
10526               ((> unmark 0)
10527                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10528               (t
10529                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10530         (setq articles (cdr articles))))
10531     ;; Hide killed subtrees.
10532     (and (null unmark)
10533          gnus-thread-hide-killed
10534          (gnus-summary-hide-thread))
10535     ;; If marked as read, go to next unread subject.
10536     (when (null unmark)
10537       ;; Go to next unread subject.
10538       (gnus-summary-next-subject 1 t)))
10539   (gnus-set-mode-line 'summary))
10540
10541 ;; Summary sorting commands
10542
10543 (defun gnus-summary-sort-by-number (&optional reverse)
10544   "Sort the summary buffer by article number.
10545 Argument REVERSE means reverse order."
10546   (interactive "P")
10547   (gnus-summary-sort 'number reverse))
10548
10549 (defun gnus-summary-sort-by-random (&optional reverse)
10550   "Randomize the order in the summary buffer.
10551 Argument REVERSE means to randomize in reverse order."
10552   (interactive "P")
10553   (gnus-summary-sort 'random reverse))
10554
10555 (defun gnus-summary-sort-by-author (&optional reverse)
10556   "Sort the summary buffer by author name alphabetically.
10557 If `case-fold-search' is non-nil, case of letters is ignored.
10558 Argument REVERSE means reverse order."
10559   (interactive "P")
10560   (gnus-summary-sort 'author reverse))
10561
10562 (defun gnus-summary-sort-by-subject (&optional reverse)
10563   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10564 If `case-fold-search' is non-nil, case of letters is ignored.
10565 Argument REVERSE means reverse order."
10566   (interactive "P")
10567   (gnus-summary-sort 'subject reverse))
10568
10569 (defun gnus-summary-sort-by-date (&optional reverse)
10570   "Sort the summary buffer by date.
10571 Argument REVERSE means reverse order."
10572   (interactive "P")
10573   (gnus-summary-sort 'date reverse))
10574
10575 (defun gnus-summary-sort-by-score (&optional reverse)
10576   "Sort the summary buffer by score.
10577 Argument REVERSE means reverse order."
10578   (interactive "P")
10579   (gnus-summary-sort 'score reverse))
10580
10581 (defun gnus-summary-sort-by-lines (&optional reverse)
10582   "Sort the summary buffer by the number of lines.
10583 Argument REVERSE means reverse order."
10584   (interactive "P")
10585   (gnus-summary-sort 'lines reverse))
10586
10587 (defun gnus-summary-sort-by-chars (&optional reverse)
10588   "Sort the summary buffer by article length.
10589 Argument REVERSE means reverse order."
10590   (interactive "P")
10591   (gnus-summary-sort 'chars reverse))
10592
10593 (defun gnus-summary-sort-by-original (&optional reverse)
10594   "Sort the summary buffer using the default sorting method.
10595 Argument REVERSE means reverse order."
10596   (interactive "P")
10597   (let* ((buffer-read-only)
10598          (gnus-summary-prepare-hook nil))
10599     ;; We do the sorting by regenerating the threads.
10600     (gnus-summary-prepare)
10601     ;; Hide subthreads if needed.
10602     (gnus-summary-maybe-hide-threads)))
10603
10604 (defun gnus-summary-sort (predicate reverse)
10605   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10606   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10607          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10608          (gnus-thread-sort-functions
10609           (if (not reverse)
10610               thread
10611             `(lambda (t1 t2)
10612                (,thread t2 t1))))
10613          (gnus-sort-gathered-threads-function
10614           gnus-thread-sort-functions)
10615          (gnus-article-sort-functions
10616           (if (not reverse)
10617               article
10618             `(lambda (t1 t2)
10619                (,article t2 t1))))
10620          (buffer-read-only)
10621          (gnus-summary-prepare-hook nil))
10622     ;; We do the sorting by regenerating the threads.
10623     (gnus-summary-prepare)
10624     ;; Hide subthreads if needed.
10625     (gnus-summary-maybe-hide-threads)))
10626
10627 ;; Summary saving commands.
10628
10629 (defun gnus-summary-save-article (&optional n not-saved)
10630   "Save the current article using the default saver function.
10631 If N is a positive number, save the N next articles.
10632 If N is a negative number, save the N previous articles.
10633 If N is nil and any articles have been marked with the process mark,
10634 save those articles instead.
10635 The variable `gnus-default-article-saver' specifies the saver function."
10636   (interactive "P")
10637   (let* ((articles (gnus-summary-work-articles n))
10638          (save-buffer (save-excursion
10639                         (nnheader-set-temp-buffer " *Gnus Save*")))
10640          (num (length articles))
10641          header file)
10642     (dolist (article articles)
10643       (setq header (gnus-summary-article-header article))
10644       (if (not (vectorp header))
10645           ;; This is a pseudo-article.
10646           (if (assq 'name header)
10647               (gnus-copy-file (cdr (assq 'name header)))
10648             (gnus-message 1 "Article %d is unsaveable" article))
10649         ;; This is a real article.
10650         (save-window-excursion
10651           (let ((gnus-display-mime-function nil)
10652                 (gnus-article-prepare-hook nil))
10653             (gnus-summary-select-article t nil nil article)))
10654         (save-excursion
10655           (set-buffer save-buffer)
10656           (erase-buffer)
10657           (insert-buffer-substring gnus-original-article-buffer))
10658         (setq file (gnus-article-save save-buffer file num))
10659         (gnus-summary-remove-process-mark article)
10660         (unless not-saved
10661           (gnus-summary-set-saved-mark article))))
10662     (gnus-kill-buffer save-buffer)
10663     (gnus-summary-position-point)
10664     (gnus-set-mode-line 'summary)
10665     n))
10666
10667 (defun gnus-summary-pipe-output (&optional arg headers)
10668   "Pipe the current article to a subprocess.
10669 If N is a positive number, pipe the N next articles.
10670 If N is a negative number, pipe the N previous articles.
10671 If N is nil and any articles have been marked with the process mark,
10672 pipe those articles instead.
10673 If HEADERS (the symbolic prefix), include the headers, too."
10674   (interactive (gnus-interactive "P\ny"))
10675   (require 'gnus-art)
10676   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10677         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10678     (gnus-summary-save-article arg t))
10679   (let ((buffer (get-buffer "*Shell Command Output*")))
10680     (when (and buffer
10681                (not (zerop (buffer-size buffer))))
10682       (gnus-configure-windows 'pipe))))
10683
10684 (defun gnus-summary-save-article-mail (&optional arg)
10685   "Append the current article to an mail file.
10686 If N is a positive number, save the N next articles.
10687 If N is a negative number, save the N previous articles.
10688 If N is nil and any articles have been marked with the process mark,
10689 save those articles instead."
10690   (interactive "P")
10691   (require 'gnus-art)
10692   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10693     (gnus-summary-save-article arg)))
10694
10695 (defun gnus-summary-save-article-rmail (&optional arg)
10696   "Append the current article to an rmail file.
10697 If N is a positive number, save the N next articles.
10698 If N is a negative number, save the N previous articles.
10699 If N is nil and any articles have been marked with the process mark,
10700 save those articles instead."
10701   (interactive "P")
10702   (require 'gnus-art)
10703   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10704     (gnus-summary-save-article arg)))
10705
10706 (defun gnus-summary-save-article-file (&optional arg)
10707   "Append the current article to a file.
10708 If N is a positive number, save the N next articles.
10709 If N is a negative number, save the N previous articles.
10710 If N is nil and any articles have been marked with the process mark,
10711 save those articles instead."
10712   (interactive "P")
10713   (require 'gnus-art)
10714   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10715     (gnus-summary-save-article arg)))
10716
10717 (defun gnus-summary-write-article-file (&optional arg)
10718   "Write the current article to a file, deleting the previous file.
10719 If N is a positive number, save the N next articles.
10720 If N is a negative number, save the N previous articles.
10721 If N is nil and any articles have been marked with the process mark,
10722 save those articles instead."
10723   (interactive "P")
10724   (require 'gnus-art)
10725   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10726     (gnus-summary-save-article arg)))
10727
10728 (defun gnus-summary-save-article-body-file (&optional arg)
10729   "Append the current article body to a file.
10730 If N is a positive number, save the N next articles.
10731 If N is a negative number, save the N previous articles.
10732 If N is nil and any articles have been marked with the process mark,
10733 save those articles instead."
10734   (interactive "P")
10735   (require 'gnus-art)
10736   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10737     (gnus-summary-save-article arg)))
10738
10739 (defun gnus-summary-muttprint (&optional arg)
10740   "Print the current article using Muttprint.
10741 If N is a positive number, save the N next articles.
10742 If N is a negative number, save the N previous articles.
10743 If N is nil and any articles have been marked with the process mark,
10744 save those articles instead."
10745   (interactive "P")
10746   (require 'gnus-art)
10747   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10748     (gnus-summary-save-article arg t)))
10749
10750 (defun gnus-summary-pipe-message (program)
10751   "Pipe the current article through PROGRAM."
10752   (interactive "sProgram: ")
10753   (gnus-summary-select-article)
10754   (let ((mail-header-separator ""))
10755     (gnus-eval-in-buffer-window gnus-article-buffer
10756       (save-restriction
10757         (widen)
10758         (let ((start (window-start))
10759               buffer-read-only)
10760           (message-pipe-buffer-body program)
10761           (set-window-start (get-buffer-window (current-buffer)) start))))))
10762
10763 (defun gnus-get-split-value (methods)
10764   "Return a value based on the split METHODS."
10765   (let (split-name method result match)
10766     (when methods
10767       (save-excursion
10768         (set-buffer gnus-original-article-buffer)
10769         (save-restriction
10770           (nnheader-narrow-to-headers)
10771           (while (and methods (not split-name))
10772             (goto-char (point-min))
10773             (setq method (pop methods))
10774             (setq match (car method))
10775             (when (cond
10776                    ((stringp match)
10777                     ;; Regular expression.
10778                     (ignore-errors
10779                       (re-search-forward match nil t)))
10780                    ((gnus-functionp match)
10781                     ;; Function.
10782                     (save-restriction
10783                       (widen)
10784                       (setq result (funcall match gnus-newsgroup-name))))
10785                    ((consp match)
10786                     ;; Form.
10787                     (save-restriction
10788                       (widen)
10789                       (setq result (eval match)))))
10790               (setq split-name (cdr method))
10791               (cond ((stringp result)
10792                      (push (expand-file-name
10793                             result gnus-article-save-directory)
10794                            split-name))
10795                     ((consp result)
10796                      (setq split-name (append result split-name)))))))))
10797     (nreverse split-name)))
10798
10799 (defun gnus-valid-move-group-p (group)
10800   (and (boundp group)
10801        (symbol-name group)
10802        (symbol-value group)
10803        (gnus-get-function (gnus-find-method-for-group
10804                            (symbol-name group)) 'request-accept-article t)))
10805
10806 (defun gnus-read-move-group-name (prompt default articles prefix)
10807   "Read a group name."
10808   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10809          (minibuffer-confirm-incomplete nil) ; XEmacs
10810          (prom
10811           (format "%s %s to:"
10812                   prompt
10813                   (if (> (length articles) 1)
10814                       (format "these %d articles" (length articles))
10815                     "this article")))
10816          (to-newsgroup
10817           (cond
10818            ((null split-name)
10819             (gnus-completing-read-with-default
10820              default prom
10821              gnus-active-hashtb
10822              'gnus-valid-move-group-p
10823              nil prefix
10824              'gnus-group-history))
10825            ((= 1 (length split-name))
10826             (gnus-completing-read-with-default
10827              (car split-name) prom
10828              gnus-active-hashtb
10829              'gnus-valid-move-group-p
10830              nil nil
10831              'gnus-group-history))
10832            (t
10833             (gnus-completing-read-with-default
10834              nil prom
10835              (mapcar (lambda (el) (list el))
10836                      (nreverse split-name))
10837              nil nil nil
10838              'gnus-group-history))))
10839          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10840     (when to-newsgroup
10841       (if (or (string= to-newsgroup "")
10842               (string= to-newsgroup prefix))
10843           (setq to-newsgroup default))
10844       (unless to-newsgroup
10845         (error "No group name entered"))
10846       (or (gnus-active to-newsgroup)
10847           (gnus-activate-group to-newsgroup nil nil to-method)
10848           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10849                                      to-newsgroup))
10850               (or (and (gnus-request-create-group to-newsgroup to-method)
10851                        (gnus-activate-group
10852                         to-newsgroup nil nil to-method)
10853                        (gnus-subscribe-group to-newsgroup))
10854                   (error "Couldn't create group %s" to-newsgroup)))
10855           (error "No such group: %s" to-newsgroup)))
10856     to-newsgroup))
10857
10858 (defun gnus-summary-save-parts (type dir n &optional reverse)
10859   "Save parts matching TYPE to DIR.
10860 If REVERSE, save parts that do not match TYPE."
10861   (interactive
10862    (list (read-string "Save parts of type: "
10863                       (or (car gnus-summary-save-parts-type-history)
10864                           gnus-summary-save-parts-default-mime)
10865                       'gnus-summary-save-parts-type-history)
10866          (setq gnus-summary-save-parts-last-directory
10867                (read-file-name "Save to directory: "
10868                                gnus-summary-save-parts-last-directory
10869                                nil t))
10870          current-prefix-arg))
10871   (gnus-summary-iterate n
10872     (let ((gnus-display-mime-function nil)
10873           (gnus-inhibit-treatment t))
10874       (gnus-summary-select-article))
10875     (save-excursion
10876       (set-buffer gnus-article-buffer)
10877       (let ((handles (or gnus-article-mime-handles
10878                          (mm-dissect-buffer nil gnus-article-loose-mime)
10879                          (and gnus-article-emulate-mime
10880                               (mm-uu-dissect)))))
10881         (when handles
10882           (gnus-summary-save-parts-1 type dir handles reverse)
10883           (unless gnus-article-mime-handles ;; Don't destroy this case.
10884             (mm-destroy-parts handles)))))))
10885
10886 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10887   (if (stringp (car handle))
10888       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10889               (cdr handle))
10890     (when (if reverse
10891               (not (string-match type (mm-handle-media-type handle)))
10892             (string-match type (mm-handle-media-type handle)))
10893       (let ((file (expand-file-name
10894                    (file-name-nondirectory
10895                     (or
10896                      (mail-content-type-get
10897                       (mm-handle-disposition handle) 'filename)
10898                      (concat gnus-newsgroup-name
10899                              "." (number-to-string
10900                                   (cdr gnus-article-current)))))
10901                    dir)))
10902         (unless (file-exists-p file)
10903           (mm-save-part-to-file handle file))))))
10904
10905 ;; Summary extract commands
10906
10907 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10908   (let ((buffer-read-only nil)
10909         (article (gnus-summary-article-number))
10910         after-article b e)
10911     (unless (gnus-summary-goto-subject article)
10912       (error "No such article: %d" article))
10913     (gnus-summary-position-point)
10914     ;; If all commands are to be bunched up on one line, we collect
10915     ;; them here.
10916     (unless gnus-view-pseudos-separately
10917       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10918             files action)
10919         (while ps
10920           (setq action (cdr (assq 'action (car ps))))
10921           (setq files (list (cdr (assq 'name (car ps)))))
10922           (while (and ps (cdr ps)
10923                       (string= (or action "1")
10924                                (or (cdr (assq 'action (cadr ps))) "2")))
10925             (push (cdr (assq 'name (cadr ps))) files)
10926             (setcdr ps (cddr ps)))
10927           (when files
10928             (when (not (string-match "%s" action))
10929               (push " " files))
10930             (push " " files)
10931             (when (assq 'execute (car ps))
10932               (setcdr (assq 'execute (car ps))
10933                       (funcall (if (string-match "%s" action)
10934                                    'format 'concat)
10935                                action
10936                                (mapconcat
10937                                 (lambda (f)
10938                                   (if (equal f " ")
10939                                       f
10940                                     (mm-quote-arg f)))
10941                                 files " ")))))
10942           (setq ps (cdr ps)))))
10943     (if (and gnus-view-pseudos (not not-view))
10944         (while pslist
10945           (when (assq 'execute (car pslist))
10946             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10947                                   (eq gnus-view-pseudos 'not-confirm)))
10948           (setq pslist (cdr pslist)))
10949       (save-excursion
10950         (while pslist
10951           (setq after-article (or (cdr (assq 'article (car pslist)))
10952                                   (gnus-summary-article-number)))
10953           (gnus-summary-goto-subject after-article)
10954           (forward-line 1)
10955           (setq b (point))
10956           (insert "    " (file-name-nondirectory
10957                           (cdr (assq 'name (car pslist))))
10958                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10959           (setq e (point))
10960           (forward-line -1)             ; back to `b'
10961           (gnus-add-text-properties
10962            b (1- e) (list 'gnus-number gnus-reffed-article-number
10963                           gnus-mouse-face-prop gnus-mouse-face))
10964           (gnus-data-enter
10965            after-article gnus-reffed-article-number
10966            gnus-unread-mark b (car pslist) 0 (- e b))
10967           (setq gnus-newsgroup-unreads
10968                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10969                                          gnus-reffed-article-number))
10970           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10971           (setq pslist (cdr pslist)))))))
10972
10973 (defun gnus-pseudos< (p1 p2)
10974   (let ((c1 (cdr (assq 'action p1)))
10975         (c2 (cdr (assq 'action p2))))
10976     (and c1 c2 (string< c1 c2))))
10977
10978 (defun gnus-request-pseudo-article (props)
10979   (cond ((assq 'execute props)
10980          (gnus-execute-command (cdr (assq 'execute props)))))
10981   (let ((gnus-current-article (gnus-summary-article-number)))
10982     (gnus-run-hooks 'gnus-mark-article-hook)))
10983
10984 (defun gnus-execute-command (command &optional automatic)
10985   (save-excursion
10986     (gnus-article-setup-buffer)
10987     (set-buffer gnus-article-buffer)
10988     (setq buffer-read-only nil)
10989     (let ((command (if automatic command
10990                      (read-string "Command: " (cons command 0)))))
10991       (erase-buffer)
10992       (insert "$ " command "\n\n")
10993       (if gnus-view-pseudo-asynchronously
10994           (start-process "gnus-execute" (current-buffer) shell-file-name
10995                          shell-command-switch command)
10996         (call-process shell-file-name nil t nil
10997                       shell-command-switch command)))))
10998
10999 ;; Summary kill commands.
11000
11001 (defun gnus-summary-edit-global-kill (article)
11002   "Edit the \"global\" kill file."
11003   (interactive (list (gnus-summary-article-number)))
11004   (gnus-group-edit-global-kill article))
11005
11006 (defun gnus-summary-edit-local-kill ()
11007   "Edit a local kill file applied to the current newsgroup."
11008   (interactive)
11009   (setq gnus-current-headers (gnus-summary-article-header))
11010   (gnus-group-edit-local-kill
11011    (gnus-summary-article-number) gnus-newsgroup-name))
11012
11013 ;;; Header reading.
11014
11015 (defun gnus-read-header (id &optional header)
11016   "Read the headers of article ID and enter them into the Gnus system."
11017   (let ((group gnus-newsgroup-name)
11018         (gnus-override-method
11019          (or
11020           gnus-override-method
11021           (and (gnus-news-group-p gnus-newsgroup-name)
11022                (car (gnus-refer-article-methods)))))
11023         where)
11024     ;; First we check to see whether the header in question is already
11025     ;; fetched.
11026     (if (stringp id)
11027         ;; This is a Message-ID.
11028         (setq header (or header (gnus-id-to-header id)))
11029       ;; This is an article number.
11030       (setq header (or header (gnus-summary-article-header id))))
11031     (if (and header
11032              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11033         ;; We have found the header.
11034         header
11035       ;; If this is a sparse article, we have to nix out its
11036       ;; previous entry in the thread hashtb.
11037       (when (and header
11038                  (gnus-summary-article-sparse-p (mail-header-number header)))
11039         (let* ((parent (gnus-parent-id (mail-header-references header)))
11040                (thread (and parent (gnus-id-to-thread parent))))
11041           (when thread
11042             (delq (assq header thread) thread))))
11043       ;; We have to really fetch the header to this article.
11044       (save-excursion
11045         (set-buffer nntp-server-buffer)
11046         (when (setq where (gnus-request-head id group))
11047           (nnheader-fold-continuation-lines)
11048           (goto-char (point-max))
11049           (insert ".\n")
11050           (goto-char (point-min))
11051           (insert "211 ")
11052           (princ (cond
11053                   ((numberp id) id)
11054                   ((cdr where) (cdr where))
11055                   (header (mail-header-number header))
11056                   (t gnus-reffed-article-number))
11057                  (current-buffer))
11058           (insert " Article retrieved.\n"))
11059         (if (or (not where)
11060                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11061             ()                          ; Malformed head.
11062           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11063             (when (and (stringp id)
11064                        (not (string= (gnus-group-real-name group)
11065                                      (car where))))
11066               ;; If we fetched by Message-ID and the article came
11067               ;; from a different group, we fudge some bogus article
11068               ;; numbers for this article.
11069               (mail-header-set-number header gnus-reffed-article-number))
11070             (save-excursion
11071               (set-buffer gnus-summary-buffer)
11072               (decf gnus-reffed-article-number)
11073               (gnus-remove-header (mail-header-number header))
11074               (push header gnus-newsgroup-headers)
11075               (setq gnus-current-headers header)
11076               (push (mail-header-number header) gnus-newsgroup-limit)))
11077           header)))))
11078
11079 (defun gnus-remove-header (number)
11080   "Remove header NUMBER from `gnus-newsgroup-headers'."
11081   (if (and gnus-newsgroup-headers
11082            (= number (mail-header-number (car gnus-newsgroup-headers))))
11083       (pop gnus-newsgroup-headers)
11084     (let ((headers gnus-newsgroup-headers))
11085       (while (and (cdr headers)
11086                   (not (= number (mail-header-number (cadr headers)))))
11087         (pop headers))
11088       (when (cdr headers)
11089         (setcdr headers (cddr headers))))))
11090
11091 ;;;
11092 ;;; summary highlights
11093 ;;;
11094
11095 (defun gnus-highlight-selected-summary ()
11096   "Highlight selected article in summary buffer."
11097   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11098   (when gnus-summary-selected-face
11099     (save-excursion
11100       (let* ((beg (gnus-point-at-bol))
11101              (end (gnus-point-at-eol))
11102              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11103              (from (if (get-text-property beg gnus-mouse-face-prop)
11104                        beg
11105                      (or (next-single-property-change
11106                           beg gnus-mouse-face-prop nil end)
11107                          beg)))
11108              (to
11109               (if (= from end)
11110                   (- from 2)
11111                 (or (next-single-property-change
11112                      from gnus-mouse-face-prop nil end)
11113                     end))))
11114         ;; If no mouse-face prop on line we will have to = from = end,
11115         ;; so we highlight the entire line instead.
11116         (when (= (+ to 2) from)
11117           (setq from beg)
11118           (setq to end))
11119         (if gnus-newsgroup-selected-overlay
11120             ;; Move old overlay.
11121             (gnus-move-overlay
11122              gnus-newsgroup-selected-overlay from to (current-buffer))
11123           ;; Create new overlay.
11124           (gnus-overlay-put
11125            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11126            'face gnus-summary-selected-face))))))
11127
11128 (defvar gnus-summary-highlight-line-cached nil)
11129 (defvar gnus-summary-highlight-line-trigger nil)
11130
11131 (defun gnus-summary-highlight-line-0 ()
11132   (if (and (eq gnus-summary-highlight-line-trigger 
11133                gnus-summary-highlight)
11134            gnus-summary-highlight-line-cached)
11135       gnus-summary-highlight-line-cached
11136     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11137           gnus-summary-highlight-line-cached
11138           (let* ((cond (list 'cond))
11139                  (c cond)
11140                  (list gnus-summary-highlight))
11141             (while list
11142               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11143                               nil))
11144               (setq c (cdr c)
11145                     list (cdr list)))
11146             (gnus-byte-compile (list 'lambda nil cond))))))
11147
11148 (defun gnus-summary-highlight-line ()
11149   "Highlight current line according to `gnus-summary-highlight'."
11150   (let* ((beg (gnus-point-at-bol))
11151          (article (or (gnus-summary-article-number) gnus-current-article))
11152          (score (or (cdr (assq article
11153                                gnus-newsgroup-scored))
11154                     gnus-summary-default-score 0))
11155          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11156          (inhibit-read-only t)
11157          (default gnus-summary-default-score)
11158          (default-high gnus-summary-default-high-score)
11159          (default-low gnus-summary-default-low-score)
11160          (uncached (memq article gnus-newsgroup-undownloaded))
11161          (downloaded (not uncached)))
11162     (let ((face (funcall (gnus-summary-highlight-line-0))))
11163       (unless (eq face (get-text-property beg 'face))
11164         (gnus-put-text-property-excluding-characters-with-faces
11165          beg (gnus-point-at-eol) 'face
11166          (setq face (if (boundp face) (symbol-value face) face)))
11167         (when gnus-summary-highlight-line-function
11168           (funcall gnus-summary-highlight-line-function article face))))))
11169
11170 (defun gnus-update-read-articles (group unread &optional compute)
11171   "Update the list of read articles in GROUP.
11172 UNREAD is a sorted list."
11173   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11174          (entry (gnus-gethash group gnus-newsrc-hashtb))
11175          (info (nth 2 entry))
11176          (prev 1)
11177          read)
11178     (if (or (not info) (not active))
11179         ;; There is no info on this group if it was, in fact,
11180         ;; killed.  Gnus stores no information on killed groups, so
11181         ;; there's nothing to be done.
11182         ;; One could store the information somewhere temporarily,
11183         ;; perhaps...  Hmmm...
11184         ()
11185       ;; Remove any negative articles numbers.
11186       (while (and unread (< (car unread) 0))
11187         (setq unread (cdr unread)))
11188       ;; Remove any expired article numbers
11189       (while (and unread (< (car unread) (car active)))
11190         (setq unread (cdr unread)))
11191       ;; Compute the ranges of read articles by looking at the list of
11192       ;; unread articles.
11193       (while unread
11194         (when (/= (car unread) prev)
11195           (push (if (= prev (1- (car unread))) prev
11196                   (cons prev (1- (car unread))))
11197                 read))
11198         (setq prev (1+ (car unread)))
11199         (setq unread (cdr unread)))
11200       (when (<= prev (cdr active))
11201         (push (cons prev (cdr active)) read))
11202       (setq read (if (> (length read) 1) (nreverse read) read))
11203       (if compute
11204           read
11205         (save-excursion
11206           (let (setmarkundo)
11207             ;; Propagate the read marks to the backend.
11208             (when (gnus-check-backend-function 'request-set-mark group)
11209               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11210                     (add (gnus-remove-from-range read (gnus-info-read info))))
11211                 (when (or add del)
11212                   (unless (gnus-check-group group)
11213                     (error "Can't open server for %s" group))
11214                   (gnus-request-set-mark
11215                    group (delq nil (list (if add (list add 'add '(read)))
11216                                          (if del (list del 'del '(read))))))
11217                   (setq setmarkundo
11218                         `(gnus-request-set-mark
11219                           ,group
11220                           ',(delq nil (list
11221                                        (if del (list del 'add '(read)))
11222                                        (if add (list add 'del '(read))))))))))
11223             (set-buffer gnus-group-buffer)
11224             (gnus-undo-register
11225               `(progn
11226                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11227                  (gnus-info-set-read ',info ',(gnus-info-read info))
11228                  (gnus-get-unread-articles-in-group ',info
11229                                                     (gnus-active ,group))
11230                  (gnus-group-update-group ,group t)
11231                  ,setmarkundo))))
11232         ;; Enter this list into the group info.
11233         (gnus-info-set-read info read)
11234         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11235         (gnus-get-unread-articles-in-group info (gnus-active group))
11236         t))))
11237
11238 (defun gnus-offer-save-summaries ()
11239   "Offer to save all active summary buffers."
11240   (let (buffers)
11241     ;; Go through all buffers and find all summaries.
11242     (dolist (buffer (buffer-list))
11243       (when (and (setq buffer (buffer-name buffer))
11244                  (string-match "Summary" buffer)
11245                  (save-excursion
11246                    (set-buffer buffer)
11247                    ;; We check that this is, indeed, a summary buffer.
11248                    (and (eq major-mode 'gnus-summary-mode)
11249                         ;; Also make sure this isn't bogus.
11250                         gnus-newsgroup-prepared
11251                         ;; Also make sure that this isn't a
11252                         ;; dead summary buffer.
11253                         (not gnus-dead-summary-mode))))
11254         (push buffer buffers)))
11255     ;; Go through all these summary buffers and offer to save them.
11256     (when buffers
11257       (save-excursion
11258         (map-y-or-n-p
11259          "Update summary buffer %s? "
11260          (lambda (buf)
11261            (switch-to-buffer buf)
11262            (gnus-summary-exit))
11263          buffers)))))
11264
11265 (defun gnus-summary-setup-default-charset ()
11266   "Setup newsgroup default charset."
11267   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11268       (setq gnus-newsgroup-charset nil)
11269     (let* ((ignored-charsets
11270             (or gnus-newsgroup-ephemeral-ignored-charsets
11271                 (append
11272                  (and gnus-newsgroup-name
11273                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11274                  gnus-newsgroup-ignored-charsets))))
11275       (setq gnus-newsgroup-charset
11276             (or gnus-newsgroup-ephemeral-charset
11277                 (and gnus-newsgroup-name
11278                      (gnus-parameter-charset gnus-newsgroup-name))
11279                 gnus-default-charset))
11280       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11281            ignored-charsets))))
11282
11283 ;;;
11284 ;;; Mime Commands
11285 ;;;
11286
11287 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11288   "Display the current article buffer fully MIME-buttonized.
11289 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11290 treated as multipart/mixed."
11291   (interactive "P")
11292   (require 'gnus-art)
11293   (let ((gnus-unbuttonized-mime-types nil)
11294         (gnus-mime-display-multipart-as-mixed show-all-parts))
11295     (gnus-summary-show-article)))
11296
11297 (defun gnus-summary-repair-multipart (article)
11298   "Add a Content-Type header to a multipart article without one."
11299   (interactive (list (gnus-summary-article-number)))
11300   (gnus-with-article article
11301     (message-narrow-to-head)
11302     (message-remove-header "Mime-Version")
11303     (goto-char (point-max))
11304     (insert "Mime-Version: 1.0\n")
11305     (widen)
11306     (when (search-forward "\n--" nil t)
11307       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11308         (message-narrow-to-head)
11309         (message-remove-header "Content-Type")
11310         (goto-char (point-max))
11311         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11312                         separator))
11313         (widen))))
11314   (let (gnus-mark-article-hook)
11315     (gnus-summary-select-article t t nil article)))
11316
11317 (defun gnus-summary-toggle-display-buttonized ()
11318   "Toggle the buttonizing of the article buffer."
11319   (interactive)
11320   (require 'gnus-art)
11321   (if (setq gnus-inhibit-mime-unbuttonizing
11322             (not gnus-inhibit-mime-unbuttonizing))
11323       (let ((gnus-unbuttonized-mime-types nil))
11324         (gnus-summary-show-article))
11325     (gnus-summary-show-article)))
11326
11327 ;;;
11328 ;;; Generic summary marking commands
11329 ;;;
11330
11331 (defvar gnus-summary-marking-alist
11332   '((read gnus-del-mark "d")
11333     (unread gnus-unread-mark "u")
11334     (ticked gnus-ticked-mark "!")
11335     (dormant gnus-dormant-mark "?")
11336     (expirable gnus-expirable-mark "e"))
11337   "An alist of names/marks/keystrokes.")
11338
11339 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11340 (defvar gnus-summary-mark-map)
11341
11342 (defun gnus-summary-make-all-marking-commands ()
11343   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11344   (dolist (elem gnus-summary-marking-alist)
11345     (apply 'gnus-summary-make-marking-command elem)))
11346
11347 (defun gnus-summary-make-marking-command (name mark keystroke)
11348   (let ((map (make-sparse-keymap)))
11349     (define-key gnus-summary-generic-mark-map keystroke map)
11350     (dolist (lway `((next "next" next nil "n")
11351                     (next-unread "next unread" next t "N")
11352                     (prev "previous" prev nil "p")
11353                     (prev-unread "previous unread" prev t "P")
11354                     (nomove "" nil nil ,keystroke)))
11355       (let ((func (gnus-summary-make-marking-command-1
11356                    mark (car lway) lway name)))
11357         (setq func (eval func))
11358         (define-key map (nth 4 lway) func)))))
11359
11360 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11361   `(defun ,(intern
11362             (format "gnus-summary-put-mark-as-%s%s"
11363                     name (if (eq way 'nomove)
11364                              ""
11365                            (concat "-" (symbol-name way)))))
11366      (n)
11367      ,(format
11368        "Mark the current article as %s%s.
11369 If N, the prefix, then repeat N times.
11370 If N is negative, move in reverse order.
11371 The difference between N and the actual number of articles marked is
11372 returned."
11373        name (cadr lway))
11374      (interactive "p")
11375      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11376
11377 (defun gnus-summary-generic-mark (n mark move unread)
11378   "Mark N articles with MARK."
11379   (unless (eq major-mode 'gnus-summary-mode)
11380     (error "This command can only be used in the summary buffer"))
11381   (gnus-summary-show-thread)
11382   (let ((nummove
11383          (cond
11384           ((eq move 'next) 1)
11385           ((eq move 'prev) -1)
11386           (t 0))))
11387     (if (zerop nummove)
11388         (setq n 1)
11389       (when (< n 0)
11390         (setq n (abs n)
11391               nummove (* -1 nummove))))
11392     (while (and (> n 0)
11393                 (gnus-summary-mark-article nil mark)
11394                 (zerop (gnus-summary-next-subject nummove unread t)))
11395       (setq n (1- n)))
11396     (when (/= 0 n)
11397       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11398     (gnus-summary-recenter)
11399     (gnus-summary-position-point)
11400     (gnus-set-mode-line 'summary)
11401     n))
11402
11403 (defun gnus-summary-insert-articles (articles)
11404   (when (setq articles
11405               (gnus-sorted-difference articles
11406                                       (mapcar (lambda (h)
11407                                                 (mail-header-number h))
11408                                               gnus-newsgroup-headers)))
11409     (setq gnus-newsgroup-headers
11410           (merge 'list
11411                  gnus-newsgroup-headers
11412                  (gnus-fetch-headers articles)
11413                  'gnus-article-sort-by-number))
11414     ;; Suppress duplicates?
11415     (when gnus-suppress-duplicates
11416       (gnus-dup-suppress-articles))
11417
11418     ;; We might want to build some more threads first.
11419     (when (and gnus-fetch-old-headers
11420                (eq gnus-headers-retrieved-by 'nov))
11421       (if (eq gnus-fetch-old-headers 'invisible)
11422           (gnus-build-all-threads)
11423         (gnus-build-old-threads)))
11424     ;; Let the Gnus agent mark articles as read.
11425     (when gnus-agent
11426       (gnus-agent-get-undownloaded-list))
11427     ;; Remove list identifiers from subject
11428     (when gnus-list-identifiers
11429       (gnus-summary-remove-list-identifiers))
11430     ;; First and last article in this newsgroup.
11431     (when gnus-newsgroup-headers
11432       (setq gnus-newsgroup-begin
11433             (mail-header-number (car gnus-newsgroup-headers))
11434             gnus-newsgroup-end
11435             (mail-header-number
11436              (gnus-last-element gnus-newsgroup-headers))))
11437     (when gnus-use-scoring
11438       (gnus-possibly-score-headers))))
11439
11440 (defun gnus-summary-insert-old-articles (&optional all)
11441   "Insert all old articles in this group.
11442 If ALL is non-nil, already read articles become readable.
11443 If ALL is a number, fetch this number of articles."
11444   (interactive "P")
11445   (prog1
11446       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11447             older len)
11448         (setq older
11449               ;; Some nntp servers lie about their active range.  When
11450               ;; this happens, the active range can be in the millions.
11451               ;; Use a compressed range to avoid creating a huge list.
11452               (gnus-range-difference (list gnus-newsgroup-active) old))
11453         (setq len (gnus-range-length older))
11454         (cond
11455          ((null older) nil)
11456          ((numberp all)
11457           (if (< all len)
11458               (let ((older-range (nreverse older)))
11459                 (setq older nil)
11460
11461                 (while (> all 0)
11462                   (let* ((r (pop older-range))
11463                          (min (if (numberp r) r (car r)))
11464                          (max (if (numberp r) r (cdr r))))
11465                     (while (and (<= min max)
11466                                 (> all 0))
11467                       (push max older)
11468                       (setq all (1- all)
11469                             max (1- max))))))
11470             (setq older (gnus-uncompress-range older))))
11471          (all
11472           (setq older (gnus-uncompress-range older)))
11473          (t
11474           (when (and (numberp gnus-large-newsgroup)
11475                    (> len gnus-large-newsgroup))
11476               (let* ((cursor-in-echo-area nil)
11477                      (initial (gnus-parameter-large-newsgroup-initial
11478                                gnus-newsgroup-name))
11479                      (input
11480                       (read-string
11481                        (format
11482                         "How many articles from %s (%s %d): "
11483                         (gnus-limit-string
11484                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11485                         (if initial "max" "default")
11486                         len)
11487                        (if initial
11488                            (cons (number-to-string initial)
11489                                  0)))))
11490                 (unless (string-match "^[ \t]*$" input)
11491                   (setq all (string-to-number input))
11492                   (if (< all len)
11493                       (let ((older-range (nreverse older)))
11494                         (setq older nil)
11495
11496                         (while (> all 0)
11497                           (let* ((r (pop older-range))
11498                                  (min (if (numberp r) r (car r)))
11499                                  (max (if (numberp r) r (cdr r))))
11500                             (while (and (<= min max)
11501                                         (> all 0))
11502                               (push max older)
11503                               (setq all (1- all)
11504                                     max (1- max))))))))))
11505           (setq older (gnus-uncompress-range older))))
11506         (if (not older)
11507             (message "No old news.")
11508           (gnus-summary-insert-articles older)
11509           (gnus-summary-limit (gnus-sorted-nunion old older))))
11510     (gnus-summary-position-point)))
11511
11512 (defun gnus-summary-insert-new-articles ()
11513   "Insert all new articles in this group."
11514   (interactive)
11515   (prog1
11516       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11517             (old-active gnus-newsgroup-active)
11518             (nnmail-fetched-sources (list t))
11519             i new)
11520         (setq gnus-newsgroup-active
11521               (gnus-activate-group gnus-newsgroup-name 'scan))
11522         (setq i (cdr gnus-newsgroup-active))
11523         (while (> i (cdr old-active))
11524           (push i new)
11525           (decf i))
11526         (if (not new)
11527             (message "No gnus is bad news.")
11528           (gnus-summary-insert-articles new)
11529           (setq gnus-newsgroup-unreads
11530                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11531           (gnus-summary-limit (gnus-sorted-nunion old new))))
11532     (gnus-summary-position-point)))
11533
11534 (gnus-summary-make-all-marking-commands)
11535
11536 (gnus-ems-redefine)
11537
11538 (provide 'gnus-sum)
11539
11540 (run-hooks 'gnus-sum-load-hook)
11541
11542 ;;; gnus-sum.el ends here