a76b6e91ad090a69327c0cf19d99ea50af6a3e33
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 ;; Recursive :-(.
42 ;; (require 'gnus-art)
43 (require 'nnoo)
44 (require 'mime-view)
45
46 (eval-when-compile
47   (require 'mime-play)
48   (require 'static))
49
50 (eval-and-compile
51   (autoload 'pgg-decrypt-region "pgg" nil t)
52   (autoload 'pgg-verify-region "pgg" nil t))
53
54 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
55 (autoload 'gnus-cache-write-active "gnus-cache")
56 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
57 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
58 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
59 (autoload 'mm-uu-dissect "mm-uu")
60
61 (defcustom gnus-kill-summary-on-exit t
62   "*If non-nil, kill the summary buffer when you exit from it.
63 If nil, the summary will become a \"*Dead Summary*\" buffer, and
64 it will be killed sometime later."
65   :group 'gnus-summary-exit
66   :type 'boolean)
67
68 (defcustom gnus-fetch-old-headers nil
69   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
70 If an unread article in the group refers to an older, already read (or
71 just marked as read) article, the old article will not normally be
72 displayed in the Summary buffer.  If this variable is non-nil, Gnus
73 will attempt to grab the headers to the old articles, and thereby
74 build complete threads.  If it has the value `some', only enough
75 headers to connect otherwise loose threads will be displayed.  This
76 variable can also be a number.  In that case, no more than that number
77 of old headers will be fetched.  If it has the value `invisible', all
78 old headers will be fetched, but none will be displayed.
79
80 The server has to support NOV for any of this to work."
81   :group 'gnus-thread
82   :type '(choice (const :tag "off" nil)
83                  (const some)
84                  number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-refer-thread-limit 200
88   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
89 If t, fetch all the available old headers."
90   :group 'gnus-thread
91   :type '(choice number
92                  (sexp :menu-tag "other" t)))
93
94 (defcustom gnus-summary-make-false-root 'adopt
95   "*nil means that Gnus won't gather loose threads.
96 If the root of a thread has expired or been read in a previous
97 session, the information necessary to build a complete thread has been
98 lost.  Instead of having many small sub-threads from this original thread
99 scattered all over the summary buffer, Gnus can gather them.
100
101 If non-nil, Gnus will try to gather all loose sub-threads from an
102 original thread into one large thread.
103
104 If this variable is non-nil, it should be one of `none', `adopt',
105 `dummy' or `empty'.
106
107 If this variable is `none', Gnus will not make a false root, but just
108 present the sub-threads after another.
109 If this variable is `dummy', Gnus will create a dummy root that will
110 have all the sub-threads as children.
111 If this variable is `adopt', Gnus will make one of the \"children\"
112 the parent and mark all the step-children as such.
113 If this variable is `empty', the \"children\" are printed with empty
114 subject fields.  (Or rather, they will be printed with a string
115 given by the `gnus-summary-same-subject' variable.)"
116   :group 'gnus-thread
117   :type '(choice (const :tag "off" nil)
118                  (const none)
119                  (const dummy)
120                  (const adopt)
121                  (const empty)))
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include: `gnus-simplify-subject-re',
151 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 (they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-default-high-score 0
216   "*Default threshold for a high scored article.
217 An article will be highlighted as high scored if its score is greater
218 than this score."
219   :group 'gnus-score-default
220   :type 'integer)
221
222 (defcustom gnus-summary-default-low-score 0
223   "*Default threshold for a low scored article.
224 An article will be highlighted as low scored if its score is smaller
225 than this score."
226   :group 'gnus-score-default
227   :type 'integer)
228
229 (defcustom gnus-summary-zcore-fuzz 0
230   "*Fuzziness factor for the zcore in the summary buffer.
231 Articles with scores closer than this to `gnus-summary-default-score'
232 will not be marked."
233   :group 'gnus-summary-format
234   :type 'integer)
235
236 (defcustom gnus-simplify-subject-fuzzy-regexp nil
237   "*Strings to be removed when doing fuzzy matches.
238 This can either be a regular expression or list of regular expressions
239 that will be removed from subject strings if fuzzy subject
240 simplification is selected."
241   :group 'gnus-thread
242   :type '(repeat regexp))
243
244 (defcustom gnus-show-threads t
245   "*If non-nil, display threads in summary mode."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-hide-subtree nil
250   "*If non-nil, hide all threads initially.
251 If threads are hidden, you have to run the command
252 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
253 to expose hidden threads."
254   :group 'gnus-thread
255   :type 'boolean)
256
257 (defcustom gnus-thread-hide-killed t
258   "*If non-nil, hide killed threads automatically."
259   :group 'gnus-thread
260   :type 'boolean)
261
262 (defcustom gnus-thread-ignore-subject t
263   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
264 If nil, articles that have different subjects from their parents will
265 start separate threads."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-operation-ignore-subject t
270   "*If non-nil, subjects will be ignored when doing thread commands.
271 This affects commands like `gnus-summary-kill-thread' and
272 `gnus-summary-lower-thread'.
273
274 If this variable is nil, articles in the same thread with different
275 subjects will not be included in the operation in question.  If this
276 variable is `fuzzy', only articles that have subjects that are fuzzily
277 equal will be included."
278   :group 'gnus-thread
279   :type '(choice (const :tag "off" nil)
280                  (const fuzzy)
281                  (sexp :tag "on" t)))
282
283 (defcustom gnus-thread-indent-level 4
284   "*Number that says how much each sub-thread should be indented."
285   :group 'gnus-thread
286   :type 'integer)
287
288 (defcustom gnus-auto-extend-newsgroup t
289   "*If non-nil, extend newsgroup forward and backward when requested."
290   :group 'gnus-summary-choose
291   :type 'boolean)
292
293 (defcustom gnus-auto-select-first t
294   "*If nil, don't select the first unread article when entering a group.
295 If this variable is `best', select the highest-scored unread article
296 in the group.  If t, select the first unread article.
297
298 This variable can also be a function to place point on a likely
299 subject line.  Useful values include `gnus-summary-first-unread-subject',
300 `gnus-summary-first-unread-article' and
301 `gnus-summary-best-unread-article'.
302
303 If you want to prevent automatic selection of the first unread article
304 in some newsgroups, set the variable to nil in
305 `gnus-select-group-hook'."
306   :group 'gnus-group-select
307   :type '(choice (const :tag "none" nil)
308                  (const best)
309                  (sexp :menu-tag "first" t)
310                  (function-item gnus-summary-first-unread-subject)
311                  (function-item gnus-summary-first-unread-article)
312                  (function-item gnus-summary-best-unread-article)))
313
314 (defcustom gnus-dont-select-after-jump-to-other-group nil
315   "If non-nil, don't select the first unread article after entering the
316 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
317 it is depend on the value of `gnus-auto-select-first' whether to select
318 or not."
319   :group 'gnus-group-select
320   :type 'boolean)
321
322 (defcustom gnus-auto-select-next t
323   "*If non-nil, offer to go to the next group from the end of the previous.
324 If the value is t and the next newsgroup is empty, Gnus will exit
325 summary mode and go back to group mode.  If the value is neither nil
326 nor t, Gnus will select the following unread newsgroup.  In
327 particular, if the value is the symbol `quietly', the next unread
328 newsgroup will be selected without any confirmation, and if it is
329 `almost-quietly', the next group will be selected without any
330 confirmation if you are located on the last article in the group.
331 Finally, if this variable is `slightly-quietly', the `Z n' command
332 will go to the next group without confirmation."
333   :group 'gnus-summary-maneuvering
334   :type '(choice (const :tag "off" nil)
335                  (const quietly)
336                  (const almost-quietly)
337                  (const slightly-quietly)
338                  (sexp :menu-tag "on" t)))
339
340 (defcustom gnus-auto-select-same nil
341   "*If non-nil, select the next article with the same subject.
342 If there are no more articles with the same subject, go to
343 the first unread article."
344   :group 'gnus-summary-maneuvering
345   :type 'boolean)
346
347 (defcustom gnus-summary-check-current nil
348   "*If non-nil, consider the current article when moving.
349 The \"unread\" movement commands will stay on the same line if the
350 current article is unread."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-center-summary t
355   "*If non-nil, always center the current summary buffer.
356 In particular, if `vertical' do only vertical recentering.  If non-nil
357 and non-`vertical', do both horizontal and vertical recentering."
358   :group 'gnus-summary-maneuvering
359   :type '(choice (const :tag "none" nil)
360                  (const vertical)
361                  (integer :tag "height")
362                  (sexp :menu-tag "both" t)))
363
364 (defcustom gnus-show-all-headers nil
365   "*If non-nil, don't hide any headers."
366   :group 'gnus-article-hiding
367   :group 'gnus-article-headers
368   :type 'boolean)
369
370 (defcustom gnus-summary-ignore-duplicates nil
371   "*If non-nil, ignore articles with identical Message-ID headers."
372   :group 'gnus-summary
373   :type 'boolean)
374
375 (defcustom gnus-single-article-buffer t
376   "*If non-nil, display all articles in the same buffer.
377 If nil, each group will get its own article buffer."
378   :group 'gnus-article-various
379   :type 'boolean)
380
381 (defcustom gnus-break-pages t
382   "*If non-nil, do page breaking on articles.
383 The page delimiter is specified by the `gnus-page-delimiter'
384 variable."
385   :group 'gnus-article-various
386   :type 'boolean)
387
388 (defcustom gnus-show-mime t
389   "*If non-nil, do mime processing of articles.
390 The articles will simply be fed to the function given by
391 `gnus-article-display-method-for-mime'."
392   :group 'gnus-article-mime
393   :type 'boolean)
394
395 (defcustom gnus-move-split-methods nil
396   "*Variable used to suggest where articles are to be moved to.
397 It uses the same syntax as the `gnus-split-methods' variable.
398 However, whereas `gnus-split-methods' specifies file names as targets,
399 this variable specifies group names."
400   :group 'gnus-summary-mail
401   :type '(repeat (choice (list :value (fun) function)
402                          (cons :value ("" "") regexp (repeat string))
403                          (sexp :value nil))))
404
405 (defcustom gnus-unread-mark ?\ ;;;Whitespace
406   "*Mark used for unread articles."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-ticked-mark ?!
411   "*Mark used for ticked articles."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-dormant-mark ??
416   "*Mark used for dormant articles."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-del-mark ?r
421   "*Mark used for del'd articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-read-mark ?R
426   "*Mark used for read articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-expirable-mark ?E
431   "*Mark used for expirable articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-killed-mark ?K
436   "*Mark used for killed articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-souped-mark ?F
441   "*Mark used for souped articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-kill-file-mark ?X
446   "*Mark used for articles killed by kill files."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-low-score-mark ?Y
451   "*Mark used for articles with a low score."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-catchup-mark ?C
456   "*Mark used for articles that are caught up."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-replied-mark ?A
461   "*Mark used for articles that have been replied to."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-forwarded-mark ?O
466   "*Mark used for articles that have been forwarded."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-cached-mark ?*
471   "*Mark used for articles that are in the cache."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-saved-mark ?S
476   "*Mark used for articles that have been saved to."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-no-mark ?\ ;;;Whitespace
481   "*Mark used for articles that have no other secondary mark."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-ancient-mark ?O
486   "*Mark used for ancient articles."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-sparse-mark ?Q
491   "*Mark used for sparsely reffed articles."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-canceled-mark ?G
496   "*Mark used for canceled articles."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-duplicate-mark ?M
501   "*Mark used for duplicate articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-undownloaded-mark ?@
506   "*Mark used for articles that weren't downloaded."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-downloadable-mark ?%
511   "*Mark used for articles that are to be downloaded."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-unsendable-mark ?=
516   "*Mark used for articles that won't be sent."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-score-over-mark ?+
521   "*Score mark used for articles with high scores."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-score-below-mark ?-
526   "*Score mark used for articles with low scores."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
531   "*There is no thread under the article."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-not-empty-thread-mark ?=
536   "*There is a thread under the article."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-view-pseudo-asynchronously nil
541   "*If non-nil, Gnus will view pseudo-articles asynchronously."
542   :group 'gnus-extract-view
543   :type 'boolean)
544
545 (defcustom gnus-auto-expirable-marks
546   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
547         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
548         gnus-souped-mark gnus-duplicate-mark)
549   "*The list of marks converted into expiration if a group is auto-expirable."
550   :version "21.1"
551   :group 'gnus-summary
552   :type '(repeat character))
553
554 (defcustom gnus-inhibit-user-auto-expire t
555   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
556   :version "21.1"
557   :group 'gnus-summary
558   :type 'boolean)
559
560 (defcustom gnus-view-pseudos nil
561   "*If `automatic', pseudo-articles will be viewed automatically.
562 If `not-confirm', pseudos will be viewed automatically, and the user
563 will not be asked to confirm the command."
564   :group 'gnus-extract-view
565   :type '(choice (const :tag "off" nil)
566                  (const automatic)
567                  (const not-confirm)))
568
569 (defcustom gnus-view-pseudos-separately t
570   "*If non-nil, one pseudo-article will be created for each file to be viewed.
571 If nil, all files that use the same viewing command will be given as a
572 list of parameters to that command."
573   :group 'gnus-extract-view
574   :type 'boolean)
575
576 (defcustom gnus-insert-pseudo-articles t
577   "*If non-nil, insert pseudo-articles when decoding articles."
578   :group 'gnus-extract-view
579   :type 'boolean)
580
581 (defcustom gnus-summary-dummy-line-format
582   "  %(:                          :%) %S\n"
583   "*The format specification for the dummy roots in the summary buffer.
584 It works along the same lines as a normal formatting string,
585 with some simple extensions.
586
587 %S  The subject"
588   :group 'gnus-threading
589   :type 'string)
590
591 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
592   "*The format specification for the summary mode line.
593 It works along the same lines as a normal formatting string,
594 with some simple extensions:
595
596 %G  Group name
597 %p  Unprefixed group name
598 %A  Current article number
599 %z  Current article score
600 %V  Gnus version
601 %U  Number of unread articles in the group
602 %e  Number of unselected articles in the group
603 %Z  A string with unread/unselected article counts
604 %g  Shortish group name
605 %S  Subject of the current article
606 %u  User-defined spec
607 %s  Current score file name
608 %d  Number of dormant articles
609 %r  Number of articles that have been marked as read in this session
610 %E  Number of articles expunged by the score files"
611   :group 'gnus-summary-format
612   :type 'string)
613
614 (defcustom gnus-list-identifiers nil
615   "Regexp that matches list identifiers to be removed from subject.
616 This can also be a list of regexps."
617   :version "21.1"
618   :group 'gnus-summary-format
619   :group 'gnus-article-hiding
620   :type '(choice (const :tag "none" nil)
621                  (regexp :value ".*")
622                  (repeat :value (".*") regexp)))
623
624 (defcustom gnus-summary-mark-below 0
625   "*Mark all articles with a score below this variable as read.
626 This variable is local to each summary buffer and usually set by the
627 score file."
628   :group 'gnus-score-default
629   :type 'integer)
630
631 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
632   "*List of functions used for sorting articles in the summary buffer.
633
634 Each function takes two articles and returns non-nil if the first
635 article should be sorted before the other.  If you use more than one
636 function, the primary sort function should be the last.  You should
637 probably always include `gnus-article-sort-by-number' in the list of
638 sorting functions -- preferably first.  Also note that sorting by date
639 is often much slower than sorting by number, and the sorting order is
640 very similar.  (Sorting by date means sorting by the time the message
641 was sent, sorting by number means sorting by arrival time.)
642
643 Ready-made functions include `gnus-article-sort-by-number',
644 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
645 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
646
647 When threading is turned on, the variable `gnus-thread-sort-functions'
648 controls how articles are sorted."
649   :group 'gnus-summary-sort
650   :type '(repeat (choice (function-item gnus-article-sort-by-number)
651                          (function-item gnus-article-sort-by-author)
652                          (function-item gnus-article-sort-by-subject)
653                          (function-item gnus-article-sort-by-date)
654                          (function-item gnus-article-sort-by-score)
655                          (function :tag "other"))))
656
657 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
658   "*List of functions used for sorting threads in the summary buffer.
659 By default, threads are sorted by article number.
660
661 Each function takes two threads and returns non-nil if the first
662 thread should be sorted before the other.  If you use more than one
663 function, the primary sort function should be the last.  You should
664 probably always include `gnus-thread-sort-by-number' in the list of
665 sorting functions -- preferably first.  Also note that sorting by date
666 is often much slower than sorting by number, and the sorting order is
667 very similar.  (Sorting by date means sorting by the time the message
668 was sent, sorting by number means sorting by arrival time.)
669
670 Ready-made functions include `gnus-thread-sort-by-number',
671 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
672 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
673 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
674
675 When threading is turned off, the variable
676 `gnus-article-sort-functions' controls how articles are sorted."
677   :group 'gnus-summary-sort
678   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
679                          (function-item gnus-thread-sort-by-author)
680                          (function-item gnus-thread-sort-by-subject)
681                          (function-item gnus-thread-sort-by-date)
682                          (function-item gnus-thread-sort-by-score)
683                          (function-item gnus-thread-sort-by-total-score)
684                          (function :tag "other"))))
685
686 (defcustom gnus-thread-score-function '+
687   "*Function used for calculating the total score of a thread.
688
689 The function is called with the scores of the article and each
690 subthread and should then return the score of the thread.
691
692 Some functions you can use are `+', `max', or `min'."
693   :group 'gnus-summary-sort
694   :type 'function)
695
696 (defcustom gnus-summary-expunge-below nil
697   "All articles that have a score less than this variable will be expunged.
698 This variable is local to the summary buffers."
699   :group 'gnus-score-default
700   :type '(choice (const :tag "off" nil)
701                  integer))
702
703 (defcustom gnus-thread-expunge-below nil
704   "All threads that have a total score less than this variable will be expunged.
705 See `gnus-thread-score-function' for en explanation of what a
706 \"thread score\" is.
707
708 This variable is local to the summary buffers."
709   :group 'gnus-threading
710   :group 'gnus-score-default
711   :type '(choice (const :tag "off" nil)
712                  integer))
713
714 (defcustom gnus-summary-mode-hook nil
715   "*A hook for Gnus summary mode.
716 This hook is run before any variables are set in the summary buffer."
717   :options '(turn-on-gnus-mailing-list-mode)
718   :group 'gnus-summary-various
719   :type 'hook)
720
721 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
722 (when (featurep 'xemacs)
723   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
724   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
725   (add-hook 'gnus-summary-mode-hook
726             'gnus-xmas-switch-horizontal-scrollbar-off))
727
728 (defcustom gnus-summary-menu-hook nil
729   "*Hook run after the creation of the summary mode menu."
730   :group 'gnus-summary-visual
731   :type 'hook)
732
733 (defcustom gnus-summary-exit-hook nil
734   "*A hook called on exit from the summary buffer.
735 It will be called with point in the group buffer."
736   :group 'gnus-summary-exit
737   :type 'hook)
738
739 (defcustom gnus-summary-prepare-hook nil
740   "*A hook called after the summary buffer has been generated.
741 If you want to modify the summary buffer, you can use this hook."
742   :group 'gnus-summary-various
743   :type 'hook)
744
745 (defcustom gnus-summary-prepared-hook nil
746   "*A hook called as the last thing after the summary buffer has been generated."
747   :group 'gnus-summary-various
748   :type 'hook)
749
750 (defcustom gnus-summary-generate-hook nil
751   "*A hook run just before generating the summary buffer.
752 This hook is commonly used to customize threading variables and the
753 like."
754   :group 'gnus-summary-various
755   :type 'hook)
756
757 (defcustom gnus-select-group-hook nil
758   "*A hook called when a newsgroup is selected.
759
760 If you'd like to simplify subjects like the
761 `gnus-summary-next-same-subject' command does, you can use the
762 following hook:
763
764  (setq gnus-select-group-hook
765       (list
766         (lambda ()
767           (mapcar (lambda (header)
768                      (mail-header-set-subject
769                       header
770                       (gnus-simplify-subject
771                        (mail-header-subject header) 're-only)))
772                   gnus-newsgroup-headers))))"
773   :group 'gnus-group-select
774   :type 'hook)
775
776 (defcustom gnus-select-article-hook nil
777   "*A hook called when an article is selected."
778   :group 'gnus-summary-choose
779   :type 'hook)
780
781 (defcustom gnus-visual-mark-article-hook
782   (list 'gnus-highlight-selected-summary)
783   "*Hook run after selecting an article in the summary buffer.
784 It is meant to be used for highlighting the article in some way.  It
785 is not run if `gnus-visual' is nil."
786   :group 'gnus-summary-visual
787   :type 'hook)
788
789 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
790   "*A hook called before parsing the headers."
791   :group 'gnus-various
792   :type 'hook)
793
794 (defcustom gnus-exit-group-hook nil
795   "*A hook called when exiting summary mode.
796 This hook is not called from the non-updating exit commands like `Q'."
797   :group 'gnus-various
798   :type 'hook)
799
800 (defcustom gnus-summary-update-hook
801   (list 'gnus-summary-highlight-line)
802   "*A hook called when a summary line is changed.
803 The hook will not be called if `gnus-visual' is nil.
804
805 The default function `gnus-summary-highlight-line' will
806 highlight the line according to the `gnus-summary-highlight'
807 variable."
808   :group 'gnus-summary-visual
809   :type 'hook)
810
811 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
812   "*A hook called when an article is selected for the first time.
813 The hook is intended to mark an article as read (or unread)
814 automatically when it is selected."
815   :group 'gnus-summary-choose
816   :type 'hook)
817
818 (defcustom gnus-group-no-more-groups-hook nil
819   "*A hook run when returning to group mode having no more (unread) groups."
820   :group 'gnus-group-select
821   :type 'hook)
822
823 (defcustom gnus-ps-print-hook nil
824   "*A hook run before ps-printing something from Gnus."
825   :group 'gnus-summary
826   :type 'hook)
827
828 (defcustom gnus-summary-display-arrow
829   (and (fboundp 'display-graphic-p)
830        (display-graphic-p))
831   "*If non-nil, display an arrow highlighting the current article."
832   :version "21.1"
833   :group 'gnus-summary
834   :type 'boolean)
835
836 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
837   "Face used for highlighting the current article in the summary buffer."
838   :group 'gnus-summary-visual
839   :type 'face)
840
841 (defcustom gnus-summary-highlight
842   '(((= mark gnus-canceled-mark)
843      . gnus-summary-cancelled-face)
844     ((and (> score default-high)
845           (or (= mark gnus-dormant-mark)
846               (= mark gnus-ticked-mark)))
847      . gnus-summary-high-ticked-face)
848     ((and (< score default-low)
849           (or (= mark gnus-dormant-mark)
850               (= mark gnus-ticked-mark)))
851      . gnus-summary-low-ticked-face)
852     ((or (= mark gnus-dormant-mark)
853          (= mark gnus-ticked-mark))
854      . gnus-summary-normal-ticked-face)
855     ((and (> score default-high) (= mark gnus-ancient-mark))
856      . gnus-summary-high-ancient-face)
857     ((and (< score default-low) (= mark gnus-ancient-mark))
858      . gnus-summary-low-ancient-face)
859     ((= mark gnus-ancient-mark)
860      . gnus-summary-normal-ancient-face)
861     ((and (> score default-high) (= mark gnus-unread-mark))
862      . gnus-summary-high-unread-face)
863     ((and (< score default-low) (= mark gnus-unread-mark))
864      . gnus-summary-low-unread-face)
865     ((= mark gnus-unread-mark)
866      . gnus-summary-normal-unread-face)
867     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
868                                                   gnus-undownloaded-mark)))
869      . gnus-summary-high-unread-face)
870     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
871                                                  gnus-undownloaded-mark)))
872      . gnus-summary-low-unread-face)
873     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
874           (memq article gnus-newsgroup-unreads))
875      . gnus-summary-normal-unread-face)
876     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
877      . gnus-summary-normal-read-face)
878     ((> score default-high)
879      . gnus-summary-high-read-face)
880     ((< score default-low)
881      . gnus-summary-low-read-face)
882     (t
883      . gnus-summary-normal-read-face))
884   "*Controls the highlighting of summary buffer lines.
885
886 A list of (FORM . FACE) pairs.  When deciding how a a particular
887 summary line should be displayed, each form is evaluated.  The content
888 of the face field after the first true form is used.  You can change
889 how those summary lines are displayed, by editing the face field.
890
891 You can use the following variables in the FORM field.
892
893 score:        The article's score
894 default:      The default article score.
895 default-high: The default score for high scored articles.
896 default-low:  The default score for low scored articles.
897 below:        The score below which articles are automatically marked as read.
898 mark:         The articles mark."
899   :group 'gnus-summary-visual
900   :type '(repeat (cons (sexp :tag "Form" nil)
901                        face)))
902
903 (defcustom gnus-alter-header-function nil
904   "Function called to allow alteration of article header structures.
905 The function is called with one parameter, the article header vector,
906 which it may alter in any way.")
907
908 (defvar gnus-decode-encoded-word-function
909   (mime-find-field-decoder 'From 'nov)
910   "Variable that says which function should be used to decode a string with encoded words.")
911
912 (defcustom gnus-extra-headers nil
913   "*Extra headers to parse."
914   :version "21.1"
915   :group 'gnus-summary
916   :type '(repeat symbol))
917
918 (defcustom gnus-ignored-from-addresses
919   (and user-mail-address (regexp-quote user-mail-address))
920   "*Regexp of From headers that may be suppressed in favor of To headers."
921   :version "21.1"
922   :group 'gnus-summary
923   :type 'regexp)
924
925 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
926   "List of charsets that should be ignored.
927 When these charsets are used in the \"charset\" parameter, the
928 default charset will be used instead."
929   :version "21.1"
930   :type '(repeat symbol)
931   :group 'gnus-charset)
932
933 (gnus-define-group-parameter
934  ignored-charsets
935  :type list
936  :function-document
937  "Return the ignored charsets of GROUP."
938  :variable gnus-group-ignored-charsets-alist
939  :variable-default
940  '(("alt\\.chinese\\.text" iso-8859-1))
941  :variable-document
942  "Alist of regexps (to match group names) and charsets that should be ignored.
943 When these charsets are used in the \"charset\" parameter, the
944 default charset will be used instead."
945  :variable-group gnus-charset
946  :variable-type '(repeat (cons (regexp :tag "Group")
947                                (repeat symbol)))
948  :parameter-type '(choice :tag "Ignored charsets"
949                           :value nil
950                           (repeat (symbol)))
951  :parameter-document       "\
952 List of charsets that should be ignored.
953
954 When these charsets are used in the \"charset\" parameter, the
955 default charset will be used instead.")
956
957 (defcustom gnus-group-highlight-words-alist nil
958   "Alist of group regexps and highlight regexps.
959 This variable uses the same syntax as `gnus-emphasis-alist'."
960   :version "21.1"
961   :type '(repeat (cons (regexp :tag "Group")
962                        (repeat (list (regexp :tag "Highlight regexp")
963                                      (number :tag "Group for entire word" 0)
964                                      (number :tag "Group for displayed part" 0)
965                                      (symbol :tag "Face"
966                                              gnus-emphasis-highlight-words)))))
967   :group 'gnus-summary-visual)
968
969 (defcustom gnus-use-wheel nil
970   "Use Intelli-mouse on summary movement"
971   :type 'boolean
972   :group 'gnus-summary-maneuvering)
973
974 (defcustom gnus-wheel-scroll-amount '(5 . 1)
975   "Amount to scroll messages by spinning the mouse wheel.
976 This is actually a cons cell, where the first item is the amount to scroll
977 on a normal wheel event, and the second is the amount to scroll when the
978 wheel is moved with the shift key depressed."
979   :type '(cons (integer :tag "Shift") integer)
980   :group 'gnus-summary-maneuvering)
981
982 (defcustom gnus-wheel-edge-resistance 2
983   "How hard it should be to change the current article
984 by moving the mouse over the edge of the article window."
985   :type 'integer
986   :group 'gnus-summary-maneuvering)
987
988 (defcustom gnus-summary-show-article-charset-alist
989   nil
990   "Alist of number and charset.
991 The article will be shown with the charset corresponding to the
992 numbered argument.
993 For example: ((1 . cn-gb-2312) (2 . big5))."
994   :version "21.1"
995   :type '(repeat (cons (number :tag "Argument" 1)
996                        (symbol :tag "Charset")))
997   :group 'gnus-charset)
998
999 (defcustom gnus-preserve-marks t
1000   "Whether marks are preserved when moving, copying and respooling messages."
1001   :version "21.1"
1002   :type 'boolean
1003   :group 'gnus-summary-marks)
1004
1005 (defcustom gnus-alter-articles-to-read-function nil
1006   "Function to be called to alter the list of articles to be selected."
1007   :type '(choice (const nil) function)
1008   :group 'gnus-summary)
1009
1010 (defcustom gnus-orphan-score nil
1011   "*All orphans get this score added.  Set in the score file."
1012   :group 'gnus-score-default
1013   :type '(choice (const nil)
1014                  integer))
1015
1016 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1017   "*A regexp to match MIME parts when saving multiple parts of a message
1018 with gnus-summary-save-parts (X m). This regexp will be used by default
1019 when prompting the user for which type of files to save."
1020   :group 'gnus-summary
1021   :type 'regexp)
1022
1023
1024 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1025   "*A regexp to match MIME parts when saving multiple parts of a message
1026 with gnus-summary-save-parts (X m). This regexp will be used by default
1027 when prompting the user for which type of files to save."
1028   :group 'gnus-summary
1029   :type 'regexp)
1030
1031 (defcustom gnus-read-all-available-headers nil
1032   "Whether Gnus should parse all headers made available to it.
1033 This is mostly relevant for slow backends where the user may
1034 wish to widen the summary buffer to include all headers
1035 that were fetched.  Say, for nnultimate groups."
1036   :group 'gnus-summary
1037   :type '(choice boolean regexp))
1038
1039 ;;; Internal variables
1040
1041 (defvar gnus-article-mime-handles nil)
1042 (defvar gnus-article-decoded-p nil)
1043 (defvar gnus-article-charset nil)
1044 (defvar gnus-article-ignored-charsets nil)
1045 (defvar gnus-scores-exclude-files nil)
1046 (defvar gnus-page-broken nil)
1047 (defvar gnus-inhibit-mime-unbuttonizing nil)
1048
1049 (defvar gnus-original-article nil)
1050 (defvar gnus-article-internal-prepare-hook nil)
1051 (defvar gnus-newsgroup-process-stack nil)
1052
1053 (defvar gnus-thread-indent-array nil)
1054 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1055 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1056   "Function called to sort the articles within a thread after it has been gathered together.")
1057
1058 (defvar gnus-summary-save-parts-type-history nil)
1059 (defvar gnus-summary-save-parts-last-directory nil)
1060
1061 (defvar gnus-summary-save-parts-type-history nil)
1062 (defvar gnus-summary-save-parts-last-directory nil)
1063
1064 ;; Avoid highlighting in kill files.
1065 (defvar gnus-summary-inhibit-highlight nil)
1066 (defvar gnus-newsgroup-selected-overlay nil)
1067 (defvar gnus-inhibit-limiting nil)
1068 (defvar gnus-newsgroup-adaptive-score-file nil)
1069 (defvar gnus-current-score-file nil)
1070 (defvar gnus-current-move-group nil)
1071 (defvar gnus-current-copy-group nil)
1072 (defvar gnus-current-crosspost-group nil)
1073
1074 (defvar gnus-newsgroup-dependencies nil)
1075 (defvar gnus-newsgroup-adaptive nil)
1076 (defvar gnus-summary-display-article-function nil)
1077 (defvar gnus-summary-highlight-line-function nil
1078   "Function called after highlighting a summary line.")
1079
1080 (defvar gnus-summary-line-format-alist
1081   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1082     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1083     (?s gnus-tmp-subject-or-nil ?s)
1084     (?n gnus-tmp-name ?s)
1085     (?A (std11-address-string
1086          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1087     (?a (or (std11-full-name-string
1088              (car (mime-entity-read-field gnus-tmp-header 'From)))
1089             gnus-tmp-from) ?s)
1090     (?F gnus-tmp-from ?s)
1091     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1092     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1093     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1094     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1095     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1096     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1097     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1098     (?L gnus-tmp-lines ?s)
1099     (?I gnus-tmp-indentation ?s)
1100     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1101     (?R gnus-tmp-replied ?c)
1102     (?\[ gnus-tmp-opening-bracket ?c)
1103     (?\] gnus-tmp-closing-bracket ?c)
1104     (?\> (make-string gnus-tmp-level ? ) ?s)
1105     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1106     (?i gnus-tmp-score ?d)
1107     (?z gnus-tmp-score-char ?c)
1108     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1109     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1110     (?U gnus-tmp-unread ?c)
1111     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1112     (?t (gnus-summary-number-of-articles-in-thread
1113          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1114         ?d)
1115     (?e (gnus-summary-number-of-articles-in-thread
1116          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1117         ?c)
1118     (?u gnus-tmp-user-defined ?s)
1119     (?P (gnus-pick-line-number) ?d)
1120     (?B gnus-tmp-thread-tree-header-string ?s))
1121   "An alist of format specifications that can appear in summary lines.
1122 These are paired with what variables they correspond with, along with
1123 the type of the variable (string, integer, character, etc).")
1124
1125 (defvar gnus-summary-dummy-line-format-alist
1126   `((?S gnus-tmp-subject ?s)
1127     (?N gnus-tmp-number ?d)
1128     (?u gnus-tmp-user-defined ?s)))
1129
1130 (defvar gnus-summary-mode-line-format-alist
1131   `((?G gnus-tmp-group-name ?s)
1132     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1133     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1134     (?A gnus-tmp-article-number ?d)
1135     (?Z gnus-tmp-unread-and-unselected ?s)
1136     (?V gnus-version ?s)
1137     (?U gnus-tmp-unread-and-unticked ?d)
1138     (?S gnus-tmp-subject ?s)
1139     (?e gnus-tmp-unselected ?d)
1140     (?u gnus-tmp-user-defined ?s)
1141     (?d (length gnus-newsgroup-dormant) ?d)
1142     (?t (length gnus-newsgroup-marked) ?d)
1143     (?r (length gnus-newsgroup-reads) ?d)
1144     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1145     (?E gnus-newsgroup-expunged-tally ?d)
1146     (?s (gnus-current-score-file-nondirectory) ?s)))
1147
1148 (defvar gnus-last-search-regexp nil
1149   "Default regexp for article search command.")
1150
1151 (defvar gnus-summary-search-article-matched-data nil
1152   "Last matched data of article search command.  It is the local variable
1153 in `gnus-article-buffer' which consists of the list of start position,
1154 end position and text.")
1155
1156 (defvar gnus-last-shell-command nil
1157   "Default shell command on article.")
1158
1159 (defvar gnus-newsgroup-begin nil)
1160 (defvar gnus-newsgroup-end nil)
1161 (defvar gnus-newsgroup-last-rmail nil)
1162 (defvar gnus-newsgroup-last-mail nil)
1163 (defvar gnus-newsgroup-last-folder nil)
1164 (defvar gnus-newsgroup-last-file nil)
1165 (defvar gnus-newsgroup-auto-expire nil)
1166 (defvar gnus-newsgroup-active nil)
1167
1168 (defvar gnus-newsgroup-data nil)
1169 (defvar gnus-newsgroup-data-reverse nil)
1170 (defvar gnus-newsgroup-limit nil)
1171 (defvar gnus-newsgroup-limits nil)
1172
1173 (defvar gnus-newsgroup-unreads nil
1174   "List of unread articles in the current newsgroup.")
1175
1176 (defvar gnus-newsgroup-unselected nil
1177   "List of unselected unread articles in the current newsgroup.")
1178
1179 (defvar gnus-newsgroup-reads nil
1180   "Alist of read articles and article marks in the current newsgroup.")
1181
1182 (defvar gnus-newsgroup-expunged-tally nil)
1183
1184 (defvar gnus-newsgroup-marked nil
1185   "List of ticked articles in the current newsgroup (a subset of unread art).")
1186
1187 (defvar gnus-newsgroup-killed nil
1188   "List of ranges of articles that have been through the scoring process.")
1189
1190 (defvar gnus-newsgroup-cached nil
1191   "List of articles that come from the article cache.")
1192
1193 (defvar gnus-newsgroup-saved nil
1194   "List of articles that have been saved.")
1195
1196 (defvar gnus-newsgroup-kill-headers nil)
1197
1198 (defvar gnus-newsgroup-replied nil
1199   "List of articles that have been replied to in the current newsgroup.")
1200
1201 (defvar gnus-newsgroup-forwarded nil
1202   "List of articles that have been forwarded in the current newsgroup.")
1203
1204 (defvar gnus-newsgroup-expirable nil
1205   "List of articles in the current newsgroup that can be expired.")
1206
1207 (defvar gnus-newsgroup-processable nil
1208   "List of articles in the current newsgroup that can be processed.")
1209
1210 (defvar gnus-newsgroup-downloadable nil
1211   "List of articles in the current newsgroup that can be processed.")
1212
1213 (defvar gnus-newsgroup-undownloaded nil
1214   "List of articles in the current newsgroup that haven't been downloaded..")
1215
1216 (defvar gnus-newsgroup-unsendable nil
1217   "List of articles in the current newsgroup that won't be sent.")
1218
1219 (defvar gnus-newsgroup-bookmarks nil
1220   "List of articles in the current newsgroup that have bookmarks.")
1221
1222 (defvar gnus-newsgroup-dormant nil
1223   "List of dormant articles in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-scored nil
1226   "List of scored articles in the current newsgroup.")
1227
1228 (defvar gnus-newsgroup-incorporated nil
1229   "List of incorporated articles in the current newsgroup.")
1230
1231 (defvar gnus-newsgroup-headers nil
1232   "List of article headers in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-threads nil)
1235
1236 (defvar gnus-newsgroup-prepared nil
1237   "Whether the current group has been prepared properly.")
1238
1239 (defvar gnus-newsgroup-ancient nil
1240   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1241
1242 (defvar gnus-newsgroup-sparse nil)
1243
1244 (defvar gnus-current-article nil)
1245 (defvar gnus-article-current nil)
1246 (defvar gnus-current-headers nil)
1247 (defvar gnus-have-all-headers nil)
1248 (defvar gnus-last-article nil)
1249 (defvar gnus-newsgroup-history nil)
1250 (defvar gnus-newsgroup-charset nil)
1251 (defvar gnus-newsgroup-ephemeral-charset nil)
1252 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1253
1254 (defvar gnus-article-before-search nil)
1255
1256 (defconst gnus-summary-local-variables
1257   '(gnus-newsgroup-name
1258     gnus-newsgroup-begin gnus-newsgroup-end
1259     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1260     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1261     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1262     gnus-newsgroup-unselected gnus-newsgroup-marked
1263     gnus-newsgroup-reads gnus-newsgroup-saved
1264     gnus-newsgroup-replied gnus-newsgroup-forwarded
1265     gnus-newsgroup-expirable
1266     gnus-newsgroup-processable gnus-newsgroup-killed
1267     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1268     gnus-newsgroup-unsendable
1269     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1270     gnus-newsgroup-headers gnus-newsgroup-threads
1271     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1272     gnus-current-article gnus-current-headers gnus-have-all-headers
1273     gnus-last-article gnus-article-internal-prepare-hook
1274     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1275     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1276     gnus-thread-expunge-below
1277     gnus-score-alist gnus-current-score-file
1278     (gnus-summary-expunge-below . global)
1279     (gnus-summary-mark-below . global)
1280     (gnus-orphan-score . global)
1281     gnus-newsgroup-active gnus-scores-exclude-files
1282     gnus-newsgroup-history gnus-newsgroup-ancient
1283     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1284     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1285     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1286     (gnus-newsgroup-expunged-tally . 0)
1287     gnus-cache-removable-articles gnus-newsgroup-cached
1288     gnus-newsgroup-data gnus-newsgroup-data-reverse
1289     gnus-newsgroup-limit gnus-newsgroup-limits
1290     gnus-newsgroup-charset
1291     gnus-newsgroup-incorporated)
1292   "Variables that are buffer-local to the summary buffers.")
1293
1294 (defvar gnus-newsgroup-variables nil
1295   "Variables that have separate values in the newsgroups.")
1296
1297 ;; Byte-compiler warning.
1298 (eval-when-compile (defvar gnus-article-mode-map))
1299
1300 ;; Subject simplification.
1301
1302 (defun gnus-simplify-whitespace (str)
1303   "Remove excessive whitespace from STR."
1304   (let ((mystr str))
1305     ;; Multiple spaces.
1306     (while (string-match "[ \t][ \t]+" mystr)
1307       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1308                           " "
1309                           (substring mystr (match-end 0)))))
1310     ;; Leading spaces.
1311     (when (string-match "^[ \t]+" mystr)
1312       (setq mystr (substring mystr (match-end 0))))
1313     ;; Trailing spaces.
1314     (when (string-match "[ \t]+$" mystr)
1315       (setq mystr (substring mystr 0 (match-beginning 0))))
1316     mystr))
1317
1318 (defsubst gnus-simplify-subject-re (subject)
1319   "Remove \"Re:\" from subject lines."
1320   (if (string-match message-subject-re-regexp subject)
1321       (substring subject (match-end 0))
1322     subject))
1323
1324 (defun gnus-simplify-subject (subject &optional re-only)
1325   "Remove `Re:' and words in parentheses.
1326 If RE-ONLY is non-nil, strip leading `Re:'s only."
1327   (let ((case-fold-search t))           ;Ignore case.
1328     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1329     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1330       (setq subject (substring subject (match-end 0))))
1331     ;; Remove uninteresting prefixes.
1332     (when (and (not re-only)
1333                gnus-simplify-ignored-prefixes
1334                (string-match gnus-simplify-ignored-prefixes subject))
1335       (setq subject (substring subject (match-end 0))))
1336     ;; Remove words in parentheses from end.
1337     (unless re-only
1338       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1339         (setq subject (substring subject 0 (match-beginning 0)))))
1340     ;; Return subject string.
1341     subject))
1342
1343 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1344 ;; all whitespace.
1345 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1346   (goto-char (point-min))
1347   (while (re-search-forward regexp nil t)
1348     (replace-match (or newtext ""))))
1349
1350 (defun gnus-simplify-buffer-fuzzy ()
1351   "Simplify string in the buffer fuzzily.
1352 The string in the accessible portion of the current buffer is simplified.
1353 It is assumed to be a single-line subject.
1354 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1355 matter is removed.  Additional things can be deleted by setting
1356 `gnus-simplify-subject-fuzzy-regexp'."
1357   (let ((case-fold-search t)
1358         (modified-tick))
1359     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1360
1361     (while (not (eq modified-tick (buffer-modified-tick)))
1362       (setq modified-tick (buffer-modified-tick))
1363       (cond
1364        ((listp gnus-simplify-subject-fuzzy-regexp)
1365         (mapcar 'gnus-simplify-buffer-fuzzy-step
1366                 gnus-simplify-subject-fuzzy-regexp))
1367        (gnus-simplify-subject-fuzzy-regexp
1368         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1369       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1370       (gnus-simplify-buffer-fuzzy-step
1371        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1372       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1373
1374     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1375     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1376     (gnus-simplify-buffer-fuzzy-step " $")
1377     (gnus-simplify-buffer-fuzzy-step "^ +")))
1378
1379 (defun gnus-simplify-subject-fuzzy (subject)
1380   "Simplify a subject string fuzzily.
1381 See `gnus-simplify-buffer-fuzzy' for details."
1382   (save-excursion
1383     (gnus-set-work-buffer)
1384     (let ((case-fold-search t))
1385       ;; Remove uninteresting prefixes.
1386       (when (and gnus-simplify-ignored-prefixes
1387                  (string-match gnus-simplify-ignored-prefixes subject))
1388         (setq subject (substring subject (match-end 0))))
1389       (insert subject)
1390       (inline (gnus-simplify-buffer-fuzzy))
1391       (buffer-string))))
1392
1393 (defsubst gnus-simplify-subject-fully (subject)
1394   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1395   (cond
1396    (gnus-simplify-subject-functions
1397     (gnus-map-function gnus-simplify-subject-functions subject))
1398    ((null gnus-summary-gather-subject-limit)
1399     (gnus-simplify-subject-re subject))
1400    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1401     (gnus-simplify-subject-fuzzy subject))
1402    ((numberp gnus-summary-gather-subject-limit)
1403     (gnus-limit-string (gnus-simplify-subject-re subject)
1404                        gnus-summary-gather-subject-limit))
1405    (t
1406     subject)))
1407
1408 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1409   "Check whether two subjects are equal.
1410 If optional argument simple-first is t, first argument is already
1411 simplified."
1412   (cond
1413    ((null simple-first)
1414     (equal (gnus-simplify-subject-fully s1)
1415            (gnus-simplify-subject-fully s2)))
1416    (t
1417     (equal s1
1418            (gnus-simplify-subject-fully s2)))))
1419
1420 (defun gnus-summary-bubble-group ()
1421   "Increase the score of the current group.
1422 This is a handy function to add to `gnus-summary-exit-hook' to
1423 increase the score of each group you read."
1424   (gnus-group-add-score gnus-newsgroup-name))
1425
1426 \f
1427 ;;;
1428 ;;; Gnus summary mode
1429 ;;;
1430
1431 (put 'gnus-summary-mode 'mode-class 'special)
1432
1433 (defvar gnus-article-commands-menu)
1434
1435 (when t
1436   ;; Non-orthogonal keys
1437
1438   (gnus-define-keys gnus-summary-mode-map
1439     " " gnus-summary-next-page
1440     "\177" gnus-summary-prev-page
1441     [delete] gnus-summary-prev-page
1442     [backspace] gnus-summary-prev-page
1443     "\r" gnus-summary-scroll-up
1444     "\M-\r" gnus-summary-scroll-down
1445     "n" gnus-summary-next-unread-article
1446     "p" gnus-summary-prev-unread-article
1447     "N" gnus-summary-next-article
1448     "P" gnus-summary-prev-article
1449     "\M-\C-n" gnus-summary-next-same-subject
1450     "\M-\C-p" gnus-summary-prev-same-subject
1451     "\M-n" gnus-summary-next-unread-subject
1452     "\M-p" gnus-summary-prev-unread-subject
1453     "." gnus-summary-first-unread-article
1454     "," gnus-summary-best-unread-article
1455     "\M-s" gnus-summary-search-article-forward
1456     "\M-r" gnus-summary-search-article-backward
1457     "<" gnus-summary-beginning-of-article
1458     ">" gnus-summary-end-of-article
1459     "j" gnus-summary-goto-article
1460     "^" gnus-summary-refer-parent-article
1461     "\M-^" gnus-summary-refer-article
1462     "u" gnus-summary-tick-article-forward
1463     "!" gnus-summary-tick-article-forward
1464     "U" gnus-summary-tick-article-backward
1465     "d" gnus-summary-mark-as-read-forward
1466     "D" gnus-summary-mark-as-read-backward
1467     "E" gnus-summary-mark-as-expirable
1468     "\M-u" gnus-summary-clear-mark-forward
1469     "\M-U" gnus-summary-clear-mark-backward
1470     "k" gnus-summary-kill-same-subject-and-select
1471     "\C-k" gnus-summary-kill-same-subject
1472     "\M-\C-k" gnus-summary-kill-thread
1473     "\M-\C-l" gnus-summary-lower-thread
1474     "e" gnus-summary-edit-article
1475     "#" gnus-summary-mark-as-processable
1476     "\M-#" gnus-summary-unmark-as-processable
1477     "\M-\C-t" gnus-summary-toggle-threads
1478     "\M-\C-s" gnus-summary-show-thread
1479     "\M-\C-h" gnus-summary-hide-thread
1480     "\M-\C-f" gnus-summary-next-thread
1481     "\M-\C-b" gnus-summary-prev-thread
1482     [(meta down)] gnus-summary-next-thread
1483     [(meta up)] gnus-summary-prev-thread
1484     "\M-\C-u" gnus-summary-up-thread
1485     "\M-\C-d" gnus-summary-down-thread
1486     "&" gnus-summary-execute-command
1487     "c" gnus-summary-catchup-and-exit
1488     "\C-w" gnus-summary-mark-region-as-read
1489     "\C-t" gnus-summary-toggle-truncation
1490     "?" gnus-summary-mark-as-dormant
1491     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1492     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1493     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1494     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1495     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1496     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1497     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1498     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1499     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1500     "=" gnus-summary-expand-window
1501     "\C-x\C-s" gnus-summary-reselect-current-group
1502     "\M-g" gnus-summary-rescan-group
1503     "w" gnus-summary-stop-page-breaking
1504     "\C-c\C-r" gnus-summary-caesar-message
1505     "\M-t" gnus-summary-toggle-mime
1506     "f" gnus-summary-followup
1507     "F" gnus-summary-followup-with-original
1508     "C" gnus-summary-cancel-article
1509     "r" gnus-summary-reply
1510     "R" gnus-summary-reply-with-original
1511     "\C-c\C-f" gnus-summary-mail-forward
1512     "o" gnus-summary-save-article
1513     "\C-o" gnus-summary-save-article-mail
1514     "|" gnus-summary-pipe-output
1515     "\M-k" gnus-summary-edit-local-kill
1516     "\M-K" gnus-summary-edit-global-kill
1517     ;; "V" gnus-version
1518     "\C-c\C-d" gnus-summary-describe-group
1519     "q" gnus-summary-exit
1520     "Q" gnus-summary-exit-no-update
1521     "\C-c\C-i" gnus-info-find-node
1522     gnus-mouse-2 gnus-mouse-pick-article
1523     "m" gnus-summary-mail-other-window
1524     "a" gnus-summary-post-news
1525     "x" gnus-summary-limit-to-unread
1526     "s" gnus-summary-isearch-article
1527     "t" gnus-article-toggle-headers
1528     "g" gnus-summary-show-article
1529     "l" gnus-summary-goto-last-article
1530     "v" gnus-summary-preview-mime-message
1531     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1532     "\C-d" gnus-summary-enter-digest-group
1533     "\M-\C-d" gnus-summary-read-document
1534     "\M-\C-e" gnus-summary-edit-parameters
1535     "\M-\C-a" gnus-summary-customize-parameters
1536     "\C-c\C-b" gnus-bug
1537     "*" gnus-cache-enter-article
1538     "\M-*" gnus-cache-remove-article
1539     "\M-&" gnus-summary-universal-argument
1540     "\C-l" gnus-recenter
1541     "I" gnus-summary-increase-score
1542     "L" gnus-summary-lower-score
1543     "\M-i" gnus-symbolic-argument
1544     "h" gnus-summary-select-article-buffer
1545
1546     "V" gnus-summary-score-map
1547     "X" gnus-uu-extract-map
1548     "S" gnus-summary-send-map)
1549
1550   ;; Sort of orthogonal keymap
1551   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1552     "t" gnus-summary-tick-article-forward
1553     "!" gnus-summary-tick-article-forward
1554     "d" gnus-summary-mark-as-read-forward
1555     "r" gnus-summary-mark-as-read-forward
1556     "c" gnus-summary-clear-mark-forward
1557     " " gnus-summary-clear-mark-forward
1558     "e" gnus-summary-mark-as-expirable
1559     "x" gnus-summary-mark-as-expirable
1560     "?" gnus-summary-mark-as-dormant
1561     "b" gnus-summary-set-bookmark
1562     "B" gnus-summary-remove-bookmark
1563     "#" gnus-summary-mark-as-processable
1564     "\M-#" gnus-summary-unmark-as-processable
1565     "S" gnus-summary-limit-include-expunged
1566     "C" gnus-summary-catchup
1567     "H" gnus-summary-catchup-to-here
1568     "h" gnus-summary-catchup-from-here
1569     "\C-c" gnus-summary-catchup-all
1570     "k" gnus-summary-kill-same-subject-and-select
1571     "K" gnus-summary-kill-same-subject
1572     "P" gnus-uu-mark-map)
1573
1574   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1575     "c" gnus-summary-clear-above
1576     "u" gnus-summary-tick-above
1577     "m" gnus-summary-mark-above
1578     "k" gnus-summary-kill-below)
1579
1580   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1581     "/" gnus-summary-limit-to-subject
1582     "n" gnus-summary-limit-to-articles
1583     "w" gnus-summary-pop-limit
1584     "s" gnus-summary-limit-to-subject
1585     "a" gnus-summary-limit-to-author
1586     "u" gnus-summary-limit-to-unread
1587     "m" gnus-summary-limit-to-marks
1588     "M" gnus-summary-limit-exclude-marks
1589     "v" gnus-summary-limit-to-score
1590     "*" gnus-summary-limit-include-cached
1591     "D" gnus-summary-limit-include-dormant
1592     "T" gnus-summary-limit-include-thread
1593     "d" gnus-summary-limit-exclude-dormant
1594     "t" gnus-summary-limit-to-age
1595     "x" gnus-summary-limit-to-extra
1596     "E" gnus-summary-limit-include-expunged
1597     "c" gnus-summary-limit-exclude-childless-dormant
1598     "C" gnus-summary-limit-mark-excluded-as-read
1599     "o" gnus-summary-insert-old-articles
1600     "N" gnus-summary-insert-new-articles)
1601
1602   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1603     "n" gnus-summary-next-unread-article
1604     "p" gnus-summary-prev-unread-article
1605     "N" gnus-summary-next-article
1606     "P" gnus-summary-prev-article
1607     "\C-n" gnus-summary-next-same-subject
1608     "\C-p" gnus-summary-prev-same-subject
1609     "\M-n" gnus-summary-next-unread-subject
1610     "\M-p" gnus-summary-prev-unread-subject
1611     "f" gnus-summary-first-unread-article
1612     "b" gnus-summary-best-unread-article
1613     "j" gnus-summary-goto-article
1614     "g" gnus-summary-goto-subject
1615     "l" gnus-summary-goto-last-article
1616     "o" gnus-summary-pop-article)
1617
1618   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1619     "k" gnus-summary-kill-thread
1620     "l" gnus-summary-lower-thread
1621     "i" gnus-summary-raise-thread
1622     "T" gnus-summary-toggle-threads
1623     "t" gnus-summary-rethread-current
1624     "^" gnus-summary-reparent-thread
1625     "s" gnus-summary-show-thread
1626     "S" gnus-summary-show-all-threads
1627     "h" gnus-summary-hide-thread
1628     "H" gnus-summary-hide-all-threads
1629     "n" gnus-summary-next-thread
1630     "p" gnus-summary-prev-thread
1631     "u" gnus-summary-up-thread
1632     "o" gnus-summary-top-thread
1633     "d" gnus-summary-down-thread
1634     "#" gnus-uu-mark-thread
1635     "\M-#" gnus-uu-unmark-thread)
1636
1637   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1638     "g" gnus-summary-prepare
1639     "c" gnus-summary-insert-cached-articles)
1640
1641   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1642     "c" gnus-summary-catchup-and-exit
1643     "C" gnus-summary-catchup-all-and-exit
1644     "E" gnus-summary-exit-no-update
1645     "J" gnus-summary-jump-to-other-group
1646     "Q" gnus-summary-exit
1647     "Z" gnus-summary-exit
1648     "n" gnus-summary-catchup-and-goto-next-group
1649     "R" gnus-summary-reselect-current-group
1650     "G" gnus-summary-rescan-group
1651     "N" gnus-summary-next-group
1652     "s" gnus-summary-save-newsrc
1653     "P" gnus-summary-prev-group)
1654
1655   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1656     " " gnus-summary-next-page
1657     "n" gnus-summary-next-page
1658     "\177" gnus-summary-prev-page
1659     [delete] gnus-summary-prev-page
1660     "p" gnus-summary-prev-page
1661     "\r" gnus-summary-scroll-up
1662     "\M-\r" gnus-summary-scroll-down
1663     "<" gnus-summary-beginning-of-article
1664     ">" gnus-summary-end-of-article
1665     "b" gnus-summary-beginning-of-article
1666     "e" gnus-summary-end-of-article
1667     "^" gnus-summary-refer-parent-article
1668     "r" gnus-summary-refer-parent-article
1669     "D" gnus-summary-enter-digest-group
1670     "R" gnus-summary-refer-references
1671     "T" gnus-summary-refer-thread
1672     "g" gnus-summary-show-article
1673     "s" gnus-summary-isearch-article
1674     "P" gnus-summary-print-article
1675     "M" gnus-mailing-list-insinuate
1676     "t" gnus-article-babel)
1677
1678   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1679     "b" gnus-article-add-buttons
1680     "B" gnus-article-add-buttons-to-head
1681     "o" gnus-article-treat-overstrike
1682     "e" gnus-article-emphasize
1683     "w" gnus-article-fill-cited-article
1684     "Q" gnus-article-fill-long-lines
1685     "C" gnus-article-capitalize-sentences
1686     "c" gnus-article-remove-cr
1687     "Z" gnus-article-decode-HZ
1688     "f" gnus-article-display-x-face
1689     "l" gnus-summary-stop-page-breaking
1690     "r" gnus-summary-caesar-message
1691     "t" gnus-article-toggle-headers
1692     "v" gnus-summary-verbose-headers
1693     "m" gnus-summary-toggle-mime
1694     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1695     "p" gnus-article-verify-x-pgp-sig
1696     "d" gnus-article-treat-dumbquotes
1697     "s" gnus-smiley-display)
1698
1699   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1700     "a" gnus-article-hide
1701     "h" gnus-article-toggle-headers
1702     "b" gnus-article-hide-boring-headers
1703     "s" gnus-article-hide-signature
1704     "c" gnus-article-hide-citation
1705     "C" gnus-article-hide-citation-in-followups
1706     "l" gnus-article-hide-list-identifiers
1707     "p" gnus-article-hide-pgp
1708     "B" gnus-article-strip-banner
1709     "P" gnus-article-hide-pem
1710     "\C-c" gnus-article-hide-citation-maybe)
1711
1712   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1713     "a" gnus-article-highlight
1714     "h" gnus-article-highlight-headers
1715     "c" gnus-article-highlight-citation
1716     "s" gnus-article-highlight-signature)
1717
1718   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1719     "z" gnus-article-date-ut
1720     "u" gnus-article-date-ut
1721     "l" gnus-article-date-local
1722     "p" gnus-article-date-english
1723     "e" gnus-article-date-lapsed
1724     "o" gnus-article-date-original
1725     "i" gnus-article-date-iso8601
1726     "s" gnus-article-date-user)
1727
1728   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1729     "t" gnus-article-remove-trailing-blank-lines
1730     "l" gnus-article-strip-leading-blank-lines
1731     "m" gnus-article-strip-multiple-blank-lines
1732     "a" gnus-article-strip-blank-lines
1733     "A" gnus-article-strip-all-blank-lines
1734     "s" gnus-article-strip-leading-space
1735     "e" gnus-article-strip-trailing-space
1736     "w" gnus-article-remove-leading-whitespace)
1737
1738   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1739     "v" gnus-version
1740     "f" gnus-summary-fetch-faq
1741     "d" gnus-summary-describe-group
1742     "h" gnus-summary-describe-briefly
1743     "i" gnus-info-find-node)
1744
1745   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1746     "e" gnus-summary-expire-articles
1747     "\M-\C-e" gnus-summary-expire-articles-now
1748     "\177" gnus-summary-delete-article
1749     [delete] gnus-summary-delete-article
1750     [backspace] gnus-summary-delete-article
1751     "m" gnus-summary-move-article
1752     "r" gnus-summary-respool-article
1753     "w" gnus-summary-edit-article
1754     "c" gnus-summary-copy-article
1755     "B" gnus-summary-crosspost-article
1756     "q" gnus-summary-respool-query
1757     "t" gnus-summary-respool-trace
1758     "i" gnus-summary-import-article
1759     "I" gnus-summary-create-article
1760     "p" gnus-summary-article-posted-p)
1761
1762   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1763     "o" gnus-summary-save-article
1764     "m" gnus-summary-save-article-mail
1765     "F" gnus-summary-write-article-file
1766     "r" gnus-summary-save-article-rmail
1767     "f" gnus-summary-save-article-file
1768     "b" gnus-summary-save-article-body-file
1769     "h" gnus-summary-save-article-folder
1770     "v" gnus-summary-save-article-vm
1771     "p" gnus-summary-pipe-output
1772     "s" gnus-soup-add-article)
1773
1774   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1775     "b" gnus-summary-display-buttonized
1776     "m" gnus-summary-repair-multipart
1777     "v" gnus-article-view-part
1778     "o" gnus-article-save-part
1779     "c" gnus-article-copy-part
1780     "C" gnus-article-view-part-as-charset
1781     "e" gnus-article-externalize-part
1782     "E" gnus-article-encrypt-body
1783     "i" gnus-article-inline-part
1784     "|" gnus-article-pipe-part))
1785
1786 (defvar gnus-article-post-menu nil)
1787
1788 (defun gnus-summary-make-menu-bar ()
1789   (gnus-turn-off-edit-menu 'summary)
1790
1791   (unless (boundp 'gnus-summary-misc-menu)
1792
1793     (easy-menu-define
1794      gnus-summary-kill-menu gnus-summary-mode-map ""
1795      (cons
1796       "Score"
1797       (nconc
1798        (list
1799         ["Customize" gnus-score-customize t])
1800        (gnus-make-score-map 'increase)
1801        (gnus-make-score-map 'lower)
1802        '(("Mark"
1803           ["Kill below" gnus-summary-kill-below t]
1804           ["Mark above" gnus-summary-mark-above t]
1805           ["Tick above" gnus-summary-tick-above t]
1806           ["Clear above" gnus-summary-clear-above t])
1807          ["Current score" gnus-summary-current-score t]
1808          ["Set score" gnus-summary-set-score t]
1809          ["Switch current score file..." gnus-score-change-score-file t]
1810          ["Set mark below..." gnus-score-set-mark-below t]
1811          ["Set expunge below..." gnus-score-set-expunge-below t]
1812          ["Edit current score file" gnus-score-edit-current-scores t]
1813          ["Edit score file" gnus-score-edit-file t]
1814          ["Trace score" gnus-score-find-trace t]
1815          ["Find words" gnus-score-find-favourite-words t]
1816          ["Rescore buffer" gnus-summary-rescore t]
1817          ["Increase score..." gnus-summary-increase-score t]
1818          ["Lower score..." gnus-summary-lower-score t]))))
1819
1820     ;; Define both the Article menu in the summary buffer and the equivalent
1821     ;; Commands menu in the article buffer here for consistency.
1822     (let ((innards
1823            `(("Hide"
1824               ["All" gnus-article-hide t]
1825               ["Headers" gnus-article-toggle-headers t]
1826               ["Signature" gnus-article-hide-signature t]
1827               ["Citation" gnus-article-hide-citation t]
1828               ["List identifiers" gnus-article-hide-list-identifiers t]
1829               ["PGP" gnus-article-hide-pgp t]
1830               ["Banner" gnus-article-strip-banner t]
1831               ["Boring headers" gnus-article-hide-boring-headers t])
1832              ("Highlight"
1833               ["All" gnus-article-highlight t]
1834               ["Headers" gnus-article-highlight-headers t]
1835               ["Signature" gnus-article-highlight-signature t]
1836               ["Citation" gnus-article-highlight-citation t])
1837              ("Date"
1838               ["Local" gnus-article-date-local t]
1839               ["ISO8601" gnus-article-date-iso8601 t]
1840               ["UT" gnus-article-date-ut t]
1841               ["Original" gnus-article-date-original t]
1842               ["Lapsed" gnus-article-date-lapsed t]
1843               ["User-defined" gnus-article-date-user t])
1844              ("Washing"
1845               ("Remove Blanks"
1846                ["Leading" gnus-article-strip-leading-blank-lines t]
1847                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1848                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1849                ["All of the above" gnus-article-strip-blank-lines t]
1850                ["All" gnus-article-strip-all-blank-lines t]
1851                ["Leading space" gnus-article-strip-leading-space t]
1852                ["Trailing space" gnus-article-strip-trailing-space t]
1853                ["Leading space in headers"
1854                 gnus-article-remove-leading-whitespace t])
1855               ["Overstrike" gnus-article-treat-overstrike t]
1856               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1857               ["Emphasis" gnus-article-emphasize t]
1858               ["Word wrap" gnus-article-fill-cited-article t]
1859               ["Fill long lines" gnus-article-fill-long-lines t]
1860               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1861               ["CR" gnus-article-remove-cr t]
1862               ["Show X-Face" gnus-article-display-x-face t]
1863               ["Rot 13" gnus-summary-caesar-message
1864                ,@(if (featurep 'xemacs) '(t)
1865                    '(:help "\"Caesar rotate\" article by 13"))]
1866               ["Unix pipe" gnus-summary-pipe-message t]
1867               ["Add buttons" gnus-article-add-buttons t]
1868               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1869               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1870               ["Toggle MIME" gnus-summary-toggle-mime t]
1871               ["Verbose header" gnus-summary-verbose-headers t]
1872               ["Toggle header" gnus-summary-toggle-header t]
1873               ["Toggle smileys" gnus-smiley-display t]
1874               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1875               ["HZ" gnus-article-decode-HZ t])
1876              ("Output"
1877               ["Save in default format" gnus-summary-save-article
1878                ,@(if (featurep 'xemacs) '(t)
1879                    '(:help "Save article using default method"))]
1880               ["Save in file" gnus-summary-save-article-file
1881                ,@(if (featurep 'xemacs) '(t)
1882                    '(:help "Save article in file"))]
1883               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1884               ["Save in MH folder" gnus-summary-save-article-folder t]
1885               ["Save in VM folder" gnus-summary-save-article-vm t]
1886               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1887               ["Save body in file" gnus-summary-save-article-body-file t]
1888               ["Pipe through a filter" gnus-summary-pipe-output t]
1889               ["Add to SOUP packet" gnus-soup-add-article t]
1890               ["Print" gnus-summary-print-article t])
1891              ("Backend"
1892               ["Respool article..." gnus-summary-respool-article t]
1893               ["Move article..." gnus-summary-move-article
1894                (gnus-check-backend-function
1895                 'request-move-article gnus-newsgroup-name)]
1896               ["Copy article..." gnus-summary-copy-article t]
1897               ["Crosspost article..." gnus-summary-crosspost-article
1898                (gnus-check-backend-function
1899                 'request-replace-article gnus-newsgroup-name)]
1900               ["Import file..." gnus-summary-import-article t]
1901               ["Create article..." gnus-summary-create-article t]
1902               ["Check if posted" gnus-summary-article-posted-p t]
1903               ["Edit article" gnus-summary-edit-article
1904                (not (gnus-group-read-only-p))]
1905               ["Delete article" gnus-summary-delete-article
1906                (gnus-check-backend-function
1907                 'request-expire-articles gnus-newsgroup-name)]
1908               ["Query respool" gnus-summary-respool-query t]
1909               ["Trace respool" gnus-summary-respool-trace t]
1910               ["Delete expirable articles" gnus-summary-expire-articles-now
1911                (gnus-check-backend-function
1912                 'request-expire-articles gnus-newsgroup-name)])
1913              ("Extract"
1914               ["Uudecode" gnus-uu-decode-uu
1915                ,@(if (featurep 'xemacs) '(t)
1916                    '(:help "Decode uuencoded article(s)"))]
1917               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1918               ["Unshar" gnus-uu-decode-unshar t]
1919               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1920               ["Save" gnus-uu-decode-save t]
1921               ["Binhex" gnus-uu-decode-binhex t]
1922               ["Postscript" gnus-uu-decode-postscript t])
1923              ("Cache"
1924               ["Enter article" gnus-cache-enter-article t]
1925               ["Remove article" gnus-cache-remove-article t])
1926              ["Translate" gnus-article-babel t]
1927              ["Select article buffer" gnus-summary-select-article-buffer t]
1928              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1929              ["Isearch article..." gnus-summary-isearch-article t]
1930              ["Beginning of the article" gnus-summary-beginning-of-article t]
1931              ["End of the article" gnus-summary-end-of-article t]
1932              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1933              ["Fetch referenced articles" gnus-summary-refer-references t]
1934              ["Fetch current thread" gnus-summary-refer-thread t]
1935              ["Fetch article with id..." gnus-summary-refer-article t]
1936              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1937              ["Redisplay" gnus-summary-show-article t]
1938              ["Raw article" gnus-summary-show-raw-article t])))
1939       (easy-menu-define
1940        gnus-summary-article-menu gnus-summary-mode-map ""
1941        (cons "Article" innards))
1942
1943       (if (not (keymapp gnus-summary-article-menu))
1944           (easy-menu-define
1945            gnus-article-commands-menu gnus-article-mode-map ""
1946            (cons "Commands" innards))
1947         ;; in Emacs, don't share menu.
1948         (setq gnus-article-commands-menu
1949               (copy-keymap gnus-summary-article-menu))
1950         (define-key gnus-article-mode-map [menu-bar commands]
1951           (cons "Commands" gnus-article-commands-menu))))
1952
1953     (easy-menu-define
1954      gnus-summary-thread-menu gnus-summary-mode-map ""
1955      '("Threads"
1956        ["Toggle threading" gnus-summary-toggle-threads t]
1957        ["Hide threads" gnus-summary-hide-all-threads t]
1958        ["Show threads" gnus-summary-show-all-threads t]
1959        ["Hide thread" gnus-summary-hide-thread t]
1960        ["Show thread" gnus-summary-show-thread t]
1961        ["Go to next thread" gnus-summary-next-thread t]
1962        ["Go to previous thread" gnus-summary-prev-thread t]
1963        ["Go down thread" gnus-summary-down-thread t]
1964        ["Go up thread" gnus-summary-up-thread t]
1965        ["Top of thread" gnus-summary-top-thread t]
1966        ["Mark thread as read" gnus-summary-kill-thread t]
1967        ["Lower thread score" gnus-summary-lower-thread t]
1968        ["Raise thread score" gnus-summary-raise-thread t]
1969        ["Rethread current" gnus-summary-rethread-current t]))
1970
1971     (easy-menu-define
1972      gnus-summary-post-menu gnus-summary-mode-map ""
1973      `("Post"
1974        ["Post an article" gnus-summary-post-news
1975         ,@(if (featurep 'xemacs) '(t)
1976             '(:help "Post an article"))]
1977        ["Followup" gnus-summary-followup
1978         ,@(if (featurep 'xemacs) '(t)
1979             '(:help "Post followup to this article"))]
1980        ["Followup and yank" gnus-summary-followup-with-original
1981         ,@(if (featurep 'xemacs) '(t)
1982             '(:help "Post followup to this article, quoting its contents"))]
1983        ["Supersede article" gnus-summary-supersede-article t]
1984        ["Cancel article" gnus-summary-cancel-article
1985         ,@(if (featurep 'xemacs) '(t)
1986             '(:help "Cancel an article you posted"))]
1987        ["Reply" gnus-summary-reply t]
1988        ["Reply and yank" gnus-summary-reply-with-original t]
1989        ["Wide reply" gnus-summary-wide-reply t]
1990        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1991         ,@(if (featurep 'xemacs) '(t)
1992             '(:help "Mail a reply, quoting this article"))]
1993        ["Mail forward" gnus-summary-mail-forward t]
1994        ["Post forward" gnus-summary-post-forward t]
1995        ["Digest and mail" gnus-summary-digest-mail-forward t]
1996        ["Digest and post" gnus-summary-digest-post-forward t]
1997        ["Resend message" gnus-summary-resend-message t]
1998        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1999        ["Send a mail" gnus-summary-mail-other-window t]
2000        ["Uuencode and post" gnus-uu-post-news
2001         ,@(if (featurep 'xemacs) '(t)
2002             '(:help "Post a uuencoded article"))]
2003        ["Followup via news" gnus-summary-followup-to-mail t]
2004        ["Followup via news and yank"
2005         gnus-summary-followup-to-mail-with-original t]
2006        ;;("Draft"
2007        ;;["Send" gnus-summary-send-draft t]
2008        ;;["Send bounced" gnus-resend-bounced-mail t])
2009        ))
2010
2011     (cond
2012      ((not (keymapp gnus-summary-post-menu))
2013       (setq gnus-article-post-menu gnus-summary-post-menu))
2014      ((not gnus-article-post-menu)
2015       ;; Don't share post menu.
2016       (setq gnus-article-post-menu
2017             (copy-keymap gnus-summary-post-menu))))
2018     (define-key gnus-article-mode-map [menu-bar post]
2019       (cons "Post" gnus-article-post-menu))
2020
2021     (easy-menu-define
2022      gnus-summary-misc-menu gnus-summary-mode-map ""
2023      `("Misc"
2024        ("Mark Read"
2025         ["Mark as read" gnus-summary-mark-as-read-forward t]
2026         ["Mark same subject and select"
2027          gnus-summary-kill-same-subject-and-select t]
2028         ["Mark same subject" gnus-summary-kill-same-subject t]
2029         ["Catchup" gnus-summary-catchup
2030          ,@(if (featurep 'xemacs) '(t)
2031              '(:help "Mark unread articles in this group as read"))]
2032         ["Catchup all" gnus-summary-catchup-all t]
2033         ["Catchup to here" gnus-summary-catchup-to-here t]
2034         ["Catchup from here" gnus-summary-catchup-from-here t]
2035         ["Catchup region" gnus-summary-mark-region-as-read t]
2036         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2037        ("Mark Various"
2038         ["Tick" gnus-summary-tick-article-forward t]
2039         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2040         ["Remove marks" gnus-summary-clear-mark-forward t]
2041         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2042         ["Set bookmark" gnus-summary-set-bookmark t]
2043         ["Remove bookmark" gnus-summary-remove-bookmark t])
2044        ("Mark Limit"
2045         ["Marks..." gnus-summary-limit-to-marks t]
2046         ["Subject..." gnus-summary-limit-to-subject t]
2047         ["Author..." gnus-summary-limit-to-author t]
2048         ["Age..." gnus-summary-limit-to-age t]
2049         ["Extra..." gnus-summary-limit-to-extra t]
2050         ["Score" gnus-summary-limit-to-score t]
2051         ["Unread" gnus-summary-limit-to-unread t]
2052         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2053         ["Articles" gnus-summary-limit-to-articles t]
2054         ["Pop limit" gnus-summary-pop-limit t]
2055         ["Show dormant" gnus-summary-limit-include-dormant t]
2056         ["Hide childless dormant"
2057          gnus-summary-limit-exclude-childless-dormant t]
2058         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2059         ["Hide marked" gnus-summary-limit-exclude-marks t]
2060         ["Show expunged" gnus-summary-limit-include-expunged t])
2061        ("Process Mark"
2062         ["Set mark" gnus-summary-mark-as-processable t]
2063         ["Remove mark" gnus-summary-unmark-as-processable t]
2064         ["Remove all marks" gnus-summary-unmark-all-processable t]
2065         ["Mark above" gnus-uu-mark-over t]
2066         ["Mark series" gnus-uu-mark-series t]
2067         ["Mark region" gnus-uu-mark-region t]
2068         ["Unmark region" gnus-uu-unmark-region t]
2069         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2070         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2071         ["Mark all" gnus-uu-mark-all t]
2072         ["Mark buffer" gnus-uu-mark-buffer t]
2073         ["Mark sparse" gnus-uu-mark-sparse t]
2074         ["Mark thread" gnus-uu-mark-thread t]
2075         ["Unmark thread" gnus-uu-unmark-thread t]
2076         ("Process Mark Sets"
2077          ["Kill" gnus-summary-kill-process-mark t]
2078          ["Yank" gnus-summary-yank-process-mark
2079           gnus-newsgroup-process-stack]
2080          ["Save" gnus-summary-save-process-mark t]))
2081        ("Scroll article"
2082         ["Page forward" gnus-summary-next-page
2083          ,@(if (featurep 'xemacs) '(t)
2084              '(:help "Show next page of article"))]
2085         ["Page backward" gnus-summary-prev-page
2086          ,@(if (featurep 'xemacs) '(t)
2087              '(:help "Show previous page of article"))]
2088         ["Line forward" gnus-summary-scroll-up t])
2089        ("Move"
2090         ["Next unread article" gnus-summary-next-unread-article t]
2091         ["Previous unread article" gnus-summary-prev-unread-article t]
2092         ["Next article" gnus-summary-next-article t]
2093         ["Previous article" gnus-summary-prev-article t]
2094         ["Next unread subject" gnus-summary-next-unread-subject t]
2095         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2096         ["Next article same subject" gnus-summary-next-same-subject t]
2097         ["Previous article same subject" gnus-summary-prev-same-subject t]
2098         ["First unread article" gnus-summary-first-unread-article t]
2099         ["Best unread article" gnus-summary-best-unread-article t]
2100         ["Go to subject number..." gnus-summary-goto-subject t]
2101         ["Go to article number..." gnus-summary-goto-article t]
2102         ["Go to the last article" gnus-summary-goto-last-article t]
2103         ["Pop article off history" gnus-summary-pop-article t])
2104        ("Sort"
2105         ["Sort by number" gnus-summary-sort-by-number t]
2106         ["Sort by author" gnus-summary-sort-by-author t]
2107         ["Sort by subject" gnus-summary-sort-by-subject t]
2108         ["Sort by date" gnus-summary-sort-by-date t]
2109         ["Sort by score" gnus-summary-sort-by-score t]
2110         ["Sort by lines" gnus-summary-sort-by-lines t]
2111         ["Sort by characters" gnus-summary-sort-by-chars t]
2112         ["Original sort" gnus-summary-sort-by-original t])
2113        ("Help"
2114         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2115         ["Describe group" gnus-summary-describe-group t]
2116         ["Read manual" gnus-info-find-node t])
2117        ("Modes"
2118         ["Pick and read" gnus-pick-mode t]
2119         ["Binary" gnus-binary-mode t])
2120        ("Regeneration"
2121         ["Regenerate" gnus-summary-prepare t]
2122         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2123         ["Toggle threading" gnus-summary-toggle-threads t])
2124        ["See old articles" gnus-summary-insert-old-articles t]
2125        ["See new articles" gnus-summary-insert-new-articles t]
2126        ["Filter articles..." gnus-summary-execute-command t]
2127        ["Run command on subjects..." gnus-summary-universal-argument t]
2128        ["Search articles forward..." gnus-summary-search-article-forward t]
2129        ["Search articles backward..." gnus-summary-search-article-backward t]
2130        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2131        ["Expand window" gnus-summary-expand-window t]
2132        ["Expire expirable articles" gnus-summary-expire-articles
2133         (gnus-check-backend-function
2134          'request-expire-articles gnus-newsgroup-name)]
2135        ["Edit local kill file" gnus-summary-edit-local-kill t]
2136        ["Edit main kill file" gnus-summary-edit-global-kill t]
2137        ["Edit group parameters" gnus-summary-edit-parameters t]
2138        ["Customize group parameters" gnus-summary-customize-parameters t]
2139        ["Send a bug report" gnus-bug t]
2140        ("Exit"
2141         ["Catchup and exit" gnus-summary-catchup-and-exit
2142          ,@(if (featurep 'xemacs) '(t)
2143              '(:help "Mark unread articles in this group as read, then exit"))]
2144         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2145         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2146         ["Exit group" gnus-summary-exit
2147          ,@(if (featurep 'xemacs) '(t)
2148              '(:help "Exit current group, return to group selection mode"))]
2149         ["Exit group without updating" gnus-summary-exit-no-update t]
2150         ["Exit and goto next group" gnus-summary-next-group t]
2151         ["Exit and goto prev group" gnus-summary-prev-group t]
2152         ["Reselect group" gnus-summary-reselect-current-group t]
2153         ["Rescan group" gnus-summary-rescan-group t]
2154         ["Update dribble" gnus-summary-save-newsrc t])))
2155
2156     (gnus-run-hooks 'gnus-summary-menu-hook)))
2157
2158 (defvar gnus-summary-tool-bar-map nil)
2159
2160 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2161 (defun gnus-summary-make-tool-bar ()
2162   (if (and (fboundp 'tool-bar-add-item-from-menu)
2163            (default-value 'tool-bar-mode)
2164            (not gnus-summary-tool-bar-map))
2165       (setq gnus-summary-tool-bar-map
2166             (let ((tool-bar-map (make-sparse-keymap))
2167                   (load-path (mm-image-load-path)))
2168               (tool-bar-add-item-from-menu
2169                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2170               (tool-bar-add-item-from-menu
2171                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2172               (tool-bar-add-item-from-menu
2173                'gnus-summary-post-news "post" gnus-summary-mode-map)
2174               (tool-bar-add-item-from-menu
2175                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2176               (tool-bar-add-item-from-menu
2177                'gnus-summary-followup "followup" gnus-summary-mode-map)
2178               (tool-bar-add-item-from-menu
2179                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2180               (tool-bar-add-item-from-menu
2181                'gnus-summary-reply "reply" gnus-summary-mode-map)
2182               (tool-bar-add-item-from-menu
2183                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2184               (tool-bar-add-item-from-menu
2185                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2186               (tool-bar-add-item-from-menu
2187                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2188               (tool-bar-add-item-from-menu
2189                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2190               (tool-bar-add-item-from-menu
2191                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2192               (tool-bar-add-item-from-menu
2193                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2194               (tool-bar-add-item-from-menu
2195                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2196               (tool-bar-add-item-from-menu
2197                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2198               tool-bar-map)))
2199   (if gnus-summary-tool-bar-map
2200       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2201
2202 (defun gnus-score-set-default (var value)
2203   "A version of set that updates the GNU Emacs menu-bar."
2204   (set var value)
2205   ;; It is the message that forces the active status to be updated.
2206   (message ""))
2207
2208 (defun gnus-make-score-map (type)
2209   "Make a summary score map of type TYPE."
2210   (if t
2211       nil
2212     (let ((headers '(("author" "from" string)
2213                      ("subject" "subject" string)
2214                      ("article body" "body" string)
2215                      ("article head" "head" string)
2216                      ("xref" "xref" string)
2217                      ("extra header" "extra" string)
2218                      ("lines" "lines" number)
2219                      ("followups to author" "followup" string)))
2220           (types '((number ("less than" <)
2221                            ("greater than" >)
2222                            ("equal" =))
2223                    (string ("substring" s)
2224                            ("exact string" e)
2225                            ("fuzzy string" f)
2226                            ("regexp" r))))
2227           (perms '(("temporary" (current-time-string))
2228                    ("permanent" nil)
2229                    ("immediate" now)))
2230           header)
2231       (list
2232        (apply
2233         'nconc
2234         (list
2235          (if (eq type 'lower)
2236              "Lower score"
2237            "Increase score"))
2238         (let (outh)
2239           (while headers
2240             (setq header (car headers))
2241             (setq outh
2242                   (cons
2243                    (apply
2244                     'nconc
2245                     (list (car header))
2246                     (let ((ts (cdr (assoc (nth 2 header) types)))
2247                           outt)
2248                       (while ts
2249                         (setq outt
2250                               (cons
2251                                (apply
2252                                 'nconc
2253                                 (list (caar ts))
2254                                 (let ((ps perms)
2255                                       outp)
2256                                   (while ps
2257                                     (setq outp
2258                                           (cons
2259                                            (vector
2260                                             (caar ps)
2261                                             (list
2262                                              'gnus-summary-score-entry
2263                                              (nth 1 header)
2264                                              (if (or (string= (nth 1 header)
2265                                                               "head")
2266                                                      (string= (nth 1 header)
2267                                                               "body"))
2268                                                  ""
2269                                                (list 'gnus-summary-header
2270                                                      (nth 1 header)))
2271                                              (list 'quote (nth 1 (car ts)))
2272                                              (list 'gnus-score-delta-default
2273                                                    nil)
2274                                              (nth 1 (car ps))
2275                                              t)
2276                                             t)
2277                                            outp))
2278                                     (setq ps (cdr ps)))
2279                                   (list (nreverse outp))))
2280                                outt))
2281                         (setq ts (cdr ts)))
2282                       (list (nreverse outt))))
2283                    outh))
2284             (setq headers (cdr headers)))
2285           (list (nreverse outh))))))))
2286
2287 \f
2288
2289 (defun gnus-summary-mode (&optional group)
2290   "Major mode for reading articles.
2291
2292 All normal editing commands are switched off.
2293 \\<gnus-summary-mode-map>
2294 Each line in this buffer represents one article.  To read an
2295 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2296 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2297 respectively.
2298
2299 You can also post articles and send mail from this buffer.  To
2300 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2301 of an article, type `\\[gnus-summary-reply]'.
2302
2303 There are approx. one gazillion commands you can execute in this
2304 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2305
2306 The following commands are available:
2307
2308 \\{gnus-summary-mode-map}"
2309   (interactive)
2310   (kill-all-local-variables)
2311   (when (gnus-visual-p 'summary-menu 'menu)
2312     (gnus-summary-make-menu-bar)
2313     (gnus-summary-make-tool-bar))
2314   (gnus-summary-make-local-variables)
2315   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2316     (gnus-summary-make-local-variables))
2317   (gnus-make-thread-indent-array)
2318   (gnus-simplify-mode-line)
2319   (setq major-mode 'gnus-summary-mode)
2320   (setq mode-name "Summary")
2321   (make-local-variable 'minor-mode-alist)
2322   (use-local-map gnus-summary-mode-map)
2323   (buffer-disable-undo)
2324   (setq buffer-read-only t)             ;Disable modification
2325   (setq truncate-lines t)
2326   (setq selective-display t)
2327   (setq selective-display-ellipses t)   ;Display `...'
2328   (gnus-summary-set-display-table)
2329   (gnus-set-default-directory)
2330   (setq gnus-newsgroup-name group)
2331   (unless (gnus-news-group-p group)
2332     (setq gnus-newsgroup-incorporated
2333           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2334   (make-local-variable 'gnus-summary-line-format)
2335   (make-local-variable 'gnus-summary-line-format-spec)
2336   (make-local-variable 'gnus-summary-dummy-line-format)
2337   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2338   (make-local-variable 'gnus-summary-mark-positions)
2339   (make-local-hook 'pre-command-hook)
2340   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2341   (gnus-run-hooks 'gnus-summary-mode-hook)
2342   (turn-on-gnus-mailing-list-mode)
2343   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2344   (gnus-update-summary-mark-positions))
2345
2346 (defun gnus-summary-make-local-variables ()
2347   "Make all the local summary buffer variables."
2348   (let (global)
2349     (dolist (local gnus-summary-local-variables)
2350       (if (consp local)
2351           (progn
2352             (if (eq (cdr local) 'global)
2353                 ;; Copy the global value of the variable.
2354                 (setq global (symbol-value (car local)))
2355               ;; Use the value from the list.
2356               (setq global (eval (cdr local))))
2357             (set (make-local-variable (car local)) global))
2358         ;; Simple nil-valued local variable.
2359         (set (make-local-variable local) nil)))))
2360
2361 (defun gnus-summary-clear-local-variables ()
2362   (let ((locals gnus-summary-local-variables))
2363     (while locals
2364       (if (consp (car locals))
2365           (and (vectorp (caar locals))
2366                (set (caar locals) nil))
2367         (and (vectorp (car locals))
2368              (set (car locals) nil)))
2369       (setq locals (cdr locals)))))
2370
2371 ;; Summary data functions.
2372
2373 (defmacro gnus-data-number (data)
2374   `(car ,data))
2375
2376 (defmacro gnus-data-set-number (data number)
2377   `(setcar ,data ,number))
2378
2379 (defmacro gnus-data-mark (data)
2380   `(nth 1 ,data))
2381
2382 (defmacro gnus-data-set-mark (data mark)
2383   `(setcar (nthcdr 1 ,data) ,mark))
2384
2385 (defmacro gnus-data-pos (data)
2386   `(nth 2 ,data))
2387
2388 (defmacro gnus-data-set-pos (data pos)
2389   `(setcar (nthcdr 2 ,data) ,pos))
2390
2391 (defmacro gnus-data-header (data)
2392   `(nth 3 ,data))
2393
2394 (defmacro gnus-data-set-header (data header)
2395   `(setcar (nthcdr 3 ,data) ,header))
2396
2397 (defmacro gnus-data-level (data)
2398   `(nth 4 ,data))
2399
2400 (defmacro gnus-data-unread-p (data)
2401   `(= (nth 1 ,data) gnus-unread-mark))
2402
2403 (defmacro gnus-data-read-p (data)
2404   `(/= (nth 1 ,data) gnus-unread-mark))
2405
2406 (defmacro gnus-data-pseudo-p (data)
2407   `(consp (nth 3 ,data)))
2408
2409 (defmacro gnus-data-find (number)
2410   `(assq ,number gnus-newsgroup-data))
2411
2412 (defmacro gnus-data-find-list (number &optional data)
2413   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2414      (memq (assq ,number bdata)
2415            bdata)))
2416
2417 (defmacro gnus-data-make (number mark pos header level)
2418   `(list ,number ,mark ,pos ,header ,level))
2419
2420 (defun gnus-data-enter (after-article number mark pos header level offset)
2421   (let ((data (gnus-data-find-list after-article)))
2422     (unless data
2423       (error "No such article: %d" after-article))
2424     (setcdr data (cons (gnus-data-make number mark pos header level)
2425                        (cdr data)))
2426     (setq gnus-newsgroup-data-reverse nil)
2427     (gnus-data-update-list (cddr data) offset)))
2428
2429 (defun gnus-data-enter-list (after-article list &optional offset)
2430   (when list
2431     (let ((data (and after-article (gnus-data-find-list after-article)))
2432           (ilist list))
2433       (if (not (or data
2434                    after-article))
2435           (let ((odata gnus-newsgroup-data))
2436             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2437             (when offset
2438               (gnus-data-update-list odata offset)))
2439         ;; Find the last element in the list to be spliced into the main
2440         ;; list.
2441         (while (cdr list)
2442           (setq list (cdr list)))
2443         (if (not data)
2444             (progn
2445               (setcdr list gnus-newsgroup-data)
2446               (setq gnus-newsgroup-data ilist)
2447               (when offset
2448                 (gnus-data-update-list (cdr list) offset)))
2449           (setcdr list (cdr data))
2450           (setcdr data ilist)
2451           (when offset
2452             (gnus-data-update-list (cdr list) offset))))
2453       (setq gnus-newsgroup-data-reverse nil))))
2454
2455 (defun gnus-data-remove (article &optional offset)
2456   (let ((data gnus-newsgroup-data))
2457     (if (= (gnus-data-number (car data)) article)
2458         (progn
2459           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2460                 gnus-newsgroup-data-reverse nil)
2461           (when offset
2462             (gnus-data-update-list gnus-newsgroup-data offset)))
2463       (while (cdr data)
2464         (when (= (gnus-data-number (cadr data)) article)
2465           (setcdr data (cddr data))
2466           (when offset
2467             (gnus-data-update-list (cdr data) offset))
2468           (setq data nil
2469                 gnus-newsgroup-data-reverse nil))
2470         (setq data (cdr data))))))
2471
2472 (defmacro gnus-data-list (backward)
2473   `(if ,backward
2474        (or gnus-newsgroup-data-reverse
2475            (setq gnus-newsgroup-data-reverse
2476                  (reverse gnus-newsgroup-data)))
2477      gnus-newsgroup-data))
2478
2479 (defun gnus-data-update-list (data offset)
2480   "Add OFFSET to the POS of all data entries in DATA."
2481   (setq gnus-newsgroup-data-reverse nil)
2482   (while data
2483     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2484     (setq data (cdr data))))
2485
2486 (defun gnus-summary-article-pseudo-p (article)
2487   "Say whether this article is a pseudo article or not."
2488   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2489
2490 (defmacro gnus-summary-article-sparse-p (article)
2491   "Say whether this article is a sparse article or not."
2492   `(memq ,article gnus-newsgroup-sparse))
2493
2494 (defmacro gnus-summary-article-ancient-p (article)
2495   "Say whether this article is a sparse article or not."
2496   `(memq ,article gnus-newsgroup-ancient))
2497
2498 (defun gnus-article-parent-p (number)
2499   "Say whether this article is a parent or not."
2500   (let ((data (gnus-data-find-list number)))
2501     (and (cdr data)                     ; There has to be an article after...
2502          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2503             (gnus-data-level (nth 1 data))))))
2504
2505 (defun gnus-article-children (number)
2506   "Return a list of all children to NUMBER."
2507   (let* ((data (gnus-data-find-list number))
2508          (level (gnus-data-level (car data)))
2509          children)
2510     (setq data (cdr data))
2511     (while (and data
2512                 (= (gnus-data-level (car data)) (1+ level)))
2513       (push (gnus-data-number (car data)) children)
2514       (setq data (cdr data)))
2515     children))
2516
2517 (defmacro gnus-summary-skip-intangible ()
2518   "If the current article is intangible, then jump to a different article."
2519   '(let ((to (get-text-property (point) 'gnus-intangible)))
2520      (and to (gnus-summary-goto-subject to))))
2521
2522 (defmacro gnus-summary-article-intangible-p ()
2523   "Say whether this article is intangible or not."
2524   '(get-text-property (point) 'gnus-intangible))
2525
2526 (defun gnus-article-read-p (article)
2527   "Say whether ARTICLE is read or not."
2528   (not (or (memq article gnus-newsgroup-marked)
2529            (memq article gnus-newsgroup-unreads)
2530            (memq article gnus-newsgroup-unselected)
2531            (memq article gnus-newsgroup-dormant))))
2532
2533 ;; Some summary mode macros.
2534
2535 (defmacro gnus-summary-article-number ()
2536   "The article number of the article on the current line.
2537 If there isn's an article number here, then we return the current
2538 article number."
2539   '(progn
2540      (gnus-summary-skip-intangible)
2541      (or (get-text-property (point) 'gnus-number)
2542          (gnus-summary-last-subject))))
2543
2544 (defmacro gnus-summary-article-header (&optional number)
2545   "Return the header of article NUMBER."
2546   `(gnus-data-header (gnus-data-find
2547                       ,(or number '(gnus-summary-article-number)))))
2548
2549 (defmacro gnus-summary-thread-level (&optional number)
2550   "Return the level of thread that starts with article NUMBER."
2551   `(if (and (eq gnus-summary-make-false-root 'dummy)
2552             (get-text-property (point) 'gnus-intangible))
2553        0
2554      (gnus-data-level (gnus-data-find
2555                        ,(or number '(gnus-summary-article-number))))))
2556
2557 (defmacro gnus-summary-article-mark (&optional number)
2558   "Return the mark of article NUMBER."
2559   `(gnus-data-mark (gnus-data-find
2560                     ,(or number '(gnus-summary-article-number)))))
2561
2562 (defmacro gnus-summary-article-pos (&optional number)
2563   "Return the position of the line of article NUMBER."
2564   `(gnus-data-pos (gnus-data-find
2565                    ,(or number '(gnus-summary-article-number)))))
2566
2567 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2568 (defmacro gnus-summary-article-subject (&optional number)
2569   "Return current subject string or nil if nothing."
2570   `(let ((headers
2571           ,(if number
2572                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2573              '(gnus-data-header (assq (gnus-summary-article-number)
2574                                       gnus-newsgroup-data)))))
2575      (and headers
2576           (vectorp headers)
2577           (mail-header-subject headers))))
2578
2579 (defmacro gnus-summary-article-score (&optional number)
2580   "Return current article score."
2581   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2582                   gnus-newsgroup-scored))
2583        gnus-summary-default-score 0))
2584
2585 (defun gnus-summary-article-children (&optional number)
2586   "Return a list of article numbers that are children of article NUMBER."
2587   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2588          (level (gnus-data-level (car data)))
2589          l children)
2590     (while (and (setq data (cdr data))
2591                 (> (setq l (gnus-data-level (car data))) level))
2592       (and (= (1+ level) l)
2593            (push (gnus-data-number (car data))
2594                  children)))
2595     (nreverse children)))
2596
2597 (defun gnus-summary-article-parent (&optional number)
2598   "Return the article number of the parent of article NUMBER."
2599   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2600                                     (gnus-data-list t)))
2601          (level (gnus-data-level (car data))))
2602     (if (zerop level)
2603         ()                              ; This is a root.
2604       ;; We search until we find an article with a level less than
2605       ;; this one.  That function has to be the parent.
2606       (while (and (setq data (cdr data))
2607                   (not (< (gnus-data-level (car data)) level))))
2608       (and data (gnus-data-number (car data))))))
2609
2610 (defun gnus-unread-mark-p (mark)
2611   "Say whether MARK is the unread mark."
2612   (= mark gnus-unread-mark))
2613
2614 (defun gnus-read-mark-p (mark)
2615   "Say whether MARK is one of the marks that mark as read.
2616 This is all marks except unread, ticked, dormant, and expirable."
2617   (not (or (= mark gnus-unread-mark)
2618            (= mark gnus-ticked-mark)
2619            (= mark gnus-dormant-mark)
2620            (= mark gnus-expirable-mark))))
2621
2622 (defmacro gnus-article-mark (number)
2623   "Return the MARK of article NUMBER.
2624 This macro should only be used when computing the mark the \"first\"
2625 time; i.e., when generating the summary lines.  After that,
2626 `gnus-summary-article-mark' should be used to examine the
2627 marks of articles."
2628   `(cond
2629     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2630     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2631     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2632     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2633     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2634     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2635     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2636     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2637            gnus-ancient-mark))))
2638
2639 ;; Saving hidden threads.
2640
2641 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2642 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2643
2644 (defmacro gnus-save-hidden-threads (&rest forms)
2645   "Save hidden threads, eval FORMS, and restore the hidden threads."
2646   (let ((config (make-symbol "config")))
2647     `(let ((,config (gnus-hidden-threads-configuration)))
2648        (unwind-protect
2649            (save-excursion
2650              ,@forms)
2651          (gnus-restore-hidden-threads-configuration ,config)))))
2652
2653 (defun gnus-data-compute-positions ()
2654   "Compute the positions of all articles."
2655   (setq gnus-newsgroup-data-reverse nil)
2656   (let ((data gnus-newsgroup-data))
2657     (save-excursion
2658       (gnus-save-hidden-threads
2659         (gnus-summary-show-all-threads)
2660         (goto-char (point-min))
2661         (while data
2662           (while (get-text-property (point) 'gnus-intangible)
2663             (forward-line 1))
2664           (gnus-data-set-pos (car data) (+ (point) 3))
2665           (setq data (cdr data))
2666           (forward-line 1))))))
2667
2668 (defun gnus-hidden-threads-configuration ()
2669   "Return the current hidden threads configuration."
2670   (save-excursion
2671     (let (config)
2672       (goto-char (point-min))
2673       (while (search-forward "\r" nil t)
2674         (push (1- (point)) config))
2675       config)))
2676
2677 (defun gnus-restore-hidden-threads-configuration (config)
2678   "Restore hidden threads configuration from CONFIG."
2679   (save-excursion
2680     (let (point buffer-read-only)
2681       (while (setq point (pop config))
2682         (when (and (< point (point-max))
2683                    (goto-char point)
2684                    (eq (char-after) ?\n))
2685           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2686
2687 ;; Various summary mode internalish functions.
2688
2689 (defun gnus-mouse-pick-article (e)
2690   (interactive "e")
2691   (mouse-set-point e)
2692   (gnus-summary-next-page nil t))
2693
2694 (defun gnus-summary-set-display-table ()
2695   "Change the display table.
2696 Odd characters have a tendency to mess
2697 up nicely formatted displays - we make all possible glyphs
2698 display only a single character."
2699
2700   ;; We start from the standard display table, if any.
2701   (let ((table (or (copy-sequence standard-display-table)
2702                    (make-display-table)))
2703         (i 32))
2704     ;; Nix out all the control chars...
2705     (while (>= (setq i (1- i)) 0)
2706       (aset table i [??]))
2707     ;; ... but not newline and cr, of course.  (cr is necessary for the
2708     ;; selective display).
2709     (aset table ?\n nil)
2710     (aset table ?\r nil)
2711     ;; We keep TAB as well.
2712     (aset table ?\t nil)
2713     ;; We nix out any glyphs over 126 that are not set already.
2714     (let ((i 256))
2715       (while (>= (setq i (1- i)) 127)
2716         ;; Only modify if the entry is nil.
2717         (unless (aref table i)
2718           (aset table i [??]))))
2719     (setq buffer-display-table table)))
2720
2721 (defun gnus-summary-set-article-display-arrow (pos)
2722   "Update the overlay arrow to point to line at position POS."
2723   (when (and gnus-summary-display-arrow
2724              (boundp 'overlay-arrow-position)
2725              (boundp 'overlay-arrow-string))
2726     (save-excursion
2727       (goto-char pos)
2728       (beginning-of-line)
2729       (unless overlay-arrow-position
2730         (setq overlay-arrow-position (make-marker)))
2731       (setq overlay-arrow-string "=>"
2732             overlay-arrow-position (set-marker overlay-arrow-position
2733                                                (point)
2734                                                (current-buffer))))))
2735
2736 (defun gnus-summary-buffer-name (group)
2737   "Return the summary buffer name of GROUP."
2738   (concat "*Summary " group "*"))
2739
2740 (defun gnus-summary-setup-buffer (group)
2741   "Initialize summary buffer."
2742   (let ((buffer (gnus-summary-buffer-name group)))
2743     (if (get-buffer buffer)
2744         (progn
2745           (set-buffer buffer)
2746           (setq gnus-summary-buffer (current-buffer))
2747           (not gnus-newsgroup-prepared))
2748       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2749       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2750       (gnus-summary-mode group)
2751       (when gnus-carpal
2752         (gnus-carpal-setup-buffer 'summary))
2753       (unless gnus-single-article-buffer
2754         (make-local-variable 'gnus-article-buffer)
2755         (make-local-variable 'gnus-article-current)
2756         (make-local-variable 'gnus-original-article-buffer))
2757       (setq gnus-newsgroup-name group)
2758       ;; Set any local variables in the group parameters.
2759       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2760       t)))
2761
2762 (defun gnus-set-global-variables ()
2763   "Set the global equivalents of the buffer-local variables.
2764 They are set to the latest values they had.  These reflect the summary
2765 buffer that was in action when the last article was fetched."
2766   (when (eq major-mode 'gnus-summary-mode)
2767     (setq gnus-summary-buffer (current-buffer))
2768     (let ((name gnus-newsgroup-name)
2769           (marked gnus-newsgroup-marked)
2770           (unread gnus-newsgroup-unreads)
2771           (headers gnus-current-headers)
2772           (data gnus-newsgroup-data)
2773           (summary gnus-summary-buffer)
2774           (article-buffer gnus-article-buffer)
2775           (original gnus-original-article-buffer)
2776           (gac gnus-article-current)
2777           (reffed gnus-reffed-article-number)
2778           (score-file gnus-current-score-file)
2779           (default-charset gnus-newsgroup-charset)
2780           vlist)
2781       (let ((locals gnus-newsgroup-variables))
2782         (while locals
2783           (if (consp (car locals))
2784               (push (eval (caar locals)) vlist)
2785             (push (eval (car locals)) vlist))
2786           (setq locals (cdr locals)))
2787         (setq vlist (nreverse vlist)))
2788       (save-excursion
2789         (set-buffer gnus-group-buffer)
2790         (setq gnus-newsgroup-name name
2791               gnus-newsgroup-marked marked
2792               gnus-newsgroup-unreads unread
2793               gnus-current-headers headers
2794               gnus-newsgroup-data data
2795               gnus-article-current gac
2796               gnus-summary-buffer summary
2797               gnus-article-buffer article-buffer
2798               gnus-original-article-buffer original
2799               gnus-reffed-article-number reffed
2800               gnus-current-score-file score-file
2801               gnus-newsgroup-charset default-charset)
2802         (let ((locals gnus-newsgroup-variables))
2803           (while locals
2804             (if (consp (car locals))
2805                 (set (caar locals) (pop vlist))
2806               (set (car locals) (pop vlist)))
2807             (setq locals (cdr locals))))
2808         ;; The article buffer also has local variables.
2809         (when (gnus-buffer-live-p gnus-article-buffer)
2810           (set-buffer gnus-article-buffer)
2811           (setq gnus-summary-buffer summary))))))
2812
2813 (defun gnus-summary-article-unread-p (article)
2814   "Say whether ARTICLE is unread or not."
2815   (memq article gnus-newsgroup-unreads))
2816
2817 (defun gnus-summary-first-article-p (&optional article)
2818   "Return whether ARTICLE is the first article in the buffer."
2819   (if (not (setq article (or article (gnus-summary-article-number))))
2820       nil
2821     (eq article (caar gnus-newsgroup-data))))
2822
2823 (defun gnus-summary-last-article-p (&optional article)
2824   "Return whether ARTICLE is the last article in the buffer."
2825   (if (not (setq article (or article (gnus-summary-article-number))))
2826       ;; All non-existent numbers are the last article.  :-)
2827       t
2828     (not (cdr (gnus-data-find-list article)))))
2829
2830 (defun gnus-make-thread-indent-array ()
2831   (let ((n 200))
2832     (unless (and gnus-thread-indent-array
2833                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2834       (setq gnus-thread-indent-array (make-vector 201 "")
2835             gnus-thread-indent-array-level gnus-thread-indent-level)
2836       (while (>= n 0)
2837         (aset gnus-thread-indent-array n
2838               (make-string (* n gnus-thread-indent-level) ? ))
2839         (setq n (1- n))))))
2840
2841 (defun gnus-update-summary-mark-positions ()
2842   "Compute where the summary marks are to go."
2843   (save-excursion
2844     (when (gnus-buffer-exists-p gnus-summary-buffer)
2845       (set-buffer gnus-summary-buffer))
2846     (let ((gnus-replied-mark 129)
2847           (gnus-score-below-mark 130)
2848           (gnus-score-over-mark 130)
2849           (gnus-download-mark 131)
2850           (spec gnus-summary-line-format-spec)
2851           gnus-visual pos)
2852       (save-excursion
2853         (gnus-set-work-buffer)
2854         (let ((gnus-summary-line-format-spec spec)
2855               (gnus-newsgroup-downloadable '((0 . t))))
2856           (gnus-summary-insert-line
2857            (make-full-mail-header 0 "" "nobody"
2858                                   "05 Apr 2001 23:33:09 +0400"
2859                                   "" "" 0 0 "" nil)
2860            0 nil 128 t nil "" nil 1)
2861           (goto-char (point-min))
2862           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2863                                              (- (point) 2)))))
2864           (goto-char (point-min))
2865           (push (cons 'replied (and (search-forward "\201" nil t)
2866                                     (- (point) 2)))
2867                 pos)
2868           (goto-char (point-min))
2869           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2870                 pos)
2871           (goto-char (point-min))
2872           (push (cons 'download
2873                       (and (search-forward "\203" nil t) (- (point) 2)))
2874                 pos)))
2875       (setq gnus-summary-mark-positions pos))))
2876
2877 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2878   "Insert a dummy root in the summary buffer."
2879   (beginning-of-line)
2880   (gnus-add-text-properties
2881    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2882    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2883
2884 (defun gnus-summary-from-or-to-or-newsgroups (header)
2885   (let ((to (cdr (assq 'To (mail-header-extra header))))
2886         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2887         (default-mime-charset (with-current-buffer gnus-summary-buffer
2888                                 default-mime-charset)))
2889     (cond
2890      ((and to
2891            gnus-ignored-from-addresses
2892            (string-match gnus-ignored-from-addresses
2893                          (mail-header-from header)))
2894       (concat "-> "
2895               (or (car (funcall gnus-extract-address-components
2896                                 (funcall
2897                                  gnus-decode-encoded-word-function to)))
2898                   (funcall gnus-decode-encoded-word-function to))))
2899      ((and newsgroups
2900            gnus-ignored-from-addresses
2901            (string-match gnus-ignored-from-addresses
2902                          (mail-header-from header)))
2903       (concat "=> " newsgroups))
2904      (t
2905       (or (car (funcall gnus-extract-address-components
2906                         (mail-header-from header)))
2907           (mail-header-from header))))))
2908
2909 (defun gnus-summary-insert-line (gnus-tmp-header
2910                                  gnus-tmp-level gnus-tmp-current
2911                                  gnus-tmp-unread gnus-tmp-replied
2912                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2913                                  &optional gnus-tmp-dummy gnus-tmp-score
2914                                  gnus-tmp-process)
2915   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2916          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2917          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2918          (gnus-tmp-score-char
2919           (if (or (null gnus-summary-default-score)
2920                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2921                       gnus-summary-zcore-fuzz))
2922               ?\ ;;;Whitespace
2923             (if (< gnus-tmp-score gnus-summary-default-score)
2924                 gnus-score-below-mark gnus-score-over-mark)))
2925          (gnus-tmp-replied
2926           (cond (gnus-tmp-process gnus-process-mark)
2927                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2928                  gnus-cached-mark)
2929                 (gnus-tmp-replied gnus-replied-mark)
2930                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2931                  gnus-saved-mark)
2932                 (t gnus-no-mark)))
2933          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2934          (gnus-tmp-name
2935           (cond
2936            ((string-match "<[^>]+> *$" gnus-tmp-from)
2937             (let ((beg (match-beginning 0)))
2938               (or (and (string-match "^\".+\"" gnus-tmp-from)
2939                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2940                   (substring gnus-tmp-from 0 beg))))
2941            ((string-match "(.+)" gnus-tmp-from)
2942             (substring gnus-tmp-from
2943                        (1+ (match-beginning 0)) (1- (match-end 0))))
2944            (t gnus-tmp-from)))
2945          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2946          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2947          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2948          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2949          (buffer-read-only nil))
2950     (when (string= gnus-tmp-name "")
2951       (setq gnus-tmp-name gnus-tmp-from))
2952     (unless (numberp gnus-tmp-lines)
2953       (setq gnus-tmp-lines -1))
2954     (when (= gnus-tmp-lines -1)
2955       (setq gnus-tmp-lines "?"))
2956     (gnus-put-text-property-excluding-characters-with-faces
2957      (point)
2958      (progn (eval gnus-summary-line-format-spec) (point))
2959      'gnus-number gnus-tmp-number)
2960     (when (gnus-visual-p 'summary-highlight 'highlight)
2961       (forward-line -1)
2962       (gnus-run-hooks 'gnus-summary-update-hook)
2963       (forward-line 1))))
2964
2965 (defun gnus-summary-update-line (&optional dont-update)
2966   "Update summary line after change."
2967   (when (and gnus-summary-default-score
2968              (not gnus-summary-inhibit-highlight))
2969     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2970            (article (gnus-summary-article-number))
2971            (score (gnus-summary-article-score article)))
2972       (unless dont-update
2973         (if (and gnus-summary-mark-below
2974                  (< (gnus-summary-article-score)
2975                     gnus-summary-mark-below))
2976             ;; This article has a low score, so we mark it as read.
2977             (when (memq article gnus-newsgroup-unreads)
2978               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2979           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2980             ;; This article was previously marked as read on account
2981             ;; of a low score, but now it has risen, so we mark it as
2982             ;; unread.
2983             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2984         (gnus-summary-update-mark
2985          (if (or (null gnus-summary-default-score)
2986                  (<= (abs (- score gnus-summary-default-score))
2987                      gnus-summary-zcore-fuzz))
2988              ?\ ;;;Whitespace
2989            (if (< score gnus-summary-default-score)
2990                gnus-score-below-mark gnus-score-over-mark))
2991          'score))
2992       ;; Do visual highlighting.
2993       (when (gnus-visual-p 'summary-highlight 'highlight)
2994         (gnus-run-hooks 'gnus-summary-update-hook)))))
2995
2996 (defvar gnus-tmp-new-adopts nil)
2997
2998 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2999   "Return the number of articles in THREAD.
3000 This may be 0 in some cases -- if none of the articles in
3001 the thread are to be displayed."
3002   (let* ((number
3003           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3004           (cond
3005            ((not (listp thread))
3006             1)
3007            ((and (consp thread) (cdr thread))
3008             (apply
3009              '+ 1 (mapcar
3010                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3011            ((null thread)
3012             1)
3013            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3014             1)
3015            (t 0))))
3016     (when (and level (zerop level) gnus-tmp-new-adopts)
3017       (incf number
3018             (apply '+ (mapcar
3019                        'gnus-summary-number-of-articles-in-thread
3020                        gnus-tmp-new-adopts))))
3021     (if char
3022         (if (> number 1) gnus-not-empty-thread-mark
3023           gnus-empty-thread-mark)
3024       number)))
3025
3026 (defun gnus-summary-set-local-parameters (group)
3027   "Go through the local params of GROUP and set all variable specs in that list."
3028   (let ((params (gnus-group-find-parameter group))
3029         (vars '(quit-config))           ; Ignore quit-config.
3030         elem)
3031     (while params
3032       (setq elem (car params)
3033             params (cdr params))
3034       (and (consp elem)                 ; Has to be a cons.
3035            (consp (cdr elem))           ; The cdr has to be a list.
3036            (symbolp (car elem))         ; Has to be a symbol in there.
3037            (not (memq (car elem) vars))
3038            (ignore-errors               ; So we set it.
3039              (push (car elem) vars)
3040              (make-local-variable (car elem))
3041              (set (car elem) (eval (nth 1 elem))))))))
3042
3043 (defun gnus-summary-read-group (group &optional show-all no-article
3044                                       kill-buffer no-display backward
3045                                       select-articles)
3046   "Start reading news in newsgroup GROUP.
3047 If SHOW-ALL is non-nil, already read articles are also listed.
3048 If NO-ARTICLE is non-nil, no article is selected initially.
3049 If NO-DISPLAY, don't generate a summary buffer."
3050   (let (result)
3051     (while (and group
3052                 (null (setq result
3053                             (let ((gnus-auto-select-next nil))
3054                               (or (gnus-summary-read-group-1
3055                                    group show-all no-article
3056                                    kill-buffer no-display
3057                                    select-articles)
3058                                   (setq show-all nil
3059                                         select-articles nil)))))
3060                 (eq gnus-auto-select-next 'quietly))
3061       (set-buffer gnus-group-buffer)
3062       ;; The entry function called above goes to the next
3063       ;; group automatically, so we go two groups back
3064       ;; if we are searching for the previous group.
3065       (when backward
3066         (gnus-group-prev-unread-group 2))
3067       (if (not (equal group (gnus-group-group-name)))
3068           (setq group (gnus-group-group-name))
3069         (setq group nil)))
3070     result))
3071
3072 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3073   "Directly jump to the other GROUP from summary buffer.
3074 If SHOW-ALL is non-nil, already read articles are also listed."
3075   (interactive
3076    (if (eq gnus-summary-buffer (current-buffer))
3077        (list (completing-read
3078               "Group: " gnus-active-hashtb nil t
3079               (when (and gnus-newsgroup-name
3080                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3081                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3082               'gnus-group-history)
3083              current-prefix-arg)
3084      (error "%s must be invoked from a gnus summary buffer." this-command)))
3085   (unless (or (zerop (length group))
3086               (and gnus-newsgroup-name
3087                    (string-equal gnus-newsgroup-name group)))
3088     (gnus-summary-exit)
3089     (gnus-summary-read-group group show-all
3090                              gnus-dont-select-after-jump-to-other-group)))
3091
3092 (defun gnus-summary-read-group-1 (group show-all no-article
3093                                         kill-buffer no-display
3094                                         &optional select-articles)
3095   ;; Killed foreign groups can't be entered.
3096   ;;  (when (and (not (gnus-group-native-p group))
3097   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3098   ;;    (error "Dead non-native groups can't be entered"))
3099   (gnus-message 5 "Retrieving newsgroup: %s..."
3100                 (gnus-group-decoded-name group))
3101   (let* ((new-group (gnus-summary-setup-buffer group))
3102          (quit-config (gnus-group-quit-config group))
3103          (did-select (and new-group (gnus-select-newsgroup
3104                                      group show-all select-articles))))
3105     (cond
3106      ;; This summary buffer exists already, so we just select it.
3107      ((not new-group)
3108       (gnus-set-global-variables)
3109       (when kill-buffer
3110         (gnus-kill-or-deaden-summary kill-buffer))
3111       (gnus-configure-windows 'summary 'force)
3112       (gnus-set-mode-line 'summary)
3113       (gnus-summary-position-point)
3114       (message "")
3115       t)
3116      ;; We couldn't select this group.
3117      ((null did-select)
3118       (when (and (eq major-mode 'gnus-summary-mode)
3119                  (not (equal (current-buffer) kill-buffer)))
3120         (kill-buffer (current-buffer))
3121         (if (not quit-config)
3122             (progn
3123               ;; Update the info -- marks might need to be removed,
3124               ;; for instance.
3125               (gnus-summary-update-info)
3126               (set-buffer gnus-group-buffer)
3127               (gnus-group-jump-to-group group)
3128               (gnus-group-next-unread-group 1))
3129           (gnus-handle-ephemeral-exit quit-config)))
3130       (let ((grpinfo (gnus-get-info group)))
3131         (if (null (gnus-info-read grpinfo))
3132             (gnus-message 3 "Group %s contains no messages"
3133                           (gnus-group-decoded-name group))
3134           (gnus-message 3 "Can't select group")))
3135       nil)
3136      ;; The user did a `C-g' while prompting for number of articles,
3137      ;; so we exit this group.
3138      ((eq did-select 'quit)
3139       (and (eq major-mode 'gnus-summary-mode)
3140            (not (equal (current-buffer) kill-buffer))
3141            (kill-buffer (current-buffer)))
3142       (when kill-buffer
3143         (gnus-kill-or-deaden-summary kill-buffer))
3144       (if (not quit-config)
3145           (progn
3146             (set-buffer gnus-group-buffer)
3147             (gnus-group-jump-to-group group)
3148             (gnus-group-next-unread-group 1)
3149             (gnus-configure-windows 'group 'force))
3150         (gnus-handle-ephemeral-exit quit-config))
3151       ;; Finally signal the quit.
3152       (signal 'quit nil))
3153      ;; The group was successfully selected.
3154      (t
3155       (gnus-set-global-variables)
3156       ;; Save the active value in effect when the group was entered.
3157       (setq gnus-newsgroup-active
3158             (gnus-copy-sequence
3159              (gnus-active gnus-newsgroup-name)))
3160       ;; You can change the summary buffer in some way with this hook.
3161       (gnus-run-hooks 'gnus-select-group-hook)
3162       (gnus-update-format-specifications
3163        nil 'summary 'summary-mode 'summary-dummy)
3164       (gnus-update-summary-mark-positions)
3165       ;; Do score processing.
3166       (when gnus-use-scoring
3167         (gnus-possibly-score-headers))
3168       ;; Check whether to fill in the gaps in the threads.
3169       (when gnus-build-sparse-threads
3170         (gnus-build-sparse-threads))
3171       ;; Find the initial limit.
3172       (if gnus-show-threads
3173           (if show-all
3174               (let ((gnus-newsgroup-dormant nil))
3175                 (gnus-summary-initial-limit show-all))
3176             (gnus-summary-initial-limit show-all))
3177         ;; When untreaded, all articles are always shown.
3178         (setq gnus-newsgroup-limit
3179               (mapcar
3180                (lambda (header) (mail-header-number header))
3181                gnus-newsgroup-headers)))
3182       ;; Generate the summary buffer.
3183       (unless no-display
3184         (gnus-summary-prepare))
3185       (when gnus-use-trees
3186         (gnus-tree-open group)
3187         (setq gnus-summary-highlight-line-function
3188               'gnus-tree-highlight-article))
3189       ;; If the summary buffer is empty, but there are some low-scored
3190       ;; articles or some excluded dormants, we include these in the
3191       ;; buffer.
3192       (when (and (zerop (buffer-size))
3193                  (not no-display))
3194         (cond (gnus-newsgroup-dormant
3195                (gnus-summary-limit-include-dormant))
3196               ((and gnus-newsgroup-scored show-all)
3197                (gnus-summary-limit-include-expunged t))))
3198       ;; Function `gnus-apply-kill-file' must be called in this hook.
3199       (gnus-run-hooks 'gnus-apply-kill-hook)
3200       (if (and (zerop (buffer-size))
3201                (not no-display))
3202           (progn
3203             ;; This newsgroup is empty.
3204             (gnus-summary-catchup-and-exit nil t)
3205             (gnus-message 6 "No unread news")
3206             (when kill-buffer
3207               (gnus-kill-or-deaden-summary kill-buffer))
3208             ;; Return nil from this function.
3209             nil)
3210         ;; Hide conversation thread subtrees.  We cannot do this in
3211         ;; gnus-summary-prepare-hook since kill processing may not
3212         ;; work with hidden articles.
3213         (and gnus-show-threads
3214              gnus-thread-hide-subtree
3215              (gnus-summary-hide-all-threads))
3216         (when kill-buffer
3217           (gnus-kill-or-deaden-summary kill-buffer))
3218         ;; Show first unread article if requested.
3219         (if (and (not no-article)
3220                  (not no-display)
3221                  gnus-newsgroup-unreads
3222                  gnus-auto-select-first)
3223             (progn
3224               (gnus-configure-windows 'summary)
3225               (cond
3226                ((eq gnus-auto-select-first 'best)
3227                 (gnus-summary-best-unread-article))
3228                ((eq gnus-auto-select-first t)
3229                 (gnus-summary-first-unread-article))
3230                ((gnus-functionp gnus-auto-select-first)
3231                 (funcall gnus-auto-select-first))))
3232           ;; Don't select any articles, just move point to the first
3233           ;; article in the group.
3234           (goto-char (point-min))
3235           (gnus-summary-position-point)
3236           (gnus-configure-windows 'summary 'force)
3237           (gnus-set-mode-line 'summary))
3238         (when (get-buffer-window gnus-group-buffer t)
3239           ;; Gotta use windows, because recenter does weird stuff if
3240           ;; the current buffer ain't the displayed window.
3241           (let ((owin (selected-window)))
3242             (select-window (get-buffer-window gnus-group-buffer t))
3243             (when (gnus-group-goto-group group)
3244               (recenter))
3245             (select-window owin)))
3246         ;; Mark this buffer as "prepared".
3247         (setq gnus-newsgroup-prepared t)
3248         (gnus-run-hooks 'gnus-summary-prepared-hook)
3249         t)))))
3250
3251 (defun gnus-summary-prepare ()
3252   "Generate the summary buffer."
3253   (interactive)
3254   (let ((buffer-read-only nil))
3255     (erase-buffer)
3256     (setq gnus-newsgroup-data nil
3257           gnus-newsgroup-data-reverse nil)
3258     (gnus-run-hooks 'gnus-summary-generate-hook)
3259     ;; Generate the buffer, either with threads or without.
3260     (when gnus-newsgroup-headers
3261       (gnus-summary-prepare-threads
3262        (if gnus-show-threads
3263            (gnus-sort-gathered-threads
3264             (funcall gnus-summary-thread-gathering-function
3265                      (gnus-sort-threads
3266                       (gnus-cut-threads (gnus-make-threads)))))
3267          ;; Unthreaded display.
3268          (gnus-sort-articles gnus-newsgroup-headers))))
3269     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3270     ;; Call hooks for modifying summary buffer.
3271     (goto-char (point-min))
3272     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3273
3274 (defsubst gnus-general-simplify-subject (subject)
3275   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3276   (setq subject
3277         (cond
3278          ;; Truncate the subject.
3279          (gnus-simplify-subject-functions
3280           (gnus-map-function gnus-simplify-subject-functions subject))
3281          ((numberp gnus-summary-gather-subject-limit)
3282           (setq subject (gnus-simplify-subject-re subject))
3283           (if (> (length subject) gnus-summary-gather-subject-limit)
3284               (substring subject 0 gnus-summary-gather-subject-limit)
3285             subject))
3286          ;; Fuzzily simplify it.
3287          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3288           (gnus-simplify-subject-fuzzy subject))
3289          ;; Just remove the leading "Re:".
3290          (t
3291           (gnus-simplify-subject-re subject))))
3292
3293   (if (and gnus-summary-gather-exclude-subject
3294            (string-match gnus-summary-gather-exclude-subject subject))
3295       nil                               ; This article shouldn't be gathered
3296     subject))
3297
3298 (defun gnus-summary-simplify-subject-query ()
3299   "Query where the respool algorithm would put this article."
3300   (interactive)
3301   (gnus-summary-select-article)
3302   (message "%s"
3303            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3304
3305 (defun gnus-gather-threads-by-subject (threads)
3306   "Gather threads by looking at Subject headers."
3307   (if (not gnus-summary-make-false-root)
3308       threads
3309     (let ((hashtb (gnus-make-hashtable 1024))
3310           (prev threads)
3311           (result threads)
3312           subject hthread whole-subject)
3313       (while threads
3314         (setq subject (gnus-general-simplify-subject
3315                        (setq whole-subject (mail-header-subject
3316                                             (caar threads)))))
3317         (when subject
3318           (if (setq hthread (gnus-gethash subject hashtb))
3319               (progn
3320                 ;; We enter a dummy root into the thread, if we
3321                 ;; haven't done that already.
3322                 (unless (stringp (caar hthread))
3323                   (setcar hthread (list whole-subject (car hthread))))
3324                 ;; We add this new gathered thread to this gathered
3325                 ;; thread.
3326                 (setcdr (car hthread)
3327                         (nconc (cdar hthread) (list (car threads))))
3328                 ;; Remove it from the list of threads.
3329                 (setcdr prev (cdr threads))
3330                 (setq threads prev))
3331             ;; Enter this thread into the hash table.
3332             (gnus-sethash subject threads hashtb)))
3333         (setq prev threads)
3334         (setq threads (cdr threads)))
3335       result)))
3336
3337 (defun gnus-gather-threads-by-references (threads)
3338   "Gather threads by looking at References headers."
3339   (let ((idhashtb (gnus-make-hashtable 1024))
3340         (thhashtb (gnus-make-hashtable 1024))
3341         (prev threads)
3342         (result threads)
3343         ids references id gthread gid entered ref)
3344     (while threads
3345       (when (setq references (mail-header-references (caar threads)))
3346         (setq id (mail-header-id (caar threads))
3347               ids (gnus-split-references references)
3348               entered nil)
3349         (while (setq ref (pop ids))
3350           (setq ids (delete ref ids))
3351           (if (not (setq gid (gnus-gethash ref idhashtb)))
3352               (progn
3353                 (gnus-sethash ref id idhashtb)
3354                 (gnus-sethash id threads thhashtb))
3355             (setq gthread (gnus-gethash gid thhashtb))
3356             (unless entered
3357               ;; We enter a dummy root into the thread, if we
3358               ;; haven't done that already.
3359               (unless (stringp (caar gthread))
3360                 (setcar gthread (list (mail-header-subject (caar gthread))
3361                                       (car gthread))))
3362               ;; We add this new gathered thread to this gathered
3363               ;; thread.
3364               (setcdr (car gthread)
3365                       (nconc (cdar gthread) (list (car threads)))))
3366             ;; Add it into the thread hash table.
3367             (gnus-sethash id gthread thhashtb)
3368             (setq entered t)
3369             ;; Remove it from the list of threads.
3370             (setcdr prev (cdr threads))
3371             (setq threads prev))))
3372       (setq prev threads)
3373       (setq threads (cdr threads)))
3374     result))
3375
3376 (defun gnus-sort-gathered-threads (threads)
3377   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3378   (let ((result threads))
3379     (while threads
3380       (when (stringp (caar threads))
3381         (setcdr (car threads)
3382                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3383       (setq threads (cdr threads)))
3384     result))
3385
3386 (defun gnus-thread-loop-p (root thread)
3387   "Say whether ROOT is in THREAD."
3388   (let ((stack (list thread))
3389         (infloop 0)
3390         th)
3391     (while (setq thread (pop stack))
3392       (setq th (cdr thread))
3393       (while (and th
3394                   (not (eq (caar th) root)))
3395         (pop th))
3396       (if th
3397           ;; We have found a loop.
3398           (let (ref-dep)
3399             (setcdr thread (delq (car th) (cdr thread)))
3400             (if (boundp (setq ref-dep (intern "none"
3401                                               gnus-newsgroup-dependencies)))
3402                 (setcdr (symbol-value ref-dep)
3403                         (nconc (cdr (symbol-value ref-dep))
3404                                (list (car th))))
3405               (set ref-dep (list nil (car th))))
3406             (setq infloop 1
3407                   stack nil))
3408         ;; Push all the subthreads onto the stack.
3409         (push (cdr thread) stack)))
3410     infloop))
3411
3412 (defun gnus-make-threads ()
3413   "Go through the dependency hashtb and find the roots.  Return all threads."
3414   (let (threads)
3415     (while (catch 'infloop
3416              (mapatoms
3417               (lambda (refs)
3418                 ;; Deal with self-referencing References loops.
3419                 (when (and (car (symbol-value refs))
3420                            (not (zerop
3421                                  (apply
3422                                   '+
3423                                   (mapcar
3424                                    (lambda (thread)
3425                                      (gnus-thread-loop-p
3426                                       (car (symbol-value refs)) thread))
3427                                    (cdr (symbol-value refs)))))))
3428                   (setq threads nil)
3429                   (throw 'infloop t))
3430                 (unless (car (symbol-value refs))
3431                   ;; These threads do not refer back to any other articles,
3432                   ;; so they're roots.
3433                   (setq threads (append (cdr (symbol-value refs)) threads))))
3434               gnus-newsgroup-dependencies)))
3435     threads))
3436
3437 ;; Build the thread tree.
3438 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3439   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3440
3441 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3442 if it was already present.
3443
3444 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3445 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3446 Message-IDs will be renamed be renamed to a unique Message-ID before
3447 being entered.
3448
3449 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3450   (let* ((id (mail-header-id header))
3451          (id-dep (and id (intern id dependencies)))
3452          ref ref-dep ref-header)
3453     ;; Enter this `header' in the `dependencies' table.
3454     (cond
3455      ((not id-dep)
3456       (setq header nil))
3457      ;; The first two cases do the normal part: enter a new `header'
3458      ;; in the `dependencies' table.
3459      ((not (boundp id-dep))
3460       (set id-dep (list header)))
3461      ((null (car (symbol-value id-dep)))
3462       (setcar (symbol-value id-dep) header))
3463
3464      ;; From here the `header' was already present in the
3465      ;; `dependencies' table.
3466      (force-new
3467       ;; Overrides an existing entry;
3468       ;; just set the header part of the entry.
3469       (setcar (symbol-value id-dep) header))
3470
3471      ;; Renames the existing `header' to a unique Message-ID.
3472      ((not gnus-summary-ignore-duplicates)
3473       ;; An article with this Message-ID has already been seen.
3474       ;; We rename the Message-ID.
3475       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3476            (list header))
3477       (mail-header-set-id header id))
3478
3479      ;; The last case ignores an existing entry, except it adds any
3480      ;; additional Xrefs (in case the two articles came from different
3481      ;; servers.
3482      ;; Also sets `header' to `nil' meaning that the `dependencies'
3483      ;; table was *not* modified.
3484      (t
3485       (mail-header-set-xref
3486        (car (symbol-value id-dep))
3487        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3488                    "")
3489                (or (mail-header-xref header) "")))
3490       (setq header nil)))
3491
3492     (when header
3493       ;; First check if that we are not creating a References loop.
3494       (setq ref (gnus-parent-id (mail-header-references header)))
3495       (while (and ref
3496                   (setq ref-dep (intern-soft ref dependencies))
3497                   (boundp ref-dep)
3498                   (setq ref-header (car (symbol-value ref-dep))))
3499         (if (string= id ref)
3500             ;; Yuk!  This is a reference loop.  Make the article be a
3501             ;; root article.
3502             (progn
3503               (mail-header-set-references (car (symbol-value id-dep)) "none")
3504               (setq ref nil))
3505           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3506       (setq ref (gnus-parent-id (mail-header-references header)))
3507       (setq ref-dep (intern (or ref "none") dependencies))
3508       (if (boundp ref-dep)
3509           (setcdr (symbol-value ref-dep)
3510                   (nconc (cdr (symbol-value ref-dep))
3511                          (list (symbol-value id-dep))))
3512         (set ref-dep (list nil (symbol-value id-dep)))))
3513     header))
3514
3515 (defun gnus-build-sparse-threads ()
3516   (let ((headers gnus-newsgroup-headers)
3517         (mail-parse-charset gnus-newsgroup-charset)
3518         (gnus-summary-ignore-duplicates t)
3519         header references generation relations
3520         subject child end new-child date)
3521     ;; First we create an alist of generations/relations, where
3522     ;; generations is how much we trust the relation, and the relation
3523     ;; is parent/child.
3524     (gnus-message 7 "Making sparse threads...")
3525     (save-excursion
3526       (nnheader-set-temp-buffer " *gnus sparse threads*")
3527       (while (setq header (pop headers))
3528         (when (and (setq references (mail-header-references header))
3529                    (not (string= references "")))
3530           (insert references)
3531           (setq child (mail-header-id header)
3532                 subject (mail-header-subject header)
3533                 date (mail-header-date header)
3534                 generation 0)
3535           (while (search-backward ">" nil t)
3536             (setq end (1+ (point)))
3537             (when (search-backward "<" nil t)
3538               (setq new-child (buffer-substring (point) end))
3539               (push (list (incf generation)
3540                           child (setq child new-child)
3541                           subject date)
3542                     relations)))
3543           (when child
3544             (push (list (1+ generation) child nil subject) relations))
3545           (erase-buffer)))
3546       (kill-buffer (current-buffer)))
3547     ;; Sort over trustworthiness.
3548     (mapcar
3549      (lambda (relation)
3550        (when (gnus-dependencies-add-header
3551               (make-full-mail-header-from-decoded-header
3552                gnus-reffed-article-number
3553                (nth 3 relation) "" (or (nth 4 relation) "")
3554                (nth 1 relation)
3555                (or (nth 2 relation) "") 0 0 "")
3556               gnus-newsgroup-dependencies nil)
3557          (push gnus-reffed-article-number gnus-newsgroup-limit)
3558          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3559          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3560                gnus-newsgroup-reads)
3561          (decf gnus-reffed-article-number)))
3562      (sort relations 'car-less-than-car))
3563     (gnus-message 7 "Making sparse threads...done")))
3564
3565 (defun gnus-build-old-threads ()
3566   ;; Look at all the articles that refer back to old articles, and
3567   ;; fetch the headers for the articles that aren't there.  This will
3568   ;; build complete threads - if the roots haven't been expired by the
3569   ;; server, that is.
3570   (let ((mail-parse-charset gnus-newsgroup-charset)
3571         id heads)
3572     (mapatoms
3573      (lambda (refs)
3574        (when (not (car (symbol-value refs)))
3575          (setq heads (cdr (symbol-value refs)))
3576          (while heads
3577            (if (memq (mail-header-number (caar heads))
3578                      gnus-newsgroup-dormant)
3579                (setq heads (cdr heads))
3580              (setq id (symbol-name refs))
3581              (while (and (setq id (gnus-build-get-header id))
3582                          (not (car (gnus-id-to-thread id)))))
3583              (setq heads nil)))))
3584      gnus-newsgroup-dependencies)))
3585
3586 ;; This function has to be called with point after the article number
3587 ;; on the beginning of the line.
3588 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3589   (let ((eol (gnus-point-at-eol))
3590         (buffer (current-buffer))
3591         header)
3592
3593     ;; overview: [num subject from date id refs chars lines misc]
3594     (unwind-protect
3595         (progn
3596           (narrow-to-region (point) eol)
3597           (unless (eobp)
3598             (forward-char))
3599
3600           (setq header
3601                 (make-full-mail-header
3602                  number                         ; number
3603                  (nnheader-nov-field)           ; subject
3604                  (nnheader-nov-field)           ; from
3605                  (nnheader-nov-field)           ; date
3606                  (nnheader-nov-read-message-id) ; id
3607                  (nnheader-nov-field)           ; refs
3608                  (nnheader-nov-read-integer)    ; chars
3609                  (nnheader-nov-read-integer)    ; lines
3610                  (unless (eobp)
3611                    (if (looking-at "Xref: ")
3612                        (goto-char (match-end 0)))
3613                    (nnheader-nov-field))        ; Xref
3614                  (nnheader-nov-parse-extra))))  ; extra
3615
3616       (widen))
3617
3618     (when gnus-alter-header-function
3619       (funcall gnus-alter-header-function header))
3620     (gnus-dependencies-add-header header dependencies force-new)))
3621
3622 (defun gnus-build-get-header (id)
3623   "Look through the buffer of NOV lines and find the header to ID.
3624 Enter this line into the dependencies hash table, and return
3625 the id of the parent article (if any)."
3626   (let ((deps gnus-newsgroup-dependencies)
3627         found header)
3628     (prog1
3629         (save-excursion
3630           (set-buffer nntp-server-buffer)
3631           (let ((case-fold-search nil))
3632             (goto-char (point-min))
3633             (while (and (not found)
3634                         (search-forward id nil t))
3635               (beginning-of-line)
3636               (setq found (looking-at
3637                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3638                                    (regexp-quote id))))
3639               (or found (beginning-of-line 2)))
3640             (when found
3641               (beginning-of-line)
3642               (and
3643                (setq header (gnus-nov-parse-line
3644                              (read (current-buffer)) deps))
3645                (gnus-parent-id (mail-header-references header))))))
3646       (when header
3647         (let ((number (mail-header-number header)))
3648           (push number gnus-newsgroup-limit)
3649           (push header gnus-newsgroup-headers)
3650           (if (memq number gnus-newsgroup-unselected)
3651               (progn
3652                 (push number gnus-newsgroup-unreads)
3653                 (setq gnus-newsgroup-unselected
3654                       (delq number gnus-newsgroup-unselected)))
3655             (push number gnus-newsgroup-ancient)))))))
3656
3657 (defun gnus-build-all-threads ()
3658   "Read all the headers."
3659   (let ((gnus-summary-ignore-duplicates t)
3660         (mail-parse-charset gnus-newsgroup-charset)
3661         (dependencies gnus-newsgroup-dependencies)
3662         header article)
3663     (save-excursion
3664       (set-buffer nntp-server-buffer)
3665       (let ((case-fold-search nil))
3666         (goto-char (point-min))
3667         (while (not (eobp))
3668           (ignore-errors
3669             (setq article (read (current-buffer))
3670                   header (gnus-nov-parse-line article dependencies)))
3671           (when header
3672             (save-excursion
3673               (set-buffer gnus-summary-buffer)
3674               (push header gnus-newsgroup-headers)
3675               (if (memq (setq article (mail-header-number header))
3676                         gnus-newsgroup-unselected)
3677                   (progn
3678                     (push article gnus-newsgroup-unreads)
3679                     (setq gnus-newsgroup-unselected
3680                           (delq article gnus-newsgroup-unselected)))
3681                 (push article gnus-newsgroup-ancient)))
3682             (forward-line 1)))))))
3683
3684 (defun gnus-summary-update-article-line (article header)
3685   "Update the line for ARTICLE using HEADERS."
3686   (let* ((id (mail-header-id header))
3687          (thread (gnus-id-to-thread id)))
3688     (unless thread
3689       (error "Article in no thread"))
3690     ;; Update the thread.
3691     (setcar thread header)
3692     (gnus-summary-goto-subject article)
3693     (let* ((datal (gnus-data-find-list article))
3694            (data (car datal))
3695            (length (when (cdr datal)
3696                      (- (gnus-data-pos data)
3697                         (gnus-data-pos (cadr datal)))))
3698            (buffer-read-only nil)
3699            (level (gnus-summary-thread-level)))
3700       (gnus-delete-line)
3701       (gnus-summary-insert-line
3702        header level nil (gnus-article-mark article)
3703        (memq article gnus-newsgroup-replied)
3704        (memq article gnus-newsgroup-expirable)
3705        ;; Only insert the Subject string when it's different
3706        ;; from the previous Subject string.
3707        (if (and
3708             gnus-show-threads
3709             (gnus-subject-equal
3710              (condition-case ()
3711                  (mail-header-subject
3712                   (gnus-data-header
3713                    (cadr
3714                     (gnus-data-find-list
3715                      article
3716                      (gnus-data-list t)))))
3717                ;; Error on the side of excessive subjects.
3718                (error ""))
3719              (mail-header-subject header)))
3720            ""
3721          (mail-header-subject header))
3722        nil (cdr (assq article gnus-newsgroup-scored))
3723        (memq article gnus-newsgroup-processable))
3724       (when length
3725         (gnus-data-update-list
3726          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3727
3728 (defun gnus-summary-update-article (article &optional iheader)
3729   "Update ARTICLE in the summary buffer."
3730   (set-buffer gnus-summary-buffer)
3731   (let* ((header (gnus-summary-article-header article))
3732          (id (mail-header-id header))
3733          (data (gnus-data-find article))
3734          (thread (gnus-id-to-thread id))
3735          (references (mail-header-references header))
3736          (parent
3737           (gnus-id-to-thread
3738            (or (gnus-parent-id
3739                 (when (and references
3740                            (not (equal "" references)))
3741                   references))
3742                "none")))
3743          (buffer-read-only nil)
3744          (old (car thread)))
3745     (when thread
3746       (unless iheader
3747         (setcar thread nil)
3748         (when parent
3749           (delq thread parent)))
3750       (if (gnus-summary-insert-subject id header)
3751           ;; Set the (possibly) new article number in the data structure.
3752           (gnus-data-set-number data (gnus-id-to-article id))
3753         (setcar thread old)
3754         nil))))
3755
3756 (defun gnus-rebuild-thread (id &optional line)
3757   "Rebuild the thread containing ID.
3758 If LINE, insert the rebuilt thread starting on line LINE."
3759   (let ((buffer-read-only nil)
3760         old-pos current thread data)
3761     (if (not gnus-show-threads)
3762         (setq thread (list (car (gnus-id-to-thread id))))
3763       ;; Get the thread this article is part of.
3764       (setq thread (gnus-remove-thread id)))
3765     (setq old-pos (gnus-point-at-bol))
3766     (setq current (save-excursion
3767                     (and (re-search-backward "[\r\n]" nil t)
3768                          (gnus-summary-article-number))))
3769     ;; If this is a gathered thread, we have to go some re-gathering.
3770     (when (stringp (car thread))
3771       (let ((subject (car thread))
3772             roots thr)
3773         (setq thread (cdr thread))
3774         (while thread
3775           (unless (memq (setq thr (gnus-id-to-thread
3776                                    (gnus-root-id
3777                                     (mail-header-id (caar thread)))))
3778                         roots)
3779             (push thr roots))
3780           (setq thread (cdr thread)))
3781         ;; We now have all (unique) roots.
3782         (if (= (length roots) 1)
3783             ;; All the loose roots are now one solid root.
3784             (setq thread (car roots))
3785           (setq thread (cons subject (gnus-sort-threads roots))))))
3786     (let (threads)
3787       ;; We then insert this thread into the summary buffer.
3788       (when line
3789         (goto-char (point-min))
3790         (forward-line (1- line)))
3791       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3792         (if gnus-show-threads
3793             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3794           (gnus-summary-prepare-unthreaded thread))
3795         (setq data (nreverse gnus-newsgroup-data))
3796         (setq threads gnus-newsgroup-threads))
3797       ;; We splice the new data into the data structure.
3798       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3799       ;;!!! then we want to insert at the beginning of the buffer.
3800       ;;!!! That happens to be true with Gnus now, but that may
3801       ;;!!! change in the future.  Perhaps.
3802       (gnus-data-enter-list
3803        (if line nil current) data (- (point) old-pos))
3804       (setq gnus-newsgroup-threads
3805             (nconc threads gnus-newsgroup-threads))
3806       (gnus-data-compute-positions))))
3807
3808 (defun gnus-number-to-header (number)
3809   "Return the header for article NUMBER."
3810   (let ((headers gnus-newsgroup-headers))
3811     (while (and headers
3812                 (not (= number (mail-header-number (car headers)))))
3813       (pop headers))
3814     (when headers
3815       (car headers))))
3816
3817 (defun gnus-parent-headers (in-headers &optional generation)
3818   "Return the headers of the GENERATIONeth parent of HEADERS."
3819   (unless generation
3820     (setq generation 1))
3821   (let ((parent t)
3822         (headers in-headers)
3823         references)
3824     (while (and parent
3825                 (not (zerop generation))
3826                 (setq references (mail-header-references headers)))
3827       (setq headers (if (and references
3828                              (setq parent (gnus-parent-id references)))
3829                         (car (gnus-id-to-thread parent))
3830                       nil))
3831       (decf generation))
3832     (and (not (eq headers in-headers))
3833          headers)))
3834
3835 (defun gnus-id-to-thread (id)
3836   "Return the (sub-)thread where ID appears."
3837   (gnus-gethash id gnus-newsgroup-dependencies))
3838
3839 (defun gnus-id-to-article (id)
3840   "Return the article number of ID."
3841   (let ((thread (gnus-id-to-thread id)))
3842     (when (and thread
3843                (car thread))
3844       (mail-header-number (car thread)))))
3845
3846 (defun gnus-id-to-header (id)
3847   "Return the article headers of ID."
3848   (car (gnus-id-to-thread id)))
3849
3850 (defun gnus-article-displayed-root-p (article)
3851   "Say whether ARTICLE is a root(ish) article."
3852   (let ((level (gnus-summary-thread-level article))
3853         (refs (mail-header-references  (gnus-summary-article-header article)))
3854         particle)
3855     (cond
3856      ((null level) nil)
3857      ((zerop level) t)
3858      ((null refs) t)
3859      ((null (gnus-parent-id refs)) t)
3860      ((and (= 1 level)
3861            (null (setq particle (gnus-id-to-article
3862                                  (gnus-parent-id refs))))
3863            (null (gnus-summary-thread-level particle)))))))
3864
3865 (defun gnus-root-id (id)
3866   "Return the id of the root of the thread where ID appears."
3867   (let (last-id prev)
3868     (while (and id (setq prev (car (gnus-id-to-thread id))))
3869       (setq last-id id
3870             id (gnus-parent-id (mail-header-references prev))))
3871     last-id))
3872
3873 (defun gnus-articles-in-thread (thread)
3874   "Return the list of articles in THREAD."
3875   (cons (mail-header-number (car thread))
3876         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3877
3878 (defun gnus-remove-thread (id &optional dont-remove)
3879   "Remove the thread that has ID in it."
3880   (let (headers thread last-id)
3881     ;; First go up in this thread until we find the root.
3882     (setq last-id (gnus-root-id id)
3883           headers (message-flatten-list (gnus-id-to-thread last-id)))
3884     ;; We have now found the real root of this thread.  It might have
3885     ;; been gathered into some loose thread, so we have to search
3886     ;; through the threads to find the thread we wanted.
3887     (let ((threads gnus-newsgroup-threads)
3888           sub)
3889       (while threads
3890         (setq sub (car threads))
3891         (if (stringp (car sub))
3892             ;; This is a gathered thread, so we look at the roots
3893             ;; below it to find whether this article is in this
3894             ;; gathered root.
3895             (progn
3896               (setq sub (cdr sub))
3897               (while sub
3898                 (when (member (caar sub) headers)
3899                   (setq thread (car threads)
3900                         threads nil
3901                         sub nil))
3902                 (setq sub (cdr sub))))
3903           ;; It's an ordinary thread, so we check it.
3904           (when (eq (car sub) (car headers))
3905             (setq thread sub
3906                   threads nil)))
3907         (setq threads (cdr threads)))
3908       ;; If this article is in no thread, then it's a root.
3909       (if thread
3910           (unless dont-remove
3911             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3912         (setq thread (gnus-id-to-thread last-id)))
3913       (when thread
3914         (prog1
3915             thread                      ; We return this thread.
3916           (unless dont-remove
3917             (if (stringp (car thread))
3918                 (progn
3919                   ;; If we use dummy roots, then we have to remove the
3920                   ;; dummy root as well.
3921                   (when (eq gnus-summary-make-false-root 'dummy)
3922                     ;; We go to the dummy root by going to
3923                     ;; the first sub-"thread", and then one line up.
3924                     (gnus-summary-goto-article
3925                      (mail-header-number (caadr thread)))
3926                     (forward-line -1)
3927                     (gnus-delete-line)
3928                     (gnus-data-compute-positions))
3929                   (setq thread (cdr thread))
3930                   (while thread
3931                     (gnus-remove-thread-1 (car thread))
3932                     (setq thread (cdr thread))))
3933               (gnus-remove-thread-1 thread))))))))
3934
3935 (defun gnus-remove-thread-1 (thread)
3936   "Remove the thread THREAD recursively."
3937   (let ((number (mail-header-number (pop thread)))
3938         d)
3939     (setq thread (reverse thread))
3940     (while thread
3941       (gnus-remove-thread-1 (pop thread)))
3942     (when (setq d (gnus-data-find number))
3943       (goto-char (gnus-data-pos d))
3944       (gnus-summary-show-thread)
3945       (gnus-data-remove
3946        number
3947        (- (gnus-point-at-bol)
3948           (prog1
3949               (1+ (gnus-point-at-eol))
3950             (gnus-delete-line)))))))
3951
3952 (defun gnus-sort-threads-1 (threads func)
3953   (sort (mapcar (lambda (thread)
3954                   (cons (car thread)
3955                         (and (cdr thread)
3956                              (gnus-sort-threads-1 (cdr thread) func))))
3957                 threads) func))
3958
3959 (defun gnus-sort-threads (threads)
3960   "Sort THREADS."
3961   (if (not gnus-thread-sort-functions)
3962       threads
3963     (gnus-message 8 "Sorting threads...")
3964     (prog1
3965         (gnus-sort-threads-1
3966          threads
3967          (gnus-make-sort-function gnus-thread-sort-functions))
3968       (gnus-message 8 "Sorting threads...done"))))
3969
3970 (defun gnus-sort-articles (articles)
3971   "Sort ARTICLES."
3972   (when gnus-article-sort-functions
3973     (gnus-message 7 "Sorting articles...")
3974     (prog1
3975         (setq gnus-newsgroup-headers
3976               (sort articles (gnus-make-sort-function
3977                               gnus-article-sort-functions)))
3978       (gnus-message 7 "Sorting articles...done"))))
3979
3980 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3981 (defmacro gnus-thread-header (thread)
3982   "Return header of first article in THREAD.
3983 Note that THREAD must never, ever be anything else than a variable -
3984 using some other form will lead to serious barfage."
3985   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3986   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3987   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3988         (vector thread) 2))
3989
3990 (defsubst gnus-article-sort-by-number (h1 h2)
3991   "Sort articles by article number."
3992   (< (mail-header-number h1)
3993      (mail-header-number h2)))
3994
3995 (defun gnus-thread-sort-by-number (h1 h2)
3996   "Sort threads by root article number."
3997   (gnus-article-sort-by-number
3998    (gnus-thread-header h1) (gnus-thread-header h2)))
3999
4000 (defsubst gnus-article-sort-by-lines (h1 h2)
4001   "Sort articles by article Lines header."
4002   (< (mail-header-lines h1)
4003      (mail-header-lines h2)))
4004
4005 (defun gnus-thread-sort-by-lines (h1 h2)
4006   "Sort threads by root article Lines header."
4007   (gnus-article-sort-by-lines
4008    (gnus-thread-header h1) (gnus-thread-header h2)))
4009
4010 (defsubst gnus-article-sort-by-chars (h1 h2)
4011   "Sort articles by octet length."
4012   (< (mail-header-chars h1)
4013      (mail-header-chars h2)))
4014
4015 (defun gnus-thread-sort-by-chars (h1 h2)
4016   "Sort threads by root article octet length."
4017   (gnus-article-sort-by-chars
4018    (gnus-thread-header h1) (gnus-thread-header h2)))
4019
4020 (defsubst gnus-article-sort-by-author (h1 h2)
4021   "Sort articles by root author."
4022   (string-lessp
4023    (let ((addr (car (mime-entity-read-field h1 'From))))
4024      (or (std11-full-name-string addr)
4025          (std11-address-string addr)
4026          ""))
4027    (let ((addr (car (mime-entity-read-field h2 'From))))
4028      (or (std11-full-name-string addr)
4029          (std11-address-string addr)
4030          ""))
4031    ))
4032
4033 (defun gnus-thread-sort-by-author (h1 h2)
4034   "Sort threads by root author."
4035   (gnus-article-sort-by-author
4036    (gnus-thread-header h1)  (gnus-thread-header h2)))
4037
4038 (defsubst gnus-article-sort-by-subject (h1 h2)
4039   "Sort articles by root subject."
4040   (string-lessp
4041    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4042    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4043
4044 (defun gnus-thread-sort-by-subject (h1 h2)
4045   "Sort threads by root subject."
4046   (gnus-article-sort-by-subject
4047    (gnus-thread-header h1) (gnus-thread-header h2)))
4048
4049 (defsubst gnus-article-sort-by-date (h1 h2)
4050   "Sort articles by root article date."
4051   (time-less-p
4052    (gnus-date-get-time (mail-header-date h1))
4053    (gnus-date-get-time (mail-header-date h2))))
4054
4055 (defun gnus-thread-sort-by-date (h1 h2)
4056   "Sort threads by root article date."
4057   (gnus-article-sort-by-date
4058    (gnus-thread-header h1) (gnus-thread-header h2)))
4059
4060 (defsubst gnus-article-sort-by-score (h1 h2)
4061   "Sort articles by root article score.
4062 Unscored articles will be counted as having a score of zero."
4063   (> (or (cdr (assq (mail-header-number h1)
4064                     gnus-newsgroup-scored))
4065          gnus-summary-default-score 0)
4066      (or (cdr (assq (mail-header-number h2)
4067                     gnus-newsgroup-scored))
4068          gnus-summary-default-score 0)))
4069
4070 (defun gnus-thread-sort-by-score (h1 h2)
4071   "Sort threads by root article score."
4072   (gnus-article-sort-by-score
4073    (gnus-thread-header h1) (gnus-thread-header h2)))
4074
4075 (defun gnus-thread-sort-by-total-score (h1 h2)
4076   "Sort threads by the sum of all scores in the thread.
4077 Unscored articles will be counted as having a score of zero."
4078   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4079
4080 (defun gnus-thread-total-score (thread)
4081   ;; This function find the total score of THREAD.
4082   (cond ((null thread)
4083          0)
4084         ((consp thread)
4085          (if (stringp (car thread))
4086              (apply gnus-thread-score-function 0
4087                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4088            (gnus-thread-total-score-1 thread)))
4089         (t
4090          (gnus-thread-total-score-1 (list thread)))))
4091
4092 (defun gnus-thread-total-score-1 (root)
4093   ;; This function find the total score of the thread below ROOT.
4094   (setq root (car root))
4095   (apply gnus-thread-score-function
4096          (or (append
4097               (mapcar 'gnus-thread-total-score
4098                       (cdr (gnus-id-to-thread (mail-header-id root))))
4099               (when (> (mail-header-number root) 0)
4100                 (list (or (cdr (assq (mail-header-number root)
4101                                      gnus-newsgroup-scored))
4102                           gnus-summary-default-score 0))))
4103              (list gnus-summary-default-score)
4104              '(0))))
4105
4106 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4107 (defvar gnus-tmp-prev-subject nil)
4108 (defvar gnus-tmp-false-parent nil)
4109 (defvar gnus-tmp-root-expunged nil)
4110 (defvar gnus-tmp-dummy-line nil)
4111
4112 (eval-when-compile (defvar gnus-tmp-header))
4113 (defun gnus-extra-header (type &optional header)
4114   "Return the extra header of TYPE."
4115   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4116       ""))
4117
4118 (defvar gnus-tmp-thread-tree-header-string "")
4119
4120 (defvar gnus-sum-thread-tree-root "> "
4121   "With %B spec, used for the root of a thread.
4122 If nil, use subject instead.")
4123 (defvar gnus-sum-thread-tree-single-indent ""
4124   "With %B spec, used for a thread with just one message.
4125 If nil, use subject instead.")
4126 (defvar gnus-sum-thread-tree-vertical "| "
4127   "With %B spec, used for drawing a vertical line.")
4128 (defvar gnus-sum-thread-tree-indent "  "
4129   "With %B spec, used for indenting.")
4130 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4131   "With %B spec, used for a leaf with brothers.")
4132 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4133   "With %B spec, used for a leaf without brothers.")
4134
4135 (defun gnus-summary-prepare-threads (threads)
4136   "Prepare summary buffer from THREADS and indentation LEVEL.
4137 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4138 or a straight list of headers."
4139   (gnus-message 7 "Generating summary...")
4140
4141   (setq gnus-newsgroup-threads threads)
4142   (beginning-of-line)
4143
4144   (let ((gnus-tmp-level 0)
4145         (default-score (or gnus-summary-default-score 0))
4146         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4147         thread number subject stack state gnus-tmp-gathered beg-match
4148         new-roots gnus-tmp-new-adopts thread-end
4149         gnus-tmp-header gnus-tmp-unread
4150         gnus-tmp-replied gnus-tmp-subject-or-nil
4151         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4152         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4153         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4154         tree-stack)
4155
4156     (setq gnus-tmp-prev-subject nil)
4157
4158     (if (vectorp (car threads))
4159         ;; If this is a straight (sic) list of headers, then a
4160         ;; threaded summary display isn't required, so we just create
4161         ;; an unthreaded one.
4162         (gnus-summary-prepare-unthreaded threads)
4163
4164       ;; Do the threaded display.
4165
4166       (while (or threads stack gnus-tmp-new-adopts new-roots)
4167
4168         (if (and (= gnus-tmp-level 0)
4169                  (or (not stack)
4170                      (= (caar stack) 0))
4171                  (not gnus-tmp-false-parent)
4172                  (or gnus-tmp-new-adopts new-roots))
4173             (if gnus-tmp-new-adopts
4174                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4175                       thread (list (car gnus-tmp-new-adopts))
4176                       gnus-tmp-header (caar thread)
4177                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4178               (when new-roots
4179                 (setq thread (list (car new-roots))
4180                       gnus-tmp-header (caar thread)
4181                       new-roots (cdr new-roots))))
4182
4183           (if threads
4184               ;; If there are some threads, we do them before the
4185               ;; threads on the stack.
4186               (setq thread threads
4187                     gnus-tmp-header (caar thread))
4188             ;; There were no current threads, so we pop something off
4189             ;; the stack.
4190             (setq state (car stack)
4191                   gnus-tmp-level (car state)
4192                   tree-stack (cadr state)
4193                   thread (caddr state)
4194                   stack (cdr stack)
4195                   gnus-tmp-header (caar thread))))
4196
4197         (setq gnus-tmp-false-parent nil)
4198         (setq gnus-tmp-root-expunged nil)
4199         (setq thread-end nil)
4200
4201         (if (stringp gnus-tmp-header)
4202             ;; The header is a dummy root.
4203             (cond
4204              ((eq gnus-summary-make-false-root 'adopt)
4205               ;; We let the first article adopt the rest.
4206               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4207                                                (cddar thread)))
4208               (setq gnus-tmp-gathered
4209                     (nconc (mapcar
4210                             (lambda (h) (mail-header-number (car h)))
4211                             (cddar thread))
4212                            gnus-tmp-gathered))
4213               (setq thread (cons (list (caar thread)
4214                                        (cadar thread))
4215                                  (cdr thread)))
4216               (setq gnus-tmp-level -1
4217                     gnus-tmp-false-parent t))
4218              ((eq gnus-summary-make-false-root 'empty)
4219               ;; We print adopted articles with empty subject fields.
4220               (setq gnus-tmp-gathered
4221                     (nconc (mapcar
4222                             (lambda (h) (mail-header-number (car h)))
4223                             (cddar thread))
4224                            gnus-tmp-gathered))
4225               (setq gnus-tmp-level -1))
4226              ((eq gnus-summary-make-false-root 'dummy)
4227               ;; We remember that we probably want to output a dummy
4228               ;; root.
4229               (setq gnus-tmp-dummy-line gnus-tmp-header)
4230               (setq gnus-tmp-prev-subject gnus-tmp-header))
4231              (t
4232               ;; We do not make a root for the gathered
4233               ;; sub-threads at all.
4234               (setq gnus-tmp-level -1)))
4235
4236           (setq number (mail-header-number gnus-tmp-header)
4237                 subject (mail-header-subject gnus-tmp-header))
4238
4239           (cond
4240            ;; If the thread has changed subject, we might want to make
4241            ;; this subthread into a root.
4242            ((and (null gnus-thread-ignore-subject)
4243                  (not (zerop gnus-tmp-level))
4244                  gnus-tmp-prev-subject
4245                  (not (inline
4246                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4247             (setq new-roots (nconc new-roots (list (car thread)))
4248                   thread-end t
4249                   gnus-tmp-header nil))
4250            ;; If the article lies outside the current limit,
4251            ;; then we do not display it.
4252            ((not (memq number gnus-newsgroup-limit))
4253             (setq gnus-tmp-gathered
4254                   (nconc (mapcar
4255                           (lambda (h) (mail-header-number (car h)))
4256                           (cdar thread))
4257                          gnus-tmp-gathered))
4258             (setq gnus-tmp-new-adopts (if (cdar thread)
4259                                           (append gnus-tmp-new-adopts
4260                                                   (cdar thread))
4261                                         gnus-tmp-new-adopts)
4262                   thread-end t
4263                   gnus-tmp-header nil)
4264             (when (zerop gnus-tmp-level)
4265               (setq gnus-tmp-root-expunged t)))
4266            ;; Perhaps this article is to be marked as read?
4267            ((and gnus-summary-mark-below
4268                  (< (or (cdr (assq number gnus-newsgroup-scored))
4269                         default-score)
4270                     gnus-summary-mark-below)
4271                  ;; Don't touch sparse articles.
4272                  (not (gnus-summary-article-sparse-p number))
4273                  (not (gnus-summary-article-ancient-p number)))
4274             (setq gnus-newsgroup-unreads
4275                   (delq number gnus-newsgroup-unreads))
4276             (if gnus-newsgroup-auto-expire
4277                 (push number gnus-newsgroup-expirable)
4278               (push (cons number gnus-low-score-mark)
4279                     gnus-newsgroup-reads))))
4280
4281           (when gnus-tmp-header
4282             ;; We may have an old dummy line to output before this
4283             ;; article.
4284             (when (and gnus-tmp-dummy-line
4285                        (gnus-subject-equal
4286                         gnus-tmp-dummy-line
4287                         (mail-header-subject gnus-tmp-header)))
4288               (gnus-summary-insert-dummy-line
4289                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4290               (setq gnus-tmp-dummy-line nil))
4291
4292             ;; Compute the mark.
4293             (setq gnus-tmp-unread (gnus-article-mark number))
4294
4295             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4296                                   gnus-tmp-header gnus-tmp-level)
4297                   gnus-newsgroup-data)
4298
4299             ;; Actually insert the line.
4300             (setq
4301              gnus-tmp-subject-or-nil
4302              (cond
4303               ((and gnus-thread-ignore-subject
4304                     gnus-tmp-prev-subject
4305                     (not (inline (gnus-subject-equal
4306                                   gnus-tmp-prev-subject subject))))
4307                subject)
4308               ((zerop gnus-tmp-level)
4309                (if (and (eq gnus-summary-make-false-root 'empty)
4310                         (memq number gnus-tmp-gathered)
4311                         gnus-tmp-prev-subject
4312                         (inline (gnus-subject-equal
4313                                  gnus-tmp-prev-subject subject)))
4314                    gnus-summary-same-subject
4315                  subject))
4316               (t gnus-summary-same-subject)))
4317             (if (and (eq gnus-summary-make-false-root 'adopt)
4318                      (= gnus-tmp-level 1)
4319                      (memq number gnus-tmp-gathered))
4320                 (setq gnus-tmp-opening-bracket ?\<
4321                       gnus-tmp-closing-bracket ?\>)
4322               (setq gnus-tmp-opening-bracket ?\[
4323                     gnus-tmp-closing-bracket ?\]))
4324             (setq
4325              gnus-tmp-indentation
4326              (aref gnus-thread-indent-array gnus-tmp-level)
4327              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4328              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4329                                 gnus-summary-default-score 0)
4330              gnus-tmp-score-char
4331              (if (or (null gnus-summary-default-score)
4332                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4333                          gnus-summary-zcore-fuzz))
4334                  ?\ ;;;Whitespace
4335                (if (< gnus-tmp-score gnus-summary-default-score)
4336                    gnus-score-below-mark gnus-score-over-mark))
4337              gnus-tmp-replied
4338              (cond ((memq number gnus-newsgroup-processable)
4339                     gnus-process-mark)
4340                    ((memq number gnus-newsgroup-cached)
4341                     gnus-cached-mark)
4342                    ((memq number gnus-newsgroup-replied)
4343                     gnus-replied-mark)
4344                    ((memq number gnus-newsgroup-forwarded)
4345                     gnus-forwarded-mark)
4346                    ((memq number gnus-newsgroup-saved)
4347                     gnus-saved-mark)
4348                    (t gnus-no-mark))
4349              gnus-tmp-from (mail-header-from gnus-tmp-header)
4350              gnus-tmp-name
4351              (cond
4352               ((string-match "<[^>]+> *$" gnus-tmp-from)
4353                (setq beg-match (match-beginning 0))
4354                (or (and (string-match "^\".+\"" gnus-tmp-from)
4355                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4356                    (substring gnus-tmp-from 0 beg-match)))
4357               ((string-match "(.+)" gnus-tmp-from)
4358                (substring gnus-tmp-from
4359                           (1+ (match-beginning 0)) (1- (match-end 0))))
4360               (t gnus-tmp-from))
4361              gnus-tmp-thread-tree-header-string
4362              (cond
4363               ((not gnus-show-threads) "")
4364               ((zerop gnus-tmp-level)
4365                (if (cdar thread)
4366                    (or gnus-sum-thread-tree-root subject)
4367                  (or gnus-sum-thread-tree-single-indent subject)))
4368               (t
4369                (concat (apply 'concat
4370                               (mapcar (lambda (item)
4371                                         (if (= item 1)
4372                                             gnus-sum-thread-tree-vertical
4373                                           gnus-sum-thread-tree-indent))
4374                                       (cdr (reverse tree-stack))))
4375                        (if (nth 1 thread)
4376                            gnus-sum-thread-tree-leaf-with-other
4377                          gnus-sum-thread-tree-single-leaf)))))
4378             (when (string= gnus-tmp-name "")
4379               (setq gnus-tmp-name gnus-tmp-from))
4380             (unless (numberp gnus-tmp-lines)
4381               (setq gnus-tmp-lines -1))
4382             (when (= gnus-tmp-lines -1)
4383               (setq gnus-tmp-lines "?"))
4384             (gnus-put-text-property
4385              (point)
4386              (progn (eval gnus-summary-line-format-spec) (point))
4387              'gnus-number number)
4388             (when gnus-visual-p
4389               (forward-line -1)
4390               (gnus-run-hooks 'gnus-summary-update-hook)
4391               (forward-line 1))
4392
4393             (setq gnus-tmp-prev-subject subject)))
4394
4395         (when (nth 1 thread)
4396           (push (list (max 0 gnus-tmp-level)
4397                       (copy-list tree-stack)
4398                       (nthcdr 1 thread))
4399                 stack))
4400         (push (if (nth 1 thread) 1 0) tree-stack)
4401         (incf gnus-tmp-level)
4402         (setq threads (if thread-end nil (cdar thread)))
4403         (unless threads
4404           (setq gnus-tmp-level 0)))))
4405   (gnus-message 7 "Generating summary...done"))
4406
4407 (defun gnus-summary-prepare-unthreaded (headers)
4408   "Generate an unthreaded summary buffer based on HEADERS."
4409   (let (header number mark)
4410
4411     (beginning-of-line)
4412
4413     (while headers
4414       ;; We may have to root out some bad articles...
4415       (when (memq (setq number (mail-header-number
4416                                 (setq header (pop headers))))
4417                   gnus-newsgroup-limit)
4418         ;; Mark article as read when it has a low score.
4419         (when (and gnus-summary-mark-below
4420                    (< (or (cdr (assq number gnus-newsgroup-scored))
4421                           gnus-summary-default-score 0)
4422                       gnus-summary-mark-below)
4423                    (not (gnus-summary-article-ancient-p number)))
4424           (setq gnus-newsgroup-unreads
4425                 (delq number gnus-newsgroup-unreads))
4426           (if gnus-newsgroup-auto-expire
4427               (push number gnus-newsgroup-expirable)
4428             (push (cons number gnus-low-score-mark)
4429                   gnus-newsgroup-reads)))
4430
4431         (setq mark (gnus-article-mark number))
4432         (push (gnus-data-make number mark (1+ (point)) header 0)
4433               gnus-newsgroup-data)
4434         (gnus-summary-insert-line
4435          header 0 number
4436          mark (memq number gnus-newsgroup-replied)
4437          (memq number gnus-newsgroup-expirable)
4438          (mail-header-subject header) nil
4439          (cdr (assq number gnus-newsgroup-scored))
4440          (memq number gnus-newsgroup-processable))))))
4441
4442 (defun gnus-summary-remove-list-identifiers ()
4443   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4444   (let ((regexp (if (consp gnus-list-identifiers)
4445                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4446                   gnus-list-identifiers))
4447         changed subject)
4448     (when regexp
4449       (dolist (header gnus-newsgroup-headers)
4450         (setq subject (mail-header-subject header)
4451               changed nil)
4452         (while (string-match
4453                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4454                 subject)
4455           (setq subject
4456                 (concat (substring subject 0 (match-beginning 2))
4457                         (substring subject (match-end 0)))
4458                 changed t))
4459         (when (and changed
4460                    (string-match
4461                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4462           (setq subject
4463                 (concat (substring subject 0 (match-beginning 1))
4464                         (substring subject (match-end 1)))))
4465         (when changed
4466           (mail-header-set-subject header subject))))))
4467
4468 (defun gnus-fetch-headers (articles)
4469   "Fetch headers of ARTICLES."
4470   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4471     (gnus-message 5 "Fetching headers for %s..." name)
4472     (prog1
4473         (if (eq 'nov
4474                 (setq gnus-headers-retrieved-by
4475                       (gnus-retrieve-headers
4476                        articles gnus-newsgroup-name
4477                        ;; We might want to fetch old headers, but
4478                        ;; not if there is only 1 article.
4479                        (and (or (and
4480                                  (not (eq gnus-fetch-old-headers 'some))
4481                                  (not (numberp gnus-fetch-old-headers)))
4482                                 (> (length articles) 1))
4483                             gnus-fetch-old-headers))))
4484             (gnus-get-newsgroup-headers-xover
4485              articles nil nil gnus-newsgroup-name t)
4486           (gnus-get-newsgroup-headers))
4487       (gnus-message 5 "Fetching headers for %s...done" name))))
4488
4489 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4490   "Select newsgroup GROUP.
4491 If READ-ALL is non-nil, all articles in the group are selected.
4492 If SELECT-ARTICLES, only select those articles from GROUP."
4493   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4494          ;;!!! Dirty hack; should be removed.
4495          (gnus-summary-ignore-duplicates
4496           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4497               t
4498             gnus-summary-ignore-duplicates))
4499          (info (nth 2 entry))
4500          articles fetched-articles cached)
4501
4502     (unless (gnus-check-server
4503              (setq gnus-current-select-method
4504                    (gnus-find-method-for-group group)))
4505       (error "Couldn't open server"))
4506
4507     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4508         (gnus-activate-group group)     ; Or we can activate it...
4509         (progn                          ; Or we bug out.
4510           (when (equal major-mode 'gnus-summary-mode)
4511             (kill-buffer (current-buffer)))
4512           (error "Couldn't activate group %s: %s"
4513                  group (gnus-status-message group))))
4514
4515     (unless (gnus-request-group group t)
4516       (when (equal major-mode 'gnus-summary-mode)
4517         (kill-buffer (current-buffer)))
4518       (error "Couldn't request group %s: %s"
4519              group (gnus-status-message group)))
4520
4521     (setq gnus-newsgroup-name group)
4522     (setq gnus-newsgroup-unselected nil)
4523     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4524     (gnus-summary-setup-default-charset)
4525
4526     ;; Adjust and set lists of article marks.
4527     (when info
4528       (gnus-adjust-marked-articles info))
4529
4530     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4531     (when (gnus-virtual-group-p group)
4532       (setq cached gnus-newsgroup-cached))
4533
4534     (setq gnus-newsgroup-unreads
4535           (gnus-set-difference
4536            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4537            gnus-newsgroup-dormant))
4538
4539     (setq gnus-newsgroup-processable nil)
4540
4541     (gnus-update-read-articles group gnus-newsgroup-unreads)
4542
4543     (if (setq articles select-articles)
4544         (setq gnus-newsgroup-unselected
4545               (gnus-sorted-intersection
4546                gnus-newsgroup-unreads
4547                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4548       (setq articles (gnus-articles-to-read group read-all)))
4549
4550     (cond
4551      ((null articles)
4552       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4553       'quit)
4554      ((eq articles 0) nil)
4555      (t
4556       ;; Init the dependencies hash table.
4557       (setq gnus-newsgroup-dependencies
4558             (gnus-make-hashtable (length articles)))
4559       (gnus-set-global-variables)
4560       ;; Retrieve the headers and read them in.
4561       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4562
4563       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4564       (when cached
4565         (setq gnus-newsgroup-cached cached))
4566
4567       ;; Suppress duplicates?
4568       (when gnus-suppress-duplicates
4569         (gnus-dup-suppress-articles))
4570
4571       ;; Set the initial limit.
4572       (setq gnus-newsgroup-limit (copy-sequence articles))
4573       ;; Remove canceled articles from the list of unread articles.
4574       (setq gnus-newsgroup-unreads
4575             (gnus-set-sorted-intersection
4576              gnus-newsgroup-unreads
4577              (setq fetched-articles
4578                    (mapcar (lambda (headers) (mail-header-number headers))
4579                            gnus-newsgroup-headers))))
4580       ;; Removed marked articles that do not exist.
4581       (gnus-update-missing-marks
4582        (gnus-sorted-complement fetched-articles articles))
4583       ;; We might want to build some more threads first.
4584       (when (and gnus-fetch-old-headers
4585                  (eq gnus-headers-retrieved-by 'nov))
4586         (if (eq gnus-fetch-old-headers 'invisible)
4587             (gnus-build-all-threads)
4588           (gnus-build-old-threads)))
4589       ;; Let the Gnus agent mark articles as read.
4590       (when gnus-agent
4591         (gnus-agent-get-undownloaded-list))
4592       ;; Remove list identifiers from subject
4593       (when gnus-list-identifiers
4594         (gnus-summary-remove-list-identifiers))
4595       ;; Check whether auto-expire is to be done in this group.
4596       (setq gnus-newsgroup-auto-expire
4597             (gnus-group-auto-expirable-p group))
4598       ;; Set up the article buffer now, if necessary.
4599       (unless gnus-single-article-buffer
4600         (gnus-article-setup-buffer))
4601       ;; First and last article in this newsgroup.
4602       (when gnus-newsgroup-headers
4603         (setq gnus-newsgroup-begin
4604               (mail-header-number (car gnus-newsgroup-headers))
4605               gnus-newsgroup-end
4606               (mail-header-number
4607                (gnus-last-element gnus-newsgroup-headers))))
4608       ;; GROUP is successfully selected.
4609       (or gnus-newsgroup-headers t)))))
4610
4611 (defun gnus-articles-to-read (group &optional read-all)
4612   "Find out what articles the user wants to read."
4613   (let* ((articles
4614           ;; Select all articles if `read-all' is non-nil, or if there
4615           ;; are no unread articles.
4616           (if (or read-all
4617                   (and (zerop (length gnus-newsgroup-marked))
4618                        (zerop (length gnus-newsgroup-unreads)))
4619                   (eq (gnus-group-find-parameter group 'display)
4620                       'all))
4621               (or
4622                (gnus-uncompress-range (gnus-active group))
4623                (gnus-cache-articles-in-group group))
4624             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4625                           (copy-sequence gnus-newsgroup-unreads))
4626                   '<)))
4627          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4628          (scored (length scored-list))
4629          (number (length articles))
4630          (marked (+ (length gnus-newsgroup-marked)
4631                     (length gnus-newsgroup-dormant)))
4632          (select
4633           (cond
4634            ((numberp read-all)
4635             read-all)
4636            (t
4637             (condition-case ()
4638                 (cond
4639                  ((and (or (<= scored marked) (= scored number))
4640                        (natnump gnus-large-newsgroup)
4641                        (> number gnus-large-newsgroup))
4642                   (let* ((cursor-in-echo-area nil)
4643                          (input
4644                           (read-from-minibuffer
4645                            (format
4646                             "How many articles from %s (max %d): "
4647                             (gnus-limit-string
4648                              (gnus-group-decoded-name gnus-newsgroup-name)
4649                              35)
4650                             number)
4651                            (cons (number-to-string gnus-large-newsgroup)
4652                                  0))))
4653                     (if (string-match "^[ \t]*$" input)
4654                         number
4655                       input)))
4656                  ((and (> scored marked) (< scored number)
4657                        (> (- scored number) 20))
4658                   (let ((input
4659                          (read-string
4660                           (format "%s %s (%d scored, %d total): "
4661                                   "How many articles from"
4662                                   (gnus-group-decoded-name group)
4663                                   scored number))))
4664                     (if (string-match "^[ \t]*$" input)
4665                         number input)))
4666                  (t number))
4667               (quit
4668                (message "Quit getting the articles to read")
4669                nil))))))
4670     (setq select (if (stringp select) (string-to-number select) select))
4671     (if (or (null select) (zerop select))
4672         select
4673       (if (and (not (zerop scored)) (<= (abs select) scored))
4674           (progn
4675             (setq articles (sort scored-list '<))
4676             (setq number (length articles)))
4677         (setq articles (copy-sequence articles)))
4678
4679       (when (< (abs select) number)
4680         (if (< select 0)
4681             ;; Select the N oldest articles.
4682             (setcdr (nthcdr (1- (abs select)) articles) nil)
4683           ;; Select the N most recent articles.
4684           (setq articles (nthcdr (- number select) articles))))
4685       (setq gnus-newsgroup-unselected
4686             (gnus-sorted-intersection
4687              gnus-newsgroup-unreads
4688              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4689       (when gnus-alter-articles-to-read-function
4690         (setq gnus-newsgroup-unreads
4691               (sort
4692                (funcall gnus-alter-articles-to-read-function
4693                         gnus-newsgroup-name gnus-newsgroup-unreads)
4694                '<)))
4695       articles)))
4696
4697 (defun gnus-killed-articles (killed articles)
4698   (let (out)
4699     (while articles
4700       (when (inline (gnus-member-of-range (car articles) killed))
4701         (push (car articles) out))
4702       (setq articles (cdr articles)))
4703     out))
4704
4705 (defun gnus-uncompress-marks (marks)
4706   "Uncompress the mark ranges in MARKS."
4707   (let ((uncompressed '(score bookmark))
4708         out)
4709     (while marks
4710       (if (memq (caar marks) uncompressed)
4711           (push (car marks) out)
4712         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4713       (setq marks (cdr marks)))
4714     out))
4715
4716 (defun gnus-adjust-marked-articles (info)
4717   "Set all article lists and remove all marks that are no longer valid."
4718   (let* ((marked-lists (gnus-info-marks info))
4719          (active (gnus-active (gnus-info-group info)))
4720          (min (car active))
4721          (max (cdr active))
4722          (types gnus-article-mark-lists)
4723          (uncompressed '(score bookmark killed))
4724          marks var articles article mark)
4725
4726     (while marked-lists
4727       (setq marks (pop marked-lists))
4728       (set (setq var (intern (format "gnus-newsgroup-%s"
4729                                      (car (rassq (setq mark (car marks))
4730                                                  types)))))
4731            (if (memq (car marks) uncompressed) (cdr marks)
4732              (gnus-uncompress-range (cdr marks))))
4733
4734       (setq articles (symbol-value var))
4735
4736       ;; All articles have to be subsets of the active articles.
4737       (cond
4738        ;; Adjust "simple" lists.
4739        ((memq mark '(tick dormant expire reply save))
4740         (while articles
4741           (when (or (< (setq article (pop articles)) min) (> article max))
4742             (set var (delq article (symbol-value var))))))
4743        ;; Adjust assocs.
4744        ((memq mark uncompressed)
4745         (when (not (listp (cdr (symbol-value var))))
4746           (set var (list (symbol-value var))))
4747         (when (not (listp (cdr articles)))
4748           (setq articles (list articles)))
4749         (while articles
4750           (when (or (not (consp (setq article (pop articles))))
4751                     (< (car article) min)
4752                     (> (car article) max))
4753             (set var (delq article (symbol-value var))))))))))
4754
4755 (defun gnus-update-missing-marks (missing)
4756   "Go through the list of MISSING articles and remove them from the mark lists."
4757   (when missing
4758     (let ((types gnus-article-mark-lists)
4759           var m)
4760       ;; Go through all types.
4761       (while types
4762         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4763         (when (symbol-value var)
4764           ;; This list has articles.  So we delete all missing articles
4765           ;; from it.
4766           (setq m missing)
4767           (while m
4768             (set var (delq (pop m) (symbol-value var)))))))))
4769
4770 (defun gnus-update-marks ()
4771   "Enter the various lists of marked articles into the newsgroup info list."
4772   (let ((types gnus-article-mark-lists)
4773         (info (gnus-get-info gnus-newsgroup-name))
4774         (uncompressed '(score bookmark killed))
4775         type list newmarked symbol delta-marks)
4776     (when info
4777       ;; Add all marks lists to the list of marks lists.
4778       (while (setq type (pop types))
4779         (setq list (symbol-value
4780                     (setq symbol
4781                           (intern (format "gnus-newsgroup-%s"
4782                                           (car type))))))
4783
4784         (when list
4785           ;; Get rid of the entries of the articles that have the
4786           ;; default score.
4787           (when (and (eq (cdr type) 'score)
4788                      gnus-save-score
4789                      list)
4790             (let* ((arts list)
4791                    (prev (cons nil list))
4792                    (all prev))
4793               (while arts
4794                 (if (or (not (consp (car arts)))
4795                         (= (cdar arts) gnus-summary-default-score))
4796                     (setcdr prev (cdr arts))
4797                   (setq prev arts))
4798                 (setq arts (cdr arts)))
4799               (setq list (cdr all)))))
4800
4801         (unless (memq (cdr type) uncompressed)
4802           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4803
4804         (when (gnus-check-backend-function
4805                'request-set-mark gnus-newsgroup-name)
4806           ;; propagate flags to server, with the following exceptions:
4807           ;; uncompressed:s are not proper flags (they are cons cells)
4808           ;; cache is a internal gnus flag
4809           ;; download are local to one gnus installation (well)
4810           ;; unsend are for nndraft groups only
4811           ;; xxx: generality of this?  this suits nnimap anyway
4812           (unless (memq (cdr type) (append '(cache download unsend)
4813                                            uncompressed))
4814             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4815                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4816                    (add (gnus-remove-from-range
4817                          (gnus-copy-sequence list) old)))
4818               (when add
4819                 (push (list add 'add (list (cdr type))) delta-marks))
4820               (when del
4821                 (push (list del 'del (list (cdr type))) delta-marks)))))
4822
4823         (when list
4824           (push (cons (cdr type) list) newmarked)))
4825
4826       (when delta-marks
4827         (unless (gnus-check-group gnus-newsgroup-name)
4828           (error "Can't open server for %s" gnus-newsgroup-name))
4829         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4830
4831       ;; Enter these new marks into the info of the group.
4832       (if (nthcdr 3 info)
4833           (setcar (nthcdr 3 info) newmarked)
4834         ;; Add the marks lists to the end of the info.
4835         (when newmarked
4836           (setcdr (nthcdr 2 info) (list newmarked))))
4837
4838       ;; Cut off the end of the info if there's nothing else there.
4839       (let ((i 5))
4840         (while (and (> i 2)
4841                     (not (nth i info)))
4842           (when (nthcdr (decf i) info)
4843             (setcdr (nthcdr i info) nil)))))))
4844
4845 (defun gnus-set-mode-line (where)
4846   "Set the mode line of the article or summary buffers.
4847 If WHERE is `summary', the summary mode line format will be used."
4848   ;; Is this mode line one we keep updated?
4849   (when (and (memq where gnus-updated-mode-lines)
4850              (symbol-value
4851               (intern (format "gnus-%s-mode-line-format-spec" where))))
4852     (let (mode-string)
4853       (save-excursion
4854         ;; We evaluate this in the summary buffer since these
4855         ;; variables are buffer-local to that buffer.
4856         (set-buffer gnus-summary-buffer)
4857         ;; We bind all these variables that are used in the `eval' form
4858         ;; below.
4859         (let* ((mformat (symbol-value
4860                          (intern
4861                           (format "gnus-%s-mode-line-format-spec" where))))
4862                (gnus-tmp-group-name (gnus-group-decoded-name
4863                                      gnus-newsgroup-name))
4864                (gnus-tmp-article-number (or gnus-current-article 0))
4865                (gnus-tmp-unread gnus-newsgroup-unreads)
4866                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4867                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4868                (gnus-tmp-unread-and-unselected
4869                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4870                             (zerop gnus-tmp-unselected))
4871                        "")
4872                       ((zerop gnus-tmp-unselected)
4873                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4874                       (t (format "{%d(+%d) more}"
4875                                  gnus-tmp-unread-and-unticked
4876                                  gnus-tmp-unselected))))
4877                (gnus-tmp-subject
4878                 (if (and gnus-current-headers
4879                          (vectorp gnus-current-headers))
4880                     (gnus-mode-string-quote
4881                      (mail-header-subject gnus-current-headers))
4882                   ""))
4883                bufname-length max-len
4884                gnus-tmp-header);; passed as argument to any user-format-funcs
4885           (setq mode-string (eval mformat))
4886           (setq bufname-length (if (string-match "%b" mode-string)
4887                                    (- (length
4888                                        (buffer-name
4889                                         (if (eq where 'summary)
4890                                             nil
4891                                           (get-buffer gnus-article-buffer))))
4892                                       2)
4893                                  0))
4894           (setq max-len (max 4 (if gnus-mode-non-string-length
4895                                    (- (window-width)
4896                                       gnus-mode-non-string-length
4897                                       bufname-length)
4898                                  (length mode-string))))
4899           ;; We might have to chop a bit of the string off...
4900           (when (> (length mode-string) max-len)
4901             (setq mode-string
4902                   (concat (gnus-truncate-string mode-string (- max-len 3))
4903                           "...")))
4904           ;; Pad the mode string a bit.
4905           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4906       ;; Update the mode line.
4907       (setq mode-line-buffer-identification
4908             (gnus-mode-line-buffer-identification (list mode-string)))
4909       (set-buffer-modified-p t))))
4910
4911 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4912   "Go through the HEADERS list and add all Xrefs to a hash table.
4913 The resulting hash table is returned, or nil if no Xrefs were found."
4914   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4915          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4916          (xref-hashtb (gnus-make-hashtable))
4917          start group entry number xrefs header)
4918     (while headers
4919       (setq header (pop headers))
4920       (when (and (setq xrefs (mail-header-xref header))
4921                  (not (memq (setq number (mail-header-number header))
4922                             unreads)))
4923         (setq start 0)
4924         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4925           (setq start (match-end 0))
4926           (setq group (if prefix
4927                           (concat prefix (substring xrefs (match-beginning 1)
4928                                                     (match-end 1)))
4929                         (substring xrefs (match-beginning 1) (match-end 1))))
4930           (setq number
4931                 (string-to-int (substring xrefs (match-beginning 2)
4932                                           (match-end 2))))
4933           (if (setq entry (gnus-gethash group xref-hashtb))
4934               (setcdr entry (cons number (cdr entry)))
4935             (gnus-sethash group (cons number nil) xref-hashtb)))))
4936     (and start xref-hashtb)))
4937
4938 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4939   "Look through all the headers and mark the Xrefs as read."
4940   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4941         name entry info xref-hashtb idlist method nth4)
4942     (save-excursion
4943       (set-buffer gnus-group-buffer)
4944       (when (setq xref-hashtb
4945                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4946         (mapatoms
4947          (lambda (group)
4948            (unless (string= from-newsgroup (setq name (symbol-name group)))
4949              (setq idlist (symbol-value group))
4950              ;; Dead groups are not updated.
4951              (and (prog1
4952                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4953                             info (nth 2 entry))
4954                     (when (stringp (setq nth4 (gnus-info-method info)))
4955                       (setq nth4 (gnus-server-to-method nth4))))
4956                   ;; Only do the xrefs if the group has the same
4957                   ;; select method as the group we have just read.
4958                   (or (gnus-methods-equal-p
4959                        nth4 (gnus-find-method-for-group from-newsgroup))
4960                       virtual
4961                       (equal nth4 (setq method (gnus-find-method-for-group
4962                                                 from-newsgroup)))
4963                       (and (equal (car nth4) (car method))
4964                            (equal (nth 1 nth4) (nth 1 method))))
4965                   gnus-use-cross-reference
4966                   (or (not (eq gnus-use-cross-reference t))
4967                       virtual
4968                       ;; Only do cross-references on subscribed
4969                       ;; groups, if that is what is wanted.
4970                       (<= (gnus-info-level info) gnus-level-subscribed))
4971                   (gnus-group-make-articles-read name idlist))))
4972          xref-hashtb)))))
4973
4974 (defun gnus-compute-read-articles (group articles)
4975   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4976          (info (nth 2 entry))
4977          (active (gnus-active group))
4978          ninfo)
4979     (when entry
4980       ;; First peel off all invalid article numbers.
4981       (when active
4982         (let ((ids articles)
4983               id first)
4984           (while (setq id (pop ids))
4985             (when (and first (> id (cdr active)))
4986               ;; We'll end up in this situation in one particular
4987               ;; obscure situation.  If you re-scan a group and get
4988               ;; a new article that is cross-posted to a different
4989               ;; group that has not been re-scanned, you might get
4990               ;; crossposted article that has a higher number than
4991               ;; Gnus believes possible.  So we re-activate this
4992               ;; group as well.  This might mean doing the
4993               ;; crossposting thingy will *increase* the number
4994               ;; of articles in some groups.  Tsk, tsk.
4995               (setq active (or (gnus-activate-group group) active)))
4996             (when (or (> id (cdr active))
4997                       (< id (car active)))
4998               (setq articles (delq id articles))))))
4999       ;; If the read list is nil, we init it.
5000       (if (and active
5001                (null (gnus-info-read info))
5002                (> (car active) 1))
5003           (setq ninfo (cons 1 (1- (car active))))
5004         (setq ninfo (gnus-info-read info)))
5005       ;; Then we add the read articles to the range.
5006       (gnus-add-to-range
5007        ninfo (setq articles (sort articles '<))))))
5008
5009 (defun gnus-group-make-articles-read (group articles)
5010   "Update the info of GROUP to say that ARTICLES are read."
5011   (let* ((num 0)
5012          (entry (gnus-gethash group gnus-newsrc-hashtb))
5013          (info (nth 2 entry))
5014          (active (gnus-active group))
5015          range)
5016     (when entry
5017       (setq range (gnus-compute-read-articles group articles))
5018       (save-excursion
5019         (set-buffer gnus-group-buffer)
5020         (gnus-undo-register
5021           `(progn
5022              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5023              (gnus-info-set-read ',info ',(gnus-info-read info))
5024              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5025              (gnus-group-update-group ,group t))))
5026       ;; Add the read articles to the range.
5027       (gnus-info-set-read info range)
5028       ;; Then we have to re-compute how many unread
5029       ;; articles there are in this group.
5030       (when active
5031         (cond
5032          ((not range)
5033           (setq num (- (1+ (cdr active)) (car active))))
5034          ((not (listp (cdr range)))
5035           (setq num (- (cdr active) (- (1+ (cdr range))
5036                                        (car range)))))
5037          (t
5038           (while range
5039             (if (numberp (car range))
5040                 (setq num (1+ num))
5041               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5042             (setq range (cdr range)))
5043           (setq num (- (cdr active) num))))
5044         ;; Update the number of unread articles.
5045         (setcar entry num)
5046         ;; Update the group buffer.
5047         (gnus-group-update-group group t)))))
5048
5049 (defvar gnus-newsgroup-none-id 0)
5050
5051 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5052   (let ((cur nntp-server-buffer)
5053         (dependencies
5054          (or dependencies
5055              (save-excursion (set-buffer gnus-summary-buffer)
5056                              gnus-newsgroup-dependencies)))
5057         headers id end ref
5058         (mail-parse-charset gnus-newsgroup-charset)
5059         (mail-parse-ignored-charsets
5060          (save-excursion (condition-case nil
5061                              (set-buffer gnus-summary-buffer)
5062                            (error))
5063                          gnus-newsgroup-ignored-charsets)))
5064     (save-excursion
5065       (set-buffer nntp-server-buffer)
5066       ;; Translate all TAB characters into SPACE characters.
5067       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5068       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5069       (gnus-run-hooks 'gnus-parse-headers-hook)
5070       (let ((case-fold-search t)
5071             in-reply-to header p lines chars ctype)
5072         (goto-char (point-min))
5073         ;; Search to the beginning of the next header.  Error messages
5074         ;; do not begin with 2 or 3.
5075         (while (re-search-forward "^[23][0-9]+ " nil t)
5076           (setq id nil
5077                 ref nil)
5078           ;; This implementation of this function, with nine
5079           ;; search-forwards instead of the one re-search-forward and
5080           ;; a case (which basically was the old function) is actually
5081           ;; about twice as fast, even though it looks messier.  You
5082           ;; can't have everything, I guess.  Speed and elegance
5083           ;; doesn't always go hand in hand.
5084           (setq
5085            header
5086            (make-full-mail-header
5087             ;; Number.
5088             (prog1
5089                 (read cur)
5090               (end-of-line)
5091               (setq p (point))
5092               (narrow-to-region (point)
5093                                 (or (and (search-forward "\n.\n" nil t)
5094                                          (- (point) 2))
5095                                     (point))))
5096             ;; Subject.
5097             (progn
5098               (goto-char p)
5099               (if (search-forward "\nsubject:" nil t)
5100                   (nnheader-header-value)
5101                 "(none)"))
5102             ;; From.
5103             (progn
5104               (goto-char p)
5105               (if (search-forward "\nfrom:" nil t)
5106                   (nnheader-header-value)
5107                 "(nobody)"))
5108             ;; Date.
5109             (progn
5110               (goto-char p)
5111               (if (search-forward "\ndate:" nil t)
5112                   (nnheader-header-value) ""))
5113             ;; Message-ID.
5114             (progn
5115               (goto-char p)
5116               (setq id (if (re-search-forward
5117                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5118                            ;; We do it this way to make sure the Message-ID
5119                            ;; is (somewhat) syntactically valid.
5120                            (buffer-substring (match-beginning 1)
5121                                              (match-end 1))
5122                          ;; If there was no message-id, we just fake one
5123                          ;; to make subsequent routines simpler.
5124                          (nnheader-generate-fake-message-id))))
5125             ;; References.
5126             (progn
5127               (goto-char p)
5128               (if (search-forward "\nreferences:" nil t)
5129                   (progn
5130                     (setq end (point))
5131                     (prog1
5132                         (nnheader-header-value)
5133                       (setq ref
5134                             (buffer-substring
5135                              (progn
5136                                ;; (end-of-line)
5137                                (search-backward ">" end t)
5138                                (1+ (point)))
5139                              (progn
5140                                (search-backward "<" end t)
5141                                (point))))))
5142                 ;; Get the references from the in-reply-to header if there
5143                 ;; were no references and the in-reply-to header looks
5144                 ;; promising.
5145                 (if (and (search-forward "\nin-reply-to:" nil t)
5146                          (setq in-reply-to (nnheader-header-value))
5147                          (string-match "<[^>]+>" in-reply-to))
5148                     (let (ref2)
5149                       (setq ref (substring in-reply-to (match-beginning 0)
5150                                            (match-end 0)))
5151                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5152                         (setq ref2 (substring in-reply-to (match-beginning 0)
5153                                               (match-end 0)))
5154                         (when (> (length ref2) (length ref))
5155                           (setq ref ref2)))
5156                       ref)
5157                   (setq ref nil))))
5158             ;; Chars.
5159             (progn
5160               (goto-char p)
5161               (if (search-forward "\nchars: " nil t)
5162                   (if (numberp (setq chars (ignore-errors (read cur))))
5163                       chars -1)
5164                 -1))
5165             ;; Lines.
5166             (progn
5167               (goto-char p)
5168               (if (search-forward "\nlines: " nil t)
5169                   (if (numberp (setq lines (ignore-errors (read cur))))
5170                       lines -1)
5171                 -1))
5172             ;; Xref.
5173             (progn
5174               (goto-char p)
5175               (and (search-forward "\nxref:" nil t)
5176                    (nnheader-header-value)))
5177             ;; Extra.
5178             (when gnus-extra-headers
5179               (let ((extra gnus-extra-headers)
5180                     out)
5181                 (while extra
5182                   (goto-char p)
5183                   (when (search-forward
5184                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5185                     (push (cons (car extra) (nnheader-header-value)) out))
5186                   (pop extra))
5187                 out))))
5188           (goto-char p)
5189           (if (and (search-forward "\ncontent-type: " nil t)
5190                    (setq ctype (nnheader-header-value)))
5191               (mime-entity-set-content-type-internal
5192                header (mime-parse-Content-Type ctype)))
5193           (when (equal id ref)
5194             (setq ref nil))
5195
5196           (when gnus-alter-header-function
5197             (funcall gnus-alter-header-function header)
5198             (setq id (mail-header-id header)
5199                   ref (gnus-parent-id (mail-header-references header))))
5200
5201           (when (setq header
5202                       (gnus-dependencies-add-header
5203                        header dependencies force-new))
5204             (push header headers))
5205           (goto-char (point-max))
5206           (widen))
5207         (nreverse headers)))))
5208
5209 ;; Goes through the xover lines and returns a list of vectors
5210 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5211                                                   force-new dependencies
5212                                                   group also-fetch-heads)
5213   "Parse the news overview data in the server buffer.
5214 Return a list of headers that match SEQUENCE (see
5215 `nntp-retrieve-headers')."
5216   ;; Get the Xref when the users reads the articles since most/some
5217   ;; NNTP servers do not include Xrefs when using XOVER.
5218   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5219   (let ((mail-parse-charset gnus-newsgroup-charset)
5220         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5221         (cur nntp-server-buffer)
5222         (dependencies (or dependencies gnus-newsgroup-dependencies))
5223         (allp (cond
5224                ((eq gnus-read-all-available-headers t)
5225                 t)
5226                ((stringp gnus-read-all-available-headers)
5227                 (string-match gnus-read-all-available-headers group))
5228                (t
5229                 nil)))
5230         number headers header)
5231     (save-excursion
5232       (set-buffer nntp-server-buffer)
5233       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5234       ;; Allow the user to mangle the headers before parsing them.
5235       (gnus-run-hooks 'gnus-parse-headers-hook)
5236       (goto-char (point-min))
5237       (while (not (eobp))
5238         (condition-case ()
5239             (while (and (or sequence allp)
5240                         (not (eobp)))
5241               (setq number (read cur))
5242               (when (not allp)
5243                 (while (and sequence
5244                             (< (car sequence) number))
5245                   (setq sequence (cdr sequence))))
5246               (when (and (or allp
5247                              (and sequence
5248                                   (eq number (car sequence))))
5249                          (progn
5250                            (setq sequence (cdr sequence))
5251                            (setq header (inline
5252                                           (gnus-nov-parse-line
5253                                            number dependencies force-new)))))
5254                 (push header headers))
5255               (forward-line 1))
5256           (error
5257            (gnus-error 4 "Strange nov line (%d)"
5258                        (count-lines (point-min) (point)))))
5259         (forward-line 1))
5260       ;; A common bug in inn is that if you have posted an article and
5261       ;; then retrieves the active file, it will answer correctly --
5262       ;; the new article is included.  However, a NOV entry for the
5263       ;; article may not have been generated yet, so this may fail.
5264       ;; We work around this problem by retrieving the last few
5265       ;; headers using HEAD.
5266       (if (or (not also-fetch-heads)
5267               (not sequence))
5268           ;; We (probably) got all the headers.
5269           (nreverse headers)
5270         (let ((gnus-nov-is-evil t))
5271           (nconc
5272            (nreverse headers)
5273            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5274              (gnus-get-newsgroup-headers))))))))
5275
5276 (defun gnus-article-get-xrefs ()
5277   "Fill in the Xref value in `gnus-current-headers', if necessary.
5278 This is meant to be called in `gnus-article-internal-prepare-hook'."
5279   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5280                                  gnus-current-headers)))
5281     (or (not gnus-use-cross-reference)
5282         (not headers)
5283         (and (mail-header-xref headers)
5284              (not (string= (mail-header-xref headers) "")))
5285         (let ((case-fold-search t)
5286               xref)
5287           (save-restriction
5288             (nnheader-narrow-to-headers)
5289             (goto-char (point-min))
5290             (when (or (and (not (eobp))
5291                            (eq (downcase (char-after)) ?x)
5292                            (looking-at "Xref:"))
5293                       (search-forward "\nXref:" nil t))
5294               (goto-char (1+ (match-end 0)))
5295               (setq xref (buffer-substring (point)
5296                                            (progn (end-of-line) (point))))
5297               (mail-header-set-xref headers xref)))))))
5298
5299 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5300   "Find article ID and insert the summary line for that article.
5301 OLD-HEADER can either be a header or a line number to insert
5302 the subject line on."
5303   (let* ((line (and (numberp old-header) old-header))
5304          (old-header (and (vectorp old-header) old-header))
5305          (header (cond ((and old-header use-old-header)
5306                         old-header)
5307                        ((and (numberp id)
5308                              (gnus-number-to-header id))
5309                         (gnus-number-to-header id))
5310                        (t
5311                         (gnus-read-header id))))
5312          (number (and (numberp id) id))
5313          d)
5314     (when header
5315       ;; Rebuild the thread that this article is part of and go to the
5316       ;; article we have fetched.
5317       (when (and (not gnus-show-threads)
5318                  old-header)
5319         (when (and number
5320                    (setq d (gnus-data-find (mail-header-number old-header))))
5321           (goto-char (gnus-data-pos d))
5322           (gnus-data-remove
5323            number
5324            (- (gnus-point-at-bol)
5325               (prog1
5326                   (1+ (gnus-point-at-eol))
5327                 (gnus-delete-line))))))
5328       (when old-header
5329         (mail-header-set-number header (mail-header-number old-header)))
5330       (setq gnus-newsgroup-sparse
5331             (delq (setq number (mail-header-number header))
5332                   gnus-newsgroup-sparse))
5333       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5334       (push number gnus-newsgroup-limit)
5335       (gnus-rebuild-thread (mail-header-id header) line)
5336       (gnus-summary-goto-subject number nil t))
5337     (when (and (numberp number)
5338                (> number 0))
5339       ;; We have to update the boundaries even if we can't fetch the
5340       ;; article if ID is a number -- so that the next `P' or `N'
5341       ;; command will fetch the previous (or next) article even
5342       ;; if the one we tried to fetch this time has been canceled.
5343       (when (> number gnus-newsgroup-end)
5344         (setq gnus-newsgroup-end number))
5345       (when (< number gnus-newsgroup-begin)
5346         (setq gnus-newsgroup-begin number))
5347       (setq gnus-newsgroup-unselected
5348             (delq number gnus-newsgroup-unselected)))
5349     ;; Report back a success?
5350     (and header (mail-header-number header))))
5351
5352 ;;; Process/prefix in the summary buffer
5353
5354 (defun gnus-summary-work-articles (n)
5355   "Return a list of articles to be worked upon.
5356 The prefix argument, the list of process marked articles, and the
5357 current article will be taken into consideration."
5358   (save-excursion
5359     (set-buffer gnus-summary-buffer)
5360     (cond
5361      (n
5362       ;; A numerical prefix has been given.
5363       (setq n (prefix-numeric-value n))
5364       (let ((backward (< n 0))
5365             (n (abs (prefix-numeric-value n)))
5366             articles article)
5367         (save-excursion
5368           (while
5369               (and (> n 0)
5370                    (push (setq article (gnus-summary-article-number))
5371                          articles)
5372                    (if backward
5373                        (gnus-summary-find-prev nil article)
5374                      (gnus-summary-find-next nil article)))
5375             (decf n)))
5376         (nreverse articles)))
5377      ((and (gnus-region-active-p) (mark))
5378       (message "region active")
5379       ;; Work on the region between point and mark.
5380       (let ((max (max (point) (mark)))
5381             articles article)
5382         (save-excursion
5383           (goto-char (min (point) (mark)))
5384           (while
5385               (and
5386                (push (setq article (gnus-summary-article-number)) articles)
5387                (gnus-summary-find-next nil article)
5388                (< (point) max)))
5389           (nreverse articles))))
5390      (gnus-newsgroup-processable
5391       ;; There are process-marked articles present.
5392       ;; Save current state.
5393       (gnus-summary-save-process-mark)
5394       ;; Return the list.
5395       (reverse gnus-newsgroup-processable))
5396      (t
5397       ;; Just return the current article.
5398       (list (gnus-summary-article-number))))))
5399
5400 (defmacro gnus-summary-iterate (arg &rest forms)
5401   "Iterate over the process/prefixed articles and do FORMS.
5402 ARG is the interactive prefix given to the command.  FORMS will be
5403 executed with point over the summary line of the articles."
5404   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5405     `(let ((,articles (gnus-summary-work-articles ,arg)))
5406        (while ,articles
5407          (gnus-summary-goto-subject (car ,articles))
5408          ,@forms
5409          (pop ,articles)))))
5410
5411 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5412 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5413
5414 (defun gnus-summary-save-process-mark ()
5415   "Push the current set of process marked articles on the stack."
5416   (interactive)
5417   (push (copy-sequence gnus-newsgroup-processable)
5418         gnus-newsgroup-process-stack))
5419
5420 (defun gnus-summary-kill-process-mark ()
5421   "Push the current set of process marked articles on the stack and unmark."
5422   (interactive)
5423   (gnus-summary-save-process-mark)
5424   (gnus-summary-unmark-all-processable))
5425
5426 (defun gnus-summary-yank-process-mark ()
5427   "Pop the last process mark state off the stack and restore it."
5428   (interactive)
5429   (unless gnus-newsgroup-process-stack
5430     (error "Empty mark stack"))
5431   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5432
5433 (defun gnus-summary-process-mark-set (set)
5434   "Make SET into the current process marked articles."
5435   (gnus-summary-unmark-all-processable)
5436   (while set
5437     (gnus-summary-set-process-mark (pop set))))
5438
5439 ;;; Searching and stuff
5440
5441 (defun gnus-summary-search-group (&optional backward use-level)
5442   "Search for next unread newsgroup.
5443 If optional argument BACKWARD is non-nil, search backward instead."
5444   (save-excursion
5445     (set-buffer gnus-group-buffer)
5446     (when (gnus-group-search-forward
5447            backward nil (if use-level (gnus-group-group-level) nil))
5448       (gnus-group-group-name))))
5449
5450 (defun gnus-summary-best-group (&optional exclude-group)
5451   "Find the name of the best unread group.
5452 If EXCLUDE-GROUP, do not go to this group."
5453   (save-excursion
5454     (set-buffer gnus-group-buffer)
5455     (save-excursion
5456       (gnus-group-best-unread-group exclude-group))))
5457
5458 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5459   (if backward (gnus-summary-find-prev)
5460     (let* ((dummy (gnus-summary-article-intangible-p))
5461            (article (or article (gnus-summary-article-number)))
5462            (arts (gnus-data-find-list article))
5463            result)
5464       (when (and (not dummy)
5465                  (or (not gnus-summary-check-current)
5466                      (not unread)
5467                      (not (gnus-data-unread-p (car arts)))))
5468         (setq arts (cdr arts)))
5469       (when (setq result
5470                   (if unread
5471                       (progn
5472                         (while arts
5473                           (when (or (and undownloaded
5474                                          (eq gnus-undownloaded-mark
5475                                              (gnus-data-mark (car arts))))
5476                                     (gnus-data-unread-p (car arts)))
5477                             (setq result (car arts)
5478                                   arts nil))
5479                           (setq arts (cdr arts)))
5480                         result)
5481                     (car arts)))
5482         (goto-char (gnus-data-pos result))
5483         (gnus-data-number result)))))
5484
5485 (defun gnus-summary-find-prev (&optional unread article)
5486   (let* ((eobp (eobp))
5487          (article (or article (gnus-summary-article-number)))
5488          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5489          result)
5490     (when (and (not eobp)
5491                (or (not gnus-summary-check-current)
5492                    (not unread)
5493                    (not (gnus-data-unread-p (car arts)))))
5494       (setq arts (cdr arts)))
5495     (when (setq result
5496                 (if unread
5497                     (progn
5498                       (while arts
5499                         (when (gnus-data-unread-p (car arts))
5500                           (setq result (car arts)
5501                                 arts nil))
5502                         (setq arts (cdr arts)))
5503                       result)
5504                   (car arts)))
5505       (goto-char (gnus-data-pos result))
5506       (gnus-data-number result))))
5507
5508 (defun gnus-summary-find-subject (subject &optional unread backward article)
5509   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5510          (article (or article (gnus-summary-article-number)))
5511          (articles (gnus-data-list backward))
5512          (arts (gnus-data-find-list article articles))
5513          result)
5514     (when (or (not gnus-summary-check-current)
5515               (not unread)
5516               (not (gnus-data-unread-p (car arts))))
5517       (setq arts (cdr arts)))
5518     (while arts
5519       (and (or (not unread)
5520                (gnus-data-unread-p (car arts)))
5521            (vectorp (gnus-data-header (car arts)))
5522            (gnus-subject-equal
5523             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5524            (setq result (car arts)
5525                  arts nil))
5526       (setq arts (cdr arts)))
5527     (and result
5528          (goto-char (gnus-data-pos result))
5529          (gnus-data-number result))))
5530
5531 (defun gnus-summary-search-forward (&optional unread subject backward)
5532   "Search forward for an article.
5533 If UNREAD, look for unread articles.  If SUBJECT, look for
5534 articles with that subject.  If BACKWARD, search backward instead."
5535   (cond (subject (gnus-summary-find-subject subject unread backward))
5536         (backward (gnus-summary-find-prev unread))
5537         (t (gnus-summary-find-next unread))))
5538
5539 (defun gnus-recenter (&optional n)
5540   "Center point in window and redisplay frame.
5541 Also do horizontal recentering."
5542   (interactive "P")
5543   (when (and gnus-auto-center-summary
5544              (not (eq gnus-auto-center-summary 'vertical)))
5545     (gnus-horizontal-recenter))
5546   (recenter n))
5547
5548 (defun gnus-summary-recenter ()
5549   "Center point in the summary window.
5550 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5551 displayed, no centering will be performed."
5552   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5553   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5554   (interactive)
5555   (let* ((top (cond ((< (window-height) 4) 0)
5556                     ((< (window-height) 7) 1)
5557                     (t (if (numberp gnus-auto-center-summary)
5558                            gnus-auto-center-summary
5559                          2))))
5560          (height (1- (window-height)))
5561          (bottom (save-excursion (goto-char (point-max))
5562                                  (forward-line (- height))
5563                                  (point)))
5564          (window (get-buffer-window (current-buffer))))
5565     ;; The user has to want it.
5566     (when gnus-auto-center-summary
5567       (when (get-buffer-window gnus-article-buffer)
5568         ;; Only do recentering when the article buffer is displayed,
5569         ;; Set the window start to either `bottom', which is the biggest
5570         ;; possible valid number, or the second line from the top,
5571         ;; whichever is the least.
5572         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5573           (if (> bottom top-pos)
5574               ;; Keep the second line from the top visible
5575               (set-window-start window top-pos t)
5576             ;; Try to keep the bottom line visible; if it's partially
5577             ;; obscured, either scroll one more line to make it fully
5578             ;; visible, or revert to using TOP-POS.
5579             (save-excursion
5580               (goto-char (point-max))
5581               (forward-line -1)
5582               (let ((last-line-start (point)))
5583                 (goto-char bottom)
5584                 (set-window-start window (point) t)
5585                 (when (not (pos-visible-in-window-p last-line-start window))
5586                   (forward-line 1)
5587                   (set-window-start window (min (point) top-pos) t)))))))
5588       ;; Do horizontal recentering while we're at it.
5589       (when (and (get-buffer-window (current-buffer) t)
5590                  (not (eq gnus-auto-center-summary 'vertical)))
5591         (let ((selected (selected-window)))
5592           (select-window (get-buffer-window (current-buffer) t))
5593           (gnus-summary-position-point)
5594           (gnus-horizontal-recenter)
5595           (select-window selected))))))
5596
5597 (defun gnus-summary-jump-to-group (newsgroup)
5598   "Move point to NEWSGROUP in group mode buffer."
5599   ;; Keep update point of group mode buffer if visible.
5600   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5601       (save-window-excursion
5602         ;; Take care of tree window mode.
5603         (when (get-buffer-window gnus-group-buffer)
5604           (pop-to-buffer gnus-group-buffer))
5605         (gnus-group-jump-to-group newsgroup))
5606     (save-excursion
5607       ;; Take care of tree window mode.
5608       (if (get-buffer-window gnus-group-buffer)
5609           (pop-to-buffer gnus-group-buffer)
5610         (set-buffer gnus-group-buffer))
5611       (gnus-group-jump-to-group newsgroup))))
5612
5613 ;; This function returns a list of article numbers based on the
5614 ;; difference between the ranges of read articles in this group and
5615 ;; the range of active articles.
5616 (defun gnus-list-of-unread-articles (group)
5617   (let* ((read (gnus-info-read (gnus-get-info group)))
5618          (active (or (gnus-active group) (gnus-activate-group group)))
5619          (last (cdr active))
5620          first nlast unread)
5621     ;; If none are read, then all are unread.
5622     (if (not read)
5623         (setq first (car active))
5624       ;; If the range of read articles is a single range, then the
5625       ;; first unread article is the article after the last read
5626       ;; article.  Sounds logical, doesn't it?
5627       (if (and (not (listp (cdr read)))
5628                (or (< (car read) (car active))
5629                    (progn (setq read (list read))
5630                           nil)))
5631           (setq first (max (car active) (1+ (cdr read))))
5632         ;; `read' is a list of ranges.
5633         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5634                                   (caar read)))
5635                   1)
5636           (setq first (car active)))
5637         (while read
5638           (when first
5639             (while (< first nlast)
5640               (push first unread)
5641               (setq first (1+ first))))
5642           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5643           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5644           (setq read (cdr read)))))
5645     ;; And add the last unread articles.
5646     (while (<= first last)
5647       (push first unread)
5648       (setq first (1+ first)))
5649     ;; Return the list of unread articles.
5650     (delq 0 (nreverse unread))))
5651
5652 (defun gnus-list-of-read-articles (group)
5653   "Return a list of unread, unticked and non-dormant articles."
5654   (let* ((info (gnus-get-info group))
5655          (marked (gnus-info-marks info))
5656          (active (gnus-active group)))
5657     (and info active
5658          (gnus-set-difference
5659           (gnus-sorted-complement
5660            (gnus-uncompress-range active)
5661            (gnus-list-of-unread-articles group))
5662           (append
5663            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5664            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5665
5666 ;; Various summary commands
5667
5668 (defun gnus-summary-select-article-buffer ()
5669   "Reconfigure windows to show article buffer."
5670   (interactive)
5671   (if (not (gnus-buffer-live-p gnus-article-buffer))
5672       (error "There is no article buffer for this summary buffer")
5673     (gnus-configure-windows 'article)
5674     (select-window (get-buffer-window gnus-article-buffer))))
5675
5676 (defun gnus-summary-universal-argument (arg)
5677   "Perform any operation on all articles that are process/prefixed."
5678   (interactive "P")
5679   (let ((articles (gnus-summary-work-articles arg))
5680         func article)
5681     (if (eq
5682          (setq
5683           func
5684           (key-binding
5685            (read-key-sequence
5686             (substitute-command-keys
5687              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5688          'undefined)
5689         (gnus-error 1 "Undefined key")
5690       (save-excursion
5691         (while articles
5692           (gnus-summary-goto-subject (setq article (pop articles)))
5693           (let (gnus-newsgroup-processable)
5694             (command-execute func))
5695           (gnus-summary-remove-process-mark article)))))
5696   (gnus-summary-position-point))
5697
5698 (defun gnus-summary-toggle-truncation (&optional arg)
5699   "Toggle truncation of summary lines.
5700 With arg, turn line truncation on iff arg is positive."
5701   (interactive "P")
5702   (setq truncate-lines
5703         (if (null arg) (not truncate-lines)
5704           (> (prefix-numeric-value arg) 0)))
5705   (redraw-display))
5706
5707 (defun gnus-summary-reselect-current-group (&optional all rescan)
5708   "Exit and then reselect the current newsgroup.
5709 The prefix argument ALL means to select all articles."
5710   (interactive "P")
5711   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5712     (error "Ephemeral groups can't be reselected"))
5713   (let ((current-subject (gnus-summary-article-number))
5714         (group gnus-newsgroup-name))
5715     (setq gnus-newsgroup-begin nil)
5716     (gnus-summary-exit)
5717     ;; We have to adjust the point of group mode buffer because
5718     ;; point was moved to the next unread newsgroup by exiting.
5719     (gnus-summary-jump-to-group group)
5720     (when rescan
5721       (save-excursion
5722         (save-window-excursion
5723           ;; Don't show group contents.
5724           (set-window-start (selected-window) (point-max))
5725           (gnus-group-get-new-news-this-group 1))))
5726     (gnus-group-read-group all t)
5727     (gnus-summary-goto-subject current-subject nil t)))
5728
5729 (defun gnus-summary-rescan-group (&optional all)
5730   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5731   (interactive "P")
5732   (gnus-summary-reselect-current-group all t))
5733
5734 (defun gnus-summary-update-info (&optional non-destructive)
5735   (save-excursion
5736     (let ((group gnus-newsgroup-name))
5737       (when group
5738         (when gnus-newsgroup-kill-headers
5739           (setq gnus-newsgroup-killed
5740                 (gnus-compress-sequence
5741                  (nconc
5742                   (gnus-set-sorted-intersection
5743                    (gnus-uncompress-range gnus-newsgroup-killed)
5744                    (setq gnus-newsgroup-unselected
5745                          (sort gnus-newsgroup-unselected '<)))
5746                   (setq gnus-newsgroup-unreads
5747                         (sort gnus-newsgroup-unreads '<)))
5748                  t)))
5749         (unless (listp (cdr gnus-newsgroup-killed))
5750           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5751         (let ((headers gnus-newsgroup-headers))
5752           ;; Set the new ranges of read articles.
5753           (save-excursion
5754             (set-buffer gnus-group-buffer)
5755             (gnus-undo-force-boundary))
5756           (gnus-update-read-articles
5757            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5758           ;; Set the current article marks.
5759           (let ((gnus-newsgroup-scored
5760                  (if (and (not gnus-save-score)
5761                           (not non-destructive))
5762                      nil
5763                    gnus-newsgroup-scored)))
5764             (save-excursion
5765               (gnus-update-marks)))
5766           ;; Do the cross-ref thing.
5767           (when gnus-use-cross-reference
5768             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5769           ;; Do not switch windows but change the buffer to work.
5770           (set-buffer gnus-group-buffer)
5771           (unless (gnus-ephemeral-group-p group)
5772             (gnus-group-update-group group)))))))
5773
5774 (defun gnus-summary-save-newsrc (&optional force)
5775   "Save the current number of read/marked articles in the dribble buffer.
5776 The dribble buffer will then be saved.
5777 If FORCE (the prefix), also save the .newsrc file(s)."
5778   (interactive "P")
5779   (gnus-summary-update-info t)
5780   (if force
5781       (gnus-save-newsrc-file)
5782     (gnus-dribble-save)))
5783
5784 (defun gnus-summary-exit (&optional temporary)
5785   "Exit reading current newsgroup, and then return to group selection mode.
5786 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5787   (interactive)
5788   (gnus-set-global-variables)
5789   (gnus-kill-save-kill-buffer)
5790   (gnus-async-halt-prefetch)
5791   (let* ((group gnus-newsgroup-name)
5792          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5793          (mode major-mode)
5794          (group-point nil)
5795          (buf (current-buffer)))
5796     (unless quit-config
5797       ;; Do adaptive scoring, and possibly save score files.
5798       (when gnus-newsgroup-adaptive
5799         (gnus-score-adaptive))
5800       (when gnus-use-scoring
5801         (gnus-score-save)))
5802     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5803     ;; If we have several article buffers, we kill them at exit.
5804     (unless gnus-single-article-buffer
5805       (gnus-kill-buffer gnus-original-article-buffer)
5806       (setq gnus-article-current nil))
5807     (when gnus-use-cache
5808       (gnus-cache-possibly-remove-articles)
5809       (gnus-cache-save-buffers))
5810     (gnus-async-prefetch-remove-group group)
5811     (when gnus-suppress-duplicates
5812       (gnus-dup-enter-articles))
5813     (when gnus-use-trees
5814       (gnus-tree-close group))
5815     (when gnus-use-cache
5816       (gnus-cache-write-active))
5817     ;; Remove entries for this group.
5818     (nnmail-purge-split-history (gnus-group-real-name group))
5819     ;; Make all changes in this group permanent.
5820     (unless quit-config
5821       (gnus-run-hooks 'gnus-exit-group-hook)
5822       (gnus-summary-update-info))
5823     (gnus-close-group group)
5824     ;; Make sure where we were, and go to next newsgroup.
5825     (set-buffer gnus-group-buffer)
5826     (unless quit-config
5827       (gnus-group-jump-to-group group))
5828     (gnus-run-hooks 'gnus-summary-exit-hook)
5829     (unless (or quit-config
5830                 ;; If this group has disappeared from the summary
5831                 ;; buffer, don't skip forwards.
5832                 (not (string= group (gnus-group-group-name))))
5833       (gnus-group-next-unread-group 1))
5834     (setq group-point (point))
5835     (if temporary
5836         nil                             ;Nothing to do.
5837       ;; If we have several article buffers, we kill them at exit.
5838       (unless gnus-single-article-buffer
5839         (gnus-kill-buffer gnus-article-buffer)
5840         (gnus-kill-buffer gnus-original-article-buffer)
5841         (setq gnus-article-current nil))
5842       (set-buffer buf)
5843       (if (not gnus-kill-summary-on-exit)
5844           (progn
5845             (gnus-deaden-summary)
5846             (setq mode nil))
5847         ;; We set all buffer-local variables to nil.  It is unclear why
5848         ;; this is needed, but if we don't, buffer-local variables are
5849         ;; not garbage-collected, it seems.  This would the lead to en
5850         ;; ever-growing Emacs.
5851         (gnus-summary-clear-local-variables)
5852         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5853           (gnus-summary-clear-local-variables))
5854         (when (get-buffer gnus-article-buffer)
5855           (bury-buffer gnus-article-buffer))
5856         ;; We clear the global counterparts of the buffer-local
5857         ;; variables as well, just to be on the safe side.
5858         (set-buffer gnus-group-buffer)
5859         (gnus-summary-clear-local-variables)
5860         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5861           (gnus-summary-clear-local-variables)))
5862       (setq gnus-current-select-method gnus-select-method)
5863       (pop-to-buffer gnus-group-buffer)
5864       (if (not quit-config)
5865           (progn
5866             (goto-char group-point)
5867             (gnus-configure-windows 'group 'force)
5868             (unless (pos-visible-in-window-p)
5869               (forward-line (/ (static-if (featurep 'xemacs)
5870                                    (window-displayed-height)
5871                                  (1- (window-height)))
5872                                -2))
5873               (set-window-start (selected-window) (point))
5874               (goto-char group-point)))
5875         (gnus-handle-ephemeral-exit quit-config))
5876       ;; Return to group mode buffer.
5877       (when (eq mode 'gnus-summary-mode)
5878         (gnus-kill-buffer buf))
5879       ;; Clear the current group name.
5880       (unless quit-config
5881         (setq gnus-newsgroup-name nil)))))
5882
5883 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5884 (defun gnus-summary-exit-no-update (&optional no-questions)
5885   "Quit reading current newsgroup without updating read article info."
5886   (interactive)
5887   (let* ((group gnus-newsgroup-name)
5888          (quit-config (gnus-group-quit-config group)))
5889     (when (or no-questions
5890               gnus-expert-user
5891               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5892       (gnus-async-halt-prefetch)
5893       (mapcar 'funcall
5894               (delq 'gnus-summary-expire-articles
5895                     (copy-sequence gnus-summary-prepare-exit-hook)))
5896       ;; If we have several article buffers, we kill them at exit.
5897       (unless gnus-single-article-buffer
5898         (gnus-kill-buffer gnus-article-buffer)
5899         (gnus-kill-buffer gnus-original-article-buffer)
5900         (setq gnus-article-current nil))
5901       (if (not gnus-kill-summary-on-exit)
5902           (gnus-deaden-summary)
5903         (gnus-close-group group)
5904         (gnus-summary-clear-local-variables)
5905         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5906           (gnus-summary-clear-local-variables))
5907         (set-buffer gnus-group-buffer)
5908         (gnus-summary-clear-local-variables)
5909         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5910           (gnus-summary-clear-local-variables))
5911         (when (get-buffer gnus-summary-buffer)
5912           (kill-buffer gnus-summary-buffer)))
5913       (unless gnus-single-article-buffer
5914         (setq gnus-article-current nil))
5915       (when gnus-use-trees
5916         (gnus-tree-close group))
5917       (gnus-async-prefetch-remove-group group)
5918       (when (get-buffer gnus-article-buffer)
5919         (bury-buffer gnus-article-buffer))
5920       ;; Return to the group buffer.
5921       (gnus-configure-windows 'group 'force)
5922       ;; Clear the current group name.
5923       (setq gnus-newsgroup-name nil)
5924       (when (equal (gnus-group-group-name) group)
5925         (gnus-group-next-unread-group 1))
5926       (when quit-config
5927         (gnus-handle-ephemeral-exit quit-config)))))
5928
5929 (defun gnus-handle-ephemeral-exit (quit-config)
5930   "Handle movement when leaving an ephemeral group.
5931 The state which existed when entering the ephemeral is reset."
5932   (if (not (buffer-name (car quit-config)))
5933       (gnus-configure-windows 'group 'force)
5934     (set-buffer (car quit-config))
5935     (cond ((eq major-mode 'gnus-summary-mode)
5936            (gnus-set-global-variables))
5937           ((eq major-mode 'gnus-article-mode)
5938            (save-excursion
5939              ;; The `gnus-summary-buffer' variable may point
5940              ;; to the old summary buffer when using a single
5941              ;; article buffer.
5942              (unless (gnus-buffer-live-p gnus-summary-buffer)
5943                (set-buffer gnus-group-buffer))
5944              (set-buffer gnus-summary-buffer)
5945              (gnus-set-global-variables))))
5946     (if (or (eq (cdr quit-config) 'article)
5947             (eq (cdr quit-config) 'pick))
5948         (progn
5949           ;; The current article may be from the ephemeral group
5950           ;; thus it is best that we reload this article
5951           (gnus-summary-show-article)
5952           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5953               (gnus-configure-windows 'pick 'force)
5954             (gnus-configure-windows (cdr quit-config) 'force)))
5955       (gnus-configure-windows (cdr quit-config) 'force))
5956     (when (eq major-mode 'gnus-summary-mode)
5957       (gnus-summary-next-subject 1 nil t)
5958       (gnus-summary-recenter)
5959       (gnus-summary-position-point))))
5960
5961 (defun gnus-summary-preview-mime-message ()
5962   "MIME decode and play this message."
5963   (interactive)
5964   (let ((gnus-break-pages nil)
5965         (gnus-show-mime t))
5966     (gnus-summary-select-article gnus-show-all-headers t))
5967   (select-window (get-buffer-window gnus-article-buffer)))
5968
5969 ;;; Dead summaries.
5970
5971 (defvar gnus-dead-summary-mode-map nil)
5972
5973 (unless gnus-dead-summary-mode-map
5974   (setq gnus-dead-summary-mode-map (make-keymap))
5975   (suppress-keymap gnus-dead-summary-mode-map)
5976   (substitute-key-definition
5977    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5978   (let ((keys '("\C-d" "\r" "\177" [delete])))
5979     (while keys
5980       (define-key gnus-dead-summary-mode-map
5981         (pop keys) 'gnus-summary-wake-up-the-dead))))
5982
5983 (defvar gnus-dead-summary-mode nil
5984   "Minor mode for Gnus summary buffers.")
5985
5986 (defun gnus-dead-summary-mode (&optional arg)
5987   "Minor mode for Gnus summary buffers."
5988   (interactive "P")
5989   (when (eq major-mode 'gnus-summary-mode)
5990     (make-local-variable 'gnus-dead-summary-mode)
5991     (setq gnus-dead-summary-mode
5992           (if (null arg) (not gnus-dead-summary-mode)
5993             (> (prefix-numeric-value arg) 0)))
5994     (when gnus-dead-summary-mode
5995       (gnus-add-minor-mode
5996        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5997
5998 (defun gnus-deaden-summary ()
5999   "Make the current summary buffer into a dead summary buffer."
6000   ;; Kill any previous dead summary buffer.
6001   (when (and gnus-dead-summary
6002              (buffer-name gnus-dead-summary))
6003     (save-excursion
6004       (set-buffer gnus-dead-summary)
6005       (when gnus-dead-summary-mode
6006         (kill-buffer (current-buffer)))))
6007   ;; Make this the current dead summary.
6008   (setq gnus-dead-summary (current-buffer))
6009   (gnus-dead-summary-mode 1)
6010   (let ((name (buffer-name)))
6011     (when (string-match "Summary" name)
6012       (rename-buffer
6013        (concat (substring name 0 (match-beginning 0)) "Dead "
6014                (substring name (match-beginning 0)))
6015        t)
6016       (bury-buffer))))
6017
6018 (defun gnus-kill-or-deaden-summary (buffer)
6019   "Kill or deaden the summary BUFFER."
6020   (save-excursion
6021     (when (and (buffer-name buffer)
6022                (not gnus-single-article-buffer))
6023       (save-excursion
6024         (set-buffer buffer)
6025         (gnus-kill-buffer gnus-article-buffer)
6026         (gnus-kill-buffer gnus-original-article-buffer)))
6027     (cond (gnus-kill-summary-on-exit
6028            (when (and gnus-use-trees
6029                       (gnus-buffer-exists-p buffer))
6030              (save-excursion
6031                (set-buffer buffer)
6032                (gnus-tree-close gnus-newsgroup-name)))
6033            (gnus-kill-buffer buffer))
6034           ((gnus-buffer-exists-p buffer)
6035            (save-excursion
6036              (set-buffer buffer)
6037              (gnus-deaden-summary))))))
6038
6039 (defun gnus-summary-wake-up-the-dead (&rest args)
6040   "Wake up the dead summary buffer."
6041   (interactive)
6042   (gnus-dead-summary-mode -1)
6043   (let ((name (buffer-name)))
6044     (when (string-match "Dead " name)
6045       (rename-buffer
6046        (concat (substring name 0 (match-beginning 0))
6047                (substring name (match-end 0)))
6048        t)))
6049   (gnus-message 3 "This dead summary is now alive again"))
6050
6051 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6052 (defun gnus-summary-fetch-faq (&optional faq-dir)
6053   "Fetch the FAQ for the current group.
6054 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6055 in."
6056   (interactive
6057    (list
6058     (when current-prefix-arg
6059       (completing-read
6060        "Faq dir: " (and (listp gnus-group-faq-directory)
6061                         (mapcar (lambda (file) (list file))
6062                                 gnus-group-faq-directory))))))
6063   (let (gnus-faq-buffer)
6064     (when (setq gnus-faq-buffer
6065                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6066       (gnus-configure-windows 'summary-faq))))
6067
6068 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6069 (defun gnus-summary-describe-group (&optional force)
6070   "Describe the current newsgroup."
6071   (interactive "P")
6072   (gnus-group-describe-group force gnus-newsgroup-name))
6073
6074 (defun gnus-summary-describe-briefly ()
6075   "Describe summary mode commands briefly."
6076   (interactive)
6077   (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")))
6078
6079 ;; Walking around group mode buffer from summary mode.
6080
6081 (defun gnus-summary-next-group (&optional no-article target-group backward)
6082   "Exit current newsgroup and then select next unread newsgroup.
6083 If prefix argument NO-ARTICLE is non-nil, no article is selected
6084 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6085 previous group instead."
6086   (interactive "P")
6087   ;; Stop pre-fetching.
6088   (gnus-async-halt-prefetch)
6089   (let ((current-group gnus-newsgroup-name)
6090         (current-buffer (current-buffer))
6091         entered)
6092     ;; First we semi-exit this group to update Xrefs and all variables.
6093     ;; We can't do a real exit, because the window conf must remain
6094     ;; the same in case the user is prompted for info, and we don't
6095     ;; want the window conf to change before that...
6096     (gnus-summary-exit t)
6097     (while (not entered)
6098       ;; Then we find what group we are supposed to enter.
6099       (set-buffer gnus-group-buffer)
6100       (gnus-group-jump-to-group current-group)
6101       (setq target-group
6102             (or target-group
6103                 (if (eq gnus-keep-same-level 'best)
6104                     (gnus-summary-best-group gnus-newsgroup-name)
6105                   (gnus-summary-search-group backward gnus-keep-same-level))))
6106       (if (not target-group)
6107           ;; There are no further groups, so we return to the group
6108           ;; buffer.
6109           (progn
6110             (gnus-message 5 "Returning to the group buffer")
6111             (setq entered t)
6112             (when (gnus-buffer-live-p current-buffer)
6113               (set-buffer current-buffer)
6114               (gnus-summary-exit))
6115             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6116         ;; We try to enter the target group.
6117         (gnus-group-jump-to-group target-group)
6118         (let ((unreads (gnus-group-group-unread)))
6119           (if (and (or (eq t unreads)
6120                        (and unreads (not (zerop unreads))))
6121                    (gnus-summary-read-group
6122                     target-group nil no-article
6123                     (and (buffer-name current-buffer) current-buffer)
6124                     nil backward))
6125               (setq entered t)
6126             (setq current-group target-group
6127                   target-group nil)))))))
6128
6129 (defun gnus-summary-prev-group (&optional no-article)
6130   "Exit current newsgroup and then select previous unread newsgroup.
6131 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6132   (interactive "P")
6133   (gnus-summary-next-group no-article nil t))
6134
6135 ;; Walking around summary lines.
6136
6137 (defun gnus-summary-first-subject (&optional unread undownloaded)
6138   "Go to the first unread subject.
6139 If UNREAD is non-nil, go to the first unread article.
6140 Returns the article selected or nil if there are no unread articles."
6141   (interactive "P")
6142   (prog1
6143       (cond
6144        ;; Empty summary.
6145        ((null gnus-newsgroup-data)
6146         (gnus-message 3 "No articles in the group")
6147         nil)
6148        ;; Pick the first article.
6149        ((not unread)
6150         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6151         (gnus-data-number (car gnus-newsgroup-data)))
6152        ;; No unread articles.
6153        ((null gnus-newsgroup-unreads)
6154         (gnus-message 3 "No more unread articles")
6155         nil)
6156        ;; Find the first unread article.
6157        (t
6158         (let ((data gnus-newsgroup-data))
6159           (while (and data
6160                       (and (not (and undownloaded
6161                                      (eq gnus-undownloaded-mark
6162                                          (gnus-data-mark (car data)))))
6163                            (not (gnus-data-unread-p (car data)))))
6164             (setq data (cdr data)))
6165           (when data
6166             (goto-char (gnus-data-pos (car data)))
6167             (gnus-data-number (car data))))))
6168     (gnus-summary-position-point)))
6169
6170 (defun gnus-summary-next-subject (n &optional unread dont-display)
6171   "Go to next N'th summary line.
6172 If N is negative, go to the previous N'th subject line.
6173 If UNREAD is non-nil, only unread articles are selected.
6174 The difference between N and the actual number of steps taken is
6175 returned."
6176   (interactive "p")
6177   (let ((backward (< n 0))
6178         (n (abs n)))
6179     (while (and (> n 0)
6180                 (if backward
6181                     (gnus-summary-find-prev unread)
6182                   (gnus-summary-find-next unread)))
6183       (unless (zerop (setq n (1- n)))
6184         (gnus-summary-show-thread)))
6185     (when (/= 0 n)
6186       (gnus-message 7 "No more%s articles"
6187                     (if unread " unread" "")))
6188     (unless dont-display
6189       (gnus-summary-recenter)
6190       (gnus-summary-position-point))
6191     n))
6192
6193 (defun gnus-summary-next-unread-subject (n)
6194   "Go to next N'th unread summary line."
6195   (interactive "p")
6196   (gnus-summary-next-subject n t))
6197
6198 (defun gnus-summary-prev-subject (n &optional unread)
6199   "Go to previous N'th summary line.
6200 If optional argument UNREAD is non-nil, only unread article is selected."
6201   (interactive "p")
6202   (gnus-summary-next-subject (- n) unread))
6203
6204 (defun gnus-summary-prev-unread-subject (n)
6205   "Go to previous N'th unread summary line."
6206   (interactive "p")
6207   (gnus-summary-next-subject (- n) t))
6208
6209 (defun gnus-summary-goto-subject (article &optional force silent)
6210   "Go the subject line of ARTICLE.
6211 If FORCE, also allow jumping to articles not currently shown."
6212   (interactive "nArticle number: ")
6213   (let ((b (point))
6214         (data (gnus-data-find article)))
6215     ;; We read in the article if we have to.
6216     (and (not data)
6217          force
6218          (gnus-summary-insert-subject
6219           article
6220           (if (or (numberp force) (vectorp force)) force)
6221           t)
6222          (setq data (gnus-data-find article)))
6223     (goto-char b)
6224     (if (not data)
6225         (progn
6226           (unless silent
6227             (gnus-message 3 "Can't find article %d" article))
6228           nil)
6229       (let ((pt (gnus-data-pos data)))
6230         (goto-char pt)
6231         (gnus-summary-set-article-display-arrow pt))
6232       (gnus-summary-position-point)
6233       article)))
6234
6235 ;; Walking around summary lines with displaying articles.
6236
6237 (defun gnus-summary-expand-window (&optional arg)
6238   "Make the summary buffer take up the entire Emacs frame.
6239 Given a prefix, will force an `article' buffer configuration."
6240   (interactive "P")
6241   (if arg
6242       (gnus-configure-windows 'article 'force)
6243     (gnus-configure-windows 'summary 'force)))
6244
6245 (defun gnus-summary-display-article (article &optional all-header)
6246   "Display ARTICLE in article buffer."
6247   (when (gnus-buffer-live-p gnus-article-buffer)
6248     (with-current-buffer gnus-article-buffer
6249       (set-buffer-multibyte t)))
6250   (gnus-set-global-variables)
6251   (when (gnus-buffer-live-p gnus-article-buffer)
6252     (with-current-buffer gnus-article-buffer
6253       (setq gnus-article-charset gnus-newsgroup-charset)
6254       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6255   (if (null article)
6256       nil
6257     (prog1
6258         (if gnus-summary-display-article-function
6259             (funcall gnus-summary-display-article-function article all-header)
6260           (gnus-article-prepare article all-header))
6261       (with-current-buffer gnus-article-buffer
6262         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6263              nil))
6264       (gnus-run-hooks 'gnus-select-article-hook)
6265       (when (and gnus-current-article
6266                  (not (zerop gnus-current-article)))
6267         (gnus-summary-goto-subject gnus-current-article))
6268       (gnus-summary-recenter)
6269       (when (and gnus-use-trees gnus-show-threads)
6270         (gnus-possibly-generate-tree article)
6271         (gnus-highlight-selected-tree article))
6272       ;; Successfully display article.
6273       (gnus-article-set-window-start
6274        (cdr (assq article gnus-newsgroup-bookmarks))))))
6275
6276 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6277   "Select the current article.
6278 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6279 non-nil, the article will be re-fetched even if it already present in
6280 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6281 be displayed."
6282   ;; Make sure we are in the summary buffer to work around bbdb bug.
6283   (unless (eq major-mode 'gnus-summary-mode)
6284     (set-buffer gnus-summary-buffer))
6285   (let ((article (or article (gnus-summary-article-number)))
6286         (all-headers (not (not all-headers))) ;Must be T or NIL.
6287         gnus-summary-display-article-function)
6288     (and (not pseudo)
6289          (gnus-summary-article-pseudo-p article)
6290          (error "This is a pseudo-article"))
6291     (save-excursion
6292       (set-buffer gnus-summary-buffer)
6293       (if (or (and gnus-single-article-buffer
6294                    (or (null gnus-current-article)
6295                        (null gnus-article-current)
6296                        (null (get-buffer gnus-article-buffer))
6297                        (not (eq article (cdr gnus-article-current)))
6298                        (not (equal (car gnus-article-current)
6299                                    gnus-newsgroup-name))))
6300               (and (not gnus-single-article-buffer)
6301                    (or (null gnus-current-article)
6302                        (not (eq gnus-current-article article))))
6303               force)
6304           ;; The requested article is different from the current article.
6305           (progn
6306             (gnus-summary-display-article article all-headers)
6307             (when (or all-headers gnus-show-all-headers)
6308               (gnus-article-show-all-headers))
6309             (gnus-article-set-window-start
6310              (cdr (assq article gnus-newsgroup-bookmarks)))
6311             article)
6312         (when (or all-headers gnus-show-all-headers)
6313           (gnus-article-show-all-headers))
6314         'old))))
6315
6316 (defun gnus-summary-force-verify-and-decrypt ()
6317   (interactive)
6318   (let ((mm-verify-option 'known)
6319         (mm-decrypt-option 'known))
6320     (gnus-summary-select-article nil 'force)))
6321
6322 (defun gnus-summary-set-current-mark (&optional current-mark)
6323   "Obsolete function."
6324   nil)
6325
6326 (defun gnus-summary-next-article (&optional unread subject backward push)
6327   "Select the next article.
6328 If UNREAD, only unread articles are selected.
6329 If SUBJECT, only articles with SUBJECT are selected.
6330 If BACKWARD, the previous article is selected instead of the next."
6331   (interactive "P")
6332   (cond
6333    ;; Is there such an article?
6334    ((and (gnus-summary-search-forward unread subject backward)
6335          (or (gnus-summary-display-article (gnus-summary-article-number))
6336              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6337     (gnus-summary-position-point))
6338    ;; If not, we try the first unread, if that is wanted.
6339    ((and subject
6340          gnus-auto-select-same
6341          (gnus-summary-first-unread-article))
6342     (gnus-summary-position-point)
6343     (gnus-message 6 "Wrapped"))
6344    ;; Try to get next/previous article not displayed in this group.
6345    ((and gnus-auto-extend-newsgroup
6346          (not unread) (not subject))
6347     (gnus-summary-goto-article
6348      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6349      nil (count-lines (point-min) (point))))
6350    ;; Go to next/previous group.
6351    (t
6352     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6353       (gnus-summary-jump-to-group gnus-newsgroup-name))
6354     (let ((cmd last-command-char)
6355           (point
6356            (save-excursion
6357              (set-buffer gnus-group-buffer)
6358              (point)))
6359           (group
6360            (if (eq gnus-keep-same-level 'best)
6361                (gnus-summary-best-group gnus-newsgroup-name)
6362              (gnus-summary-search-group backward gnus-keep-same-level))))
6363       ;; For some reason, the group window gets selected.  We change
6364       ;; it back.
6365       (select-window (get-buffer-window (current-buffer)))
6366       ;; Select next unread newsgroup automagically.
6367       (cond
6368        ((or (not gnus-auto-select-next)
6369             (not cmd))
6370         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6371        ((or (eq gnus-auto-select-next 'quietly)
6372             (and (eq gnus-auto-select-next 'slightly-quietly)
6373                  push)
6374             (and (eq gnus-auto-select-next 'almost-quietly)
6375                  (gnus-summary-last-article-p)))
6376         ;; Select quietly.
6377         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6378             (gnus-summary-exit)
6379           (gnus-message 7 "No more%s articles (%s)..."
6380                         (if unread " unread" "")
6381                         (if group (concat "selecting " group)
6382                           "exiting"))
6383           (gnus-summary-next-group nil group backward)))
6384        (t
6385         (when (gnus-key-press-event-p last-input-event)
6386           (gnus-summary-walk-group-buffer
6387            gnus-newsgroup-name cmd unread backward point))))))))
6388
6389 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6390   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6391                       (?\C-p (gnus-group-prev-unread-group 1))))
6392         (cursor-in-echo-area t)
6393         keve key group ended)
6394     (save-excursion
6395       (set-buffer gnus-group-buffer)
6396       (goto-char start)
6397       (setq group
6398             (if (eq gnus-keep-same-level 'best)
6399                 (gnus-summary-best-group gnus-newsgroup-name)
6400               (gnus-summary-search-group backward gnus-keep-same-level))))
6401     (while (not ended)
6402       (gnus-message
6403        5 "No more%s articles%s" (if unread " unread" "")
6404        (if (and group
6405                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6406            (format " (Type %s for %s [%s])"
6407                    (single-key-description cmd) group
6408                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6409          (format " (Type %s to exit %s)"
6410                  (single-key-description cmd)
6411                  gnus-newsgroup-name)))
6412       ;; Confirm auto selection.
6413       (setq key (car (setq keve (gnus-read-event-char))))
6414       (setq ended t)
6415       (cond
6416        ((assq key keystrokes)
6417         (let ((obuf (current-buffer)))
6418           (switch-to-buffer gnus-group-buffer)
6419           (when group
6420             (gnus-group-jump-to-group group))
6421           (eval (cadr (assq key keystrokes)))
6422           (setq group (gnus-group-group-name))
6423           (switch-to-buffer obuf))
6424         (setq ended nil))
6425        ((equal key cmd)
6426         (if (or (not group)
6427                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6428             (gnus-summary-exit)
6429           (gnus-summary-next-group nil group backward)))
6430        (t
6431         (push (cdr keve) unread-command-events))))))
6432
6433 (defun gnus-summary-next-unread-article ()
6434   "Select unread article after current one."
6435   (interactive)
6436   (gnus-summary-next-article
6437    (or (not (eq gnus-summary-goto-unread 'never))
6438        (gnus-summary-last-article-p (gnus-summary-article-number)))
6439    (and gnus-auto-select-same
6440         (gnus-summary-article-subject))))
6441
6442 (defun gnus-summary-prev-article (&optional unread subject)
6443   "Select the article after the current one.
6444 If UNREAD is non-nil, only unread articles are selected."
6445   (interactive "P")
6446   (gnus-summary-next-article unread subject t))
6447
6448 (defun gnus-summary-prev-unread-article ()
6449   "Select unread article before current one."
6450   (interactive)
6451   (gnus-summary-prev-article
6452    (or (not (eq gnus-summary-goto-unread 'never))
6453        (gnus-summary-first-article-p (gnus-summary-article-number)))
6454    (and gnus-auto-select-same
6455         (gnus-summary-article-subject))))
6456
6457 (defun gnus-summary-next-page (&optional lines circular)
6458   "Show next page of the selected article.
6459 If at the end of the current article, select the next article.
6460 LINES says how many lines should be scrolled up.
6461
6462 If CIRCULAR is non-nil, go to the start of the article instead of
6463 selecting the next article when reaching the end of the current
6464 article."
6465   (interactive "P")
6466   (setq gnus-summary-buffer (current-buffer))
6467   (gnus-set-global-variables)
6468   (let ((article (gnus-summary-article-number))
6469         (article-window (get-buffer-window gnus-article-buffer t))
6470         endp)
6471     ;; If the buffer is empty, we have no article.
6472     (unless article
6473       (error "No article to select"))
6474     (gnus-configure-windows 'article)
6475     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6476         (if (and (eq gnus-summary-goto-unread 'never)
6477                  (not (gnus-summary-last-article-p article)))
6478             (gnus-summary-next-article)
6479           (gnus-summary-next-unread-article))
6480       (if (or (null gnus-current-article)
6481               (null gnus-article-current)
6482               (/= article (cdr gnus-article-current))
6483               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6484           ;; Selected subject is different from current article's.
6485           (gnus-summary-display-article article)
6486         (when article-window
6487           (gnus-eval-in-buffer-window gnus-article-buffer
6488             (setq endp (gnus-article-next-page lines)))
6489           (when endp
6490             (cond (circular
6491                    (gnus-summary-beginning-of-article))
6492                   (lines
6493                    (gnus-message 3 "End of message"))
6494                   ((null lines)
6495                    (if (and (eq gnus-summary-goto-unread 'never)
6496                             (not (gnus-summary-last-article-p article)))
6497                        (gnus-summary-next-article)
6498                      (gnus-summary-next-unread-article))))))))
6499     (gnus-summary-recenter)
6500     (gnus-summary-position-point)))
6501
6502 (defun gnus-summary-prev-page (&optional lines move)
6503   "Show previous page of selected article.
6504 Argument LINES specifies lines to be scrolled down.
6505 If MOVE, move to the previous unread article if point is at
6506 the beginning of the buffer."
6507   (interactive "P")
6508   (let ((article (gnus-summary-article-number))
6509         (article-window (get-buffer-window gnus-article-buffer t))
6510         endp)
6511     (gnus-configure-windows 'article)
6512     (if (or (null gnus-current-article)
6513             (null gnus-article-current)
6514             (/= article (cdr gnus-article-current))
6515             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6516         ;; Selected subject is different from current article's.
6517         (gnus-summary-display-article article)
6518       (gnus-summary-recenter)
6519       (when article-window
6520         (gnus-eval-in-buffer-window gnus-article-buffer
6521           (setq endp (gnus-article-prev-page lines)))
6522         (when (and move endp)
6523           (cond (lines
6524                  (gnus-message 3 "Beginning of message"))
6525                 ((null lines)
6526                  (if (and (eq gnus-summary-goto-unread 'never)
6527                           (not (gnus-summary-first-article-p article)))
6528                      (gnus-summary-prev-article)
6529                    (gnus-summary-prev-unread-article))))))))
6530   (gnus-summary-position-point))
6531
6532 (defun gnus-summary-prev-page-or-article (&optional lines)
6533   "Show previous page of selected article.
6534 Argument LINES specifies lines to be scrolled down.
6535 If at the beginning of the article, go to the next article."
6536   (interactive "P")
6537   (gnus-summary-prev-page lines t))
6538
6539 (defun gnus-summary-scroll-up (lines)
6540   "Scroll up (or down) one line current article.
6541 Argument LINES specifies lines to be scrolled up (or down if negative)."
6542   (interactive "p")
6543   (gnus-configure-windows 'article)
6544   (gnus-summary-show-thread)
6545   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6546     (gnus-eval-in-buffer-window gnus-article-buffer
6547       (cond ((> lines 0)
6548              (when (gnus-article-next-page lines)
6549                (gnus-message 3 "End of message")))
6550             ((< lines 0)
6551              (gnus-article-prev-page (- lines))))))
6552   (gnus-summary-recenter)
6553   (gnus-summary-position-point))
6554
6555 (defun gnus-summary-scroll-down (lines)
6556   "Scroll down (or up) one line current article.
6557 Argument LINES specifies lines to be scrolled down (or up if negative)."
6558   (interactive "p")
6559   (gnus-summary-scroll-up (- lines)))
6560
6561 (defun gnus-summary-next-same-subject ()
6562   "Select next article which has the same subject as current one."
6563   (interactive)
6564   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6565
6566 (defun gnus-summary-prev-same-subject ()
6567   "Select previous article which has the same subject as current one."
6568   (interactive)
6569   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6570
6571 (defun gnus-summary-next-unread-same-subject ()
6572   "Select next unread article which has the same subject as current one."
6573   (interactive)
6574   (gnus-summary-next-article t (gnus-summary-article-subject)))
6575
6576 (defun gnus-summary-prev-unread-same-subject ()
6577   "Select previous unread article which has the same subject as current one."
6578   (interactive)
6579   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6580
6581 (defun gnus-summary-first-unread-article ()
6582   "Select the first unread article.
6583 Return nil if there are no unread articles."
6584   (interactive)
6585   (prog1
6586       (when (gnus-summary-first-subject t)
6587         (gnus-summary-show-thread)
6588         (gnus-summary-first-subject t)
6589         (gnus-summary-display-article (gnus-summary-article-number)))
6590     (gnus-summary-position-point)))
6591
6592 (defun gnus-summary-first-unread-subject ()
6593   "Place the point on the subject line of the first unread article.
6594 Return nil if there are no unread articles."
6595   (interactive)
6596   (prog1
6597       (when (gnus-summary-first-subject t)
6598         (gnus-summary-show-thread)
6599         (gnus-summary-first-subject t))
6600     (gnus-summary-position-point)))
6601
6602 (defun gnus-summary-first-article ()
6603   "Select the first article.
6604 Return nil if there are no articles."
6605   (interactive)
6606   (prog1
6607       (when (gnus-summary-first-subject)
6608         (gnus-summary-show-thread)
6609         (gnus-summary-first-subject)
6610         (gnus-summary-display-article (gnus-summary-article-number)))
6611     (gnus-summary-position-point)))
6612
6613 (defun gnus-summary-best-unread-article ()
6614   "Select the unread article with the highest score."
6615   (interactive)
6616   (let ((best -1000000)
6617         (data gnus-newsgroup-data)
6618         article score)
6619     (while data
6620       (and (gnus-data-unread-p (car data))
6621            (> (setq score
6622                     (gnus-summary-article-score (gnus-data-number (car data))))
6623               best)
6624            (setq best score
6625                  article (gnus-data-number (car data))))
6626       (setq data (cdr data)))
6627     (prog1
6628         (if article
6629             (gnus-summary-goto-article article)
6630           (error "No unread articles"))
6631       (gnus-summary-position-point))))
6632
6633 (defun gnus-summary-last-subject ()
6634   "Go to the last displayed subject line in the group."
6635   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6636     (when article
6637       (gnus-summary-goto-subject article))))
6638
6639 (defun gnus-summary-goto-article (article &optional all-headers force)
6640   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6641 If ALL-HEADERS is non-nil, no header lines are hidden.
6642 If FORCE, go to the article even if it isn't displayed.  If FORCE
6643 is a number, it is the line the article is to be displayed on."
6644   (interactive
6645    (list
6646     (completing-read
6647      "Article number or Message-ID: "
6648      (mapcar (lambda (number) (list (int-to-string number)))
6649              gnus-newsgroup-limit))
6650     current-prefix-arg
6651     t))
6652   (prog1
6653       (if (and (stringp article)
6654                (string-match "@" article))
6655           (gnus-summary-refer-article article)
6656         (when (stringp article)
6657           (setq article (string-to-number article)))
6658         (if (gnus-summary-goto-subject article force)
6659             (gnus-summary-display-article article all-headers)
6660           (gnus-message 4 "Couldn't go to article %s" article) nil))
6661     (gnus-summary-position-point)))
6662
6663 (defun gnus-summary-goto-last-article ()
6664   "Go to the previously read article."
6665   (interactive)
6666   (prog1
6667       (when gnus-last-article
6668         (gnus-summary-goto-article gnus-last-article nil t))
6669     (gnus-summary-position-point)))
6670
6671 (defun gnus-summary-pop-article (number)
6672   "Pop one article off the history and go to the previous.
6673 NUMBER articles will be popped off."
6674   (interactive "p")
6675   (let (to)
6676     (setq gnus-newsgroup-history
6677           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6678     (if to
6679         (gnus-summary-goto-article (car to) nil t)
6680       (error "Article history empty")))
6681   (gnus-summary-position-point))
6682
6683 ;; Summary commands and functions for limiting the summary buffer.
6684
6685 (defun gnus-summary-limit-to-articles (n)
6686   "Limit the summary buffer to the next N articles.
6687 If not given a prefix, use the process marked articles instead."
6688   (interactive "P")
6689   (prog1
6690       (let ((articles (gnus-summary-work-articles n)))
6691         (setq gnus-newsgroup-processable nil)
6692         (gnus-summary-limit articles))
6693     (gnus-summary-position-point)))
6694
6695 (defun gnus-summary-pop-limit (&optional total)
6696   "Restore the previous limit.
6697 If given a prefix, remove all limits."
6698   (interactive "P")
6699   (when total
6700     (setq gnus-newsgroup-limits
6701           (list (mapcar (lambda (h) (mail-header-number h))
6702                         gnus-newsgroup-headers))))
6703   (unless gnus-newsgroup-limits
6704     (error "No limit to pop"))
6705   (prog1
6706       (gnus-summary-limit nil 'pop)
6707     (gnus-summary-position-point)))
6708
6709 (defun gnus-summary-limit-to-subject (subject &optional header)
6710   "Limit the summary buffer to articles that have subjects that match a regexp."
6711   (interactive "sLimit to subject (regexp): ")
6712   (unless header
6713     (setq header "subject"))
6714   (when (not (equal "" subject))
6715     (prog1
6716         (let ((articles (gnus-summary-find-matching
6717                          (or header "subject") subject 'all)))
6718           (unless articles
6719             (error "Found no matches for \"%s\"" subject))
6720           (gnus-summary-limit articles))
6721       (gnus-summary-position-point))))
6722
6723 (defun gnus-summary-limit-to-author (from)
6724   "Limit the summary buffer to articles that have authors that match a regexp."
6725   (interactive "sLimit to author (regexp): ")
6726   (gnus-summary-limit-to-subject from "from"))
6727
6728 (defun gnus-summary-limit-to-age (age &optional younger-p)
6729   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6730 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6731 articles that are younger than AGE days."
6732   (interactive
6733    (let ((younger current-prefix-arg)
6734          (days-got nil)
6735          days)
6736      (while (not days-got)
6737        (setq days (if younger
6738                       (read-string "Limit to articles within (in days): ")
6739                     (read-string "Limit to articles older than (in days): ")))
6740        (when (> (length days) 0)
6741          (setq days (read days)))
6742        (if (numberp days)
6743            (setq days-got t)
6744          (message "Please enter a number.")
6745          (sleep-for 1)))
6746      (list days younger)))
6747   (prog1
6748       (let ((data gnus-newsgroup-data)
6749             (cutoff (days-to-time age))
6750             articles d date is-younger)
6751         (while (setq d (pop data))
6752           (when (and (vectorp (gnus-data-header d))
6753                      (setq date (mail-header-date (gnus-data-header d))))
6754             (setq is-younger (time-less-p
6755                               (time-since (condition-case ()
6756                                               (date-to-time date)
6757                                             (error '(0 0))))
6758                               cutoff))
6759             (when (if younger-p
6760                       is-younger
6761                     (not is-younger))
6762               (push (gnus-data-number d) articles))))
6763         (gnus-summary-limit (nreverse articles)))
6764     (gnus-summary-position-point)))
6765
6766 (defun gnus-summary-limit-to-extra (header regexp)
6767   "Limit the summary buffer to articles that match an 'extra' header."
6768   (interactive
6769    (let ((header
6770           (intern
6771            (gnus-completing-read
6772             (symbol-name (car gnus-extra-headers))
6773             "Limit extra header:"
6774             (mapcar (lambda (x)
6775                       (cons (symbol-name x) x))
6776                     gnus-extra-headers)
6777             nil
6778             t))))
6779      (list header
6780            (read-string (format "Limit to header %s (regexp): " header)))))
6781   (when (not (equal "" regexp))
6782     (prog1
6783         (let ((articles (gnus-summary-find-matching
6784                          (cons 'extra header) regexp 'all)))
6785           (unless articles
6786             (error "Found no matches for \"%s\"" regexp))
6787           (gnus-summary-limit articles))
6788       (gnus-summary-position-point))))
6789
6790 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6791 (make-obsolete
6792  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6793
6794 (defun gnus-summary-limit-to-unread (&optional all)
6795   "Limit the summary buffer to articles that are not marked as read.
6796 If ALL is non-nil, limit strictly to unread articles."
6797   (interactive "P")
6798   (if all
6799       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6800     (gnus-summary-limit-to-marks
6801      ;; Concat all the marks that say that an article is read and have
6802      ;; those removed.
6803      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6804            gnus-killed-mark gnus-kill-file-mark
6805            gnus-low-score-mark gnus-expirable-mark
6806            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6807            gnus-duplicate-mark gnus-souped-mark)
6808      'reverse)))
6809
6810 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6811 (make-obsolete 'gnus-summary-delete-marked-with
6812                'gnus-summary-limit-exlude-marks)
6813
6814 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6815   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6816 If REVERSE, limit the summary buffer to articles that are marked
6817 with MARKS.  MARKS can either be a string of marks or a list of marks.
6818 Returns how many articles were removed."
6819   (interactive "sMarks: ")
6820   (gnus-summary-limit-to-marks marks t))
6821
6822 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6823   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6824 If REVERSE (the prefix), limit the summary buffer to articles that are
6825 not marked with MARKS.  MARKS can either be a string of marks or a
6826 list of marks.
6827 Returns how many articles were removed."
6828   (interactive "sMarks: \nP")
6829   (prog1
6830       (let ((data gnus-newsgroup-data)
6831             (marks (if (listp marks) marks
6832                      (append marks nil))) ; Transform to list.
6833             articles)
6834         (while data
6835           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6836                   (memq (gnus-data-mark (car data)) marks))
6837             (push (gnus-data-number (car data)) articles))
6838           (setq data (cdr data)))
6839         (gnus-summary-limit articles))
6840     (gnus-summary-position-point)))
6841
6842 (defun gnus-summary-limit-to-score (&optional score)
6843   "Limit to articles with score at or above SCORE."
6844   (interactive "P")
6845   (setq score (if score
6846                   (prefix-numeric-value score)
6847                 (or gnus-summary-default-score 0)))
6848   (let ((data gnus-newsgroup-data)
6849         articles)
6850     (while data
6851       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6852                 score)
6853         (push (gnus-data-number (car data)) articles))
6854       (setq data (cdr data)))
6855     (prog1
6856         (gnus-summary-limit articles)
6857       (gnus-summary-position-point))))
6858
6859 (defun gnus-summary-limit-include-thread (id)
6860   "Display all the hidden articles that is in the thread with ID in it.
6861 When called interactively, ID is the Message-ID of the current
6862 article."
6863   (interactive (list (mail-header-id (gnus-summary-article-header))))
6864   (let ((articles (gnus-articles-in-thread
6865                    (gnus-id-to-thread (gnus-root-id id)))))
6866     (prog1
6867         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6868       (gnus-summary-limit-include-matching-articles
6869        "subject"
6870        (regexp-quote (gnus-simplify-subject-re
6871                       (mail-header-subject (gnus-id-to-header id)))))
6872       (gnus-summary-position-point))))
6873
6874 (defun gnus-summary-limit-include-matching-articles (header regexp)
6875   "Display all the hidden articles that have HEADERs that match REGEXP."
6876   (interactive (list (read-string "Match on header: ")
6877                      (read-string "Regexp: ")))
6878   (let ((articles (gnus-find-matching-articles header regexp)))
6879     (prog1
6880         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6881       (gnus-summary-position-point))))
6882
6883 (defun gnus-summary-limit-include-dormant ()
6884   "Display all the hidden articles that are marked as dormant.
6885 Note that this command only works on a subset of the articles currently
6886 fetched for this group."
6887   (interactive)
6888   (unless gnus-newsgroup-dormant
6889     (error "There are no dormant articles in this group"))
6890   (prog1
6891       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6892     (gnus-summary-position-point)))
6893
6894 (defun gnus-summary-limit-exclude-dormant ()
6895   "Hide all dormant articles."
6896   (interactive)
6897   (prog1
6898       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6899     (gnus-summary-position-point)))
6900
6901 (defun gnus-summary-limit-exclude-childless-dormant ()
6902   "Hide all dormant articles that have no children."
6903   (interactive)
6904   (let ((data (gnus-data-list t))
6905         articles d children)
6906     ;; Find all articles that are either not dormant or have
6907     ;; children.
6908     (while (setq d (pop data))
6909       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6910                 (and (setq children
6911                            (gnus-article-children (gnus-data-number d)))
6912                      (let (found)
6913                        (while children
6914                          (when (memq (car children) articles)
6915                            (setq children nil
6916                                  found t))
6917                          (pop children))
6918                        found)))
6919         (push (gnus-data-number d) articles)))
6920     ;; Do the limiting.
6921     (prog1
6922         (gnus-summary-limit articles)
6923       (gnus-summary-position-point))))
6924
6925 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6926   "Mark all unread excluded articles as read.
6927 If ALL, mark even excluded ticked and dormants as read."
6928   (interactive "P")
6929   (let ((articles (gnus-sorted-complement
6930                    (sort
6931                     (mapcar (lambda (h) (mail-header-number h))
6932                             gnus-newsgroup-headers)
6933                     '<)
6934                    (sort gnus-newsgroup-limit '<)))
6935         article)
6936     (setq gnus-newsgroup-unreads
6937           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6938     (if all
6939         (setq gnus-newsgroup-dormant nil
6940               gnus-newsgroup-marked nil
6941               gnus-newsgroup-reads
6942               (nconc
6943                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6944                gnus-newsgroup-reads))
6945       (while (setq article (pop articles))
6946         (unless (or (memq article gnus-newsgroup-dormant)
6947                     (memq article gnus-newsgroup-marked))
6948           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6949
6950 (defun gnus-summary-limit (articles &optional pop)
6951   (if pop
6952       ;; We pop the previous limit off the stack and use that.
6953       (setq articles (car gnus-newsgroup-limits)
6954             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6955     ;; We use the new limit, so we push the old limit on the stack.
6956     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6957   ;; Set the limit.
6958   (setq gnus-newsgroup-limit articles)
6959   (let ((total (length gnus-newsgroup-data))
6960         (data (gnus-data-find-list (gnus-summary-article-number)))
6961         (gnus-summary-mark-below nil)   ; Inhibit this.
6962         found)
6963     ;; This will do all the work of generating the new summary buffer
6964     ;; according to the new limit.
6965     (gnus-summary-prepare)
6966     ;; Hide any threads, possibly.
6967     (and gnus-show-threads
6968          gnus-thread-hide-subtree
6969          (gnus-summary-hide-all-threads))
6970     ;; Try to return to the article you were at, or one in the
6971     ;; neighborhood.
6972     (when data
6973       ;; We try to find some article after the current one.
6974       (while data
6975         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6976           (setq data nil
6977                 found t))
6978         (setq data (cdr data))))
6979     (unless found
6980       ;; If there is no data, that means that we were after the last
6981       ;; article.  The same goes when we can't find any articles
6982       ;; after the current one.
6983       (goto-char (point-max))
6984       (gnus-summary-find-prev))
6985     (gnus-set-mode-line 'summary)
6986     ;; We return how many articles were removed from the summary
6987     ;; buffer as a result of the new limit.
6988     (- total (length gnus-newsgroup-data))))
6989
6990 (defsubst gnus-invisible-cut-children (threads)
6991   (let ((num 0))
6992     (while threads
6993       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6994         (incf num))
6995       (pop threads))
6996     (< num 2)))
6997
6998 (defsubst gnus-cut-thread (thread)
6999   "Go forwards in the thread until we find an article that we want to display."
7000   (when (or (eq gnus-fetch-old-headers 'some)
7001             (eq gnus-fetch-old-headers 'invisible)
7002             (numberp gnus-fetch-old-headers)
7003             (eq gnus-build-sparse-threads 'some)
7004             (eq gnus-build-sparse-threads 'more))
7005     ;; Deal with old-fetched headers and sparse threads.
7006     (while (and
7007             thread
7008             (or
7009              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7010              (gnus-summary-article-ancient-p
7011               (mail-header-number (car thread))))
7012             (if (or (<= (length (cdr thread)) 1)
7013                     (eq gnus-fetch-old-headers 'invisible))
7014                 (setq gnus-newsgroup-limit
7015                       (delq (mail-header-number (car thread))
7016                             gnus-newsgroup-limit)
7017                       thread (cadr thread))
7018               (when (gnus-invisible-cut-children (cdr thread))
7019                 (let ((th (cdr thread)))
7020                   (while th
7021                     (if (memq (mail-header-number (caar th))
7022                               gnus-newsgroup-limit)
7023                         (setq thread (car th)
7024                               th nil)
7025                       (setq th (cdr th))))))))))
7026   thread)
7027
7028 (defun gnus-cut-threads (threads)
7029   "Cut off all uninteresting articles from the beginning of threads."
7030   (when (or (eq gnus-fetch-old-headers 'some)
7031             (eq gnus-fetch-old-headers 'invisible)
7032             (numberp gnus-fetch-old-headers)
7033             (eq gnus-build-sparse-threads 'some)
7034             (eq gnus-build-sparse-threads 'more))
7035     (let ((th threads))
7036       (while th
7037         (setcar th (gnus-cut-thread (car th)))
7038         (setq th (cdr th)))))
7039   ;; Remove nixed out threads.
7040   (delq nil threads))
7041
7042 (defun gnus-summary-initial-limit (&optional show-if-empty)
7043   "Figure out what the initial limit is supposed to be on group entry.
7044 This entails weeding out unwanted dormants, low-scored articles,
7045 fetch-old-headers verbiage, and so on."
7046   ;; Most groups have nothing to remove.
7047   (if (or gnus-inhibit-limiting
7048           (and (null gnus-newsgroup-dormant)
7049                (not (eq gnus-fetch-old-headers 'some))
7050                (not (numberp gnus-fetch-old-headers))
7051                (not (eq gnus-fetch-old-headers 'invisible))
7052                (null gnus-summary-expunge-below)
7053                (not (eq gnus-build-sparse-threads 'some))
7054                (not (eq gnus-build-sparse-threads 'more))
7055                (null gnus-thread-expunge-below)
7056                (not gnus-use-nocem)))
7057       ()                                ; Do nothing.
7058     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7059     (setq gnus-newsgroup-limit nil)
7060     (mapatoms
7061      (lambda (node)
7062        (unless (car (symbol-value node))
7063          ;; These threads have no parents -- they are roots.
7064          (let ((nodes (cdr (symbol-value node)))
7065                thread)
7066            (while nodes
7067              (if (and gnus-thread-expunge-below
7068                       (< (gnus-thread-total-score (car nodes))
7069                          gnus-thread-expunge-below))
7070                  (gnus-expunge-thread (pop nodes))
7071                (setq thread (pop nodes))
7072                (gnus-summary-limit-children thread))))))
7073      gnus-newsgroup-dependencies)
7074     ;; If this limitation resulted in an empty group, we might
7075     ;; pop the previous limit and use it instead.
7076     (when (and (not gnus-newsgroup-limit)
7077                show-if-empty)
7078       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7079     gnus-newsgroup-limit))
7080
7081 (defun gnus-summary-limit-children (thread)
7082   "Return 1 if this subthread is visible and 0 if it is not."
7083   ;; First we get the number of visible children to this thread.  This
7084   ;; is done by recursing down the thread using this function, so this
7085   ;; will really go down to a leaf article first, before slowly
7086   ;; working its way up towards the root.
7087   (when thread
7088     (let ((children
7089            (if (cdr thread)
7090                (apply '+ (mapcar 'gnus-summary-limit-children
7091                                  (cdr thread)))
7092              0))
7093           (number (mail-header-number (car thread)))
7094           score)
7095       (if (and
7096            (not (memq number gnus-newsgroup-marked))
7097            (or
7098             ;; If this article is dormant and has absolutely no visible
7099             ;; children, then this article isn't visible.
7100             (and (memq number gnus-newsgroup-dormant)
7101                  (zerop children))
7102             ;; If this is "fetch-old-headered" and there is no
7103             ;; visible children, then we don't want this article.
7104             (and (or (eq gnus-fetch-old-headers 'some)
7105                      (numberp gnus-fetch-old-headers))
7106                  (gnus-summary-article-ancient-p number)
7107                  (zerop children))
7108             ;; If this is "fetch-old-headered" and `invisible', then
7109             ;; we don't want this article.
7110             (and (eq gnus-fetch-old-headers 'invisible)
7111                  (gnus-summary-article-ancient-p number))
7112             ;; If this is a sparsely inserted article with no children,
7113             ;; we don't want it.
7114             (and (eq gnus-build-sparse-threads 'some)
7115                  (gnus-summary-article-sparse-p number)
7116                  (zerop children))
7117             ;; If we use expunging, and this article is really
7118             ;; low-scored, then we don't want this article.
7119             (when (and gnus-summary-expunge-below
7120                        (< (setq score
7121                                 (or (cdr (assq number gnus-newsgroup-scored))
7122                                     gnus-summary-default-score))
7123                           gnus-summary-expunge-below))
7124               ;; We increase the expunge-tally here, but that has
7125               ;; nothing to do with the limits, really.
7126               (incf gnus-newsgroup-expunged-tally)
7127               ;; We also mark as read here, if that's wanted.
7128               (when (and gnus-summary-mark-below
7129                          (< score gnus-summary-mark-below))
7130                 (setq gnus-newsgroup-unreads
7131                       (delq number gnus-newsgroup-unreads))
7132                 (if gnus-newsgroup-auto-expire
7133                     (push number gnus-newsgroup-expirable)
7134                   (push (cons number gnus-low-score-mark)
7135                         gnus-newsgroup-reads)))
7136               t)
7137             ;; Check NoCeM things.
7138             (if (and gnus-use-nocem
7139                      (gnus-nocem-unwanted-article-p
7140                       (mail-header-id (car thread))))
7141                 (progn
7142                   (setq gnus-newsgroup-unreads
7143                         (delq number gnus-newsgroup-unreads))
7144                   t))))
7145           ;; Nope, invisible article.
7146           0
7147         ;; Ok, this article is to be visible, so we add it to the limit
7148         ;; and return 1.
7149         (push number gnus-newsgroup-limit)
7150         1))))
7151
7152 (defun gnus-expunge-thread (thread)
7153   "Mark all articles in THREAD as read."
7154   (let* ((number (mail-header-number (car thread))))
7155     (incf gnus-newsgroup-expunged-tally)
7156     ;; We also mark as read here, if that's wanted.
7157     (setq gnus-newsgroup-unreads
7158           (delq number gnus-newsgroup-unreads))
7159     (if gnus-newsgroup-auto-expire
7160         (push number gnus-newsgroup-expirable)
7161       (push (cons number gnus-low-score-mark)
7162             gnus-newsgroup-reads)))
7163   ;; Go recursively through all subthreads.
7164   (mapcar 'gnus-expunge-thread (cdr thread)))
7165
7166 ;; Summary article oriented commands
7167
7168 (defun gnus-summary-refer-parent-article (n)
7169   "Refer parent article N times.
7170 If N is negative, go to ancestor -N instead.
7171 The difference between N and the number of articles fetched is returned."
7172   (interactive "p")
7173   (let ((skip 1)
7174         error header ref)
7175     (when (not (natnump n))
7176       (setq skip (abs n)
7177             n 1))
7178     (while (and (> n 0)
7179                 (not error))
7180       (setq header (gnus-summary-article-header))
7181       (if (and (eq (mail-header-number header)
7182                    (cdr gnus-article-current))
7183                (equal gnus-newsgroup-name
7184                       (car gnus-article-current)))
7185           ;; If we try to find the parent of the currently
7186           ;; displayed article, then we take a look at the actual
7187           ;; References header, since this is slightly more
7188           ;; reliable than the References field we got from the
7189           ;; server.
7190           (save-excursion
7191             (set-buffer gnus-original-article-buffer)
7192             (nnheader-narrow-to-headers)
7193             (unless (setq ref (message-fetch-field "references"))
7194               (setq ref (message-fetch-field "in-reply-to")))
7195             (widen))
7196         (setq ref
7197               ;; It's not the current article, so we take a bet on
7198               ;; the value we got from the server.
7199               (mail-header-references header)))
7200       (if (and ref
7201                (not (equal ref "")))
7202           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7203             (gnus-message 1 "Couldn't find parent"))
7204         (gnus-message 1 "No references in article %d"
7205                       (gnus-summary-article-number))
7206         (setq error t))
7207       (decf n))
7208     (gnus-summary-position-point)
7209     n))
7210
7211 (defun gnus-summary-refer-references ()
7212   "Fetch all articles mentioned in the References header.
7213 Return the number of articles fetched."
7214   (interactive)
7215   (let ((ref (mail-header-references (gnus-summary-article-header)))
7216         (current (gnus-summary-article-number))
7217         (n 0))
7218     (if (or (not ref)
7219             (equal ref ""))
7220         (error "No References in the current article")
7221       ;; For each Message-ID in the References header...
7222       (while (string-match "<[^>]*>" ref)
7223         (incf n)
7224         ;; ... fetch that article.
7225         (gnus-summary-refer-article
7226          (prog1 (match-string 0 ref)
7227            (setq ref (substring ref (match-end 0))))))
7228       (gnus-summary-goto-subject current)
7229       (gnus-summary-position-point)
7230       n)))
7231
7232 (defun gnus-summary-refer-thread (&optional limit)
7233   "Fetch all articles in the current thread.
7234 If LIMIT (the numerical prefix), fetch that many old headers instead
7235 of what's specified by the `gnus-refer-thread-limit' variable."
7236   (interactive "P")
7237   (let ((id (mail-header-id (gnus-summary-article-header)))
7238         (limit (if limit (prefix-numeric-value limit)
7239                  gnus-refer-thread-limit)))
7240     ;; We want to fetch LIMIT *old* headers, but we also have to
7241     ;; re-fetch all the headers in the current buffer, because many of
7242     ;; them may be undisplayed.  So we adjust LIMIT.
7243     (when (numberp limit)
7244       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7245     (unless (eq gnus-fetch-old-headers 'invisible)
7246       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7247       ;; Retrieve the headers and read them in.
7248       (if (eq (gnus-retrieve-headers
7249                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7250               'nov)
7251           (gnus-build-all-threads)
7252         (error "Can't fetch thread from backends that don't support NOV"))
7253       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7254     (gnus-summary-limit-include-thread id)))
7255
7256 (defun gnus-summary-refer-article (message-id)
7257   "Fetch an article specified by MESSAGE-ID."
7258   (interactive "sMessage-ID: ")
7259   (when (and (stringp message-id)
7260              (not (zerop (length message-id))))
7261     ;; Construct the correct Message-ID if necessary.
7262     ;; Suggested by tale@pawl.rpi.edu.
7263     (unless (string-match "^<" message-id)
7264       (setq message-id (concat "<" message-id)))
7265     (unless (string-match ">$" message-id)
7266       (setq message-id (concat message-id ">")))
7267     (let* ((header (gnus-id-to-header message-id))
7268            (sparse (and header
7269                         (gnus-summary-article-sparse-p
7270                          (mail-header-number header))
7271                         (memq (mail-header-number header)
7272                               gnus-newsgroup-limit)))
7273            number)
7274       (cond
7275        ;; If the article is present in the buffer we just go to it.
7276        ((and header
7277              (or (not (gnus-summary-article-sparse-p
7278                        (mail-header-number header)))
7279                  sparse))
7280         (prog1
7281             (gnus-summary-goto-article
7282              (mail-header-number header) nil t)
7283           (when sparse
7284             (gnus-summary-update-article (mail-header-number header)))))
7285        (t
7286         ;; We fetch the article.
7287         (catch 'found
7288           (dolist (gnus-override-method (gnus-refer-article-methods))
7289             (gnus-check-server gnus-override-method)
7290             ;; Fetch the header, and display the article.
7291             (when (setq number (gnus-summary-insert-subject message-id))
7292               (gnus-summary-select-article nil nil nil number)
7293               (throw 'found t)))
7294           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7295
7296 (defun gnus-refer-article-methods ()
7297   "Return a list of referrable methods."
7298   (cond
7299    ;; No method, so we default to current and native.
7300    ((null gnus-refer-article-method)
7301     (list gnus-current-select-method gnus-select-method))
7302    ;; Current.
7303    ((eq 'current gnus-refer-article-method)
7304     (list gnus-current-select-method))
7305    ;; List of select methods.
7306    ((not (and (symbolp (car gnus-refer-article-method))
7307               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7308     (let (out)
7309       (dolist (method gnus-refer-article-method)
7310         (push (if (eq 'current method)
7311                   gnus-current-select-method
7312                 method)
7313               out))
7314       (nreverse out)))
7315    ;; One single select method.
7316    (t
7317     (list gnus-refer-article-method))))
7318
7319 (defun gnus-summary-edit-parameters ()
7320   "Edit the group parameters of the current group."
7321   (interactive)
7322   (gnus-group-edit-group gnus-newsgroup-name 'params))
7323
7324 (defun gnus-summary-customize-parameters ()
7325   "Customize the group parameters of the current group."
7326   (interactive)
7327   (gnus-group-customize gnus-newsgroup-name))
7328
7329 (defun gnus-summary-enter-digest-group (&optional force)
7330   "Enter an nndoc group based on the current article.
7331 If FORCE, force a digest interpretation.  If not, try
7332 to guess what the document format is."
7333   (interactive "P")
7334   (let ((conf gnus-current-window-configuration))
7335     (save-excursion
7336       (gnus-summary-select-article))
7337     (setq gnus-current-window-configuration conf)
7338     (let* ((name (format "%s-%d"
7339                          (gnus-group-prefixed-name
7340                           gnus-newsgroup-name (list 'nndoc ""))
7341                          (save-excursion
7342                            (set-buffer gnus-summary-buffer)
7343                            gnus-current-article)))
7344            (ogroup gnus-newsgroup-name)
7345            (params (append (gnus-info-params (gnus-get-info ogroup))
7346                            (list (cons 'to-group ogroup))
7347                            (list (cons 'save-article-group ogroup))))
7348            (case-fold-search t)
7349            (buf (current-buffer))
7350            dig to-address)
7351       (save-excursion
7352         (set-buffer gnus-original-article-buffer)
7353         ;; Have the digest group inherit the main mail address of
7354         ;; the parent article.
7355         (when (setq to-address (or (message-fetch-field "reply-to")
7356                                    (message-fetch-field "from")))
7357           (setq params (append
7358                         (list (cons 'to-address
7359                                     (funcall gnus-decode-encoded-word-function
7360                                              to-address))))))
7361         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7362         (insert-buffer-substring gnus-original-article-buffer)
7363         ;; Remove lines that may lead nndoc to misinterpret the
7364         ;; document type.
7365         (narrow-to-region
7366          (goto-char (point-min))
7367          (or (search-forward "\n\n" nil t) (point)))
7368         (goto-char (point-min))
7369         (delete-matching-lines "^Path:\\|^From ")
7370         (widen))
7371       (unwind-protect
7372           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7373                     (gnus-newsgroup-ephemeral-ignored-charsets
7374                      gnus-newsgroup-ignored-charsets))
7375                 (gnus-group-read-ephemeral-group
7376                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7377                               (nndoc-article-type
7378                                ,(if force 'mbox 'guess))) t))
7379               ;; Make all postings to this group go to the parent group.
7380               (nconc (gnus-info-params (gnus-get-info name))
7381                      params)
7382             ;; Couldn't select this doc group.
7383             (switch-to-buffer buf)
7384             (gnus-set-global-variables)
7385             (gnus-configure-windows 'summary)
7386             (gnus-message 3 "Article couldn't be entered?"))
7387         (kill-buffer dig)))))
7388
7389 (defun gnus-summary-read-document (n)
7390   "Open a new group based on the current article(s).
7391 This will allow you to read digests and other similar
7392 documents as newsgroups.
7393 Obeys the standard process/prefix convention."
7394   (interactive "P")
7395   (let* ((articles (gnus-summary-work-articles n))
7396          (ogroup gnus-newsgroup-name)
7397          (params (append (gnus-info-params (gnus-get-info ogroup))
7398                          (list (cons 'to-group ogroup))))
7399          article group egroup groups vgroup)
7400     (while (setq article (pop articles))
7401       (setq group (format "%s-%d" gnus-newsgroup-name article))
7402       (gnus-summary-remove-process-mark article)
7403       (when (gnus-summary-display-article article)
7404         (save-excursion
7405           (with-temp-buffer
7406             (insert-buffer-substring gnus-original-article-buffer)
7407             ;; Remove some headers that may lead nndoc to make
7408             ;; the wrong guess.
7409             (message-narrow-to-head)
7410             (goto-char (point-min))
7411             (delete-matching-lines "^\\(Path\\):\\|^From ")
7412             (widen)
7413             (if (setq egroup
7414                       (gnus-group-read-ephemeral-group
7415                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7416                                      (nndoc-article-type guess))
7417                        t nil t))
7418                 (progn
7419                   ;; Make all postings to this group go to the parent group.
7420                   (nconc (gnus-info-params (gnus-get-info egroup))
7421                          params)
7422                   (push egroup groups))
7423               ;; Couldn't select this doc group.
7424               (gnus-error 3 "Article couldn't be entered"))))))
7425     ;; Now we have selected all the documents.
7426     (cond
7427      ((not groups)
7428       (error "None of the articles could be interpreted as documents"))
7429      ((gnus-group-read-ephemeral-group
7430        (setq vgroup (format
7431                      "nnvirtual:%s-%s" gnus-newsgroup-name
7432                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7433        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7434        t
7435        (cons (current-buffer) 'summary)))
7436      (t
7437       (error "Couldn't select virtual nndoc group")))))
7438
7439 (defun gnus-summary-isearch-article (&optional regexp-p)
7440   "Do incremental search forward on the current article.
7441 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7442   (interactive "P")
7443   (let* ((gnus-inhibit-treatment t)
7444          (old (gnus-summary-select-article)))
7445     (gnus-configure-windows 'article)
7446     (gnus-eval-in-buffer-window gnus-article-buffer
7447       (save-restriction
7448         (widen)
7449         (when (eq 'old old)
7450           (gnus-article-show-all-headers))
7451         (goto-char (point-min))
7452         (isearch-forward regexp-p)))))
7453
7454 (defun gnus-summary-search-article-forward (regexp &optional backward)
7455   "Search for an article containing REGEXP forward.
7456 If BACKWARD, search backward instead."
7457   (interactive
7458    (list (read-string
7459           (format "Search article %s (regexp%s): "
7460                   (if current-prefix-arg "backward" "forward")
7461                   (if gnus-last-search-regexp
7462                       (concat ", default " gnus-last-search-regexp)
7463                     "")))
7464          current-prefix-arg))
7465   (if (string-equal regexp "")
7466       (setq regexp (or gnus-last-search-regexp ""))
7467     (setq gnus-last-search-regexp regexp)
7468     (setq gnus-article-before-search gnus-current-article))
7469   ;; Intentionally set gnus-last-article.
7470   (setq gnus-last-article gnus-article-before-search)
7471   (let ((gnus-last-article gnus-last-article))
7472     (if (gnus-summary-search-article regexp backward)
7473         (gnus-summary-show-thread)
7474       (error "Search failed: \"%s\"" regexp))))
7475
7476 (defun gnus-summary-search-article-backward (regexp)
7477   "Search for an article containing REGEXP backward."
7478   (interactive
7479    (list (read-string
7480           (format "Search article backward (regexp%s): "
7481                   (if gnus-last-search-regexp
7482                       (concat ", default " gnus-last-search-regexp)
7483                     "")))))
7484   (gnus-summary-search-article-forward regexp 'backward))
7485
7486 (eval-when-compile
7487   (defmacro gnus-summary-search-article-position-point (regexp backward)
7488     "Dehighlight the last matched text and goto the beginning position."
7489     (` (if (and gnus-summary-search-article-matched-data
7490                 (let ((text (caddr gnus-summary-search-article-matched-data))
7491                       (inhibit-read-only t)
7492                       buffer-read-only)
7493                   (delete-region
7494                    (goto-char (car gnus-summary-search-article-matched-data))
7495                    (cadr gnus-summary-search-article-matched-data))
7496                   (insert text)
7497                   (string-match (, regexp) text)))
7498            (if (, backward) (beginning-of-line) (end-of-line))
7499          (goto-char (if (, backward) (point-max) (point-min))))))
7500
7501   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7502     "Place point where X-Face image is displayed."
7503     (if (featurep 'xemacs)
7504         (` (let ((end (if (search-forward "\n\n" nil t)
7505                           (goto-char (1- (point)))
7506                         (point-min)))
7507                  extent)
7508              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7509              (unless (and (re-search-forward "^From:" end t)
7510                           (setq extent (extent-at (point)))
7511                           (extent-begin-glyph extent))
7512                (goto-char (, opoint)))))
7513       (` (let ((end (if (search-forward "\n\n" nil t)
7514                         (goto-char (1- (point)))
7515                       (point-min)))
7516                (start (or (search-backward "\n\n" nil t) (point-min))))
7517            (goto-char
7518             (or (text-property-any start end 'x-face-image t);; x-face-e21
7519                 (text-property-any start end 'x-face-mule-bitmap-image t)
7520                 (, opoint)))))))
7521
7522   (defmacro gnus-summary-search-article-highlight-matched-text
7523     (backward treated x-face)
7524     "Highlight matched text in the function `gnus-summary-search-article'."
7525     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7526              (end (set-marker (make-marker) (match-end 0)))
7527              (inhibit-read-only t)
7528              buffer-read-only)
7529          (unless treated
7530            (let ((,@
7531                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7532                     (mapcar
7533                      (lambda (item) (setq items (delq item items)))
7534                      '(gnus-treat-buttonize
7535                        gnus-treat-fill-article
7536                        gnus-treat-fill-long-lines
7537                        gnus-treat-emphasize
7538                        gnus-treat-highlight-headers
7539                        gnus-treat-highlight-citation
7540                        gnus-treat-highlight-signature
7541                        gnus-treat-overstrike
7542                        gnus-treat-display-xface
7543                        gnus-treat-buttonize-head
7544                        gnus-treat-decode-article-as-default-mime-charset))
7545                     (static-if (featurep 'xemacs)
7546                         items
7547                       (cons '(x-face-mule-delete-x-face-field
7548                               (quote never))
7549                             items))))
7550                  (gnus-treat-display-xface
7551                   (when (, x-face) gnus-treat-display-xface)))
7552              (gnus-article-prepare-mime-display)))
7553          (goto-char (if (, backward) start end))
7554          (when (, x-face)
7555            (gnus-summary-search-article-highlight-goto-x-face (point)))
7556          (setq gnus-summary-search-article-matched-data
7557                (list start end (buffer-substring start end)))
7558          (unless (eq start end);; matched text has been deleted. :-<
7559            (put-text-property start end 'face
7560                               (or (find-face 'isearch)
7561                                   'secondary-selection))))))
7562   )
7563
7564 (defun gnus-summary-search-article (regexp &optional backward)
7565   "Search for an article containing REGEXP.
7566 Optional argument BACKWARD means do search for backward.
7567 `gnus-select-article-hook' is not called during the search."
7568   ;; We have to require this here to make sure that the following
7569   ;; dynamic binding isn't shadowed by autoloading.
7570   (require 'gnus-async)
7571   (require 'gnus-art)
7572   (let ((gnus-select-article-hook nil)  ;Disable hook.
7573         (gnus-article-prepare-hook nil)
7574         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7575         (gnus-use-article-prefetch nil)
7576         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7577         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7578         (sum (current-buffer))
7579         (found nil)
7580         point treated)
7581     (gnus-save-hidden-threads
7582       (static-if (featurep 'xemacs)
7583           (let ((gnus-inhibit-treatment t))
7584             (setq treated (eq 'old (gnus-summary-select-article)))
7585             (when (and treated
7586                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7587                                  (window-live-p (get-buffer-window
7588                                                  gnus-article-buffer t)))))
7589               (gnus-summary-select-article nil t)
7590               (setq treated nil)))
7591         (let ((gnus-inhibit-treatment t)
7592               (x-face-mule-delete-x-face-field 'never))
7593           (setq treated (eq 'old (gnus-summary-select-article)))
7594           (when (and treated
7595                      (not
7596                       (and (gnus-buffer-live-p gnus-article-buffer)
7597                            (window-live-p (get-buffer-window
7598                                            gnus-article-buffer t))
7599                            (or (not (string-match "^\\^X-Face:" regexp))
7600                                (with-current-buffer gnus-article-buffer
7601                                  gnus-summary-search-article-matched-data)))))
7602             (gnus-summary-select-article nil t)
7603             (setq treated nil))))
7604       (set-buffer gnus-article-buffer)
7605       (widen)
7606       (if treated
7607           (progn
7608             (gnus-article-show-all-headers)
7609             (gnus-summary-search-article-position-point regexp backward))
7610         (goto-char (if backward (point-max) (point-min))))
7611       (while (not found)
7612         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7613         (if (if backward
7614                 (re-search-backward regexp nil t)
7615               (re-search-forward regexp nil t))
7616             ;; We found the regexp.
7617             (progn
7618               (gnus-summary-search-article-highlight-matched-text
7619                backward treated (string-match "^\\^X-Face:" regexp))
7620               (setq found 'found)
7621               (forward-line
7622                (/ (- 2 (window-height
7623                         (get-buffer-window gnus-article-buffer t)))
7624                   2))
7625               (set-window-start
7626                (get-buffer-window (current-buffer))
7627                (point))
7628               (set-buffer sum)
7629               (setq point (point)))
7630           ;; We didn't find it, so we go to the next article.
7631           (set-buffer sum)
7632           (setq found 'not)
7633           (while (eq found 'not)
7634             (if (not (if backward (gnus-summary-find-prev)
7635                        (gnus-summary-find-next)))
7636                 ;; No more articles.
7637                 (setq found t)
7638               ;; Select the next article and adjust point.
7639               (unless (gnus-summary-article-sparse-p
7640                        (gnus-summary-article-number))
7641                 (setq found nil)
7642                 (let ((gnus-inhibit-treatment t))
7643                   (gnus-summary-select-article))
7644                 (setq treated nil)
7645                 (set-buffer gnus-article-buffer)
7646                 (widen)
7647                 (goto-char (if backward (point-max) (point-min))))))))
7648       (gnus-message 7 ""))
7649     ;; Return whether we found the regexp.
7650     (when (eq found 'found)
7651       (goto-char point)
7652       (gnus-summary-show-thread)
7653       (gnus-summary-goto-subject gnus-current-article)
7654       (gnus-summary-position-point)
7655       t)))
7656
7657 (defun gnus-find-matching-articles (header regexp)
7658   "Return a list of all articles that match REGEXP on HEADER.
7659 This search includes all articles in the current group that Gnus has
7660 fetched headers for, whether they are displayed or not."
7661   (let ((articles nil)
7662         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7663         (case-fold-search t))
7664     (dolist (header gnus-newsgroup-headers)
7665       (when (string-match regexp (funcall func header))
7666         (push (mail-header-number header) articles)))
7667     (nreverse articles)))
7668
7669 (defun gnus-summary-find-matching (header regexp &optional backward unread
7670                                           not-case-fold)
7671   "Return a list of all articles that match REGEXP on HEADER.
7672 The search stars on the current article and goes forwards unless
7673 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7674 If UNREAD is non-nil, only unread articles will
7675 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7676 in the comparisons."
7677   (let ((case-fold-search (not not-case-fold))
7678         articles d func)
7679     (if (consp header)
7680         (if (eq (car header) 'extra)
7681             (setq func
7682                   `(lambda (h)
7683                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7684                          "")))
7685           (error "%s is an invalid header" header))
7686       (unless (fboundp (intern (concat "mail-header-" header)))
7687         (error "%s is not a valid header" header))
7688       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7689     (dolist (d (if (eq backward 'all)
7690                    gnus-newsgroup-data
7691                  (gnus-data-find-list
7692                   (gnus-summary-article-number)
7693                   (gnus-data-list backward))))
7694       (when (and (or (not unread)       ; We want all articles...
7695                      (gnus-data-unread-p d)) ; Or just unreads.
7696                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7697                  (string-match regexp
7698                                (funcall func (gnus-data-header d)))) ; Match.
7699         (push (gnus-data-number d) articles))) ; Success!
7700     (nreverse articles)))
7701
7702 (defun gnus-summary-execute-command (header regexp command &optional backward)
7703   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7704 If HEADER is an empty string (or nil), the match is done on the entire
7705 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7706   (interactive
7707    (list (let ((completion-ignore-case t))
7708            (completing-read
7709             "Header name: "
7710             (mapcar (lambda (header) (list (format "%s" header)))
7711                     (append
7712                      '("Number" "Subject" "From" "Lines" "Date"
7713                        "Message-ID" "Xref" "References" "Body")
7714                      gnus-extra-headers))
7715             nil 'require-match))
7716          (read-string "Regexp: ")
7717          (read-key-sequence "Command: ")
7718          current-prefix-arg))
7719   (when (equal header "Body")
7720     (setq header ""))
7721   ;; Hidden thread subtrees must be searched as well.
7722   (gnus-summary-show-all-threads)
7723   ;; We don't want to change current point nor window configuration.
7724   (save-excursion
7725     (save-window-excursion
7726       (gnus-message 6 "Executing %s..." (key-description command))
7727       ;; We'd like to execute COMMAND interactively so as to give arguments.
7728       (gnus-execute header regexp
7729                     `(call-interactively ',(key-binding command))
7730                     backward)
7731       (gnus-message 6 "Executing %s...done" (key-description command)))))
7732
7733 (defun gnus-summary-beginning-of-article ()
7734   "Scroll the article back to the beginning."
7735   (interactive)
7736   (gnus-summary-select-article)
7737   (gnus-configure-windows 'article)
7738   (gnus-eval-in-buffer-window gnus-article-buffer
7739     (widen)
7740     (goto-char (point-min))
7741     (when gnus-page-broken
7742       (gnus-narrow-to-page))))
7743
7744 (defun gnus-summary-end-of-article ()
7745   "Scroll to the end of the article."
7746   (interactive)
7747   (gnus-summary-select-article)
7748   (gnus-configure-windows 'article)
7749   (gnus-eval-in-buffer-window gnus-article-buffer
7750     (widen)
7751     (goto-char (point-max))
7752     (recenter -3)
7753     (when gnus-page-broken
7754       (gnus-narrow-to-page))))
7755
7756 (defun gnus-summary-print-article (&optional filename n)
7757   "Generate and print a PostScript image of the N next (mail) articles.
7758
7759 If N is negative, print the N previous articles.  If N is nil and articles
7760 have been marked with the process mark, print these instead.
7761
7762 If the optional first argument FILENAME is nil, send the image to the
7763 printer.  If FILENAME is a string, save the PostScript image in a file with
7764 that name.  If FILENAME is a number, prompt the user for the name of the file
7765 to save in."
7766   (interactive (list (ps-print-preprint current-prefix-arg)))
7767   (dolist (article (gnus-summary-work-articles n))
7768     (gnus-summary-select-article nil nil 'pseudo article)
7769     (gnus-eval-in-buffer-window gnus-article-buffer
7770       (let ((buffer (generate-new-buffer " *print*")))
7771         (unwind-protect
7772             (progn
7773               (copy-to-buffer buffer (point-min) (point-max))
7774               (set-buffer buffer)
7775               (gnus-article-delete-invisible-text)
7776               (when (gnus-visual-p 'article-highlight 'highlight)
7777                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7778                 ;; highlight.
7779                 (let ((gnus-article-buffer buffer))
7780                   (gnus-article-highlight-citation t)
7781                   (gnus-article-highlight-signature)))
7782               (let ((ps-left-header
7783                      (list
7784                       (concat "("
7785                               (mail-header-subject gnus-current-headers) ")")
7786                       (concat "("
7787                               (mail-header-from gnus-current-headers) ")")))
7788                     (ps-right-header
7789                      (list
7790                       "/pagenumberstring load"
7791                       (concat "("
7792                               (mail-header-date gnus-current-headers) ")"))))
7793                 (gnus-run-hooks 'gnus-ps-print-hook)
7794                 (save-excursion
7795                   (if window-system
7796                       (ps-spool-buffer-with-faces)
7797                     (ps-spool-buffer)))))
7798           (kill-buffer buffer))))
7799     (gnus-summary-remove-process-mark article))
7800   (ps-despool filename))
7801
7802 (defun gnus-summary-show-article (&optional arg)
7803   "Force re-fetching of the current article.
7804 If ARG (the prefix) is a number, show the article with the charset
7805 defined in `gnus-summary-show-article-charset-alist', or the charset
7806 inputed.
7807 If ARG (the prefix) is non-nil and not a number, show the raw article
7808 without any article massaging functions being run."
7809   (interactive "P")
7810   (cond
7811    ((numberp arg)
7812     (let ((gnus-newsgroup-charset
7813            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7814                (mm-read-coding-system "Charset: ")))
7815           (gnus-newsgroup-ignored-charsets 'gnus-all))
7816       (gnus-summary-select-article nil 'force)
7817       (let ((deps gnus-newsgroup-dependencies)
7818             head header)
7819         (save-excursion
7820           (set-buffer gnus-original-article-buffer)
7821           (save-restriction
7822             (message-narrow-to-head)
7823             (setq head (buffer-string)))
7824           (with-temp-buffer
7825             (insert (format "211 %d Article retrieved.\n"
7826                             (cdr gnus-article-current)))
7827             (insert head)
7828             (insert ".\n")
7829             (let ((nntp-server-buffer (current-buffer)))
7830               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7831         (gnus-data-set-header
7832          (gnus-data-find (cdr gnus-article-current))
7833          header)
7834         (gnus-summary-update-article-line
7835          (cdr gnus-article-current) header))))
7836    ((not arg)
7837     ;; Select the article the normal way.
7838     (gnus-summary-select-article nil 'force))
7839    (t
7840     ;; We have to require this here to make sure that the following
7841     ;; dynamic binding isn't shadowed by autoloading.
7842     (require 'gnus-async)
7843     (require 'gnus-art)
7844     ;; Bind the article treatment functions to nil.
7845     (let ((gnus-have-all-headers t)
7846           gnus-article-prepare-hook
7847           gnus-article-decode-hook
7848           gnus-break-pages
7849           gnus-show-mime
7850           (gnus-inhibit-treatment t))
7851       (gnus-summary-select-article nil 'force))))
7852   (gnus-summary-goto-subject gnus-current-article)
7853   (gnus-summary-position-point))
7854
7855 (defun gnus-summary-show-raw-article ()
7856   "Show the raw article without any article massaging functions being run."
7857   (interactive)
7858   (gnus-summary-show-article t))
7859
7860 (defun gnus-summary-verbose-headers (&optional arg)
7861   "Toggle permanent full header display.
7862 If ARG is a positive number, turn header display on.
7863 If ARG is a negative number, turn header display off."
7864   (interactive "P")
7865   (setq gnus-show-all-headers
7866         (cond ((or (not (numberp arg))
7867                    (zerop arg))
7868                (not gnus-show-all-headers))
7869               ((natnump arg)
7870                t)))
7871   (gnus-summary-show-article))
7872
7873 (defun gnus-summary-toggle-header (&optional arg)
7874   "Show the headers if they are hidden, or hide them if they are shown.
7875 If ARG is a positive number, show the entire header.
7876 If ARG is a negative number, hide the unwanted header lines."
7877   (interactive "P")
7878   (save-excursion
7879     (set-buffer gnus-article-buffer)
7880     (save-restriction
7881       (let* ((buffer-read-only nil)
7882              (inhibit-point-motion-hooks t)
7883              hidden e)
7884         (setq hidden
7885               (if (numberp arg)
7886                   (>= arg 0)
7887                 (save-restriction
7888                   (article-narrow-to-head)
7889                   (gnus-article-hidden-text-p 'headers))))
7890         (goto-char (point-min))
7891         (when (search-forward "\n\n" nil t)
7892           (delete-region (point-min) (1- (point))))
7893         (goto-char (point-min))
7894         (save-excursion
7895           (set-buffer gnus-original-article-buffer)
7896           (goto-char (point-min))
7897           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7898         (insert-buffer-substring gnus-original-article-buffer 1 e)
7899         (save-restriction
7900           (narrow-to-region (point-min) (point))
7901           (article-decode-encoded-words)
7902           (if  hidden
7903               (let ((gnus-treat-hide-headers nil)
7904                     (gnus-treat-hide-boring-headers nil))
7905                 (setq gnus-article-wash-types
7906                       (delq 'headers gnus-article-wash-types))
7907                 (gnus-treat-article 'head))
7908             (gnus-treat-article 'head)))
7909         (gnus-set-mode-line 'article)))))
7910
7911 (defun gnus-summary-show-all-headers ()
7912   "Make all header lines visible."
7913   (interactive)
7914   (gnus-summary-toggle-header 1))
7915
7916 (defun gnus-summary-toggle-mime (&optional arg)
7917   "Toggle MIME processing.
7918 If ARG is a positive number, turn MIME processing on."
7919   (interactive "P")
7920   (setq gnus-show-mime
7921         (if (null arg)
7922             (not gnus-show-mime)
7923           (> (prefix-numeric-value arg) 0)))
7924   (gnus-summary-select-article t 'force))
7925
7926 (defun gnus-summary-caesar-message (&optional arg)
7927   "Caesar rotate the current article by 13.
7928 The numerical prefix specifies how many places to rotate each letter
7929 forward."
7930   (interactive "P")
7931   (gnus-summary-select-article)
7932   (let ((mail-header-separator ""))
7933     (gnus-eval-in-buffer-window gnus-article-buffer
7934       (save-restriction
7935         (widen)
7936         (let ((start (window-start))
7937               buffer-read-only)
7938           (message-caesar-buffer-body arg)
7939           (set-window-start (get-buffer-window (current-buffer)) start))))))
7940
7941 (defun gnus-summary-stop-page-breaking ()
7942   "Stop page breaking in the current article."
7943   (interactive)
7944   (gnus-summary-select-article)
7945   (gnus-eval-in-buffer-window gnus-article-buffer
7946     (widen)
7947     (when (gnus-visual-p 'page-marker)
7948       (let ((buffer-read-only nil))
7949         (gnus-remove-text-with-property 'gnus-prev)
7950         (gnus-remove-text-with-property 'gnus-next))
7951       (setq gnus-page-broken nil))))
7952
7953 (defun gnus-summary-move-article (&optional n to-newsgroup
7954                                             select-method action)
7955   "Move the current article to a different newsgroup.
7956 If N is a positive number, move the N next articles.
7957 If N is a negative number, move the N previous articles.
7958 If N is nil and any articles have been marked with the process mark,
7959 move those articles instead.
7960 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7961 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7962 re-spool using this method.
7963
7964 For this function to work, both the current newsgroup and the
7965 newsgroup that you want to move to have to support the `request-move'
7966 and `request-accept' functions.
7967
7968 ACTION can be either `move' (the default), `crosspost' or `copy'."
7969   (interactive "P")
7970   (unless action
7971     (setq action 'move))
7972   ;; Disable marking as read.
7973   (let (gnus-mark-article-hook)
7974     (save-window-excursion
7975       (gnus-summary-select-article)))
7976   ;; Check whether the source group supports the required functions.
7977   (cond ((and (eq action 'move)
7978               (not (gnus-check-backend-function
7979                     'request-move-article gnus-newsgroup-name)))
7980          (error "The current group does not support article moving"))
7981         ((and (eq action 'crosspost)
7982               (not (gnus-check-backend-function
7983                     'request-replace-article gnus-newsgroup-name)))
7984          (error "The current group does not support article editing")))
7985   (let ((articles (gnus-summary-work-articles n))
7986         (prefix (if (gnus-check-backend-function
7987                      'request-move-article gnus-newsgroup-name)
7988                     (gnus-group-real-prefix gnus-newsgroup-name)
7989                   ""))
7990         (names '((move "Move" "Moving")
7991                  (copy "Copy" "Copying")
7992                  (crosspost "Crosspost" "Crossposting")))
7993         (copy-buf (save-excursion
7994                     (nnheader-set-temp-buffer " *copy article*")))
7995         (default-marks gnus-article-mark-lists)
7996         (no-expire-marks (delete '(expirable . expire)
7997                                  (copy-sequence gnus-article-mark-lists)))
7998         art-group to-method new-xref article to-groups)
7999     (unless (assq action names)
8000       (error "Unknown action %s" action))
8001     ;; Read the newsgroup name.
8002     (when (and (not to-newsgroup)
8003                (not select-method))
8004       (setq to-newsgroup
8005             (gnus-read-move-group-name
8006              (cadr (assq action names))
8007              (symbol-value (intern (format "gnus-current-%s-group" action)))
8008              articles prefix))
8009       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8010     (setq to-method (or select-method
8011                         (gnus-server-to-method
8012                          (gnus-group-method to-newsgroup))))
8013     ;; Check the method we are to move this article to...
8014     (unless (gnus-check-backend-function
8015              'request-accept-article (car to-method))
8016       (error "%s does not support article copying" (car to-method)))
8017     (unless (gnus-check-server to-method)
8018       (error "Can't open server %s" (car to-method)))
8019     (gnus-message 6 "%s to %s: %s..."
8020                   (caddr (assq action names))
8021                   (or (car select-method) to-newsgroup) articles)
8022     (while articles
8023       (setq article (pop articles))
8024       (setq
8025        art-group
8026        (cond
8027         ;; Move the article.
8028         ((eq action 'move)
8029          ;; Remove this article from future suppression.
8030          (gnus-dup-unsuppress-article article)
8031          (gnus-request-move-article
8032           article                       ; Article to move
8033           gnus-newsgroup-name           ; From newsgroup
8034           (nth 1 (gnus-find-method-for-group
8035                   gnus-newsgroup-name)) ; Server
8036           (list 'gnus-request-accept-article
8037                 to-newsgroup (list 'quote select-method)
8038                 (not articles) t)       ; Accept form
8039           (not articles)))              ; Only save nov last time
8040         ;; Copy the article.
8041         ((eq action 'copy)
8042          (save-excursion
8043            (set-buffer copy-buf)
8044            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8045              (gnus-request-accept-article
8046               to-newsgroup select-method (not articles) t))))
8047         ;; Crosspost the article.
8048         ((eq action 'crosspost)
8049          (let ((xref (message-tokenize-header
8050                       (mail-header-xref (gnus-summary-article-header article))
8051                       " ")))
8052            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8053                                   ":" article))
8054            (unless xref
8055              (setq xref (list (system-name))))
8056            (setq new-xref
8057                  (concat
8058                   (mapconcat 'identity
8059                              (delete "Xref:" (delete new-xref xref))
8060                              " ")
8061                   " " new-xref))
8062            (save-excursion
8063              (set-buffer copy-buf)
8064              ;; First put the article in the destination group.
8065              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8066              (when (consp (setq art-group
8067                                 (gnus-request-accept-article
8068                                  to-newsgroup select-method (not articles))))
8069                (setq new-xref (concat new-xref " " (car art-group)
8070                                       ":" (cdr art-group)))
8071                ;; Now we have the new Xrefs header, so we insert
8072                ;; it and replace the new article.
8073                (nnheader-replace-header "Xref" new-xref)
8074                (gnus-request-replace-article
8075                 (cdr art-group) to-newsgroup (current-buffer))
8076                art-group))))))
8077       (cond
8078        ((not art-group)
8079         (gnus-message 1 "Couldn't %s article %s: %s"
8080                       (cadr (assq action names)) article
8081                       (nnheader-get-report (car to-method))))
8082        ((eq art-group 'junk)
8083         (when (eq action 'move)
8084           (gnus-summary-mark-article article gnus-canceled-mark)
8085           (gnus-message 4 "Deleted article %s" article)))
8086        (t
8087         (let* ((pto-group (gnus-group-prefixed-name
8088                            (car art-group) to-method))
8089                (entry
8090                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8091                (info (nth 2 entry))
8092                (to-group (gnus-info-group info))
8093                to-marks)
8094           ;; Update the group that has been moved to.
8095           (when (and info
8096                      (memq action '(move copy)))
8097             (unless (member to-group to-groups)
8098               (push to-group to-groups))
8099
8100             (unless (memq article gnus-newsgroup-unreads)
8101               (push 'read to-marks)
8102               (gnus-info-set-read
8103                info (gnus-add-to-range (gnus-info-read info)
8104                                        (list (cdr art-group)))))
8105
8106             ;; See whether the article is to be put in the cache.
8107             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8108                              default-marks
8109                            no-expire-marks))
8110                   (to-article (cdr art-group)))
8111
8112               ;; Enter the article into the cache in the new group,
8113               ;; if that is required.
8114               (when gnus-use-cache
8115                 (gnus-cache-possibly-enter-article
8116                  to-group to-article
8117                  (let ((header (copy-sequence
8118                                 (gnus-summary-article-header article))))
8119                    (mail-header-set-number header to-article)
8120                    header)
8121                  (memq article gnus-newsgroup-marked)
8122                  (memq article gnus-newsgroup-dormant)
8123                  (memq article gnus-newsgroup-unreads)))
8124
8125               (when gnus-preserve-marks
8126                 ;; Copy any marks over to the new group.
8127                 (when (and (equal to-group gnus-newsgroup-name)
8128                            (not (memq article gnus-newsgroup-unreads)))
8129                   ;; Mark this article as read in this group.
8130                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8131                   (setcdr (gnus-active to-group) to-article)
8132                   (setcdr gnus-newsgroup-active to-article))
8133
8134                 (while marks
8135                   (when (memq article (symbol-value
8136                                        (intern (format "gnus-newsgroup-%s"
8137                                                        (caar marks)))))
8138                     (push (cdar marks) to-marks)
8139                     ;; If the other group is the same as this group,
8140                     ;; then we have to add the mark to the list.
8141                     (when (equal to-group gnus-newsgroup-name)
8142                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8143                            (cons to-article
8144                                  (symbol-value
8145                                   (intern (format "gnus-newsgroup-%s"
8146                                                   (caar marks)))))))
8147                     ;; Copy the marks to other group.
8148                     (gnus-add-marked-articles
8149                      to-group (cdar marks) (list to-article) info))
8150                   (setq marks (cdr marks)))
8151
8152                 (gnus-request-set-mark to-group (list (list (list to-article)
8153                                                             'set
8154                                                             to-marks))))
8155
8156               (gnus-dribble-enter
8157                (concat "(gnus-group-set-info '"
8158                        (gnus-prin1-to-string (gnus-get-info to-group))
8159                        ")"))))
8160
8161           ;; Update the Xref header in this article to point to
8162           ;; the new crossposted article we have just created.
8163           (when (eq action 'crosspost)
8164             (save-excursion
8165               (set-buffer copy-buf)
8166               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8167               (nnheader-replace-header "Xref" new-xref)
8168               (gnus-request-replace-article
8169                article gnus-newsgroup-name (current-buffer)))))
8170
8171         ;;;!!!Why is this necessary?
8172         (set-buffer gnus-summary-buffer)
8173
8174         (gnus-summary-goto-subject article)
8175         (when (eq action 'move)
8176           (gnus-summary-mark-article article gnus-canceled-mark))))
8177       (gnus-summary-remove-process-mark article))
8178     ;; Re-activate all groups that have been moved to.
8179     (while to-groups
8180       (save-excursion
8181         (set-buffer gnus-group-buffer)
8182         (when (gnus-group-goto-group (car to-groups) t)
8183           (gnus-group-get-new-news-this-group 1 t))
8184         (pop to-groups)))
8185
8186     (gnus-kill-buffer copy-buf)
8187     (gnus-summary-position-point)
8188     (gnus-set-mode-line 'summary)))
8189
8190 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8191   "Move the current article to a different newsgroup.
8192 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8193 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8194 re-spool using this method."
8195   (interactive "P")
8196   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8197
8198 (defun gnus-summary-crosspost-article (&optional n)
8199   "Crosspost the current article to some other group."
8200   (interactive "P")
8201   (gnus-summary-move-article n nil nil 'crosspost))
8202
8203 (defcustom gnus-summary-respool-default-method nil
8204   "Default method for respooling an article.
8205 If nil, use to the current newsgroup method."
8206   :type '(choice (gnus-select-method :value (nnml ""))
8207                  (const nil))
8208   :group 'gnus-summary-mail)
8209
8210 (defun gnus-summary-respool-article (&optional n method)
8211   "Respool the current article.
8212 The article will be squeezed through the mail spooling process again,
8213 which means that it will be put in some mail newsgroup or other
8214 depending on `nnmail-split-methods'.
8215 If N is a positive number, respool the N next articles.
8216 If N is a negative number, respool the N previous articles.
8217 If N is nil and any articles have been marked with the process mark,
8218 respool those articles instead.
8219
8220 Respooling can be done both from mail groups and \"real\" newsgroups.
8221 In the former case, the articles in question will be moved from the
8222 current group into whatever groups they are destined to.  In the
8223 latter case, they will be copied into the relevant groups."
8224   (interactive
8225    (list current-prefix-arg
8226          (let* ((methods (gnus-methods-using 'respool))
8227                 (methname
8228                  (symbol-name (or gnus-summary-respool-default-method
8229                                   (car (gnus-find-method-for-group
8230                                         gnus-newsgroup-name)))))
8231                 (method
8232                  (gnus-completing-read
8233                   methname "What backend do you want to use when respooling?"
8234                   methods nil t nil 'gnus-mail-method-history))
8235                 ms)
8236            (cond
8237             ((zerop (length (setq ms (gnus-servers-using-backend
8238                                       (intern method)))))
8239              (list (intern method) ""))
8240             ((= 1 (length ms))
8241              (car ms))
8242             (t
8243              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8244                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8245                            ms-alist))))))))
8246   (unless method
8247     (error "No method given for respooling"))
8248   (if (assoc (symbol-name
8249               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8250              (gnus-methods-using 'respool))
8251       (gnus-summary-move-article n nil method)
8252     (gnus-summary-copy-article n nil method)))
8253
8254 (defun gnus-summary-import-article (file &optional edit)
8255   "Import an arbitrary file into a mail newsgroup."
8256   (interactive "fImport file: \nP")
8257   (let ((group gnus-newsgroup-name)
8258         (now (current-time))
8259         atts lines group-art)
8260     (unless (gnus-check-backend-function 'request-accept-article group)
8261       (error "%s does not support article importing" group))
8262     (or (file-readable-p file)
8263         (not (file-regular-p file))
8264         (error "Can't read %s" file))
8265     (save-excursion
8266       (set-buffer (gnus-get-buffer-create " *import file*"))
8267       (erase-buffer)
8268       (nnheader-insert-file-contents file)
8269       (goto-char (point-min))
8270       (if (nnheader-article-p)
8271           (save-restriction
8272             (goto-char (point-min))
8273             (search-forward "\n\n" nil t)
8274             (narrow-to-region (point-min) (1- (point)))
8275             (goto-char (point-min))
8276             (unless (re-search-forward "^date:" nil t)
8277               (goto-char (point-max))
8278               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8279         ;; This doesn't look like an article, so we fudge some headers.
8280         (setq atts (file-attributes file)
8281               lines (count-lines (point-min) (point-max)))
8282         (insert "From: " (read-string "From: ") "\n"
8283                 "Subject: " (read-string "Subject: ") "\n"
8284                 "Date: " (message-make-date (nth 5 atts)) "\n"
8285                 "Message-ID: " (message-make-message-id) "\n"
8286                 "Lines: " (int-to-string lines) "\n"
8287                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8288       (setq group-art (gnus-request-accept-article group nil t))
8289       (kill-buffer (current-buffer)))
8290     (setq gnus-newsgroup-active (gnus-activate-group group))
8291     (forward-line 1)
8292     (gnus-summary-goto-article (cdr group-art) nil t)
8293     (when edit
8294       (gnus-summary-edit-article))))
8295
8296 (defun gnus-summary-create-article ()
8297   "Create an article in a mail newsgroup."
8298   (interactive)
8299   (let ((group gnus-newsgroup-name)
8300         (now (current-time))
8301         group-art)
8302     (unless (gnus-check-backend-function 'request-accept-article group)
8303       (error "%s does not support article importing" group))
8304     (save-excursion
8305       (set-buffer (gnus-get-buffer-create " *import file*"))
8306       (erase-buffer)
8307       (goto-char (point-min))
8308       ;; This doesn't look like an article, so we fudge some headers.
8309       (insert "From: " (read-string "From: ") "\n"
8310               "Subject: " (read-string "Subject: ") "\n"
8311               "Date: " (message-make-date now) "\n"
8312               "Message-ID: " (message-make-message-id) "\n")
8313       (setq group-art (gnus-request-accept-article group nil t))
8314       (kill-buffer (current-buffer)))
8315     (setq gnus-newsgroup-active (gnus-activate-group group))
8316     (forward-line 1)
8317     (gnus-summary-goto-article (cdr group-art) nil t)
8318     (gnus-summary-edit-article)))
8319
8320 (defun gnus-summary-article-posted-p ()
8321   "Say whether the current (mail) article is available from news as well.
8322 This will be the case if the article has both been mailed and posted."
8323   (interactive)
8324   (let ((id (mail-header-references (gnus-summary-article-header)))
8325         (gnus-override-method (car (gnus-refer-article-methods))))
8326     (if (gnus-request-head id "")
8327         (gnus-message 2 "The current message was found on %s"
8328                       gnus-override-method)
8329       (gnus-message 2 "The current message couldn't be found on %s"
8330                     gnus-override-method)
8331       nil)))
8332
8333 (defun gnus-summary-expire-articles (&optional now)
8334   "Expire all articles that are marked as expirable in the current group."
8335   (interactive)
8336   (when (gnus-check-backend-function
8337          'request-expire-articles gnus-newsgroup-name)
8338     ;; This backend supports expiry.
8339     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8340            (expirable (if total
8341                           (progn
8342                             ;; We need to update the info for
8343                             ;; this group for `gnus-list-of-read-articles'
8344                             ;; to give us the right answer.
8345                             (gnus-run-hooks 'gnus-exit-group-hook)
8346                             (gnus-summary-update-info)
8347                             (gnus-list-of-read-articles gnus-newsgroup-name))
8348                         (setq gnus-newsgroup-expirable
8349                               (sort gnus-newsgroup-expirable '<))))
8350            (expiry-wait (if now 'immediate
8351                           (gnus-group-find-parameter
8352                            gnus-newsgroup-name 'expiry-wait)))
8353            (nnmail-expiry-target
8354             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8355                 nnmail-expiry-target))
8356            es)
8357       (when expirable
8358         ;; There are expirable articles in this group, so we run them
8359         ;; through the expiry process.
8360         (gnus-message 6 "Expiring articles...")
8361         (unless (gnus-check-group gnus-newsgroup-name)
8362           (error "Can't open server for %s" gnus-newsgroup-name))
8363         ;; The list of articles that weren't expired is returned.
8364         (save-excursion
8365           (if expiry-wait
8366               (let ((nnmail-expiry-wait-function nil)
8367                     (nnmail-expiry-wait expiry-wait))
8368                 (setq es (gnus-request-expire-articles
8369                           expirable gnus-newsgroup-name)))
8370             (setq es (gnus-request-expire-articles
8371                       expirable gnus-newsgroup-name)))
8372           (unless total
8373             (setq gnus-newsgroup-expirable es))
8374           ;; We go through the old list of expirable, and mark all
8375           ;; really expired articles as nonexistent.
8376           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8377             (let ((gnus-use-cache nil))
8378               (while expirable
8379                 (unless (memq (car expirable) es)
8380                   (when (gnus-data-find (car expirable))
8381                     (gnus-summary-mark-article
8382                      (car expirable) gnus-canceled-mark)))
8383                 (setq expirable (cdr expirable))))))
8384         (gnus-message 6 "Expiring articles...done")))))
8385
8386 (defun gnus-summary-expire-articles-now ()
8387   "Expunge all expirable articles in the current group.
8388 This means that *all* articles that are marked as expirable will be
8389 deleted forever, right now."
8390   (interactive)
8391   (or gnus-expert-user
8392       (gnus-yes-or-no-p
8393        "Are you really, really, really sure you want to delete all these messages? ")
8394       (error "Phew!"))
8395   (gnus-summary-expire-articles t))
8396
8397 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8398 (defun gnus-summary-delete-article (&optional n)
8399   "Delete the N next (mail) articles.
8400 This command actually deletes articles.  This is not a marking
8401 command.  The article will disappear forever from your life, never to
8402 return.
8403 If N is negative, delete backwards.
8404 If N is nil and articles have been marked with the process mark,
8405 delete these instead."
8406   (interactive "P")
8407   (unless (gnus-check-backend-function 'request-expire-articles
8408                                        gnus-newsgroup-name)
8409     (error "The current newsgroup does not support article deletion"))
8410   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8411     (error "Couldn't open server"))
8412   ;; Compute the list of articles to delete.
8413   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8414         not-deleted)
8415     (if (and gnus-novice-user
8416              (not (gnus-yes-or-no-p
8417                    (format "Do you really want to delete %s forever? "
8418                            (if (> (length articles) 1)
8419                                (format "these %s articles" (length articles))
8420                              "this article")))))
8421         ()
8422       ;; Delete the articles.
8423       (setq not-deleted (gnus-request-expire-articles
8424                          articles gnus-newsgroup-name 'force))
8425       (while articles
8426         (gnus-summary-remove-process-mark (car articles))
8427         ;; The backend might not have been able to delete the article
8428         ;; after all.
8429         (unless (memq (car articles) not-deleted)
8430           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8431         (setq articles (cdr articles)))
8432       (when not-deleted
8433         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8434     (gnus-summary-position-point)
8435     (gnus-set-mode-line 'summary)
8436     not-deleted))
8437
8438 (defun gnus-summary-edit-article (&optional force)
8439   "Edit the current article.
8440 This will have permanent effect only in mail groups.
8441 If FORCE is non-nil, allow editing of articles even in read-only
8442 groups."
8443   (interactive "P")
8444   (save-excursion
8445     (set-buffer gnus-summary-buffer)
8446     (let ((mail-parse-charset gnus-newsgroup-charset)
8447           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8448       (gnus-set-global-variables)
8449       (when (and (not force)
8450                  (gnus-group-read-only-p))
8451         (error "The current newsgroup does not support article editing"))
8452       (gnus-summary-show-article t)
8453       (gnus-article-edit-article
8454        'ignore
8455        `(lambda (no-highlight)
8456           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8457                 (message-options message-options)
8458                 (message-options-set-recipient)
8459                 (mail-parse-ignored-charsets
8460                  ',gnus-newsgroup-ignored-charsets))
8461             (gnus-summary-edit-article-done
8462              ,(or (mail-header-references gnus-current-headers) "")
8463              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8464
8465 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8466
8467 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8468                                                  no-highlight)
8469   "Make edits to the current article permanent."
8470   (interactive)
8471   (save-excursion
8472     ;; The buffer restriction contains the entire article if it exists.
8473     (when (article-goto-body)
8474       (let ((lines (count-lines (point) (point-max)))
8475             (length (- (point-max) (point)))
8476             (case-fold-search t)
8477             (body (copy-marker (point))))
8478         (goto-char (point-min))
8479         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8480           (delete-region (match-beginning 1) (match-end 1))
8481           (insert (number-to-string length)))
8482         (goto-char (point-min))
8483         (when (re-search-forward
8484                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8485           (delete-region (match-beginning 1) (match-end 1))
8486           (insert (number-to-string length)))
8487         (goto-char (point-min))
8488         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8489           (delete-region (match-beginning 1) (match-end 1))
8490           (insert (number-to-string lines))))))
8491   ;; Replace the article.
8492   (let ((buf (current-buffer)))
8493     (with-temp-buffer
8494       (insert-buffer-substring buf)
8495
8496       (if (and (not read-only)
8497                (not (gnus-request-replace-article
8498                      (cdr gnus-article-current) (car gnus-article-current)
8499                      (current-buffer) t)))
8500           (error "Couldn't replace article")
8501         ;; Update the summary buffer.
8502         (if (and references
8503                  (equal (message-tokenize-header references " ")
8504                         (message-tokenize-header
8505                          (or (message-fetch-field "references") "") " ")))
8506             ;; We only have to update this line.
8507             (save-excursion
8508               (save-restriction
8509                 (message-narrow-to-head)
8510                 (let ((head (buffer-string))
8511                       header)
8512                   (with-temp-buffer
8513                     (insert (format "211 %d Article retrieved.\n"
8514                                     (cdr gnus-article-current)))
8515                     (insert head)
8516                     (insert ".\n")
8517                     (let ((nntp-server-buffer (current-buffer)))
8518                       (setq header (car (gnus-get-newsgroup-headers
8519                                          (save-excursion
8520                                            (set-buffer gnus-summary-buffer)
8521                                            gnus-newsgroup-dependencies)
8522                                          t))))
8523                     (save-excursion
8524                       (set-buffer gnus-summary-buffer)
8525                       (gnus-data-set-header
8526                        (gnus-data-find (cdr gnus-article-current))
8527                        header)
8528                       (gnus-summary-update-article-line
8529                        (cdr gnus-article-current) header))))))
8530           ;; Update threads.
8531           (set-buffer (or buffer gnus-summary-buffer))
8532           (gnus-summary-update-article (cdr gnus-article-current)))
8533         ;; Prettify the article buffer again.
8534         (unless no-highlight
8535           (save-excursion
8536             (set-buffer gnus-article-buffer)
8537             ;;;!!! Fix this -- article should be rehighlighted.
8538             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8539             (set-buffer gnus-original-article-buffer)
8540             (gnus-request-article
8541              (cdr gnus-article-current)
8542              (car gnus-article-current) (current-buffer))))
8543         ;; Prettify the summary buffer line.
8544         (when (gnus-visual-p 'summary-highlight 'highlight)
8545           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8546
8547 (defun gnus-summary-edit-wash (key)
8548   "Perform editing command KEY in the article buffer."
8549   (interactive
8550    (list
8551     (progn
8552       (message "%s" (concat (this-command-keys) "- "))
8553       (read-char))))
8554   (message "")
8555   (gnus-summary-edit-article)
8556   (execute-kbd-macro (concat (this-command-keys) key))
8557   (gnus-article-edit-done))
8558
8559 ;;; Respooling
8560
8561 (defun gnus-summary-respool-query (&optional silent trace)
8562   "Query where the respool algorithm would put this article."
8563   (interactive)
8564   (let (gnus-mark-article-hook)
8565     (gnus-summary-select-article)
8566     (save-excursion
8567       (set-buffer gnus-original-article-buffer)
8568       (save-restriction
8569         (message-narrow-to-head)
8570         (let ((groups (nnmail-article-group 'identity trace)))
8571           (unless silent
8572             (if groups
8573                 (message "This message would go to %s"
8574                          (mapconcat 'car groups ", "))
8575               (message "This message would go to no groups"))
8576             groups))))))
8577
8578 (defun gnus-summary-respool-trace ()
8579   "Trace where the respool algorithm would put this article.
8580 Display a buffer showing all fancy splitting patterns which matched."
8581   (interactive)
8582   (gnus-summary-respool-query nil t))
8583
8584 ;; Summary marking commands.
8585
8586 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8587   "Mark articles which has the same subject as read, and then select the next.
8588 If UNMARK is positive, remove any kind of mark.
8589 If UNMARK is negative, tick articles."
8590   (interactive "P")
8591   (when unmark
8592     (setq unmark (prefix-numeric-value unmark)))
8593   (let ((count
8594          (gnus-summary-mark-same-subject
8595           (gnus-summary-article-subject) unmark)))
8596     ;; Select next unread article.  If auto-select-same mode, should
8597     ;; select the first unread article.
8598     (gnus-summary-next-article t (and gnus-auto-select-same
8599                                       (gnus-summary-article-subject)))
8600     (gnus-message 7 "%d article%s marked as %s"
8601                   count (if (= count 1) " is" "s are")
8602                   (if unmark "unread" "read"))))
8603
8604 (defun gnus-summary-kill-same-subject (&optional unmark)
8605   "Mark articles which has the same subject as read.
8606 If UNMARK is positive, remove any kind of mark.
8607 If UNMARK is negative, tick articles."
8608   (interactive "P")
8609   (when unmark
8610     (setq unmark (prefix-numeric-value unmark)))
8611   (let ((count
8612          (gnus-summary-mark-same-subject
8613           (gnus-summary-article-subject) unmark)))
8614     ;; If marked as read, go to next unread subject.
8615     (when (null unmark)
8616       ;; Go to next unread subject.
8617       (gnus-summary-next-subject 1 t))
8618     (gnus-message 7 "%d articles are marked as %s"
8619                   count (if unmark "unread" "read"))))
8620
8621 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8622   "Mark articles with same SUBJECT as read, and return marked number.
8623 If optional argument UNMARK is positive, remove any kinds of marks.
8624 If optional argument UNMARK is negative, mark articles as unread instead."
8625   (let ((count 1))
8626     (save-excursion
8627       (cond
8628        ((null unmark)                   ; Mark as read.
8629         (while (and
8630                 (progn
8631                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8632                   (gnus-summary-show-thread) t)
8633                 (gnus-summary-find-subject subject))
8634           (setq count (1+ count))))
8635        ((> unmark 0)                    ; Tick.
8636         (while (and
8637                 (progn
8638                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8639                   (gnus-summary-show-thread) t)
8640                 (gnus-summary-find-subject subject))
8641           (setq count (1+ count))))
8642        (t                               ; Mark as unread.
8643         (while (and
8644                 (progn
8645                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8646                   (gnus-summary-show-thread) t)
8647                 (gnus-summary-find-subject subject))
8648           (setq count (1+ count)))))
8649       (gnus-set-mode-line 'summary)
8650       ;; Return the number of marked articles.
8651       count)))
8652
8653 (defun gnus-summary-mark-as-processable (n &optional unmark)
8654   "Set the process mark on the next N articles.
8655 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8656 the process mark instead.  The difference between N and the actual
8657 number of articles marked is returned."
8658   (interactive "P")
8659   (if (and (null n) (gnus-region-active-p))
8660       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8661     (setq n (prefix-numeric-value n))
8662     (let ((backward (< n 0))
8663           (n (abs n)))
8664       (while (and
8665               (> n 0)
8666               (if unmark
8667                   (gnus-summary-remove-process-mark
8668                    (gnus-summary-article-number))
8669                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8670               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8671         (setq n (1- n)))
8672       (when (/= 0 n)
8673         (gnus-message 7 "No more articles"))
8674       (gnus-summary-recenter)
8675       (gnus-summary-position-point)
8676       n)))
8677
8678 (defun gnus-summary-unmark-as-processable (n)
8679   "Remove the process mark from the next N articles.
8680 If N is negative, unmark backward instead.  The difference between N and
8681 the actual number of articles unmarked is returned."
8682   (interactive "P")
8683   (gnus-summary-mark-as-processable n t))
8684
8685 (defun gnus-summary-unmark-all-processable ()
8686   "Remove the process mark from all articles."
8687   (interactive)
8688   (save-excursion
8689     (while gnus-newsgroup-processable
8690       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8691   (gnus-summary-position-point))
8692
8693 (defun gnus-summary-add-mark (article type)
8694   "Mark ARTICLE with a mark of TYPE."
8695   (let ((vtype (car (assq type gnus-article-mark-lists)))
8696         var)
8697     (if (not vtype)
8698         (error "No such mark type: %s" type)
8699       (setq var (intern (format "gnus-newsgroup-%s" type)))
8700       (set var (cons article (symbol-value var)))
8701       (if (memq type '(processable cached replied forwarded saved))
8702           (gnus-summary-update-secondary-mark article)
8703         ;;; !!! This is bobus.  We should find out what primary
8704         ;;; !!! mark we want to set.
8705         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8706
8707 (defun gnus-summary-mark-as-expirable (n)
8708   "Mark N articles forward as expirable.
8709 If N is negative, mark backward instead.  The difference between N and
8710 the actual number of articles marked is returned."
8711   (interactive "p")
8712   (gnus-summary-mark-forward n gnus-expirable-mark))
8713
8714 (defun gnus-summary-mark-article-as-replied (article)
8715   "Mark ARTICLE as replied to and update the summary line.
8716 ARTICLE can also be a list of articles."
8717   (interactive (list (gnus-summary-article-number)))
8718   (let ((articles (if (listp article) article (list article))))
8719     (dolist (article articles)
8720       (push article gnus-newsgroup-replied)
8721       (let ((buffer-read-only nil))
8722         (when (gnus-summary-goto-subject article nil t)
8723           (gnus-summary-update-secondary-mark article))))))
8724
8725 (defun gnus-summary-mark-article-as-forwarded (article)
8726   "Mark ARTICLE as forwarded and update the summary line.
8727 ARTICLE can also be a list of articles."
8728   (let ((articles (if (listp article) article (list article))))
8729     (dolist (article articles)
8730       (push article gnus-newsgroup-forwarded)
8731       (let ((buffer-read-only nil))
8732         (when (gnus-summary-goto-subject article nil t)
8733           (gnus-summary-update-secondary-mark article))))))
8734
8735 (defun gnus-summary-set-bookmark (article)
8736   "Set a bookmark in current article."
8737   (interactive (list (gnus-summary-article-number)))
8738   (when (or (not (get-buffer gnus-article-buffer))
8739             (not gnus-current-article)
8740             (not gnus-article-current)
8741             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8742     (error "No current article selected"))
8743   ;; Remove old bookmark, if one exists.
8744   (let ((old (assq article gnus-newsgroup-bookmarks)))
8745     (when old
8746       (setq gnus-newsgroup-bookmarks
8747             (delq old gnus-newsgroup-bookmarks))))
8748   ;; Set the new bookmark, which is on the form
8749   ;; (article-number . line-number-in-body).
8750   (push
8751    (cons article
8752          (save-excursion
8753            (set-buffer gnus-article-buffer)
8754            (count-lines
8755             (min (point)
8756                  (save-excursion
8757                    (goto-char (point-min))
8758                    (search-forward "\n\n" nil t)
8759                    (point)))
8760             (point))))
8761    gnus-newsgroup-bookmarks)
8762   (gnus-message 6 "A bookmark has been added to the current article."))
8763
8764 (defun gnus-summary-remove-bookmark (article)
8765   "Remove the bookmark from the current article."
8766   (interactive (list (gnus-summary-article-number)))
8767   ;; Remove old bookmark, if one exists.
8768   (let ((old (assq article gnus-newsgroup-bookmarks)))
8769     (if old
8770         (progn
8771           (setq gnus-newsgroup-bookmarks
8772                 (delq old gnus-newsgroup-bookmarks))
8773           (gnus-message 6 "Removed bookmark."))
8774       (gnus-message 6 "No bookmark in current article."))))
8775
8776 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8777 (defun gnus-summary-mark-as-dormant (n)
8778   "Mark N articles forward as dormant.
8779 If N is negative, mark backward instead.  The difference between N and
8780 the actual number of articles marked is returned."
8781   (interactive "p")
8782   (gnus-summary-mark-forward n gnus-dormant-mark))
8783
8784 (defun gnus-summary-set-process-mark (article)
8785   "Set the process mark on ARTICLE and update the summary line."
8786   (setq gnus-newsgroup-processable
8787         (cons article
8788               (delq article gnus-newsgroup-processable)))
8789   (when (gnus-summary-goto-subject article)
8790     (gnus-summary-show-thread)
8791     (gnus-summary-goto-subject article)
8792     (gnus-summary-update-secondary-mark article)))
8793
8794 (defun gnus-summary-remove-process-mark (article)
8795   "Remove the process mark from ARTICLE and update the summary line."
8796   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8797   (when (gnus-summary-goto-subject article)
8798     (gnus-summary-show-thread)
8799     (gnus-summary-goto-subject article)
8800     (gnus-summary-update-secondary-mark article)))
8801
8802 (defun gnus-summary-set-saved-mark (article)
8803   "Set the process mark on ARTICLE and update the summary line."
8804   (push article gnus-newsgroup-saved)
8805   (when (gnus-summary-goto-subject article)
8806     (gnus-summary-update-secondary-mark article)))
8807
8808 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8809   "Mark N articles as read forwards.
8810 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8811 The difference between N and the actual number of articles marked is
8812 returned.
8813 Iff NO-EXPIRE, auto-expiry will be inhibited."
8814   (interactive "p")
8815   (gnus-summary-show-thread)
8816   (let ((backward (< n 0))
8817         (gnus-summary-goto-unread
8818          (and gnus-summary-goto-unread
8819               (not (eq gnus-summary-goto-unread 'never))
8820               (not (memq mark (list gnus-unread-mark
8821                                     gnus-ticked-mark gnus-dormant-mark)))))
8822         (n (abs n))
8823         (mark (or mark gnus-del-mark)))
8824     (while (and (> n 0)
8825                 (gnus-summary-mark-article nil mark no-expire)
8826                 (zerop (gnus-summary-next-subject
8827                         (if backward -1 1)
8828                         (and gnus-summary-goto-unread
8829                              (not (eq gnus-summary-goto-unread 'never)))
8830                         t)))
8831       (setq n (1- n)))
8832     (when (/= 0 n)
8833       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8834     (gnus-summary-recenter)
8835     (gnus-summary-position-point)
8836     (gnus-set-mode-line 'summary)
8837     n))
8838
8839 (defun gnus-summary-mark-article-as-read (mark)
8840   "Mark the current article quickly as read with MARK."
8841   (let ((article (gnus-summary-article-number)))
8842     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8843     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8844     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8845     (push (cons article mark) gnus-newsgroup-reads)
8846     ;; Possibly remove from cache, if that is used.
8847     (when gnus-use-cache
8848       (gnus-cache-enter-remove-article article))
8849     ;; Allow the backend to change the mark.
8850     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8851     ;; Check for auto-expiry.
8852     (when (and gnus-newsgroup-auto-expire
8853                (memq mark gnus-auto-expirable-marks))
8854       (setq mark gnus-expirable-mark)
8855       ;; Let the backend know about the mark change.
8856       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8857       (push article gnus-newsgroup-expirable))
8858     ;; Set the mark in the buffer.
8859     (gnus-summary-update-mark mark 'unread)
8860     t))
8861
8862 (defun gnus-summary-mark-article-as-unread (mark)
8863   "Mark the current article quickly as unread with MARK."
8864   (let* ((article (gnus-summary-article-number))
8865          (old-mark (gnus-summary-article-mark article)))
8866     ;; Allow the backend to change the mark.
8867     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8868     (if (eq mark old-mark)
8869         t
8870       (if (<= article 0)
8871           (progn
8872             (gnus-error 1 "Can't mark negative article numbers")
8873             nil)
8874         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8875         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8876         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8877         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8878         (cond ((= mark gnus-ticked-mark)
8879                (push article gnus-newsgroup-marked))
8880               ((= mark gnus-dormant-mark)
8881                (push article gnus-newsgroup-dormant))
8882               (t
8883                (push article gnus-newsgroup-unreads)))
8884         (gnus-pull article gnus-newsgroup-reads)
8885
8886         ;; See whether the article is to be put in the cache.
8887         (and gnus-use-cache
8888              (vectorp (gnus-summary-article-header article))
8889              (save-excursion
8890                (gnus-cache-possibly-enter-article
8891                 gnus-newsgroup-name article
8892                 (gnus-summary-article-header article)
8893                 (= mark gnus-ticked-mark)
8894                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8895
8896         ;; Fix the mark.
8897         (gnus-summary-update-mark mark 'unread)
8898         t))))
8899
8900 (defun gnus-summary-mark-article (&optional article mark no-expire)
8901   "Mark ARTICLE with MARK.  MARK can be any character.
8902 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8903 `??' (dormant) and `?E' (expirable).
8904 If MARK is nil, then the default character `?r' is used.
8905 If ARTICLE is nil, then the article on the current line will be
8906 marked.
8907 Iff NO-EXPIRE, auto-expiry will be inhibited."
8908   ;; The mark might be a string.
8909   (when (stringp mark)
8910     (setq mark (aref mark 0)))
8911   ;; If no mark is given, then we check auto-expiring.
8912   (when (null mark)
8913     (setq mark gnus-del-mark))
8914   (when (and (not no-expire)
8915              gnus-newsgroup-auto-expire
8916              (memq mark gnus-auto-expirable-marks))
8917     (setq mark gnus-expirable-mark))
8918   (let ((article (or article (gnus-summary-article-number)))
8919         (old-mark (gnus-summary-article-mark article)))
8920     ;; Allow the backend to change the mark.
8921     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8922     (if (eq mark old-mark)
8923         t
8924       (unless article
8925         (error "No article on current line"))
8926       (if (not (if (or (= mark gnus-unread-mark)
8927                        (= mark gnus-ticked-mark)
8928                        (= mark gnus-dormant-mark))
8929                    (gnus-mark-article-as-unread article mark)
8930                  (gnus-mark-article-as-read article mark)))
8931           t
8932         ;; See whether the article is to be put in the cache.
8933         (and gnus-use-cache
8934              (not (= mark gnus-canceled-mark))
8935              (vectorp (gnus-summary-article-header article))
8936              (save-excursion
8937                (gnus-cache-possibly-enter-article
8938                 gnus-newsgroup-name article
8939                 (gnus-summary-article-header article)
8940                 (= mark gnus-ticked-mark)
8941                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8942
8943         (when (gnus-summary-goto-subject article nil t)
8944           (let ((buffer-read-only nil))
8945             (gnus-summary-show-thread)
8946             ;; Fix the mark.
8947             (gnus-summary-update-mark mark 'unread)
8948             t))))))
8949
8950 (defun gnus-summary-update-secondary-mark (article)
8951   "Update the secondary (read, process, cache) mark."
8952   (gnus-summary-update-mark
8953    (cond ((memq article gnus-newsgroup-processable)
8954           gnus-process-mark)
8955          ((memq article gnus-newsgroup-cached)
8956           gnus-cached-mark)
8957          ((memq article gnus-newsgroup-replied)
8958           gnus-replied-mark)
8959          ((memq article gnus-newsgroup-forwarded)
8960           gnus-forwarded-mark)
8961          ((memq article gnus-newsgroup-saved)
8962           gnus-saved-mark)
8963          (t gnus-no-mark))
8964    'replied)
8965   (when (gnus-visual-p 'summary-highlight 'highlight)
8966     (gnus-run-hooks 'gnus-summary-update-hook))
8967   t)
8968
8969 (defun gnus-summary-update-mark (mark type)
8970   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8971         (buffer-read-only nil))
8972     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8973     (when forward
8974       (when (looking-at "\r")
8975         (incf forward))
8976       (when (<= (+ forward (point)) (point-max))
8977         ;; Go to the right position on the line.
8978         (goto-char (+ forward (point)))
8979         ;; Replace the old mark with the new mark.
8980         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8981         ;; Optionally update the marks by some user rule.
8982         (when (eq type 'unread)
8983           (gnus-data-set-mark
8984            (gnus-data-find (gnus-summary-article-number)) mark)
8985           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8986
8987 (defun gnus-mark-article-as-read (article &optional mark)
8988   "Enter ARTICLE in the pertinent lists and remove it from others."
8989   ;; Make the article expirable.
8990   (let ((mark (or mark gnus-del-mark)))
8991     (if (= mark gnus-expirable-mark)
8992         (push article gnus-newsgroup-expirable)
8993       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8994     ;; Remove from unread and marked lists.
8995     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8996     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8997     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8998     (push (cons article mark) gnus-newsgroup-reads)
8999     ;; Possibly remove from cache, if that is used.
9000     (when gnus-use-cache
9001       (gnus-cache-enter-remove-article article))
9002     t))
9003
9004 (defun gnus-mark-article-as-unread (article &optional mark)
9005   "Enter ARTICLE in the pertinent lists and remove it from others."
9006   (let ((mark (or mark gnus-ticked-mark)))
9007     (if (<= article 0)
9008         (progn
9009           (gnus-error 1 "Can't mark negative article numbers")
9010           nil)
9011       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9012             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9013             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9014             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9015
9016       ;; Unsuppress duplicates?
9017       (when gnus-suppress-duplicates
9018         (gnus-dup-unsuppress-article article))
9019
9020       (cond ((= mark gnus-ticked-mark)
9021              (push article gnus-newsgroup-marked))
9022             ((= mark gnus-dormant-mark)
9023              (push article gnus-newsgroup-dormant))
9024             (t
9025              (push article gnus-newsgroup-unreads)))
9026       (gnus-pull article gnus-newsgroup-reads)
9027       t)))
9028
9029 (defalias 'gnus-summary-mark-as-unread-forward
9030   'gnus-summary-tick-article-forward)
9031 (make-obsolete 'gnus-summary-mark-as-unread-forward
9032                'gnus-summary-tick-article-forward)
9033 (defun gnus-summary-tick-article-forward (n)
9034   "Tick N articles forwards.
9035 If N is negative, tick backwards instead.
9036 The difference between N and the number of articles ticked is returned."
9037   (interactive "p")
9038   (gnus-summary-mark-forward n gnus-ticked-mark))
9039
9040 (defalias 'gnus-summary-mark-as-unread-backward
9041   'gnus-summary-tick-article-backward)
9042 (make-obsolete 'gnus-summary-mark-as-unread-backward
9043                'gnus-summary-tick-article-backward)
9044 (defun gnus-summary-tick-article-backward (n)
9045   "Tick N articles backwards.
9046 The difference between N and the number of articles ticked is returned."
9047   (interactive "p")
9048   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9049
9050 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9051 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9052 (defun gnus-summary-tick-article (&optional article clear-mark)
9053   "Mark current article as unread.
9054 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9055 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9056   (interactive)
9057   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9058                                        gnus-ticked-mark)))
9059
9060 (defun gnus-summary-mark-as-read-forward (n)
9061   "Mark N articles as read forwards.
9062 If N is negative, mark backwards instead.
9063 The difference between N and the actual number of articles marked is
9064 returned."
9065   (interactive "p")
9066   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9067
9068 (defun gnus-summary-mark-as-read-backward (n)
9069   "Mark the N articles as read backwards.
9070 The difference between N and the actual number of articles marked is
9071 returned."
9072   (interactive "p")
9073   (gnus-summary-mark-forward
9074    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9075
9076 (defun gnus-summary-mark-as-read (&optional article mark)
9077   "Mark current article as read.
9078 ARTICLE specifies the article to be marked as read.
9079 MARK specifies a string to be inserted at the beginning of the line."
9080   (gnus-summary-mark-article article mark))
9081
9082 (defun gnus-summary-clear-mark-forward (n)
9083   "Clear marks from N articles forward.
9084 If N is negative, clear backward instead.
9085 The difference between N and the number of marks cleared is returned."
9086   (interactive "p")
9087   (gnus-summary-mark-forward n gnus-unread-mark))
9088
9089 (defun gnus-summary-clear-mark-backward (n)
9090   "Clear marks from N articles backward.
9091 The difference between N and the number of marks cleared is returned."
9092   (interactive "p")
9093   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9094
9095 (defun gnus-summary-mark-unread-as-read ()
9096   "Intended to be used by `gnus-summary-mark-article-hook'."
9097   (when (memq gnus-current-article gnus-newsgroup-unreads)
9098     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9099
9100 (defun gnus-summary-mark-read-and-unread-as-read ()
9101   "Intended to be used by `gnus-summary-mark-article-hook'."
9102   (let ((mark (gnus-summary-article-mark)))
9103     (when (or (gnus-unread-mark-p mark)
9104               (gnus-read-mark-p mark))
9105       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9106
9107 (defun gnus-summary-mark-unread-as-ticked ()
9108   "Intended to be used by `gnus-summary-mark-article-hook'."
9109   (when (memq gnus-current-article gnus-newsgroup-unreads)
9110     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9111
9112 (defun gnus-summary-mark-region-as-read (point mark all)
9113   "Mark all unread articles between point and mark as read.
9114 If given a prefix, mark all articles between point and mark as read,
9115 even ticked and dormant ones."
9116   (interactive "r\nP")
9117   (save-excursion
9118     (let (article)
9119       (goto-char point)
9120       (beginning-of-line)
9121       (while (and
9122               (< (point) mark)
9123               (progn
9124                 (when (or all
9125                           (memq (setq article (gnus-summary-article-number))
9126                                 gnus-newsgroup-unreads))
9127                   (gnus-summary-mark-article article gnus-del-mark))
9128                 t)
9129               (gnus-summary-find-next))))))
9130
9131 (defun gnus-summary-mark-below (score mark)
9132   "Mark articles with score less than SCORE with MARK."
9133   (interactive "P\ncMark: ")
9134   (setq score (if score
9135                   (prefix-numeric-value score)
9136                 (or gnus-summary-default-score 0)))
9137   (save-excursion
9138     (set-buffer gnus-summary-buffer)
9139     (goto-char (point-min))
9140     (while
9141         (progn
9142           (and (< (gnus-summary-article-score) score)
9143                (gnus-summary-mark-article nil mark))
9144           (gnus-summary-find-next)))))
9145
9146 (defun gnus-summary-kill-below (&optional score)
9147   "Mark articles with score below SCORE as read."
9148   (interactive "P")
9149   (gnus-summary-mark-below score gnus-killed-mark))
9150
9151 (defun gnus-summary-clear-above (&optional score)
9152   "Clear all marks from articles with score above SCORE."
9153   (interactive "P")
9154   (gnus-summary-mark-above score gnus-unread-mark))
9155
9156 (defun gnus-summary-tick-above (&optional score)
9157   "Tick all articles with score above SCORE."
9158   (interactive "P")
9159   (gnus-summary-mark-above score gnus-ticked-mark))
9160
9161 (defun gnus-summary-mark-above (score mark)
9162   "Mark articles with score over SCORE with MARK."
9163   (interactive "P\ncMark: ")
9164   (setq score (if score
9165                   (prefix-numeric-value score)
9166                 (or gnus-summary-default-score 0)))
9167   (save-excursion
9168     (set-buffer gnus-summary-buffer)
9169     (goto-char (point-min))
9170     (while (and (progn
9171                   (when (> (gnus-summary-article-score) score)
9172                     (gnus-summary-mark-article nil mark))
9173                   t)
9174                 (gnus-summary-find-next)))))
9175
9176 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9177 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9178 (defun gnus-summary-limit-include-expunged (&optional no-error)
9179   "Display all the hidden articles that were expunged for low scores."
9180   (interactive)
9181   (let ((buffer-read-only nil))
9182     (let ((scored gnus-newsgroup-scored)
9183           headers h)
9184       (while scored
9185         (unless (gnus-summary-article-header (caar scored))
9186           (and (setq h (gnus-number-to-header (caar scored)))
9187                (< (cdar scored) gnus-summary-expunge-below)
9188                (push h headers)))
9189         (setq scored (cdr scored)))
9190       (if (not headers)
9191           (when (not no-error)
9192             (error "No expunged articles hidden"))
9193         (goto-char (point-min))
9194         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9195         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9196         (mapcar (lambda (x) (push (mail-header-number x)
9197                                   gnus-newsgroup-limit))
9198                 headers)
9199         (gnus-summary-prepare-unthreaded (nreverse headers))
9200         (goto-char (point-min))
9201         (gnus-summary-position-point)
9202         t))))
9203
9204 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9205   "Mark all unread articles in this newsgroup as read.
9206 If prefix argument ALL is non-nil, ticked and dormant articles will
9207 also be marked as read.
9208 If QUIETLY is non-nil, no questions will be asked.
9209 If TO-HERE is non-nil, it should be a point in the buffer.  All
9210 articles before (after, if REVERSE is set) this point will be marked as read.
9211 Note that this function will only catch up the unread article
9212 in the current summary buffer limitation.
9213 The number of articles marked as read is returned."
9214   (interactive "P")
9215   (prog1
9216       (save-excursion
9217         (when (or quietly
9218                   (not gnus-interactive-catchup) ;Without confirmation?
9219                   gnus-expert-user
9220                   (gnus-y-or-n-p
9221                    (if all
9222                        "Mark absolutely all articles as read? "
9223                      "Mark all unread articles as read? ")))
9224           (if (and not-mark
9225                    (not gnus-newsgroup-adaptive)
9226                    (not gnus-newsgroup-auto-expire)
9227                    (not gnus-suppress-duplicates)
9228                    (or (not gnus-use-cache)
9229                        (eq gnus-use-cache 'passive)))
9230               (progn
9231                 (when all
9232                   (setq gnus-newsgroup-marked nil
9233                         gnus-newsgroup-dormant nil))
9234                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9235             ;; We actually mark all articles as canceled, which we
9236             ;; have to do when using auto-expiry or adaptive scoring.
9237             (gnus-summary-show-all-threads)
9238             (if (and to-here reverse)
9239                 (progn
9240                   (goto-char to-here)
9241                   (while (and
9242                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9243                           (gnus-summary-find-next (not all) nil nil t))))
9244               (when (gnus-summary-first-subject (not all) t)
9245                 (while (and
9246                         (if to-here (< (point) to-here) t)
9247                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9248                         (gnus-summary-find-next (not all) nil nil t)))))
9249             (gnus-set-mode-line 'summary))
9250           t))
9251     (gnus-summary-position-point)))
9252
9253 (defun gnus-summary-catchup-to-here (&optional all)
9254   "Mark all unticked articles before the current one as read.
9255 If ALL is non-nil, also mark ticked and dormant articles as read."
9256   (interactive "P")
9257   (save-excursion
9258     (gnus-save-hidden-threads
9259       (let ((beg (point)))
9260         ;; We check that there are unread articles.
9261         (when (or all (gnus-summary-find-prev))
9262           (gnus-summary-catchup all t beg)))))
9263   (gnus-summary-position-point))
9264
9265 (defun gnus-summary-catchup-from-here (&optional all)
9266   "Mark all unticked articles after the current one as read.
9267 If ALL is non-nil, also mark ticked and dormant articles as read."
9268   (interactive "P")
9269   (save-excursion
9270     (gnus-save-hidden-threads
9271       (let ((beg (point)))
9272         ;; We check that there are unread articles.
9273         (when (or all (gnus-summary-find-next))
9274           (gnus-summary-catchup all t beg nil t)))))
9275   (gnus-summary-position-point))
9276
9277 (defun gnus-summary-catchup-all (&optional quietly)
9278   "Mark all articles in this newsgroup as read."
9279   (interactive "P")
9280   (gnus-summary-catchup t quietly))
9281
9282 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9283   "Mark all unread articles in this group as read, then exit.
9284 If prefix argument ALL is non-nil, all articles are marked as read.
9285 If QUIETLY is non-nil, no questions will be asked."
9286   (interactive "P")
9287   (when (gnus-summary-catchup all quietly nil 'fast)
9288     ;; Select next newsgroup or exit.
9289     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9290              (eq gnus-auto-select-next 'quietly))
9291         (gnus-summary-next-group nil)
9292       (gnus-summary-exit))))
9293
9294 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9295   "Mark all articles in this newsgroup as read, and then exit."
9296   (interactive "P")
9297   (gnus-summary-catchup-and-exit t quietly))
9298
9299 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9300   "Mark all articles in this group as read and select the next group.
9301 If given a prefix, mark all articles, unread as well as ticked, as
9302 read."
9303   (interactive "P")
9304   (save-excursion
9305     (gnus-summary-catchup all))
9306   (gnus-summary-next-group))
9307
9308 ;;;
9309 ;;; with article
9310 ;;;
9311
9312 (defmacro gnus-with-article (article &rest forms)
9313   "Select ARTICLE and perform FORMS in the original article buffer.
9314 Then replace the article with the result."
9315   `(progn
9316      ;; We don't want the article to be marked as read.
9317      (let (gnus-mark-article-hook)
9318        (gnus-summary-select-article t t nil ,article))
9319      (set-buffer gnus-original-article-buffer)
9320      ,@forms
9321      (if (not (gnus-check-backend-function
9322                'request-replace-article (car gnus-article-current)))
9323          (gnus-message 5 "Read-only group; not replacing")
9324        (unless (gnus-request-replace-article
9325                 ,article (car gnus-article-current)
9326                 (current-buffer) t)
9327          (error "Couldn't replace article")))
9328      ;; The cache and backlog have to be flushed somewhat.
9329      (when gnus-keep-backlog
9330        (gnus-backlog-remove-article
9331         (car gnus-article-current) (cdr gnus-article-current)))
9332      (when gnus-use-cache
9333        (gnus-cache-update-article
9334         (car gnus-article-current) (cdr gnus-article-current)))))
9335
9336 (put 'gnus-with-article 'lisp-indent-function 1)
9337 (put 'gnus-with-article 'edebug-form-spec '(form body))
9338
9339 ;; Thread-based commands.
9340
9341 (defun gnus-summary-articles-in-thread (&optional article)
9342   "Return a list of all articles in the current thread.
9343 If ARTICLE is non-nil, return all articles in the thread that starts
9344 with that article."
9345   (let* ((article (or article (gnus-summary-article-number)))
9346          (data (gnus-data-find-list article))
9347          (top-level (gnus-data-level (car data)))
9348          (top-subject
9349           (cond ((null gnus-thread-operation-ignore-subject)
9350                  (gnus-simplify-subject-re
9351                   (mail-header-subject (gnus-data-header (car data)))))
9352                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9353                  (gnus-simplify-subject-fuzzy
9354                   (mail-header-subject (gnus-data-header (car data)))))
9355                 (t nil)))
9356          (end-point (save-excursion
9357                       (if (gnus-summary-go-to-next-thread)
9358                           (point) (point-max))))
9359          articles)
9360     (while (and data
9361                 (< (gnus-data-pos (car data)) end-point))
9362       (when (or (not top-subject)
9363                 (string= top-subject
9364                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9365                              (gnus-simplify-subject-fuzzy
9366                               (mail-header-subject
9367                                (gnus-data-header (car data))))
9368                            (gnus-simplify-subject-re
9369                             (mail-header-subject
9370                              (gnus-data-header (car data)))))))
9371         (push (gnus-data-number (car data)) articles))
9372       (unless (and (setq data (cdr data))
9373                    (> (gnus-data-level (car data)) top-level))
9374         (setq data nil)))
9375     ;; Return the list of articles.
9376     (nreverse articles)))
9377
9378 (defun gnus-summary-rethread-current ()
9379   "Rethread the thread the current article is part of."
9380   (interactive)
9381   (let* ((gnus-show-threads t)
9382          (article (gnus-summary-article-number))
9383          (id (mail-header-id (gnus-summary-article-header)))
9384          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9385     (unless id
9386       (error "No article on the current line"))
9387     (gnus-rebuild-thread id)
9388     (gnus-summary-goto-subject article)))
9389
9390 (defun gnus-summary-reparent-thread ()
9391   "Make the current article child of the marked (or previous) article.
9392
9393 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9394 is non-nil or the Subject: of both articles are the same."
9395   (interactive)
9396   (unless (not (gnus-group-read-only-p))
9397     (error "The current newsgroup does not support article editing"))
9398   (unless (<= (length gnus-newsgroup-processable) 1)
9399     (error "No more than one article may be marked"))
9400   (save-window-excursion
9401     (let ((gnus-article-buffer " *reparent*")
9402           (current-article (gnus-summary-article-number))
9403           ;; First grab the marked article, otherwise one line up.
9404           (parent-article (if (not (null gnus-newsgroup-processable))
9405                               (car gnus-newsgroup-processable)
9406                             (save-excursion
9407                               (if (eq (forward-line -1) 0)
9408                                   (gnus-summary-article-number)
9409                                 (error "Beginning of summary buffer"))))))
9410       (unless (not (eq current-article parent-article))
9411         (error "An article may not be self-referential"))
9412       (let ((message-id (mail-header-id
9413                          (gnus-summary-article-header parent-article))))
9414         (unless (and message-id (not (equal message-id "")))
9415           (error "No message-id in desired parent"))
9416         (gnus-with-article current-article
9417           (save-restriction
9418             (goto-char (point-min))
9419             (message-narrow-to-head)
9420             (if (re-search-forward "^References: " nil t)
9421                 (progn
9422                   (re-search-forward "^[^ \t]" nil t)
9423                   (forward-line -1)
9424                   (end-of-line)
9425                   (insert " " message-id))
9426               (insert "References: " message-id "\n"))))
9427         (set-buffer gnus-summary-buffer)
9428         (gnus-summary-unmark-all-processable)
9429         (gnus-summary-update-article current-article)
9430         (gnus-summary-rethread-current)
9431         (gnus-message 3 "Article %d is now the child of article %d"
9432                       current-article parent-article)))))
9433
9434 (defun gnus-summary-toggle-threads (&optional arg)
9435   "Toggle showing conversation threads.
9436 If ARG is positive number, turn showing conversation threads on."
9437   (interactive "P")
9438   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9439     (setq gnus-show-threads
9440           (if (null arg) (not gnus-show-threads)
9441             (> (prefix-numeric-value arg) 0)))
9442     (gnus-summary-prepare)
9443     (gnus-summary-goto-subject current)
9444     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9445     (gnus-summary-position-point)))
9446
9447 (defun gnus-summary-show-all-threads ()
9448   "Show all threads."
9449   (interactive)
9450   (save-excursion
9451     (let ((buffer-read-only nil))
9452       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9453   (gnus-summary-position-point))
9454
9455 (defun gnus-summary-show-thread ()
9456   "Show thread subtrees.
9457 Returns nil if no thread was there to be shown."
9458   (interactive)
9459   (let ((buffer-read-only nil)
9460         (orig (point))
9461         ;; first goto end then to beg, to have point at beg after let
9462         (end (progn (end-of-line) (point)))
9463         (beg (progn (beginning-of-line) (point))))
9464     (prog1
9465         ;; Any hidden lines here?
9466         (search-forward "\r" end t)
9467       (subst-char-in-region beg end ?\^M ?\n t)
9468       (goto-char orig)
9469       (gnus-summary-position-point))))
9470
9471 (defun gnus-summary-hide-all-threads ()
9472   "Hide all thread subtrees."
9473   (interactive)
9474   (save-excursion
9475     (goto-char (point-min))
9476     (gnus-summary-hide-thread)
9477     (while (zerop (gnus-summary-next-thread 1 t))
9478       (gnus-summary-hide-thread)))
9479   (gnus-summary-position-point))
9480
9481 (defun gnus-summary-hide-thread ()
9482   "Hide thread subtrees.
9483 Returns nil if no threads were there to be hidden."
9484   (interactive)
9485   (let ((buffer-read-only nil)
9486         (start (point))
9487         (article (gnus-summary-article-number)))
9488     (goto-char start)
9489     ;; Go forward until either the buffer ends or the subthread
9490     ;; ends.
9491     (when (and (not (eobp))
9492                (or (zerop (gnus-summary-next-thread 1 t))
9493                    (goto-char (point-max))))
9494       (prog1
9495           (if (and (> (point) start)
9496                    (search-backward "\n" start t))
9497               (progn
9498                 (subst-char-in-region start (point) ?\n ?\^M)
9499                 (gnus-summary-goto-subject article))
9500             (goto-char start)
9501             nil)))))
9502
9503 (defun gnus-summary-go-to-next-thread (&optional previous)
9504   "Go to the same level (or less) next thread.
9505 If PREVIOUS is non-nil, go to previous thread instead.
9506 Return the article number moved to, or nil if moving was impossible."
9507   (let ((level (gnus-summary-thread-level))
9508         (way (if previous -1 1))
9509         (beg (point)))
9510     (forward-line way)
9511     (while (and (not (eobp))
9512                 (< level (gnus-summary-thread-level)))
9513       (forward-line way))
9514     (if (eobp)
9515         (progn
9516           (goto-char beg)
9517           nil)
9518       (setq beg (point))
9519       (prog1
9520           (gnus-summary-article-number)
9521         (goto-char beg)))))
9522
9523 (defun gnus-summary-next-thread (n &optional silent)
9524   "Go to the same level next N'th thread.
9525 If N is negative, search backward instead.
9526 Returns the difference between N and the number of skips actually
9527 done.
9528
9529 If SILENT, don't output messages."
9530   (interactive "p")
9531   (let ((backward (< n 0))
9532         (n (abs n)))
9533     (while (and (> n 0)
9534                 (gnus-summary-go-to-next-thread backward))
9535       (decf n))
9536     (unless silent
9537       (gnus-summary-position-point))
9538     (when (and (not silent) (/= 0 n))
9539       (gnus-message 7 "No more threads"))
9540     n))
9541
9542 (defun gnus-summary-prev-thread (n)
9543   "Go to the same level previous N'th thread.
9544 Returns the difference between N and the number of skips actually
9545 done."
9546   (interactive "p")
9547   (gnus-summary-next-thread (- n)))
9548
9549 (defun gnus-summary-go-down-thread ()
9550   "Go down one level in the current thread."
9551   (let ((children (gnus-summary-article-children)))
9552     (when children
9553       (gnus-summary-goto-subject (car children)))))
9554
9555 (defun gnus-summary-go-up-thread ()
9556   "Go up one level in the current thread."
9557   (let ((parent (gnus-summary-article-parent)))
9558     (when parent
9559       (gnus-summary-goto-subject parent))))
9560
9561 (defun gnus-summary-down-thread (n)
9562   "Go down thread N steps.
9563 If N is negative, go up instead.
9564 Returns the difference between N and how many steps down that were
9565 taken."
9566   (interactive "p")
9567   (let ((up (< n 0))
9568         (n (abs n)))
9569     (while (and (> n 0)
9570                 (if up (gnus-summary-go-up-thread)
9571                   (gnus-summary-go-down-thread)))
9572       (setq n (1- n)))
9573     (gnus-summary-position-point)
9574     (when (/= 0 n)
9575       (gnus-message 7 "Can't go further"))
9576     n))
9577
9578 (defun gnus-summary-up-thread (n)
9579   "Go up thread N steps.
9580 If N is negative, go down instead.
9581 Returns the difference between N and how many steps down that were
9582 taken."
9583   (interactive "p")
9584   (gnus-summary-down-thread (- n)))
9585
9586 (defun gnus-summary-top-thread ()
9587   "Go to the top of the thread."
9588   (interactive)
9589   (while (gnus-summary-go-up-thread))
9590   (gnus-summary-article-number))
9591
9592 (defun gnus-summary-kill-thread (&optional unmark)
9593   "Mark articles under current thread as read.
9594 If the prefix argument is positive, remove any kinds of marks.
9595 If the prefix argument is negative, tick articles instead."
9596   (interactive "P")
9597   (when unmark
9598     (setq unmark (prefix-numeric-value unmark)))
9599   (let ((articles (gnus-summary-articles-in-thread)))
9600     (save-excursion
9601       ;; Expand the thread.
9602       (gnus-summary-show-thread)
9603       ;; Mark all the articles.
9604       (while articles
9605         (gnus-summary-goto-subject (car articles))
9606         (cond ((null unmark)
9607                (gnus-summary-mark-article-as-read gnus-killed-mark))
9608               ((> unmark 0)
9609                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9610               (t
9611                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9612         (setq articles (cdr articles))))
9613     ;; Hide killed subtrees.
9614     (and (null unmark)
9615          gnus-thread-hide-killed
9616          (gnus-summary-hide-thread))
9617     ;; If marked as read, go to next unread subject.
9618     (when (null unmark)
9619       ;; Go to next unread subject.
9620       (gnus-summary-next-subject 1 t)))
9621   (gnus-set-mode-line 'summary))
9622
9623 ;; Summary sorting commands
9624
9625 (defun gnus-summary-sort-by-number (&optional reverse)
9626   "Sort the summary buffer by article number.
9627 Argument REVERSE means reverse order."
9628   (interactive "P")
9629   (gnus-summary-sort 'number reverse))
9630
9631 (defun gnus-summary-sort-by-author (&optional reverse)
9632   "Sort the summary buffer by author name alphabetically.
9633 If `case-fold-search' is non-nil, case of letters is ignored.
9634 Argument REVERSE means reverse order."
9635   (interactive "P")
9636   (gnus-summary-sort 'author reverse))
9637
9638 (defun gnus-summary-sort-by-subject (&optional reverse)
9639   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9640 If `case-fold-search' is non-nil, case of letters is ignored.
9641 Argument REVERSE means reverse order."
9642   (interactive "P")
9643   (gnus-summary-sort 'subject reverse))
9644
9645 (defun gnus-summary-sort-by-date (&optional reverse)
9646   "Sort the summary buffer by date.
9647 Argument REVERSE means reverse order."
9648   (interactive "P")
9649   (gnus-summary-sort 'date reverse))
9650
9651 (defun gnus-summary-sort-by-score (&optional reverse)
9652   "Sort the summary buffer by score.
9653 Argument REVERSE means reverse order."
9654   (interactive "P")
9655   (gnus-summary-sort 'score reverse))
9656
9657 (defun gnus-summary-sort-by-lines (&optional reverse)
9658   "Sort the summary buffer by the number of lines.
9659 Argument REVERSE means reverse order."
9660   (interactive "P")
9661   (gnus-summary-sort 'lines reverse))
9662
9663 (defun gnus-summary-sort-by-chars (&optional reverse)
9664   "Sort the summary buffer by article length.
9665 Argument REVERSE means reverse order."
9666   (interactive "P")
9667   (gnus-summary-sort 'chars reverse))
9668
9669 (defun gnus-summary-sort-by-original (&optional reverse)
9670   "Sort the summary buffer using the default sorting method.
9671 Argument REVERSE means reverse order."
9672   (interactive "P")
9673   (let* ((buffer-read-only)
9674          (gnus-summary-prepare-hook nil))
9675     ;; We do the sorting by regenerating the threads.
9676     (gnus-summary-prepare)
9677     ;; Hide subthreads if needed.
9678     (when (and gnus-show-threads gnus-thread-hide-subtree)
9679       (gnus-summary-hide-all-threads))))
9680
9681 (defun gnus-summary-sort (predicate reverse)
9682   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9683   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9684          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9685          (gnus-thread-sort-functions
9686           (if (not reverse)
9687               thread
9688             `(lambda (t1 t2)
9689                (,thread t2 t1))))
9690          (gnus-sort-gathered-threads-function
9691           gnus-thread-sort-functions)
9692          (gnus-article-sort-functions
9693           (if (not reverse)
9694               article
9695             `(lambda (t1 t2)
9696                (,article t2 t1))))
9697          (buffer-read-only)
9698          (gnus-summary-prepare-hook nil))
9699     ;; We do the sorting by regenerating the threads.
9700     (gnus-summary-prepare)
9701     ;; Hide subthreads if needed.
9702     (when (and gnus-show-threads gnus-thread-hide-subtree)
9703       (gnus-summary-hide-all-threads))))
9704
9705 ;; Summary saving commands.
9706
9707 (defun gnus-summary-save-article (&optional n not-saved)
9708   "Save the current article using the default saver function.
9709 If N is a positive number, save the N next articles.
9710 If N is a negative number, save the N previous articles.
9711 If N is nil and any articles have been marked with the process mark,
9712 save those articles instead.
9713 The variable `gnus-default-article-saver' specifies the saver function."
9714   (interactive "P")
9715   (let* ((articles (gnus-summary-work-articles n))
9716          (save-buffer (save-excursion
9717                         (nnheader-set-temp-buffer " *Gnus Save*")))
9718          (num (length articles))
9719          header file)
9720     (dolist (article articles)
9721       (setq header (gnus-summary-article-header article))
9722       (if (not (vectorp header))
9723           ;; This is a pseudo-article.
9724           (if (assq 'name header)
9725               (gnus-copy-file (cdr (assq 'name header)))
9726             (gnus-message 1 "Article %d is unsaveable" article))
9727         ;; This is a real article.
9728         (save-window-excursion
9729           (gnus-summary-select-article t nil nil article))
9730         (save-excursion
9731           (set-buffer save-buffer)
9732           (erase-buffer)
9733           (insert-buffer-substring gnus-original-article-buffer))
9734         (setq file (gnus-article-save save-buffer file num))
9735         (gnus-summary-remove-process-mark article)
9736         (unless not-saved
9737           (gnus-summary-set-saved-mark article))))
9738     (gnus-kill-buffer save-buffer)
9739     (gnus-summary-position-point)
9740     (gnus-set-mode-line 'summary)
9741     n))
9742
9743 (defun gnus-summary-pipe-output (&optional arg)
9744   "Pipe the current article to a subprocess.
9745 If N is a positive number, pipe the N next articles.
9746 If N is a negative number, pipe the N previous articles.
9747 If N is nil and any articles have been marked with the process mark,
9748 pipe those articles instead."
9749   (interactive "P")
9750   (require 'gnus-art)
9751   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9752     (gnus-summary-save-article arg t))
9753   (let ((buffer (get-buffer "*Shell Command Output*")))
9754     (if (and buffer
9755              (with-current-buffer buffer (> (point-max) (point-min))))
9756         (gnus-configure-windows 'pipe))))
9757
9758 (defun gnus-summary-save-article-mail (&optional arg)
9759   "Append the current article to an mail file.
9760 If N is a positive number, save the N next articles.
9761 If N is a negative number, save the N previous articles.
9762 If N is nil and any articles have been marked with the process mark,
9763 save those articles instead."
9764   (interactive "P")
9765   (require 'gnus-art)
9766   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9767     (gnus-summary-save-article arg)))
9768
9769 (defun gnus-summary-save-article-rmail (&optional arg)
9770   "Append the current article to an rmail file.
9771 If N is a positive number, save the N next articles.
9772 If N is a negative number, save the N previous articles.
9773 If N is nil and any articles have been marked with the process mark,
9774 save those articles instead."
9775   (interactive "P")
9776   (require 'gnus-art)
9777   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9778     (gnus-summary-save-article arg)))
9779
9780 (defun gnus-summary-save-article-file (&optional arg)
9781   "Append the current article to a file.
9782 If N is a positive number, save the N next articles.
9783 If N is a negative number, save the N previous articles.
9784 If N is nil and any articles have been marked with the process mark,
9785 save those articles instead."
9786   (interactive "P")
9787   (require 'gnus-art)
9788   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9789     (gnus-summary-save-article arg)))
9790
9791 (defun gnus-summary-write-article-file (&optional arg)
9792   "Write the current article to a file, deleting the previous file.
9793 If N is a positive number, save the N next articles.
9794 If N is a negative number, save the N previous articles.
9795 If N is nil and any articles have been marked with the process mark,
9796 save those articles instead."
9797   (interactive "P")
9798   (require 'gnus-art)
9799   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9800     (gnus-summary-save-article arg)))
9801
9802 (defun gnus-summary-save-article-body-file (&optional arg)
9803   "Append the current article body to a file.
9804 If N is a positive number, save the N next articles.
9805 If N is a negative number, save the N previous articles.
9806 If N is nil and any articles have been marked with the process mark,
9807 save those articles instead."
9808   (interactive "P")
9809   (require 'gnus-art)
9810   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9811     (gnus-summary-save-article arg)))
9812
9813 (defun gnus-summary-pipe-message (program)
9814   "Pipe the current article through PROGRAM."
9815   (interactive "sProgram: ")
9816   (gnus-summary-select-article)
9817   (let ((mail-header-separator ""))
9818     (gnus-eval-in-buffer-window gnus-article-buffer
9819       (save-restriction
9820         (widen)
9821         (let ((start (window-start))
9822               buffer-read-only)
9823           (message-pipe-buffer-body program)
9824           (set-window-start (get-buffer-window (current-buffer)) start))))))
9825
9826 (defun gnus-get-split-value (methods)
9827   "Return a value based on the split METHODS."
9828   (let (split-name method result match)
9829     (when methods
9830       (save-excursion
9831         (set-buffer gnus-original-article-buffer)
9832         (save-restriction
9833           (nnheader-narrow-to-headers)
9834           (while (and methods (not split-name))
9835             (goto-char (point-min))
9836             (setq method (pop methods))
9837             (setq match (car method))
9838             (when (cond
9839                    ((stringp match)
9840                     ;; Regular expression.
9841                     (ignore-errors
9842                       (re-search-forward match nil t)))
9843                    ((gnus-functionp match)
9844                     ;; Function.
9845                     (save-restriction
9846                       (widen)
9847                       (setq result (funcall match gnus-newsgroup-name))))
9848                    ((consp match)
9849                     ;; Form.
9850                     (save-restriction
9851                       (widen)
9852                       (setq result (eval match)))))
9853               (setq split-name (cdr method))
9854               (cond ((stringp result)
9855                      (push (expand-file-name
9856                             result gnus-article-save-directory)
9857                            split-name))
9858                     ((consp result)
9859                      (setq split-name (append result split-name)))))))))
9860     (nreverse split-name)))
9861
9862 (defun gnus-valid-move-group-p (group)
9863   (and (boundp group)
9864        (symbol-name group)
9865        (symbol-value group)
9866        (gnus-get-function (gnus-find-method-for-group
9867                            (symbol-name group)) 'request-accept-article t)))
9868
9869 (defun gnus-read-move-group-name (prompt default articles prefix)
9870   "Read a group name."
9871   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9872          (minibuffer-confirm-incomplete nil) ; XEmacs
9873          (prom
9874           (format "%s %s to:"
9875                   prompt
9876                   (if (> (length articles) 1)
9877                       (format "these %d articles" (length articles))
9878                     "this article")))
9879          (to-newsgroup
9880           (cond
9881            ((null split-name)
9882             (gnus-completing-read default prom
9883                                   gnus-active-hashtb
9884                                   'gnus-valid-move-group-p
9885                                   nil prefix
9886                                   'gnus-group-history))
9887            ((= 1 (length split-name))
9888             (gnus-completing-read (car split-name) prom
9889                                   gnus-active-hashtb
9890                                   'gnus-valid-move-group-p
9891                                   nil nil
9892                                   'gnus-group-history))
9893            (t
9894             (gnus-completing-read nil prom
9895                                   (mapcar (lambda (el) (list el))
9896                                           (nreverse split-name))
9897                                   nil nil nil
9898                                   'gnus-group-history))))
9899          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9900     (when to-newsgroup
9901       (if (or (string= to-newsgroup "")
9902               (string= to-newsgroup prefix))
9903           (setq to-newsgroup default))
9904       (unless to-newsgroup
9905         (error "No group name entered"))
9906       (or (gnus-active to-newsgroup)
9907           (gnus-activate-group to-newsgroup nil nil to-method)
9908           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9909                                      to-newsgroup))
9910               (or (and (gnus-request-create-group to-newsgroup to-method)
9911                        (gnus-activate-group
9912                         to-newsgroup nil nil to-method)
9913                        (gnus-subscribe-group to-newsgroup))
9914                   (error "Couldn't create group %s" to-newsgroup)))
9915           (error "No such group: %s" to-newsgroup)))
9916     to-newsgroup))
9917
9918 (defun gnus-summary-save-parts (type dir n &optional reverse)
9919   "Save parts matching TYPE to DIR.
9920 If REVERSE, save parts that do not match TYPE."
9921   (interactive
9922    (list (read-string "Save parts of type: "
9923                       (or (car gnus-summary-save-parts-type-history)
9924                           gnus-summary-save-parts-default-mime)
9925                       'gnus-summary-save-parts-type-history)
9926          (setq gnus-summary-save-parts-last-directory
9927                (read-file-name "Save to directory: "
9928                                gnus-summary-save-parts-last-directory
9929                                nil t))
9930          current-prefix-arg))
9931   (gnus-summary-iterate n
9932     (let ((gnus-display-mime-function nil)
9933           (gnus-inhibit-treatment t))
9934       (gnus-summary-select-article))
9935     (save-excursion
9936       (set-buffer gnus-article-buffer)
9937       (let ((handles (or gnus-article-mime-handles
9938                          (mm-dissect-buffer) (mm-uu-dissect))))
9939         (when handles
9940           (gnus-summary-save-parts-1 type dir handles reverse)
9941           (unless gnus-article-mime-handles ;; Don't destroy this case.
9942             (mm-destroy-parts handles)))))))
9943
9944 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9945   (if (stringp (car handle))
9946       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9947               (cdr handle))
9948     (when (if reverse
9949               (not (string-match type (mm-handle-media-type handle)))
9950             (string-match type (mm-handle-media-type handle)))
9951       (let ((file (expand-file-name
9952                    (file-name-nondirectory
9953                     (or
9954                      (mail-content-type-get
9955                       (mm-handle-disposition handle) 'filename)
9956                      (concat gnus-newsgroup-name
9957                              "." (number-to-string
9958                                   (cdr gnus-article-current)))))
9959                    dir)))
9960         (unless (file-exists-p file)
9961           (mm-save-part-to-file handle file))))))
9962
9963 ;; Summary extract commands
9964
9965 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9966   (let ((buffer-read-only nil)
9967         (article (gnus-summary-article-number))
9968         after-article b e)
9969     (unless (gnus-summary-goto-subject article)
9970       (error "No such article: %d" article))
9971     (gnus-summary-position-point)
9972     ;; If all commands are to be bunched up on one line, we collect
9973     ;; them here.
9974     (unless gnus-view-pseudos-separately
9975       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9976             files action)
9977         (while ps
9978           (setq action (cdr (assq 'action (car ps))))
9979           (setq files (list (cdr (assq 'name (car ps)))))
9980           (while (and ps (cdr ps)
9981                       (string= (or action "1")
9982                                (or (cdr (assq 'action (cadr ps))) "2")))
9983             (push (cdr (assq 'name (cadr ps))) files)
9984             (setcdr ps (cddr ps)))
9985           (when files
9986             (when (not (string-match "%s" action))
9987               (push " " files))
9988             (push " " files)
9989             (when (assq 'execute (car ps))
9990               (setcdr (assq 'execute (car ps))
9991                       (funcall (if (string-match "%s" action)
9992                                    'format 'concat)
9993                                action
9994                                (mapconcat
9995                                 (lambda (f)
9996                                   (if (equal f " ")
9997                                       f
9998                                     (gnus-quote-arg-for-sh-or-csh f)))
9999                                 files " ")))))
10000           (setq ps (cdr ps)))))
10001     (if (and gnus-view-pseudos (not not-view))
10002         (while pslist
10003           (when (assq 'execute (car pslist))
10004             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10005                                   (eq gnus-view-pseudos 'not-confirm)))
10006           (setq pslist (cdr pslist)))
10007       (save-excursion
10008         (while pslist
10009           (setq after-article (or (cdr (assq 'article (car pslist)))
10010                                   (gnus-summary-article-number)))
10011           (gnus-summary-goto-subject after-article)
10012           (forward-line 1)
10013           (setq b (point))
10014           (insert "    " (file-name-nondirectory
10015                           (cdr (assq 'name (car pslist))))
10016                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10017           (setq e (point))
10018           (forward-line -1)             ; back to `b'
10019           (gnus-add-text-properties
10020            b (1- e) (list 'gnus-number gnus-reffed-article-number
10021                           gnus-mouse-face-prop gnus-mouse-face))
10022           (gnus-data-enter
10023            after-article gnus-reffed-article-number
10024            gnus-unread-mark b (car pslist) 0 (- e b))
10025           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10026           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10027           (setq pslist (cdr pslist)))))))
10028
10029 (defun gnus-pseudos< (p1 p2)
10030   (let ((c1 (cdr (assq 'action p1)))
10031         (c2 (cdr (assq 'action p2))))
10032     (and c1 c2 (string< c1 c2))))
10033
10034 (defun gnus-request-pseudo-article (props)
10035   (cond ((assq 'execute props)
10036          (gnus-execute-command (cdr (assq 'execute props)))))
10037   (let ((gnus-current-article (gnus-summary-article-number)))
10038     (gnus-run-hooks 'gnus-mark-article-hook)))
10039
10040 (defun gnus-execute-command (command &optional automatic)
10041   (save-excursion
10042     (gnus-article-setup-buffer)
10043     (set-buffer gnus-article-buffer)
10044     (setq buffer-read-only nil)
10045     (let ((command (if automatic command
10046                      (read-string "Command: " (cons command 0)))))
10047       (erase-buffer)
10048       (insert "$ " command "\n\n")
10049       (if gnus-view-pseudo-asynchronously
10050           (start-process "gnus-execute" (current-buffer) shell-file-name
10051                          shell-command-switch command)
10052         (call-process shell-file-name nil t nil
10053                       shell-command-switch command)))))
10054
10055 ;; Summary kill commands.
10056
10057 (defun gnus-summary-edit-global-kill (article)
10058   "Edit the \"global\" kill file."
10059   (interactive (list (gnus-summary-article-number)))
10060   (gnus-group-edit-global-kill article))
10061
10062 (defun gnus-summary-edit-local-kill ()
10063   "Edit a local kill file applied to the current newsgroup."
10064   (interactive)
10065   (setq gnus-current-headers (gnus-summary-article-header))
10066   (gnus-group-edit-local-kill
10067    (gnus-summary-article-number) gnus-newsgroup-name))
10068
10069 ;;; Header reading.
10070
10071 (defun gnus-read-header (id &optional header)
10072   "Read the headers of article ID and enter them into the Gnus system."
10073   (let ((group gnus-newsgroup-name)
10074         (gnus-override-method
10075          (or
10076           gnus-override-method
10077           (and (gnus-news-group-p gnus-newsgroup-name)
10078                (car (gnus-refer-article-methods)))))
10079         where)
10080     ;; First we check to see whether the header in question is already
10081     ;; fetched.
10082     (if (stringp id)
10083         ;; This is a Message-ID.
10084         (setq header (or header (gnus-id-to-header id)))
10085       ;; This is an article number.
10086       (setq header (or header (gnus-summary-article-header id))))
10087     (if (and header
10088              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10089         ;; We have found the header.
10090         header
10091       ;; If this is a sparse article, we have to nix out its
10092       ;; previous entry in the thread hashtb.
10093       (when (and header
10094                  (gnus-summary-article-sparse-p (mail-header-number header)))
10095         (let* ((parent (gnus-parent-id (mail-header-references header)))
10096                (thread (and parent (gnus-id-to-thread parent))))
10097           (when thread
10098             (delq (assq header thread) thread))))
10099       ;; We have to really fetch the header to this article.
10100       (save-excursion
10101         (set-buffer nntp-server-buffer)
10102         (when (setq where (gnus-request-head id group))
10103           (nnheader-fold-continuation-lines)
10104           (goto-char (point-max))
10105           (insert ".\n")
10106           (goto-char (point-min))
10107           (insert "211 ")
10108           (princ (cond
10109                   ((numberp id) id)
10110                   ((cdr where) (cdr where))
10111                   (header (mail-header-number header))
10112                   (t gnus-reffed-article-number))
10113                  (current-buffer))
10114           (insert " Article retrieved.\n"))
10115         (if (or (not where)
10116                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10117             ()                          ; Malformed head.
10118           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10119             (when (and (stringp id)
10120                        (not (string= (gnus-group-real-name group)
10121                                      (car where))))
10122               ;; If we fetched by Message-ID and the article came
10123               ;; from a different group, we fudge some bogus article
10124               ;; numbers for this article.
10125               (mail-header-set-number header gnus-reffed-article-number))
10126             (save-excursion
10127               (set-buffer gnus-summary-buffer)
10128               (decf gnus-reffed-article-number)
10129               (gnus-remove-header (mail-header-number header))
10130               (push header gnus-newsgroup-headers)
10131               (setq gnus-current-headers header)
10132               (push (mail-header-number header) gnus-newsgroup-limit)))
10133           header)))))
10134
10135 (defun gnus-remove-header (number)
10136   "Remove header NUMBER from `gnus-newsgroup-headers'."
10137   (if (and gnus-newsgroup-headers
10138            (= number (mail-header-number (car gnus-newsgroup-headers))))
10139       (pop gnus-newsgroup-headers)
10140     (let ((headers gnus-newsgroup-headers))
10141       (while (and (cdr headers)
10142                   (not (= number (mail-header-number (cadr headers)))))
10143         (pop headers))
10144       (when (cdr headers)
10145         (setcdr headers (cddr headers))))))
10146
10147 ;;;
10148 ;;; summary highlights
10149 ;;;
10150
10151 (defun gnus-highlight-selected-summary ()
10152   "Highlight selected article in summary buffer."
10153   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10154   (when gnus-summary-selected-face
10155     (save-excursion
10156       (let* ((beg (progn (beginning-of-line) (point)))
10157              (end (progn (end-of-line) (point)))
10158              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10159              (from (if (get-text-property beg gnus-mouse-face-prop)
10160                        beg
10161                      (or (next-single-property-change
10162                           beg gnus-mouse-face-prop nil end)
10163                          beg)))
10164              (to
10165               (if (= from end)
10166                   (- from 2)
10167                 (or (next-single-property-change
10168                      from gnus-mouse-face-prop nil end)
10169                     end))))
10170         ;; If no mouse-face prop on line we will have to = from = end,
10171         ;; so we highlight the entire line instead.
10172         (when (= (+ to 2) from)
10173           (setq from beg)
10174           (setq to end))
10175         (if gnus-newsgroup-selected-overlay
10176             ;; Move old overlay.
10177             (gnus-move-overlay
10178              gnus-newsgroup-selected-overlay from to (current-buffer))
10179           ;; Create new overlay.
10180           (gnus-overlay-put
10181            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10182            'face gnus-summary-selected-face))))))
10183
10184 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10185 (defun gnus-summary-highlight-line ()
10186   "Highlight current line according to `gnus-summary-highlight'."
10187   (let* ((list gnus-summary-highlight)
10188          (p (point))
10189          (end (progn (end-of-line) (point)))
10190          ;; now find out where the line starts and leave point there.
10191          (beg (progn (beginning-of-line) (point)))
10192          (article (gnus-summary-article-number))
10193          (score (or (cdr (assq (or article gnus-current-article)
10194                                gnus-newsgroup-scored))
10195                     gnus-summary-default-score 0))
10196          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10197          (inhibit-read-only t))
10198     ;; Eval the cars of the lists until we find a match.
10199     (let ((default gnus-summary-default-score)
10200           (default-high gnus-summary-default-high-score)
10201           (default-low gnus-summary-default-low-score))
10202       (while (and list
10203                   (not (eval (caar list))))
10204         (setq list (cdr list))))
10205     (let ((face (cdar list)))
10206       (unless (eq face (get-text-property beg 'face))
10207         (gnus-put-text-property-excluding-characters-with-faces
10208          beg end 'face
10209          (setq face (if (boundp face) (symbol-value face) face)))
10210         (when gnus-summary-highlight-line-function
10211           (funcall gnus-summary-highlight-line-function article face))))
10212     (goto-char p)))
10213
10214 (defun gnus-update-read-articles (group unread &optional compute)
10215   "Update the list of read articles in GROUP."
10216   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10217          (entry (gnus-gethash group gnus-newsrc-hashtb))
10218          (info (nth 2 entry))
10219          (prev 1)
10220          (unread (sort (copy-sequence unread) '<))
10221          read)
10222     (if (or (not info) (not active))
10223         ;; There is no info on this group if it was, in fact,
10224         ;; killed.  Gnus stores no information on killed groups, so
10225         ;; there's nothing to be done.
10226         ;; One could store the information somewhere temporarily,
10227         ;; perhaps...  Hmmm...
10228         ()
10229       ;; Remove any negative articles numbers.
10230       (while (and unread (< (car unread) 0))
10231         (setq unread (cdr unread)))
10232       ;; Remove any expired article numbers
10233       (while (and unread (< (car unread) (car active)))
10234         (setq unread (cdr unread)))
10235       ;; Compute the ranges of read articles by looking at the list of
10236       ;; unread articles.
10237       (while unread
10238         (when (/= (car unread) prev)
10239           (push (if (= prev (1- (car unread))) prev
10240                   (cons prev (1- (car unread))))
10241                 read))
10242         (setq prev (1+ (car unread)))
10243         (setq unread (cdr unread)))
10244       (when (<= prev (cdr active))
10245         (push (cons prev (cdr active)) read))
10246       (setq read (if (> (length read) 1) (nreverse read) read))
10247       (if compute
10248           read
10249         (save-excursion
10250           (let (setmarkundo)
10251             ;; Propagate the read marks to the backend.
10252             (when (gnus-check-backend-function 'request-set-mark group)
10253               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10254                     (add (gnus-remove-from-range read (gnus-info-read info))))
10255                 (when (or add del)
10256                   (unless (gnus-check-group group)
10257                     (error "Can't open server for %s" group))
10258                   (gnus-request-set-mark
10259                    group (delq nil (list (if add (list add 'add '(read)))
10260                                          (if del (list del 'del '(read))))))
10261                   (setq setmarkundo
10262                         `(gnus-request-set-mark
10263                           ,group
10264                           ',(delq nil (list
10265                                        (if del (list del 'add '(read)))
10266                                        (if add (list add 'del '(read))))))))))
10267             (set-buffer gnus-group-buffer)
10268             (gnus-undo-register
10269               `(progn
10270                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10271                  (gnus-info-set-read ',info ',(gnus-info-read info))
10272                  (gnus-get-unread-articles-in-group ',info
10273                                                     (gnus-active ,group))
10274                  (gnus-group-update-group ,group t)
10275                  ,setmarkundo))))
10276         ;; Enter this list into the group info.
10277         (gnus-info-set-read info read)
10278         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10279         (gnus-get-unread-articles-in-group info (gnus-active group))
10280         t))))
10281
10282 (defun gnus-offer-save-summaries ()
10283   "Offer to save all active summary buffers."
10284   (let (buffers)
10285     ;; Go through all buffers and find all summaries.
10286     (dolist (buffer (buffer-list))
10287       (when (and (setq buffer (buffer-name buffer))
10288                  (string-match "Summary" buffer)
10289                  (save-excursion
10290                    (set-buffer buffer)
10291                    ;; We check that this is, indeed, a summary buffer.
10292                    (and (eq major-mode 'gnus-summary-mode)
10293                         ;; Also make sure this isn't bogus.
10294                         gnus-newsgroup-prepared
10295                         ;; Also make sure that this isn't a
10296                         ;; dead summary buffer.
10297                         (not gnus-dead-summary-mode))))
10298         (push buffer buffers)))
10299     ;; Go through all these summary buffers and offer to save them.
10300     (when buffers
10301       (save-excursion
10302         (map-y-or-n-p
10303          "Update summary buffer %s? "
10304          (lambda (buf)
10305            (switch-to-buffer buf)
10306            (gnus-summary-exit))
10307          buffers)))))
10308
10309
10310 ;;; @ for mime-partial
10311 ;;;
10312
10313 (defun gnus-request-partial-message ()
10314   (save-excursion
10315     (let ((number (gnus-summary-article-number))
10316           (group gnus-newsgroup-name)
10317           (mother gnus-article-buffer))
10318       (set-buffer (get-buffer-create " *Partial Article*"))
10319       (erase-buffer)
10320       (setq mime-preview-buffer mother)
10321       (gnus-request-article-this-buffer number group)
10322       (mime-parse-buffer)
10323       )))
10324
10325 (autoload 'mime-combine-message/partial-pieces-automatically
10326   "mime-partial"
10327   "Internal method to combine message/partial messages automatically.")
10328
10329 (mime-add-condition
10330  'action '((type . message)(subtype . partial)
10331            (major-mode . gnus-original-article-mode)
10332            (method . mime-combine-message/partial-pieces-automatically)
10333            (summary-buffer-exp . gnus-summary-buffer)
10334            (request-partial-message-method . gnus-request-partial-message)
10335            ))
10336
10337
10338 ;;; @ for message/rfc822
10339 ;;;
10340
10341 (defun gnus-mime-extract-message/rfc822 (entity situation)
10342   (let (group article num cwin swin cur)
10343     (with-temp-buffer
10344       (mime-insert-entity-content entity)
10345       (setq group (or (cdr (assq 'group situation))
10346                       (completing-read "Group: "
10347                                        gnus-active-hashtb
10348                                        nil
10349                                        (gnus-read-active-file-p)
10350                                        gnus-newsgroup-name))
10351             article (gnus-request-accept-article group)))
10352     (when (and (consp article)
10353                (numberp (setq article (cdr article))))
10354       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10355             cwin (get-buffer-window (current-buffer) t))
10356       (save-window-excursion
10357         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10358             (select-window swin)
10359           (set-buffer gnus-summary-buffer))
10360         (setq cur gnus-current-article)
10361         (forward-line num)
10362         (let (gnus-show-threads)
10363           (gnus-summary-goto-subject article t))
10364         (gnus-summary-clear-mark-forward 1)
10365         (gnus-summary-goto-subject cur))
10366       (when (and cwin (window-frame cwin))
10367         (select-frame (window-frame cwin)))
10368       (when (boundp 'mime-acting-situation-to-override)
10369         (set-alist 'mime-acting-situation-to-override
10370                    'group
10371                    group)
10372         (set-alist 'mime-acting-situation-to-override
10373                    'after-method
10374                    `(progn
10375                       (save-current-buffer
10376                         (set-buffer gnus-group-buffer)
10377                         (gnus-activate-group ,group))
10378                       (gnus-summary-goto-article ,cur
10379                                                  gnus-show-all-headers)))
10380         (set-alist 'mime-acting-situation-to-override
10381                    'number num)))))
10382
10383 (mime-add-condition
10384  'action '((type . message)(subtype . rfc822)
10385            (major-mode . gnus-original-article-mode)
10386            (method . gnus-mime-extract-message/rfc822)
10387            (mode . "extract")
10388            ))
10389
10390 (mime-add-condition
10391  'action '((type . message)(subtype . news)
10392            (major-mode . gnus-original-article-mode)
10393            (method . gnus-mime-extract-message/rfc822)
10394            (mode . "extract")
10395            ))
10396
10397 (defun gnus-mime-extract-multipart (entity situation)
10398   (let ((children (mime-entity-children entity))
10399         mime-acting-situation-to-override
10400         f)
10401     (while children
10402       (mime-play-entity (car children)
10403                         (cons (assq 'mode situation)
10404                               mime-acting-situation-to-override))
10405       (setq children (cdr children)))
10406     (if (setq f (cdr (assq 'after-method
10407                            mime-acting-situation-to-override)))
10408         (eval f)
10409       )))
10410
10411 (mime-add-condition
10412  'action '((type . multipart)
10413            (method . gnus-mime-extract-multipart)
10414            (mode . "extract")
10415            )
10416  'with-default)
10417
10418
10419 ;;; @ end
10420 ;;;
10421
10422 (defun gnus-summary-setup-default-charset ()
10423   "Setup newsgroup default charset."
10424   (if (equal gnus-newsgroup-name "nndraft:drafts")
10425       (setq gnus-newsgroup-charset nil)
10426     (let* ((ignored-charsets
10427             (or gnus-newsgroup-ephemeral-ignored-charsets
10428                 (append
10429                  (and gnus-newsgroup-name
10430                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10431                  gnus-newsgroup-ignored-charsets))))
10432       (setq gnus-newsgroup-charset
10433             (or gnus-newsgroup-ephemeral-charset
10434                 (and gnus-newsgroup-name
10435                      (gnus-parameter-charset gnus-newsgroup-name))
10436                 gnus-default-charset))
10437       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10438            ignored-charsets))))
10439
10440 ;;;
10441 ;;; Mime Commands
10442 ;;;
10443
10444 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10445   "Display the current article buffer fully MIME-buttonized.
10446 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10447 treated as multipart/mixed."
10448   (interactive "P")
10449   (require 'gnus-art)
10450   (let ((gnus-unbuttonized-mime-types nil)
10451         (gnus-mime-display-multipart-as-mixed show-all-parts))
10452     (gnus-summary-show-article)))
10453
10454 (defun gnus-summary-repair-multipart (article)
10455   "Add a Content-Type header to a multipart article without one."
10456   (interactive (list (gnus-summary-article-number)))
10457   (gnus-with-article article
10458     (message-narrow-to-head)
10459     (message-remove-header "Mime-Version")
10460     (goto-char (point-max))
10461     (insert "Mime-Version: 1.0\n")
10462     (widen)
10463     (when (search-forward "\n--" nil t)
10464       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10465         (message-narrow-to-head)
10466         (message-remove-header "Content-Type")
10467         (goto-char (point-max))
10468         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10469                         separator))
10470         (widen))))
10471   (let (gnus-mark-article-hook)
10472     (gnus-summary-select-article t t nil article)))
10473
10474 (defun gnus-summary-toggle-display-buttonized ()
10475   "Toggle the buttonizing of the article buffer."
10476   (interactive)
10477   (require 'gnus-art)
10478   (if (setq gnus-inhibit-mime-unbuttonizing
10479             (not gnus-inhibit-mime-unbuttonizing))
10480       (let ((gnus-unbuttonized-mime-types nil))
10481         (gnus-summary-show-article))
10482     (gnus-summary-show-article)))
10483
10484 ;;;
10485 ;;; Intelli-mouse commmands
10486 ;;;
10487
10488 (defun gnus-wheel-summary-scroll (event)
10489   (interactive "e")
10490   (let ((amount (if (memq 'shift (event-modifiers event))
10491                     (car gnus-wheel-scroll-amount)
10492                   (cdr gnus-wheel-scroll-amount)))
10493         (direction (- (* (static-if (featurep 'xemacs)
10494                              (event-button event)
10495                            (cond ((eq 'mouse-4 (event-basic-type event))
10496                                   4)
10497                                  ((eq 'mouse-5 (event-basic-type event))
10498                                   5)))
10499                          2) 9))
10500         edge)
10501     (gnus-summary-scroll-up (* amount direction))
10502     (when (gnus-eval-in-buffer-window gnus-article-buffer
10503             (save-restriction
10504               (widen)
10505               (and (if (< 0 direction)
10506                        (gnus-article-next-page 0)
10507                      (gnus-article-prev-page 0)
10508                      (bobp))
10509                    (if (setq edge (get-text-property
10510                                    (point-min) 'gnus-wheel-edge))
10511                        (setq edge (* edge direction))
10512                      (setq edge -1))
10513                    (or (plusp edge)
10514                        (let ((buffer-read-only nil)
10515                              (inhibit-read-only t))
10516                          (put-text-property (point-min) (point-max)
10517                                             'gnus-wheel-edge direction)
10518                          nil))
10519                    (or (> edge gnus-wheel-edge-resistance)
10520                        (let ((buffer-read-only nil)
10521                              (inhibit-read-only t))
10522                          (put-text-property (point-min) (point-max)
10523                                             'gnus-wheel-edge
10524                                             (* (1+ edge) direction))
10525                          nil))
10526                    (eq last-command 'gnus-wheel-summary-scroll))))
10527       (gnus-summary-next-article nil nil (minusp direction)))))
10528
10529 (defun gnus-wheel-install ()
10530   "Enable mouse wheel support on summary window."
10531   (when gnus-use-wheel
10532     (let ((keys
10533            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10534       (dolist (key keys)
10535         (define-key gnus-summary-mode-map key
10536           'gnus-wheel-summary-scroll)))))
10537
10538 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10539
10540 ;;;
10541 ;;; Traditional PGP commmands
10542 ;;;
10543
10544 (defun gnus-summary-decrypt-article (&optional force)
10545   "Decrypt the current article in traditional PGP way.
10546 This will have permanent effect only in mail groups.
10547 If FORCE is non-nil, allow editing of articles even in read-only
10548 groups."
10549   (interactive "P")
10550   (gnus-summary-select-article t)
10551   (gnus-eval-in-buffer-window gnus-article-buffer
10552     (save-excursion
10553       (save-restriction
10554         (widen)
10555         (goto-char (point-min))
10556         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10557           (error "Not a traditional PGP message!"))
10558         (let ((armor-start (match-beginning 0)))
10559           (if (and (pgg-decrypt-region armor-start (point-max))
10560                    (or force (not (gnus-group-read-only-p))))
10561               (let ((inhibit-read-only t)
10562                     buffer-read-only)
10563                 (delete-region armor-start
10564                                (progn
10565                                  (re-search-forward "^-+END PGP" nil t)
10566                                  (beginning-of-line 2)
10567                                  (point)))
10568                 (insert-buffer-substring pgg-output-buffer))))))))
10569
10570 (defun gnus-summary-verify-article ()
10571   "Verify the current article in traditional PGP way."
10572   (interactive)
10573   (save-excursion
10574     (set-buffer gnus-original-article-buffer)
10575     (goto-char (point-min))
10576     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10577       (error "Not a traditional PGP message!"))
10578     (re-search-forward "^-+END PGP" nil t)
10579     (beginning-of-line 2)
10580     (call-interactively (function pgg-verify-region))))
10581
10582 ;;;
10583 ;;; Generic summary marking commands
10584 ;;;
10585
10586 (defvar gnus-summary-marking-alist
10587   '((read gnus-del-mark "d")
10588     (unread gnus-unread-mark "u")
10589     (ticked gnus-ticked-mark "!")
10590     (dormant gnus-dormant-mark "?")
10591     (expirable gnus-expirable-mark "e"))
10592   "An alist of names/marks/keystrokes.")
10593
10594 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10595 (defvar gnus-summary-mark-map)
10596
10597 (defun gnus-summary-make-all-marking-commands ()
10598   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10599   (dolist (elem gnus-summary-marking-alist)
10600     (apply 'gnus-summary-make-marking-command elem)))
10601
10602 (defun gnus-summary-make-marking-command (name mark keystroke)
10603   (let ((map (make-sparse-keymap)))
10604     (define-key gnus-summary-generic-mark-map keystroke map)
10605     (dolist (lway `((next "next" next nil "n")
10606                     (next-unread "next unread" next t "N")
10607                     (prev "previous" prev nil "p")
10608                     (prev-unread "previous unread" prev t "P")
10609                     (nomove "" nil nil ,keystroke)))
10610       (let ((func (gnus-summary-make-marking-command-1
10611                    mark (car lway) lway name)))
10612         (setq func (eval func))
10613         (define-key map (nth 4 lway) func)))))
10614
10615 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10616   `(defun ,(intern
10617             (format "gnus-summary-put-mark-as-%s%s"
10618                     name (if (eq way 'nomove)
10619                              ""
10620                            (concat "-" (symbol-name way)))))
10621      (n)
10622      ,(format
10623        "Mark the current article as %s%s.
10624 If N, the prefix, then repeat N times.
10625 If N is negative, move in reverse order.
10626 The difference between N and the actual number of articles marked is
10627 returned."
10628        name (car (cdr lway)))
10629      (interactive "p")
10630      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10631
10632 (defun gnus-summary-generic-mark (n mark move unread)
10633   "Mark N articles with MARK."
10634   (unless (eq major-mode 'gnus-summary-mode)
10635     (error "This command can only be used in the summary buffer"))
10636   (gnus-summary-show-thread)
10637   (let ((nummove
10638          (cond
10639           ((eq move 'next) 1)
10640           ((eq move 'prev) -1)
10641           (t 0))))
10642     (if (zerop nummove)
10643         (setq n 1)
10644       (when (< n 0)
10645         (setq n (abs n)
10646               nummove (* -1 nummove))))
10647     (while (and (> n 0)
10648                 (gnus-summary-mark-article nil mark)
10649                 (zerop (gnus-summary-next-subject nummove unread t)))
10650       (setq n (1- n)))
10651     (when (/= 0 n)
10652       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10653     (gnus-summary-recenter)
10654     (gnus-summary-position-point)
10655     (gnus-set-mode-line 'summary)
10656     n))
10657
10658 (defun gnus-summary-insert-articles (articles)
10659   (when (setq articles
10660               (gnus-set-difference articles
10661                                    (mapcar (lambda (h) (mail-header-number h))
10662                                            gnus-newsgroup-headers)))
10663     (setq gnus-newsgroup-headers
10664           (merge 'list
10665                  gnus-newsgroup-headers
10666                  (gnus-fetch-headers articles)
10667                  'gnus-article-sort-by-number))
10668     ;; Suppress duplicates?
10669     (when gnus-suppress-duplicates
10670       (gnus-dup-suppress-articles))
10671
10672     ;; We might want to build some more threads first.
10673     (when (and gnus-fetch-old-headers
10674                (eq gnus-headers-retrieved-by 'nov))
10675       (if (eq gnus-fetch-old-headers 'invisible)
10676           (gnus-build-all-threads)
10677         (gnus-build-old-threads)))
10678     ;; Let the Gnus agent mark articles as read.
10679     (when gnus-agent
10680       (gnus-agent-get-undownloaded-list))
10681     ;; Remove list identifiers from subject
10682     (when gnus-list-identifiers
10683       (gnus-summary-remove-list-identifiers))
10684     ;; First and last article in this newsgroup.
10685     (when gnus-newsgroup-headers
10686       (setq gnus-newsgroup-begin
10687             (mail-header-number (car gnus-newsgroup-headers))
10688             gnus-newsgroup-end
10689             (mail-header-number
10690              (gnus-last-element gnus-newsgroup-headers))))
10691     (when gnus-use-scoring
10692       (gnus-possibly-score-headers))))
10693
10694 (defun gnus-summary-insert-old-articles (&optional all)
10695   "Insert all old articles in this group.
10696 If ALL is non-nil, already read articles become readable.
10697 If ALL is a number, fetch this number of articles."
10698   (interactive "P")
10699   (prog1
10700       (let ((old (mapcar 'car gnus-newsgroup-data))
10701             (i (car gnus-newsgroup-active))
10702             older len)
10703         (while (<= i (cdr gnus-newsgroup-active))
10704           (or (memq i old) (push i older))
10705           (incf i))
10706         (setq len (length older))
10707         (cond
10708          ((null older) nil)
10709          ((numberp all)
10710           (if (< all len)
10711               (setq older (subseq older 0 all))))
10712          (all nil)
10713          (t
10714           (if (and (numberp gnus-large-newsgroup)
10715                    (> len gnus-large-newsgroup))
10716               (let ((input
10717                      (read-string
10718                       (format
10719                        "How many articles from %s (default %d): "
10720                        (gnus-limit-string
10721                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10722                        len))))
10723                 (unless (string-match "^[ \t]*$" input)
10724                   (setq all (string-to-number input))
10725                   (if (< all len)
10726                       (setq older (subseq older 0 all))))))))
10727         (if (not older)
10728             (message "No old news.")
10729           (gnus-summary-insert-articles older)
10730           (gnus-summary-limit (gnus-union older old))))
10731     (gnus-summary-position-point)))
10732
10733 (defun gnus-summary-insert-new-articles ()
10734   "Insert all new articles in this group."
10735   (interactive)
10736   (prog1
10737       (let ((old (mapcar 'car gnus-newsgroup-data))
10738             (old-active gnus-newsgroup-active)
10739             (nnmail-fetched-sources (list t))
10740             i new)
10741         (setq gnus-newsgroup-active
10742               (gnus-activate-group gnus-newsgroup-name 'scan))
10743         (setq i (1+ (cdr old-active)))
10744         (while (<= i (cdr gnus-newsgroup-active))
10745           (push i new)
10746           (incf i))
10747         (if (not new)
10748             (message "No gnus is bad news.")
10749           (setq new (nreverse new))
10750           (gnus-summary-insert-articles new)
10751           (setq gnus-newsgroup-unreads
10752                 (append gnus-newsgroup-unreads new))
10753           (gnus-summary-limit (gnus-union old new))))
10754     (gnus-summary-position-point)))
10755
10756 (gnus-summary-make-all-marking-commands)
10757
10758 (gnus-ems-redefine)
10759
10760 (provide 'gnus-sum)
10761
10762 (run-hooks 'gnus-sum-load-hook)
10763
10764 ;;; gnus-sum.el ends here