* gnus-sum.el (gnus-article-loose-mime)
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
50 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
51 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
52
53 (defcustom gnus-kill-summary-on-exit t
54   "*If non-nil, kill the summary buffer when you exit from it.
55 If nil, the summary will become a \"*Dead Summary*\" buffer, and
56 it will be killed sometime later."
57   :group 'gnus-summary-exit
58   :type 'boolean)
59
60 (defcustom gnus-fetch-old-headers nil
61   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
62 If an unread article in the group refers to an older, already read (or
63 just marked as read) article, the old article will not normally be
64 displayed in the Summary buffer.  If this variable is non-nil, Gnus
65 will attempt to grab the headers to the old articles, and thereby
66 build complete threads.  If it has the value `some', only enough
67 headers to connect otherwise loose threads will be displayed.  This
68 variable can also be a number.  In that case, no more than that number
69 of old headers will be fetched.  If it has the value `invisible', all
70 old headers will be fetched, but none will be displayed.
71
72 The server has to support NOV for any of this to work."
73   :group 'gnus-thread
74   :type '(choice (const :tag "off" nil)
75                  (const some)
76                  number
77                  (sexp :menu-tag "other" t)))
78
79 (defcustom gnus-refer-thread-limit 200
80   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
81 If t, fetch all the available old headers."
82   :group 'gnus-thread
83   :type '(choice number
84                  (sexp :menu-tag "other" t)))
85
86 (defcustom gnus-summary-make-false-root 'adopt
87   "*nil means that Gnus won't gather loose threads.
88 If the root of a thread has expired or been read in a previous
89 session, the information necessary to build a complete thread has been
90 lost.  Instead of having many small sub-threads from this original thread
91 scattered all over the summary buffer, Gnus can gather them.
92
93 If non-nil, Gnus will try to gather all loose sub-threads from an
94 original thread into one large thread.
95
96 If this variable is non-nil, it should be one of `none', `adopt',
97 `dummy' or `empty'.
98
99 If this variable is `none', Gnus will not make a false root, but just
100 present the sub-threads after another.
101 If this variable is `dummy', Gnus will create a dummy root that will
102 have all the sub-threads as children.
103 If this variable is `adopt', Gnus will make one of the \"children\"
104 the parent and mark all the step-children as such.
105 If this variable is `empty', the \"children\" are printed with empty
106 subject fields.  (Or rather, they will be printed with a string
107 given by the `gnus-summary-same-subject' variable.)"
108   :group 'gnus-thread
109   :type '(choice (const :tag "off" nil)
110                  (const none)
111                  (const dummy)
112                  (const adopt)
113                  (const empty)))
114
115 (defcustom gnus-summary-make-false-root-always nil
116   "Always make a false dummy root."
117   :group 'gnus-thread
118   :type 'boolean)
119
120 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
121   "*A regexp to match subjects to be excluded from loose thread gathering.
122 As loose thread gathering is done on subjects only, that means that
123 there can be many false gatherings performed.  By rooting out certain
124 common subjects, gathering might become saner."
125   :group 'gnus-thread
126   :type 'regexp)
127
128 (defcustom gnus-summary-gather-subject-limit nil
129   "*Maximum length of subject comparisons when gathering loose threads.
130 Use nil to compare full subjects.  Setting this variable to a low
131 number will help gather threads that have been corrupted by
132 newsreaders chopping off subject lines, but it might also mean that
133 unrelated articles that have subject that happen to begin with the
134 same few characters will be incorrectly gathered.
135
136 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
137 comparing subjects."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  (const fuzzy)
141                  (sexp :menu-tag "on" t)))
142
143 (defcustom gnus-simplify-subject-functions nil
144   "List of functions taking a string argument that simplify subjects.
145 The functions are applied recursively.
146
147 Useful functions to put in this list include:
148 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
149 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
150   :group 'gnus-thread
151   :type '(repeat function))
152
153 (defcustom gnus-simplify-ignored-prefixes nil
154   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  regexp))
158
159 (defcustom gnus-build-sparse-threads nil
160   "*If non-nil, fill in the gaps in threads.
161 If `some', only fill in the gaps that are needed to tie loose threads
162 together.  If `more', fill in all leaf nodes that Gnus can find.  If
163 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
164   :group 'gnus-thread
165   :type '(choice (const :tag "off" nil)
166                  (const some)
167                  (const more)
168                  (sexp :menu-tag "all" t)))
169
170 (defcustom gnus-summary-thread-gathering-function
171   'gnus-gather-threads-by-subject
172   "*Function used for gathering loose threads.
173 There are two pre-defined functions: `gnus-gather-threads-by-subject',
174 which only takes Subjects into consideration; and
175 `gnus-gather-threads-by-references', which compared the References
176 headers of the articles to find matches."
177   :group 'gnus-thread
178   :type '(radio (function-item gnus-gather-threads-by-subject)
179                 (function-item gnus-gather-threads-by-references)
180                 (function :tag "other")))
181
182 (defcustom gnus-summary-same-subject ""
183   "*String indicating that the current article has the same subject as the previous.
184 This variable will only be used if the value of
185 `gnus-summary-make-false-root' is `empty'."
186   :group 'gnus-summary-format
187   :type 'string)
188
189 (defcustom gnus-summary-goto-unread t
190   "*If t, many commands will go to the next unread article.
191 This applies to marking commands as well as other commands that
192 \"naturally\" select the next article, like, for instance, `SPC' at
193 the end of an article.
194
195 If nil, the marking commands do NOT go to the next unread article
196 \(they go to the next article instead).  If `never', commands that
197 usually go to the next unread article, will go to the next article,
198 whether it is read or not."
199   :group 'gnus-summary-marks
200   :link '(custom-manual "(gnus)Setting Marks")
201   :type '(choice (const :tag "off" nil)
202                  (const never)
203                  (sexp :menu-tag "on" t)))
204
205 (defcustom gnus-summary-default-score 0
206   "*Default article score level.
207 All scores generated by the score files will be added to this score.
208 If this variable is nil, scoring will be disabled."
209   :group 'gnus-score-default
210   :type '(choice (const :tag "disable")
211                  integer))
212
213 (defcustom gnus-summary-default-high-score 0
214   "*Default threshold for a high scored article.
215 An article will be highlighted as high scored if its score is greater
216 than this score."
217   :group 'gnus-score-default
218   :type 'integer)
219
220 (defcustom gnus-summary-default-low-score 0
221   "*Default threshold for a low scored article.
222 An article will be highlighted as low scored if its score is smaller
223 than this score."
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-zcore-fuzz 0
228   "*Fuzziness factor for the zcore in the summary buffer.
229 Articles with scores closer than this to `gnus-summary-default-score'
230 will not be marked."
231   :group 'gnus-summary-format
232   :type 'integer)
233
234 (defcustom gnus-simplify-subject-fuzzy-regexp nil
235   "*Strings to be removed when doing fuzzy matches.
236 This can either be a regular expression or list of regular expressions
237 that will be removed from subject strings if fuzzy subject
238 simplification is selected."
239   :group 'gnus-thread
240   :type '(repeat regexp))
241
242 (defcustom gnus-show-threads t
243   "*If non-nil, display threads in summary mode."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-subtree nil
248   "*If non-nil, hide all threads initially.
249 This can be a predicate specifier which says which threads to hide.
250 If threads are hidden, you have to run the command
251 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
252 to expose hidden threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-killed t
257   "*If non-nil, hide killed threads automatically."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-ignore-subject t
262   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
263 If nil, articles that have different subjects from their parents will
264 start separate threads."
265   :group 'gnus-thread
266   :type 'boolean)
267
268 (defcustom gnus-thread-operation-ignore-subject t
269   "*If non-nil, subjects will be ignored when doing thread commands.
270 This affects commands like `gnus-summary-kill-thread' and
271 `gnus-summary-lower-thread'.
272
273 If this variable is nil, articles in the same thread with different
274 subjects will not be included in the operation in question.  If this
275 variable is `fuzzy', only articles that have subjects that are fuzzily
276 equal will be included."
277   :group 'gnus-thread
278   :type '(choice (const :tag "off" nil)
279                  (const fuzzy)
280                  (sexp :tag "on" t)))
281
282 (defcustom gnus-thread-indent-level 4
283   "*Number that says how much each sub-thread should be indented."
284   :group 'gnus-thread
285   :type 'integer)
286
287 (defcustom gnus-auto-extend-newsgroup t
288   "*If non-nil, extend newsgroup forward and backward when requested."
289   :group 'gnus-summary-choose
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-first t
293   "*If non-nil, select the article under point.
294 Which article this is is controlled by the `gnus-auto-select-subject'
295 variable.
296
297 If you want to prevent automatic selection of articles in some
298 newsgroups, set the variable to nil in `gnus-select-group-hook'."
299   :group 'gnus-group-select
300   :type '(choice (const :tag "none" nil)
301                  (sexp :menu-tag "first" t)))
302
303 (defcustom gnus-auto-select-subject 'unread
304   "*Says what subject to place under point when entering a group.
305
306 This variable can either be the symbols `first' (place point on the
307 first subject), `unread' (place point on the subject line of the first
308 unread article), `best' (place point on the subject line of the
309 higest-scored article), `unseen' (place point on the subject line of
310 the first unseen article), 'unseen-or-unread' (place point on the subject
311 line of the first unseen article or, if all article have been seen, on the
312 subject line of the first unread article), or a function to be called to
313 place point on some subject line."
314   :group 'gnus-group-select
315   :type '(choice (const best)
316                  (const unread)
317                  (const first)
318                  (const unseen)
319                  (const unseen-or-unread)))
320
321 (defcustom gnus-auto-select-next t
322   "*If non-nil, offer to go to the next group from the end of the previous.
323 If the value is t and the next newsgroup is empty, Gnus will exit
324 summary mode and go back to group mode.  If the value is neither nil
325 nor t, Gnus will select the following unread newsgroup.  In
326 particular, if the value is the symbol `quietly', the next unread
327 newsgroup will be selected without any confirmation, and if it is
328 `almost-quietly', the next group will be selected without any
329 confirmation if you are located on the last article in the group.
330 Finally, if this variable is `slightly-quietly', the `\\<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              gnus-tmp-thread-tree-header-string
4823              (cond
4824               ((not gnus-show-threads) "")
4825               ((zerop gnus-tmp-level)
4826                (if (cdar thread)
4827                    (or gnus-sum-thread-tree-root subject)
4828                  (or gnus-sum-thread-tree-single-indent subject)))
4829               (t
4830                (concat (apply 'concat
4831                               (mapcar (lambda (item)
4832                                         (if (= item 1)
4833                                             gnus-sum-thread-tree-vertical
4834                                           gnus-sum-thread-tree-indent))
4835                                       (cdr (reverse tree-stack))))
4836                        (if (nth 1 thread)
4837                            gnus-sum-thread-tree-leaf-with-other
4838                          gnus-sum-thread-tree-single-leaf)))))
4839             (when (string= gnus-tmp-name "")
4840               (setq gnus-tmp-name gnus-tmp-from))
4841             (unless (numberp gnus-tmp-lines)
4842               (setq gnus-tmp-lines -1))
4843             (if (= gnus-tmp-lines -1)
4844                 (setq gnus-tmp-lines "?")
4845               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4846               (gnus-put-text-property
4847              (point)
4848              (progn (eval gnus-summary-line-format-spec) (point))
4849                'gnus-number number)
4850             (when gnus-visual-p
4851               (forward-line -1)
4852               (gnus-run-hooks 'gnus-summary-update-hook)
4853               (forward-line 1))
4854
4855             (setq gnus-tmp-prev-subject simp-subject)))
4856
4857         (when (nth 1 thread)
4858           (push (list (max 0 gnus-tmp-level)
4859                       (copy-list tree-stack)
4860                       (nthcdr 1 thread))
4861                 stack))
4862         (push (if (nth 1 thread) 1 0) tree-stack)
4863         (incf gnus-tmp-level)
4864         (setq threads (if thread-end nil (cdar thread)))
4865         (if gnus-summary-display-while-building
4866             (if building-count
4867                 (progn
4868                   ;; use a set frequency
4869                   (setq building-line-count (1- building-line-count))
4870                   (when (= building-line-count 0)
4871                     (sit-for 0)
4872                     (setq building-line-count
4873                           gnus-summary-display-while-building)))
4874               ;; always
4875               (sit-for 0)))
4876         (unless threads
4877           (setq gnus-tmp-level 0)))))
4878   (gnus-message 7 "Generating summary...done"))
4879
4880 (defun gnus-summary-prepare-unthreaded (headers)
4881   "Generate an unthreaded summary buffer based on HEADERS."
4882   (let (header number mark)
4883
4884     (beginning-of-line)
4885
4886     (while headers
4887       ;; We may have to root out some bad articles...
4888       (when (memq (setq number (mail-header-number
4889                                 (setq header (pop headers))))
4890                   gnus-newsgroup-limit)
4891         ;; Mark article as read when it has a low score.
4892         (when (and gnus-summary-mark-below
4893                    (< (or (cdr (assq number gnus-newsgroup-scored))
4894                           gnus-summary-default-score 0)
4895                       gnus-summary-mark-below)
4896                    (not (gnus-summary-article-ancient-p number)))
4897           (setq gnus-newsgroup-unreads
4898                 (delq number gnus-newsgroup-unreads))
4899           (if gnus-newsgroup-auto-expire
4900               (push number gnus-newsgroup-expirable)
4901             (push (cons number gnus-low-score-mark)
4902                   gnus-newsgroup-reads)))
4903
4904         (setq mark (gnus-article-mark number))
4905         (push (gnus-data-make number mark (1+ (point)) header 0)
4906               gnus-newsgroup-data)
4907         (gnus-summary-insert-line
4908          header 0 number
4909          (memq number gnus-newsgroup-undownloaded)
4910          mark (memq number gnus-newsgroup-replied)
4911          (memq number gnus-newsgroup-expirable)
4912          (mail-header-subject header) nil
4913          (cdr (assq number gnus-newsgroup-scored))
4914          (memq number gnus-newsgroup-processable))))))
4915
4916 (defun gnus-summary-remove-list-identifiers ()
4917   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4918   (let ((regexp (if (consp gnus-list-identifiers)
4919                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4920                   gnus-list-identifiers))
4921         changed subject)
4922     (when regexp
4923       (dolist (header gnus-newsgroup-headers)
4924         (setq subject (mail-header-subject header)
4925               changed nil)
4926         (while (string-match
4927                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4928                 subject)
4929           (setq subject
4930                 (concat (substring subject 0 (match-beginning 2))
4931                         (substring subject (match-end 0)))
4932                 changed t))
4933         (when (and changed
4934                    (string-match
4935                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4936           (setq subject
4937                 (concat (substring subject 0 (match-beginning 1))
4938                         (substring subject (match-end 1)))))
4939         (when changed
4940           (mail-header-set-subject header subject))))))
4941
4942 (defun gnus-fetch-headers (articles)
4943   "Fetch headers of ARTICLES."
4944   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4945     (gnus-message 5 "Fetching headers for %s..." name)
4946     (prog1
4947         (if (eq 'nov
4948                 (setq gnus-headers-retrieved-by
4949                       (gnus-retrieve-headers
4950                        articles gnus-newsgroup-name
4951                        ;; We might want to fetch old headers, but
4952                        ;; not if there is only 1 article.
4953                        (and (or (and
4954                                  (not (eq gnus-fetch-old-headers 'some))
4955                                  (not (numberp gnus-fetch-old-headers)))
4956                                 (> (length articles) 1))
4957                             gnus-fetch-old-headers))))
4958             (gnus-get-newsgroup-headers-xover
4959              articles nil nil gnus-newsgroup-name t)
4960           (gnus-get-newsgroup-headers))
4961       (gnus-message 5 "Fetching headers for %s...done" name))))
4962
4963 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4964   "Select newsgroup GROUP.
4965 If READ-ALL is non-nil, all articles in the group are selected.
4966 If SELECT-ARTICLES, only select those articles from GROUP."
4967   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4968          ;;!!! Dirty hack; should be removed.
4969          (gnus-summary-ignore-duplicates
4970           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4971               t
4972             gnus-summary-ignore-duplicates))
4973          (info (nth 2 entry))
4974          articles fetched-articles cached)
4975
4976     (unless (gnus-check-server
4977              (set (make-local-variable 'gnus-current-select-method)
4978                   (gnus-find-method-for-group group)))
4979       (error "Couldn't open server"))
4980
4981     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4982         (gnus-activate-group group)     ; Or we can activate it...
4983         (progn                          ; Or we bug out.
4984           (when (equal major-mode 'gnus-summary-mode)
4985             (gnus-kill-buffer (current-buffer)))
4986           (error "Couldn't activate group %s: %s"
4987                  group (gnus-status-message group))))
4988
4989     (unless (gnus-request-group group t)
4990       (when (equal major-mode 'gnus-summary-mode)
4991         (gnus-kill-buffer (current-buffer)))
4992       (error "Couldn't request group %s: %s"
4993              group (gnus-status-message group)))
4994
4995     (setq gnus-newsgroup-name group
4996           gnus-newsgroup-unselected nil
4997           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4998
4999     (let ((display (gnus-group-find-parameter group 'display)))
5000       (setq gnus-newsgroup-display
5001             (cond
5002              ((not (zerop (or (car-safe read-all) 0)))
5003               ;; The user entered the group with C-u SPC/RET, let's show
5004               ;; all articles.
5005               'gnus-not-ignore)
5006              ((eq display 'all)
5007               'gnus-not-ignore)
5008              ((arrayp display)
5009               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5010              ((numberp display)
5011               ;; The following is probably the "correct" solution, but
5012               ;; it makes Gnus fetch all headers and then limit the
5013               ;; articles (which is slow), so instead we hack the
5014               ;; select-articles parameter instead. -- Simon Josefsson
5015               ;; <jas@kth.se>
5016               ;;
5017               ;; (gnus-byte-compile
5018               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5019               ;;                         display)))))
5020               (setq select-articles
5021                     (gnus-uncompress-range
5022                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5023                              (if (> tmp 0)
5024                                  tmp
5025                                1))
5026                            (cdr (gnus-active group)))))
5027               nil)
5028              (t
5029               nil))))
5030
5031     (gnus-summary-setup-default-charset)
5032
5033     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5034     (when (gnus-virtual-group-p group)
5035       (setq cached gnus-newsgroup-cached))
5036
5037     (setq gnus-newsgroup-unreads
5038           (gnus-sorted-ndifference
5039            (gnus-sorted-ndifference gnus-newsgroup-unreads
5040                                     gnus-newsgroup-marked)
5041            gnus-newsgroup-dormant))
5042
5043     (setq gnus-newsgroup-processable nil)
5044
5045     (gnus-update-read-articles group gnus-newsgroup-unreads)
5046
5047     ;; Adjust and set lists of article marks.
5048     (when info
5049       (gnus-adjust-marked-articles info))
5050     (if (setq articles select-articles)
5051         (setq gnus-newsgroup-unselected
5052               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5053       (setq articles (gnus-articles-to-read group read-all)))
5054
5055     (cond
5056      ((null articles)
5057       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5058       'quit)
5059      ((eq articles 0) nil)
5060      (t
5061       ;; Init the dependencies hash table.
5062       (setq gnus-newsgroup-dependencies
5063             (gnus-make-hashtable (length articles)))
5064       (gnus-set-global-variables)
5065       ;; Retrieve the headers and read them in.
5066
5067       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5068
5069       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5070       (when cached
5071         (setq gnus-newsgroup-cached cached))
5072
5073       ;; Suppress duplicates?
5074       (when gnus-suppress-duplicates
5075         (gnus-dup-suppress-articles))
5076
5077       ;; Set the initial limit.
5078       (setq gnus-newsgroup-limit (copy-sequence articles))
5079       ;; Remove canceled articles from the list of unread articles.
5080       (setq fetched-articles
5081             (mapcar (lambda (headers) (mail-header-number headers))
5082                     gnus-newsgroup-headers))
5083       (setq gnus-newsgroup-articles fetched-articles)
5084       (setq gnus-newsgroup-unreads
5085             (gnus-sorted-nintersection
5086              gnus-newsgroup-unreads fetched-articles))
5087       (gnus-compute-unseen-list)
5088
5089       ;; Removed marked articles that do not exist.
5090       (gnus-update-missing-marks
5091        (gnus-sorted-difference articles fetched-articles))
5092       ;; We might want to build some more threads first.
5093       (when (and gnus-fetch-old-headers
5094                  (eq gnus-headers-retrieved-by 'nov))
5095         (if (eq gnus-fetch-old-headers 'invisible)
5096             (gnus-build-all-threads)
5097           (gnus-build-old-threads)))
5098       ;; Let the Gnus agent mark articles as read.
5099       (when gnus-agent
5100         (gnus-agent-get-undownloaded-list))
5101       ;; Remove list identifiers from subject
5102       (when gnus-list-identifiers
5103         (gnus-summary-remove-list-identifiers))
5104       ;; Check whether auto-expire is to be done in this group.
5105       (setq gnus-newsgroup-auto-expire
5106             (gnus-group-auto-expirable-p group))
5107       ;; Set up the article buffer now, if necessary.
5108       (unless gnus-single-article-buffer
5109         (gnus-article-setup-buffer))
5110       ;; First and last article in this newsgroup.
5111       (when gnus-newsgroup-headers
5112         (setq gnus-newsgroup-begin
5113               (mail-header-number (car gnus-newsgroup-headers))
5114               gnus-newsgroup-end
5115               (mail-header-number
5116                (gnus-last-element gnus-newsgroup-headers))))
5117       ;; GROUP is successfully selected.
5118       (or gnus-newsgroup-headers t)))))
5119
5120 (defun gnus-compute-unseen-list ()
5121   ;; The `seen' marks are treated specially.
5122   (if (not gnus-newsgroup-seen)
5123       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5124     (setq gnus-newsgroup-unseen
5125           (gnus-inverse-list-range-intersection
5126            gnus-newsgroup-articles gnus-newsgroup-seen))))
5127
5128 (defun gnus-summary-display-make-predicate (display)
5129   (require 'gnus-agent)
5130   (when (= (length display) 1)
5131     (setq display (car display)))
5132   (unless gnus-summary-display-cache
5133     (dolist (elem (append '((unread . unread)
5134                             (read . read)
5135                             (unseen . unseen))
5136                           gnus-article-mark-lists))
5137       (push (cons (cdr elem)
5138                   (gnus-byte-compile
5139                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5140             gnus-summary-display-cache)))
5141   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5142         (gnus-category-predicate-cache gnus-summary-display-cache))
5143     (gnus-get-predicate display)))
5144
5145 ;; Uses the dynamically bound `number' variable.
5146 (eval-when-compile
5147   (defvar number))
5148 (defun gnus-article-marked-p (type &optional article)
5149   (let ((article (or article number)))
5150     (cond
5151      ((eq type 'tick)
5152       (memq article gnus-newsgroup-marked))
5153      ((eq type 'spam)
5154       (memq article gnus-newsgroup-spam-marked))
5155      ((eq type 'unsend)
5156       (memq article gnus-newsgroup-unsendable))
5157      ((eq type 'undownload)
5158       (memq article gnus-newsgroup-undownloaded))
5159      ((eq type 'download)
5160       (memq article gnus-newsgroup-downloadable))
5161      ((eq type 'unread)
5162       (memq article gnus-newsgroup-unreads))
5163      ((eq type 'read)
5164       (memq article gnus-newsgroup-reads))
5165      ((eq type 'dormant)
5166       (memq article gnus-newsgroup-dormant) )
5167      ((eq type 'expire)
5168       (memq article gnus-newsgroup-expirable))
5169      ((eq type 'reply)
5170       (memq article gnus-newsgroup-replied))
5171      ((eq type 'killed)
5172       (memq article gnus-newsgroup-killed))
5173      ((eq type 'bookmark)
5174       (assq article gnus-newsgroup-bookmarks))
5175      ((eq type 'score)
5176       (assq article gnus-newsgroup-scored))
5177      ((eq type 'save)
5178       (memq article gnus-newsgroup-saved))
5179      ((eq type 'cache)
5180       (memq article gnus-newsgroup-cached))
5181      ((eq type 'forward)
5182       (memq article gnus-newsgroup-forwarded))
5183      ((eq type 'seen)
5184       (not (memq article gnus-newsgroup-unseen)))
5185      ((eq type 'recent)
5186       (memq article gnus-newsgroup-recent))
5187      (t t))))
5188
5189 (defun gnus-articles-to-read (group &optional read-all)
5190   "Find out what articles the user wants to read."
5191   (let* ((display (gnus-group-find-parameter group 'display))
5192          (articles
5193           ;; Select all articles if `read-all' is non-nil, or if there
5194           ;; are no unread articles.
5195           (if (or read-all
5196                   (and (zerop (length gnus-newsgroup-marked))
5197                        (zerop (length gnus-newsgroup-unreads)))
5198                   ;; Fetch all if the predicate is non-nil.
5199                   gnus-newsgroup-display)
5200               ;; We want to select the headers for all the articles in
5201               ;; the group, so we select either all the active
5202               ;; articles in the group, or (if that's nil), the
5203               ;; articles in the cache.
5204               (or
5205                (gnus-uncompress-range (gnus-active group))
5206                (gnus-cache-articles-in-group group))
5207             ;; Select only the "normal" subset of articles.
5208             (gnus-sorted-nunion
5209              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5210              gnus-newsgroup-unreads)))
5211          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5212          (scored (length scored-list))
5213          (number (length articles))
5214          (marked (+ (length gnus-newsgroup-marked)
5215                     (length gnus-newsgroup-dormant)))
5216          (select
5217           (cond
5218            ((numberp read-all)
5219             read-all)
5220            ((numberp gnus-newsgroup-display)
5221             gnus-newsgroup-display)
5222            (t
5223             (condition-case ()
5224                 (cond
5225                  ((and (or (<= scored marked) (= scored number))
5226                        (numberp gnus-large-newsgroup)
5227                        (> number gnus-large-newsgroup))
5228                   (let* ((cursor-in-echo-area nil)
5229                          (initial (gnus-parameter-large-newsgroup-initial
5230                                    gnus-newsgroup-name))
5231                          (input
5232                           (read-string
5233                            (format
5234                             "How many articles from %s (%s %d): "
5235                             (gnus-limit-string
5236                              (gnus-group-decoded-name gnus-newsgroup-name)
5237                              35)
5238                             (if initial "max" "default")
5239                             number)
5240                            (if initial
5241                                (cons (number-to-string initial)
5242                                      0)))))
5243                     (if (string-match "^[ \t]*$" input) number input)))
5244                  ((and (> scored marked) (< scored number)
5245                        (> (- scored number) 20))
5246                   (let ((input
5247                          (read-string
5248                           (format "%s %s (%d scored, %d total): "
5249                                   "How many articles from"
5250                                   (gnus-group-decoded-name group)
5251                                   scored number))))
5252                     (if (string-match "^[ \t]*$" input)
5253                         number input)))
5254                  (t number))
5255               (quit
5256                (message "Quit getting the articles to read")
5257                nil))))))
5258     (setq select (if (stringp select) (string-to-number select) select))
5259     (if (or (null select) (zerop select))
5260         select
5261       (if (and (not (zerop scored)) (<= (abs select) scored))
5262           (progn
5263             (setq articles (sort scored-list '<))
5264             (setq number (length articles)))
5265         (setq articles (copy-sequence articles)))
5266
5267       (when (< (abs select) number)
5268         (if (< select 0)
5269             ;; Select the N oldest articles.
5270             (setcdr (nthcdr (1- (abs select)) articles) nil)
5271           ;; Select the N most recent articles.
5272           (setq articles (nthcdr (- number select) articles))))
5273       (setq gnus-newsgroup-unselected
5274             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5275       (when gnus-alter-articles-to-read-function
5276         (setq articles
5277               (sort
5278                (funcall gnus-alter-articles-to-read-function
5279                         gnus-newsgroup-name articles)
5280                '<)))
5281       articles)))
5282
5283 (defun gnus-killed-articles (killed articles)
5284   (let (out)
5285     (while articles
5286       (when (inline (gnus-member-of-range (car articles) killed))
5287         (push (car articles) out))
5288       (setq articles (cdr articles)))
5289     out))
5290
5291 (defun gnus-uncompress-marks (marks)
5292   "Uncompress the mark ranges in MARKS."
5293   (let ((uncompressed '(score bookmark))
5294         out)
5295     (while marks
5296       (if (memq (caar marks) uncompressed)
5297           (push (car marks) out)
5298         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5299       (setq marks (cdr marks)))
5300     out))
5301
5302 (defun gnus-article-mark-to-type (mark)
5303   "Return the type of MARK."
5304   (or (cadr (assq mark gnus-article-special-mark-lists))
5305       'list))
5306
5307 (defun gnus-article-unpropagatable-p (mark)
5308   "Return whether MARK should be propagated to backend."
5309   (memq mark gnus-article-unpropagated-mark-lists))
5310
5311 (defun gnus-adjust-marked-articles (info)
5312   "Set all article lists and remove all marks that are no longer valid."
5313   (let* ((marked-lists (gnus-info-marks info))
5314          (active (gnus-active (gnus-info-group info)))
5315          (min (car active))
5316          (max (cdr active))
5317          (types gnus-article-mark-lists)
5318          marks var articles article mark mark-type)
5319
5320     (dolist (marks marked-lists)
5321       (setq mark (car marks)
5322             mark-type (gnus-article-mark-to-type mark)
5323             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5324
5325       ;; We set the variable according to the type of the marks list,
5326       ;; and then adjust the marks to a subset of the active articles.
5327       (cond
5328        ;; Adjust "simple" lists.
5329        ((eq mark-type 'list)
5330         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5331         (when (memq mark '(tick dormant expire reply save))
5332           (while articles
5333             (when (or (< (setq article (pop articles)) min) (> article max))
5334               (set var (delq article (symbol-value var)))))))
5335        ;; Adjust assocs.
5336        ((eq mark-type 'tuple)
5337         (set var (setq articles (cdr marks)))
5338         (when (not (listp (cdr (symbol-value var))))
5339           (set var (list (symbol-value var))))
5340         (when (not (listp (cdr articles)))
5341           (setq articles (list articles)))
5342         (while articles
5343           (when (or (not (consp (setq article (pop articles))))
5344                     (< (car article) min)
5345                     (> (car article) max))
5346             (set var (delq article (symbol-value var))))))
5347        ;; Adjust ranges (sloppily).
5348        ((eq mark-type 'range)
5349         (cond
5350          ((eq mark 'seen)
5351           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5352           ;; It should be (seen (NUM1 . NUM2)).
5353           (when (numberp (cddr marks))
5354             (setcdr marks (list (cdr marks))))
5355           (setq articles (cdr marks))
5356           (while (and articles
5357                       (or (and (consp (car articles))
5358                                (> min (cdar articles)))
5359                           (and (numberp (car articles))
5360                                (> min (car articles)))))
5361             (pop articles))
5362           (set var articles))))))))
5363
5364 (defun gnus-update-missing-marks (missing)
5365   "Go through the list of MISSING articles and remove them from the mark lists."
5366   (when missing
5367     (let (var m)
5368       ;; Go through all types.
5369       (dolist (elem gnus-article-mark-lists)
5370         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5371           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5372           (when (symbol-value var)
5373             ;; This list has articles.  So we delete all missing
5374             ;; articles from it.
5375             (setq m missing)
5376             (while m
5377               (set var (delq (pop m) (symbol-value var))))))))))
5378
5379 (defun gnus-update-marks ()
5380   "Enter the various lists of marked articles into the newsgroup info list."
5381   (let ((types gnus-article-mark-lists)
5382         (info (gnus-get-info gnus-newsgroup-name))
5383         type list newmarked symbol delta-marks)
5384     (when info
5385       ;; Add all marks lists to the list of marks lists.
5386       (while (setq type (pop types))
5387         (setq list (symbol-value
5388                     (setq symbol
5389                           (intern (format "gnus-newsgroup-%s" (car type))))))
5390
5391         (when list
5392           ;; Get rid of the entries of the articles that have the
5393           ;; default score.
5394           (when (and (eq (cdr type) 'score)
5395                      gnus-save-score
5396                      list)
5397             (let* ((arts list)
5398                    (prev (cons nil list))
5399                    (all prev))
5400               (while arts
5401                 (if (or (not (consp (car arts)))
5402                         (= (cdar arts) gnus-summary-default-score))
5403                     (setcdr prev (cdr arts))
5404                   (setq prev arts))
5405                 (setq arts (cdr arts)))
5406               (setq list (cdr all)))))
5407
5408         (when (eq (cdr type) 'seen)
5409           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5410
5411         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5412           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5413
5414         (when (and (gnus-check-backend-function
5415                     'request-set-mark gnus-newsgroup-name)
5416                    (not (gnus-article-unpropagatable-p (cdr type))))
5417           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5418                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5419                  (add (gnus-remove-from-range
5420                        (gnus-copy-sequence list) old)))
5421             (when add
5422               (push (list add 'add (list (cdr type))) delta-marks))
5423             (when del
5424               (push (list del 'del (list (cdr type))) delta-marks))))
5425
5426         (when list
5427           (push (cons (cdr type) list) newmarked)))
5428
5429       (when delta-marks
5430         (unless (gnus-check-group gnus-newsgroup-name)
5431           (error "Can't open server for %s" gnus-newsgroup-name))
5432         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5433
5434       ;; Enter these new marks into the info of the group.
5435       (if (nthcdr 3 info)
5436           (setcar (nthcdr 3 info) newmarked)
5437         ;; Add the marks lists to the end of the info.
5438         (when newmarked
5439           (setcdr (nthcdr 2 info) (list newmarked))))
5440
5441       ;; Cut off the end of the info if there's nothing else there.
5442       (let ((i 5))
5443         (while (and (> i 2)
5444                     (not (nth i info)))
5445           (when (nthcdr (decf i) info)
5446             (setcdr (nthcdr i info) nil)))))))
5447
5448 (defun gnus-set-mode-line (where)
5449   "Set the mode line of the article or summary buffers.
5450 If WHERE is `summary', the summary mode line format will be used."
5451   ;; Is this mode line one we keep updated?
5452   (when (and (memq where gnus-updated-mode-lines)
5453              (symbol-value
5454               (intern (format "gnus-%s-mode-line-format-spec" where))))
5455     (let (mode-string)
5456       (save-excursion
5457         ;; We evaluate this in the summary buffer since these
5458         ;; variables are buffer-local to that buffer.
5459         (set-buffer gnus-summary-buffer)
5460        ;; We bind all these variables that are used in the `eval' form
5461         ;; below.
5462         (let* ((mformat (symbol-value
5463                          (intern
5464                           (format "gnus-%s-mode-line-format-spec" where))))
5465                (gnus-tmp-group-name (gnus-group-decoded-name
5466                                      gnus-newsgroup-name))
5467                (gnus-tmp-article-number (or gnus-current-article 0))
5468                (gnus-tmp-unread gnus-newsgroup-unreads)
5469                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5470                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5471                (gnus-tmp-unread-and-unselected
5472                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5473                             (zerop gnus-tmp-unselected))
5474                        "")
5475                       ((zerop gnus-tmp-unselected)
5476                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5477                       (t (format "{%d(+%d) more}"
5478                                  gnus-tmp-unread-and-unticked
5479                                  gnus-tmp-unselected))))
5480                (gnus-tmp-subject
5481                 (if (and gnus-current-headers
5482                          (vectorp gnus-current-headers))
5483                     (gnus-mode-string-quote
5484                      (mail-header-subject gnus-current-headers))
5485                   ""))
5486                bufname-length max-len
5487                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5488           (setq mode-string (eval mformat))
5489           (setq bufname-length (if (string-match "%b" mode-string)
5490                                    (- (length
5491                                        (buffer-name
5492                                         (if (eq where 'summary)
5493                                             nil
5494                                           (get-buffer gnus-article-buffer))))
5495                                       2)
5496                                  0))
5497           (setq max-len (max 4 (if gnus-mode-non-string-length
5498                                    (- (window-width)
5499                                       gnus-mode-non-string-length
5500                                       bufname-length)
5501                                  (length mode-string))))
5502           ;; We might have to chop a bit of the string off...
5503           (when (> (length mode-string) max-len)
5504             (setq mode-string
5505                   (concat (truncate-string-to-width mode-string (- max-len 3))
5506                           "...")))
5507           ;; Pad the mode string a bit.
5508           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5509       ;; Update the mode line.
5510       (setq mode-line-buffer-identification
5511             (gnus-mode-line-buffer-identification (list mode-string)))
5512       (set-buffer-modified-p t))))
5513
5514 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5515   "Go through the HEADERS list and add all Xrefs to a hash table.
5516 The resulting hash table is returned, or nil if no Xrefs were found."
5517   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5518          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5519          (xref-hashtb (gnus-make-hashtable))
5520          start group entry number xrefs header)
5521     (while headers
5522       (setq header (pop headers))
5523       (when (and (setq xrefs (mail-header-xref header))
5524                  (not (memq (setq number (mail-header-number header))
5525                             unreads)))
5526         (setq start 0)
5527         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5528           (setq start (match-end 0))
5529           (setq group (if prefix
5530                           (concat prefix (substring xrefs (match-beginning 1)
5531                                                     (match-end 1)))
5532                         (substring xrefs (match-beginning 1) (match-end 1))))
5533           (setq number
5534                 (string-to-int (substring xrefs (match-beginning 2)
5535                                           (match-end 2))))
5536           (if (setq entry (gnus-gethash group xref-hashtb))
5537               (setcdr entry (cons number (cdr entry)))
5538             (gnus-sethash group (cons number nil) xref-hashtb)))))
5539     (and start xref-hashtb)))
5540
5541 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5542   "Look through all the headers and mark the Xrefs as read."
5543   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5544         name entry info xref-hashtb idlist method nth4)
5545     (save-excursion
5546       (set-buffer gnus-group-buffer)
5547       (when (setq xref-hashtb
5548                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5549         (mapatoms
5550          (lambda (group)
5551            (unless (string= from-newsgroup (setq name (symbol-name group)))
5552              (setq idlist (symbol-value group))
5553              ;; Dead groups are not updated.
5554              (and (prog1
5555                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5556                             info (nth 2 entry))
5557                     (when (stringp (setq nth4 (gnus-info-method info)))
5558                       (setq nth4 (gnus-server-to-method nth4))))
5559                   ;; Only do the xrefs if the group has the same
5560                   ;; select method as the group we have just read.
5561                   (or (gnus-methods-equal-p
5562                        nth4 (gnus-find-method-for-group from-newsgroup))
5563                       virtual
5564                       (equal nth4 (setq method (gnus-find-method-for-group
5565                                                 from-newsgroup)))
5566                       (and (equal (car nth4) (car method))
5567                            (equal (nth 1 nth4) (nth 1 method))))
5568                   gnus-use-cross-reference
5569                   (or (not (eq gnus-use-cross-reference t))
5570                       virtual
5571                       ;; Only do cross-references on subscribed
5572                       ;; groups, if that is what is wanted.
5573                       (<= (gnus-info-level info) gnus-level-subscribed))
5574                   (gnus-group-make-articles-read name idlist))))
5575          xref-hashtb)))))
5576
5577 (defun gnus-compute-read-articles (group articles)
5578   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5579          (info (nth 2 entry))
5580          (active (gnus-active group))
5581          ninfo)
5582     (when entry
5583       ;; First peel off all invalid article numbers.
5584       (when active
5585         (let ((ids articles)
5586               id first)
5587           (while (setq id (pop ids))
5588             (when (and first (> id (cdr active)))
5589               ;; We'll end up in this situation in one particular
5590               ;; obscure situation.  If you re-scan a group and get
5591               ;; a new article that is cross-posted to a different
5592               ;; group that has not been re-scanned, you might get
5593               ;; crossposted article that has a higher number than
5594               ;; Gnus believes possible.  So we re-activate this
5595               ;; group as well.  This might mean doing the
5596               ;; crossposting thingy will *increase* the number
5597               ;; of articles in some groups.  Tsk, tsk.
5598               (setq active (or (gnus-activate-group group) active)))
5599             (when (or (> id (cdr active))
5600                       (< id (car active)))
5601               (setq articles (delq id articles))))))
5602       ;; If the read list is nil, we init it.
5603       (if (and active
5604                (null (gnus-info-read info))
5605                (> (car active) 1))
5606           (setq ninfo (cons 1 (1- (car active))))
5607         (setq ninfo (gnus-info-read info)))
5608       ;; Then we add the read articles to the range.
5609       (gnus-add-to-range
5610        ninfo (setq articles (sort articles '<))))))
5611
5612 (defun gnus-group-make-articles-read (group articles)
5613   "Update the info of GROUP to say that ARTICLES are read."
5614   (let* ((num 0)
5615          (entry (gnus-gethash group gnus-newsrc-hashtb))
5616          (info (nth 2 entry))
5617          (active (gnus-active group))
5618          range)
5619     (when entry
5620       (setq range (gnus-compute-read-articles group articles))
5621       (save-excursion
5622         (set-buffer gnus-group-buffer)
5623         (gnus-undo-register
5624           `(progn
5625              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5626              (gnus-info-set-read ',info ',(gnus-info-read info))
5627              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5628              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5629              (gnus-group-update-group ,group t))))
5630       ;; Add the read articles to the range.
5631       (gnus-info-set-read info range)
5632       (gnus-request-set-mark group (list (list range 'add '(read))))
5633       ;; Then we have to re-compute how many unread
5634       ;; articles there are in this group.
5635       (when active
5636         (cond
5637          ((not range)
5638           (setq num (- (1+ (cdr active)) (car active))))
5639          ((not (listp (cdr range)))
5640           (setq num (- (cdr active) (- (1+ (cdr range))
5641                                        (car range)))))
5642          (t
5643           (while range
5644             (if (numberp (car range))
5645                 (setq num (1+ num))
5646               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5647             (setq range (cdr range)))
5648           (setq num (- (cdr active) num))))
5649         ;; Update the number of unread articles.
5650         (setcar entry num)
5651         ;; Update the group buffer.
5652         (unless (gnus-ephemeral-group-p group)
5653           (gnus-group-update-group group t))))))
5654
5655 (defvar gnus-newsgroup-none-id 0)
5656
5657 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5658   (let ((cur nntp-server-buffer)
5659         (dependencies
5660          (or dependencies
5661              (save-excursion (set-buffer gnus-summary-buffer)
5662                              gnus-newsgroup-dependencies)))
5663         headers id end ref
5664         (mail-parse-charset gnus-newsgroup-charset)
5665         (mail-parse-ignored-charsets
5666          (save-excursion (condition-case nil
5667                              (set-buffer gnus-summary-buffer)
5668                            (error))
5669                          gnus-newsgroup-ignored-charsets)))
5670     (save-excursion
5671       (set-buffer nntp-server-buffer)
5672       ;; Translate all TAB characters into SPACE characters.
5673       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5674       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5675       (gnus-run-hooks 'gnus-parse-headers-hook)
5676       (let ((case-fold-search t)
5677             in-reply-to header p lines chars)
5678         (goto-char (point-min))
5679         ;; Search to the beginning of the next header.  Error messages
5680         ;; do not begin with 2 or 3.
5681         (while (re-search-forward "^[23][0-9]+ " nil t)
5682           (setq id nil
5683                 ref nil)
5684           ;; This implementation of this function, with nine
5685           ;; search-forwards instead of the one re-search-forward and
5686           ;; a case (which basically was the old function) is actually
5687           ;; about twice as fast, even though it looks messier.  You
5688           ;; can't have everything, I guess.  Speed and elegance
5689           ;; doesn't always go hand in hand.
5690           (setq
5691            header
5692            (vector
5693             ;; Number.
5694             (prog1
5695                 (read cur)
5696               (end-of-line)
5697               (setq p (point))
5698               (narrow-to-region (point)
5699                                 (or (and (search-forward "\n.\n" nil t)
5700                                          (- (point) 2))
5701                                     (point))))
5702             ;; Subject.
5703             (progn
5704               (goto-char p)
5705               (if (search-forward "\nsubject:" nil t)
5706                   (funcall gnus-decode-encoded-word-function
5707                            (nnheader-header-value))
5708                 "(none)"))
5709             ;; From.
5710             (progn
5711               (goto-char p)
5712               (if (search-forward "\nfrom:" nil t)
5713                   (funcall gnus-decode-encoded-word-function
5714                            (nnheader-header-value))
5715                 "(nobody)"))
5716             ;; Date.
5717             (progn
5718               (goto-char p)
5719               (if (search-forward "\ndate:" nil t)
5720                   (nnheader-header-value) ""))
5721             ;; Message-ID.
5722             (progn
5723               (goto-char p)
5724               (setq id (if (re-search-forward
5725                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5726                            ;; We do it this way to make sure the Message-ID
5727                            ;; is (somewhat) syntactically valid.
5728                            (buffer-substring (match-beginning 1)
5729                                              (match-end 1))
5730                          ;; If there was no message-id, we just fake one
5731                          ;; to make subsequent routines simpler.
5732                          (nnheader-generate-fake-message-id))))
5733             ;; References.
5734             (progn
5735               (goto-char p)
5736               (if (search-forward "\nreferences:" nil t)
5737                   (progn
5738                     (setq end (point))
5739                     (prog1
5740                         (nnheader-header-value)
5741                       (setq ref
5742                             (buffer-substring
5743                              (progn
5744                                (end-of-line)
5745                                (search-backward ">" end t)
5746                                (1+ (point)))
5747                              (progn
5748                                (search-backward "<" end t)
5749                                (point))))))
5750                 ;; Get the references from the in-reply-to header if there
5751                 ;; were no references and the in-reply-to header looks
5752                 ;; promising.
5753                 (if (and (search-forward "\nin-reply-to:" nil t)
5754                          (setq in-reply-to (nnheader-header-value))
5755                          (string-match "<[^>]+>" in-reply-to))
5756                     (let (ref2)
5757                       (setq ref (substring in-reply-to (match-beginning 0)
5758                                            (match-end 0)))
5759                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5760                         (setq ref2 (substring in-reply-to (match-beginning 0)
5761                                               (match-end 0)))
5762                         (when (> (length ref2) (length ref))
5763                           (setq ref ref2)))
5764                       ref)
5765                   (setq ref nil))))
5766             ;; Chars.
5767             (progn
5768               (goto-char p)
5769               (if (search-forward "\nchars: " nil t)
5770                   (if (numberp (setq chars (ignore-errors (read cur))))
5771                       chars -1)
5772                 -1))
5773             ;; Lines.
5774             (progn
5775               (goto-char p)
5776               (if (search-forward "\nlines: " nil t)
5777                   (if (numberp (setq lines (ignore-errors (read cur))))
5778                       lines -1)
5779                 -1))
5780             ;; Xref.
5781             (progn
5782               (goto-char p)
5783               (and (search-forward "\nxref:" nil t)
5784                    (nnheader-header-value)))
5785             ;; Extra.
5786             (when gnus-extra-headers
5787               (let ((extra gnus-extra-headers)
5788                     out)
5789                 (while extra
5790                   (goto-char p)
5791                   (when (search-forward
5792                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5793                     (push (cons (car extra) (nnheader-header-value))
5794                           out))
5795                   (pop extra))
5796                 out))))
5797           (when (equal id ref)
5798             (setq ref nil))
5799
5800           (when gnus-alter-header-function
5801             (funcall gnus-alter-header-function header)
5802             (setq id (mail-header-id header)
5803                   ref (gnus-parent-id (mail-header-references header))))
5804
5805           (when (setq header
5806                       (gnus-dependencies-add-header
5807                        header dependencies force-new))
5808             (push header headers))
5809           (goto-char (point-max))
5810           (widen))
5811         (nreverse headers)))))
5812
5813 ;; Goes through the xover lines and returns a list of vectors
5814 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5815                                                   force-new dependencies
5816                                                   group also-fetch-heads)
5817   "Parse the news overview data in the server buffer.
5818 Return a list of headers that match SEQUENCE (see
5819 `nntp-retrieve-headers')."
5820   ;; Get the Xref when the users reads the articles since most/some
5821   ;; NNTP servers do not include Xrefs when using XOVER.
5822   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5823   (let ((mail-parse-charset gnus-newsgroup-charset)
5824         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5825         (cur nntp-server-buffer)
5826         (dependencies (or dependencies gnus-newsgroup-dependencies))
5827         (allp (cond
5828                ((eq gnus-read-all-available-headers t)
5829                 t)
5830                ((stringp gnus-read-all-available-headers)
5831                 (string-match gnus-read-all-available-headers group))
5832                (t
5833                 nil)))
5834         number headers header)
5835     (save-excursion
5836       (set-buffer nntp-server-buffer)
5837       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5838       ;; Allow the user to mangle the headers before parsing them.
5839       (gnus-run-hooks 'gnus-parse-headers-hook)
5840       (goto-char (point-min))
5841       (gnus-parse-without-error
5842         (while (and (or sequence allp)
5843                     (not (eobp)))
5844           (setq number (read cur))
5845           (when (not allp)
5846             (while (and sequence
5847                         (< (car sequence) number))
5848               (setq sequence (cdr sequence))))
5849           (when (and (or allp
5850                          (and sequence
5851                               (eq number (car sequence))))
5852                      (progn
5853                        (setq sequence (cdr sequence))
5854                        (setq header (inline
5855                                       (gnus-nov-parse-line
5856                                        number dependencies force-new)))))
5857             (push header headers))
5858           (forward-line 1)))
5859       ;; A common bug in inn is that if you have posted an article and
5860       ;; then retrieves the active file, it will answer correctly --
5861       ;; the new article is included.  However, a NOV entry for the
5862       ;; article may not have been generated yet, so this may fail.
5863       ;; We work around this problem by retrieving the last few
5864       ;; headers using HEAD.
5865       (if (or (not also-fetch-heads)
5866               (not sequence))
5867           ;; We (probably) got all the headers.
5868           (nreverse headers)
5869         (let ((gnus-nov-is-evil t))
5870           (nconc
5871            (nreverse headers)
5872            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5873              (gnus-get-newsgroup-headers))))))))
5874
5875 (defun gnus-article-get-xrefs ()
5876   "Fill in the Xref value in `gnus-current-headers', if necessary.
5877 This is meant to be called in `gnus-article-internal-prepare-hook'."
5878   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5879                                  gnus-current-headers)))
5880     (or (not gnus-use-cross-reference)
5881         (not headers)
5882         (and (mail-header-xref headers)
5883              (not (string= (mail-header-xref headers) "")))
5884         (let ((case-fold-search t)
5885               xref)
5886           (save-restriction
5887             (nnheader-narrow-to-headers)
5888             (goto-char (point-min))
5889             (when (or (and (not (eobp))
5890                            (eq (downcase (char-after)) ?x)
5891                            (looking-at "Xref:"))
5892                       (search-forward "\nXref:" nil t))
5893               (goto-char (1+ (match-end 0)))
5894               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5895               (mail-header-set-xref headers xref)))))))
5896
5897 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5898   "Find article ID and insert the summary line for that article.
5899 OLD-HEADER can either be a header or a line number to insert
5900 the subject line on."
5901   (let* ((line (and (numberp old-header) old-header))
5902          (old-header (and (vectorp old-header) old-header))
5903          (header (cond ((and old-header use-old-header)
5904                         old-header)
5905                        ((and (numberp id)
5906                              (gnus-number-to-header id))
5907                         (gnus-number-to-header id))
5908                        (t
5909                         (gnus-read-header id))))
5910          (number (and (numberp id) id))
5911          d)
5912     (when header
5913       ;; Rebuild the thread that this article is part of and go to the
5914       ;; article we have fetched.
5915       (when (and (not gnus-show-threads)
5916                  old-header)
5917         (when (and number
5918                    (setq d (gnus-data-find (mail-header-number old-header))))
5919           (goto-char (gnus-data-pos d))
5920           (gnus-data-remove
5921            number
5922            (- (gnus-point-at-bol)
5923               (prog1
5924                   (1+ (gnus-point-at-eol))
5925                 (gnus-delete-line))))))
5926       (when old-header
5927         (mail-header-set-number header (mail-header-number old-header)))
5928       (setq gnus-newsgroup-sparse
5929             (delq (setq number (mail-header-number header))
5930                   gnus-newsgroup-sparse))
5931       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5932       (push number gnus-newsgroup-limit)
5933       (gnus-rebuild-thread (mail-header-id header) line)
5934       (gnus-summary-goto-subject number nil t))
5935     (when (and (numberp number)
5936                (> number 0))
5937       ;; We have to update the boundaries even if we can't fetch the
5938       ;; article if ID is a number -- so that the next `P' or `N'
5939       ;; command will fetch the previous (or next) article even
5940       ;; if the one we tried to fetch this time has been canceled.
5941       (when (> number gnus-newsgroup-end)
5942         (setq gnus-newsgroup-end number))
5943       (when (< number gnus-newsgroup-begin)
5944         (setq gnus-newsgroup-begin number))
5945       (setq gnus-newsgroup-unselected
5946             (delq number gnus-newsgroup-unselected)))
5947     ;; Report back a success?
5948     (and header (mail-header-number header))))
5949
5950 ;;; Process/prefix in the summary buffer
5951
5952 (defun gnus-summary-work-articles (n)
5953   "Return a list of articles to be worked upon.
5954 The prefix argument, the list of process marked articles, and the
5955 current article will be taken into consideration."
5956   (save-excursion
5957     (set-buffer gnus-summary-buffer)
5958     (cond
5959      (n
5960       ;; A numerical prefix has been given.
5961       (setq n (prefix-numeric-value n))
5962       (let ((backward (< n 0))
5963             (n (abs (prefix-numeric-value n)))
5964             articles article)
5965         (save-excursion
5966           (while
5967               (and (> n 0)
5968                    (push (setq article (gnus-summary-article-number))
5969                          articles)
5970                    (if backward
5971                        (gnus-summary-find-prev nil article)
5972                      (gnus-summary-find-next nil article)))
5973             (decf n)))
5974         (nreverse articles)))
5975      ((and (gnus-region-active-p) (mark))
5976       (message "region active")
5977       ;; Work on the region between point and mark.
5978       (let ((max (max (point) (mark)))
5979             articles article)
5980         (save-excursion
5981           (goto-char (min (min (point) (mark))))
5982           (while
5983               (and
5984                (push (setq article (gnus-summary-article-number)) articles)
5985                (gnus-summary-find-next nil article)
5986                (< (point) max)))
5987           (nreverse articles))))
5988      (gnus-newsgroup-processable
5989       ;; There are process-marked articles present.
5990       ;; Save current state.
5991       (gnus-summary-save-process-mark)
5992       ;; Return the list.
5993       (reverse gnus-newsgroup-processable))
5994      (t
5995       ;; Just return the current article.
5996       (list (gnus-summary-article-number))))))
5997
5998 (defmacro gnus-summary-iterate (arg &rest forms)
5999   "Iterate over the process/prefixed articles and do FORMS.
6000 ARG is the interactive prefix given to the command.  FORMS will be
6001 executed with point over the summary line of the articles."
6002   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6003     `(let ((,articles (gnus-summary-work-articles ,arg)))
6004        (while ,articles
6005          (gnus-summary-goto-subject (car ,articles))
6006          ,@forms
6007          (pop ,articles)))))
6008
6009 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6010 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6011
6012 (defun gnus-summary-save-process-mark ()
6013   "Push the current set of process marked articles on the stack."
6014   (interactive)
6015   (push (copy-sequence gnus-newsgroup-processable)
6016         gnus-newsgroup-process-stack))
6017
6018 (defun gnus-summary-kill-process-mark ()
6019   "Push the current set of process marked articles on the stack and unmark."
6020   (interactive)
6021   (gnus-summary-save-process-mark)
6022   (gnus-summary-unmark-all-processable))
6023
6024 (defun gnus-summary-yank-process-mark ()
6025   "Pop the last process mark state off the stack and restore it."
6026   (interactive)
6027   (unless gnus-newsgroup-process-stack
6028     (error "Empty mark stack"))
6029   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6030
6031 (defun gnus-summary-process-mark-set (set)
6032   "Make SET into the current process marked articles."
6033   (gnus-summary-unmark-all-processable)
6034   (while set
6035     (gnus-summary-set-process-mark (pop set))))
6036
6037 ;;; Searching and stuff
6038
6039 (defun gnus-summary-search-group (&optional backward use-level)
6040   "Search for next unread newsgroup.
6041 If optional argument BACKWARD is non-nil, search backward instead."
6042   (save-excursion
6043     (set-buffer gnus-group-buffer)
6044     (when (gnus-group-search-forward
6045            backward nil (if use-level (gnus-group-group-level) nil))
6046       (gnus-group-group-name))))
6047
6048 (defun gnus-summary-best-group (&optional exclude-group)
6049   "Find the name of the best unread group.
6050 If EXCLUDE-GROUP, do not go to this group."
6051   (save-excursion
6052     (set-buffer gnus-group-buffer)
6053     (save-excursion
6054       (gnus-group-best-unread-group exclude-group))))
6055
6056 (defun gnus-summary-find-next (&optional unread article backward)
6057   (if backward (gnus-summary-find-prev unread article)
6058     (let* ((dummy (gnus-summary-article-intangible-p))
6059            (article (or article (gnus-summary-article-number)))
6060            (data (gnus-data-find-list article))
6061            result)
6062       (when (and (not dummy)
6063                  (or (not gnus-summary-check-current)
6064                      (not unread)
6065                      (not (gnus-data-unread-p (car data)))))
6066         (setq data (cdr data)))
6067       (when (setq result
6068                   (if unread
6069                       (progn
6070                         (while data
6071                           (unless (memq (gnus-data-number (car data)) 
6072                                         (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6073                                                gnus-newsgroup-undownloaded)
6074                                               (gnus-plugged
6075                                                nil)
6076                                               ((eq gnus-auto-goto-ignores 'unfetched)
6077                                                gnus-newsgroup-unfetched)
6078                                               ((eq gnus-auto-goto-ignores 'undownloaded)
6079                                                gnus-newsgroup-undownloaded)))
6080                             (when (gnus-data-unread-p (car data))
6081                               (setq result (car data)
6082                                     data nil)))
6083                           (setq data (cdr data)))
6084                         result)
6085                     (car data)))
6086         (goto-char (gnus-data-pos result))
6087         (gnus-data-number result)))))
6088
6089 (defun gnus-summary-find-prev (&optional unread article)
6090   (let* ((eobp (eobp))
6091          (article (or article (gnus-summary-article-number)))
6092          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6093          result)
6094     (when (and (not eobp)
6095                (or (not gnus-summary-check-current)
6096                    (not unread)
6097                    (not (gnus-data-unread-p (car data)))))
6098       (setq data (cdr data)))
6099     (when (setq result
6100                 (if unread
6101                     (progn
6102                       (while data
6103                         (unless (memq (gnus-data-number (car data))
6104                                       (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6105                                              gnus-newsgroup-undownloaded)
6106                                             (gnus-plugged
6107                                              nil)
6108                                             ((eq gnus-auto-goto-ignores 'unfetched)
6109                                              gnus-newsgroup-unfetched)
6110                                             ((eq gnus-auto-goto-ignores 'undownloaded)
6111                                              gnus-newsgroup-undownloaded)))
6112                           (when (gnus-data-unread-p (car data))
6113                             (setq result (car data)
6114                                   data nil)))
6115                         (setq data (cdr data)))
6116                       result)
6117                   (car data)))
6118       (goto-char (gnus-data-pos result))
6119       (gnus-data-number result))))
6120
6121 (defun gnus-summary-find-subject (subject &optional unread backward article)
6122   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6123          (article (or article (gnus-summary-article-number)))
6124          (articles (gnus-data-list backward))
6125          (arts (gnus-data-find-list article articles))
6126          result)
6127     (when (or (not gnus-summary-check-current)
6128               (not unread)
6129               (not (gnus-data-unread-p (car arts))))
6130       (setq arts (cdr arts)))
6131     (while arts
6132       (and (or (not unread)
6133                (gnus-data-unread-p (car arts)))
6134            (vectorp (gnus-data-header (car arts)))
6135            (gnus-subject-equal
6136             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6137            (setq result (car arts)
6138                  arts nil))
6139       (setq arts (cdr arts)))
6140     (and result
6141          (goto-char (gnus-data-pos result))
6142          (gnus-data-number result))))
6143
6144 (defun gnus-summary-search-forward (&optional unread subject backward)
6145   "Search forward for an article.
6146 If UNREAD, look for unread articles.  If SUBJECT, look for
6147 articles with that subject.  If BACKWARD, search backward instead."
6148   (cond (subject (gnus-summary-find-subject subject unread backward))
6149         (backward (gnus-summary-find-prev unread))
6150         (t (gnus-summary-find-next unread))))
6151
6152 (defun gnus-recenter (&optional n)
6153   "Center point in window and redisplay frame.
6154 Also do horizontal recentering."
6155   (interactive "P")
6156   (when (and gnus-auto-center-summary
6157              (not (eq gnus-auto-center-summary 'vertical)))
6158     (gnus-horizontal-recenter))
6159   (recenter n))
6160
6161 (defun gnus-summary-recenter ()
6162   "Center point in the summary window.
6163 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6164 displayed, no centering will be performed."
6165   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6166   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6167   (interactive)
6168   ;; The user has to want it.
6169   (when gnus-auto-center-summary
6170     (let* ((top (cond ((< (window-height) 4) 0)
6171                       ((< (window-height) 7) 1)
6172                       (t (if (numberp gnus-auto-center-summary)
6173                              gnus-auto-center-summary
6174                            2))))
6175            (height (1- (window-height)))
6176            (bottom (save-excursion (goto-char (point-max))
6177                                    (forward-line (- height))
6178                                    (point)))
6179            (window (get-buffer-window (current-buffer))))
6180       (when (get-buffer-window gnus-article-buffer)
6181         ;; Only do recentering when the article buffer is displayed,
6182         ;; Set the window start to either `bottom', which is the biggest
6183         ;; possible valid number, or the second line from the top,
6184         ;; whichever is the least.
6185         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6186           (if (> bottom top-pos)
6187               ;; Keep the second line from the top visible
6188               (set-window-start window top-pos t)
6189             ;; Try to keep the bottom line visible; if it's partially
6190             ;; obscured, either scroll one more line to make it fully
6191             ;; visible, or revert to using TOP-POS.
6192             (save-excursion
6193               (goto-char (point-max))
6194               (forward-line -1)
6195               (let ((last-line-start (point)))
6196                 (goto-char bottom)
6197                 (set-window-start window (point) t)
6198                 (when (not (pos-visible-in-window-p last-line-start window))
6199                   (forward-line 1)
6200                   (set-window-start window (min (point) top-pos) t)))))))
6201       ;; Do horizontal recentering while we're at it.
6202       (when (and (get-buffer-window (current-buffer) t)
6203                  (not (eq gnus-auto-center-summary 'vertical)))
6204         (let ((selected (selected-window)))
6205           (select-window (get-buffer-window (current-buffer) t))
6206           (gnus-summary-position-point)
6207           (gnus-horizontal-recenter)
6208           (select-window selected))))))
6209
6210 (defun gnus-summary-jump-to-group (newsgroup)
6211   "Move point to NEWSGROUP in group mode buffer."
6212   ;; Keep update point of group mode buffer if visible.
6213   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6214       (save-window-excursion
6215         ;; Take care of tree window mode.
6216         (when (get-buffer-window gnus-group-buffer)
6217           (pop-to-buffer gnus-group-buffer))
6218         (gnus-group-jump-to-group newsgroup))
6219     (save-excursion
6220       ;; Take care of tree window mode.
6221       (if (get-buffer-window gnus-group-buffer)
6222           (pop-to-buffer gnus-group-buffer)
6223         (set-buffer gnus-group-buffer))
6224       (gnus-group-jump-to-group newsgroup))))
6225
6226 ;; This function returns a list of article numbers based on the
6227 ;; difference between the ranges of read articles in this group and
6228 ;; the range of active articles.
6229 (defun gnus-list-of-unread-articles (group)
6230   (let* ((read (gnus-info-read (gnus-get-info group)))
6231          (active (or (gnus-active group) (gnus-activate-group group)))
6232          (last (cdr active))
6233          first nlast unread)
6234     ;; If none are read, then all are unread.
6235     (if (not read)
6236         (setq first (car active))
6237       ;; If the range of read articles is a single range, then the
6238       ;; first unread article is the article after the last read
6239       ;; article.  Sounds logical, doesn't it?
6240       (if (and (not (listp (cdr read)))
6241                (or (< (car read) (car active))
6242                    (progn (setq read (list read))
6243                           nil)))
6244           (setq first (max (car active) (1+ (cdr read))))
6245         ;; `read' is a list of ranges.
6246         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6247                                   (caar read)))
6248                   1)
6249           (setq first (car active)))
6250         (while read
6251           (when first
6252             (while (< first nlast)
6253               (push first unread)
6254               (setq first (1+ first))))
6255           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6256           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6257           (setq read (cdr read)))))
6258     ;; And add the last unread articles.
6259     (while (<= first last)
6260       (push first unread)
6261       (setq first (1+ first)))
6262     ;; Return the list of unread articles.
6263     (delq 0 (nreverse unread))))
6264
6265 (defun gnus-list-of-read-articles (group)
6266   "Return a list of unread, unticked and non-dormant articles."
6267   (let* ((info (gnus-get-info group))
6268          (marked (gnus-info-marks info))
6269          (active (gnus-active group)))
6270     (and info active
6271          (gnus-list-range-difference
6272           (gnus-list-range-difference
6273            (gnus-sorted-complement
6274             (gnus-uncompress-range active)
6275             (gnus-list-of-unread-articles group))
6276            (cdr (assq 'dormant marked)))
6277           (cdr (assq 'tick marked))))))
6278
6279 ;; Various summary commands
6280
6281 (defun gnus-summary-select-article-buffer ()
6282   "Reconfigure windows to show article buffer."
6283   (interactive)
6284   (if (not (gnus-buffer-live-p gnus-article-buffer))
6285       (error "There is no article buffer for this summary buffer")
6286     (gnus-configure-windows 'article)
6287     (select-window (get-buffer-window gnus-article-buffer))))
6288
6289 (defun gnus-summary-universal-argument (arg)
6290   "Perform any operation on all articles that are process/prefixed."
6291   (interactive "P")
6292   (let ((articles (gnus-summary-work-articles arg))
6293         func article)
6294     (if (eq
6295          (setq
6296           func
6297           (key-binding
6298            (read-key-sequence
6299             (substitute-command-keys
6300              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6301          'undefined)
6302         (gnus-error 1 "Undefined key")
6303       (save-excursion
6304         (while articles
6305           (gnus-summary-goto-subject (setq article (pop articles)))
6306           (let (gnus-newsgroup-processable)
6307             (command-execute func))
6308           (gnus-summary-remove-process-mark article)))))
6309   (gnus-summary-position-point))
6310
6311 (defun gnus-summary-toggle-truncation (&optional arg)
6312   "Toggle truncation of summary lines.
6313 With arg, turn line truncation on if arg is positive."
6314   (interactive "P")
6315   (setq truncate-lines
6316         (if (null arg) (not truncate-lines)
6317           (> (prefix-numeric-value arg) 0)))
6318   (redraw-display))
6319
6320 (defun gnus-summary-find-for-reselect ()
6321   "Return the number of an article to stay on across a reselect.
6322 The current article is considered, then following articles, then previous
6323 articles.  An article is sought which is not cancelled and isn't a temporary
6324 insertion from another group.  If there's no such then return a dummy 0."
6325   (let (found)
6326     (dolist (rev '(nil t))
6327       (unless found      ; don't demand the reverse list if we don't need it
6328         (let ((data (gnus-data-find-list
6329                      (gnus-summary-article-number) (gnus-data-list rev))))
6330           (while (and data (not found))
6331             (if (and (< 0 (gnus-data-number (car data)))
6332                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6333                 (setq found (gnus-data-number (car data))))
6334             (setq data (cdr data))))))
6335     (or found 0)))
6336
6337 (defun gnus-summary-reselect-current-group (&optional all rescan)
6338   "Exit and then reselect the current newsgroup.
6339 The prefix argument ALL means to select all articles."
6340   (interactive "P")
6341   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6342     (error "Ephemeral groups can't be reselected"))
6343   (let ((current-subject (gnus-summary-find-for-reselect))
6344         (group gnus-newsgroup-name))
6345     (setq gnus-newsgroup-begin nil)
6346     (gnus-summary-exit)
6347     ;; We have to adjust the point of group mode buffer because
6348     ;; point was moved to the next unread newsgroup by exiting.
6349     (gnus-summary-jump-to-group group)
6350     (when rescan
6351       (save-excursion
6352         (gnus-group-get-new-news-this-group 1)))
6353     (gnus-group-read-group all t)
6354     (gnus-summary-goto-subject current-subject nil t)))
6355
6356 (defun gnus-summary-rescan-group (&optional all)
6357   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6358   (interactive "P")
6359   (gnus-summary-reselect-current-group all t))
6360
6361 (defun gnus-summary-update-info (&optional non-destructive)
6362   (save-excursion
6363     (let ((group gnus-newsgroup-name))
6364       (when group
6365         (when gnus-newsgroup-kill-headers
6366           (setq gnus-newsgroup-killed
6367                 (gnus-compress-sequence
6368                  (gnus-sorted-union
6369                   (gnus-list-range-intersection
6370                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6371                   gnus-newsgroup-unreads)
6372                  t)))
6373         (unless (listp (cdr gnus-newsgroup-killed))
6374           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6375         (let ((headers gnus-newsgroup-headers))
6376           ;; Set the new ranges of read articles.
6377           (save-excursion
6378             (set-buffer gnus-group-buffer)
6379             (gnus-undo-force-boundary))
6380           (gnus-update-read-articles
6381            group (gnus-sorted-union
6382                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6383           ;; Set the current article marks.
6384           (let ((gnus-newsgroup-scored
6385                  (if (and (not gnus-save-score)
6386                           (not non-destructive))
6387                      nil
6388                    gnus-newsgroup-scored)))
6389             (save-excursion
6390               (gnus-update-marks)))
6391           ;; Do the cross-ref thing.
6392           (when gnus-use-cross-reference
6393             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6394           ;; Do not switch windows but change the buffer to work.
6395           (set-buffer gnus-group-buffer)
6396           (unless (gnus-ephemeral-group-p group)
6397             (gnus-group-update-group group)))))))
6398
6399 (defun gnus-summary-save-newsrc (&optional force)
6400   "Save the current number of read/marked articles in the dribble buffer.
6401 The dribble buffer will then be saved.
6402 If FORCE (the prefix), also save the .newsrc file(s)."
6403   (interactive "P")
6404   (gnus-summary-update-info t)
6405   (if force
6406       (gnus-save-newsrc-file)
6407     (gnus-dribble-save)))
6408
6409 (defun gnus-summary-exit (&optional temporary)
6410   "Exit reading current newsgroup, and then return to group selection mode.
6411 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6412   (interactive)
6413   (gnus-set-global-variables)
6414   (when (gnus-buffer-live-p gnus-article-buffer)
6415     (save-excursion
6416       (set-buffer gnus-article-buffer)
6417       (mm-destroy-parts gnus-article-mime-handles)
6418       ;; Set it to nil for safety reason.
6419       (setq gnus-article-mime-handle-alist nil)
6420       (setq gnus-article-mime-handles nil)))
6421   (gnus-kill-save-kill-buffer)
6422   (gnus-async-halt-prefetch)
6423   (let* ((group gnus-newsgroup-name)
6424          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6425          (gnus-group-is-exiting-p t)
6426          (mode major-mode)
6427          (group-point nil)
6428          (buf (current-buffer)))
6429     (unless quit-config
6430       ;; Do adaptive scoring, and possibly save score files.
6431       (when gnus-newsgroup-adaptive
6432         (gnus-score-adaptive))
6433       (when gnus-use-scoring
6434         (gnus-score-save)))
6435     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6436     ;; If we have several article buffers, we kill them at exit.
6437     (unless gnus-single-article-buffer
6438       (gnus-kill-buffer gnus-original-article-buffer)
6439       (setq gnus-article-current nil))
6440     (when gnus-use-cache
6441       (gnus-cache-possibly-remove-articles)
6442       (gnus-cache-save-buffers))
6443     (gnus-async-prefetch-remove-group group)
6444     (when gnus-suppress-duplicates
6445       (gnus-dup-enter-articles))
6446     (when gnus-use-trees
6447       (gnus-tree-close group))
6448     (when gnus-use-cache
6449       (gnus-cache-write-active))
6450     ;; Remove entries for this group.
6451     (nnmail-purge-split-history (gnus-group-real-name group))
6452     ;; Make all changes in this group permanent.
6453     (unless quit-config
6454       (gnus-run-hooks 'gnus-exit-group-hook)
6455       (gnus-summary-update-info))
6456     (gnus-close-group group)
6457     ;; Make sure where we were, and go to next newsgroup.
6458     (set-buffer gnus-group-buffer)
6459     (unless quit-config
6460       (gnus-group-jump-to-group group))
6461     (gnus-run-hooks 'gnus-summary-exit-hook)
6462     (unless (or quit-config
6463                 ;; If this group has disappeared from the summary
6464                 ;; buffer, don't skip forwards.
6465                 (not (string= group (gnus-group-group-name))))
6466       (gnus-group-next-unread-group 1))
6467     (setq group-point (point))
6468     (if temporary
6469         nil                             ;Nothing to do.
6470       ;; If we have several article buffers, we kill them at exit.
6471       (unless gnus-single-article-buffer
6472         (gnus-kill-buffer gnus-article-buffer)
6473         (gnus-kill-buffer gnus-original-article-buffer)
6474         (setq gnus-article-current nil))
6475       (set-buffer buf)
6476       (if (not gnus-kill-summary-on-exit)
6477           (progn
6478             (gnus-deaden-summary)
6479             (setq mode nil))
6480         ;; We set all buffer-local variables to nil.  It is unclear why
6481         ;; this is needed, but if we don't, buffer-local variables are
6482         ;; not garbage-collected, it seems.  This would the lead to en
6483         ;; ever-growing Emacs.
6484         (gnus-summary-clear-local-variables)
6485         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6486           (gnus-summary-clear-local-variables))
6487         (when (get-buffer gnus-article-buffer)
6488           (bury-buffer gnus-article-buffer))
6489         ;; We clear the global counterparts of the buffer-local
6490         ;; variables as well, just to be on the safe side.
6491         (set-buffer gnus-group-buffer)
6492         (gnus-summary-clear-local-variables)
6493         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6494           (gnus-summary-clear-local-variables))
6495         ;; Return to group mode buffer.
6496         (when (eq mode 'gnus-summary-mode)
6497           (gnus-kill-buffer buf)))
6498       (setq gnus-current-select-method gnus-select-method)
6499       (pop-to-buffer gnus-group-buffer)
6500       (if (not quit-config)
6501           (progn
6502             (goto-char group-point)
6503             (gnus-configure-windows 'group 'force))
6504         (gnus-handle-ephemeral-exit quit-config))
6505       ;; Clear the current group name.
6506       (unless quit-config
6507         (setq gnus-newsgroup-name nil)))))
6508
6509 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6510 (defun gnus-summary-exit-no-update (&optional no-questions)
6511   "Quit reading current newsgroup without updating read article info."
6512   (interactive)
6513   (let* ((group gnus-newsgroup-name)
6514          (gnus-group-is-exiting-p t)
6515          (gnus-group-is-exiting-without-update-p t)
6516          (quit-config (gnus-group-quit-config group)))
6517     (when (or no-questions
6518               gnus-expert-user
6519               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6520       (gnus-async-halt-prefetch)
6521       (run-hooks 'gnus-summary-prepare-exit-hook)
6522       (when (gnus-buffer-live-p gnus-article-buffer)
6523         (save-excursion
6524           (set-buffer gnus-article-buffer)
6525           (mm-destroy-parts gnus-article-mime-handles)
6526           ;; Set it to nil for safety reason.
6527           (setq gnus-article-mime-handle-alist nil)
6528           (setq gnus-article-mime-handles nil)))
6529       ;; If we have several article buffers, we kill them at exit.
6530       (unless gnus-single-article-buffer
6531         (gnus-kill-buffer gnus-article-buffer)
6532         (gnus-kill-buffer gnus-original-article-buffer)
6533         (setq gnus-article-current nil))
6534       (if (not gnus-kill-summary-on-exit)
6535           (gnus-deaden-summary)
6536         (gnus-close-group group)
6537         (gnus-summary-clear-local-variables)
6538         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6539           (gnus-summary-clear-local-variables))
6540         (set-buffer gnus-group-buffer)
6541         (gnus-summary-clear-local-variables)
6542         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6543           (gnus-summary-clear-local-variables))
6544         (gnus-kill-buffer gnus-summary-buffer))
6545       (unless gnus-single-article-buffer
6546         (setq gnus-article-current nil))
6547       (when gnus-use-trees
6548         (gnus-tree-close group))
6549       (gnus-async-prefetch-remove-group group)
6550       (when (get-buffer gnus-article-buffer)
6551         (bury-buffer gnus-article-buffer))
6552       ;; Return to the group buffer.
6553       (gnus-configure-windows 'group 'force)
6554       ;; Clear the current group name.
6555       (setq gnus-newsgroup-name nil)
6556       (unless (gnus-ephemeral-group-p group)
6557         (gnus-group-update-group group))
6558       (when (equal (gnus-group-group-name) group)
6559         (gnus-group-next-unread-group 1))
6560       (when quit-config
6561         (gnus-handle-ephemeral-exit quit-config)))))
6562
6563 (defun gnus-handle-ephemeral-exit (quit-config)
6564   "Handle movement when leaving an ephemeral group.
6565 The state which existed when entering the ephemeral is reset."
6566   (if (not (buffer-name (car quit-config)))
6567       (gnus-configure-windows 'group 'force)
6568     (set-buffer (car quit-config))
6569     (cond ((eq major-mode 'gnus-summary-mode)
6570            (gnus-set-global-variables))
6571           ((eq major-mode 'gnus-article-mode)
6572            (save-excursion
6573              ;; The `gnus-summary-buffer' variable may point
6574              ;; to the old summary buffer when using a single
6575              ;; article buffer.
6576              (unless (gnus-buffer-live-p gnus-summary-buffer)
6577                (set-buffer gnus-group-buffer))
6578              (set-buffer gnus-summary-buffer)
6579              (gnus-set-global-variables))))
6580     (if (or (eq (cdr quit-config) 'article)
6581             (eq (cdr quit-config) 'pick))
6582         (progn
6583           ;; The current article may be from the ephemeral group
6584           ;; thus it is best that we reload this article
6585           (gnus-summary-show-article)
6586           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6587               (gnus-configure-windows 'pick 'force)
6588             (gnus-configure-windows (cdr quit-config) 'force)))
6589       (gnus-configure-windows (cdr quit-config) 'force))
6590     (when (eq major-mode 'gnus-summary-mode)
6591       (gnus-summary-next-subject 1 nil t)
6592       (gnus-summary-recenter)
6593       (gnus-summary-position-point))))
6594
6595 ;;; Dead summaries.
6596
6597 (defvar gnus-dead-summary-mode-map nil)
6598
6599 (unless gnus-dead-summary-mode-map
6600   (setq gnus-dead-summary-mode-map (make-keymap))
6601   (suppress-keymap gnus-dead-summary-mode-map)
6602   (substitute-key-definition
6603    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6604   (dolist (key '("\C-d" "\r" "\177" [delete]))
6605     (define-key gnus-dead-summary-mode-map
6606       key 'gnus-summary-wake-up-the-dead))
6607   (dolist (key '("q" "Q"))
6608     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6609
6610 (defvar gnus-dead-summary-mode nil
6611   "Minor mode for Gnus summary buffers.")
6612
6613 (defun gnus-dead-summary-mode (&optional arg)
6614   "Minor mode for Gnus summary buffers."
6615   (interactive "P")
6616   (when (eq major-mode 'gnus-summary-mode)
6617     (make-local-variable 'gnus-dead-summary-mode)
6618     (setq gnus-dead-summary-mode
6619           (if (null arg) (not gnus-dead-summary-mode)
6620             (> (prefix-numeric-value arg) 0)))
6621     (when gnus-dead-summary-mode
6622       (gnus-add-minor-mode
6623        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6624
6625 (defun gnus-deaden-summary ()
6626   "Make the current summary buffer into a dead summary buffer."
6627   ;; Kill any previous dead summary buffer.
6628   (when (and gnus-dead-summary
6629              (buffer-name gnus-dead-summary))
6630     (save-excursion
6631       (set-buffer gnus-dead-summary)
6632       (when gnus-dead-summary-mode
6633         (kill-buffer (current-buffer)))))
6634   ;; Make this the current dead summary.
6635   (setq gnus-dead-summary (current-buffer))
6636   (gnus-dead-summary-mode 1)
6637   (let ((name (buffer-name)))
6638     (when (string-match "Summary" name)
6639       (rename-buffer
6640        (concat (substring name 0 (match-beginning 0)) "Dead "
6641                (substring name (match-beginning 0)))
6642        t)
6643       (bury-buffer))))
6644
6645 (defun gnus-kill-or-deaden-summary (buffer)
6646   "Kill or deaden the summary BUFFER."
6647   (save-excursion
6648     (when (and (buffer-name buffer)
6649                (not gnus-single-article-buffer))
6650       (save-excursion
6651         (set-buffer buffer)
6652         (gnus-kill-buffer gnus-article-buffer)
6653         (gnus-kill-buffer gnus-original-article-buffer)))
6654     (cond
6655      ;; Kill the buffer.
6656      (gnus-kill-summary-on-exit
6657       (when (and gnus-use-trees
6658                  (gnus-buffer-exists-p buffer))
6659         (save-excursion
6660           (set-buffer buffer)
6661           (gnus-tree-close gnus-newsgroup-name)))
6662       (gnus-kill-buffer buffer))
6663      ;; Deaden the buffer.
6664      ((gnus-buffer-exists-p buffer)
6665       (save-excursion
6666         (set-buffer buffer)
6667         (gnus-deaden-summary))))))
6668
6669 (defun gnus-summary-wake-up-the-dead (&rest args)
6670   "Wake up the dead summary buffer."
6671   (interactive)
6672   (gnus-dead-summary-mode -1)
6673   (let ((name (buffer-name)))
6674     (when (string-match "Dead " name)
6675       (rename-buffer
6676        (concat (substring name 0 (match-beginning 0))
6677                (substring name (match-end 0)))
6678        t)))
6679   (gnus-message 3 "This dead summary is now alive again"))
6680
6681 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6682 (defun gnus-summary-fetch-faq (&optional faq-dir)
6683   "Fetch the FAQ for the current group.
6684 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6685 in."
6686   (interactive
6687    (list
6688     (when current-prefix-arg
6689       (completing-read
6690        "FAQ dir: " (and (listp gnus-group-faq-directory)
6691                         (mapcar (lambda (file) (list file))
6692                                 gnus-group-faq-directory))))))
6693   (let (gnus-faq-buffer)
6694     (when (setq gnus-faq-buffer
6695                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6696       (gnus-configure-windows 'summary-faq))))
6697
6698 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6699 (defun gnus-summary-describe-group (&optional force)
6700   "Describe the current newsgroup."
6701   (interactive "P")
6702   (gnus-group-describe-group force gnus-newsgroup-name))
6703
6704 (defun gnus-summary-describe-briefly ()
6705   "Describe summary mode commands briefly."
6706   (interactive)
6707   (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")))
6708
6709 ;; Walking around group mode buffer from summary mode.
6710
6711 (defun gnus-summary-next-group (&optional no-article target-group backward)
6712   "Exit current newsgroup and then select next unread newsgroup.
6713 If prefix argument NO-ARTICLE is non-nil, no article is selected
6714 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6715 previous group instead."
6716   (interactive "P")
6717   ;; Stop pre-fetching.
6718   (gnus-async-halt-prefetch)
6719   (let ((current-group gnus-newsgroup-name)
6720         (current-buffer (current-buffer))
6721         entered)
6722     ;; First we semi-exit this group to update Xrefs and all variables.
6723     ;; We can't do a real exit, because the window conf must remain
6724     ;; the same in case the user is prompted for info, and we don't
6725     ;; want the window conf to change before that...
6726     (gnus-summary-exit t)
6727     (while (not entered)
6728       ;; Then we find what group we are supposed to enter.
6729       (set-buffer gnus-group-buffer)
6730       (gnus-group-jump-to-group current-group)
6731       (setq target-group
6732             (or target-group
6733                 (if (eq gnus-keep-same-level 'best)
6734                     (gnus-summary-best-group gnus-newsgroup-name)
6735                   (gnus-summary-search-group backward gnus-keep-same-level))))
6736       (if (not target-group)
6737           ;; There are no further groups, so we return to the group
6738           ;; buffer.
6739           (progn
6740             (gnus-message 5 "Returning to the group buffer")
6741             (setq entered t)
6742             (when (gnus-buffer-live-p current-buffer)
6743               (set-buffer current-buffer)
6744               (gnus-summary-exit))
6745             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6746         ;; We try to enter the target group.
6747         (gnus-group-jump-to-group target-group)
6748         (let ((unreads (gnus-group-group-unread)))
6749           (if (and (or (eq t unreads)
6750                        (and unreads (not (zerop unreads))))
6751                    (gnus-summary-read-group
6752                     target-group nil no-article
6753                     (and (buffer-name current-buffer) current-buffer)
6754                     nil backward))
6755               (setq entered t)
6756             (setq current-group target-group
6757                   target-group nil)))))))
6758
6759 (defun gnus-summary-prev-group (&optional no-article)
6760   "Exit current newsgroup and then select previous unread newsgroup.
6761 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6762   (interactive "P")
6763   (gnus-summary-next-group no-article nil t))
6764
6765 ;; Walking around summary lines.
6766
6767 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6768   "Go to the first subject satisfying any non-nil constraint.
6769 If UNREAD is non-nil, the article should be unread.
6770 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6771 If UNSEED is non-nil, the article should be unseen.
6772 Returns the article selected or nil if there are no matching articles."
6773   (interactive "P")
6774   (cond
6775    ;; Empty summary.
6776    ((null gnus-newsgroup-data)
6777     (gnus-message 3 "No articles in the group")
6778     nil)
6779    ;; Pick the first article.
6780    ((not (or unread undownloaded unseen))
6781     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6782     (gnus-data-number (car gnus-newsgroup-data)))
6783    ;; Find the first unread article.
6784    (t
6785     (let ((data gnus-newsgroup-data))
6786       (while (and data
6787                   (let ((num (gnus-data-number (car data))))
6788                     (or (memq num gnus-newsgroup-unfetched)
6789                         (not (or (and unread
6790                                       (memq num gnus-newsgroup-unreads))
6791                                  (and undownloaded
6792                                       (memq num gnus-newsgroup-undownloaded))
6793                                  (and unseen
6794                                       (memq num gnus-newsgroup-unseen)))))))
6795         (setq data (cdr data)))
6796       (prog1 
6797           (if data
6798               (progn
6799                 (goto-char (gnus-data-pos (car data)))
6800                 (gnus-data-number (car data)))
6801             (gnus-message 3 "No more%s articles"
6802                           (let* ((r (when unread " unread"))
6803                                  (d (when undownloaded " undownloaded"))
6804                                  (s (when unseen " unseen"))
6805                                  (l (delq nil (list r d s))))
6806                             (cond ((= 3 (length l))
6807                                    (concat r "," d ", or" s))
6808                                   ((= 2 (length l))
6809                                    (concat (car l) ", or" (cadr l)))
6810                                   ((= 1 (length l))
6811                                    (car l))
6812                                   (t
6813                                    ""))))
6814             nil
6815             )
6816         (gnus-summary-position-point))))))
6817
6818 (defun gnus-summary-next-subject (n &optional unread dont-display)
6819   "Go to next N'th summary line.
6820 If N is negative, go to the previous N'th subject line.
6821 If UNREAD is non-nil, only unread articles are selected.
6822 The difference between N and the actual number of steps taken is
6823 returned."
6824   (interactive "p")
6825   (let ((backward (< n 0))
6826         (n (abs n)))
6827     (while (and (> n 0)
6828                 (if backward
6829                     (gnus-summary-find-prev unread)
6830                   (gnus-summary-find-next unread)))
6831       (unless (zerop (setq n (1- n)))
6832         (gnus-summary-show-thread)))
6833     (when (/= 0 n)
6834       (gnus-message 7 "No more%s articles"
6835                     (if unread " unread" "")))
6836     (unless dont-display
6837       (gnus-summary-recenter)
6838       (gnus-summary-position-point))
6839     n))
6840
6841 (defun gnus-summary-next-unread-subject (n)
6842   "Go to next N'th unread summary line."
6843   (interactive "p")
6844   (gnus-summary-next-subject n t))
6845
6846 (defun gnus-summary-prev-subject (n &optional unread)
6847   "Go to previous N'th summary line.
6848 If optional argument UNREAD is non-nil, only unread article is selected."
6849   (interactive "p")
6850   (gnus-summary-next-subject (- n) unread))
6851
6852 (defun gnus-summary-prev-unread-subject (n)
6853   "Go to previous N'th unread summary line."
6854   (interactive "p")
6855   (gnus-summary-next-subject (- n) t))
6856
6857 (defun gnus-summary-goto-subjects (articles)
6858   "Insert the subject header for ARTICLES in the current buffer."
6859   (save-excursion
6860     (dolist (article articles)
6861       (gnus-summary-goto-subject article t)))
6862   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6863   (gnus-summary-position-point))
6864  
6865 (defun gnus-summary-goto-subject (article &optional force silent)
6866   "Go the subject line of ARTICLE.
6867 If FORCE, also allow jumping to articles not currently shown."
6868   (interactive "nArticle number: ")
6869   (unless (numberp article)
6870     (error "Article %s is not a number" article))
6871   (let ((b (point))
6872         (data (gnus-data-find article)))
6873     ;; We read in the article if we have to.
6874     (and (not data)
6875          force
6876          (gnus-summary-insert-subject
6877           article
6878           (if (or (numberp force) (vectorp force)) force)
6879           t)
6880          (setq data (gnus-data-find article)))
6881     (goto-char b)
6882     (if (not data)
6883         (progn
6884           (unless silent
6885             (gnus-message 3 "Can't find article %d" article))
6886           nil)
6887       (let ((pt (gnus-data-pos data)))
6888         (goto-char pt)
6889         (gnus-summary-set-article-display-arrow pt))
6890       (gnus-summary-position-point)
6891       article)))
6892
6893 ;; Walking around summary lines with displaying articles.
6894
6895 (defun gnus-summary-expand-window (&optional arg)
6896   "Make the summary buffer take up the entire Emacs frame.
6897 Given a prefix, will force an `article' buffer configuration."
6898   (interactive "P")
6899   (if arg
6900       (gnus-configure-windows 'article 'force)
6901     (gnus-configure-windows 'summary 'force)))
6902
6903 (defun gnus-summary-display-article (article &optional all-header)
6904   "Display ARTICLE in article buffer."
6905   (when (gnus-buffer-live-p gnus-article-buffer)
6906     (with-current-buffer gnus-article-buffer
6907       (mm-enable-multibyte)))
6908   (gnus-set-global-variables)
6909   (when (gnus-buffer-live-p gnus-article-buffer)
6910     (with-current-buffer gnus-article-buffer
6911       (setq gnus-article-charset gnus-newsgroup-charset)
6912       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6913       (mm-enable-multibyte)))
6914   (if (null article)
6915       nil
6916     (prog1
6917         (if gnus-summary-display-article-function
6918             (funcall gnus-summary-display-article-function article all-header)
6919           (gnus-article-prepare article all-header))
6920       (gnus-run-hooks 'gnus-select-article-hook)
6921       (when (and gnus-current-article
6922                  (not (zerop gnus-current-article)))
6923         (gnus-summary-goto-subject gnus-current-article))
6924       (gnus-summary-recenter)
6925       (when (and gnus-use-trees gnus-show-threads)
6926         (gnus-possibly-generate-tree article)
6927         (gnus-highlight-selected-tree article))
6928       ;; Successfully display article.
6929       (gnus-article-set-window-start
6930        (cdr (assq article gnus-newsgroup-bookmarks))))))
6931
6932 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6933   "Select the current article.
6934 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6935 non-nil, the article will be re-fetched even if it already present in
6936 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6937 be displayed."
6938   ;; Make sure we are in the summary buffer to work around bbdb bug.
6939   (unless (eq major-mode 'gnus-summary-mode)
6940     (set-buffer gnus-summary-buffer))
6941   (let ((article (or article (gnus-summary-article-number)))
6942         (all-headers (not (not all-headers))) ;Must be t or nil.
6943         gnus-summary-display-article-function)
6944     (and (not pseudo)
6945          (gnus-summary-article-pseudo-p article)
6946          (error "This is a pseudo-article"))
6947     (save-excursion
6948       (set-buffer gnus-summary-buffer)
6949       (if (or (and gnus-single-article-buffer
6950                    (or (null gnus-current-article)
6951                        (null gnus-article-current)
6952                        (null (get-buffer gnus-article-buffer))
6953                        (not (eq article (cdr gnus-article-current)))
6954                        (not (equal (car gnus-article-current)
6955                                    gnus-newsgroup-name))))
6956               (and (not gnus-single-article-buffer)
6957                    (or (null gnus-current-article)
6958                        (not (eq gnus-current-article article))))
6959               force)
6960           ;; The requested article is different from the current article.
6961           (progn
6962             (gnus-summary-display-article article all-headers)
6963             (when (gnus-buffer-live-p gnus-article-buffer)
6964               (with-current-buffer gnus-article-buffer
6965                 (if (not gnus-article-decoded-p) ;; a local variable
6966                     (mm-disable-multibyte))))
6967             (gnus-article-set-window-start
6968              (cdr (assq article gnus-newsgroup-bookmarks)))
6969             article)
6970         'old))))
6971
6972 (defun gnus-summary-force-verify-and-decrypt ()
6973   "Display buttons for signed/encrypted parts and verify/decrypt them."
6974   (interactive)
6975   (let ((mm-verify-option 'known)
6976         (mm-decrypt-option 'known)
6977         (gnus-buttonized-mime-types (append (list "multipart/signed"
6978                                                   "multipart/encrypted")
6979                                             gnus-buttonized-mime-types)))
6980     (gnus-summary-select-article nil 'force)))
6981
6982 (defun gnus-summary-set-current-mark (&optional current-mark)
6983   "Obsolete function."
6984   nil)
6985
6986 (defun gnus-summary-next-article (&optional unread subject backward push)
6987   "Select the next article.
6988 If UNREAD, only unread articles are selected.
6989 If SUBJECT, only articles with SUBJECT are selected.
6990 If BACKWARD, the previous article is selected instead of the next."
6991   (interactive "P")
6992   (cond
6993    ;; Is there such an article?
6994    ((and (gnus-summary-search-forward unread subject backward)
6995          (or (gnus-summary-display-article (gnus-summary-article-number))
6996              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6997     (gnus-summary-position-point))
6998    ;; If not, we try the first unread, if that is wanted.
6999    ((and subject
7000          gnus-auto-select-same
7001          (gnus-summary-first-unread-article))
7002     (gnus-summary-position-point)
7003     (gnus-message 6 "Wrapped"))
7004    ;; Try to get next/previous article not displayed in this group.
7005    ((and gnus-auto-extend-newsgroup
7006          (not unread) (not subject))
7007     (gnus-summary-goto-article
7008      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7009      nil (count-lines (point-min) (point))))
7010    ;; Go to next/previous group.
7011    (t
7012     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7013       (gnus-summary-jump-to-group gnus-newsgroup-name))
7014     (let ((cmd last-command-char)
7015           (point
7016            (save-excursion
7017              (set-buffer gnus-group-buffer)
7018              (point)))
7019           (group
7020            (if (eq gnus-keep-same-level 'best)
7021                (gnus-summary-best-group gnus-newsgroup-name)
7022              (gnus-summary-search-group backward gnus-keep-same-level))))
7023       ;; For some reason, the group window gets selected.  We change
7024       ;; it back.
7025       (select-window (get-buffer-window (current-buffer)))
7026       ;; Select next unread newsgroup automagically.
7027       (cond
7028        ((or (not gnus-auto-select-next)
7029             (not cmd))
7030         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7031        ((or (eq gnus-auto-select-next 'quietly)
7032             (and (eq gnus-auto-select-next 'slightly-quietly)
7033                  push)
7034             (and (eq gnus-auto-select-next 'almost-quietly)
7035                  (gnus-summary-last-article-p)))
7036         ;; Select quietly.
7037         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7038             (gnus-summary-exit)
7039           (gnus-message 7 "No more%s articles (%s)..."
7040                         (if unread " unread" "")
7041                         (if group (concat "selecting " group)
7042                           "exiting"))
7043           (gnus-summary-next-group nil group backward)))
7044        (t
7045         (when (gnus-key-press-event-p last-input-event)
7046           (gnus-summary-walk-group-buffer
7047            gnus-newsgroup-name cmd unread backward point))))))))
7048
7049 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7050   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7051                       (?\C-p (gnus-group-prev-unread-group 1))))
7052         (cursor-in-echo-area t)
7053         keve key group ended prompt)
7054     (save-excursion
7055       (set-buffer gnus-group-buffer)
7056       (goto-char start)
7057       (setq group
7058             (if (eq gnus-keep-same-level 'best)
7059                 (gnus-summary-best-group gnus-newsgroup-name)
7060               (gnus-summary-search-group backward gnus-keep-same-level))))
7061     (while (not ended)
7062       (setq prompt
7063             (format
7064              "No more%s articles%s " (if unread " unread" "")
7065              (if (and group
7066                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7067                  (format " (Type %s for %s [%s])"
7068                          (single-key-description cmd) group
7069                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7070                (format " (Type %s to exit %s)"
7071                        (single-key-description cmd)
7072                        gnus-newsgroup-name))))
7073       ;; Confirm auto selection.
7074       (setq key (car (setq keve (gnus-read-event-char prompt)))
7075             ended t)
7076       (cond
7077        ((assq key keystrokes)
7078         (let ((obuf (current-buffer)))
7079           (switch-to-buffer gnus-group-buffer)
7080           (when group
7081             (gnus-group-jump-to-group group))
7082           (eval (cadr (assq key keystrokes)))
7083           (setq group (gnus-group-group-name))
7084           (switch-to-buffer obuf))
7085         (setq ended nil))
7086        ((equal key cmd)
7087         (if (or (not group)
7088                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7089             (gnus-summary-exit)
7090           (gnus-summary-next-group nil group backward)))
7091        (t
7092         (push (cdr keve) unread-command-events))))))
7093
7094 (defun gnus-summary-next-unread-article ()
7095   "Select unread article after current one."
7096   (interactive)
7097   (gnus-summary-next-article
7098    (or (not (eq gnus-summary-goto-unread 'never))
7099        (gnus-summary-last-article-p (gnus-summary-article-number)))
7100    (and gnus-auto-select-same
7101         (gnus-summary-article-subject))))
7102
7103 (defun gnus-summary-prev-article (&optional unread subject)
7104   "Select the article after the current one.
7105 If UNREAD is non-nil, only unread articles are selected."
7106   (interactive "P")
7107   (gnus-summary-next-article unread subject t))
7108
7109 (defun gnus-summary-prev-unread-article ()
7110   "Select unread article before current one."
7111   (interactive)
7112   (gnus-summary-prev-article
7113    (or (not (eq gnus-summary-goto-unread 'never))
7114        (gnus-summary-first-article-p (gnus-summary-article-number)))
7115    (and gnus-auto-select-same
7116         (gnus-summary-article-subject))))
7117
7118 (defun gnus-summary-next-page (&optional lines circular stop)
7119   "Show next page of the selected article.
7120 If at the end of the current article, select the next article.
7121 LINES says how many lines should be scrolled up.
7122
7123 If CIRCULAR is non-nil, go to the start of the article instead of
7124 selecting the next article when reaching the end of the current
7125 article.
7126
7127 If STOP is non-nil, just stop when reaching the end of the message."
7128   (interactive "P")
7129   (setq gnus-summary-buffer (current-buffer))
7130   (gnus-set-global-variables)
7131   (let ((article (gnus-summary-article-number))
7132         (article-window (get-buffer-window gnus-article-buffer t))
7133         endp)
7134     ;; If the buffer is empty, we have no article.
7135     (unless article
7136       (error "No article to select"))
7137     (gnus-configure-windows 'article)
7138     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7139         (if (and (eq gnus-summary-goto-unread 'never)
7140                  (not (gnus-summary-last-article-p article)))
7141             (gnus-summary-next-article)
7142           (gnus-summary-next-unread-article))
7143       (if (or (null gnus-current-article)
7144               (null gnus-article-current)
7145               (/= article (cdr gnus-article-current))
7146               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7147           ;; Selected subject is different from current article's.
7148           (gnus-summary-display-article article)
7149         (when article-window
7150           (gnus-eval-in-buffer-window gnus-article-buffer
7151             (setq endp (or (gnus-article-next-page lines)
7152                            (gnus-article-only-boring-p))))
7153           (when endp
7154             (cond (stop
7155                    (gnus-message 3 "End of message"))
7156                   (circular
7157                    (gnus-summary-beginning-of-article))
7158                   (lines
7159                    (gnus-message 3 "End of message"))
7160                   ((null lines)
7161                    (if (and (eq gnus-summary-goto-unread 'never)
7162                             (not (gnus-summary-last-article-p article)))
7163                        (gnus-summary-next-article)
7164                      (gnus-summary-next-unread-article))))))))
7165     (gnus-summary-recenter)
7166     (gnus-summary-position-point)))
7167
7168 (defun gnus-summary-prev-page (&optional lines move)
7169   "Show previous page of selected article.
7170 Argument LINES specifies lines to be scrolled down.
7171 If MOVE, move to the previous unread article if point is at
7172 the beginning of the buffer."
7173   (interactive "P")
7174   (let ((article (gnus-summary-article-number))
7175         (article-window (get-buffer-window gnus-article-buffer t))
7176         endp)
7177     (gnus-configure-windows 'article)
7178     (if (or (null gnus-current-article)
7179             (null gnus-article-current)
7180             (/= article (cdr gnus-article-current))
7181             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7182         ;; Selected subject is different from current article's.
7183         (gnus-summary-display-article article)
7184       (gnus-summary-recenter)
7185       (when article-window
7186         (gnus-eval-in-buffer-window gnus-article-buffer
7187           (setq endp (gnus-article-prev-page lines)))
7188         (when (and move endp)
7189           (cond (lines
7190                  (gnus-message 3 "Beginning of message"))
7191                 ((null lines)
7192                  (if (and (eq gnus-summary-goto-unread 'never)
7193                           (not (gnus-summary-first-article-p article)))
7194                      (gnus-summary-prev-article)
7195                    (gnus-summary-prev-unread-article))))))))
7196   (gnus-summary-position-point))
7197
7198 (defun gnus-summary-prev-page-or-article (&optional lines)
7199   "Show previous page of selected article.
7200 Argument LINES specifies lines to be scrolled down.
7201 If at the beginning of the article, go to the next article."
7202   (interactive "P")
7203   (gnus-summary-prev-page lines t))
7204
7205 (defun gnus-summary-scroll-up (lines)
7206   "Scroll up (or down) one line current article.
7207 Argument LINES specifies lines to be scrolled up (or down if negative)."
7208   (interactive "p")
7209   (gnus-configure-windows 'article)
7210   (gnus-summary-show-thread)
7211   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7212     (gnus-eval-in-buffer-window gnus-article-buffer
7213       (cond ((> lines 0)
7214              (when (gnus-article-next-page lines)
7215                (gnus-message 3 "End of message")))
7216             ((< lines 0)
7217              (gnus-article-prev-page (- lines))))))
7218   (gnus-summary-recenter)
7219   (gnus-summary-position-point))
7220
7221 (defun gnus-summary-scroll-down (lines)
7222   "Scroll down (or up) one line current article.
7223 Argument LINES specifies lines to be scrolled down (or up if negative)."
7224   (interactive "p")
7225   (gnus-summary-scroll-up (- lines)))
7226
7227 (defun gnus-summary-next-same-subject ()
7228   "Select next article which has the same subject as current one."
7229   (interactive)
7230   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7231
7232 (defun gnus-summary-prev-same-subject ()
7233   "Select previous article which has the same subject as current one."
7234   (interactive)
7235   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7236
7237 (defun gnus-summary-next-unread-same-subject ()
7238   "Select next unread article which has the same subject as current one."
7239   (interactive)
7240   (gnus-summary-next-article t (gnus-summary-article-subject)))
7241
7242 (defun gnus-summary-prev-unread-same-subject ()
7243   "Select previous unread article which has the same subject as current one."
7244   (interactive)
7245   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7246
7247 (defun gnus-summary-first-unread-article ()
7248   "Select the first unread article.
7249 Return nil if there are no unread articles."
7250   (interactive)
7251   (prog1
7252       (when (gnus-summary-first-subject t)
7253         (gnus-summary-show-thread)
7254         (gnus-summary-first-subject t)
7255         (gnus-summary-display-article (gnus-summary-article-number)))
7256     (gnus-summary-position-point)))
7257
7258 (defun gnus-summary-first-unread-subject ()
7259   "Place the point on the subject line of the first unread article.
7260 Return nil if there are no unread articles."
7261   (interactive)
7262   (prog1
7263       (when (gnus-summary-first-subject t)
7264         (gnus-summary-show-thread)
7265         (gnus-summary-first-subject t))
7266     (gnus-summary-position-point)))
7267
7268 (defun gnus-summary-first-unseen-subject ()
7269   "Place the point on the subject line of the first unseen article.
7270 Return nil if there are no unseen articles."
7271   (interactive)
7272   (prog1
7273       (when (gnus-summary-first-subject nil nil t)
7274         (gnus-summary-show-thread)
7275         (gnus-summary-first-subject nil nil t))
7276     (gnus-summary-position-point)))
7277
7278 (defun gnus-summary-first-unseen-or-unread-subject ()
7279   "Place the point on the subject line of the first unseen article or,
7280 if all article have been seen, on the subject line of the first unread
7281 article."
7282   (interactive)
7283   (prog1
7284       (unless (when (gnus-summary-first-subject nil nil t)
7285                 (gnus-summary-show-thread)
7286                 (gnus-summary-first-subject nil nil t))
7287         (when (gnus-summary-first-subject t)
7288           (gnus-summary-show-thread)
7289           (gnus-summary-first-subject t)))
7290     (gnus-summary-position-point)))
7291
7292 (defun gnus-summary-first-article ()
7293   "Select the first article.
7294 Return nil if there are no articles."
7295   (interactive)
7296   (prog1
7297       (when (gnus-summary-first-subject)
7298         (gnus-summary-show-thread)
7299         (gnus-summary-first-subject)
7300         (gnus-summary-display-article (gnus-summary-article-number)))
7301     (gnus-summary-position-point)))
7302
7303 (defun gnus-summary-best-unread-article (&optional arg)
7304   "Select the unread article with the highest score.
7305 If given a prefix argument, select the next unread article that has a
7306 score higher than the default score."
7307   (interactive "P")
7308   (let ((article (if arg
7309                      (gnus-summary-better-unread-subject)
7310                    (gnus-summary-best-unread-subject))))
7311     (if article
7312         (gnus-summary-goto-article article)
7313       (error "No unread articles"))))
7314
7315 (defun gnus-summary-best-unread-subject ()
7316   "Select the unread subject with the highest score."
7317   (interactive)
7318   (let ((best -1000000)
7319         (data gnus-newsgroup-data)
7320         article score)
7321     (while data
7322       (and (gnus-data-unread-p (car data))
7323            (> (setq score
7324                     (gnus-summary-article-score (gnus-data-number (car data))))
7325               best)
7326            (setq best score
7327                  article (gnus-data-number (car data))))
7328       (setq data (cdr data)))
7329     (when article
7330       (gnus-summary-goto-subject article))
7331     (gnus-summary-position-point)
7332     article))
7333
7334 (defun gnus-summary-better-unread-subject ()
7335   "Select the first unread subject that has a score over the default score."
7336   (interactive)
7337   (let ((data gnus-newsgroup-data)
7338         article score)
7339     (while (and (setq article (gnus-data-number (car data)))
7340                 (or (gnus-data-read-p (car data))
7341                     (not (> (gnus-summary-article-score article)
7342                             gnus-summary-default-score))))
7343       (setq data (cdr data)))
7344     (when article
7345       (gnus-summary-goto-subject article))
7346     (gnus-summary-position-point)
7347     article))
7348
7349 (defun gnus-summary-last-subject ()
7350   "Go to the last displayed subject line in the group."
7351   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7352     (when article
7353       (gnus-summary-goto-subject article))))
7354
7355 (defun gnus-summary-goto-article (article &optional all-headers force)
7356   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7357 If ALL-HEADERS is non-nil, no header lines are hidden.
7358 If FORCE, go to the article even if it isn't displayed.  If FORCE
7359 is a number, it is the line the article is to be displayed on."
7360   (interactive
7361    (list
7362     (completing-read
7363      "Article number or Message-ID: "
7364      (mapcar (lambda (number) (list (int-to-string number)))
7365              gnus-newsgroup-limit))
7366     current-prefix-arg
7367     t))
7368   (prog1
7369       (if (and (stringp article)
7370                (string-match "@" article))
7371           (gnus-summary-refer-article article)
7372         (when (stringp article)
7373           (setq article (string-to-number article)))
7374         (if (gnus-summary-goto-subject article force)
7375             (gnus-summary-display-article article all-headers)
7376           (gnus-message 4 "Couldn't go to article %s" article) nil))
7377     (gnus-summary-position-point)))
7378
7379 (defun gnus-summary-goto-last-article ()
7380   "Go to the previously read article."
7381   (interactive)
7382   (prog1
7383       (when gnus-last-article
7384         (gnus-summary-goto-article gnus-last-article nil t))
7385     (gnus-summary-position-point)))
7386
7387 (defun gnus-summary-pop-article (number)
7388   "Pop one article off the history and go to the previous.
7389 NUMBER articles will be popped off."
7390   (interactive "p")
7391   (let (to)
7392     (setq gnus-newsgroup-history
7393           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7394     (if to
7395         (gnus-summary-goto-article (car to) nil t)
7396       (error "Article history empty")))
7397   (gnus-summary-position-point))
7398
7399 ;; Summary commands and functions for limiting the summary buffer.
7400
7401 (defun gnus-summary-limit-to-articles (n)
7402   "Limit the summary buffer to the next N articles.
7403 If not given a prefix, use the process marked articles instead."
7404   (interactive "P")
7405   (prog1
7406       (let ((articles (gnus-summary-work-articles n)))
7407         (setq gnus-newsgroup-processable nil)
7408         (gnus-summary-limit articles))
7409     (gnus-summary-position-point)))
7410
7411 (defun gnus-summary-pop-limit (&optional total)
7412   "Restore the previous limit.
7413 If given a prefix, remove all limits."
7414   (interactive "P")
7415   (when total
7416     (setq gnus-newsgroup-limits
7417           (list (mapcar (lambda (h) (mail-header-number h))
7418                         gnus-newsgroup-headers))))
7419   (unless gnus-newsgroup-limits
7420     (error "No limit to pop"))
7421   (prog1
7422       (gnus-summary-limit nil 'pop)
7423     (gnus-summary-position-point)))
7424
7425 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7426   "Limit the summary buffer to articles that have subjects that match a regexp.
7427 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7428   (interactive
7429    (list (read-string (if current-prefix-arg
7430                           "Exclude subject (regexp): "
7431                         "Limit to subject (regexp): "))
7432          nil current-prefix-arg))
7433   (unless header
7434     (setq header "subject"))
7435   (when (not (equal "" subject))
7436     (prog1
7437         (let ((articles (gnus-summary-find-matching
7438                          (or header "subject") subject 'all nil nil
7439                          not-matching)))
7440           (unless articles
7441             (error "Found no matches for \"%s\"" subject))
7442           (gnus-summary-limit articles))
7443       (gnus-summary-position-point))))
7444
7445 (defun gnus-summary-limit-to-author (from &optional not-matching)
7446   "Limit the summary buffer to articles that have authors that match a regexp.
7447 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7448   (interactive
7449    (list (read-string (if current-prefix-arg
7450                           "Exclude author (regexp): "
7451                         "Limit to author (regexp): "))
7452          current-prefix-arg))
7453   (gnus-summary-limit-to-subject from "from" not-matching))
7454
7455 (defun gnus-summary-limit-to-age (age &optional younger-p)
7456   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7457 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7458 articles that are younger than AGE days."
7459   (interactive
7460    (let ((younger current-prefix-arg)
7461          (days-got nil)
7462          days)
7463      (while (not days-got)
7464        (setq days (if younger
7465                       (read-string "Limit to articles younger than (in days, older when negative): ")
7466                     (read-string
7467                      "Limit to articles older than (in days, younger when negative): ")))
7468        (when (> (length days) 0)
7469          (setq days (read days)))
7470        (if (numberp days)
7471            (progn
7472              (setq days-got t)
7473              (if (< days 0)
7474                  (progn
7475                    (setq younger (not younger))
7476                    (setq days (* days -1)))))
7477          (message "Please enter a number.")
7478          (sleep-for 1)))
7479      (list days younger)))
7480   (prog1
7481       (let ((data gnus-newsgroup-data)
7482             (cutoff (days-to-time age))
7483             articles d date is-younger)
7484         (while (setq d (pop data))
7485           (when (and (vectorp (gnus-data-header d))
7486                      (setq date (mail-header-date (gnus-data-header d))))
7487             (setq is-younger (time-less-p
7488                               (time-since (condition-case ()
7489                                               (date-to-time date)
7490                                             (error '(0 0))))
7491                               cutoff))
7492             (when (if younger-p
7493                       is-younger
7494                     (not is-younger))
7495               (push (gnus-data-number d) articles))))
7496         (gnus-summary-limit (nreverse articles)))
7497     (gnus-summary-position-point)))
7498
7499 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7500   "Limit the summary buffer to articles that match an 'extra' header."
7501   (interactive
7502    (let ((header
7503           (intern
7504            (gnus-completing-read-with-default
7505             (symbol-name (car gnus-extra-headers))
7506             (if current-prefix-arg
7507                 "Exclude extra header:"
7508               "Limit extra header:")
7509             (mapcar (lambda (x)
7510                       (cons (symbol-name x) x))
7511                     gnus-extra-headers)
7512             nil
7513             t))))
7514      (list header
7515            (read-string (format "%s header %s (regexp): "
7516                                 (if current-prefix-arg "Exclude" "Limit to")
7517                                 header))
7518            current-prefix-arg)))
7519   (when (not (equal "" regexp))
7520     (prog1
7521         (let ((articles (gnus-summary-find-matching
7522                          (cons 'extra header) regexp 'all nil nil
7523                          not-matching)))
7524           (unless articles
7525             (error "Found no matches for \"%s\"" regexp))
7526           (gnus-summary-limit articles))
7527       (gnus-summary-position-point))))
7528
7529 (defun gnus-summary-limit-to-display-predicate ()
7530   "Limit the summary buffer to the predicated in the `display' group parameter."
7531   (interactive)
7532   (unless gnus-newsgroup-display
7533     (error "There is no `display' group parameter"))
7534   (let (articles)
7535     (dolist (number gnus-newsgroup-articles)
7536       (when (funcall gnus-newsgroup-display)
7537         (push number articles)))
7538     (gnus-summary-limit articles))
7539   (gnus-summary-position-point))
7540
7541 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7542 (make-obsolete
7543  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7544
7545 (defun gnus-summary-limit-to-unread (&optional all)
7546   "Limit the summary buffer to articles that are not marked as read.
7547 If ALL is non-nil, limit strictly to unread articles."
7548   (interactive "P")
7549   (if all
7550       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7551     (gnus-summary-limit-to-marks
7552      ;; Concat all the marks that say that an article is read and have
7553      ;; those removed.
7554      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7555            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7556            gnus-low-score-mark gnus-expirable-mark
7557            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7558            gnus-duplicate-mark gnus-souped-mark)
7559      'reverse)))
7560
7561 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7562 (make-obsolete 'gnus-summary-delete-marked-with
7563                'gnus-summary-limit-exclude-marks)
7564
7565 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7566   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7567 If REVERSE, limit the summary buffer to articles that are marked
7568 with MARKS.  MARKS can either be a string of marks or a list of marks.
7569 Returns how many articles were removed."
7570   (interactive "sMarks: ")
7571   (gnus-summary-limit-to-marks marks t))
7572
7573 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7574   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7575 If REVERSE (the prefix), limit the summary buffer to articles that are
7576 not marked with MARKS.  MARKS can either be a string of marks or a
7577 list of marks.
7578 Returns how many articles were removed."
7579   (interactive "sMarks: \nP")
7580   (prog1
7581       (let ((data gnus-newsgroup-data)
7582             (marks (if (listp marks) marks
7583                      (append marks nil))) ; Transform to list.
7584             articles)
7585         (while data
7586           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7587                   (memq (gnus-data-mark (car data)) marks))
7588             (push (gnus-data-number (car data)) articles))
7589           (setq data (cdr data)))
7590         (gnus-summary-limit articles))
7591     (gnus-summary-position-point)))
7592
7593 (defun gnus-summary-limit-to-score (score)
7594   "Limit to articles with score at or above SCORE."
7595   (interactive "NLimit to articles with score of at least: ")
7596   (let ((data gnus-newsgroup-data)
7597         articles)
7598     (while data
7599       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7600                 score)
7601         (push (gnus-data-number (car data)) articles))
7602       (setq data (cdr data)))
7603     (prog1
7604         (gnus-summary-limit articles)
7605       (gnus-summary-position-point))))
7606
7607 (defun gnus-summary-limit-to-unseen ()
7608   "Limit to unseen articles."
7609   (interactive)
7610   (prog1
7611       (gnus-summary-limit gnus-newsgroup-unseen)
7612     (gnus-summary-position-point)))
7613
7614 (defun gnus-summary-limit-include-thread (id)
7615   "Display all the hidden articles that is in the thread with ID in it.
7616 When called interactively, ID is the Message-ID of the current
7617 article."
7618   (interactive (list (mail-header-id (gnus-summary-article-header))))
7619   (let ((articles (gnus-articles-in-thread
7620                    (gnus-id-to-thread (gnus-root-id id)))))
7621     (prog1
7622         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7623       (gnus-summary-limit-include-matching-articles
7624        "subject"
7625        (regexp-quote (gnus-simplify-subject-re
7626                       (mail-header-subject (gnus-id-to-header id)))))
7627       (gnus-summary-position-point))))
7628
7629 (defun gnus-summary-limit-include-matching-articles (header regexp)
7630   "Display all the hidden articles that have HEADERs that match REGEXP."
7631   (interactive (list (read-string "Match on header: ")
7632                      (read-string "Regexp: ")))
7633   (let ((articles (gnus-find-matching-articles header regexp)))
7634     (prog1
7635         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7636       (gnus-summary-position-point))))
7637
7638 (defun gnus-summary-insert-dormant-articles ()
7639   "Insert all the dormat articles for this group into the current buffer."
7640   (interactive)
7641   (let ((gnus-verbose (max 6 gnus-verbose)))
7642     (if (not gnus-newsgroup-dormant)
7643         (gnus-message 3 "No cached articles for this group")
7644       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7645
7646 (defun gnus-summary-limit-include-dormant ()
7647   "Display all the hidden articles that are marked as dormant.
7648 Note that this command only works on a subset of the articles currently
7649 fetched for this group."
7650   (interactive)
7651   (unless gnus-newsgroup-dormant
7652     (error "There are no dormant articles in this group"))
7653   (prog1
7654       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7655     (gnus-summary-position-point)))
7656
7657 (defun gnus-summary-limit-exclude-dormant ()
7658   "Hide all dormant articles."
7659   (interactive)
7660   (prog1
7661       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7662     (gnus-summary-position-point)))
7663
7664 (defun gnus-summary-limit-exclude-childless-dormant ()
7665   "Hide all dormant articles that have no children."
7666   (interactive)
7667   (let ((data (gnus-data-list t))
7668         articles d children)
7669     ;; Find all articles that are either not dormant or have
7670     ;; children.
7671     (while (setq d (pop data))
7672       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7673                 (and (setq children
7674                            (gnus-article-children (gnus-data-number d)))
7675                      (let (found)
7676                        (while children
7677                          (when (memq (car children) articles)
7678                            (setq children nil
7679                                  found t))
7680                          (pop children))
7681                        found)))
7682         (push (gnus-data-number d) articles)))
7683     ;; Do the limiting.
7684     (prog1
7685         (gnus-summary-limit articles)
7686       (gnus-summary-position-point))))
7687
7688 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7689   "Mark all unread excluded articles as read.
7690 If ALL, mark even excluded ticked and dormants as read."
7691   (interactive "P")
7692   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7693   (let ((articles (gnus-sorted-ndifference
7694                    (sort
7695                     (mapcar (lambda (h) (mail-header-number h))
7696                             gnus-newsgroup-headers)
7697                     '<)
7698                    gnus-newsgroup-limit))
7699         article)
7700     (setq gnus-newsgroup-unreads
7701           (gnus-sorted-intersection gnus-newsgroup-unreads
7702                                     gnus-newsgroup-limit))
7703     (if all
7704         (setq gnus-newsgroup-dormant nil
7705               gnus-newsgroup-marked nil
7706               gnus-newsgroup-reads
7707               (nconc
7708                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7709                gnus-newsgroup-reads))
7710       (while (setq article (pop articles))
7711         (unless (or (memq article gnus-newsgroup-dormant)
7712                     (memq article gnus-newsgroup-marked))
7713           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7714
7715 (defun gnus-summary-limit (articles &optional pop)
7716   (if pop
7717       ;; We pop the previous limit off the stack and use that.
7718       (setq articles (car gnus-newsgroup-limits)
7719             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7720     ;; We use the new limit, so we push the old limit on the stack.
7721     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7722   ;; Set the limit.
7723   (setq gnus-newsgroup-limit articles)
7724   (let ((total (length gnus-newsgroup-data))
7725         (data (gnus-data-find-list (gnus-summary-article-number)))
7726         (gnus-summary-mark-below nil)   ; Inhibit this.
7727         found)
7728     ;; This will do all the work of generating the new summary buffer
7729     ;; according to the new limit.
7730     (gnus-summary-prepare)
7731     ;; Hide any threads, possibly.
7732     (gnus-summary-maybe-hide-threads)
7733     ;; Try to return to the article you were at, or one in the
7734     ;; neighborhood.
7735     (when data
7736       ;; We try to find some article after the current one.
7737       (while data
7738         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7739           (setq data nil
7740                 found t))
7741         (setq data (cdr data))))
7742     (unless found
7743       ;; If there is no data, that means that we were after the last
7744       ;; article.  The same goes when we can't find any articles
7745       ;; after the current one.
7746       (goto-char (point-max))
7747       (gnus-summary-find-prev))
7748     (gnus-set-mode-line 'summary)
7749     ;; We return how many articles were removed from the summary
7750     ;; buffer as a result of the new limit.
7751     (- total (length gnus-newsgroup-data))))
7752
7753 (defsubst gnus-invisible-cut-children (threads)
7754   (let ((num 0))
7755     (while threads
7756       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7757         (incf num))
7758       (pop threads))
7759     (< num 2)))
7760
7761 (defsubst gnus-cut-thread (thread)
7762   "Go forwards in the thread until we find an article that we want to display."
7763   (when (or (eq gnus-fetch-old-headers 'some)
7764             (eq gnus-fetch-old-headers 'invisible)
7765             (numberp gnus-fetch-old-headers)
7766             (eq gnus-build-sparse-threads 'some)
7767             (eq gnus-build-sparse-threads 'more))
7768     ;; Deal with old-fetched headers and sparse threads.
7769     (while (and
7770             thread
7771             (or
7772              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7773              (gnus-summary-article-ancient-p
7774               (mail-header-number (car thread))))
7775             (if (or (<= (length (cdr thread)) 1)
7776                     (eq gnus-fetch-old-headers 'invisible))
7777                 (setq gnus-newsgroup-limit
7778                       (delq (mail-header-number (car thread))
7779                             gnus-newsgroup-limit)
7780                       thread (cadr thread))
7781               (when (gnus-invisible-cut-children (cdr thread))
7782                 (let ((th (cdr thread)))
7783                   (while th
7784                     (if (memq (mail-header-number (caar th))
7785                               gnus-newsgroup-limit)
7786                         (setq thread (car th)
7787                               th nil)
7788                       (setq th (cdr th))))))))))
7789   thread)
7790
7791 (defun gnus-cut-threads (threads)
7792   "Cut off all uninteresting articles from the beginning of threads."
7793   (when (or (eq gnus-fetch-old-headers 'some)
7794             (eq gnus-fetch-old-headers 'invisible)
7795             (numberp gnus-fetch-old-headers)
7796             (eq gnus-build-sparse-threads 'some)
7797             (eq gnus-build-sparse-threads 'more))
7798     (let ((th threads))
7799       (while th
7800         (setcar th (gnus-cut-thread (car th)))
7801         (setq th (cdr th)))))
7802   ;; Remove nixed out threads.
7803   (delq nil threads))
7804
7805 (defun gnus-summary-initial-limit (&optional show-if-empty)
7806   "Figure out what the initial limit is supposed to be on group entry.
7807 This entails weeding out unwanted dormants, low-scored articles,
7808 fetch-old-headers verbiage, and so on."
7809   ;; Most groups have nothing to remove.
7810   (if (or gnus-inhibit-limiting
7811           (and (null gnus-newsgroup-dormant)
7812                (eq gnus-newsgroup-display 'gnus-not-ignore)
7813                (not (eq gnus-fetch-old-headers 'some))
7814                (not (numberp gnus-fetch-old-headers))
7815                (not (eq gnus-fetch-old-headers 'invisible))
7816                (null gnus-summary-expunge-below)
7817                (not (eq gnus-build-sparse-threads 'some))
7818                (not (eq gnus-build-sparse-threads 'more))
7819                (null gnus-thread-expunge-below)
7820                (not gnus-use-nocem)))
7821       ()                                ; Do nothing.
7822     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7823     (setq gnus-newsgroup-limit nil)
7824     (mapatoms
7825      (lambda (node)
7826        (unless (car (symbol-value node))
7827          ;; These threads have no parents -- they are roots.
7828          (let ((nodes (cdr (symbol-value node)))
7829                thread)
7830            (while nodes
7831              (if (and gnus-thread-expunge-below
7832                       (< (gnus-thread-total-score (car nodes))
7833                          gnus-thread-expunge-below))
7834                  (gnus-expunge-thread (pop nodes))
7835                (setq thread (pop nodes))
7836                (gnus-summary-limit-children thread))))))
7837      gnus-newsgroup-dependencies)
7838     ;; If this limitation resulted in an empty group, we might
7839     ;; pop the previous limit and use it instead.
7840     (when (and (not gnus-newsgroup-limit)
7841                show-if-empty)
7842       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7843     gnus-newsgroup-limit))
7844
7845 (defun gnus-summary-limit-children (thread)
7846   "Return 1 if this subthread is visible and 0 if it is not."
7847   ;; First we get the number of visible children to this thread.  This
7848   ;; is done by recursing down the thread using this function, so this
7849   ;; will really go down to a leaf article first, before slowly
7850   ;; working its way up towards the root.
7851   (when thread
7852     (let* ((max-lisp-eval-depth 5000)
7853            (children
7854            (if (cdr thread)
7855                (apply '+ (mapcar 'gnus-summary-limit-children
7856                                  (cdr thread)))
7857              0))
7858           (number (mail-header-number (car thread)))
7859           score)
7860       (if (and
7861            (not (memq number gnus-newsgroup-marked))
7862            (or
7863             ;; If this article is dormant and has absolutely no visible
7864             ;; children, then this article isn't visible.
7865             (and (memq number gnus-newsgroup-dormant)
7866                  (zerop children))
7867             ;; If this is "fetch-old-headered" and there is no
7868             ;; visible children, then we don't want this article.
7869             (and (or (eq gnus-fetch-old-headers 'some)
7870                      (numberp gnus-fetch-old-headers))
7871                  (gnus-summary-article-ancient-p number)
7872                  (zerop children))
7873             ;; If this is "fetch-old-headered" and `invisible', then
7874             ;; we don't want this article.
7875             (and (eq gnus-fetch-old-headers 'invisible)
7876                  (gnus-summary-article-ancient-p number))
7877             ;; If this is a sparsely inserted article with no children,
7878             ;; we don't want it.
7879             (and (eq gnus-build-sparse-threads 'some)
7880                  (gnus-summary-article-sparse-p number)
7881                  (zerop children))
7882             ;; If we use expunging, and this article is really
7883             ;; low-scored, then we don't want this article.
7884             (when (and gnus-summary-expunge-below
7885                        (< (setq score
7886                                 (or (cdr (assq number gnus-newsgroup-scored))
7887                                     gnus-summary-default-score))
7888                           gnus-summary-expunge-below))
7889               ;; We increase the expunge-tally here, but that has
7890               ;; nothing to do with the limits, really.
7891               (incf gnus-newsgroup-expunged-tally)
7892               ;; We also mark as read here, if that's wanted.
7893               (when (and gnus-summary-mark-below
7894                          (< score gnus-summary-mark-below))
7895                 (setq gnus-newsgroup-unreads
7896                       (delq number gnus-newsgroup-unreads))
7897                 (if gnus-newsgroup-auto-expire
7898                     (push number gnus-newsgroup-expirable)
7899                   (push (cons number gnus-low-score-mark)
7900                         gnus-newsgroup-reads)))
7901               t)
7902             ;; Do the `display' group parameter.
7903             (and gnus-newsgroup-display
7904                  (not (funcall gnus-newsgroup-display)))
7905             ;; Check NoCeM things.
7906             (if (and gnus-use-nocem
7907                      (gnus-nocem-unwanted-article-p
7908                       (mail-header-id (car thread))))
7909                 (progn
7910                   (setq gnus-newsgroup-unreads
7911                         (delq number gnus-newsgroup-unreads))
7912                   t))))
7913           ;; Nope, invisible article.
7914           0
7915         ;; Ok, this article is to be visible, so we add it to the limit
7916         ;; and return 1.
7917         (push number gnus-newsgroup-limit)
7918         1))))
7919
7920 (defun gnus-expunge-thread (thread)
7921   "Mark all articles in THREAD as read."
7922   (let* ((number (mail-header-number (car thread))))
7923     (incf gnus-newsgroup-expunged-tally)
7924     ;; We also mark as read here, if that's wanted.
7925     (setq gnus-newsgroup-unreads
7926           (delq number gnus-newsgroup-unreads))
7927     (if gnus-newsgroup-auto-expire
7928         (push number gnus-newsgroup-expirable)
7929       (push (cons number gnus-low-score-mark)
7930             gnus-newsgroup-reads)))
7931   ;; Go recursively through all subthreads.
7932   (mapcar 'gnus-expunge-thread (cdr thread)))
7933
7934 ;; Summary article oriented commands
7935
7936 (defun gnus-summary-refer-parent-article (n)
7937   "Refer parent article N times.
7938 If N is negative, go to ancestor -N instead.
7939 The difference between N and the number of articles fetched is returned."
7940   (interactive "p")
7941   (let ((skip 1)
7942         error header ref)
7943     (when (not (natnump n))
7944       (setq skip (abs n)
7945             n 1))
7946     (while (and (> n 0)
7947                 (not error))
7948       (setq header (gnus-summary-article-header))
7949       (if (and (eq (mail-header-number header)
7950                    (cdr gnus-article-current))
7951                (equal gnus-newsgroup-name
7952                       (car gnus-article-current)))
7953           ;; If we try to find the parent of the currently
7954           ;; displayed article, then we take a look at the actual
7955           ;; References header, since this is slightly more
7956           ;; reliable than the References field we got from the
7957           ;; server.
7958           (save-excursion
7959             (set-buffer gnus-original-article-buffer)
7960             (nnheader-narrow-to-headers)
7961             (unless (setq ref (message-fetch-field "references"))
7962               (setq ref (message-fetch-field "in-reply-to")))
7963             (widen))
7964         (setq ref
7965               ;; It's not the current article, so we take a bet on
7966               ;; the value we got from the server.
7967               (mail-header-references header)))
7968       (if (and ref
7969                (not (equal ref "")))
7970           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7971             (gnus-message 1 "Couldn't find parent"))
7972         (gnus-message 1 "No references in article %d"
7973                       (gnus-summary-article-number))
7974         (setq error t))
7975       (decf n))
7976     (gnus-summary-position-point)
7977     n))
7978
7979 (defun gnus-summary-refer-references ()
7980   "Fetch all articles mentioned in the References header.
7981 Return the number of articles fetched."
7982   (interactive)
7983   (let ((ref (mail-header-references (gnus-summary-article-header)))
7984         (current (gnus-summary-article-number))
7985         (n 0))
7986     (if (or (not ref)
7987             (equal ref ""))
7988         (error "No References in the current article")
7989       ;; For each Message-ID in the References header...
7990       (while (string-match "<[^>]*>" ref)
7991         (incf n)
7992         ;; ... fetch that article.
7993         (gnus-summary-refer-article
7994          (prog1 (match-string 0 ref)
7995            (setq ref (substring ref (match-end 0))))))
7996       (gnus-summary-goto-subject current)
7997       (gnus-summary-position-point)
7998       n)))
7999
8000 (defun gnus-summary-refer-thread (&optional limit)
8001   "Fetch all articles in the current thread.
8002 If LIMIT (the numerical prefix), fetch that many old headers instead
8003 of what's specified by the `gnus-refer-thread-limit' variable."
8004   (interactive "P")
8005   (let ((id (mail-header-id (gnus-summary-article-header)))
8006         (limit (if limit (prefix-numeric-value limit)
8007                  gnus-refer-thread-limit)))
8008     (unless (eq gnus-fetch-old-headers 'invisible)
8009       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8010       ;; Retrieve the headers and read them in.
8011       (if (eq (if (numberp limit)
8012                   (gnus-retrieve-headers
8013                    (list (min
8014                           (+ (mail-header-number
8015                               (gnus-summary-article-header))
8016                              limit)
8017                           gnus-newsgroup-end))
8018                    gnus-newsgroup-name (* limit 2))
8019                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8020                 ;; headers.
8021                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8022                                        gnus-newsgroup-name limit))
8023               'nov)
8024           (gnus-build-all-threads)
8025         (error "Can't fetch thread from backends that don't support NOV"))
8026       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8027     (gnus-summary-limit-include-thread id)))
8028
8029 (defun gnus-summary-refer-article (message-id)
8030   "Fetch an article specified by MESSAGE-ID."
8031   (interactive "sMessage-ID: ")
8032   (when (and (stringp message-id)
8033              (not (zerop (length message-id))))
8034     ;; Construct the correct Message-ID if necessary.
8035     ;; Suggested by tale@pawl.rpi.edu.
8036     (unless (string-match "^<" message-id)
8037       (setq message-id (concat "<" message-id)))
8038     (unless (string-match ">$" message-id)
8039       (setq message-id (concat message-id ">")))
8040     (let* ((header (gnus-id-to-header message-id))
8041            (sparse (and header
8042                         (gnus-summary-article-sparse-p
8043                          (mail-header-number header))
8044                         (memq (mail-header-number header)
8045                               gnus-newsgroup-limit)))
8046            number)
8047       (cond
8048        ;; If the article is present in the buffer we just go to it.
8049        ((and header
8050              (or (not (gnus-summary-article-sparse-p
8051                        (mail-header-number header)))
8052                  sparse))
8053         (prog1
8054             (gnus-summary-goto-article
8055              (mail-header-number header) nil t)
8056           (when sparse
8057             (gnus-summary-update-article (mail-header-number header)))))
8058        (t
8059         ;; We fetch the article.
8060         (catch 'found
8061           (dolist (gnus-override-method (gnus-refer-article-methods))
8062             (when (and (gnus-check-server gnus-override-method)
8063                        ;; Fetch the header,
8064                        (setq number (gnus-summary-insert-subject message-id)))
8065               ;; and display the article.
8066               (gnus-summary-select-article nil nil nil number)
8067               (throw 'found t)))
8068           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8069
8070 (defun gnus-refer-article-methods ()
8071   "Return a list of referable methods."
8072   (cond
8073    ;; No method, so we default to current and native.
8074    ((null gnus-refer-article-method)
8075     (list gnus-current-select-method gnus-select-method))
8076    ;; Current.
8077    ((eq 'current gnus-refer-article-method)
8078     (list gnus-current-select-method))
8079    ;; List of select methods.
8080    ((not (and (symbolp (car gnus-refer-article-method))
8081               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8082     (let (out)
8083       (dolist (method gnus-refer-article-method)
8084         (push (if (eq 'current method)
8085                   gnus-current-select-method
8086                 method)
8087               out))
8088       (nreverse out)))
8089    ;; One single select method.
8090    (t
8091     (list gnus-refer-article-method))))
8092
8093 (defun gnus-summary-edit-parameters ()
8094   "Edit the group parameters of the current group."
8095   (interactive)
8096   (gnus-group-edit-group gnus-newsgroup-name 'params))
8097
8098 (defun gnus-summary-customize-parameters ()
8099   "Customize the group parameters of the current group."
8100   (interactive)
8101   (gnus-group-customize gnus-newsgroup-name))
8102
8103 (defun gnus-summary-enter-digest-group (&optional force)
8104   "Enter an nndoc group based on the current article.
8105 If FORCE, force a digest interpretation.  If not, try
8106 to guess what the document format is."
8107   (interactive "P")
8108   (let ((conf gnus-current-window-configuration))
8109     (save-excursion
8110       (gnus-summary-select-article))
8111     (setq gnus-current-window-configuration conf)
8112     (let* ((name (format "%s-%d"
8113                          (gnus-group-prefixed-name
8114                           gnus-newsgroup-name (list 'nndoc ""))
8115                          (save-excursion
8116                            (set-buffer gnus-summary-buffer)
8117                            gnus-current-article)))
8118            (ogroup gnus-newsgroup-name)
8119            (params (append (gnus-info-params (gnus-get-info ogroup))
8120                            (list (cons 'to-group ogroup))
8121                            (list (cons 'save-article-group ogroup))))
8122            (case-fold-search t)
8123            (buf (current-buffer))
8124            dig to-address)
8125       (save-excursion
8126         (set-buffer gnus-original-article-buffer)
8127         ;; Have the digest group inherit the main mail address of
8128         ;; the parent article.
8129         (when (setq to-address (or (gnus-fetch-field "reply-to")
8130                                    (gnus-fetch-field "from")))
8131           (setq params (append
8132                         (list (cons 'to-address
8133                                     (funcall gnus-decode-encoded-word-function
8134                                              to-address))))))
8135         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8136         (insert-buffer-substring gnus-original-article-buffer)
8137         ;; Remove lines that may lead nndoc to misinterpret the
8138         ;; document type.
8139         (narrow-to-region
8140          (goto-char (point-min))
8141          (or (search-forward "\n\n" nil t) (point)))
8142         (goto-char (point-min))
8143         (delete-matching-lines "^Path:\\|^From ")
8144         (widen))
8145       (unwind-protect
8146           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8147                     (gnus-newsgroup-ephemeral-ignored-charsets
8148                      gnus-newsgroup-ignored-charsets))
8149                 (gnus-group-read-ephemeral-group
8150                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8151                               (nndoc-article-type
8152                                ,(if force 'mbox 'guess)))
8153                  t nil nil nil
8154                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8155                                                         "ADAPT")))))
8156               ;; Make all postings to this group go to the parent group.
8157               (nconc (gnus-info-params (gnus-get-info name))
8158                      params)
8159             ;; Couldn't select this doc group.
8160             (switch-to-buffer buf)
8161             (gnus-set-global-variables)
8162             (gnus-configure-windows 'summary)
8163             (gnus-message 3 "Article couldn't be entered?"))
8164         (kill-buffer dig)))))
8165
8166 (defun gnus-summary-read-document (n)
8167   "Open a new group based on the current article(s).
8168 This will allow you to read digests and other similar
8169 documents as newsgroups.
8170 Obeys the standard process/prefix convention."
8171   (interactive "P")
8172   (let* ((articles (gnus-summary-work-articles n))
8173          (ogroup gnus-newsgroup-name)
8174          (params (append (gnus-info-params (gnus-get-info ogroup))
8175                          (list (cons 'to-group ogroup))))
8176          article group egroup groups vgroup)
8177     (while (setq article (pop articles))
8178       (setq group (format "%s-%d" gnus-newsgroup-name article))
8179       (gnus-summary-remove-process-mark article)
8180       (when (gnus-summary-display-article article)
8181         (save-excursion
8182           (with-temp-buffer
8183             (insert-buffer-substring gnus-original-article-buffer)
8184             ;; Remove some headers that may lead nndoc to make
8185             ;; the wrong guess.
8186             (message-narrow-to-head)
8187             (goto-char (point-min))
8188             (delete-matching-lines "^\\(Path\\):\\|^From ")
8189             (widen)
8190             (if (setq egroup
8191                       (gnus-group-read-ephemeral-group
8192                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8193                                      (nndoc-article-type guess))
8194                        t nil t))
8195                 (progn
8196             ;; Make all postings to this group go to the parent group.
8197                   (nconc (gnus-info-params (gnus-get-info egroup))
8198                          params)
8199                   (push egroup groups))
8200               ;; Couldn't select this doc group.
8201               (gnus-error 3 "Article couldn't be entered"))))))
8202     ;; Now we have selected all the documents.
8203     (cond
8204      ((not groups)
8205       (error "None of the articles could be interpreted as documents"))
8206      ((gnus-group-read-ephemeral-group
8207        (setq vgroup (format
8208                      "nnvirtual:%s-%s" gnus-newsgroup-name
8209                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8210        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8211        t
8212        (cons (current-buffer) 'summary)))
8213      (t
8214       (error "Couldn't select virtual nndoc group")))))
8215
8216 (defun gnus-summary-isearch-article (&optional regexp-p)
8217   "Do incremental search forward on the current article.
8218 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8219   (interactive "P")
8220   (gnus-summary-select-article)
8221   (gnus-configure-windows 'article)
8222   (gnus-eval-in-buffer-window gnus-article-buffer
8223     (save-restriction
8224       (widen)
8225       (isearch-forward regexp-p))))
8226
8227 (defun gnus-summary-search-article-forward (regexp &optional backward)
8228   "Search for an article containing REGEXP forward.
8229 If BACKWARD, search backward instead."
8230   (interactive
8231    (list (read-string
8232           (format "Search article %s (regexp%s): "
8233                   (if current-prefix-arg "backward" "forward")
8234                   (if gnus-last-search-regexp
8235                       (concat ", default " gnus-last-search-regexp)
8236                     "")))
8237          current-prefix-arg))
8238   (if (string-equal regexp "")
8239       (setq regexp (or gnus-last-search-regexp ""))
8240     (setq gnus-last-search-regexp regexp)
8241     (setq gnus-article-before-search gnus-current-article))
8242   ;; Intentionally set gnus-last-article.
8243   (setq gnus-last-article gnus-article-before-search)
8244   (let ((gnus-last-article gnus-last-article))
8245     (if (gnus-summary-search-article regexp backward)
8246         (gnus-summary-show-thread)
8247       (error "Search failed: \"%s\"" regexp))))
8248
8249 (defun gnus-summary-search-article-backward (regexp)
8250   "Search for an article containing REGEXP backward."
8251   (interactive
8252    (list (read-string
8253           (format "Search article backward (regexp%s): "
8254                   (if gnus-last-search-regexp
8255                       (concat ", default " gnus-last-search-regexp)
8256                     "")))))
8257   (gnus-summary-search-article-forward regexp 'backward))
8258
8259 (defun gnus-summary-search-article (regexp &optional backward)
8260   "Search for an article containing REGEXP.
8261 Optional argument BACKWARD means do search for backward.
8262 `gnus-select-article-hook' is not called during the search."
8263   ;; We have to require this here to make sure that the following
8264   ;; dynamic binding isn't shadowed by autoloading.
8265   (require 'gnus-async)
8266   (require 'gnus-art)
8267   (let ((gnus-select-article-hook nil)  ;Disable hook.
8268         (gnus-article-prepare-hook nil)
8269         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8270         (gnus-use-article-prefetch nil)
8271         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8272         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8273         (gnus-visual nil)
8274         (gnus-keep-backlog nil)
8275         (gnus-break-pages nil)
8276         (gnus-summary-display-arrow nil)
8277         (gnus-updated-mode-lines nil)
8278         (gnus-auto-center-summary nil)
8279         (sum (current-buffer))
8280         (gnus-display-mime-function nil)
8281         (found nil)
8282         point)
8283     (gnus-save-hidden-threads
8284       (gnus-summary-select-article)
8285       (set-buffer gnus-article-buffer)
8286       (goto-char (window-point (get-buffer-window (current-buffer))))
8287       (when backward
8288         (forward-line -1))
8289       (while (not found)
8290         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8291         (if (if backward
8292                 (re-search-backward regexp nil t)
8293               (re-search-forward regexp nil t))
8294             ;; We found the regexp.
8295             (progn
8296               (setq found 'found)
8297               (beginning-of-line)
8298               (set-window-start
8299                (get-buffer-window (current-buffer))
8300                (point))
8301               (forward-line 1)
8302               (set-window-point
8303                (get-buffer-window (current-buffer))
8304                (point))
8305               (set-buffer sum)
8306               (setq point (point)))
8307           ;; We didn't find it, so we go to the next article.
8308           (set-buffer sum)
8309           (setq found 'not)
8310           (while (eq found 'not)
8311             (if (not (if backward (gnus-summary-find-prev)
8312                        (gnus-summary-find-next)))
8313                 ;; No more articles.
8314                 (setq found t)
8315               ;; Select the next article and adjust point.
8316               (unless (gnus-summary-article-sparse-p
8317                        (gnus-summary-article-number))
8318                 (setq found nil)
8319                 (gnus-summary-select-article)
8320                 (set-buffer gnus-article-buffer)
8321                 (widen)
8322                 (goto-char (if backward (point-max) (point-min))))))))
8323       (gnus-message 7 ""))
8324     ;; Return whether we found the regexp.
8325     (when (eq found 'found)
8326       (goto-char point)
8327       (gnus-summary-show-thread)
8328       (gnus-summary-goto-subject gnus-current-article)
8329       (gnus-summary-position-point)
8330       t)))
8331
8332 (defun gnus-find-matching-articles (header regexp)
8333   "Return a list of all articles that match REGEXP on HEADER.
8334 This search includes all articles in the current group that Gnus has
8335 fetched headers for, whether they are displayed or not."
8336   (let ((articles nil)
8337         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8338         (case-fold-search t))
8339     (dolist (header gnus-newsgroup-headers)
8340       (when (string-match regexp (funcall func header))
8341         (push (mail-header-number header) articles)))
8342     (nreverse articles)))
8343
8344 (defun gnus-summary-find-matching (header regexp &optional backward unread
8345                                           not-case-fold not-matching)
8346   "Return a list of all articles that match REGEXP on HEADER.
8347 The search stars on the current article and goes forwards unless
8348 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8349 If UNREAD is non-nil, only unread articles will
8350 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8351 in the comparisons. If NOT-MATCHING, return a list of all articles that
8352 not match REGEXP on HEADER."
8353   (let ((case-fold-search (not not-case-fold))
8354         articles d func)
8355     (if (consp header)
8356         (if (eq (car header) 'extra)
8357             (setq func
8358                   `(lambda (h)
8359                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8360                          "")))
8361           (error "%s is an invalid header" header))
8362       (unless (fboundp (intern (concat "mail-header-" header)))
8363         (error "%s is not a valid header" header))
8364       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8365     (dolist (d (if (eq backward 'all)
8366                    gnus-newsgroup-data
8367                  (gnus-data-find-list
8368                   (gnus-summary-article-number)
8369                   (gnus-data-list backward))))
8370       (when (and (or (not unread)       ; We want all articles...
8371                      (gnus-data-unread-p d)) ; Or just unreads.
8372                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8373                  (if not-matching
8374                      (not (string-match
8375                            regexp
8376                            (funcall func (gnus-data-header d))))
8377                    (string-match regexp
8378                                  (funcall func (gnus-data-header d)))))
8379         (push (gnus-data-number d) articles))) ; Success!
8380     (nreverse articles)))
8381
8382 (defun gnus-summary-execute-command (header regexp command &optional backward)
8383   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8384 If HEADER is an empty string (or nil), the match is done on the entire
8385 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8386   (interactive
8387    (list (let ((completion-ignore-case t))
8388            (completing-read
8389             "Header name: "
8390             (mapcar (lambda (header) (list (format "%s" header)))
8391                     (append
8392                      '("Number" "Subject" "From" "Lines" "Date"
8393                        "Message-ID" "Xref" "References" "Body")
8394                      gnus-extra-headers))
8395             nil 'require-match))
8396          (read-string "Regexp: ")
8397          (read-key-sequence "Command: ")
8398          current-prefix-arg))
8399   (when (equal header "Body")
8400     (setq header ""))
8401   ;; Hidden thread subtrees must be searched as well.
8402   (gnus-summary-show-all-threads)
8403   ;; We don't want to change current point nor window configuration.
8404   (save-excursion
8405     (save-window-excursion
8406       (let (gnus-visual
8407             gnus-treat-strip-trailing-blank-lines
8408             gnus-treat-strip-leading-blank-lines
8409             gnus-treat-strip-multiple-blank-lines
8410             gnus-treat-hide-boring-headers
8411             gnus-treat-fold-newsgroups
8412             gnus-article-prepare-hook)
8413         (gnus-message 6 "Executing %s..." (key-description command))
8414         ;; We'd like to execute COMMAND interactively so as to give arguments.
8415         (gnus-execute header regexp
8416                       `(call-interactively ',(key-binding command))
8417                       backward)
8418         (gnus-message 6 "Executing %s...done" (key-description command))))))
8419
8420 (defun gnus-summary-beginning-of-article ()
8421   "Scroll the article back to the beginning."
8422   (interactive)
8423   (gnus-summary-select-article)
8424   (gnus-configure-windows 'article)
8425   (gnus-eval-in-buffer-window gnus-article-buffer
8426     (widen)
8427     (goto-char (point-min))
8428     (when gnus-page-broken
8429       (gnus-narrow-to-page))))
8430
8431 (defun gnus-summary-end-of-article ()
8432   "Scroll to the end of the article."
8433   (interactive)
8434   (gnus-summary-select-article)
8435   (gnus-configure-windows 'article)
8436   (gnus-eval-in-buffer-window gnus-article-buffer
8437     (widen)
8438     (goto-char (point-max))
8439     (recenter -3)
8440     (when gnus-page-broken
8441       (gnus-narrow-to-page))))
8442
8443 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8444   "Truncate to LEN and quote all \"(\"'s in STRING."
8445   (gnus-replace-in-string (if (and len (> (length string) len))
8446                               (substring string 0 len)
8447                             string)
8448                           "[()]" "\\\\\\&"))
8449
8450 (defun gnus-summary-print-article (&optional filename n)
8451   "Generate and print a PostScript image of the N next (mail) articles.
8452
8453 If N is negative, print the N previous articles.  If N is nil and articles
8454 have been marked with the process mark, print these instead.
8455
8456 If the optional first argument FILENAME is nil, send the image to the
8457 printer.  If FILENAME is a string, save the PostScript image in a file with
8458 that name.  If FILENAME is a number, prompt the user for the name of the file
8459 to save in."
8460   (interactive (list (ps-print-preprint current-prefix-arg)))
8461   (dolist (article (gnus-summary-work-articles n))
8462     (gnus-summary-select-article nil nil 'pseudo article)
8463     (gnus-eval-in-buffer-window gnus-article-buffer
8464       (gnus-print-buffer))
8465     (gnus-summary-remove-process-mark article))
8466   (ps-despool filename))
8467
8468 (defun gnus-print-buffer ()
8469   (let ((buffer (generate-new-buffer " *print*")))
8470     (unwind-protect
8471         (progn
8472           (copy-to-buffer buffer (point-min) (point-max))
8473           (set-buffer buffer)
8474           (gnus-article-delete-invisible-text)
8475           (gnus-remove-text-with-property 'gnus-decoration)
8476           (when (gnus-visual-p 'article-highlight 'highlight)
8477             ;; Copy-to-buffer doesn't copy overlay.  So redo
8478             ;; highlight.
8479             (let ((gnus-article-buffer buffer))
8480               (gnus-article-highlight-citation t)
8481               (gnus-article-highlight-signature)))
8482           (let ((ps-left-header
8483                  (list
8484                   (concat "("
8485                           (gnus-summary-print-truncate-and-quote
8486                            (mail-header-subject gnus-current-headers)
8487                            66) ")")
8488                   (concat "("
8489                           (gnus-summary-print-truncate-and-quote
8490                            (mail-header-from gnus-current-headers)
8491                            45) ")")))
8492                 (ps-right-header
8493                  (list
8494                   "/pagenumberstring load"
8495                   (concat "("
8496                           (mail-header-date gnus-current-headers) ")"))))
8497             (gnus-run-hooks 'gnus-ps-print-hook)
8498             (save-excursion
8499               (if window-system
8500                   (ps-spool-buffer-with-faces)
8501                 (ps-spool-buffer)))))
8502       (kill-buffer buffer))))
8503
8504 (defun gnus-summary-show-article (&optional arg)
8505   "Force redisplaying of the current article.
8506 If ARG (the prefix) is a number, show the article with the charset
8507 defined in `gnus-summary-show-article-charset-alist', or the charset
8508 input.
8509 If ARG (the prefix) is non-nil and not a number, show the raw article
8510 without any article massaging functions being run.  Normally, the key strokes
8511 are `C-u g'."
8512   (interactive "P")
8513   (cond
8514    ((numberp arg)
8515     (gnus-summary-show-article t)
8516     (let ((gnus-newsgroup-charset
8517            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8518                (mm-read-coding-system
8519                 "View as charset: " ;; actually it is coding system.
8520                 (save-excursion
8521                   (set-buffer gnus-article-buffer)
8522                   (mm-detect-coding-region (point) (point-max))))))
8523           (gnus-newsgroup-ignored-charsets 'gnus-all))
8524       (gnus-summary-select-article nil 'force)
8525       (let ((deps gnus-newsgroup-dependencies)
8526             head header lines)
8527         (save-excursion
8528           (set-buffer gnus-original-article-buffer)
8529           (save-restriction
8530             (message-narrow-to-head)
8531             (setq head (buffer-string))
8532             (goto-char (point-min))
8533             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8534               (goto-char (point-max))
8535               (widen)
8536               (setq lines (1- (count-lines (point) (point-max))))))
8537           (with-temp-buffer
8538             (insert (format "211 %d Article retrieved.\n"
8539                             (cdr gnus-article-current)))
8540             (insert head)
8541             (if lines (insert (format "Lines: %d\n" lines)))
8542             (insert ".\n")
8543             (let ((nntp-server-buffer (current-buffer)))
8544               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8545         (gnus-data-set-header
8546          (gnus-data-find (cdr gnus-article-current))
8547          header)
8548         (gnus-summary-update-article-line
8549          (cdr gnus-article-current) header)
8550         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8551           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8552    ((not arg)
8553     ;; Select the article the normal way.
8554     (gnus-summary-select-article nil 'force))
8555    (t
8556     ;; We have to require this here to make sure that the following
8557     ;; dynamic binding isn't shadowed by autoloading.
8558     (require 'gnus-async)
8559     (require 'gnus-art)
8560     ;; Bind the article treatment functions to nil.
8561     (let ((gnus-have-all-headers t)
8562           gnus-article-prepare-hook
8563           gnus-article-decode-hook
8564           gnus-display-mime-function
8565           gnus-break-pages)
8566       ;; Destroy any MIME parts.
8567       (when (gnus-buffer-live-p gnus-article-buffer)
8568         (save-excursion
8569           (set-buffer gnus-article-buffer)
8570           (mm-destroy-parts gnus-article-mime-handles)
8571           ;; Set it to nil for safety reason.
8572           (setq gnus-article-mime-handle-alist nil)
8573           (setq gnus-article-mime-handles nil)))
8574       (gnus-summary-select-article nil 'force))))
8575   (gnus-summary-goto-subject gnus-current-article)
8576   (gnus-summary-position-point))
8577
8578 (defun gnus-summary-show-raw-article ()
8579   "Show the raw article without any article massaging functions being run."
8580   (interactive)
8581   (gnus-summary-show-article t))
8582
8583 (defun gnus-summary-verbose-headers (&optional arg)
8584   "Toggle permanent full header display.
8585 If ARG is a positive number, turn header display on.
8586 If ARG is a negative number, turn header display off."
8587   (interactive "P")
8588   (setq gnus-show-all-headers
8589         (cond ((or (not (numberp arg))
8590                    (zerop arg))
8591                (not gnus-show-all-headers))
8592               ((natnump arg)
8593                t)))
8594   (gnus-summary-show-article))
8595
8596 (defun gnus-summary-toggle-header (&optional arg)
8597   "Show the headers if they are hidden, or hide them if they are shown.
8598 If ARG is a positive number, show the entire header.
8599 If ARG is a negative number, hide the unwanted header lines."
8600   (interactive "P")
8601   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8602                      (get-buffer-window gnus-article-buffer t))))
8603     (with-current-buffer gnus-article-buffer
8604       (widen)
8605       (article-narrow-to-head)
8606       (let* ((buffer-read-only nil)
8607              (inhibit-point-motion-hooks t)
8608              (hidden (if (numberp arg)
8609                          (>= arg 0)
8610                        (gnus-article-hidden-text-p 'headers)))
8611              s e)
8612         (delete-region (point-min) (point-max))
8613         (with-current-buffer gnus-original-article-buffer
8614           (goto-char (setq s (point-min)))
8615           (setq e (if (search-forward "\n\n" nil t)
8616                       (1- (point))
8617                     (point-max))))
8618         (insert-buffer-substring gnus-original-article-buffer s e)
8619         (run-hooks 'gnus-article-decode-hook)
8620         (if hidden
8621             (let ((gnus-treat-hide-headers nil)
8622                   (gnus-treat-hide-boring-headers nil))
8623               (gnus-delete-wash-type 'headers)
8624               (gnus-treat-article 'head))
8625           (gnus-treat-article 'head))
8626         (widen)
8627         (if window
8628             (set-window-start window (goto-char (point-min))))
8629         (setq gnus-page-broken
8630               (when gnus-break-pages
8631                 (gnus-narrow-to-page)
8632                 t))
8633         (gnus-set-mode-line 'article)))))
8634
8635 (defun gnus-summary-show-all-headers ()
8636   "Make all header lines visible."
8637   (interactive)
8638   (gnus-summary-toggle-header 1))
8639
8640 (defun gnus-summary-caesar-message (&optional arg)
8641   "Caesar rotate the current article by 13.
8642 The numerical prefix specifies how many places to rotate each letter
8643 forward."
8644   (interactive "P")
8645   (gnus-summary-select-article)
8646   (let ((mail-header-separator ""))
8647     (gnus-eval-in-buffer-window gnus-article-buffer
8648       (save-restriction
8649         (widen)
8650         (let ((start (window-start))
8651               buffer-read-only)
8652           (message-caesar-buffer-body arg)
8653           (set-window-start (get-buffer-window (current-buffer)) start))))))
8654
8655 (autoload 'unmorse-region "morse"
8656   "Convert morse coded text in region to ordinary ASCII text."
8657   t)
8658
8659 (defun gnus-summary-morse-message (&optional arg)
8660   "Morse decode the current article."
8661   (interactive "P")
8662   (gnus-summary-select-article)
8663   (let ((mail-header-separator ""))
8664     (gnus-eval-in-buffer-window gnus-article-buffer
8665       (save-excursion
8666         (save-restriction
8667           (widen)
8668           (let ((pos (window-start))
8669                 buffer-read-only)
8670             (goto-char (point-min))
8671             (when (message-goto-body)
8672               (gnus-narrow-to-body))
8673             (goto-char (point-min))
8674             (while (re-search-forward "·" (point-max) t)
8675               (replace-match "."))
8676             (unmorse-region (point-min) (point-max))
8677             (widen)
8678             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8679
8680 (defun gnus-summary-stop-page-breaking ()
8681   "Stop page breaking in the current article."
8682   (interactive)
8683   (gnus-summary-select-article)
8684   (gnus-eval-in-buffer-window gnus-article-buffer
8685     (widen)
8686     (when (gnus-visual-p 'page-marker)
8687       (let ((buffer-read-only nil))
8688         (gnus-remove-text-with-property 'gnus-prev)
8689         (gnus-remove-text-with-property 'gnus-next))
8690       (setq gnus-page-broken nil))))
8691
8692 (defun gnus-summary-move-article (&optional n to-newsgroup
8693                                             select-method action)
8694   "Move the current article to a different newsgroup.
8695 If N is a positive number, move the N next articles.
8696 If N is a negative number, move the N previous articles.
8697 If N is nil and any articles have been marked with the process mark,
8698 move those articles instead.
8699 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8700 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8701 re-spool using this method.
8702
8703 When called interactively with TO-NEWSGROUP being nil, the value of
8704 the variable `gnus-move-split-methods' is used for finding a default
8705 for the target newsgroup.
8706
8707 For this function to work, both the current newsgroup and the
8708 newsgroup that you want to move to have to support the `request-move'
8709 and `request-accept' functions.
8710
8711 ACTION can be either `move' (the default), `crosspost' or `copy'."
8712   (interactive "P")
8713   (unless action
8714     (setq action 'move))
8715   ;; Check whether the source group supports the required functions.
8716   (cond ((and (eq action 'move)
8717               (not (gnus-check-backend-function
8718                     'request-move-article gnus-newsgroup-name)))
8719          (error "The current group does not support article moving"))
8720         ((and (eq action 'crosspost)
8721               (not (gnus-check-backend-function
8722                     'request-replace-article gnus-newsgroup-name)))
8723          (error "The current group does not support article editing")))
8724   (let ((articles (gnus-summary-work-articles n))
8725         (prefix (if (gnus-check-backend-function
8726                      'request-move-article gnus-newsgroup-name)
8727                     (gnus-group-real-prefix gnus-newsgroup-name)
8728                   ""))
8729         (names '((move "Move" "Moving")
8730                  (copy "Copy" "Copying")
8731                  (crosspost "Crosspost" "Crossposting")))
8732         (copy-buf (save-excursion
8733                     (nnheader-set-temp-buffer " *copy article*")))
8734         art-group to-method new-xref article to-groups)
8735     (unless (assq action names)
8736       (error "Unknown action %s" action))
8737     ;; Read the newsgroup name.
8738     (when (and (not to-newsgroup)
8739                (not select-method))
8740       (if (and gnus-move-split-methods
8741                (not
8742                 (and (memq gnus-current-article articles)
8743                      (gnus-buffer-live-p gnus-original-article-buffer))))
8744           ;; When `gnus-move-split-methods' is non-nil, we have to
8745           ;; select an article to give `gnus-read-move-group-name' an
8746           ;; opportunity to suggest an appropriate default.  However,
8747           ;; we needn't render or mark the article.
8748           (let ((gnus-display-mime-function nil)
8749                 (gnus-article-prepare-hook nil)
8750                 (gnus-mark-article-hook nil))
8751             (gnus-summary-select-article nil nil nil (car articles))))
8752       (setq to-newsgroup
8753             (gnus-read-move-group-name
8754              (cadr (assq action names))
8755              (symbol-value (intern (format "gnus-current-%s-group" action)))
8756              articles prefix))
8757       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8758     (setq to-method (or select-method
8759                         (gnus-server-to-method
8760                          (gnus-group-method to-newsgroup))))
8761     ;; Check the method we are to move this article to...
8762     (unless (gnus-check-backend-function
8763              'request-accept-article (car to-method))
8764       (error "%s does not support article copying" (car to-method)))
8765     (unless (gnus-check-server to-method)
8766       (error "Can't open server %s" (car to-method)))
8767     (gnus-message 6 "%s to %s: %s..."
8768                   (caddr (assq action names))
8769                   (or (car select-method) to-newsgroup) articles)
8770     (while articles
8771       (setq article (pop articles))
8772       (setq
8773        art-group
8774        (cond
8775         ;; Move the article.
8776         ((eq action 'move)
8777          ;; Remove this article from future suppression.
8778          (gnus-dup-unsuppress-article article)
8779          (gnus-request-move-article
8780           article                       ; Article to move
8781           gnus-newsgroup-name           ; From newsgroup
8782           (nth 1 (gnus-find-method-for-group
8783                   gnus-newsgroup-name)) ; Server
8784           (list 'gnus-request-accept-article
8785                 to-newsgroup (list 'quote select-method)
8786                 (not articles) t)       ; Accept form
8787           (not articles)))              ; Only save nov last time
8788         ;; Copy the article.
8789         ((eq action 'copy)
8790          (save-excursion
8791            (set-buffer copy-buf)
8792            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8793              (gnus-request-accept-article
8794               to-newsgroup select-method (not articles) t))))
8795         ;; Crosspost the article.
8796         ((eq action 'crosspost)
8797          (let ((xref (message-tokenize-header
8798                       (mail-header-xref (gnus-summary-article-header article))
8799                       " ")))
8800            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8801                                   ":" (number-to-string article)))
8802            (unless xref
8803              (setq xref (list (system-name))))
8804            (setq new-xref
8805                  (concat
8806                   (mapconcat 'identity
8807                              (delete "Xref:" (delete new-xref xref))
8808                              " ")
8809                   " " new-xref))
8810            (save-excursion
8811              (set-buffer copy-buf)
8812              ;; First put the article in the destination group.
8813              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8814              (when (consp (setq art-group
8815                                 (gnus-request-accept-article
8816                                  to-newsgroup select-method (not articles))))
8817                (setq new-xref (concat new-xref " " (car art-group)
8818                                       ":"
8819                                       (number-to-string (cdr art-group))))
8820                ;; Now we have the new Xrefs header, so we insert
8821                ;; it and replace the new article.
8822                (nnheader-replace-header "Xref" new-xref)
8823                (gnus-request-replace-article
8824                 (cdr art-group) to-newsgroup (current-buffer))
8825                art-group))))))
8826       (cond
8827        ((not art-group)
8828         (gnus-message 1 "Couldn't %s article %s: %s"
8829                       (cadr (assq action names)) article
8830                       (nnheader-get-report (car to-method))))
8831        ((eq art-group 'junk)
8832         (when (eq action 'move)
8833           (gnus-summary-mark-article article gnus-canceled-mark)
8834           (gnus-message 4 "Deleted article %s" article)
8835           ;; run the delete hook
8836           (run-hook-with-args 'gnus-summary-article-delete-hook
8837                               action
8838                               (gnus-data-header
8839                                (assoc article (gnus-data-list nil)))
8840                               gnus-newsgroup-name nil
8841                               select-method)))
8842        (t
8843         (let* ((pto-group (gnus-group-prefixed-name
8844                            (car art-group) to-method))
8845                (entry
8846                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8847                (info (nth 2 entry))
8848                (to-group (gnus-info-group info))
8849                to-marks)
8850           ;; Update the group that has been moved to.
8851           (when (and info
8852                      (memq action '(move copy)))
8853             (unless (member to-group to-groups)
8854               (push to-group to-groups))
8855
8856             (unless (memq article gnus-newsgroup-unreads)
8857               (push 'read to-marks)
8858               (gnus-info-set-read
8859                info (gnus-add-to-range (gnus-info-read info)
8860                                        (list (cdr art-group)))))
8861
8862             ;; See whether the article is to be put in the cache.
8863             (let ((marks gnus-article-mark-lists)
8864                   (to-article (cdr art-group)))
8865
8866               ;; Enter the article into the cache in the new group,
8867               ;; if that is required.
8868               (when gnus-use-cache
8869                 (gnus-cache-possibly-enter-article
8870                  to-group to-article
8871                  (memq article gnus-newsgroup-marked)
8872                  (memq article gnus-newsgroup-dormant)
8873                  (memq article gnus-newsgroup-unreads)))
8874
8875               (when gnus-preserve-marks
8876                 ;; Copy any marks over to the new group.
8877                 (when (and (equal to-group gnus-newsgroup-name)
8878                            (not (memq article gnus-newsgroup-unreads)))
8879                   ;; Mark this article as read in this group.
8880                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8881                   (setcdr (gnus-active to-group) to-article)
8882                   (setcdr gnus-newsgroup-active to-article))
8883
8884                 (while marks
8885                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8886                     (when (memq article (symbol-value
8887                                          (intern (format "gnus-newsgroup-%s"
8888                                                          (caar marks)))))
8889                       (push (cdar marks) to-marks)
8890                       ;; If the other group is the same as this group,
8891                       ;; then we have to add the mark to the list.
8892                       (when (equal to-group gnus-newsgroup-name)
8893                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8894                              (cons to-article
8895                                    (symbol-value
8896                                     (intern (format "gnus-newsgroup-%s"
8897                                                     (caar marks)))))))
8898                       ;; Copy the marks to other group.
8899                       (gnus-add-marked-articles
8900                        to-group (cdar marks) (list to-article) info)))
8901                   (setq marks (cdr marks)))
8902
8903                 (gnus-request-set-mark
8904                  to-group (list (list (list to-article) 'add to-marks))))
8905
8906               (gnus-dribble-enter
8907                (concat "(gnus-group-set-info '"
8908                        (gnus-prin1-to-string (gnus-get-info to-group))
8909                        ")"))))
8910
8911           ;; Update the Xref header in this article to point to
8912           ;; the new crossposted article we have just created.
8913           (when (eq action 'crosspost)
8914             (save-excursion
8915               (set-buffer copy-buf)
8916               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8917               (nnheader-replace-header "Xref" new-xref)
8918               (gnus-request-replace-article
8919                article gnus-newsgroup-name (current-buffer))))
8920
8921           ;; run the move/copy/crosspost/respool hook
8922           (run-hook-with-args 'gnus-summary-article-move-hook 
8923                               action
8924                               (gnus-data-header 
8925                                (assoc article (gnus-data-list nil)))
8926                               gnus-newsgroup-name
8927                               to-newsgroup
8928                               select-method))
8929
8930         ;;;!!!Why is this necessary?
8931         (set-buffer gnus-summary-buffer)
8932         
8933         (gnus-summary-goto-subject article)
8934         (when (eq action 'move)
8935           (gnus-summary-mark-article article gnus-canceled-mark))))
8936       (gnus-summary-remove-process-mark article))
8937     ;; Re-activate all groups that have been moved to.
8938     (save-excursion
8939       (set-buffer gnus-group-buffer)
8940       (let ((gnus-group-marked to-groups))
8941         (gnus-group-get-new-news-this-group nil t)))
8942
8943     (gnus-kill-buffer copy-buf)
8944     (gnus-summary-position-point)
8945     (gnus-set-mode-line 'summary)))
8946
8947 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8948   "Move the current article to a different newsgroup.
8949 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8950 When called interactively, if TO-NEWSGROUP is nil, use the value of
8951 the variable `gnus-move-split-methods' for finding a default target
8952 newsgroup.
8953 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8954 re-spool using this method."
8955   (interactive "P")
8956   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8957
8958 (defun gnus-summary-crosspost-article (&optional n)
8959   "Crosspost the current article to some other group."
8960   (interactive "P")
8961   (gnus-summary-move-article n nil nil 'crosspost))
8962
8963 (defcustom gnus-summary-respool-default-method nil
8964   "Default method type for respooling an article.
8965 If nil, use to the current newsgroup method."
8966   :type 'symbol
8967   :group 'gnus-summary-mail)
8968
8969 (defcustom gnus-summary-display-while-building nil
8970   "If not-nil, show and update the summary buffer as it's being built.
8971 If the value is t, update the buffer after every line is inserted.  If
8972 the value is an integer (N), update the display every N lines."
8973   :group 'gnus-thread
8974   :type '(choice (const :tag "off" nil)
8975                  number
8976                  (const :tag "frequently" t)))
8977
8978 (defun gnus-summary-respool-article (&optional n method)
8979   "Respool the current article.
8980 The article will be squeezed through the mail spooling process again,
8981 which means that it will be put in some mail newsgroup or other
8982 depending on `nnmail-split-methods'.
8983 If N is a positive number, respool the N next articles.
8984 If N is a negative number, respool the N previous articles.
8985 If N is nil and any articles have been marked with the process mark,
8986 respool those articles instead.
8987
8988 Respooling can be done both from mail groups and \"real\" newsgroups.
8989 In the former case, the articles in question will be moved from the
8990 current group into whatever groups they are destined to.  In the
8991 latter case, they will be copied into the relevant groups."
8992   (interactive
8993    (list current-prefix-arg
8994          (let* ((methods (gnus-methods-using 'respool))
8995                 (methname
8996                  (symbol-name (or gnus-summary-respool-default-method
8997                                   (car (gnus-find-method-for-group
8998                                         gnus-newsgroup-name)))))
8999                 (method
9000                  (gnus-completing-read-with-default
9001                   methname "What backend do you want to use when respooling?"
9002                   methods nil t nil 'gnus-mail-method-history))
9003                 ms)
9004            (cond
9005             ((zerop (length (setq ms (gnus-servers-using-backend
9006                                       (intern method)))))
9007              (list (intern method) ""))
9008             ((= 1 (length ms))
9009              (car ms))
9010             (t
9011              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9012                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9013                            ms-alist))))))))
9014   (unless method
9015     (error "No method given for respooling"))
9016   (if (assoc (symbol-name
9017               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9018              (gnus-methods-using 'respool))
9019       (gnus-summary-move-article n nil method)
9020     (gnus-summary-copy-article n nil method)))
9021
9022 (defun gnus-summary-import-article (file &optional edit)
9023   "Import an arbitrary file into a mail newsgroup."
9024   (interactive "fImport file: \nP")
9025   (let ((group gnus-newsgroup-name)
9026         (now (current-time))
9027         atts lines group-art)
9028     (unless (gnus-check-backend-function 'request-accept-article group)
9029       (error "%s does not support article importing" group))
9030     (or (file-readable-p file)
9031         (not (file-regular-p file))
9032         (error "Can't read %s" file))
9033     (save-excursion
9034       (set-buffer (gnus-get-buffer-create " *import file*"))
9035       (erase-buffer)
9036       (nnheader-insert-file-contents file)
9037       (goto-char (point-min))
9038       (if (nnheader-article-p)
9039           (save-restriction
9040             (goto-char (point-min))
9041             (search-forward "\n\n" nil t)
9042             (narrow-to-region (point-min) (1- (point)))
9043             (goto-char (point-min))
9044             (unless (re-search-forward "^date:" nil t)
9045               (goto-char (point-max))
9046               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9047        ;; This doesn't look like an article, so we fudge some headers.
9048         (setq atts (file-attributes file)
9049               lines (count-lines (point-min) (point-max)))
9050         (insert "From: " (read-string "From: ") "\n"
9051                 "Subject: " (read-string "Subject: ") "\n"
9052                 "Date: " (message-make-date (nth 5 atts)) "\n"
9053                 "Message-ID: " (message-make-message-id) "\n"
9054                 "Lines: " (int-to-string lines) "\n"
9055                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9056       (setq group-art (gnus-request-accept-article group nil t))
9057       (kill-buffer (current-buffer)))
9058     (setq gnus-newsgroup-active (gnus-activate-group group))
9059     (forward-line 1)
9060     (gnus-summary-goto-article (cdr group-art) nil t)
9061     (when edit
9062       (gnus-summary-edit-article))))
9063
9064 (defun gnus-summary-create-article ()
9065   "Create an article in a mail newsgroup."
9066   (interactive)
9067   (let ((group gnus-newsgroup-name)
9068         (now (current-time))
9069         group-art)
9070     (unless (gnus-check-backend-function 'request-accept-article group)
9071       (error "%s does not support article importing" group))
9072     (save-excursion
9073       (set-buffer (gnus-get-buffer-create " *import file*"))
9074       (erase-buffer)
9075       (goto-char (point-min))
9076       ;; This doesn't look like an article, so we fudge some headers.
9077       (insert "From: " (read-string "From: ") "\n"
9078               "Subject: " (read-string "Subject: ") "\n"
9079               "Date: " (message-make-date now) "\n"
9080               "Message-ID: " (message-make-message-id) "\n")
9081       (setq group-art (gnus-request-accept-article group nil t))
9082       (kill-buffer (current-buffer)))
9083     (setq gnus-newsgroup-active (gnus-activate-group group))
9084     (forward-line 1)
9085     (gnus-summary-goto-article (cdr group-art) nil t)
9086     (gnus-summary-edit-article)))
9087
9088 (defun gnus-summary-article-posted-p ()
9089   "Say whether the current (mail) article is available from news as well.
9090 This will be the case if the article has both been mailed and posted."
9091   (interactive)
9092   (let ((id (mail-header-references (gnus-summary-article-header)))
9093         (gnus-override-method (car (gnus-refer-article-methods))))
9094     (if (gnus-request-head id "")
9095         (gnus-message 2 "The current message was found on %s"
9096                       gnus-override-method)
9097       (gnus-message 2 "The current message couldn't be found on %s"
9098                     gnus-override-method)
9099       nil)))
9100
9101 (defun gnus-summary-expire-articles (&optional now)
9102   "Expire all articles that are marked as expirable in the current group."
9103   (interactive)
9104   (when (and (not gnus-group-is-exiting-without-update-p)
9105              (gnus-check-backend-function
9106               'request-expire-articles gnus-newsgroup-name))
9107     ;; This backend supports expiry.
9108     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9109            (expirable (if total
9110                           (progn
9111                             ;; We need to update the info for
9112                             ;; this group for `gnus-list-of-read-articles'
9113                             ;; to give us the right answer.
9114                             (gnus-run-hooks 'gnus-exit-group-hook)
9115                             (gnus-summary-update-info)
9116                             (gnus-list-of-read-articles gnus-newsgroup-name))
9117                         (setq gnus-newsgroup-expirable
9118                               (sort gnus-newsgroup-expirable '<))))
9119            (expiry-wait (if now 'immediate
9120                           (gnus-group-find-parameter
9121                            gnus-newsgroup-name 'expiry-wait)))
9122            (nnmail-expiry-target
9123             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9124                 nnmail-expiry-target))
9125            es)
9126       (when expirable
9127         ;; There are expirable articles in this group, so we run them
9128         ;; through the expiry process.
9129         (gnus-message 6 "Expiring articles...")
9130         (unless (gnus-check-group gnus-newsgroup-name)
9131           (error "Can't open server for %s" gnus-newsgroup-name))
9132         ;; The list of articles that weren't expired is returned.
9133         (save-excursion
9134           (if expiry-wait
9135               (let ((nnmail-expiry-wait-function nil)
9136                     (nnmail-expiry-wait expiry-wait))
9137                 (setq es (gnus-request-expire-articles
9138                           expirable gnus-newsgroup-name)))
9139             (setq es (gnus-request-expire-articles
9140                       expirable gnus-newsgroup-name)))
9141           (unless total
9142             (setq gnus-newsgroup-expirable es))
9143           ;; We go through the old list of expirable, and mark all
9144           ;; really expired articles as nonexistent.
9145           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9146             (let ((gnus-use-cache nil))
9147               (dolist (article expirable)
9148                 (when (and (not (memq article es))
9149                            (gnus-data-find article))
9150                   (gnus-summary-mark-article article gnus-canceled-mark)
9151                   (run-hook-with-args 'gnus-summary-article-expire-hook
9152                                       'delete
9153                                       (gnus-data-header
9154                                        (assoc article (gnus-data-list nil)))
9155                                       gnus-newsgroup-name
9156                                       nil
9157                                       nil))))))
9158         (gnus-message 6 "Expiring articles...done")))))
9159
9160 (defun gnus-summary-expire-articles-now ()
9161   "Expunge all expirable articles in the current group.
9162 This means that *all* articles that are marked as expirable will be
9163 deleted forever, right now."
9164   (interactive)
9165   (or gnus-expert-user
9166       (gnus-yes-or-no-p
9167        "Are you really, really, really sure you want to delete all these messages? ")
9168       (error "Phew!"))
9169   (gnus-summary-expire-articles t))
9170
9171 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9172 (defun gnus-summary-delete-article (&optional n)
9173   "Delete the N next (mail) articles.
9174 This command actually deletes articles.  This is not a marking
9175 command.  The article will disappear forever from your life, never to
9176 return.
9177 If N is negative, delete backwards.
9178 If N is nil and articles have been marked with the process mark,
9179 delete these instead."
9180   (interactive "P")
9181   (unless (gnus-check-backend-function 'request-expire-articles
9182                                        gnus-newsgroup-name)
9183     (error "The current newsgroup does not support article deletion"))
9184   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9185     (error "Couldn't open server"))
9186   ;; Compute the list of articles to delete.
9187   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9188         (nnmail-expiry-target 'delete)
9189         not-deleted)
9190     (if (and gnus-novice-user
9191              (not (gnus-yes-or-no-p
9192                    (format "Do you really want to delete %s forever? "
9193                            (if (> (length articles) 1)
9194                                (format "these %s articles" (length articles))
9195                              "this article")))))
9196         ()
9197       ;; Delete the articles.
9198       (setq not-deleted (gnus-request-expire-articles
9199                          articles gnus-newsgroup-name 'force))
9200       (while articles
9201         (gnus-summary-remove-process-mark (car articles))
9202         ;; The backend might not have been able to delete the article
9203         ;; after all.
9204         (unless (memq (car articles) not-deleted)
9205           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9206         (let* ((article (car articles))
9207                (id (mail-header-id (gnus-data-header 
9208                                     (assoc article (gnus-data-list nil))))))
9209           (run-hook-with-args 'gnus-summary-article-delete-hook
9210                               'delete id gnus-newsgroup-name nil
9211                               nil))
9212         (setq articles (cdr articles)))
9213       (when not-deleted
9214         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9215     (gnus-summary-position-point)
9216     (gnus-set-mode-line 'summary)
9217     not-deleted))
9218
9219 (defun gnus-summary-edit-article (&optional arg)
9220   "Edit the current article.
9221 This will have permanent effect only in mail groups.
9222 If ARG is nil, edit the decoded articles.
9223 If ARG is 1, edit the raw articles.
9224 If ARG is 2, edit the raw articles even in read-only groups.
9225 If ARG is 3, edit the articles with the current handles.
9226 Otherwise, allow editing of articles even in read-only
9227 groups."
9228   (interactive "P")
9229   (let (force raw current-handles)
9230     (cond
9231      ((null arg))
9232      ((eq arg 1)
9233       (setq raw t))
9234      ((eq arg 2)
9235       (setq raw t
9236             force t))
9237      ((eq arg 3)
9238       (setq current-handles
9239             (and (gnus-buffer-live-p gnus-article-buffer)
9240                  (with-current-buffer gnus-article-buffer
9241                    (prog1
9242                        gnus-article-mime-handles
9243                      (setq gnus-article-mime-handles nil))))))
9244      (t
9245       (setq force t)))
9246     (when (and raw (not force)
9247                (member gnus-newsgroup-name '("nndraft:delayed"
9248                                              "nndraft:drafts"
9249                                              "nndraft:queue")))
9250       (error "Can't edit the raw article in group %s"
9251              gnus-newsgroup-name))
9252     (save-excursion
9253       (set-buffer gnus-summary-buffer)
9254       (let ((mail-parse-charset gnus-newsgroup-charset)
9255             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9256         (gnus-set-global-variables)
9257         (when (and (not force)
9258                    (gnus-group-read-only-p))
9259           (error "The current newsgroup does not support article editing"))
9260         (gnus-summary-show-article t)
9261         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9262           (with-current-buffer gnus-article-buffer
9263             (mm-enable-multibyte)))
9264         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9265             (setq raw t))
9266         (gnus-article-edit-article
9267          (if raw 'ignore
9268            `(lambda ()
9269               (let ((mbl mml-buffer-list))
9270                 (setq mml-buffer-list nil)
9271                 (mime-to-mml ,'current-handles)
9272                 (let ((mbl1 mml-buffer-list))
9273                   (setq mml-buffer-list mbl)
9274                   (set (make-local-variable 'mml-buffer-list) mbl1))
9275                 (make-local-hook 'kill-buffer-hook)
9276                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9277          `(lambda (no-highlight)
9278             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9279                   (message-options message-options)
9280                   (message-options-set-recipient)
9281                   (mail-parse-ignored-charsets
9282                    ',gnus-newsgroup-ignored-charsets))
9283               ,(if (not raw) '(progn
9284                                 (mml-to-mime)
9285                                 (mml-destroy-buffers)
9286                                 (remove-hook 'kill-buffer-hook
9287                                              'mml-destroy-buffers t)
9288                                 (kill-local-variable 'mml-buffer-list)))
9289               (gnus-summary-edit-article-done
9290                ,(or (mail-header-references gnus-current-headers) "")
9291                ,(gnus-group-read-only-p)
9292                ,gnus-summary-buffer no-highlight))))))))
9293
9294 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9295
9296 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9297                                                  no-highlight)
9298   "Make edits to the current article permanent."
9299   (interactive)
9300   (save-excursion
9301    ;; The buffer restriction contains the entire article if it exists.
9302     (when (article-goto-body)
9303       (let ((lines (count-lines (point) (point-max)))
9304             (length (- (point-max) (point)))
9305             (case-fold-search t)
9306             (body (copy-marker (point))))
9307         (goto-char (point-min))
9308         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9309           (delete-region (match-beginning 1) (match-end 1))
9310           (insert (number-to-string length)))
9311         (goto-char (point-min))
9312         (when (re-search-forward
9313                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9314           (delete-region (match-beginning 1) (match-end 1))
9315           (insert (number-to-string length)))
9316         (goto-char (point-min))
9317         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9318           (delete-region (match-beginning 1) (match-end 1))
9319           (insert (number-to-string lines))))))
9320   ;; Replace the article.
9321   (let ((buf (current-buffer)))
9322     (with-temp-buffer
9323       (insert-buffer-substring buf)
9324
9325       (if (and (not read-only)
9326                (not (gnus-request-replace-article
9327                      (cdr gnus-article-current) (car gnus-article-current)
9328                      (current-buffer) t)))
9329           (error "Couldn't replace article")
9330         ;; Update the summary buffer.
9331         (if (and references
9332                  (equal (message-tokenize-header references " ")
9333                         (message-tokenize-header
9334                          (or (message-fetch-field "references") "") " ")))
9335             ;; We only have to update this line.
9336             (save-excursion
9337               (save-restriction
9338                 (message-narrow-to-head)
9339                 (let ((head (buffer-string))
9340                       header)
9341                   (with-temp-buffer
9342                     (insert (format "211 %d Article retrieved.\n"
9343                                     (cdr gnus-article-current)))
9344                     (insert head)
9345                     (insert ".\n")
9346                     (let ((nntp-server-buffer (current-buffer)))
9347                       (setq header (car (gnus-get-newsgroup-headers
9348                                          nil t))))
9349                     (save-excursion
9350                       (set-buffer gnus-summary-buffer)
9351                       (gnus-data-set-header
9352                        (gnus-data-find (cdr gnus-article-current))
9353                        header)
9354                       (gnus-summary-update-article-line
9355                        (cdr gnus-article-current) header)
9356                       (if (gnus-summary-goto-subject
9357                            (cdr gnus-article-current) nil t)
9358                           (gnus-summary-update-secondary-mark
9359                            (cdr gnus-article-current))))))))
9360           ;; Update threads.
9361           (set-buffer (or buffer gnus-summary-buffer))
9362           (gnus-summary-update-article (cdr gnus-article-current))
9363           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9364               (gnus-summary-update-secondary-mark
9365                (cdr gnus-article-current))))
9366         ;; Prettify the article buffer again.
9367         (unless no-highlight
9368           (save-excursion
9369             (set-buffer gnus-article-buffer)
9370             ;;;!!! Fix this -- article should be rehighlighted.
9371             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9372             (set-buffer gnus-original-article-buffer)
9373             (gnus-request-article
9374              (cdr gnus-article-current)
9375              (car gnus-article-current) (current-buffer))))
9376         ;; Prettify the summary buffer line.
9377         (when (gnus-visual-p 'summary-highlight 'highlight)
9378           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9379
9380 (defun gnus-summary-edit-wash (key)
9381   "Perform editing command KEY in the article buffer."
9382   (interactive
9383    (list
9384     (progn
9385       (message "%s" (concat (this-command-keys) "- "))
9386       (read-char))))
9387   (message "")
9388   (gnus-summary-edit-article)
9389   (execute-kbd-macro (concat (this-command-keys) key))
9390   (gnus-article-edit-done))
9391
9392 ;;; Respooling
9393
9394 (defun gnus-summary-respool-query (&optional silent trace)
9395   "Query where the respool algorithm would put this article."
9396   (interactive)
9397   (let (gnus-mark-article-hook)
9398     (gnus-summary-select-article)
9399     (save-excursion
9400       (set-buffer gnus-original-article-buffer)
9401       (save-restriction
9402         (message-narrow-to-head)
9403         (let ((groups (nnmail-article-group 'identity trace)))
9404           (unless silent
9405             (if groups
9406                 (message "This message would go to %s"
9407                          (mapconcat 'car groups ", "))
9408               (message "This message would go to no groups"))
9409             groups))))))
9410
9411 (defun gnus-summary-respool-trace ()
9412   "Trace where the respool algorithm would put this article.
9413 Display a buffer showing all fancy splitting patterns which matched."
9414   (interactive)
9415   (gnus-summary-respool-query nil t))
9416
9417 ;; Summary marking commands.
9418
9419 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9420   "Mark articles which has the same subject as read, and then select the next.
9421 If UNMARK is positive, remove any kind of mark.
9422 If UNMARK is negative, tick articles."
9423   (interactive "P")
9424   (when unmark
9425     (setq unmark (prefix-numeric-value unmark)))
9426   (let ((count
9427          (gnus-summary-mark-same-subject
9428           (gnus-summary-article-subject) unmark)))
9429     ;; Select next unread article.  If auto-select-same mode, should
9430     ;; select the first unread article.
9431     (gnus-summary-next-article t (and gnus-auto-select-same
9432                                       (gnus-summary-article-subject)))
9433     (gnus-message 7 "%d article%s marked as %s"
9434                   count (if (= count 1) " is" "s are")
9435                   (if unmark "unread" "read"))))
9436
9437 (defun gnus-summary-kill-same-subject (&optional unmark)
9438   "Mark articles which has the same subject as read.
9439 If UNMARK is positive, remove any kind of mark.
9440 If UNMARK is negative, tick articles."
9441   (interactive "P")
9442   (when unmark
9443     (setq unmark (prefix-numeric-value unmark)))
9444   (let ((count
9445          (gnus-summary-mark-same-subject
9446           (gnus-summary-article-subject) unmark)))
9447     ;; If marked as read, go to next unread subject.
9448     (when (null unmark)
9449       ;; Go to next unread subject.
9450       (gnus-summary-next-subject 1 t))
9451     (gnus-message 7 "%d articles are marked as %s"
9452                   count (if unmark "unread" "read"))))
9453
9454 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9455   "Mark articles with same SUBJECT as read, and return marked number.
9456 If optional argument UNMARK is positive, remove any kinds of marks.
9457 If optional argument UNMARK is negative, mark articles as unread instead."
9458   (let ((count 1))
9459     (save-excursion
9460       (cond
9461        ((null unmark)                   ; Mark as read.
9462         (while (and
9463                 (progn
9464                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9465                   (gnus-summary-show-thread) t)
9466                 (gnus-summary-find-subject subject))
9467           (setq count (1+ count))))
9468        ((> unmark 0)                    ; Tick.
9469         (while (and
9470                 (progn
9471                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9472                   (gnus-summary-show-thread) t)
9473                 (gnus-summary-find-subject subject))
9474           (setq count (1+ count))))
9475        (t                               ; Mark as unread.
9476         (while (and
9477                 (progn
9478                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9479                   (gnus-summary-show-thread) t)
9480                 (gnus-summary-find-subject subject))
9481           (setq count (1+ count)))))
9482       (gnus-set-mode-line 'summary)
9483       ;; Return the number of marked articles.
9484       count)))
9485
9486 (defun gnus-summary-mark-as-processable (n &optional unmark)
9487   "Set the process mark on the next N articles.
9488 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9489 the process mark instead.  The difference between N and the actual
9490 number of articles marked is returned."
9491   (interactive "P")
9492   (if (and (null n) (gnus-region-active-p))
9493       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9494     (setq n (prefix-numeric-value n))
9495     (let ((backward (< n 0))
9496           (n (abs n)))
9497       (while (and
9498               (> n 0)
9499               (if unmark
9500                   (gnus-summary-remove-process-mark
9501                    (gnus-summary-article-number))
9502                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9503               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9504         (setq n (1- n)))
9505       (when (/= 0 n)
9506         (gnus-message 7 "No more articles"))
9507       (gnus-summary-recenter)
9508       (gnus-summary-position-point)
9509       n)))
9510
9511 (defun gnus-summary-unmark-as-processable (n)
9512   "Remove the process mark from the next N articles.
9513 If N is negative, unmark backward instead.  The difference between N and
9514 the actual number of articles unmarked is returned."
9515   (interactive "P")
9516   (gnus-summary-mark-as-processable n t))
9517
9518 (defun gnus-summary-unmark-all-processable ()
9519   "Remove the process mark from all articles."
9520   (interactive)
9521   (save-excursion
9522     (while gnus-newsgroup-processable
9523       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9524   (gnus-summary-position-point))
9525
9526 (defun gnus-summary-add-mark (article type)
9527   "Mark ARTICLE with a mark of TYPE."
9528   (let ((vtype (car (assq type gnus-article-mark-lists)))
9529         var)
9530     (if (not vtype)
9531         (error "No such mark type: %s" type)
9532       (setq var (intern (format "gnus-newsgroup-%s" type)))
9533       (set var (cons article (symbol-value var)))
9534       (if (memq type '(processable cached replied forwarded recent saved))
9535           (gnus-summary-update-secondary-mark article)
9536         ;;; !!! This is bogus.  We should find out what primary
9537         ;;; !!! mark we want to set.
9538         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9539
9540 (defun gnus-summary-mark-as-expirable (n)
9541   "Mark N articles forward as expirable.
9542 If N is negative, mark backward instead.  The difference between N and
9543 the actual number of articles marked is returned."
9544   (interactive "p")
9545   (gnus-summary-mark-forward n gnus-expirable-mark))
9546
9547 (defun gnus-summary-mark-as-spam (n)
9548   "Mark N articles forward as spam.
9549 If N is negative, mark backward instead.  The difference between N and
9550 the actual number of articles marked is returned."
9551   (interactive "p")
9552   (gnus-summary-mark-forward n gnus-spam-mark))
9553
9554 (defun gnus-summary-mark-article-as-replied (article)
9555   "Mark ARTICLE as replied to and update the summary line.
9556 ARTICLE can also be a list of articles."
9557   (interactive (list (gnus-summary-article-number)))
9558   (let ((articles (if (listp article) article (list article))))
9559     (dolist (article articles)
9560       (unless (numberp article)
9561         (error "%s is not a number" article))
9562       (push article gnus-newsgroup-replied)
9563       (let ((buffer-read-only nil))
9564         (when (gnus-summary-goto-subject article nil t)
9565           (gnus-summary-update-secondary-mark article))))))
9566
9567 (defun gnus-summary-mark-article-as-forwarded (article)
9568   "Mark ARTICLE as forwarded and update the summary line.
9569 ARTICLE can also be a list of articles."
9570   (let ((articles (if (listp article) article (list article))))
9571     (dolist (article articles)
9572       (push article gnus-newsgroup-forwarded)
9573       (let ((buffer-read-only nil))
9574         (when (gnus-summary-goto-subject article nil t)
9575           (gnus-summary-update-secondary-mark article))))))
9576
9577 (defun gnus-summary-set-bookmark (article)
9578   "Set a bookmark in current article."
9579   (interactive (list (gnus-summary-article-number)))
9580   (when (or (not (get-buffer gnus-article-buffer))
9581             (not gnus-current-article)
9582             (not gnus-article-current)
9583             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9584     (error "No current article selected"))
9585   ;; Remove old bookmark, if one exists.
9586   (gnus-pull article gnus-newsgroup-bookmarks)
9587   ;; Set the new bookmark, which is on the form
9588   ;; (article-number . line-number-in-body).
9589   (push
9590    (cons article
9591          (save-excursion
9592            (set-buffer gnus-article-buffer)
9593            (count-lines
9594             (min (point)
9595                  (save-excursion
9596                    (article-goto-body)
9597                    (point)))
9598             (point))))
9599    gnus-newsgroup-bookmarks)
9600   (gnus-message 6 "A bookmark has been added to the current article."))
9601
9602 (defun gnus-summary-remove-bookmark (article)
9603   "Remove the bookmark from the current article."
9604   (interactive (list (gnus-summary-article-number)))
9605   ;; Remove old bookmark, if one exists.
9606   (if (not (assq article gnus-newsgroup-bookmarks))
9607       (gnus-message 6 "No bookmark in current article.")
9608     (gnus-pull article gnus-newsgroup-bookmarks)
9609     (gnus-message 6 "Removed bookmark.")))
9610
9611 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9612 (defun gnus-summary-mark-as-dormant (n)
9613   "Mark N articles forward as dormant.
9614 If N is negative, mark backward instead.  The difference between N and
9615 the actual number of articles marked is returned."
9616   (interactive "p")
9617   (gnus-summary-mark-forward n gnus-dormant-mark))
9618
9619 (defun gnus-summary-set-process-mark (article)
9620   "Set the process mark on ARTICLE and update the summary line."
9621   (setq gnus-newsgroup-processable
9622         (cons article
9623               (delq article gnus-newsgroup-processable)))
9624   (when (gnus-summary-goto-subject article)
9625     (gnus-summary-show-thread)
9626     (gnus-summary-goto-subject article)
9627     (gnus-summary-update-secondary-mark article)))
9628
9629 (defun gnus-summary-remove-process-mark (article)
9630   "Remove the process mark from ARTICLE and update the summary line."
9631   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9632   (when (gnus-summary-goto-subject article)
9633     (gnus-summary-show-thread)
9634     (gnus-summary-goto-subject article)
9635     (gnus-summary-update-secondary-mark article)))
9636
9637 (defun gnus-summary-set-saved-mark (article)
9638   "Set the process mark on ARTICLE and update the summary line."
9639   (push article gnus-newsgroup-saved)
9640   (when (gnus-summary-goto-subject article)
9641     (gnus-summary-update-secondary-mark article)))
9642
9643 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9644   "Mark N articles as read forwards.
9645 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9646 The difference between N and the actual number of articles marked is
9647 returned.
9648 If NO-EXPIRE, auto-expiry will be inhibited."
9649   (interactive "p")
9650   (gnus-summary-show-thread)
9651   (let ((backward (< n 0))
9652         (gnus-summary-goto-unread
9653          (and gnus-summary-goto-unread
9654               (not (eq gnus-summary-goto-unread 'never))
9655               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9656                                     gnus-ticked-mark gnus-dormant-mark)))))
9657         (n (abs n))
9658         (mark (or mark gnus-del-mark)))
9659     (while (and (> n 0)
9660                 (gnus-summary-mark-article nil mark no-expire)
9661                 (zerop (gnus-summary-next-subject
9662                         (if backward -1 1)
9663                         (and gnus-summary-goto-unread
9664                              (not (eq gnus-summary-goto-unread 'never)))
9665                         t)))
9666       (setq n (1- n)))
9667     (when (/= 0 n)
9668       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9669     (gnus-summary-recenter)
9670     (gnus-summary-position-point)
9671     (gnus-set-mode-line 'summary)
9672     n))
9673
9674 (defun gnus-summary-mark-article-as-read (mark)
9675   "Mark the current article quickly as read with MARK."
9676   (let ((article (gnus-summary-article-number)))
9677     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9678     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9679     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9680     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9681     (push (cons article mark) gnus-newsgroup-reads)
9682     ;; Possibly remove from cache, if that is used.
9683     (when gnus-use-cache
9684       (gnus-cache-enter-remove-article article))
9685     ;; Allow the backend to change the mark.
9686     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9687     ;; Check for auto-expiry.
9688     (when (and gnus-newsgroup-auto-expire
9689                (memq mark gnus-auto-expirable-marks))
9690       (setq mark gnus-expirable-mark)
9691       ;; Let the backend know about the mark change.
9692       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9693       (push article gnus-newsgroup-expirable))
9694     ;; Set the mark in the buffer.
9695     (gnus-summary-update-mark mark 'unread)
9696     t))
9697
9698 (defun gnus-summary-mark-article-as-unread (mark)
9699   "Mark the current article quickly as unread with MARK."
9700   (let* ((article (gnus-summary-article-number))
9701          (old-mark (gnus-summary-article-mark article)))
9702     ;; Allow the backend to change the mark.
9703     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9704     (if (eq mark old-mark)
9705         t
9706       (if (<= article 0)
9707           (progn
9708             (gnus-error 1 "Can't mark negative article numbers")
9709             nil)
9710         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9711         (setq gnus-newsgroup-spam-marked
9712               (delq article gnus-newsgroup-spam-marked))
9713         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9714         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9715         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9716         (cond ((= mark gnus-ticked-mark)
9717                (setq gnus-newsgroup-marked
9718                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9719                                               article)))
9720               ((= mark gnus-spam-mark)
9721                (setq gnus-newsgroup-spam-marked
9722                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9723                                               article)))
9724               ((= mark gnus-dormant-mark)
9725                (setq gnus-newsgroup-dormant
9726                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9727                                               article)))
9728               (t
9729                (setq gnus-newsgroup-unreads
9730                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9731                                               article))))
9732         (gnus-pull article gnus-newsgroup-reads)
9733
9734         ;; See whether the article is to be put in the cache.
9735         (and gnus-use-cache
9736              (vectorp (gnus-summary-article-header article))
9737              (save-excursion
9738                (gnus-cache-possibly-enter-article
9739                 gnus-newsgroup-name article
9740                 (= mark gnus-ticked-mark)
9741                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9742
9743         ;; Fix the mark.
9744         (gnus-summary-update-mark mark 'unread)
9745         t))))
9746
9747 (defun gnus-summary-mark-article (&optional article mark no-expire)
9748   "Mark ARTICLE with MARK.  MARK can be any character.
9749 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9750 `??' (dormant) and `?E' (expirable).
9751 If MARK is nil, then the default character `?r' is used.
9752 If ARTICLE is nil, then the article on the current line will be
9753 marked.
9754 If NO-EXPIRE, auto-expiry will be inhibited."
9755   ;; The mark might be a string.
9756   (when (stringp mark)
9757     (setq mark (aref mark 0)))
9758   ;; If no mark is given, then we check auto-expiring.
9759   (when (null mark)
9760     (setq mark gnus-del-mark))
9761   (when (and (not no-expire)
9762              gnus-newsgroup-auto-expire
9763              (memq mark gnus-auto-expirable-marks))
9764     (setq mark gnus-expirable-mark))
9765   (let ((article (or article (gnus-summary-article-number)))
9766         (old-mark (gnus-summary-article-mark article)))
9767     ;; Allow the backend to change the mark.
9768     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9769     (if (eq mark old-mark)
9770         t
9771       (unless article
9772         (error "No article on current line"))
9773       (if (not (if (or (= mark gnus-unread-mark)
9774                        (= mark gnus-ticked-mark)
9775                        (= mark gnus-spam-mark)
9776                        (= mark gnus-dormant-mark))
9777                    (gnus-mark-article-as-unread article mark)
9778                  (gnus-mark-article-as-read article mark)))
9779           t
9780         ;; See whether the article is to be put in the cache.
9781         (and gnus-use-cache
9782              (not (= mark gnus-canceled-mark))
9783              (vectorp (gnus-summary-article-header article))
9784              (save-excursion
9785                (gnus-cache-possibly-enter-article
9786                 gnus-newsgroup-name article
9787                 (= mark gnus-ticked-mark)
9788                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9789
9790         (when (gnus-summary-goto-subject article nil t)
9791           (let ((buffer-read-only nil))
9792             (gnus-summary-show-thread)
9793             ;; Fix the mark.
9794             (gnus-summary-update-mark mark 'unread)
9795             t))))))
9796
9797 (defun gnus-summary-update-secondary-mark (article)
9798   "Update the secondary (read, process, cache) mark."
9799   (gnus-summary-update-mark
9800    (cond ((memq article gnus-newsgroup-processable)
9801           gnus-process-mark)
9802          ((memq article gnus-newsgroup-cached)
9803           gnus-cached-mark)
9804          ((memq article gnus-newsgroup-replied)
9805           gnus-replied-mark)
9806          ((memq article gnus-newsgroup-forwarded)
9807           gnus-forwarded-mark)
9808          ((memq article gnus-newsgroup-saved)
9809           gnus-saved-mark)
9810          ((memq article gnus-newsgroup-recent)
9811           gnus-recent-mark)
9812          ((memq article gnus-newsgroup-unseen)
9813           gnus-unseen-mark)
9814          (t gnus-no-mark))
9815    'replied)
9816   (when (gnus-visual-p 'summary-highlight 'highlight)
9817     (gnus-run-hooks 'gnus-summary-update-hook))
9818   t)
9819
9820 (defun gnus-summary-update-download-mark (article)
9821   "Update the download mark."
9822   (gnus-summary-update-mark
9823    (cond ((memq article gnus-newsgroup-undownloaded) 
9824           gnus-undownloaded-mark)
9825          (gnus-newsgroup-agentized
9826           gnus-downloaded-mark)
9827          (t
9828           gnus-no-mark))
9829    'download)
9830   (gnus-summary-update-line t)
9831   t)
9832
9833 (defun gnus-summary-update-mark (mark type)
9834   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9835         (buffer-read-only nil))
9836     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9837     (when forward
9838       (when (looking-at "\r")
9839         (incf forward))
9840       (when (<= (+ forward (point)) (point-max))
9841         ;; Go to the right position on the line.
9842         (goto-char (+ forward (point)))
9843         ;; Replace the old mark with the new mark.
9844         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9845         ;; Optionally update the marks by some user rule.
9846         (when (eq type 'unread)
9847           (gnus-data-set-mark
9848            (gnus-data-find (gnus-summary-article-number)) mark)
9849           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9850
9851 (defun gnus-mark-article-as-read (article &optional mark)
9852   "Enter ARTICLE in the pertinent lists and remove it from others."
9853   ;; Make the article expirable.
9854   (let ((mark (or mark gnus-del-mark)))
9855     (setq gnus-newsgroup-expirable
9856           (if (= mark gnus-expirable-mark)
9857               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9858             (delq article gnus-newsgroup-expirable)))
9859     ;; Remove from unread and marked lists.
9860     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9861     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9862     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9863     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9864     (push (cons article mark) gnus-newsgroup-reads)
9865     ;; Possibly remove from cache, if that is used.
9866     (when gnus-use-cache
9867       (gnus-cache-enter-remove-article article))
9868     t))
9869
9870 (defun gnus-mark-article-as-unread (article &optional mark)
9871   "Enter ARTICLE in the pertinent lists and remove it from others."
9872   (let ((mark (or mark gnus-ticked-mark)))
9873     (if (<= article 0)
9874         (progn
9875           (gnus-error 1 "Can't mark negative article numbers")
9876           nil)
9877       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9878             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9879             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9880             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9881             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9882
9883       ;; Unsuppress duplicates?
9884       (when gnus-suppress-duplicates
9885         (gnus-dup-unsuppress-article article))
9886
9887       (cond ((= mark gnus-ticked-mark)
9888              (setq gnus-newsgroup-marked
9889                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9890             ((= mark gnus-spam-mark)
9891              (setq gnus-newsgroup-spam-marked
9892                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9893                                             article)))
9894             ((= mark gnus-dormant-mark)
9895              (setq gnus-newsgroup-dormant
9896                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9897             (t
9898              (setq gnus-newsgroup-unreads
9899                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9900       (gnus-pull article gnus-newsgroup-reads)
9901       t)))
9902
9903 (defalias 'gnus-summary-mark-as-unread-forward
9904   'gnus-summary-tick-article-forward)
9905 (make-obsolete 'gnus-summary-mark-as-unread-forward
9906                'gnus-summary-tick-article-forward)
9907 (defun gnus-summary-tick-article-forward (n)
9908   "Tick N articles forwards.
9909 If N is negative, tick backwards instead.
9910 The difference between N and the number of articles ticked is returned."
9911   (interactive "p")
9912   (gnus-summary-mark-forward n gnus-ticked-mark))
9913
9914 (defalias 'gnus-summary-mark-as-unread-backward
9915   'gnus-summary-tick-article-backward)
9916 (make-obsolete 'gnus-summary-mark-as-unread-backward
9917                'gnus-summary-tick-article-backward)
9918 (defun gnus-summary-tick-article-backward (n)
9919   "Tick N articles backwards.
9920 The difference between N and the number of articles ticked is returned."
9921   (interactive "p")
9922   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9923
9924 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9925 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9926 (defun gnus-summary-tick-article (&optional article clear-mark)
9927   "Mark current article as unread.
9928 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9929 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9930   (interactive)
9931   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9932                                        gnus-ticked-mark)))
9933
9934 (defun gnus-summary-mark-as-read-forward (n)
9935   "Mark N articles as read forwards.
9936 If N is negative, mark backwards instead.
9937 The difference between N and the actual number of articles marked is
9938 returned."
9939   (interactive "p")
9940   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9941
9942 (defun gnus-summary-mark-as-read-backward (n)
9943   "Mark the N articles as read backwards.
9944 The difference between N and the actual number of articles marked is
9945 returned."
9946   (interactive "p")
9947   (gnus-summary-mark-forward
9948    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9949
9950 (defun gnus-summary-mark-as-read (&optional article mark)
9951   "Mark current article as read.
9952 ARTICLE specifies the article to be marked as read.
9953 MARK specifies a string to be inserted at the beginning of the line."
9954   (gnus-summary-mark-article article mark))
9955
9956 (defun gnus-summary-clear-mark-forward (n)
9957   "Clear marks from N articles forward.
9958 If N is negative, clear backward instead.
9959 The difference between N and the number of marks cleared is returned."
9960   (interactive "p")
9961   (gnus-summary-mark-forward n gnus-unread-mark))
9962
9963 (defun gnus-summary-clear-mark-backward (n)
9964   "Clear marks from N articles backward.
9965 The difference between N and the number of marks cleared is returned."
9966   (interactive "p")
9967   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9968
9969 (defun gnus-summary-mark-unread-as-read ()
9970   "Intended to be used by `gnus-summary-mark-article-hook'."
9971   (when (memq gnus-current-article gnus-newsgroup-unreads)
9972     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9973
9974 (defun gnus-summary-mark-read-and-unread-as-read ()
9975   "Intended to be used by `gnus-summary-mark-article-hook'."
9976   (let ((mark (gnus-summary-article-mark)))
9977     (when (or (gnus-unread-mark-p mark)
9978               (gnus-read-mark-p mark))
9979       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9980
9981 (defun gnus-summary-mark-unread-as-ticked ()
9982   "Intended to be used by `gnus-summary-mark-article-hook'."
9983   (when (memq gnus-current-article gnus-newsgroup-unreads)
9984     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9985
9986 (defun gnus-summary-mark-region-as-read (point mark all)
9987   "Mark all unread articles between point and mark as read.
9988 If given a prefix, mark all articles between point and mark as read,
9989 even ticked and dormant ones."
9990   (interactive "r\nP")
9991   (save-excursion
9992     (let (article)
9993       (goto-char point)
9994       (beginning-of-line)
9995       (while (and
9996               (< (point) mark)
9997               (progn
9998                 (when (or all
9999                           (memq (setq article (gnus-summary-article-number))
10000                                 gnus-newsgroup-unreads))
10001                   (gnus-summary-mark-article article gnus-del-mark))
10002                 t)
10003               (gnus-summary-find-next))))))
10004
10005 (defun gnus-summary-mark-below (score mark)
10006   "Mark articles with score less than SCORE with MARK."
10007   (interactive "P\ncMark: ")
10008   (setq score (if score
10009                   (prefix-numeric-value score)
10010                 (or gnus-summary-default-score 0)))
10011   (save-excursion
10012     (set-buffer gnus-summary-buffer)
10013     (goto-char (point-min))
10014     (while
10015         (progn
10016           (and (< (gnus-summary-article-score) score)
10017                (gnus-summary-mark-article nil mark))
10018           (gnus-summary-find-next)))))
10019
10020 (defun gnus-summary-kill-below (&optional score)
10021   "Mark articles with score below SCORE as read."
10022   (interactive "P")
10023   (gnus-summary-mark-below score gnus-killed-mark))
10024
10025 (defun gnus-summary-clear-above (&optional score)
10026   "Clear all marks from articles with score above SCORE."
10027   (interactive "P")
10028   (gnus-summary-mark-above score gnus-unread-mark))
10029
10030 (defun gnus-summary-tick-above (&optional score)
10031   "Tick all articles with score above SCORE."
10032   (interactive "P")
10033   (gnus-summary-mark-above score gnus-ticked-mark))
10034
10035 (defun gnus-summary-mark-above (score mark)
10036   "Mark articles with score over SCORE with MARK."
10037   (interactive "P\ncMark: ")
10038   (setq score (if score
10039                   (prefix-numeric-value score)
10040                 (or gnus-summary-default-score 0)))
10041   (save-excursion
10042     (set-buffer gnus-summary-buffer)
10043     (goto-char (point-min))
10044     (while (and (progn
10045                   (when (> (gnus-summary-article-score) score)
10046                     (gnus-summary-mark-article nil mark))
10047                   t)
10048                 (gnus-summary-find-next)))))
10049
10050 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10051 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10052 (defun gnus-summary-limit-include-expunged (&optional no-error)
10053   "Display all the hidden articles that were expunged for low scores."
10054   (interactive)
10055   (let ((buffer-read-only nil))
10056     (let ((scored gnus-newsgroup-scored)
10057           headers h)
10058       (while scored
10059         (unless (gnus-summary-article-header (caar scored))
10060           (and (setq h (gnus-number-to-header (caar scored)))
10061                (< (cdar scored) gnus-summary-expunge-below)
10062                (push h headers)))
10063         (setq scored (cdr scored)))
10064       (if (not headers)
10065           (when (not no-error)
10066             (error "No expunged articles hidden"))
10067         (goto-char (point-min))
10068         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10069         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10070         (mapcar (lambda (x) (push (mail-header-number x)
10071                                   gnus-newsgroup-limit))
10072                 headers)
10073         (gnus-summary-prepare-unthreaded (nreverse headers))
10074         (goto-char (point-min))
10075         (gnus-summary-position-point)
10076         t))))
10077
10078 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10079   "Mark all unread articles in this newsgroup as read.
10080 If prefix argument ALL is non-nil, ticked and dormant articles will
10081 also be marked as read.
10082 If QUIETLY is non-nil, no questions will be asked.
10083 If TO-HERE is non-nil, it should be a point in the buffer.  All
10084 articles before (after, if REVERSE is set) this point will be marked as read.
10085 Note that this function will only catch up the unread article
10086 in the current summary buffer limitation.
10087 The number of articles marked as read is returned."
10088   (interactive "P")
10089   (prog1
10090       (save-excursion
10091         (when (or quietly
10092                   (not gnus-interactive-catchup) ;Without confirmation?
10093                   gnus-expert-user
10094                   (gnus-y-or-n-p
10095                    (if all
10096                        "Mark absolutely all articles as read? "
10097                      "Mark all unread articles as read? ")))
10098           (if (and not-mark
10099                    (not gnus-newsgroup-adaptive)
10100                    (not gnus-newsgroup-auto-expire)
10101                    (not gnus-suppress-duplicates)
10102                    (or (not gnus-use-cache)
10103                        (eq gnus-use-cache 'passive)))
10104               (progn
10105                 (when all
10106                   (setq gnus-newsgroup-marked nil
10107                         gnus-newsgroup-spam-marked nil
10108                         gnus-newsgroup-dormant nil))
10109                 (setq gnus-newsgroup-unreads
10110                       (gnus-sorted-nunion
10111                        (gnus-intersection gnus-newsgroup-unreads
10112                                           gnus-newsgroup-downloadable)
10113                        gnus-newsgroup-unfetched)))
10114             ;; We actually mark all articles as canceled, which we
10115             ;; have to do when using auto-expiry or adaptive scoring.
10116             (gnus-summary-show-all-threads)
10117             (if (and to-here reverse)
10118                 (progn
10119                   (goto-char to-here)
10120                   (while (and
10121                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10122                           (gnus-summary-find-next (not all)))))
10123               (when (gnus-summary-first-subject (not all))
10124                 (while (and
10125                         (if to-here (< (point) to-here) t)
10126                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10127                         (gnus-summary-find-next (not all))))))
10128             (gnus-set-mode-line 'summary))
10129           t))
10130     (gnus-summary-position-point)))
10131
10132 (defun gnus-summary-catchup-to-here (&optional all)
10133   "Mark all unticked articles before the current one as read.
10134 If ALL is non-nil, also mark ticked and dormant articles as read."
10135   (interactive "P")
10136   (save-excursion
10137     (gnus-save-hidden-threads
10138       (let ((beg (point)))
10139         ;; We check that there are unread articles.
10140         (when (or all (gnus-summary-find-prev))
10141           (gnus-summary-catchup all t beg)))))
10142   (gnus-summary-position-point))
10143
10144 (defun gnus-summary-catchup-from-here (&optional all)
10145   "Mark all unticked articles after the current one as read.
10146 If ALL is non-nil, also mark ticked and dormant articles as read."
10147   (interactive "P")
10148   (save-excursion
10149     (gnus-save-hidden-threads
10150       (let ((beg (point)))
10151         ;; We check that there are unread articles.
10152         (when (or all (gnus-summary-find-next))
10153           (gnus-summary-catchup all t beg nil t)))))
10154
10155   (gnus-summary-position-point))
10156 (defun gnus-summary-catchup-all (&optional quietly)
10157   "Mark all articles in this newsgroup as read.
10158 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10159 instead, which marks only unread articles as read."
10160   (interactive "P")
10161   (gnus-summary-catchup t quietly))
10162
10163 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10164   "Mark all unread articles in this group as read, then exit.
10165 If prefix argument ALL is non-nil, all articles are marked as read.
10166 If QUIETLY is non-nil, no questions will be asked."
10167   (interactive "P")
10168   (when (gnus-summary-catchup all quietly nil 'fast)
10169     ;; Select next newsgroup or exit.
10170     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10171              (eq gnus-auto-select-next 'quietly))
10172         (gnus-summary-next-group nil)
10173       (gnus-summary-exit))))
10174
10175 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10176   "Mark all articles in this newsgroup as read, and then exit.
10177 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10178 instead, which marks only unread articles as read."
10179   (interactive "P")
10180   (gnus-summary-catchup-and-exit t quietly))
10181
10182 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10183   "Mark all articles in this group as read and select the next group.
10184 If given a prefix, mark all articles, unread as well as ticked, as
10185 read."
10186   (interactive "P")
10187   (save-excursion
10188     (gnus-summary-catchup all))
10189   (gnus-summary-next-group))
10190
10191 ;;;
10192 ;;; with article
10193 ;;;
10194
10195 (defmacro gnus-with-article (article &rest forms)
10196   "Select ARTICLE and perform FORMS in the original article buffer.
10197 Then replace the article with the result."
10198   `(progn
10199      ;; We don't want the article to be marked as read.
10200      (let (gnus-mark-article-hook)
10201        (gnus-summary-select-article t t nil ,article))
10202      (set-buffer gnus-original-article-buffer)
10203      ,@forms
10204      (if (not (gnus-check-backend-function
10205                'request-replace-article (car gnus-article-current)))
10206          (gnus-message 5 "Read-only group; not replacing")
10207        (unless (gnus-request-replace-article
10208                 ,article (car gnus-article-current)
10209                 (current-buffer) t)
10210          (error "Couldn't replace article")))
10211      ;; The cache and backlog have to be flushed somewhat.
10212      (when gnus-keep-backlog
10213        (gnus-backlog-remove-article
10214         (car gnus-article-current) (cdr gnus-article-current)))
10215      (when gnus-use-cache
10216        (gnus-cache-update-article
10217         (car gnus-article-current) (cdr gnus-article-current)))))
10218
10219 (put 'gnus-with-article 'lisp-indent-function 1)
10220 (put 'gnus-with-article 'edebug-form-spec '(form body))
10221
10222 ;; Thread-based commands.
10223
10224 (defun gnus-summary-articles-in-thread (&optional article)
10225   "Return a list of all articles in the current thread.
10226 If ARTICLE is non-nil, return all articles in the thread that starts
10227 with that article."
10228   (let* ((article (or article (gnus-summary-article-number)))
10229          (data (gnus-data-find-list article))
10230          (top-level (gnus-data-level (car data)))
10231          (top-subject
10232           (cond ((null gnus-thread-operation-ignore-subject)
10233                  (gnus-simplify-subject-re
10234                   (mail-header-subject (gnus-data-header (car data)))))
10235                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10236                  (gnus-simplify-subject-fuzzy
10237                   (mail-header-subject (gnus-data-header (car data)))))
10238                 (t nil)))
10239          (end-point (save-excursion
10240                       (if (gnus-summary-go-to-next-thread)
10241                           (point) (point-max))))
10242          articles)
10243     (while (and data
10244                 (< (gnus-data-pos (car data)) end-point))
10245       (when (or (not top-subject)
10246                 (string= top-subject
10247                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10248                              (gnus-simplify-subject-fuzzy
10249                               (mail-header-subject
10250                                (gnus-data-header (car data))))
10251                            (gnus-simplify-subject-re
10252                             (mail-header-subject
10253                              (gnus-data-header (car data)))))))
10254         (push (gnus-data-number (car data)) articles))
10255       (unless (and (setq data (cdr data))
10256                    (> (gnus-data-level (car data)) top-level))
10257         (setq data nil)))
10258     ;; Return the list of articles.
10259     (nreverse articles)))
10260
10261 (defun gnus-summary-rethread-current ()
10262   "Rethread the thread the current article is part of."
10263   (interactive)
10264   (let* ((gnus-show-threads t)
10265          (article (gnus-summary-article-number))
10266          (id (mail-header-id (gnus-summary-article-header)))
10267          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10268     (unless id
10269       (error "No article on the current line"))
10270     (gnus-rebuild-thread id)
10271     (gnus-summary-goto-subject article)))
10272
10273 (defun gnus-summary-reparent-thread ()
10274   "Make the current article child of the marked (or previous) article.
10275
10276 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10277 is non-nil or the Subject: of both articles are the same."
10278   (interactive)
10279   (unless (not (gnus-group-read-only-p))
10280     (error "The current newsgroup does not support article editing"))
10281   (unless (<= (length gnus-newsgroup-processable) 1)
10282     (error "No more than one article may be marked"))
10283   (save-window-excursion
10284     (let ((gnus-article-buffer " *reparent*")
10285           (current-article (gnus-summary-article-number))
10286           ;; First grab the marked article, otherwise one line up.
10287           (parent-article (if (not (null gnus-newsgroup-processable))
10288                               (car gnus-newsgroup-processable)
10289                             (save-excursion
10290                               (if (eq (forward-line -1) 0)
10291                                   (gnus-summary-article-number)
10292                                 (error "Beginning of summary buffer"))))))
10293       (unless (not (eq current-article parent-article))
10294         (error "An article may not be self-referential"))
10295       (let ((message-id (mail-header-id
10296                          (gnus-summary-article-header parent-article))))
10297         (unless (and message-id (not (equal message-id "")))
10298           (error "No message-id in desired parent"))
10299         (gnus-with-article current-article
10300           (save-restriction
10301             (goto-char (point-min))
10302             (message-narrow-to-head)
10303             (if (re-search-forward "^References: " nil t)
10304                 (progn
10305                   (re-search-forward "^[^ \t]" nil t)
10306                   (forward-line -1)
10307                   (end-of-line)
10308                   (insert " " message-id))
10309               (insert "References: " message-id "\n"))))
10310         (set-buffer gnus-summary-buffer)
10311         (gnus-summary-unmark-all-processable)
10312         (gnus-summary-update-article current-article)
10313         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10314             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10315         (gnus-summary-rethread-current)
10316         (gnus-message 3 "Article %d is now the child of article %d"
10317                       current-article parent-article)))))
10318
10319 (defun gnus-summary-toggle-threads (&optional arg)
10320   "Toggle showing conversation threads.
10321 If ARG is positive number, turn showing conversation threads on."
10322   (interactive "P")
10323   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10324     (setq gnus-show-threads
10325           (if (null arg) (not gnus-show-threads)
10326             (> (prefix-numeric-value arg) 0)))
10327     (gnus-summary-prepare)
10328     (gnus-summary-goto-subject current)
10329     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10330     (gnus-summary-position-point)))
10331
10332 (defun gnus-summary-show-all-threads ()
10333   "Show all threads."
10334   (interactive)
10335   (save-excursion
10336     (let ((buffer-read-only nil))
10337       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10338   (gnus-summary-position-point))
10339
10340 (defun gnus-summary-show-thread ()
10341   "Show thread subtrees.
10342 Returns nil if no thread was there to be shown."
10343   (interactive)
10344   (let ((buffer-read-only nil)
10345         (orig (point))
10346         (end (gnus-point-at-eol))
10347         ;; Leave point at bol
10348         (beg (progn (beginning-of-line) (point))))
10349     (prog1
10350         ;; Any hidden lines here?
10351         (search-forward "\r" end t)
10352       (subst-char-in-region beg end ?\^M ?\n t)
10353       (goto-char orig)
10354       (gnus-summary-position-point))))
10355
10356 (defun gnus-summary-maybe-hide-threads ()
10357   "If requested, hide the threads that should be hidden."
10358   (when (and gnus-show-threads
10359              gnus-thread-hide-subtree)
10360     (gnus-summary-hide-all-threads
10361      (if (or (consp gnus-thread-hide-subtree)
10362              (gnus-functionp gnus-thread-hide-subtree))
10363          (gnus-make-predicate gnus-thread-hide-subtree)
10364        nil))))
10365
10366 ;;; Hiding predicates.
10367
10368 (defun gnus-article-unread-p (header)
10369   (memq (mail-header-number header) gnus-newsgroup-unreads))
10370
10371 (defun gnus-article-unseen-p (header)
10372   (memq (mail-header-number header) gnus-newsgroup-unseen))
10373
10374 (defun gnus-map-articles (predicate articles)
10375   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10376   (apply 'gnus-or (mapcar predicate
10377                           (mapcar 'gnus-summary-article-header articles))))
10378
10379 (defun gnus-summary-hide-all-threads (&optional predicate)
10380   "Hide all thread subtrees.
10381 If PREDICATE is supplied, threads that satisfy this predicate
10382 will not be hidden."
10383   (interactive)
10384   (save-excursion
10385     (goto-char (point-min))
10386     (let ((end nil))
10387       (while (not end)
10388         (when (or (not predicate)
10389                   (gnus-map-articles
10390                    predicate (gnus-summary-article-children)))
10391             (gnus-summary-hide-thread))
10392         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10393   (gnus-summary-position-point))
10394
10395 (defun gnus-summary-hide-thread ()
10396   "Hide thread subtrees.
10397 If PREDICATE is supplied, threads that satisfy this predicate
10398 will not be hidden.
10399 Returns nil if no threads were there to be hidden."
10400   (interactive)
10401   (let ((buffer-read-only nil)
10402         (start (point))
10403         (article (gnus-summary-article-number)))
10404     (goto-char start)
10405     ;; Go forward until either the buffer ends or the subthread
10406     ;; ends.
10407     (when (and (not (eobp))
10408                (or (zerop (gnus-summary-next-thread 1 t))
10409                    (goto-char (point-max))))
10410       (prog1
10411           (if (and (> (point) start)
10412                    (search-backward "\n" start t))
10413               (progn
10414                 (subst-char-in-region start (point) ?\n ?\^M)
10415                 (gnus-summary-goto-subject article))
10416             (goto-char start)
10417             nil)))))
10418
10419 (defun gnus-summary-go-to-next-thread (&optional previous)
10420   "Go to the same level (or less) next thread.
10421 If PREVIOUS is non-nil, go to previous thread instead.
10422 Return the article number moved to, or nil if moving was impossible."
10423   (let ((level (gnus-summary-thread-level))
10424         (way (if previous -1 1))
10425         (beg (point)))
10426     (forward-line way)
10427     (while (and (not (eobp))
10428                 (< level (gnus-summary-thread-level)))
10429       (forward-line way))
10430     (if (eobp)
10431         (progn
10432           (goto-char beg)
10433           nil)
10434       (setq beg (point))
10435       (prog1
10436           (gnus-summary-article-number)
10437         (goto-char beg)))))
10438
10439 (defun gnus-summary-next-thread (n &optional silent)
10440   "Go to the same level next N'th thread.
10441 If N is negative, search backward instead.
10442 Returns the difference between N and the number of skips actually
10443 done.
10444
10445 If SILENT, don't output messages."
10446   (interactive "p")
10447   (let ((backward (< n 0))
10448         (n (abs n)))
10449     (while (and (> n 0)
10450                 (gnus-summary-go-to-next-thread backward))
10451       (decf n))
10452     (unless silent
10453       (gnus-summary-position-point))
10454     (when (and (not silent) (/= 0 n))
10455       (gnus-message 7 "No more threads"))
10456     n))
10457
10458 (defun gnus-summary-prev-thread (n)
10459   "Go to the same level previous N'th thread.
10460 Returns the difference between N and the number of skips actually
10461 done."
10462   (interactive "p")
10463   (gnus-summary-next-thread (- n)))
10464
10465 (defun gnus-summary-go-down-thread ()
10466   "Go down one level in the current thread."
10467   (let ((children (gnus-summary-article-children)))
10468     (when children
10469       (gnus-summary-goto-subject (car children)))))
10470
10471 (defun gnus-summary-go-up-thread ()
10472   "Go up one level in the current thread."
10473   (let ((parent (gnus-summary-article-parent)))
10474     (when parent
10475       (gnus-summary-goto-subject parent))))
10476
10477 (defun gnus-summary-down-thread (n)
10478   "Go down thread N steps.
10479 If N is negative, go up instead.
10480 Returns the difference between N and how many steps down that were
10481 taken."
10482   (interactive "p")
10483   (let ((up (< n 0))
10484         (n (abs n)))
10485     (while (and (> n 0)
10486                 (if up (gnus-summary-go-up-thread)
10487                   (gnus-summary-go-down-thread)))
10488       (setq n (1- n)))
10489     (gnus-summary-position-point)
10490     (when (/= 0 n)
10491       (gnus-message 7 "Can't go further"))
10492     n))
10493
10494 (defun gnus-summary-up-thread (n)
10495   "Go up thread N steps.
10496 If N is negative, go down instead.
10497 Returns the difference between N and how many steps down that were
10498 taken."
10499   (interactive "p")
10500   (gnus-summary-down-thread (- n)))
10501
10502 (defun gnus-summary-top-thread ()
10503   "Go to the top of the thread."
10504   (interactive)
10505   (while (gnus-summary-go-up-thread))
10506   (gnus-summary-article-number))
10507
10508 (defun gnus-summary-kill-thread (&optional unmark)
10509   "Mark articles under current thread as read.
10510 If the prefix argument is positive, remove any kinds of marks.
10511 If the prefix argument is negative, tick articles instead."
10512   (interactive "P")
10513   (when unmark
10514     (setq unmark (prefix-numeric-value unmark)))
10515   (let ((articles (gnus-summary-articles-in-thread)))
10516     (save-excursion
10517       ;; Expand the thread.
10518       (gnus-summary-show-thread)
10519       ;; Mark all the articles.
10520       (while articles
10521         (gnus-summary-goto-subject (car articles))
10522         (cond ((null unmark)
10523                (gnus-summary-mark-article-as-read gnus-killed-mark))
10524               ((> unmark 0)
10525                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10526               (t
10527                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10528         (setq articles (cdr articles))))
10529     ;; Hide killed subtrees.
10530     (and (null unmark)
10531          gnus-thread-hide-killed
10532          (gnus-summary-hide-thread))
10533     ;; If marked as read, go to next unread subject.
10534     (when (null unmark)
10535       ;; Go to next unread subject.
10536       (gnus-summary-next-subject 1 t)))
10537   (gnus-set-mode-line 'summary))
10538
10539 ;; Summary sorting commands
10540
10541 (defun gnus-summary-sort-by-number (&optional reverse)
10542   "Sort the summary buffer by article number.
10543 Argument REVERSE means reverse order."
10544   (interactive "P")
10545   (gnus-summary-sort 'number reverse))
10546
10547 (defun gnus-summary-sort-by-random (&optional reverse)
10548   "Randomize the order in the summary buffer.
10549 Argument REVERSE means to randomize in reverse order."
10550   (interactive "P")
10551   (gnus-summary-sort 'random reverse))
10552
10553 (defun gnus-summary-sort-by-author (&optional reverse)
10554   "Sort the summary buffer by author name alphabetically.
10555 If `case-fold-search' is non-nil, case of letters is ignored.
10556 Argument REVERSE means reverse order."
10557   (interactive "P")
10558   (gnus-summary-sort 'author reverse))
10559
10560 (defun gnus-summary-sort-by-subject (&optional reverse)
10561   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10562 If `case-fold-search' is non-nil, case of letters is ignored.
10563 Argument REVERSE means reverse order."
10564   (interactive "P")
10565   (gnus-summary-sort 'subject reverse))
10566
10567 (defun gnus-summary-sort-by-date (&optional reverse)
10568   "Sort the summary buffer by date.
10569 Argument REVERSE means reverse order."
10570   (interactive "P")
10571   (gnus-summary-sort 'date reverse))
10572
10573 (defun gnus-summary-sort-by-score (&optional reverse)
10574   "Sort the summary buffer by score.
10575 Argument REVERSE means reverse order."
10576   (interactive "P")
10577   (gnus-summary-sort 'score reverse))
10578
10579 (defun gnus-summary-sort-by-lines (&optional reverse)
10580   "Sort the summary buffer by the number of lines.
10581 Argument REVERSE means reverse order."
10582   (interactive "P")
10583   (gnus-summary-sort 'lines reverse))
10584
10585 (defun gnus-summary-sort-by-chars (&optional reverse)
10586   "Sort the summary buffer by article length.
10587 Argument REVERSE means reverse order."
10588   (interactive "P")
10589   (gnus-summary-sort 'chars reverse))
10590
10591 (defun gnus-summary-sort-by-original (&optional reverse)
10592   "Sort the summary buffer using the default sorting method.
10593 Argument REVERSE means reverse order."
10594   (interactive "P")
10595   (let* ((buffer-read-only)
10596          (gnus-summary-prepare-hook nil))
10597     ;; We do the sorting by regenerating the threads.
10598     (gnus-summary-prepare)
10599     ;; Hide subthreads if needed.
10600     (gnus-summary-maybe-hide-threads)))
10601
10602 (defun gnus-summary-sort (predicate reverse)
10603   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10604   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10605          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10606          (gnus-thread-sort-functions
10607           (if (not reverse)
10608               thread
10609             `(lambda (t1 t2)
10610                (,thread t2 t1))))
10611          (gnus-sort-gathered-threads-function
10612           gnus-thread-sort-functions)
10613          (gnus-article-sort-functions
10614           (if (not reverse)
10615               article
10616             `(lambda (t1 t2)
10617                (,article t2 t1))))
10618          (buffer-read-only)
10619          (gnus-summary-prepare-hook nil))
10620     ;; We do the sorting by regenerating the threads.
10621     (gnus-summary-prepare)
10622     ;; Hide subthreads if needed.
10623     (gnus-summary-maybe-hide-threads)))
10624
10625 ;; Summary saving commands.
10626
10627 (defun gnus-summary-save-article (&optional n not-saved)
10628   "Save the current article using the default saver function.
10629 If N is a positive number, save the N next articles.
10630 If N is a negative number, save the N previous articles.
10631 If N is nil and any articles have been marked with the process mark,
10632 save those articles instead.
10633 The variable `gnus-default-article-saver' specifies the saver function."
10634   (interactive "P")
10635   (let* ((articles (gnus-summary-work-articles n))
10636          (save-buffer (save-excursion
10637                         (nnheader-set-temp-buffer " *Gnus Save*")))
10638          (num (length articles))
10639          header file)
10640     (dolist (article articles)
10641       (setq header (gnus-summary-article-header article))
10642       (if (not (vectorp header))
10643           ;; This is a pseudo-article.
10644           (if (assq 'name header)
10645               (gnus-copy-file (cdr (assq 'name header)))
10646             (gnus-message 1 "Article %d is unsaveable" article))
10647         ;; This is a real article.
10648         (save-window-excursion
10649           (let ((gnus-display-mime-function nil)
10650                 (gnus-article-prepare-hook nil))
10651             (gnus-summary-select-article t nil nil article)))
10652         (save-excursion
10653           (set-buffer save-buffer)
10654           (erase-buffer)
10655           (insert-buffer-substring gnus-original-article-buffer))
10656         (setq file (gnus-article-save save-buffer file num))
10657         (gnus-summary-remove-process-mark article)
10658         (unless not-saved
10659           (gnus-summary-set-saved-mark article))))
10660     (gnus-kill-buffer save-buffer)
10661     (gnus-summary-position-point)
10662     (gnus-set-mode-line 'summary)
10663     n))
10664
10665 (defun gnus-summary-pipe-output (&optional arg headers)
10666   "Pipe the current article to a subprocess.
10667 If N is a positive number, pipe the N next articles.
10668 If N is a negative number, pipe the N previous articles.
10669 If N is nil and any articles have been marked with the process mark,
10670 pipe those articles instead.
10671 If HEADERS (the symbolic prefix), include the headers, too."
10672   (interactive (gnus-interactive "P\ny"))
10673   (require 'gnus-art)
10674   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10675         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10676     (gnus-summary-save-article arg t))
10677   (let ((buffer (get-buffer "*Shell Command Output*")))
10678     (when (and buffer
10679                (not (zerop (buffer-size buffer))))
10680       (gnus-configure-windows 'pipe))))
10681
10682 (defun gnus-summary-save-article-mail (&optional arg)
10683   "Append the current article to an mail file.
10684 If N is a positive number, save the N next articles.
10685 If N is a negative number, save the N previous articles.
10686 If N is nil and any articles have been marked with the process mark,
10687 save those articles instead."
10688   (interactive "P")
10689   (require 'gnus-art)
10690   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10691     (gnus-summary-save-article arg)))
10692
10693 (defun gnus-summary-save-article-rmail (&optional arg)
10694   "Append the current article to an rmail file.
10695 If N is a positive number, save the N next articles.
10696 If N is a negative number, save the N previous articles.
10697 If N is nil and any articles have been marked with the process mark,
10698 save those articles instead."
10699   (interactive "P")
10700   (require 'gnus-art)
10701   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10702     (gnus-summary-save-article arg)))
10703
10704 (defun gnus-summary-save-article-file (&optional arg)
10705   "Append the current article to a file.
10706 If N is a positive number, save the N next articles.
10707 If N is a negative number, save the N previous articles.
10708 If N is nil and any articles have been marked with the process mark,
10709 save those articles instead."
10710   (interactive "P")
10711   (require 'gnus-art)
10712   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10713     (gnus-summary-save-article arg)))
10714
10715 (defun gnus-summary-write-article-file (&optional arg)
10716   "Write the current article to a file, deleting the previous file.
10717 If N is a positive number, save the N next articles.
10718 If N is a negative number, save the N previous articles.
10719 If N is nil and any articles have been marked with the process mark,
10720 save those articles instead."
10721   (interactive "P")
10722   (require 'gnus-art)
10723   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10724     (gnus-summary-save-article arg)))
10725
10726 (defun gnus-summary-save-article-body-file (&optional arg)
10727   "Append the current article body to a file.
10728 If N is a positive number, save the N next articles.
10729 If N is a negative number, save the N previous articles.
10730 If N is nil and any articles have been marked with the process mark,
10731 save those articles instead."
10732   (interactive "P")
10733   (require 'gnus-art)
10734   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10735     (gnus-summary-save-article arg)))
10736
10737 (defun gnus-summary-muttprint (&optional arg)
10738   "Print the current article using Muttprint.
10739 If N is a positive number, save the N next articles.
10740 If N is a negative number, save the N previous articles.
10741 If N is nil and any articles have been marked with the process mark,
10742 save those articles instead."
10743   (interactive "P")
10744   (require 'gnus-art)
10745   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10746     (gnus-summary-save-article arg t)))
10747
10748 (defun gnus-summary-pipe-message (program)
10749   "Pipe the current article through PROGRAM."
10750   (interactive "sProgram: ")
10751   (gnus-summary-select-article)
10752   (let ((mail-header-separator ""))
10753     (gnus-eval-in-buffer-window gnus-article-buffer
10754       (save-restriction
10755         (widen)
10756         (let ((start (window-start))
10757               buffer-read-only)
10758           (message-pipe-buffer-body program)
10759           (set-window-start (get-buffer-window (current-buffer)) start))))))
10760
10761 (defun gnus-get-split-value (methods)
10762   "Return a value based on the split METHODS."
10763   (let (split-name method result match)
10764     (when methods
10765       (save-excursion
10766         (set-buffer gnus-original-article-buffer)
10767         (save-restriction
10768           (nnheader-narrow-to-headers)
10769           (while (and methods (not split-name))
10770             (goto-char (point-min))
10771             (setq method (pop methods))
10772             (setq match (car method))
10773             (when (cond
10774                    ((stringp match)
10775                     ;; Regular expression.
10776                     (ignore-errors
10777                       (re-search-forward match nil t)))
10778                    ((gnus-functionp match)
10779                     ;; Function.
10780                     (save-restriction
10781                       (widen)
10782                       (setq result (funcall match gnus-newsgroup-name))))
10783                    ((consp match)
10784                     ;; Form.
10785                     (save-restriction
10786                       (widen)
10787                       (setq result (eval match)))))
10788               (setq split-name (cdr method))
10789               (cond ((stringp result)
10790                      (push (expand-file-name
10791                             result gnus-article-save-directory)
10792                            split-name))
10793                     ((consp result)
10794                      (setq split-name (append result split-name)))))))))
10795     (nreverse split-name)))
10796
10797 (defun gnus-valid-move-group-p (group)
10798   (and (boundp group)
10799        (symbol-name group)
10800        (symbol-value group)
10801        (gnus-get-function (gnus-find-method-for-group
10802                            (symbol-name group)) 'request-accept-article t)))
10803
10804 (defun gnus-read-move-group-name (prompt default articles prefix)
10805   "Read a group name."
10806   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10807          (minibuffer-confirm-incomplete nil) ; XEmacs
10808          (prom
10809           (format "%s %s to:"
10810                   prompt
10811                   (if (> (length articles) 1)
10812                       (format "these %d articles" (length articles))
10813                     "this article")))
10814          (to-newsgroup
10815           (cond
10816            ((null split-name)
10817             (gnus-completing-read-with-default
10818              default prom
10819              gnus-active-hashtb
10820              'gnus-valid-move-group-p
10821              nil prefix
10822              'gnus-group-history))
10823            ((= 1 (length split-name))
10824             (gnus-completing-read-with-default
10825              (car split-name) prom
10826              gnus-active-hashtb
10827              'gnus-valid-move-group-p
10828              nil nil
10829              'gnus-group-history))
10830            (t
10831             (gnus-completing-read-with-default
10832              nil prom
10833              (mapcar (lambda (el) (list el))
10834                      (nreverse split-name))
10835              nil nil nil
10836              'gnus-group-history))))
10837          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10838     (when to-newsgroup
10839       (if (or (string= to-newsgroup "")
10840               (string= to-newsgroup prefix))
10841           (setq to-newsgroup default))
10842       (unless to-newsgroup
10843         (error "No group name entered"))
10844       (or (gnus-active to-newsgroup)
10845           (gnus-activate-group to-newsgroup nil nil to-method)
10846           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10847                                      to-newsgroup))
10848               (or (and (gnus-request-create-group to-newsgroup to-method)
10849                        (gnus-activate-group
10850                         to-newsgroup nil nil to-method)
10851                        (gnus-subscribe-group to-newsgroup))
10852                   (error "Couldn't create group %s" to-newsgroup)))
10853           (error "No such group: %s" to-newsgroup)))
10854     to-newsgroup))
10855
10856 (defun gnus-summary-save-parts (type dir n &optional reverse)
10857   "Save parts matching TYPE to DIR.
10858 If REVERSE, save parts that do not match TYPE."
10859   (interactive
10860    (list (read-string "Save parts of type: "
10861                       (or (car gnus-summary-save-parts-type-history)
10862                           gnus-summary-save-parts-default-mime)
10863                       'gnus-summary-save-parts-type-history)
10864          (setq gnus-summary-save-parts-last-directory
10865                (read-file-name "Save to directory: "
10866                                gnus-summary-save-parts-last-directory
10867                                nil t))
10868          current-prefix-arg))
10869   (gnus-summary-iterate n
10870     (let ((gnus-display-mime-function nil)
10871           (gnus-inhibit-treatment t))
10872       (gnus-summary-select-article))
10873     (save-excursion
10874       (set-buffer gnus-article-buffer)
10875       (let ((handles (or gnus-article-mime-handles
10876                          (mm-dissect-buffer nil gnus-article-loose-mime)
10877                          (and gnus-article-emulate-mime
10878                               (mm-uu-dissect)))))
10879         (when handles
10880           (gnus-summary-save-parts-1 type dir handles reverse)
10881           (unless gnus-article-mime-handles ;; Don't destroy this case.
10882             (mm-destroy-parts handles)))))))
10883
10884 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10885   (if (stringp (car handle))
10886       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10887               (cdr handle))
10888     (when (if reverse
10889               (not (string-match type (mm-handle-media-type handle)))
10890             (string-match type (mm-handle-media-type handle)))
10891       (let ((file (expand-file-name
10892                    (file-name-nondirectory
10893                     (or
10894                      (mail-content-type-get
10895                       (mm-handle-disposition handle) 'filename)
10896                      (concat gnus-newsgroup-name
10897                              "." (number-to-string
10898                                   (cdr gnus-article-current)))))
10899                    dir)))
10900         (unless (file-exists-p file)
10901           (mm-save-part-to-file handle file))))))
10902
10903 ;; Summary extract commands
10904
10905 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10906   (let ((buffer-read-only nil)
10907         (article (gnus-summary-article-number))
10908         after-article b e)
10909     (unless (gnus-summary-goto-subject article)
10910       (error "No such article: %d" article))
10911     (gnus-summary-position-point)
10912     ;; If all commands are to be bunched up on one line, we collect
10913     ;; them here.
10914     (unless gnus-view-pseudos-separately
10915       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10916             files action)
10917         (while ps
10918           (setq action (cdr (assq 'action (car ps))))
10919           (setq files (list (cdr (assq 'name (car ps)))))
10920           (while (and ps (cdr ps)
10921                       (string= (or action "1")
10922                                (or (cdr (assq 'action (cadr ps))) "2")))
10923             (push (cdr (assq 'name (cadr ps))) files)
10924             (setcdr ps (cddr ps)))
10925           (when files
10926             (when (not (string-match "%s" action))
10927               (push " " files))
10928             (push " " files)
10929             (when (assq 'execute (car ps))
10930               (setcdr (assq 'execute (car ps))
10931                       (funcall (if (string-match "%s" action)
10932                                    'format 'concat)
10933                                action
10934                                (mapconcat
10935                                 (lambda (f)
10936                                   (if (equal f " ")
10937                                       f
10938                                     (mm-quote-arg f)))
10939                                 files " ")))))
10940           (setq ps (cdr ps)))))
10941     (if (and gnus-view-pseudos (not not-view))
10942         (while pslist
10943           (when (assq 'execute (car pslist))
10944             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10945                                   (eq gnus-view-pseudos 'not-confirm)))
10946           (setq pslist (cdr pslist)))
10947       (save-excursion
10948         (while pslist
10949           (setq after-article (or (cdr (assq 'article (car pslist)))
10950                                   (gnus-summary-article-number)))
10951           (gnus-summary-goto-subject after-article)
10952           (forward-line 1)
10953           (setq b (point))
10954           (insert "    " (file-name-nondirectory
10955                           (cdr (assq 'name (car pslist))))
10956                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10957           (setq e (point))
10958           (forward-line -1)             ; back to `b'
10959           (gnus-add-text-properties
10960            b (1- e) (list 'gnus-number gnus-reffed-article-number
10961                           gnus-mouse-face-prop gnus-mouse-face))
10962           (gnus-data-enter
10963            after-article gnus-reffed-article-number
10964            gnus-unread-mark b (car pslist) 0 (- e b))
10965           (setq gnus-newsgroup-unreads
10966                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10967                                          gnus-reffed-article-number))
10968           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10969           (setq pslist (cdr pslist)))))))
10970
10971 (defun gnus-pseudos< (p1 p2)
10972   (let ((c1 (cdr (assq 'action p1)))
10973         (c2 (cdr (assq 'action p2))))
10974     (and c1 c2 (string< c1 c2))))
10975
10976 (defun gnus-request-pseudo-article (props)
10977   (cond ((assq 'execute props)
10978          (gnus-execute-command (cdr (assq 'execute props)))))
10979   (let ((gnus-current-article (gnus-summary-article-number)))
10980     (gnus-run-hooks 'gnus-mark-article-hook)))
10981
10982 (defun gnus-execute-command (command &optional automatic)
10983   (save-excursion
10984     (gnus-article-setup-buffer)
10985     (set-buffer gnus-article-buffer)
10986     (setq buffer-read-only nil)
10987     (let ((command (if automatic command
10988                      (read-string "Command: " (cons command 0)))))
10989       (erase-buffer)
10990       (insert "$ " command "\n\n")
10991       (if gnus-view-pseudo-asynchronously
10992           (start-process "gnus-execute" (current-buffer) shell-file-name
10993                          shell-command-switch command)
10994         (call-process shell-file-name nil t nil
10995                       shell-command-switch command)))))
10996
10997 ;; Summary kill commands.
10998
10999 (defun gnus-summary-edit-global-kill (article)
11000   "Edit the \"global\" kill file."
11001   (interactive (list (gnus-summary-article-number)))
11002   (gnus-group-edit-global-kill article))
11003
11004 (defun gnus-summary-edit-local-kill ()
11005   "Edit a local kill file applied to the current newsgroup."
11006   (interactive)
11007   (setq gnus-current-headers (gnus-summary-article-header))
11008   (gnus-group-edit-local-kill
11009    (gnus-summary-article-number) gnus-newsgroup-name))
11010
11011 ;;; Header reading.
11012
11013 (defun gnus-read-header (id &optional header)
11014   "Read the headers of article ID and enter them into the Gnus system."
11015   (let ((group gnus-newsgroup-name)
11016         (gnus-override-method
11017          (or
11018           gnus-override-method
11019           (and (gnus-news-group-p gnus-newsgroup-name)
11020                (car (gnus-refer-article-methods)))))
11021         where)
11022     ;; First we check to see whether the header in question is already
11023     ;; fetched.
11024     (if (stringp id)
11025         ;; This is a Message-ID.
11026         (setq header (or header (gnus-id-to-header id)))
11027       ;; This is an article number.
11028       (setq header (or header (gnus-summary-article-header id))))
11029     (if (and header
11030              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11031         ;; We have found the header.
11032         header
11033       ;; If this is a sparse article, we have to nix out its
11034       ;; previous entry in the thread hashtb.
11035       (when (and header
11036                  (gnus-summary-article-sparse-p (mail-header-number header)))
11037         (let* ((parent (gnus-parent-id (mail-header-references header)))
11038                (thread (and parent (gnus-id-to-thread parent))))
11039           (when thread
11040             (delq (assq header thread) thread))))
11041       ;; We have to really fetch the header to this article.
11042       (save-excursion
11043         (set-buffer nntp-server-buffer)
11044         (when (setq where (gnus-request-head id group))
11045           (nnheader-fold-continuation-lines)
11046           (goto-char (point-max))
11047           (insert ".\n")
11048           (goto-char (point-min))
11049           (insert "211 ")
11050           (princ (cond
11051                   ((numberp id) id)
11052                   ((cdr where) (cdr where))
11053                   (header (mail-header-number header))
11054                   (t gnus-reffed-article-number))
11055                  (current-buffer))
11056           (insert " Article retrieved.\n"))
11057         (if (or (not where)
11058                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11059             ()                          ; Malformed head.
11060           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11061             (when (and (stringp id)
11062                        (not (string= (gnus-group-real-name group)
11063                                      (car where))))
11064               ;; If we fetched by Message-ID and the article came
11065               ;; from a different group, we fudge some bogus article
11066               ;; numbers for this article.
11067               (mail-header-set-number header gnus-reffed-article-number))
11068             (save-excursion
11069               (set-buffer gnus-summary-buffer)
11070               (decf gnus-reffed-article-number)
11071               (gnus-remove-header (mail-header-number header))
11072               (push header gnus-newsgroup-headers)
11073               (setq gnus-current-headers header)
11074               (push (mail-header-number header) gnus-newsgroup-limit)))
11075           header)))))
11076
11077 (defun gnus-remove-header (number)
11078   "Remove header NUMBER from `gnus-newsgroup-headers'."
11079   (if (and gnus-newsgroup-headers
11080            (= number (mail-header-number (car gnus-newsgroup-headers))))
11081       (pop gnus-newsgroup-headers)
11082     (let ((headers gnus-newsgroup-headers))
11083       (while (and (cdr headers)
11084                   (not (= number (mail-header-number (cadr headers)))))
11085         (pop headers))
11086       (when (cdr headers)
11087         (setcdr headers (cddr headers))))))
11088
11089 ;;;
11090 ;;; summary highlights
11091 ;;;
11092
11093 (defun gnus-highlight-selected-summary ()
11094   "Highlight selected article in summary buffer."
11095   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11096   (when gnus-summary-selected-face
11097     (save-excursion
11098       (let* ((beg (gnus-point-at-bol))
11099              (end (gnus-point-at-eol))
11100              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11101              (from (if (get-text-property beg gnus-mouse-face-prop)
11102                        beg
11103                      (or (next-single-property-change
11104                           beg gnus-mouse-face-prop nil end)
11105                          beg)))
11106              (to
11107               (if (= from end)
11108                   (- from 2)
11109                 (or (next-single-property-change
11110                      from gnus-mouse-face-prop nil end)
11111                     end))))
11112         ;; If no mouse-face prop on line we will have to = from = end,
11113         ;; so we highlight the entire line instead.
11114         (when (= (+ to 2) from)
11115           (setq from beg)
11116           (setq to end))
11117         (if gnus-newsgroup-selected-overlay
11118             ;; Move old overlay.
11119             (gnus-move-overlay
11120              gnus-newsgroup-selected-overlay from to (current-buffer))
11121           ;; Create new overlay.
11122           (gnus-overlay-put
11123            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11124            'face gnus-summary-selected-face))))))
11125
11126 (defvar gnus-summary-highlight-line-cached nil)
11127 (defvar gnus-summary-highlight-line-trigger nil)
11128
11129 (defun gnus-summary-highlight-line-0 ()
11130   (if (and (eq gnus-summary-highlight-line-trigger 
11131                gnus-summary-highlight)
11132            gnus-summary-highlight-line-cached)
11133       gnus-summary-highlight-line-cached
11134     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11135           gnus-summary-highlight-line-cached
11136           (let* ((cond (list 'cond))
11137                  (c cond)
11138                  (list gnus-summary-highlight))
11139             (while list
11140               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11141               (setq c (cdr c)
11142                     list (cdr list)))
11143             (gnus-byte-compile (list 'lambda nil cond))))))
11144
11145 (defun gnus-summary-highlight-line ()
11146   "Highlight current line according to `gnus-summary-highlight'."
11147   (let* ((beg (gnus-point-at-bol))
11148          (article (or (gnus-summary-article-number) gnus-current-article))
11149          (score (or (cdr (assq article
11150                                gnus-newsgroup-scored))
11151                     gnus-summary-default-score 0))
11152          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11153          (inhibit-read-only t)
11154          (default gnus-summary-default-score)
11155          (default-high gnus-summary-default-high-score)
11156          (default-low gnus-summary-default-low-score)
11157          (uncached (memq article gnus-newsgroup-undownloaded))
11158          (downloaded (not uncached)))
11159     (let ((face (funcall (gnus-summary-highlight-line-0))))
11160       (unless (eq face (get-text-property beg 'face))
11161         (gnus-put-text-property-excluding-characters-with-faces
11162          beg (gnus-point-at-eol) 'face
11163          (setq face (if (boundp face) (symbol-value face) face)))
11164         (when gnus-summary-highlight-line-function
11165           (funcall gnus-summary-highlight-line-function article face))))))
11166
11167 (defun gnus-update-read-articles (group unread &optional compute)
11168   "Update the list of read articles in GROUP.
11169 UNREAD is a sorted list."
11170   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11171          (entry (gnus-gethash group gnus-newsrc-hashtb))
11172          (info (nth 2 entry))
11173          (prev 1)
11174          read)
11175     (if (or (not info) (not active))
11176         ;; There is no info on this group if it was, in fact,
11177         ;; killed.  Gnus stores no information on killed groups, so
11178         ;; there's nothing to be done.
11179         ;; One could store the information somewhere temporarily,
11180         ;; perhaps...  Hmmm...
11181         ()
11182       ;; Remove any negative articles numbers.
11183       (while (and unread (< (car unread) 0))
11184         (setq unread (cdr unread)))
11185       ;; Remove any expired article numbers
11186       (while (and unread (< (car unread) (car active)))
11187         (setq unread (cdr unread)))
11188       ;; Compute the ranges of read articles by looking at the list of
11189       ;; unread articles.
11190       (while unread
11191         (when (/= (car unread) prev)
11192           (push (if (= prev (1- (car unread))) prev
11193                   (cons prev (1- (car unread))))
11194                 read))
11195         (setq prev (1+ (car unread)))
11196         (setq unread (cdr unread)))
11197       (when (<= prev (cdr active))
11198         (push (cons prev (cdr active)) read))
11199       (setq read (if (> (length read) 1) (nreverse read) read))
11200       (if compute
11201           read
11202         (save-excursion
11203           (let (setmarkundo)
11204             ;; Propagate the read marks to the backend.
11205             (when (gnus-check-backend-function 'request-set-mark group)
11206               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11207                     (add (gnus-remove-from-range read (gnus-info-read info))))
11208                 (when (or add del)
11209                   (unless (gnus-check-group group)
11210                     (error "Can't open server for %s" group))
11211                   (gnus-request-set-mark
11212                    group (delq nil (list (if add (list add 'add '(read)))
11213                                          (if del (list del 'del '(read))))))
11214                   (setq setmarkundo
11215                         `(gnus-request-set-mark
11216                           ,group
11217                           ',(delq nil (list
11218                                        (if del (list del 'add '(read)))
11219                                        (if add (list add 'del '(read))))))))))
11220             (set-buffer gnus-group-buffer)
11221             (gnus-undo-register
11222               `(progn
11223                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11224                  (gnus-info-set-read ',info ',(gnus-info-read info))
11225                  (gnus-get-unread-articles-in-group ',info
11226                                                     (gnus-active ,group))
11227                  (gnus-group-update-group ,group t)
11228                  ,setmarkundo))))
11229         ;; Enter this list into the group info.
11230         (gnus-info-set-read info read)
11231         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11232         (gnus-get-unread-articles-in-group info (gnus-active group))
11233         t))))
11234
11235 (defun gnus-offer-save-summaries ()
11236   "Offer to save all active summary buffers."
11237   (let (buffers)
11238     ;; Go through all buffers and find all summaries.
11239     (dolist (buffer (buffer-list))
11240       (when (and (setq buffer (buffer-name buffer))
11241                  (string-match "Summary" buffer)
11242                  (save-excursion
11243                    (set-buffer buffer)
11244                    ;; We check that this is, indeed, a summary buffer.
11245                    (and (eq major-mode 'gnus-summary-mode)
11246                         ;; Also make sure this isn't bogus.
11247                         gnus-newsgroup-prepared
11248                         ;; Also make sure that this isn't a
11249                         ;; dead summary buffer.
11250                         (not gnus-dead-summary-mode))))
11251         (push buffer buffers)))
11252     ;; Go through all these summary buffers and offer to save them.
11253     (when buffers
11254       (save-excursion
11255         (map-y-or-n-p
11256          "Update summary buffer %s? "
11257          (lambda (buf)
11258            (switch-to-buffer buf)
11259            (gnus-summary-exit))
11260          buffers)))))
11261
11262 (defun gnus-summary-setup-default-charset ()
11263   "Setup newsgroup default charset."
11264   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11265       (setq gnus-newsgroup-charset nil)
11266     (let* ((ignored-charsets
11267             (or gnus-newsgroup-ephemeral-ignored-charsets
11268                 (append
11269                  (and gnus-newsgroup-name
11270                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11271                  gnus-newsgroup-ignored-charsets))))
11272       (setq gnus-newsgroup-charset
11273             (or gnus-newsgroup-ephemeral-charset
11274                 (and gnus-newsgroup-name
11275                      (gnus-parameter-charset gnus-newsgroup-name))
11276                 gnus-default-charset))
11277       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11278            ignored-charsets))))
11279
11280 ;;;
11281 ;;; Mime Commands
11282 ;;;
11283
11284 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11285   "Display the current article buffer fully MIME-buttonized.
11286 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11287 treated as multipart/mixed."
11288   (interactive "P")
11289   (require 'gnus-art)
11290   (let ((gnus-unbuttonized-mime-types nil)
11291         (gnus-mime-display-multipart-as-mixed show-all-parts))
11292     (gnus-summary-show-article)))
11293
11294 (defun gnus-summary-repair-multipart (article)
11295   "Add a Content-Type header to a multipart article without one."
11296   (interactive (list (gnus-summary-article-number)))
11297   (gnus-with-article article
11298     (message-narrow-to-head)
11299     (message-remove-header "Mime-Version")
11300     (goto-char (point-max))
11301     (insert "Mime-Version: 1.0\n")
11302     (widen)
11303     (when (search-forward "\n--" nil t)
11304       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11305         (message-narrow-to-head)
11306         (message-remove-header "Content-Type")
11307         (goto-char (point-max))
11308         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11309                         separator))
11310         (widen))))
11311   (let (gnus-mark-article-hook)
11312     (gnus-summary-select-article t t nil article)))
11313
11314 (defun gnus-summary-toggle-display-buttonized ()
11315   "Toggle the buttonizing of the article buffer."
11316   (interactive)
11317   (require 'gnus-art)
11318   (if (setq gnus-inhibit-mime-unbuttonizing
11319             (not gnus-inhibit-mime-unbuttonizing))
11320       (let ((gnus-unbuttonized-mime-types nil))
11321         (gnus-summary-show-article))
11322     (gnus-summary-show-article)))
11323
11324 ;;;
11325 ;;; Generic summary marking commands
11326 ;;;
11327
11328 (defvar gnus-summary-marking-alist
11329   '((read gnus-del-mark "d")
11330     (unread gnus-unread-mark "u")
11331     (ticked gnus-ticked-mark "!")
11332     (dormant gnus-dormant-mark "?")
11333     (expirable gnus-expirable-mark "e"))
11334   "An alist of names/marks/keystrokes.")
11335
11336 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11337 (defvar gnus-summary-mark-map)
11338
11339 (defun gnus-summary-make-all-marking-commands ()
11340   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11341   (dolist (elem gnus-summary-marking-alist)
11342     (apply 'gnus-summary-make-marking-command elem)))
11343
11344 (defun gnus-summary-make-marking-command (name mark keystroke)
11345   (let ((map (make-sparse-keymap)))
11346     (define-key gnus-summary-generic-mark-map keystroke map)
11347     (dolist (lway `((next "next" next nil "n")
11348                     (next-unread "next unread" next t "N")
11349                     (prev "previous" prev nil "p")
11350                     (prev-unread "previous unread" prev t "P")
11351                     (nomove "" nil nil ,keystroke)))
11352       (let ((func (gnus-summary-make-marking-command-1
11353                    mark (car lway) lway name)))
11354         (setq func (eval func))
11355         (define-key map (nth 4 lway) func)))))
11356
11357 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11358   `(defun ,(intern
11359             (format "gnus-summary-put-mark-as-%s%s"
11360                     name (if (eq way 'nomove)
11361                              ""
11362                            (concat "-" (symbol-name way)))))
11363      (n)
11364      ,(format
11365        "Mark the current article as %s%s.
11366 If N, the prefix, then repeat N times.
11367 If N is negative, move in reverse order.
11368 The difference between N and the actual number of articles marked is
11369 returned."
11370        name (cadr lway))
11371      (interactive "p")
11372      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11373
11374 (defun gnus-summary-generic-mark (n mark move unread)
11375   "Mark N articles with MARK."
11376   (unless (eq major-mode 'gnus-summary-mode)
11377     (error "This command can only be used in the summary buffer"))
11378   (gnus-summary-show-thread)
11379   (let ((nummove
11380          (cond
11381           ((eq move 'next) 1)
11382           ((eq move 'prev) -1)
11383           (t 0))))
11384     (if (zerop nummove)
11385         (setq n 1)
11386       (when (< n 0)
11387         (setq n (abs n)
11388               nummove (* -1 nummove))))
11389     (while (and (> n 0)
11390                 (gnus-summary-mark-article nil mark)
11391                 (zerop (gnus-summary-next-subject nummove unread t)))
11392       (setq n (1- n)))
11393     (when (/= 0 n)
11394       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11395     (gnus-summary-recenter)
11396     (gnus-summary-position-point)
11397     (gnus-set-mode-line 'summary)
11398     n))
11399
11400 (defun gnus-summary-insert-articles (articles)
11401   (when (setq articles
11402               (gnus-sorted-difference articles
11403                                       (mapcar (lambda (h)
11404                                                 (mail-header-number h))
11405                                               gnus-newsgroup-headers)))
11406     (setq gnus-newsgroup-headers
11407           (merge 'list
11408                  gnus-newsgroup-headers
11409                  (gnus-fetch-headers articles)
11410                  'gnus-article-sort-by-number))
11411     ;; Suppress duplicates?
11412     (when gnus-suppress-duplicates
11413       (gnus-dup-suppress-articles))
11414
11415     ;; We might want to build some more threads first.
11416     (when (and gnus-fetch-old-headers
11417                (eq gnus-headers-retrieved-by 'nov))
11418       (if (eq gnus-fetch-old-headers 'invisible)
11419           (gnus-build-all-threads)
11420         (gnus-build-old-threads)))
11421     ;; Let the Gnus agent mark articles as read.
11422     (when gnus-agent
11423       (gnus-agent-get-undownloaded-list))
11424     ;; Remove list identifiers from subject
11425     (when gnus-list-identifiers
11426       (gnus-summary-remove-list-identifiers))
11427     ;; First and last article in this newsgroup.
11428     (when gnus-newsgroup-headers
11429       (setq gnus-newsgroup-begin
11430             (mail-header-number (car gnus-newsgroup-headers))
11431             gnus-newsgroup-end
11432             (mail-header-number
11433              (gnus-last-element gnus-newsgroup-headers))))
11434     (when gnus-use-scoring
11435       (gnus-possibly-score-headers))))
11436
11437 (defun gnus-summary-insert-old-articles (&optional all)
11438   "Insert all old articles in this group.
11439 If ALL is non-nil, already read articles become readable.
11440 If ALL is a number, fetch this number of articles."
11441   (interactive "P")
11442   (prog1
11443       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11444             older len)
11445         (setq older
11446               ;; Some nntp servers lie about their active range.  When
11447               ;; this happens, the active range can be in the millions.
11448               ;; Use a compressed range to avoid creating a huge list.
11449               (gnus-range-difference (list gnus-newsgroup-active) old))
11450         (setq len (gnus-range-length older))
11451         (cond
11452          ((null older) nil)
11453          ((numberp all)
11454           (if (< all len)
11455               (let ((older-range (nreverse older)))
11456                 (setq older nil)
11457
11458                 (while (> all 0)
11459                   (let* ((r (pop older-range))
11460                          (min (if (numberp r) r (car r)))
11461                          (max (if (numberp r) r (cdr r))))
11462                     (while (and (<= min max)
11463                                 (> all 0))
11464                       (push max older)
11465                       (setq all (1- all)
11466                             max (1- max))))))
11467             (setq older (gnus-uncompress-range older))))
11468          (all
11469           (setq older (gnus-uncompress-range older)))
11470          (t
11471           (when (and (numberp gnus-large-newsgroup)
11472                    (> len gnus-large-newsgroup))
11473               (let* ((cursor-in-echo-area nil)
11474                      (initial (gnus-parameter-large-newsgroup-initial
11475                                gnus-newsgroup-name))
11476                      (input
11477                       (read-string
11478                        (format
11479                         "How many articles from %s (%s %d): "
11480                         (gnus-limit-string
11481                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11482                         (if initial "max" "default")
11483                         len)
11484                        (if initial
11485                            (cons (number-to-string initial)
11486                                  0)))))
11487                 (unless (string-match "^[ \t]*$" input)
11488                   (setq all (string-to-number input))
11489                   (if (< all len)
11490                       (let ((older-range (nreverse older)))
11491                         (setq older nil)
11492
11493                         (while (> all 0)
11494                           (let* ((r (pop older-range))
11495                                  (min (if (numberp r) r (car r)))
11496                                  (max (if (numberp r) r (cdr r))))
11497                             (while (and (<= min max)
11498                                         (> all 0))
11499                               (push max older)
11500                               (setq all (1- all)
11501                                     max (1- max))))))))))
11502           (setq older (gnus-uncompress-range older))))
11503         (if (not older)
11504             (message "No old news.")
11505           (gnus-summary-insert-articles older)
11506           (gnus-summary-limit (gnus-sorted-nunion old older))))
11507     (gnus-summary-position-point)))
11508
11509 (defun gnus-summary-insert-new-articles ()
11510   "Insert all new articles in this group."
11511   (interactive)
11512   (prog1
11513       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11514             (old-active gnus-newsgroup-active)
11515             (nnmail-fetched-sources (list t))
11516             i new)
11517         (setq gnus-newsgroup-active
11518               (gnus-activate-group gnus-newsgroup-name 'scan))
11519         (setq i (cdr gnus-newsgroup-active))
11520         (while (> i (cdr old-active))
11521           (push i new)
11522           (decf i))
11523         (if (not new)
11524             (message "No gnus is bad news.")
11525           (gnus-summary-insert-articles new)
11526           (setq gnus-newsgroup-unreads
11527                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11528           (gnus-summary-limit (gnus-sorted-nunion old new))))
11529     (gnus-summary-position-point)))
11530
11531 (gnus-summary-make-all-marking-commands)
11532
11533 (gnus-ems-redefine)
11534
11535 (provide 'gnus-sum)
11536
11537 (run-hooks 'gnus-sum-load-hook)
11538
11539 ;;; gnus-sum.el ends here