Synch with Oort Gnus.
[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 ?F
466   "*Mark used for articles that have been forwarded."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-recent-mark ?N
471   "*Mark used for articles that are recent."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-cached-mark ?*
476   "*Mark used for articles that are in the cache."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-saved-mark ?S
481   "*Mark used for articles that have been saved."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-unseen-mark ?.
486   "*Mark used for articles that haven't been seen."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-no-mark ?\ ;;;Whitespace
491   "*Mark used for articles that have no other secondary mark."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-ancient-mark ?O
496   "*Mark used for ancient articles."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-sparse-mark ?Q
501   "*Mark used for sparsely reffed articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-canceled-mark ?G
506   "*Mark used for canceled articles."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-duplicate-mark ?M
511   "*Mark used for duplicate articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-undownloaded-mark ?@
516   "*Mark used for articles that weren't downloaded."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-downloadable-mark ?%
521   "*Mark used for articles that are to be downloaded."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-unsendable-mark ?=
526   "*Mark used for articles that won't be sent."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-score-over-mark ?+
531   "*Score mark used for articles with high scores."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-score-below-mark ?-
536   "*Score mark used for articles with low scores."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
541   "*There is no thread under the article."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-not-empty-thread-mark ?=
546   "*There is a thread under the article."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-view-pseudo-asynchronously nil
551   "*If non-nil, Gnus will view pseudo-articles asynchronously."
552   :group 'gnus-extract-view
553   :type 'boolean)
554
555 (defcustom gnus-auto-expirable-marks
556   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
557         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
558         gnus-souped-mark gnus-duplicate-mark)
559   "*The list of marks converted into expiration if a group is auto-expirable."
560   :version "21.1"
561   :group 'gnus-summary
562   :type '(repeat character))
563
564 (defcustom gnus-inhibit-user-auto-expire t
565   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
566   :version "21.1"
567   :group 'gnus-summary
568   :type 'boolean)
569
570 (defcustom gnus-view-pseudos nil
571   "*If `automatic', pseudo-articles will be viewed automatically.
572 If `not-confirm', pseudos will be viewed automatically, and the user
573 will not be asked to confirm the command."
574   :group 'gnus-extract-view
575   :type '(choice (const :tag "off" nil)
576                  (const automatic)
577                  (const not-confirm)))
578
579 (defcustom gnus-view-pseudos-separately t
580   "*If non-nil, one pseudo-article will be created for each file to be viewed.
581 If nil, all files that use the same viewing command will be given as a
582 list of parameters to that command."
583   :group 'gnus-extract-view
584   :type 'boolean)
585
586 (defcustom gnus-insert-pseudo-articles t
587   "*If non-nil, insert pseudo-articles when decoding articles."
588   :group 'gnus-extract-view
589   :type 'boolean)
590
591 (defcustom gnus-summary-dummy-line-format
592   "  %(:                          :%) %S\n"
593   "*The format specification for the dummy roots in the summary buffer.
594 It works along the same lines as a normal formatting string,
595 with some simple extensions.
596
597 %S  The subject"
598   :group 'gnus-threading
599   :type 'string)
600
601 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
602   "*The format specification for the summary mode line.
603 It works along the same lines as a normal formatting string,
604 with some simple extensions:
605
606 %G  Group name
607 %p  Unprefixed group name
608 %A  Current article number
609 %z  Current article score
610 %V  Gnus version
611 %U  Number of unread articles in the group
612 %e  Number of unselected articles in the group
613 %Z  A string with unread/unselected article counts
614 %g  Shortish group name
615 %S  Subject of the current article
616 %u  User-defined spec
617 %s  Current score file name
618 %d  Number of dormant articles
619 %r  Number of articles that have been marked as read in this session
620 %E  Number of articles expunged by the score files"
621   :group 'gnus-summary-format
622   :type 'string)
623
624 (defcustom gnus-list-identifiers nil
625   "Regexp that matches list identifiers to be removed from subject.
626 This can also be a list of regexps."
627   :version "21.1"
628   :group 'gnus-summary-format
629   :group 'gnus-article-hiding
630   :type '(choice (const :tag "none" nil)
631                  (regexp :value ".*")
632                  (repeat :value (".*") regexp)))
633
634 (defcustom gnus-summary-mark-below 0
635   "*Mark all articles with a score below this variable as read.
636 This variable is local to each summary buffer and usually set by the
637 score file."
638   :group 'gnus-score-default
639   :type 'integer)
640
641 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
642   "*List of functions used for sorting articles in the summary buffer.
643
644 Each function takes two articles and returns non-nil if the first
645 article should be sorted before the other.  If you use more than one
646 function, the primary sort function should be the last.  You should
647 probably always include `gnus-article-sort-by-number' in the list of
648 sorting functions -- preferably first.  Also note that sorting by date
649 is often much slower than sorting by number, and the sorting order is
650 very similar.  (Sorting by date means sorting by the time the message
651 was sent, sorting by number means sorting by arrival time.)
652
653 Ready-made functions include `gnus-article-sort-by-number',
654 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
655 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
656
657 When threading is turned on, the variable `gnus-thread-sort-functions'
658 controls how articles are sorted."
659   :group 'gnus-summary-sort
660   :type '(repeat (choice (function-item gnus-article-sort-by-number)
661                          (function-item gnus-article-sort-by-author)
662                          (function-item gnus-article-sort-by-subject)
663                          (function-item gnus-article-sort-by-date)
664                          (function-item gnus-article-sort-by-score)
665                          (function :tag "other"))))
666
667 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
668   "*List of functions used for sorting threads in the summary buffer.
669 By default, threads are sorted by article number.
670
671 Each function takes two threads and returns non-nil if the first
672 thread should be sorted before the other.  If you use more than one
673 function, the primary sort function should be the last.  You should
674 probably always include `gnus-thread-sort-by-number' in the list of
675 sorting functions -- preferably first.  Also note that sorting by date
676 is often much slower than sorting by number, and the sorting order is
677 very similar.  (Sorting by date means sorting by the time the message
678 was sent, sorting by number means sorting by arrival time.)
679
680 Ready-made functions include `gnus-thread-sort-by-number',
681 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
682 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
683 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
684
685 When threading is turned off, the variable
686 `gnus-article-sort-functions' controls how articles are sorted."
687   :group 'gnus-summary-sort
688   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
689                          (function-item gnus-thread-sort-by-author)
690                          (function-item gnus-thread-sort-by-subject)
691                          (function-item gnus-thread-sort-by-date)
692                          (function-item gnus-thread-sort-by-score)
693                          (function-item gnus-thread-sort-by-total-score)
694                          (function :tag "other"))))
695
696 (defcustom gnus-thread-score-function '+
697   "*Function used for calculating the total score of a thread.
698
699 The function is called with the scores of the article and each
700 subthread and should then return the score of the thread.
701
702 Some functions you can use are `+', `max', or `min'."
703   :group 'gnus-summary-sort
704   :type 'function)
705
706 (defcustom gnus-summary-expunge-below nil
707   "All articles that have a score less than this variable will be expunged.
708 This variable is local to the summary buffers."
709   :group 'gnus-score-default
710   :type '(choice (const :tag "off" nil)
711                  integer))
712
713 (defcustom gnus-thread-expunge-below nil
714   "All threads that have a total score less than this variable will be expunged.
715 See `gnus-thread-score-function' for en explanation of what a
716 \"thread score\" is.
717
718 This variable is local to the summary buffers."
719   :group 'gnus-threading
720   :group 'gnus-score-default
721   :type '(choice (const :tag "off" nil)
722                  integer))
723
724 (defcustom gnus-summary-mode-hook nil
725   "*A hook for Gnus summary mode.
726 This hook is run before any variables are set in the summary buffer."
727   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
728   :group 'gnus-summary-various
729   :type 'hook)
730
731 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
732 (when (featurep 'xemacs)
733   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
734   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
735   (add-hook 'gnus-summary-mode-hook
736             'gnus-xmas-switch-horizontal-scrollbar-off))
737
738 (defcustom gnus-summary-menu-hook nil
739   "*Hook run after the creation of the summary mode menu."
740   :group 'gnus-summary-visual
741   :type 'hook)
742
743 (defcustom gnus-summary-exit-hook nil
744   "*A hook called on exit from the summary buffer.
745 It will be called with point in the group buffer."
746   :group 'gnus-summary-exit
747   :type 'hook)
748
749 (defcustom gnus-summary-prepare-hook nil
750   "*A hook called after the summary buffer has been generated.
751 If you want to modify the summary buffer, you can use this hook."
752   :group 'gnus-summary-various
753   :type 'hook)
754
755 (defcustom gnus-summary-prepared-hook nil
756   "*A hook called as the last thing after the summary buffer has been generated."
757   :group 'gnus-summary-various
758   :type 'hook)
759
760 (defcustom gnus-summary-generate-hook nil
761   "*A hook run just before generating the summary buffer.
762 This hook is commonly used to customize threading variables and the
763 like."
764   :group 'gnus-summary-various
765   :type 'hook)
766
767 (defcustom gnus-select-group-hook nil
768   "*A hook called when a newsgroup is selected.
769
770 If you'd like to simplify subjects like the
771 `gnus-summary-next-same-subject' command does, you can use the
772 following hook:
773
774  (setq gnus-select-group-hook
775       (list
776         (lambda ()
777           (mapcar (lambda (header)
778                      (mail-header-set-subject
779                       header
780                       (gnus-simplify-subject
781                        (mail-header-subject header) 're-only)))
782                   gnus-newsgroup-headers))))"
783   :group 'gnus-group-select
784   :type 'hook)
785
786 (defcustom gnus-select-article-hook nil
787   "*A hook called when an article is selected."
788   :group 'gnus-summary-choose
789   :type 'hook)
790
791 (defcustom gnus-visual-mark-article-hook
792   (list 'gnus-highlight-selected-summary)
793   "*Hook run after selecting an article in the summary buffer.
794 It is meant to be used for highlighting the article in some way.  It
795 is not run if `gnus-visual' is nil."
796   :group 'gnus-summary-visual
797   :type 'hook)
798
799 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
800   "*A hook called before parsing the headers."
801   :group 'gnus-various
802   :type 'hook)
803
804 (defcustom gnus-exit-group-hook nil
805   "*A hook called when exiting summary mode.
806 This hook is not called from the non-updating exit commands like `Q'."
807   :group 'gnus-various
808   :type 'hook)
809
810 (defcustom gnus-summary-update-hook
811   (list 'gnus-summary-highlight-line)
812   "*A hook called when a summary line is changed.
813 The hook will not be called if `gnus-visual' is nil.
814
815 The default function `gnus-summary-highlight-line' will
816 highlight the line according to the `gnus-summary-highlight'
817 variable."
818   :group 'gnus-summary-visual
819   :type 'hook)
820
821 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
822   "*A hook called when an article is selected for the first time.
823 The hook is intended to mark an article as read (or unread)
824 automatically when it is selected."
825   :group 'gnus-summary-choose
826   :type 'hook)
827
828 (defcustom gnus-group-no-more-groups-hook nil
829   "*A hook run when returning to group mode having no more (unread) groups."
830   :group 'gnus-group-select
831   :type 'hook)
832
833 (defcustom gnus-ps-print-hook nil
834   "*A hook run before ps-printing something from Gnus."
835   :group 'gnus-summary
836   :type 'hook)
837
838 (defcustom gnus-summary-display-arrow
839   (and (fboundp 'display-graphic-p)
840        (display-graphic-p))
841   "*If non-nil, display an arrow highlighting the current article."
842   :version "21.1"
843   :group 'gnus-summary
844   :type 'boolean)
845
846 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
847   "Face used for highlighting the current article in the summary buffer."
848   :group 'gnus-summary-visual
849   :type 'face)
850
851 (defcustom gnus-summary-highlight
852   '(((= mark gnus-canceled-mark)
853      . gnus-summary-cancelled-face)
854     ((and (> score default-high)
855           (or (= mark gnus-dormant-mark)
856               (= mark gnus-ticked-mark)))
857      . gnus-summary-high-ticked-face)
858     ((and (< score default-low)
859           (or (= mark gnus-dormant-mark)
860               (= mark gnus-ticked-mark)))
861      . gnus-summary-low-ticked-face)
862     ((or (= mark gnus-dormant-mark)
863          (= mark gnus-ticked-mark))
864      . gnus-summary-normal-ticked-face)
865     ((and (> score default-high) (= mark gnus-ancient-mark))
866      . gnus-summary-high-ancient-face)
867     ((and (< score default-low) (= mark gnus-ancient-mark))
868      . gnus-summary-low-ancient-face)
869     ((= mark gnus-ancient-mark)
870      . gnus-summary-normal-ancient-face)
871     ((and (> score default-high) (= mark gnus-unread-mark))
872      . gnus-summary-high-unread-face)
873     ((and (< score default-low) (= mark gnus-unread-mark))
874      . gnus-summary-low-unread-face)
875     ((= mark gnus-unread-mark)
876      . gnus-summary-normal-unread-face)
877     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
878                                                   gnus-undownloaded-mark)))
879      . gnus-summary-high-unread-face)
880     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
881                                                  gnus-undownloaded-mark)))
882      . gnus-summary-low-unread-face)
883     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
884           (memq article gnus-newsgroup-unreads))
885      . gnus-summary-normal-unread-face)
886     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
887      . gnus-summary-normal-read-face)
888     ((> score default-high)
889      . gnus-summary-high-read-face)
890     ((< score default-low)
891      . gnus-summary-low-read-face)
892     (t
893      . gnus-summary-normal-read-face))
894   "*Controls the highlighting of summary buffer lines.
895
896 A list of (FORM . FACE) pairs.  When deciding how a a particular
897 summary line should be displayed, each form is evaluated.  The content
898 of the face field after the first true form is used.  You can change
899 how those summary lines are displayed, by editing the face field.
900
901 You can use the following variables in the FORM field.
902
903 score:        The article's score
904 default:      The default article score.
905 default-high: The default score for high scored articles.
906 default-low:  The default score for low scored articles.
907 below:        The score below which articles are automatically marked as read.
908 mark:         The articles mark."
909   :group 'gnus-summary-visual
910   :type '(repeat (cons (sexp :tag "Form" nil)
911                        face)))
912
913 (defcustom gnus-alter-header-function nil
914   "Function called to allow alteration of article header structures.
915 The function is called with one parameter, the article header vector,
916 which it may alter in any way.")
917
918 (defvar gnus-decode-encoded-word-function
919   (mime-find-field-decoder 'From 'nov)
920   "Variable that says which function should be used to decode a string with encoded words.")
921
922 (defcustom gnus-extra-headers nil
923   "*Extra headers to parse."
924   :version "21.1"
925   :group 'gnus-summary
926   :type '(repeat symbol))
927
928 (defcustom gnus-ignored-from-addresses
929   (and user-mail-address (regexp-quote user-mail-address))
930   "*Regexp of From headers that may be suppressed in favor of To headers."
931   :version "21.1"
932   :group 'gnus-summary
933   :type 'regexp)
934
935 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
936   "List of charsets that should be ignored.
937 When these charsets are used in the \"charset\" parameter, the
938 default charset will be used instead."
939   :version "21.1"
940   :type '(repeat symbol)
941   :group 'gnus-charset)
942
943 (gnus-define-group-parameter
944  ignored-charsets
945  :type list
946  :function-document
947  "Return the ignored charsets of GROUP."
948  :variable gnus-group-ignored-charsets-alist
949  :variable-default
950  '(("alt\\.chinese\\.text" iso-8859-1))
951  :variable-document
952  "Alist of regexps (to match group names) and charsets that should be ignored.
953 When these charsets are used in the \"charset\" parameter, the
954 default charset will be used instead."
955  :variable-group gnus-charset
956  :variable-type '(repeat (cons (regexp :tag "Group")
957                                (repeat symbol)))
958  :parameter-type '(choice :tag "Ignored charsets"
959                           :value nil
960                           (repeat (symbol)))
961  :parameter-document       "\
962 List of charsets that should be ignored.
963
964 When these charsets are used in the \"charset\" parameter, the
965 default charset will be used instead.")
966
967 (defcustom gnus-group-highlight-words-alist nil
968   "Alist of group regexps and highlight regexps.
969 This variable uses the same syntax as `gnus-emphasis-alist'."
970   :version "21.1"
971   :type '(repeat (cons (regexp :tag "Group")
972                        (repeat (list (regexp :tag "Highlight regexp")
973                                      (number :tag "Group for entire word" 0)
974                                      (number :tag "Group for displayed part" 0)
975                                      (symbol :tag "Face"
976                                              gnus-emphasis-highlight-words)))))
977   :group 'gnus-summary-visual)
978
979 (defcustom gnus-use-wheel nil
980   "Use Intelli-mouse on summary movement"
981   :type 'boolean
982   :group 'gnus-summary-maneuvering)
983
984 (defcustom gnus-wheel-scroll-amount '(5 . 1)
985   "Amount to scroll messages by spinning the mouse wheel.
986 This is actually a cons cell, where the first item is the amount to scroll
987 on a normal wheel event, and the second is the amount to scroll when the
988 wheel is moved with the shift key depressed."
989   :type '(cons (integer :tag "Shift") integer)
990   :group 'gnus-summary-maneuvering)
991
992 (defcustom gnus-wheel-edge-resistance 2
993   "How hard it should be to change the current article
994 by moving the mouse over the edge of the article window."
995   :type 'integer
996   :group 'gnus-summary-maneuvering)
997
998 (defcustom gnus-summary-show-article-charset-alist
999   nil
1000   "Alist of number and charset.
1001 The article will be shown with the charset corresponding to the
1002 numbered argument.
1003 For example: ((1 . cn-gb-2312) (2 . big5))."
1004   :version "21.1"
1005   :type '(repeat (cons (number :tag "Argument" 1)
1006                        (symbol :tag "Charset")))
1007   :group 'gnus-charset)
1008
1009 (defcustom gnus-preserve-marks t
1010   "Whether marks are preserved when moving, copying and respooling messages."
1011   :version "21.1"
1012   :type 'boolean
1013   :group 'gnus-summary-marks)
1014
1015 (defcustom gnus-alter-articles-to-read-function nil
1016   "Function to be called to alter the list of articles to be selected."
1017   :type '(choice (const nil) function)
1018   :group 'gnus-summary)
1019
1020 (defcustom gnus-orphan-score nil
1021   "*All orphans get this score added.  Set in the score file."
1022   :group 'gnus-score-default
1023   :type '(choice (const nil)
1024                  integer))
1025
1026 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1027   "*A regexp to match MIME parts when saving multiple parts of a message
1028 with gnus-summary-save-parts (X m). This regexp will be used by default
1029 when prompting the user for which type of files to save."
1030   :group 'gnus-summary
1031   :type 'regexp)
1032
1033
1034 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1035   "*A regexp to match MIME parts when saving multiple parts of a message
1036 with gnus-summary-save-parts (X m). This regexp will be used by default
1037 when prompting the user for which type of files to save."
1038   :group 'gnus-summary
1039   :type 'regexp)
1040
1041 (defcustom gnus-read-all-available-headers nil
1042   "Whether Gnus should parse all headers made available to it.
1043 This is mostly relevant for slow backends where the user may
1044 wish to widen the summary buffer to include all headers
1045 that were fetched.  Say, for nnultimate groups."
1046   :group 'gnus-summary
1047   :type '(choice boolean regexp))
1048
1049 (defcustom gnus-summary-muttprint-program "muttprint"
1050   "Command (and optional arguments) used to run Muttprint."
1051   :group 'gnus-summary
1052   :type 'string)
1053
1054 ;;; Internal variables
1055
1056 (defvar gnus-summary-display-cache nil)
1057 (defvar gnus-article-mime-handles nil)
1058 (defvar gnus-article-decoded-p nil)
1059 (defvar gnus-article-charset nil)
1060 (defvar gnus-article-ignored-charsets nil)
1061 (defvar gnus-scores-exclude-files nil)
1062 (defvar gnus-page-broken nil)
1063 (defvar gnus-inhibit-mime-unbuttonizing nil)
1064
1065 (defvar gnus-original-article nil)
1066 (defvar gnus-article-internal-prepare-hook nil)
1067 (defvar gnus-newsgroup-process-stack nil)
1068
1069 (defvar gnus-thread-indent-array nil)
1070 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1071 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1072   "Function called to sort the articles within a thread after it has been gathered together.")
1073
1074 (defvar gnus-summary-save-parts-type-history nil)
1075 (defvar gnus-summary-save-parts-last-directory nil)
1076
1077 (defvar gnus-summary-save-parts-type-history nil)
1078 (defvar gnus-summary-save-parts-last-directory nil)
1079
1080 ;; Avoid highlighting in kill files.
1081 (defvar gnus-summary-inhibit-highlight nil)
1082 (defvar gnus-newsgroup-selected-overlay nil)
1083 (defvar gnus-inhibit-limiting nil)
1084 (defvar gnus-newsgroup-adaptive-score-file nil)
1085 (defvar gnus-current-score-file nil)
1086 (defvar gnus-current-move-group nil)
1087 (defvar gnus-current-copy-group nil)
1088 (defvar gnus-current-crosspost-group nil)
1089 (defvar gnus-newsgroup-display nil)
1090
1091 (defvar gnus-newsgroup-dependencies nil)
1092 (defvar gnus-newsgroup-adaptive nil)
1093 (defvar gnus-summary-display-article-function nil)
1094 (defvar gnus-summary-highlight-line-function nil
1095   "Function called after highlighting a summary line.")
1096
1097 (defvar gnus-summary-line-format-alist
1098   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1099     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1100     (?s gnus-tmp-subject-or-nil ?s)
1101     (?n gnus-tmp-name ?s)
1102     (?A (std11-address-string
1103          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1104     (?a (or (std11-full-name-string
1105              (car (mime-entity-read-field gnus-tmp-header 'From)))
1106             gnus-tmp-from) ?s)
1107     (?F gnus-tmp-from ?s)
1108     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1109     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1110     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1111     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1112     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1113     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1114     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1115     (?L gnus-tmp-lines ?s)
1116     (?I gnus-tmp-indentation ?s)
1117     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1118     (?R gnus-tmp-replied ?c)
1119     (?\[ gnus-tmp-opening-bracket ?c)
1120     (?\] gnus-tmp-closing-bracket ?c)
1121     (?\> (make-string gnus-tmp-level ? ) ?s)
1122     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1123     (?i gnus-tmp-score ?d)
1124     (?z gnus-tmp-score-char ?c)
1125     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1126     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1127     (?U gnus-tmp-unread ?c)
1128     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1129         ?s)
1130     (?t (gnus-summary-number-of-articles-in-thread
1131          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1132         ?d)
1133     (?e (gnus-summary-number-of-articles-in-thread
1134          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1135         ?c)
1136     (?u gnus-tmp-user-defined ?s)
1137     (?P (gnus-pick-line-number) ?d)
1138     (?B gnus-tmp-thread-tree-header-string ?s)
1139     (user-date (gnus-user-date
1140                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1141   "An alist of format specifications that can appear in summary lines.
1142 These are paired with what variables they correspond with, along with
1143 the type of the variable (string, integer, character, etc).")
1144
1145 (defvar gnus-summary-dummy-line-format-alist
1146   `((?S gnus-tmp-subject ?s)
1147     (?N gnus-tmp-number ?d)
1148     (?u gnus-tmp-user-defined ?s)))
1149
1150 (defvar gnus-summary-mode-line-format-alist
1151   `((?G gnus-tmp-group-name ?s)
1152     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1153     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1154     (?A gnus-tmp-article-number ?d)
1155     (?Z gnus-tmp-unread-and-unselected ?s)
1156     (?V gnus-version ?s)
1157     (?U gnus-tmp-unread-and-unticked ?d)
1158     (?S gnus-tmp-subject ?s)
1159     (?e gnus-tmp-unselected ?d)
1160     (?u gnus-tmp-user-defined ?s)
1161     (?d (length gnus-newsgroup-dormant) ?d)
1162     (?t (length gnus-newsgroup-marked) ?d)
1163     (?r (length gnus-newsgroup-reads) ?d)
1164     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1165     (?E gnus-newsgroup-expunged-tally ?d)
1166     (?s (gnus-current-score-file-nondirectory) ?s)))
1167
1168 (defvar gnus-last-search-regexp nil
1169   "Default regexp for article search command.")
1170
1171 (defvar gnus-summary-search-article-matched-data nil
1172   "Last matched data of article search command.  It is the local variable
1173 in `gnus-article-buffer' which consists of the list of start position,
1174 end position and text.")
1175
1176 (defvar gnus-last-shell-command nil
1177   "Default shell command on article.")
1178
1179 (defvar gnus-newsgroup-begin nil)
1180 (defvar gnus-newsgroup-end nil)
1181 (defvar gnus-newsgroup-last-rmail nil)
1182 (defvar gnus-newsgroup-last-mail nil)
1183 (defvar gnus-newsgroup-last-folder nil)
1184 (defvar gnus-newsgroup-last-file nil)
1185 (defvar gnus-newsgroup-auto-expire nil)
1186 (defvar gnus-newsgroup-active nil)
1187
1188 (defvar gnus-newsgroup-data nil)
1189 (defvar gnus-newsgroup-data-reverse nil)
1190 (defvar gnus-newsgroup-limit nil)
1191 (defvar gnus-newsgroup-limits nil)
1192
1193 (defvar gnus-newsgroup-unreads nil
1194   "List of unread articles in the current newsgroup.")
1195
1196 (defvar gnus-newsgroup-unselected nil
1197   "List of unselected unread articles in the current newsgroup.")
1198
1199 (defvar gnus-newsgroup-reads nil
1200   "Alist of read articles and article marks in the current newsgroup.")
1201
1202 (defvar gnus-newsgroup-expunged-tally nil)
1203
1204 (defvar gnus-newsgroup-marked nil
1205   "List of ticked articles in the current newsgroup (a subset of unread art).")
1206
1207 (defvar gnus-newsgroup-killed nil
1208   "List of ranges of articles that have been through the scoring process.")
1209
1210 (defvar gnus-newsgroup-cached nil
1211   "List of articles that come from the article cache.")
1212
1213 (defvar gnus-newsgroup-saved nil
1214   "List of articles that have been saved.")
1215
1216 (defvar gnus-newsgroup-kill-headers nil)
1217
1218 (defvar gnus-newsgroup-replied nil
1219   "List of articles that have been replied to in the current newsgroup.")
1220
1221 (defvar gnus-newsgroup-forwarded nil
1222   "List of articles that have been forwarded in the current newsgroup.")
1223
1224 (defvar gnus-newsgroup-recent nil
1225   "List of articles that have are recent in the current newsgroup.")
1226
1227 (defvar gnus-newsgroup-expirable nil
1228   "List of articles in the current newsgroup that can be expired.")
1229
1230 (defvar gnus-newsgroup-processable nil
1231   "List of articles in the current newsgroup that can be processed.")
1232
1233 (defvar gnus-newsgroup-downloadable nil
1234   "List of articles in the current newsgroup that can be processed.")
1235
1236 (defvar gnus-newsgroup-undownloaded nil
1237   "List of articles in the current newsgroup that haven't been downloaded..")
1238
1239 (defvar gnus-newsgroup-unsendable nil
1240   "List of articles in the current newsgroup that won't be sent.")
1241
1242 (defvar gnus-newsgroup-bookmarks nil
1243   "List of articles in the current newsgroup that have bookmarks.")
1244
1245 (defvar gnus-newsgroup-dormant nil
1246   "List of dormant articles in the current newsgroup.")
1247
1248 (defvar gnus-newsgroup-unseen nil
1249   "List of unseen articles in the current newsgroup.")
1250
1251 (defvar gnus-newsgroup-seen nil
1252   "Range of seen articles in the current newsgroup.")
1253
1254 (defvar gnus-newsgroup-articles nil
1255   "List of articles in the current newsgroup.")
1256
1257 (defvar gnus-newsgroup-scored nil
1258   "List of scored articles in the current newsgroup.")
1259
1260 (defvar gnus-newsgroup-incorporated nil
1261   "List of incorporated articles in the current newsgroup.")
1262
1263 (defvar gnus-newsgroup-headers nil
1264   "List of article headers in the current newsgroup.")
1265
1266 (defvar gnus-newsgroup-threads nil)
1267
1268 (defvar gnus-newsgroup-prepared nil
1269   "Whether the current group has been prepared properly.")
1270
1271 (defvar gnus-newsgroup-ancient nil
1272   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1273
1274 (defvar gnus-newsgroup-sparse nil)
1275
1276 (defvar gnus-current-article nil)
1277 (defvar gnus-article-current nil)
1278 (defvar gnus-current-headers nil)
1279 (defvar gnus-have-all-headers nil)
1280 (defvar gnus-last-article nil)
1281 (defvar gnus-newsgroup-history nil)
1282 (defvar gnus-newsgroup-charset nil)
1283 (defvar gnus-newsgroup-ephemeral-charset nil)
1284 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1285
1286 (defvar gnus-article-before-search nil)
1287
1288 (defconst gnus-summary-local-variables
1289   '(gnus-newsgroup-name
1290     gnus-newsgroup-begin gnus-newsgroup-end
1291     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1292     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1293     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1294     gnus-newsgroup-unselected gnus-newsgroup-marked
1295     gnus-newsgroup-reads gnus-newsgroup-saved
1296     gnus-newsgroup-replied gnus-newsgroup-forwarded
1297     gnus-newsgroup-recent
1298     gnus-newsgroup-expirable
1299     gnus-newsgroup-processable gnus-newsgroup-killed
1300     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1301     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1302     gnus-newsgroup-seen gnus-newsgroup-articles
1303     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1304     gnus-newsgroup-headers gnus-newsgroup-threads
1305     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1306     gnus-current-article gnus-current-headers gnus-have-all-headers
1307     gnus-last-article gnus-article-internal-prepare-hook
1308     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1309     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1310     gnus-thread-expunge-below
1311     gnus-score-alist gnus-current-score-file
1312     (gnus-summary-expunge-below . global)
1313     (gnus-summary-mark-below . global)
1314     (gnus-orphan-score . global)
1315     gnus-newsgroup-active gnus-scores-exclude-files
1316     gnus-newsgroup-history gnus-newsgroup-ancient
1317     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1318     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1319     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1320     (gnus-newsgroup-expunged-tally . 0)
1321     gnus-cache-removable-articles gnus-newsgroup-cached
1322     gnus-newsgroup-data gnus-newsgroup-data-reverse
1323     gnus-newsgroup-limit gnus-newsgroup-limits
1324     gnus-newsgroup-charset gnus-newsgroup-display
1325     gnus-newsgroup-incorporated)
1326   "Variables that are buffer-local to the summary buffers.")
1327
1328 (defvar gnus-newsgroup-variables nil
1329   "A list of variables that have separate values in different newsgroups.
1330 A list of newsgroup (summary buffer) local variables, or cons of
1331 variables and their default values (when the default values are not
1332 nil), that should be made global while the summary buffer is active.
1333 These variables can be used to set variables in the group parameters
1334 while still allowing them to affect operations done in other
1335 buffers. For example:
1336
1337 \(setq gnus-newsgroup-variables
1338      '(message-use-followup-to
1339        (gnus-visible-headers .
1340          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1341 ")
1342
1343 ;; Byte-compiler warning.
1344 (eval-when-compile (defvar gnus-article-mode-map))
1345
1346 ;; Subject simplification.
1347
1348 (defun gnus-simplify-whitespace (str)
1349   "Remove excessive whitespace from STR."
1350   (let ((mystr str))
1351     ;; Multiple spaces.
1352     (while (string-match "[ \t][ \t]+" mystr)
1353       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1354                           " "
1355                           (substring mystr (match-end 0)))))
1356     ;; Leading spaces.
1357     (when (string-match "^[ \t]+" mystr)
1358       (setq mystr (substring mystr (match-end 0))))
1359     ;; Trailing spaces.
1360     (when (string-match "[ \t]+$" mystr)
1361       (setq mystr (substring mystr 0 (match-beginning 0))))
1362     mystr))
1363
1364 (defsubst gnus-simplify-subject-re (subject)
1365   "Remove \"Re:\" from subject lines."
1366   (if (string-match message-subject-re-regexp subject)
1367       (substring subject (match-end 0))
1368     subject))
1369
1370 (defun gnus-simplify-subject (subject &optional re-only)
1371   "Remove `Re:' and words in parentheses.
1372 If RE-ONLY is non-nil, strip leading `Re:'s only."
1373   (let ((case-fold-search t))           ;Ignore case.
1374     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1375     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1376       (setq subject (substring subject (match-end 0))))
1377     ;; Remove uninteresting prefixes.
1378     (when (and (not re-only)
1379                gnus-simplify-ignored-prefixes
1380                (string-match gnus-simplify-ignored-prefixes subject))
1381       (setq subject (substring subject (match-end 0))))
1382     ;; Remove words in parentheses from end.
1383     (unless re-only
1384       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1385         (setq subject (substring subject 0 (match-beginning 0)))))
1386     ;; Return subject string.
1387     subject))
1388
1389 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1390 ;; all whitespace.
1391 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1392   (goto-char (point-min))
1393   (while (re-search-forward regexp nil t)
1394     (replace-match (or newtext ""))))
1395
1396 (defun gnus-simplify-buffer-fuzzy ()
1397   "Simplify string in the buffer fuzzily.
1398 The string in the accessible portion of the current buffer is simplified.
1399 It is assumed to be a single-line subject.
1400 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1401 matter is removed.  Additional things can be deleted by setting
1402 `gnus-simplify-subject-fuzzy-regexp'."
1403   (let ((case-fold-search t)
1404         (modified-tick))
1405     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1406
1407     (while (not (eq modified-tick (buffer-modified-tick)))
1408       (setq modified-tick (buffer-modified-tick))
1409       (cond
1410        ((listp gnus-simplify-subject-fuzzy-regexp)
1411         (mapcar 'gnus-simplify-buffer-fuzzy-step
1412                 gnus-simplify-subject-fuzzy-regexp))
1413        (gnus-simplify-subject-fuzzy-regexp
1414         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1415       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1416       (gnus-simplify-buffer-fuzzy-step
1417        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1418       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1419
1420     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1421     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1422     (gnus-simplify-buffer-fuzzy-step " $")
1423     (gnus-simplify-buffer-fuzzy-step "^ +")))
1424
1425 (defun gnus-simplify-subject-fuzzy (subject)
1426   "Simplify a subject string fuzzily.
1427 See `gnus-simplify-buffer-fuzzy' for details."
1428   (save-excursion
1429     (gnus-set-work-buffer)
1430     (let ((case-fold-search t))
1431       ;; Remove uninteresting prefixes.
1432       (when (and gnus-simplify-ignored-prefixes
1433                  (string-match gnus-simplify-ignored-prefixes subject))
1434         (setq subject (substring subject (match-end 0))))
1435       (insert subject)
1436       (inline (gnus-simplify-buffer-fuzzy))
1437       (buffer-string))))
1438
1439 (defsubst gnus-simplify-subject-fully (subject)
1440   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1441   (cond
1442    (gnus-simplify-subject-functions
1443     (gnus-map-function gnus-simplify-subject-functions subject))
1444    ((null gnus-summary-gather-subject-limit)
1445     (gnus-simplify-subject-re subject))
1446    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1447     (gnus-simplify-subject-fuzzy subject))
1448    ((numberp gnus-summary-gather-subject-limit)
1449     (gnus-limit-string (gnus-simplify-subject-re subject)
1450                        gnus-summary-gather-subject-limit))
1451    (t
1452     subject)))
1453
1454 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1455   "Check whether two subjects are equal.
1456 If optional argument simple-first is t, first argument is already
1457 simplified."
1458   (cond
1459    ((null simple-first)
1460     (equal (gnus-simplify-subject-fully s1)
1461            (gnus-simplify-subject-fully s2)))
1462    (t
1463     (equal s1
1464            (gnus-simplify-subject-fully s2)))))
1465
1466 (defun gnus-summary-bubble-group ()
1467   "Increase the score of the current group.
1468 This is a handy function to add to `gnus-summary-exit-hook' to
1469 increase the score of each group you read."
1470   (gnus-group-add-score gnus-newsgroup-name))
1471
1472 \f
1473 ;;;
1474 ;;; Gnus summary mode
1475 ;;;
1476
1477 (put 'gnus-summary-mode 'mode-class 'special)
1478
1479 (defvar gnus-article-commands-menu)
1480
1481 (when t
1482   ;; Non-orthogonal keys
1483
1484   (gnus-define-keys gnus-summary-mode-map
1485     " " gnus-summary-next-page
1486     "\177" gnus-summary-prev-page
1487     [delete] gnus-summary-prev-page
1488     [backspace] gnus-summary-prev-page
1489     "\r" gnus-summary-scroll-up
1490     "\M-\r" gnus-summary-scroll-down
1491     "n" gnus-summary-next-unread-article
1492     "p" gnus-summary-prev-unread-article
1493     "N" gnus-summary-next-article
1494     "P" gnus-summary-prev-article
1495     "\M-\C-n" gnus-summary-next-same-subject
1496     "\M-\C-p" gnus-summary-prev-same-subject
1497     "\M-n" gnus-summary-next-unread-subject
1498     "\M-p" gnus-summary-prev-unread-subject
1499     "." gnus-summary-first-unread-article
1500     "," gnus-summary-best-unread-article
1501     "\M-s" gnus-summary-search-article-forward
1502     "\M-r" gnus-summary-search-article-backward
1503     "<" gnus-summary-beginning-of-article
1504     ">" gnus-summary-end-of-article
1505     "j" gnus-summary-goto-article
1506     "^" gnus-summary-refer-parent-article
1507     "\M-^" gnus-summary-refer-article
1508     "u" gnus-summary-tick-article-forward
1509     "!" gnus-summary-tick-article-forward
1510     "U" gnus-summary-tick-article-backward
1511     "d" gnus-summary-mark-as-read-forward
1512     "D" gnus-summary-mark-as-read-backward
1513     "E" gnus-summary-mark-as-expirable
1514     "\M-u" gnus-summary-clear-mark-forward
1515     "\M-U" gnus-summary-clear-mark-backward
1516     "k" gnus-summary-kill-same-subject-and-select
1517     "\C-k" gnus-summary-kill-same-subject
1518     "\M-\C-k" gnus-summary-kill-thread
1519     "\M-\C-l" gnus-summary-lower-thread
1520     "e" gnus-summary-edit-article
1521     "#" gnus-summary-mark-as-processable
1522     "\M-#" gnus-summary-unmark-as-processable
1523     "\M-\C-t" gnus-summary-toggle-threads
1524     "\M-\C-s" gnus-summary-show-thread
1525     "\M-\C-h" gnus-summary-hide-thread
1526     "\M-\C-f" gnus-summary-next-thread
1527     "\M-\C-b" gnus-summary-prev-thread
1528     [(meta down)] gnus-summary-next-thread
1529     [(meta up)] gnus-summary-prev-thread
1530     "\M-\C-u" gnus-summary-up-thread
1531     "\M-\C-d" gnus-summary-down-thread
1532     "&" gnus-summary-execute-command
1533     "c" gnus-summary-catchup-and-exit
1534     "\C-w" gnus-summary-mark-region-as-read
1535     "\C-t" gnus-summary-toggle-truncation
1536     "?" gnus-summary-mark-as-dormant
1537     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1538     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1539     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1540     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1541     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1542     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1543     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1544     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1545     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1546     "=" gnus-summary-expand-window
1547     "\C-x\C-s" gnus-summary-reselect-current-group
1548     "\M-g" gnus-summary-rescan-group
1549     "w" gnus-summary-stop-page-breaking
1550     "\C-c\C-r" gnus-summary-caesar-message
1551     "\M-t" gnus-summary-toggle-mime
1552     "f" gnus-summary-followup
1553     "F" gnus-summary-followup-with-original
1554     "C" gnus-summary-cancel-article
1555     "r" gnus-summary-reply
1556     "R" gnus-summary-reply-with-original
1557     "\C-c\C-f" gnus-summary-mail-forward
1558     "o" gnus-summary-save-article
1559     "\C-o" gnus-summary-save-article-mail
1560     "|" gnus-summary-pipe-output
1561     "\M-k" gnus-summary-edit-local-kill
1562     "\M-K" gnus-summary-edit-global-kill
1563     ;; "V" gnus-version
1564     "\C-c\C-d" gnus-summary-describe-group
1565     "q" gnus-summary-exit
1566     "Q" gnus-summary-exit-no-update
1567     "\C-c\C-i" gnus-info-find-node
1568     gnus-mouse-2 gnus-mouse-pick-article
1569     "m" gnus-summary-mail-other-window
1570     "a" gnus-summary-post-news
1571     "i" gnus-summary-news-other-window
1572     "x" gnus-summary-limit-to-unread
1573     "s" gnus-summary-isearch-article
1574     "t" gnus-article-toggle-headers
1575     "g" gnus-summary-show-article
1576     "l" gnus-summary-goto-last-article
1577     "v" gnus-summary-preview-mime-message
1578     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1579     "\C-d" gnus-summary-enter-digest-group
1580     "\M-\C-d" gnus-summary-read-document
1581     "\M-\C-e" gnus-summary-edit-parameters
1582     "\M-\C-a" gnus-summary-customize-parameters
1583     "\C-c\C-b" gnus-bug
1584     "\C-c\C-n" gnus-namazu-search
1585     "*" gnus-cache-enter-article
1586     "\M-*" gnus-cache-remove-article
1587     "\M-&" gnus-summary-universal-argument
1588     "\C-l" gnus-recenter
1589     "I" gnus-summary-increase-score
1590     "L" gnus-summary-lower-score
1591     "\M-i" gnus-symbolic-argument
1592     "h" gnus-summary-select-article-buffer
1593
1594     "V" gnus-summary-score-map
1595     "X" gnus-uu-extract-map
1596     "S" gnus-summary-send-map)
1597
1598   ;; Sort of orthogonal keymap
1599   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1600     "t" gnus-summary-tick-article-forward
1601     "!" gnus-summary-tick-article-forward
1602     "d" gnus-summary-mark-as-read-forward
1603     "r" gnus-summary-mark-as-read-forward
1604     "c" gnus-summary-clear-mark-forward
1605     " " gnus-summary-clear-mark-forward
1606     "e" gnus-summary-mark-as-expirable
1607     "x" gnus-summary-mark-as-expirable
1608     "?" gnus-summary-mark-as-dormant
1609     "b" gnus-summary-set-bookmark
1610     "B" gnus-summary-remove-bookmark
1611     "#" gnus-summary-mark-as-processable
1612     "\M-#" gnus-summary-unmark-as-processable
1613     "S" gnus-summary-limit-include-expunged
1614     "C" gnus-summary-catchup
1615     "H" gnus-summary-catchup-to-here
1616     "h" gnus-summary-catchup-from-here
1617     "\C-c" gnus-summary-catchup-all
1618     "k" gnus-summary-kill-same-subject-and-select
1619     "K" gnus-summary-kill-same-subject
1620     "P" gnus-uu-mark-map)
1621
1622   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1623     "c" gnus-summary-clear-above
1624     "u" gnus-summary-tick-above
1625     "m" gnus-summary-mark-above
1626     "k" gnus-summary-kill-below)
1627
1628   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1629     "/" gnus-summary-limit-to-subject
1630     "n" gnus-summary-limit-to-articles
1631     "w" gnus-summary-pop-limit
1632     "s" gnus-summary-limit-to-subject
1633     "a" gnus-summary-limit-to-author
1634     "u" gnus-summary-limit-to-unread
1635     "m" gnus-summary-limit-to-marks
1636     "M" gnus-summary-limit-exclude-marks
1637     "v" gnus-summary-limit-to-score
1638     "*" gnus-summary-limit-include-cached
1639     "D" gnus-summary-limit-include-dormant
1640     "T" gnus-summary-limit-include-thread
1641     "d" gnus-summary-limit-exclude-dormant
1642     "t" gnus-summary-limit-to-age
1643     "x" gnus-summary-limit-to-extra
1644     "p" gnus-summary-limit-to-display-predicate
1645     "E" gnus-summary-limit-include-expunged
1646     "c" gnus-summary-limit-exclude-childless-dormant
1647     "C" gnus-summary-limit-mark-excluded-as-read
1648     "o" gnus-summary-insert-old-articles
1649     "N" gnus-summary-insert-new-articles)
1650
1651   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1652     "n" gnus-summary-next-unread-article
1653     "p" gnus-summary-prev-unread-article
1654     "N" gnus-summary-next-article
1655     "P" gnus-summary-prev-article
1656     "\C-n" gnus-summary-next-same-subject
1657     "\C-p" gnus-summary-prev-same-subject
1658     "\M-n" gnus-summary-next-unread-subject
1659     "\M-p" gnus-summary-prev-unread-subject
1660     "f" gnus-summary-first-unread-article
1661     "b" gnus-summary-best-unread-article
1662     "j" gnus-summary-goto-article
1663     "g" gnus-summary-goto-subject
1664     "l" gnus-summary-goto-last-article
1665     "o" gnus-summary-pop-article)
1666
1667   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1668     "k" gnus-summary-kill-thread
1669     "l" gnus-summary-lower-thread
1670     "i" gnus-summary-raise-thread
1671     "T" gnus-summary-toggle-threads
1672     "t" gnus-summary-rethread-current
1673     "^" gnus-summary-reparent-thread
1674     "s" gnus-summary-show-thread
1675     "S" gnus-summary-show-all-threads
1676     "h" gnus-summary-hide-thread
1677     "H" gnus-summary-hide-all-threads
1678     "n" gnus-summary-next-thread
1679     "p" gnus-summary-prev-thread
1680     "u" gnus-summary-up-thread
1681     "o" gnus-summary-top-thread
1682     "d" gnus-summary-down-thread
1683     "#" gnus-uu-mark-thread
1684     "\M-#" gnus-uu-unmark-thread)
1685
1686   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1687     "g" gnus-summary-prepare
1688     "c" gnus-summary-insert-cached-articles)
1689
1690   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1691     "c" gnus-summary-catchup-and-exit
1692     "C" gnus-summary-catchup-all-and-exit
1693     "E" gnus-summary-exit-no-update
1694     "J" gnus-summary-jump-to-other-group
1695     "Q" gnus-summary-exit
1696     "Z" gnus-summary-exit
1697     "n" gnus-summary-catchup-and-goto-next-group
1698     "R" gnus-summary-reselect-current-group
1699     "G" gnus-summary-rescan-group
1700     "N" gnus-summary-next-group
1701     "s" gnus-summary-save-newsrc
1702     "P" gnus-summary-prev-group)
1703
1704   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1705     " " gnus-summary-next-page
1706     "n" gnus-summary-next-page
1707     "\177" gnus-summary-prev-page
1708     [delete] gnus-summary-prev-page
1709     "p" gnus-summary-prev-page
1710     "\r" gnus-summary-scroll-up
1711     "\M-\r" gnus-summary-scroll-down
1712     "<" gnus-summary-beginning-of-article
1713     ">" gnus-summary-end-of-article
1714     "b" gnus-summary-beginning-of-article
1715     "e" gnus-summary-end-of-article
1716     "^" gnus-summary-refer-parent-article
1717     "r" gnus-summary-refer-parent-article
1718     "D" gnus-summary-enter-digest-group
1719     "R" gnus-summary-refer-references
1720     "T" gnus-summary-refer-thread
1721     "g" gnus-summary-show-article
1722     "s" gnus-summary-isearch-article
1723     "P" gnus-summary-print-article
1724     "M" gnus-mailing-list-insinuate
1725     "t" gnus-article-babel)
1726
1727   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1728     "b" gnus-article-add-buttons
1729     "B" gnus-article-add-buttons-to-head
1730     "o" gnus-article-treat-overstrike
1731     "e" gnus-article-emphasize
1732     "w" gnus-article-fill-cited-article
1733     "Q" gnus-article-fill-long-lines
1734     "C" gnus-article-capitalize-sentences
1735     "c" gnus-article-remove-cr
1736     "Z" gnus-article-decode-HZ
1737     "f" gnus-article-display-x-face
1738     "l" gnus-summary-stop-page-breaking
1739     "r" gnus-summary-caesar-message
1740     "t" gnus-article-toggle-headers
1741     "g" gnus-summary-toggle-smiley
1742     "v" gnus-summary-verbose-headers
1743     "m" gnus-summary-toggle-mime
1744     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1745     "p" gnus-article-verify-x-pgp-sig
1746     "d" gnus-article-treat-dumbquotes)
1747
1748   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1749     "a" gnus-article-hide
1750     "h" gnus-article-toggle-headers
1751     "b" gnus-article-hide-boring-headers
1752     "s" gnus-article-hide-signature
1753     "c" gnus-article-hide-citation
1754     "C" gnus-article-hide-citation-in-followups
1755     "l" gnus-article-hide-list-identifiers
1756     "p" gnus-article-hide-pgp
1757     "B" gnus-article-strip-banner
1758     "P" gnus-article-hide-pem
1759     "\C-c" gnus-article-hide-citation-maybe)
1760
1761   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1762     "a" gnus-article-highlight
1763     "h" gnus-article-highlight-headers
1764     "c" gnus-article-highlight-citation
1765     "s" gnus-article-highlight-signature)
1766
1767   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1768     "z" gnus-article-date-ut
1769     "u" gnus-article-date-ut
1770     "l" gnus-article-date-local
1771     "p" gnus-article-date-english
1772     "e" gnus-article-date-lapsed
1773     "o" gnus-article-date-original
1774     "i" gnus-article-date-iso8601
1775     "s" gnus-article-date-user)
1776
1777   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1778     "t" gnus-article-remove-trailing-blank-lines
1779     "l" gnus-article-strip-leading-blank-lines
1780     "m" gnus-article-strip-multiple-blank-lines
1781     "a" gnus-article-strip-blank-lines
1782     "A" gnus-article-strip-all-blank-lines
1783     "s" gnus-article-strip-leading-space
1784     "e" gnus-article-strip-trailing-space
1785     "w" gnus-article-remove-leading-whitespace)
1786
1787   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1788     "v" gnus-version
1789     "f" gnus-summary-fetch-faq
1790     "d" gnus-summary-describe-group
1791     "h" gnus-summary-describe-briefly
1792     "i" gnus-info-find-node)
1793
1794   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1795     "e" gnus-summary-expire-articles
1796     "\M-\C-e" gnus-summary-expire-articles-now
1797     "\177" gnus-summary-delete-article
1798     [delete] gnus-summary-delete-article
1799     [backspace] gnus-summary-delete-article
1800     "m" gnus-summary-move-article
1801     "r" gnus-summary-respool-article
1802     "w" gnus-summary-edit-article
1803     "c" gnus-summary-copy-article
1804     "B" gnus-summary-crosspost-article
1805     "q" gnus-summary-respool-query
1806     "t" gnus-summary-respool-trace
1807     "i" gnus-summary-import-article
1808     "I" gnus-summary-create-article
1809     "p" gnus-summary-article-posted-p)
1810
1811   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1812     "o" gnus-summary-save-article
1813     "m" gnus-summary-save-article-mail
1814     "F" gnus-summary-write-article-file
1815     "r" gnus-summary-save-article-rmail
1816     "f" gnus-summary-save-article-file
1817     "b" gnus-summary-save-article-body-file
1818     "h" gnus-summary-save-article-folder
1819     "v" gnus-summary-save-article-vm
1820     "p" gnus-summary-pipe-output
1821     "P" gnus-summary-muttprint
1822     "s" gnus-soup-add-article)
1823
1824   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1825     "b" gnus-summary-display-buttonized
1826     "m" gnus-summary-repair-multipart
1827     "v" gnus-article-view-part
1828     "o" gnus-article-save-part
1829     "c" gnus-article-copy-part
1830     "C" gnus-article-view-part-as-charset
1831     "e" gnus-article-externalize-part
1832     "E" gnus-article-encrypt-body
1833     "i" gnus-article-inline-part
1834     "|" gnus-article-pipe-part))
1835
1836 (defvar gnus-article-post-menu nil)
1837
1838 (defun gnus-summary-make-menu-bar ()
1839   (gnus-turn-off-edit-menu 'summary)
1840
1841   (unless (boundp 'gnus-summary-misc-menu)
1842
1843     (easy-menu-define
1844      gnus-summary-kill-menu gnus-summary-mode-map ""
1845      (cons
1846       "Score"
1847       (nconc
1848        (list
1849         ["Customize" gnus-score-customize t])
1850        (gnus-make-score-map 'increase)
1851        (gnus-make-score-map 'lower)
1852        '(("Mark"
1853           ["Kill below" gnus-summary-kill-below t]
1854           ["Mark above" gnus-summary-mark-above t]
1855           ["Tick above" gnus-summary-tick-above t]
1856           ["Clear above" gnus-summary-clear-above t])
1857          ["Current score" gnus-summary-current-score t]
1858          ["Set score" gnus-summary-set-score t]
1859          ["Switch current score file..." gnus-score-change-score-file t]
1860          ["Set mark below..." gnus-score-set-mark-below t]
1861          ["Set expunge below..." gnus-score-set-expunge-below t]
1862          ["Edit current score file" gnus-score-edit-current-scores t]
1863          ["Edit score file" gnus-score-edit-file t]
1864          ["Trace score" gnus-score-find-trace t]
1865          ["Find words" gnus-score-find-favourite-words t]
1866          ["Rescore buffer" gnus-summary-rescore t]
1867          ["Increase score..." gnus-summary-increase-score t]
1868          ["Lower score..." gnus-summary-lower-score t]))))
1869
1870     ;; Define both the Article menu in the summary buffer and the equivalent
1871     ;; Commands menu in the article buffer here for consistency.
1872     (let ((innards
1873            `(("Hide"
1874               ["All" gnus-article-hide t]
1875               ["Headers" gnus-article-toggle-headers t]
1876               ["Signature" gnus-article-hide-signature t]
1877               ["Citation" gnus-article-hide-citation t]
1878               ["List identifiers" gnus-article-hide-list-identifiers t]
1879               ["PGP" gnus-article-hide-pgp t]
1880               ["Banner" gnus-article-strip-banner t]
1881               ["Boring headers" gnus-article-hide-boring-headers t])
1882              ("Highlight"
1883               ["All" gnus-article-highlight t]
1884               ["Headers" gnus-article-highlight-headers t]
1885               ["Signature" gnus-article-highlight-signature t]
1886               ["Citation" gnus-article-highlight-citation t])
1887              ("Date"
1888               ["Local" gnus-article-date-local t]
1889               ["ISO8601" gnus-article-date-iso8601 t]
1890               ["UT" gnus-article-date-ut t]
1891               ["Original" gnus-article-date-original t]
1892               ["Lapsed" gnus-article-date-lapsed t]
1893               ["User-defined" gnus-article-date-user t])
1894              ("Washing"
1895               ("Remove Blanks"
1896                ["Leading" gnus-article-strip-leading-blank-lines t]
1897                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1898                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1899                ["All of the above" gnus-article-strip-blank-lines t]
1900                ["All" gnus-article-strip-all-blank-lines t]
1901                ["Leading space" gnus-article-strip-leading-space t]
1902                ["Trailing space" gnus-article-strip-trailing-space t]
1903                ["Leading space in headers"
1904                 gnus-article-remove-leading-whitespace t])
1905               ["Overstrike" gnus-article-treat-overstrike t]
1906               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1907               ["Emphasis" gnus-article-emphasize t]
1908               ["Word wrap" gnus-article-fill-cited-article t]
1909               ["Fill long lines" gnus-article-fill-long-lines t]
1910               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1911               ["CR" gnus-article-remove-cr t]
1912               ["Show X-Face" gnus-article-display-x-face t]
1913               ["Rot 13" gnus-summary-caesar-message
1914                ,@(if (featurep 'xemacs) '(t)
1915                    '(:help "\"Caesar rotate\" article by 13"))]
1916               ["Unix pipe" gnus-summary-pipe-message t]
1917               ["Add buttons" gnus-article-add-buttons t]
1918               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1919               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1920               ["Toggle MIME" gnus-summary-toggle-mime t]
1921               ["Verbose header" gnus-summary-verbose-headers t]
1922               ["Toggle header" gnus-summary-toggle-header t]
1923               ["Toggle smiley" gnus-summary-toggle-smiley t]
1924               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1925               ["HZ" gnus-article-decode-HZ t])
1926              ("Output"
1927               ["Save in default format" gnus-summary-save-article
1928                ,@(if (featurep 'xemacs) '(t)
1929                    '(:help "Save article using default method"))]
1930               ["Save in file" gnus-summary-save-article-file
1931                ,@(if (featurep 'xemacs) '(t)
1932                    '(:help "Save article in file"))]
1933               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1934               ["Save in MH folder" gnus-summary-save-article-folder t]
1935               ["Save in VM folder" gnus-summary-save-article-vm t]
1936               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1937               ["Save body in file" gnus-summary-save-article-body-file t]
1938               ["Pipe through a filter" gnus-summary-pipe-output t]
1939               ["Add to SOUP packet" gnus-soup-add-article t]
1940               ["Print with Muttprint" gnus-summary-muttprint t]
1941               ["Print" gnus-summary-print-article t])
1942              ("Backend"
1943               ["Respool article..." gnus-summary-respool-article t]
1944               ["Move article..." gnus-summary-move-article
1945                (gnus-check-backend-function
1946                 'request-move-article gnus-newsgroup-name)]
1947               ["Copy article..." gnus-summary-copy-article t]
1948               ["Crosspost article..." gnus-summary-crosspost-article
1949                (gnus-check-backend-function
1950                 'request-replace-article gnus-newsgroup-name)]
1951               ["Import file..." gnus-summary-import-article t]
1952               ["Create article..." gnus-summary-create-article t]
1953               ["Check if posted" gnus-summary-article-posted-p t]
1954               ["Edit article" gnus-summary-edit-article
1955                (not (gnus-group-read-only-p))]
1956               ["Delete article" gnus-summary-delete-article
1957                (gnus-check-backend-function
1958                 'request-expire-articles gnus-newsgroup-name)]
1959               ["Query respool" gnus-summary-respool-query t]
1960               ["Trace respool" gnus-summary-respool-trace t]
1961               ["Delete expirable articles" gnus-summary-expire-articles-now
1962                (gnus-check-backend-function
1963                 'request-expire-articles gnus-newsgroup-name)])
1964              ("Extract"
1965               ["Uudecode" gnus-uu-decode-uu
1966                ,@(if (featurep 'xemacs) '(t)
1967                    '(:help "Decode uuencoded article(s)"))]
1968               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1969               ["Unshar" gnus-uu-decode-unshar t]
1970               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1971               ["Save" gnus-uu-decode-save t]
1972               ["Binhex" gnus-uu-decode-binhex t]
1973               ["Postscript" gnus-uu-decode-postscript t])
1974              ("Cache"
1975               ["Enter article" gnus-cache-enter-article t]
1976               ["Remove article" gnus-cache-remove-article t])
1977              ["Translate" gnus-article-babel t]
1978              ["Select article buffer" gnus-summary-select-article-buffer t]
1979              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1980              ["Isearch article..." gnus-summary-isearch-article t]
1981              ["Beginning of the article" gnus-summary-beginning-of-article t]
1982              ["End of the article" gnus-summary-end-of-article t]
1983              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1984              ["Fetch referenced articles" gnus-summary-refer-references t]
1985              ["Fetch current thread" gnus-summary-refer-thread t]
1986              ["Fetch article with id..." gnus-summary-refer-article t]
1987              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1988              ["Redisplay" gnus-summary-show-article t]
1989              ["Raw article" gnus-summary-show-raw-article t])))
1990       (easy-menu-define
1991        gnus-summary-article-menu gnus-summary-mode-map ""
1992        (cons "Article" innards))
1993
1994       (if (not (keymapp gnus-summary-article-menu))
1995           (easy-menu-define
1996            gnus-article-commands-menu gnus-article-mode-map ""
1997            (cons "Commands" innards))
1998         ;; in Emacs, don't share menu.
1999         (setq gnus-article-commands-menu
2000               (copy-keymap gnus-summary-article-menu))
2001         (define-key gnus-article-mode-map [menu-bar commands]
2002           (cons "Commands" gnus-article-commands-menu))))
2003
2004     (easy-menu-define
2005      gnus-summary-thread-menu gnus-summary-mode-map ""
2006      '("Threads"
2007        ["Toggle threading" gnus-summary-toggle-threads t]
2008        ["Hide threads" gnus-summary-hide-all-threads t]
2009        ["Show threads" gnus-summary-show-all-threads t]
2010        ["Hide thread" gnus-summary-hide-thread t]
2011        ["Show thread" gnus-summary-show-thread t]
2012        ["Go to next thread" gnus-summary-next-thread t]
2013        ["Go to previous thread" gnus-summary-prev-thread t]
2014        ["Go down thread" gnus-summary-down-thread t]
2015        ["Go up thread" gnus-summary-up-thread t]
2016        ["Top of thread" gnus-summary-top-thread t]
2017        ["Mark thread as read" gnus-summary-kill-thread t]
2018        ["Lower thread score" gnus-summary-lower-thread t]
2019        ["Raise thread score" gnus-summary-raise-thread t]
2020        ["Rethread current" gnus-summary-rethread-current t]))
2021
2022     (easy-menu-define
2023      gnus-summary-post-menu gnus-summary-mode-map ""
2024      `("Post"
2025        ["Send a message (mail or news)" gnus-summary-post-news
2026         ,@(if (featurep 'xemacs) '(t)
2027             '(:help "Post an article"))]
2028        ["Followup" gnus-summary-followup
2029         ,@(if (featurep 'xemacs) '(t)
2030             '(:help "Post followup to this article"))]
2031        ["Followup and yank" gnus-summary-followup-with-original
2032         ,@(if (featurep 'xemacs) '(t)
2033             '(:help "Post followup to this article, quoting its contents"))]
2034        ["Supersede article" gnus-summary-supersede-article t]
2035        ["Cancel article" gnus-summary-cancel-article
2036         ,@(if (featurep 'xemacs) '(t)
2037             '(:help "Cancel an article you posted"))]
2038        ["Reply" gnus-summary-reply t]
2039        ["Reply and yank" gnus-summary-reply-with-original t]
2040        ["Wide reply" gnus-summary-wide-reply t]
2041        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2042         ,@(if (featurep 'xemacs) '(t)
2043             '(:help "Mail a reply, quoting this article"))]
2044        ["Mail forward" gnus-summary-mail-forward t]
2045        ["Post forward" gnus-summary-post-forward t]
2046        ["Digest and mail" gnus-summary-digest-mail-forward t]
2047        ["Digest and post" gnus-summary-digest-post-forward t]
2048        ["Resend message" gnus-summary-resend-message t]
2049        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2050        ["Send a mail" gnus-summary-mail-other-window t]
2051        ["Create a local message" gnus-summary-news-other-window t]
2052        ["Uuencode and post" gnus-uu-post-news
2053         ,@(if (featurep 'xemacs) '(t)
2054             '(:help "Post a uuencoded article"))]
2055        ["Followup via news" gnus-summary-followup-to-mail t]
2056        ["Followup via news and yank"
2057         gnus-summary-followup-to-mail-with-original t]
2058        ;;("Draft"
2059        ;;["Send" gnus-summary-send-draft t]
2060        ;;["Send bounced" gnus-resend-bounced-mail t])
2061        ))
2062
2063     (cond
2064      ((not (keymapp gnus-summary-post-menu))
2065       (setq gnus-article-post-menu gnus-summary-post-menu))
2066      ((not gnus-article-post-menu)
2067       ;; Don't share post menu.
2068       (setq gnus-article-post-menu
2069             (copy-keymap gnus-summary-post-menu))))
2070     (define-key gnus-article-mode-map [menu-bar post]
2071       (cons "Post" gnus-article-post-menu))
2072
2073     (easy-menu-define
2074      gnus-summary-misc-menu gnus-summary-mode-map ""
2075      `("Gnus"
2076        ("Mark Read"
2077         ["Mark as read" gnus-summary-mark-as-read-forward t]
2078         ["Mark same subject and select"
2079          gnus-summary-kill-same-subject-and-select t]
2080         ["Mark same subject" gnus-summary-kill-same-subject t]
2081         ["Catchup" gnus-summary-catchup
2082          ,@(if (featurep 'xemacs) '(t)
2083              '(:help "Mark unread articles in this group as read"))]
2084         ["Catchup all" gnus-summary-catchup-all t]
2085         ["Catchup to here" gnus-summary-catchup-to-here t]
2086         ["Catchup from here" gnus-summary-catchup-from-here t]
2087         ["Catchup region" gnus-summary-mark-region-as-read t]
2088         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2089        ("Mark Various"
2090         ["Tick" gnus-summary-tick-article-forward t]
2091         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2092         ["Remove marks" gnus-summary-clear-mark-forward t]
2093         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2094         ["Set bookmark" gnus-summary-set-bookmark t]
2095         ["Remove bookmark" gnus-summary-remove-bookmark t])
2096        ("Mark Limit"
2097         ["Marks..." gnus-summary-limit-to-marks t]
2098         ["Subject..." gnus-summary-limit-to-subject t]
2099         ["Author..." gnus-summary-limit-to-author t]
2100         ["Age..." gnus-summary-limit-to-age t]
2101         ["Extra..." gnus-summary-limit-to-extra t]
2102         ["Score" gnus-summary-limit-to-score t]
2103         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2104         ["Unread" gnus-summary-limit-to-unread t]
2105         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2106         ["Articles" gnus-summary-limit-to-articles t]
2107         ["Pop limit" gnus-summary-pop-limit t]
2108         ["Show dormant" gnus-summary-limit-include-dormant t]
2109         ["Hide childless dormant"
2110          gnus-summary-limit-exclude-childless-dormant t]
2111         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2112         ["Hide marked" gnus-summary-limit-exclude-marks t]
2113         ["Show expunged" gnus-summary-limit-include-expunged t])
2114        ("Process Mark"
2115         ["Set mark" gnus-summary-mark-as-processable t]
2116         ["Remove mark" gnus-summary-unmark-as-processable t]
2117         ["Remove all marks" gnus-summary-unmark-all-processable t]
2118         ["Mark above" gnus-uu-mark-over t]
2119         ["Mark series" gnus-uu-mark-series t]
2120         ["Mark region" gnus-uu-mark-region t]
2121         ["Unmark region" gnus-uu-unmark-region t]
2122         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2123         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2124         ["Mark all" gnus-uu-mark-all t]
2125         ["Mark buffer" gnus-uu-mark-buffer t]
2126         ["Mark sparse" gnus-uu-mark-sparse t]
2127         ["Mark thread" gnus-uu-mark-thread t]
2128         ["Unmark thread" gnus-uu-unmark-thread t]
2129         ("Process Mark Sets"
2130          ["Kill" gnus-summary-kill-process-mark t]
2131          ["Yank" gnus-summary-yank-process-mark
2132           gnus-newsgroup-process-stack]
2133          ["Save" gnus-summary-save-process-mark t]))
2134        ("Scroll article"
2135         ["Page forward" gnus-summary-next-page
2136          ,@(if (featurep 'xemacs) '(t)
2137              '(:help "Show next page of article"))]
2138         ["Page backward" gnus-summary-prev-page
2139          ,@(if (featurep 'xemacs) '(t)
2140              '(:help "Show previous page of article"))]
2141         ["Line forward" gnus-summary-scroll-up t])
2142        ("Move"
2143         ["Next unread article" gnus-summary-next-unread-article t]
2144         ["Previous unread article" gnus-summary-prev-unread-article t]
2145         ["Next article" gnus-summary-next-article t]
2146         ["Previous article" gnus-summary-prev-article t]
2147         ["Next unread subject" gnus-summary-next-unread-subject t]
2148         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2149         ["Next article same subject" gnus-summary-next-same-subject t]
2150         ["Previous article same subject" gnus-summary-prev-same-subject t]
2151         ["First unread article" gnus-summary-first-unread-article t]
2152         ["Best unread article" gnus-summary-best-unread-article t]
2153         ["Go to subject number..." gnus-summary-goto-subject t]
2154         ["Go to article number..." gnus-summary-goto-article t]
2155         ["Go to the last article" gnus-summary-goto-last-article t]
2156         ["Pop article off history" gnus-summary-pop-article t])
2157        ("Sort"
2158         ["Sort by number" gnus-summary-sort-by-number t]
2159         ["Sort by author" gnus-summary-sort-by-author t]
2160         ["Sort by subject" gnus-summary-sort-by-subject t]
2161         ["Sort by date" gnus-summary-sort-by-date t]
2162         ["Sort by score" gnus-summary-sort-by-score t]
2163         ["Sort by lines" gnus-summary-sort-by-lines t]
2164         ["Sort by characters" gnus-summary-sort-by-chars t]
2165         ["Original sort" gnus-summary-sort-by-original t])
2166        ("Help"
2167         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2168         ["Describe group" gnus-summary-describe-group t]
2169         ["Read manual" gnus-info-find-node t])
2170        ("Modes"
2171         ["Pick and read" gnus-pick-mode t]
2172         ["Binary" gnus-binary-mode t])
2173        ("Regeneration"
2174         ["Regenerate" gnus-summary-prepare t]
2175         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2176         ["Toggle threading" gnus-summary-toggle-threads t])
2177        ["See old articles" gnus-summary-insert-old-articles t]
2178        ["See new articles" gnus-summary-insert-new-articles t]
2179        ["Filter articles..." gnus-summary-execute-command t]
2180        ["Run command on subjects..." gnus-summary-universal-argument t]
2181        ["Search articles forward..." gnus-summary-search-article-forward t]
2182        ["Search articles backward..." gnus-summary-search-article-backward t]
2183        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2184        ["Expand window" gnus-summary-expand-window t]
2185        ["Expire expirable articles" gnus-summary-expire-articles
2186         (gnus-check-backend-function
2187          'request-expire-articles gnus-newsgroup-name)]
2188        ["Edit local kill file" gnus-summary-edit-local-kill t]
2189        ["Edit main kill file" gnus-summary-edit-global-kill t]
2190        ["Edit group parameters" gnus-summary-edit-parameters t]
2191        ["Customize group parameters" gnus-summary-customize-parameters t]
2192        ["Send a bug report" gnus-bug t]
2193        ("Exit"
2194         ["Catchup and exit" gnus-summary-catchup-and-exit
2195          ,@(if (featurep 'xemacs) '(t)
2196              '(:help "Mark unread articles in this group as read, then exit"))]
2197         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2198         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2199         ["Exit group" gnus-summary-exit
2200          ,@(if (featurep 'xemacs) '(t)
2201              '(:help "Exit current group, return to group selection mode"))]
2202         ["Exit group without updating" gnus-summary-exit-no-update t]
2203         ["Exit and goto next group" gnus-summary-next-group t]
2204         ["Exit and goto prev group" gnus-summary-prev-group t]
2205         ["Reselect group" gnus-summary-reselect-current-group t]
2206         ["Rescan group" gnus-summary-rescan-group t]
2207         ["Update dribble" gnus-summary-save-newsrc t])))
2208
2209     (gnus-run-hooks 'gnus-summary-menu-hook)))
2210
2211 (defvar gnus-summary-tool-bar-map nil)
2212
2213 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2214 (defun gnus-summary-make-tool-bar ()
2215   (if (and (fboundp 'tool-bar-add-item-from-menu)
2216            (default-value 'tool-bar-mode)
2217            (not gnus-summary-tool-bar-map))
2218       (setq gnus-summary-tool-bar-map
2219             (let ((tool-bar-map (make-sparse-keymap))
2220                   (load-path (mm-image-load-path)))
2221               (tool-bar-add-item-from-menu
2222                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2223               (tool-bar-add-item-from-menu
2224                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2225               (tool-bar-add-item-from-menu
2226                'gnus-summary-post-news "post" gnus-summary-mode-map)
2227               (tool-bar-add-item-from-menu
2228                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2229               (tool-bar-add-item-from-menu
2230                'gnus-summary-followup "followup" gnus-summary-mode-map)
2231               (tool-bar-add-item-from-menu
2232                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2233               (tool-bar-add-item-from-menu
2234                'gnus-summary-reply "reply" gnus-summary-mode-map)
2235               (tool-bar-add-item-from-menu
2236                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2237               (tool-bar-add-item-from-menu
2238                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2239               (tool-bar-add-item-from-menu
2240                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2241               (tool-bar-add-item-from-menu
2242                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2243               (tool-bar-add-item-from-menu
2244                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2245               (tool-bar-add-item-from-menu
2246                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2247               (tool-bar-add-item-from-menu
2248                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2249               (tool-bar-add-item-from-menu
2250                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2251               tool-bar-map)))
2252   (if gnus-summary-tool-bar-map
2253       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2254
2255 (defun gnus-score-set-default (var value)
2256   "A version of set that updates the GNU Emacs menu-bar."
2257   (set var value)
2258   ;; It is the message that forces the active status to be updated.
2259   (message ""))
2260
2261 (defun gnus-make-score-map (type)
2262   "Make a summary score map of type TYPE."
2263   (if t
2264       nil
2265     (let ((headers '(("author" "from" string)
2266                      ("subject" "subject" string)
2267                      ("article body" "body" string)
2268                      ("article head" "head" string)
2269                      ("xref" "xref" string)
2270                      ("extra header" "extra" string)
2271                      ("lines" "lines" number)
2272                      ("followups to author" "followup" string)))
2273           (types '((number ("less than" <)
2274                            ("greater than" >)
2275                            ("equal" =))
2276                    (string ("substring" s)
2277                            ("exact string" e)
2278                            ("fuzzy string" f)
2279                            ("regexp" r))))
2280           (perms '(("temporary" (current-time-string))
2281                    ("permanent" nil)
2282                    ("immediate" now)))
2283           header)
2284       (list
2285        (apply
2286         'nconc
2287         (list
2288          (if (eq type 'lower)
2289              "Lower score"
2290            "Increase score"))
2291         (let (outh)
2292           (while headers
2293             (setq header (car headers))
2294             (setq outh
2295                   (cons
2296                    (apply
2297                     'nconc
2298                     (list (car header))
2299                     (let ((ts (cdr (assoc (nth 2 header) types)))
2300                           outt)
2301                       (while ts
2302                         (setq outt
2303                               (cons
2304                                (apply
2305                                 'nconc
2306                                 (list (caar ts))
2307                                 (let ((ps perms)
2308                                       outp)
2309                                   (while ps
2310                                     (setq outp
2311                                           (cons
2312                                            (vector
2313                                             (caar ps)
2314                                             (list
2315                                              'gnus-summary-score-entry
2316                                              (nth 1 header)
2317                                              (if (or (string= (nth 1 header)
2318                                                               "head")
2319                                                      (string= (nth 1 header)
2320                                                               "body"))
2321                                                  ""
2322                                                (list 'gnus-summary-header
2323                                                      (nth 1 header)))
2324                                              (list 'quote (nth 1 (car ts)))
2325                                              (list 'gnus-score-delta-default
2326                                                    nil)
2327                                              (nth 1 (car ps))
2328                                              t)
2329                                             t)
2330                                            outp))
2331                                     (setq ps (cdr ps)))
2332                                   (list (nreverse outp))))
2333                                outt))
2334                         (setq ts (cdr ts)))
2335                       (list (nreverse outt))))
2336                    outh))
2337             (setq headers (cdr headers)))
2338           (list (nreverse outh))))))))
2339
2340 \f
2341
2342 (defun gnus-summary-mode (&optional group)
2343   "Major mode for reading articles.
2344
2345 All normal editing commands are switched off.
2346 \\<gnus-summary-mode-map>
2347 Each line in this buffer represents one article.  To read an
2348 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2349 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2350 respectively.
2351
2352 You can also post articles and send mail from this buffer.  To
2353 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2354 of an article, type `\\[gnus-summary-reply]'.
2355
2356 There are approx. one gazillion commands you can execute in this
2357 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2358
2359 The following commands are available:
2360
2361 \\{gnus-summary-mode-map}"
2362   (interactive)
2363   (kill-all-local-variables)
2364   (when (gnus-visual-p 'summary-menu 'menu)
2365     (gnus-summary-make-menu-bar)
2366     (gnus-summary-make-tool-bar))
2367   (gnus-summary-make-local-variables)
2368   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2369     (gnus-summary-make-local-variables))
2370   (gnus-make-thread-indent-array)
2371   (gnus-simplify-mode-line)
2372   (setq major-mode 'gnus-summary-mode)
2373   (setq mode-name "Summary")
2374   (make-local-variable 'minor-mode-alist)
2375   (use-local-map gnus-summary-mode-map)
2376   (buffer-disable-undo)
2377   (setq buffer-read-only t)             ;Disable modification
2378   (setq truncate-lines t)
2379   (setq selective-display t)
2380   (setq selective-display-ellipses t)   ;Display `...'
2381   (gnus-summary-set-display-table)
2382   (gnus-set-default-directory)
2383   (setq gnus-newsgroup-name group)
2384   (unless (gnus-news-group-p group)
2385     (setq gnus-newsgroup-incorporated
2386           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2387   (make-local-variable 'gnus-summary-line-format)
2388   (make-local-variable 'gnus-summary-line-format-spec)
2389   (make-local-variable 'gnus-summary-dummy-line-format)
2390   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2391   (make-local-variable 'gnus-summary-mark-positions)
2392   (make-local-hook 'pre-command-hook)
2393   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2394   (gnus-run-hooks 'gnus-summary-mode-hook)
2395   (turn-on-gnus-mailing-list-mode)
2396   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2397   (gnus-update-summary-mark-positions))
2398
2399 (defun gnus-summary-make-local-variables ()
2400   "Make all the local summary buffer variables."
2401   (let (global)
2402     (dolist (local gnus-summary-local-variables)
2403       (if (consp local)
2404           (progn
2405             (if (eq (cdr local) 'global)
2406                 ;; Copy the global value of the variable.
2407                 (setq global (symbol-value (car local)))
2408               ;; Use the value from the list.
2409               (setq global (eval (cdr local))))
2410             (set (make-local-variable (car local)) global))
2411         ;; Simple nil-valued local variable.
2412         (set (make-local-variable local) nil)))))
2413
2414 (defun gnus-summary-clear-local-variables ()
2415   (let ((locals gnus-summary-local-variables))
2416     (while locals
2417       (if (consp (car locals))
2418           (and (vectorp (caar locals))
2419                (set (caar locals) nil))
2420         (and (vectorp (car locals))
2421              (set (car locals) nil)))
2422       (setq locals (cdr locals)))))
2423
2424 ;; Summary data functions.
2425
2426 (defmacro gnus-data-number (data)
2427   `(car ,data))
2428
2429 (defmacro gnus-data-set-number (data number)
2430   `(setcar ,data ,number))
2431
2432 (defmacro gnus-data-mark (data)
2433   `(nth 1 ,data))
2434
2435 (defmacro gnus-data-set-mark (data mark)
2436   `(setcar (nthcdr 1 ,data) ,mark))
2437
2438 (defmacro gnus-data-pos (data)
2439   `(nth 2 ,data))
2440
2441 (defmacro gnus-data-set-pos (data pos)
2442   `(setcar (nthcdr 2 ,data) ,pos))
2443
2444 (defmacro gnus-data-header (data)
2445   `(nth 3 ,data))
2446
2447 (defmacro gnus-data-set-header (data header)
2448   `(setcar (nthcdr 3 ,data) ,header))
2449
2450 (defmacro gnus-data-level (data)
2451   `(nth 4 ,data))
2452
2453 (defmacro gnus-data-unread-p (data)
2454   `(= (nth 1 ,data) gnus-unread-mark))
2455
2456 (defmacro gnus-data-read-p (data)
2457   `(/= (nth 1 ,data) gnus-unread-mark))
2458
2459 (defmacro gnus-data-pseudo-p (data)
2460   `(consp (nth 3 ,data)))
2461
2462 (defmacro gnus-data-find (number)
2463   `(assq ,number gnus-newsgroup-data))
2464
2465 (defmacro gnus-data-find-list (number &optional data)
2466   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2467      (memq (assq ,number bdata)
2468            bdata)))
2469
2470 (defmacro gnus-data-make (number mark pos header level)
2471   `(list ,number ,mark ,pos ,header ,level))
2472
2473 (defun gnus-data-enter (after-article number mark pos header level offset)
2474   (let ((data (gnus-data-find-list after-article)))
2475     (unless data
2476       (error "No such article: %d" after-article))
2477     (setcdr data (cons (gnus-data-make number mark pos header level)
2478                        (cdr data)))
2479     (setq gnus-newsgroup-data-reverse nil)
2480     (gnus-data-update-list (cddr data) offset)))
2481
2482 (defun gnus-data-enter-list (after-article list &optional offset)
2483   (when list
2484     (let ((data (and after-article (gnus-data-find-list after-article)))
2485           (ilist list))
2486       (if (not (or data
2487                    after-article))
2488           (let ((odata gnus-newsgroup-data))
2489             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2490             (when offset
2491               (gnus-data-update-list odata offset)))
2492         ;; Find the last element in the list to be spliced into the main
2493         ;; list.
2494         (while (cdr list)
2495           (setq list (cdr list)))
2496         (if (not data)
2497             (progn
2498               (setcdr list gnus-newsgroup-data)
2499               (setq gnus-newsgroup-data ilist)
2500               (when offset
2501                 (gnus-data-update-list (cdr list) offset)))
2502           (setcdr list (cdr data))
2503           (setcdr data ilist)
2504           (when offset
2505             (gnus-data-update-list (cdr list) offset))))
2506       (setq gnus-newsgroup-data-reverse nil))))
2507
2508 (defun gnus-data-remove (article &optional offset)
2509   (let ((data gnus-newsgroup-data))
2510     (if (= (gnus-data-number (car data)) article)
2511         (progn
2512           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2513                 gnus-newsgroup-data-reverse nil)
2514           (when offset
2515             (gnus-data-update-list gnus-newsgroup-data offset)))
2516       (while (cdr data)
2517         (when (= (gnus-data-number (cadr data)) article)
2518           (setcdr data (cddr data))
2519           (when offset
2520             (gnus-data-update-list (cdr data) offset))
2521           (setq data nil
2522                 gnus-newsgroup-data-reverse nil))
2523         (setq data (cdr data))))))
2524
2525 (defmacro gnus-data-list (backward)
2526   `(if ,backward
2527        (or gnus-newsgroup-data-reverse
2528            (setq gnus-newsgroup-data-reverse
2529                  (reverse gnus-newsgroup-data)))
2530      gnus-newsgroup-data))
2531
2532 (defun gnus-data-update-list (data offset)
2533   "Add OFFSET to the POS of all data entries in DATA."
2534   (setq gnus-newsgroup-data-reverse nil)
2535   (while data
2536     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2537     (setq data (cdr data))))
2538
2539 (defun gnus-summary-article-pseudo-p (article)
2540   "Say whether this article is a pseudo article or not."
2541   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2542
2543 (defmacro gnus-summary-article-sparse-p (article)
2544   "Say whether this article is a sparse article or not."
2545   `(memq ,article gnus-newsgroup-sparse))
2546
2547 (defmacro gnus-summary-article-ancient-p (article)
2548   "Say whether this article is a sparse article or not."
2549   `(memq ,article gnus-newsgroup-ancient))
2550
2551 (defun gnus-article-parent-p (number)
2552   "Say whether this article is a parent or not."
2553   (let ((data (gnus-data-find-list number)))
2554     (and (cdr data)                     ; There has to be an article after...
2555          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2556             (gnus-data-level (nth 1 data))))))
2557
2558 (defun gnus-article-children (number)
2559   "Return a list of all children to NUMBER."
2560   (let* ((data (gnus-data-find-list number))
2561          (level (gnus-data-level (car data)))
2562          children)
2563     (setq data (cdr data))
2564     (while (and data
2565                 (= (gnus-data-level (car data)) (1+ level)))
2566       (push (gnus-data-number (car data)) children)
2567       (setq data (cdr data)))
2568     children))
2569
2570 (defmacro gnus-summary-skip-intangible ()
2571   "If the current article is intangible, then jump to a different article."
2572   '(let ((to (get-text-property (point) 'gnus-intangible)))
2573      (and to (gnus-summary-goto-subject to))))
2574
2575 (defmacro gnus-summary-article-intangible-p ()
2576   "Say whether this article is intangible or not."
2577   '(get-text-property (point) 'gnus-intangible))
2578
2579 (defun gnus-article-read-p (article)
2580   "Say whether ARTICLE is read or not."
2581   (not (or (memq article gnus-newsgroup-marked)
2582            (memq article gnus-newsgroup-unreads)
2583            (memq article gnus-newsgroup-unselected)
2584            (memq article gnus-newsgroup-dormant))))
2585
2586 ;; Some summary mode macros.
2587
2588 (defmacro gnus-summary-article-number ()
2589   "The article number of the article on the current line.
2590 If there isn's an article number here, then we return the current
2591 article number."
2592   '(progn
2593      (gnus-summary-skip-intangible)
2594      (or (get-text-property (point) 'gnus-number)
2595          (gnus-summary-last-subject))))
2596
2597 (defmacro gnus-summary-article-header (&optional number)
2598   "Return the header of article NUMBER."
2599   `(gnus-data-header (gnus-data-find
2600                       ,(or number '(gnus-summary-article-number)))))
2601
2602 (defmacro gnus-summary-thread-level (&optional number)
2603   "Return the level of thread that starts with article NUMBER."
2604   `(if (and (eq gnus-summary-make-false-root 'dummy)
2605             (get-text-property (point) 'gnus-intangible))
2606        0
2607      (gnus-data-level (gnus-data-find
2608                        ,(or number '(gnus-summary-article-number))))))
2609
2610 (defmacro gnus-summary-article-mark (&optional number)
2611   "Return the mark of article NUMBER."
2612   `(gnus-data-mark (gnus-data-find
2613                     ,(or number '(gnus-summary-article-number)))))
2614
2615 (defmacro gnus-summary-article-pos (&optional number)
2616   "Return the position of the line of article NUMBER."
2617   `(gnus-data-pos (gnus-data-find
2618                    ,(or number '(gnus-summary-article-number)))))
2619
2620 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2621 (defmacro gnus-summary-article-subject (&optional number)
2622   "Return current subject string or nil if nothing."
2623   `(let ((headers
2624           ,(if number
2625                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2626              '(gnus-data-header (assq (gnus-summary-article-number)
2627                                       gnus-newsgroup-data)))))
2628      (and headers
2629           (vectorp headers)
2630           (mail-header-subject headers))))
2631
2632 (defmacro gnus-summary-article-score (&optional number)
2633   "Return current article score."
2634   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2635                   gnus-newsgroup-scored))
2636        gnus-summary-default-score 0))
2637
2638 (defun gnus-summary-article-children (&optional number)
2639   "Return a list of article numbers that are children of article NUMBER."
2640   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2641          (level (gnus-data-level (car data)))
2642          l children)
2643     (while (and (setq data (cdr data))
2644                 (> (setq l (gnus-data-level (car data))) level))
2645       (and (= (1+ level) l)
2646            (push (gnus-data-number (car data))
2647                  children)))
2648     (nreverse children)))
2649
2650 (defun gnus-summary-article-parent (&optional number)
2651   "Return the article number of the parent of article NUMBER."
2652   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2653                                     (gnus-data-list t)))
2654          (level (gnus-data-level (car data))))
2655     (if (zerop level)
2656         ()                              ; This is a root.
2657       ;; We search until we find an article with a level less than
2658       ;; this one.  That function has to be the parent.
2659       (while (and (setq data (cdr data))
2660                   (not (< (gnus-data-level (car data)) level))))
2661       (and data (gnus-data-number (car data))))))
2662
2663 (defun gnus-unread-mark-p (mark)
2664   "Say whether MARK is the unread mark."
2665   (= mark gnus-unread-mark))
2666
2667 (defun gnus-read-mark-p (mark)
2668   "Say whether MARK is one of the marks that mark as read.
2669 This is all marks except unread, ticked, dormant, and expirable."
2670   (not (or (= mark gnus-unread-mark)
2671            (= mark gnus-ticked-mark)
2672            (= mark gnus-dormant-mark)
2673            (= mark gnus-expirable-mark))))
2674
2675 (defmacro gnus-article-mark (number)
2676   "Return the MARK of article NUMBER.
2677 This macro should only be used when computing the mark the \"first\"
2678 time; i.e., when generating the summary lines.  After that,
2679 `gnus-summary-article-mark' should be used to examine the
2680 marks of articles."
2681   `(cond
2682     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2683     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2684     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2685     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2686     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2687     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2688     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2689     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2690            gnus-ancient-mark))))
2691
2692 ;; Saving hidden threads.
2693
2694 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2695 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2696
2697 (defmacro gnus-save-hidden-threads (&rest forms)
2698   "Save hidden threads, eval FORMS, and restore the hidden threads."
2699   (let ((config (make-symbol "config")))
2700     `(let ((,config (gnus-hidden-threads-configuration)))
2701        (unwind-protect
2702            (save-excursion
2703              ,@forms)
2704          (gnus-restore-hidden-threads-configuration ,config)))))
2705
2706 (defun gnus-data-compute-positions ()
2707   "Compute the positions of all articles."
2708   (setq gnus-newsgroup-data-reverse nil)
2709   (let ((data gnus-newsgroup-data))
2710     (save-excursion
2711       (gnus-save-hidden-threads
2712         (gnus-summary-show-all-threads)
2713         (goto-char (point-min))
2714         (while data
2715           (while (get-text-property (point) 'gnus-intangible)
2716             (forward-line 1))
2717           (gnus-data-set-pos (car data) (+ (point) 3))
2718           (setq data (cdr data))
2719           (forward-line 1))))))
2720
2721 (defun gnus-hidden-threads-configuration ()
2722   "Return the current hidden threads configuration."
2723   (save-excursion
2724     (let (config)
2725       (goto-char (point-min))
2726       (while (search-forward "\r" nil t)
2727         (push (1- (point)) config))
2728       config)))
2729
2730 (defun gnus-restore-hidden-threads-configuration (config)
2731   "Restore hidden threads configuration from CONFIG."
2732   (save-excursion
2733     (let (point buffer-read-only)
2734       (while (setq point (pop config))
2735         (when (and (< point (point-max))
2736                    (goto-char point)
2737                    (eq (char-after) ?\n))
2738           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2739
2740 ;; Various summary mode internalish functions.
2741
2742 (defun gnus-mouse-pick-article (e)
2743   (interactive "e")
2744   (mouse-set-point e)
2745   (gnus-summary-next-page nil t))
2746
2747 (defun gnus-summary-set-display-table ()
2748   "Change the display table.
2749 Odd characters have a tendency to mess
2750 up nicely formatted displays - we make all possible glyphs
2751 display only a single character."
2752
2753   ;; We start from the standard display table, if any.
2754   (let ((table (or (copy-sequence standard-display-table)
2755                    (make-display-table)))
2756         (i 32))
2757     ;; Nix out all the control chars...
2758     (while (>= (setq i (1- i)) 0)
2759       (aset table i [??]))
2760     ;; ... but not newline and cr, of course.  (cr is necessary for the
2761     ;; selective display).
2762     (aset table ?\n nil)
2763     (aset table ?\r nil)
2764     ;; We keep TAB as well.
2765     (aset table ?\t nil)
2766     ;; We nix out any glyphs over 126 that are not set already.
2767     (let ((i 256))
2768       (while (>= (setq i (1- i)) 127)
2769         ;; Only modify if the entry is nil.
2770         (unless (aref table i)
2771           (aset table i [??]))))
2772     (setq buffer-display-table table)))
2773
2774 (defun gnus-summary-set-article-display-arrow (pos)
2775   "Update the overlay arrow to point to line at position POS."
2776   (when (and gnus-summary-display-arrow
2777              (boundp 'overlay-arrow-position)
2778              (boundp 'overlay-arrow-string))
2779     (save-excursion
2780       (goto-char pos)
2781       (beginning-of-line)
2782       (unless overlay-arrow-position
2783         (setq overlay-arrow-position (make-marker)))
2784       (setq overlay-arrow-string "=>"
2785             overlay-arrow-position (set-marker overlay-arrow-position
2786                                                (point)
2787                                                (current-buffer))))))
2788
2789 (defun gnus-summary-buffer-name (group)
2790   "Return the summary buffer name of GROUP."
2791   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2792
2793 (defun gnus-summary-setup-buffer (group)
2794   "Initialize summary buffer."
2795   (let ((buffer (gnus-summary-buffer-name group)))
2796     (if (get-buffer buffer)
2797         (progn
2798           (set-buffer buffer)
2799           (setq gnus-summary-buffer (current-buffer))
2800           (not gnus-newsgroup-prepared))
2801       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2802       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2803       (gnus-summary-mode group)
2804       (when gnus-carpal
2805         (gnus-carpal-setup-buffer 'summary))
2806       (unless gnus-single-article-buffer
2807         (make-local-variable 'gnus-article-buffer)
2808         (make-local-variable 'gnus-article-current)
2809         (make-local-variable 'gnus-original-article-buffer))
2810       (setq gnus-newsgroup-name group)
2811       ;; Set any local variables in the group parameters.
2812       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2813       t)))
2814
2815 (defun gnus-set-global-variables ()
2816   "Set the global equivalents of the buffer-local variables.
2817 They are set to the latest values they had.  These reflect the summary
2818 buffer that was in action when the last article was fetched."
2819   (when (eq major-mode 'gnus-summary-mode)
2820     (setq gnus-summary-buffer (current-buffer))
2821     (let ((name gnus-newsgroup-name)
2822           (marked gnus-newsgroup-marked)
2823           (unread gnus-newsgroup-unreads)
2824           (headers gnus-current-headers)
2825           (data gnus-newsgroup-data)
2826           (summary gnus-summary-buffer)
2827           (article-buffer gnus-article-buffer)
2828           (original gnus-original-article-buffer)
2829           (gac gnus-article-current)
2830           (reffed gnus-reffed-article-number)
2831           (score-file gnus-current-score-file)
2832           (default-charset gnus-newsgroup-charset)
2833           vlist)
2834       (let ((locals gnus-newsgroup-variables))
2835         (while locals
2836           (if (consp (car locals))
2837               (push (eval (caar locals)) vlist)
2838             (push (eval (car locals)) vlist))
2839           (setq locals (cdr locals)))
2840         (setq vlist (nreverse vlist)))
2841       (save-excursion
2842         (set-buffer gnus-group-buffer)
2843         (setq gnus-newsgroup-name name
2844               gnus-newsgroup-marked marked
2845               gnus-newsgroup-unreads unread
2846               gnus-current-headers headers
2847               gnus-newsgroup-data data
2848               gnus-article-current gac
2849               gnus-summary-buffer summary
2850               gnus-article-buffer article-buffer
2851               gnus-original-article-buffer original
2852               gnus-reffed-article-number reffed
2853               gnus-current-score-file score-file
2854               gnus-newsgroup-charset default-charset)
2855         (let ((locals gnus-newsgroup-variables))
2856           (while locals
2857             (if (consp (car locals))
2858                 (set (caar locals) (pop vlist))
2859               (set (car locals) (pop vlist)))
2860             (setq locals (cdr locals))))
2861         ;; The article buffer also has local variables.
2862         (when (gnus-buffer-live-p gnus-article-buffer)
2863           (set-buffer gnus-article-buffer)
2864           (setq gnus-summary-buffer summary))))))
2865
2866 (defun gnus-summary-article-unread-p (article)
2867   "Say whether ARTICLE is unread or not."
2868   (memq article gnus-newsgroup-unreads))
2869
2870 (defun gnus-summary-first-article-p (&optional article)
2871   "Return whether ARTICLE is the first article in the buffer."
2872   (if (not (setq article (or article (gnus-summary-article-number))))
2873       nil
2874     (eq article (caar gnus-newsgroup-data))))
2875
2876 (defun gnus-summary-last-article-p (&optional article)
2877   "Return whether ARTICLE is the last article in the buffer."
2878   (if (not (setq article (or article (gnus-summary-article-number))))
2879       ;; All non-existent numbers are the last article.  :-)
2880       t
2881     (not (cdr (gnus-data-find-list article)))))
2882
2883 (defun gnus-make-thread-indent-array ()
2884   (let ((n 200))
2885     (unless (and gnus-thread-indent-array
2886                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2887       (setq gnus-thread-indent-array (make-vector 201 "")
2888             gnus-thread-indent-array-level gnus-thread-indent-level)
2889       (while (>= n 0)
2890         (aset gnus-thread-indent-array n
2891               (make-string (* n gnus-thread-indent-level) ? ))
2892         (setq n (1- n))))))
2893
2894 (defun gnus-update-summary-mark-positions ()
2895   "Compute where the summary marks are to go."
2896   (save-excursion
2897     (when (gnus-buffer-exists-p gnus-summary-buffer)
2898       (set-buffer gnus-summary-buffer))
2899     (let ((gnus-replied-mark 129)
2900           (gnus-score-below-mark 130)
2901           (gnus-score-over-mark 130)
2902           (gnus-download-mark 131)
2903           (spec gnus-summary-line-format-spec)
2904           gnus-visual pos)
2905       (save-excursion
2906         (gnus-set-work-buffer)
2907         (let ((gnus-summary-line-format-spec spec)
2908               (gnus-newsgroup-downloadable '((0 . t))))
2909           (gnus-summary-insert-line
2910            (make-full-mail-header 0 "" "nobody"
2911                                   "05 Apr 2001 23:33:09 +0400"
2912                                   "" "" 0 0 "" nil)
2913            0 nil 128 t nil "" nil 1)
2914           (goto-char (point-min))
2915           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2916                                              (- (point) 2)))))
2917           (goto-char (point-min))
2918           (push (cons 'replied (and (search-forward "\201" nil t)
2919                                     (- (point) 2)))
2920                 pos)
2921           (goto-char (point-min))
2922           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2923                 pos)
2924           (goto-char (point-min))
2925           (push (cons 'download
2926                       (and (search-forward "\203" nil t) (- (point) 2)))
2927                 pos)))
2928       (setq gnus-summary-mark-positions pos))))
2929
2930 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2931   "Insert a dummy root in the summary buffer."
2932   (beginning-of-line)
2933   (gnus-add-text-properties
2934    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2935    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2936
2937 (defun gnus-summary-extract-address-component (from)
2938   (or (car (funcall gnus-extract-address-components from))
2939       from))
2940
2941 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2942   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
2943                                 default-mime-charset)))
2944     ;; Is it really necessary to do this next part for each summary line?
2945     ;; Luckily, doesn't seem to slow things down much.
2946     (or
2947      (and gnus-ignored-from-addresses
2948           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2949           (let ((extra-headers (mail-header-extra header))
2950                 to
2951                 newsgroups)
2952             (cond
2953              ((setq to (cdr (assq 'To extra-headers)))
2954               (concat "-> "
2955                       (gnus-summary-extract-address-component
2956                        (funcall gnus-decode-encoded-word-function to))))
2957              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
2958               (concat "=> " newsgroups)))))
2959      (gnus-summary-extract-address-component gnus-tmp-from))))
2960
2961 (defun gnus-summary-insert-line (gnus-tmp-header
2962                                  gnus-tmp-level gnus-tmp-current
2963                                  gnus-tmp-unread gnus-tmp-replied
2964                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2965                                  &optional gnus-tmp-dummy gnus-tmp-score
2966                                  gnus-tmp-process)
2967   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2968          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2969          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2970          (gnus-tmp-score-char
2971           (if (or (null gnus-summary-default-score)
2972                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2973                       gnus-summary-zcore-fuzz))
2974               ?\ ;;;Whitespace
2975             (if (< gnus-tmp-score gnus-summary-default-score)
2976                 gnus-score-below-mark gnus-score-over-mark)))
2977          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2978          (gnus-tmp-replied
2979           (cond (gnus-tmp-process gnus-process-mark)
2980                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2981                  gnus-cached-mark)
2982                 (gnus-tmp-replied gnus-replied-mark)
2983                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
2984                  gnus-forwarded-mark)
2985                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2986                  gnus-saved-mark)
2987                 ((memq gnus-tmp-number gnus-newsgroup-recent)
2988                  gnus-recent-mark)
2989                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
2990                  gnus-unseen-mark)
2991                 (t gnus-no-mark)))
2992          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2993          (gnus-tmp-name
2994           (cond
2995            ((string-match "<[^>]+> *$" gnus-tmp-from)
2996             (let ((beg (match-beginning 0)))
2997               (or (and (string-match "^\".+\"" gnus-tmp-from)
2998                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2999                   (substring gnus-tmp-from 0 beg))))
3000            ((string-match "(.+)" gnus-tmp-from)
3001             (substring gnus-tmp-from
3002                        (1+ (match-beginning 0)) (1- (match-end 0))))
3003            (t gnus-tmp-from)))
3004          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3005          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3006          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3007          (buffer-read-only nil))
3008     (when (string= gnus-tmp-name "")
3009       (setq gnus-tmp-name gnus-tmp-from))
3010     (unless (numberp gnus-tmp-lines)
3011       (setq gnus-tmp-lines -1))
3012     (if (= gnus-tmp-lines -1)
3013         (setq gnus-tmp-lines "?")
3014       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3015     (gnus-put-text-property-excluding-characters-with-faces
3016      (point)
3017      (progn (eval gnus-summary-line-format-spec) (point))
3018      'gnus-number gnus-tmp-number)
3019     (when (gnus-visual-p 'summary-highlight 'highlight)
3020       (forward-line -1)
3021       (gnus-run-hooks 'gnus-summary-update-hook)
3022       (forward-line 1))))
3023
3024 (defun gnus-summary-update-line (&optional dont-update)
3025   "Update summary line after change."
3026   (when (and gnus-summary-default-score
3027              (not gnus-summary-inhibit-highlight))
3028     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3029            (article (gnus-summary-article-number))
3030            (score (gnus-summary-article-score article)))
3031       (unless dont-update
3032         (if (and gnus-summary-mark-below
3033                  (< (gnus-summary-article-score)
3034                     gnus-summary-mark-below))
3035             ;; This article has a low score, so we mark it as read.
3036             (when (memq article gnus-newsgroup-unreads)
3037               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3038           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3039             ;; This article was previously marked as read on account
3040             ;; of a low score, but now it has risen, so we mark it as
3041             ;; unread.
3042             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3043         (gnus-summary-update-mark
3044          (if (or (null gnus-summary-default-score)
3045                  (<= (abs (- score gnus-summary-default-score))
3046                      gnus-summary-zcore-fuzz))
3047              ?\ ;;;Whitespace
3048            (if (< score gnus-summary-default-score)
3049                gnus-score-below-mark gnus-score-over-mark))
3050          'score))
3051       ;; Do visual highlighting.
3052       (when (gnus-visual-p 'summary-highlight 'highlight)
3053         (gnus-run-hooks 'gnus-summary-update-hook)))))
3054
3055 (defvar gnus-tmp-new-adopts nil)
3056
3057 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3058   "Return the number of articles in THREAD.
3059 This may be 0 in some cases -- if none of the articles in
3060 the thread are to be displayed."
3061   (let* ((number
3062           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3063           (cond
3064            ((not (listp thread))
3065             1)
3066            ((and (consp thread) (cdr thread))
3067             (apply
3068              '+ 1 (mapcar
3069                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3070            ((null thread)
3071             1)
3072            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3073             1)
3074            (t 0))))
3075     (when (and level (zerop level) gnus-tmp-new-adopts)
3076       (incf number
3077             (apply '+ (mapcar
3078                        'gnus-summary-number-of-articles-in-thread
3079                        gnus-tmp-new-adopts))))
3080     (if char
3081         (if (> number 1) gnus-not-empty-thread-mark
3082           gnus-empty-thread-mark)
3083       number)))
3084
3085 (defun gnus-summary-set-local-parameters (group)
3086   "Go through the local params of GROUP and set all variable specs in that list."
3087   (let ((params (gnus-group-find-parameter group))
3088         (vars '(quit-config))           ; Ignore quit-config.
3089         elem)
3090     (while params
3091       (setq elem (car params)
3092             params (cdr params))
3093       (and (consp elem)                 ; Has to be a cons.
3094            (consp (cdr elem))           ; The cdr has to be a list.
3095            (symbolp (car elem))         ; Has to be a symbol in there.
3096            (not (memq (car elem) vars))
3097            (ignore-errors               ; So we set it.
3098              (push (car elem) vars)
3099              (make-local-variable (car elem))
3100              (set (car elem) (eval (nth 1 elem))))))))
3101
3102 (defun gnus-summary-read-group (group &optional show-all no-article
3103                                       kill-buffer no-display backward
3104                                       select-articles)
3105   "Start reading news in newsgroup GROUP.
3106 If SHOW-ALL is non-nil, already read articles are also listed.
3107 If NO-ARTICLE is non-nil, no article is selected initially.
3108 If NO-DISPLAY, don't generate a summary buffer."
3109   (let (result)
3110     (while (and group
3111                 (null (setq result
3112                             (let ((gnus-auto-select-next nil))
3113                               (or (gnus-summary-read-group-1
3114                                    group show-all no-article
3115                                    kill-buffer no-display
3116                                    select-articles)
3117                                   (setq show-all nil
3118                                         select-articles nil)))))
3119                 (eq gnus-auto-select-next 'quietly))
3120       (set-buffer gnus-group-buffer)
3121       ;; The entry function called above goes to the next
3122       ;; group automatically, so we go two groups back
3123       ;; if we are searching for the previous group.
3124       (when backward
3125         (gnus-group-prev-unread-group 2))
3126       (if (not (equal group (gnus-group-group-name)))
3127           (setq group (gnus-group-group-name))
3128         (setq group nil)))
3129     result))
3130
3131 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3132   "Directly jump to the other GROUP from summary buffer.
3133 If SHOW-ALL is non-nil, already read articles are also listed."
3134   (interactive
3135    (if (eq gnus-summary-buffer (current-buffer))
3136        (list (completing-read
3137               "Group: " gnus-active-hashtb nil t
3138               (when (and gnus-newsgroup-name
3139                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3140                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3141               'gnus-group-history)
3142              current-prefix-arg)
3143      (error "%s must be invoked from a gnus summary buffer." this-command)))
3144   (unless (or (zerop (length group))
3145               (and gnus-newsgroup-name
3146                    (string-equal gnus-newsgroup-name group)))
3147     (gnus-summary-exit)
3148     (gnus-summary-read-group group show-all
3149                              gnus-dont-select-after-jump-to-other-group)))
3150
3151 (defun gnus-summary-read-group-1 (group show-all no-article
3152                                         kill-buffer no-display
3153                                         &optional select-articles)
3154   ;; Killed foreign groups can't be entered.
3155   ;;  (when (and (not (gnus-group-native-p group))
3156   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3157   ;;    (error "Dead non-native groups can't be entered"))
3158   (gnus-message 5 "Retrieving newsgroup: %s..."
3159                 (gnus-group-decoded-name group))
3160   (let* ((new-group (gnus-summary-setup-buffer group))
3161          (quit-config (gnus-group-quit-config group))
3162          (did-select (and new-group (gnus-select-newsgroup
3163                                      group show-all select-articles))))
3164     (cond
3165      ;; This summary buffer exists already, so we just select it.
3166      ((not new-group)
3167       (gnus-set-global-variables)
3168       (when kill-buffer
3169         (gnus-kill-or-deaden-summary kill-buffer))
3170       (gnus-configure-windows 'summary 'force)
3171       (gnus-set-mode-line 'summary)
3172       (gnus-summary-position-point)
3173       (message "")
3174       t)
3175      ;; We couldn't select this group.
3176      ((null did-select)
3177       (when (and (eq major-mode 'gnus-summary-mode)
3178                  (not (equal (current-buffer) kill-buffer)))
3179         (kill-buffer (current-buffer))
3180         (if (not quit-config)
3181             (progn
3182               ;; Update the info -- marks might need to be removed,
3183               ;; for instance.
3184               (gnus-summary-update-info)
3185               (set-buffer gnus-group-buffer)
3186               (gnus-group-jump-to-group group)
3187               (gnus-group-next-unread-group 1))
3188           (gnus-handle-ephemeral-exit quit-config)))
3189       (let ((grpinfo (gnus-get-info group)))
3190         (if (null (gnus-info-read grpinfo))
3191             (gnus-message 3 "Group %s contains no messages"
3192                           (gnus-group-decoded-name group))
3193           (gnus-message 3 "Can't select group")))
3194       nil)
3195      ;; The user did a `C-g' while prompting for number of articles,
3196      ;; so we exit this group.
3197      ((eq did-select 'quit)
3198       (and (eq major-mode 'gnus-summary-mode)
3199            (not (equal (current-buffer) kill-buffer))
3200            (kill-buffer (current-buffer)))
3201       (when kill-buffer
3202         (gnus-kill-or-deaden-summary kill-buffer))
3203       (if (not quit-config)
3204           (progn
3205             (set-buffer gnus-group-buffer)
3206             (gnus-group-jump-to-group group)
3207             (gnus-group-next-unread-group 1)
3208             (gnus-configure-windows 'group 'force))
3209         (gnus-handle-ephemeral-exit quit-config))
3210       ;; Finally signal the quit.
3211       (signal 'quit nil))
3212      ;; The group was successfully selected.
3213      (t
3214       (gnus-set-global-variables)
3215       ;; Save the active value in effect when the group was entered.
3216       (setq gnus-newsgroup-active
3217             (gnus-copy-sequence
3218              (gnus-active gnus-newsgroup-name)))
3219       ;; You can change the summary buffer in some way with this hook.
3220       (gnus-run-hooks 'gnus-select-group-hook)
3221       (gnus-update-format-specifications
3222        nil 'summary 'summary-mode 'summary-dummy)
3223       (gnus-update-summary-mark-positions)
3224       ;; Do score processing.
3225       (when gnus-use-scoring
3226         (gnus-possibly-score-headers))
3227       ;; Check whether to fill in the gaps in the threads.
3228       (when gnus-build-sparse-threads
3229         (gnus-build-sparse-threads))
3230       ;; Find the initial limit.
3231       (if gnus-show-threads
3232           (if show-all
3233               (let ((gnus-newsgroup-dormant nil))
3234                 (gnus-summary-initial-limit show-all))
3235             (gnus-summary-initial-limit show-all))
3236         ;; When untreaded, all articles are always shown.
3237         (setq gnus-newsgroup-limit
3238               (mapcar
3239                (lambda (header) (mail-header-number header))
3240                gnus-newsgroup-headers)))
3241       ;; Generate the summary buffer.
3242       (unless no-display
3243         (gnus-summary-prepare))
3244       (when gnus-use-trees
3245         (gnus-tree-open group)
3246         (setq gnus-summary-highlight-line-function
3247               'gnus-tree-highlight-article))
3248       ;; If the summary buffer is empty, but there are some low-scored
3249       ;; articles or some excluded dormants, we include these in the
3250       ;; buffer.
3251       (when (and (zerop (buffer-size))
3252                  (not no-display))
3253         (cond (gnus-newsgroup-dormant
3254                (gnus-summary-limit-include-dormant))
3255               ((and gnus-newsgroup-scored show-all)
3256                (gnus-summary-limit-include-expunged t))))
3257       ;; Function `gnus-apply-kill-file' must be called in this hook.
3258       (gnus-run-hooks 'gnus-apply-kill-hook)
3259       (if (and (zerop (buffer-size))
3260                (not no-display))
3261           (progn
3262             ;; This newsgroup is empty.
3263             (gnus-summary-catchup-and-exit nil t)
3264             (gnus-message 6 "No unread news")
3265             (when kill-buffer
3266               (gnus-kill-or-deaden-summary kill-buffer))
3267             ;; Return nil from this function.
3268             nil)
3269         ;; Hide conversation thread subtrees.  We cannot do this in
3270         ;; gnus-summary-prepare-hook since kill processing may not
3271         ;; work with hidden articles.
3272         (and gnus-show-threads
3273              gnus-thread-hide-subtree
3274              (gnus-summary-hide-all-threads))
3275         (when kill-buffer
3276           (gnus-kill-or-deaden-summary kill-buffer))
3277         ;; Show first unread article if requested.
3278         (if (and (not no-article)
3279                  (not no-display)
3280                  gnus-newsgroup-unreads
3281                  gnus-auto-select-first)
3282             (progn
3283               (gnus-configure-windows 'summary)
3284               (cond
3285                ((eq gnus-auto-select-first 'best)
3286                 (gnus-summary-best-unread-article))
3287                ((eq gnus-auto-select-first t)
3288                 (gnus-summary-first-unread-article))
3289                ((gnus-functionp gnus-auto-select-first)
3290                 (funcall gnus-auto-select-first))))
3291           ;; Don't select any articles, just move point to the first
3292           ;; article in the group.
3293           (goto-char (point-min))
3294           (gnus-summary-position-point)
3295           (gnus-configure-windows 'summary 'force)
3296           (gnus-set-mode-line 'summary))
3297         (when (get-buffer-window gnus-group-buffer t)
3298           ;; Gotta use windows, because recenter does weird stuff if
3299           ;; the current buffer ain't the displayed window.
3300           (let ((owin (selected-window)))
3301             (select-window (get-buffer-window gnus-group-buffer t))
3302             (when (gnus-group-goto-group group)
3303               (recenter))
3304             (select-window owin)))
3305         ;; Mark this buffer as "prepared".
3306         (setq gnus-newsgroup-prepared t)
3307         (gnus-run-hooks 'gnus-summary-prepared-hook)
3308         t)))))
3309
3310 (defun gnus-summary-prepare ()
3311   "Generate the summary buffer."
3312   (interactive)
3313   (let ((buffer-read-only nil))
3314     (erase-buffer)
3315     (setq gnus-newsgroup-data nil
3316           gnus-newsgroup-data-reverse nil)
3317     (gnus-run-hooks 'gnus-summary-generate-hook)
3318     ;; Generate the buffer, either with threads or without.
3319     (when gnus-newsgroup-headers
3320       (gnus-summary-prepare-threads
3321        (if gnus-show-threads
3322            (gnus-sort-gathered-threads
3323             (funcall gnus-summary-thread-gathering-function
3324                      (gnus-sort-threads
3325                       (gnus-cut-threads (gnus-make-threads)))))
3326          ;; Unthreaded display.
3327          (gnus-sort-articles gnus-newsgroup-headers))))
3328     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3329     ;; Call hooks for modifying summary buffer.
3330     (goto-char (point-min))
3331     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3332
3333 (defsubst gnus-general-simplify-subject (subject)
3334   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3335   (setq subject
3336         (cond
3337          ;; Truncate the subject.
3338          (gnus-simplify-subject-functions
3339           (gnus-map-function gnus-simplify-subject-functions subject))
3340          ((numberp gnus-summary-gather-subject-limit)
3341           (setq subject (gnus-simplify-subject-re subject))
3342           (if (> (length subject) gnus-summary-gather-subject-limit)
3343               (substring subject 0 gnus-summary-gather-subject-limit)
3344             subject))
3345          ;; Fuzzily simplify it.
3346          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3347           (gnus-simplify-subject-fuzzy subject))
3348          ;; Just remove the leading "Re:".
3349          (t
3350           (gnus-simplify-subject-re subject))))
3351
3352   (if (and gnus-summary-gather-exclude-subject
3353            (string-match gnus-summary-gather-exclude-subject subject))
3354       nil                               ; This article shouldn't be gathered
3355     subject))
3356
3357 (defun gnus-summary-simplify-subject-query ()
3358   "Query where the respool algorithm would put this article."
3359   (interactive)
3360   (gnus-summary-select-article)
3361   (message "%s"
3362            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3363
3364 (defun gnus-gather-threads-by-subject (threads)
3365   "Gather threads by looking at Subject headers."
3366   (if (not gnus-summary-make-false-root)
3367       threads
3368     (let ((hashtb (gnus-make-hashtable 1024))
3369           (prev threads)
3370           (result threads)
3371           subject hthread whole-subject)
3372       (while threads
3373         (setq subject (gnus-general-simplify-subject
3374                        (setq whole-subject (mail-header-subject
3375                                             (caar threads)))))
3376         (when subject
3377           (if (setq hthread (gnus-gethash subject hashtb))
3378               (progn
3379                 ;; We enter a dummy root into the thread, if we
3380                 ;; haven't done that already.
3381                 (unless (stringp (caar hthread))
3382                   (setcar hthread (list whole-subject (car hthread))))
3383                 ;; We add this new gathered thread to this gathered
3384                 ;; thread.
3385                 (setcdr (car hthread)
3386                         (nconc (cdar hthread) (list (car threads))))
3387                 ;; Remove it from the list of threads.
3388                 (setcdr prev (cdr threads))
3389                 (setq threads prev))
3390             ;; Enter this thread into the hash table.
3391             (gnus-sethash subject threads hashtb)))
3392         (setq prev threads)
3393         (setq threads (cdr threads)))
3394       result)))
3395
3396 (defun gnus-gather-threads-by-references (threads)
3397   "Gather threads by looking at References headers."
3398   (let ((idhashtb (gnus-make-hashtable 1024))
3399         (thhashtb (gnus-make-hashtable 1024))
3400         (prev threads)
3401         (result threads)
3402         ids references id gthread gid entered ref)
3403     (while threads
3404       (when (setq references (mail-header-references (caar threads)))
3405         (setq id (mail-header-id (caar threads))
3406               ids (gnus-split-references references)
3407               entered nil)
3408         (while (setq ref (pop ids))
3409           (setq ids (delete ref ids))
3410           (if (not (setq gid (gnus-gethash ref idhashtb)))
3411               (progn
3412                 (gnus-sethash ref id idhashtb)
3413                 (gnus-sethash id threads thhashtb))
3414             (setq gthread (gnus-gethash gid thhashtb))
3415             (unless entered
3416               ;; We enter a dummy root into the thread, if we
3417               ;; haven't done that already.
3418               (unless (stringp (caar gthread))
3419                 (setcar gthread (list (mail-header-subject (caar gthread))
3420                                       (car gthread))))
3421               ;; We add this new gathered thread to this gathered
3422               ;; thread.
3423               (setcdr (car gthread)
3424                       (nconc (cdar gthread) (list (car threads)))))
3425             ;; Add it into the thread hash table.
3426             (gnus-sethash id gthread thhashtb)
3427             (setq entered t)
3428             ;; Remove it from the list of threads.
3429             (setcdr prev (cdr threads))
3430             (setq threads prev))))
3431       (setq prev threads)
3432       (setq threads (cdr threads)))
3433     result))
3434
3435 (defun gnus-sort-gathered-threads (threads)
3436   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3437   (let ((result threads))
3438     (while threads
3439       (when (stringp (caar threads))
3440         (setcdr (car threads)
3441                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3442       (setq threads (cdr threads)))
3443     result))
3444
3445 (defun gnus-thread-loop-p (root thread)
3446   "Say whether ROOT is in THREAD."
3447   (let ((stack (list thread))
3448         (infloop 0)
3449         th)
3450     (while (setq thread (pop stack))
3451       (setq th (cdr thread))
3452       (while (and th
3453                   (not (eq (caar th) root)))
3454         (pop th))
3455       (if th
3456           ;; We have found a loop.
3457           (let (ref-dep)
3458             (setcdr thread (delq (car th) (cdr thread)))
3459             (if (boundp (setq ref-dep (intern "none"
3460                                               gnus-newsgroup-dependencies)))
3461                 (setcdr (symbol-value ref-dep)
3462                         (nconc (cdr (symbol-value ref-dep))
3463                                (list (car th))))
3464               (set ref-dep (list nil (car th))))
3465             (setq infloop 1
3466                   stack nil))
3467         ;; Push all the subthreads onto the stack.
3468         (push (cdr thread) stack)))
3469     infloop))
3470
3471 (defun gnus-make-threads ()
3472   "Go through the dependency hashtb and find the roots.  Return all threads."
3473   (let (threads)
3474     (while (catch 'infloop
3475              (mapatoms
3476               (lambda (refs)
3477                 ;; Deal with self-referencing References loops.
3478                 (when (and (car (symbol-value refs))
3479                            (not (zerop
3480                                  (apply
3481                                   '+
3482                                   (mapcar
3483                                    (lambda (thread)
3484                                      (gnus-thread-loop-p
3485                                       (car (symbol-value refs)) thread))
3486                                    (cdr (symbol-value refs)))))))
3487                   (setq threads nil)
3488                   (throw 'infloop t))
3489                 (unless (car (symbol-value refs))
3490                   ;; These threads do not refer back to any other articles,
3491                   ;; so they're roots.
3492                   (setq threads (append (cdr (symbol-value refs)) threads))))
3493               gnus-newsgroup-dependencies)))
3494     threads))
3495
3496 ;; Build the thread tree.
3497 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3498   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3499
3500 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3501 if it was already present.
3502
3503 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3504 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3505 Message-IDs will be renamed be renamed to a unique Message-ID before
3506 being entered.
3507
3508 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3509   (let* ((id (mail-header-id header))
3510          (id-dep (and id (intern id dependencies)))
3511          ref ref-dep ref-header)
3512     ;; Enter this `header' in the `dependencies' table.
3513     (cond
3514      ((not id-dep)
3515       (setq header nil))
3516      ;; The first two cases do the normal part: enter a new `header'
3517      ;; in the `dependencies' table.
3518      ((not (boundp id-dep))
3519       (set id-dep (list header)))
3520      ((null (car (symbol-value id-dep)))
3521       (setcar (symbol-value id-dep) header))
3522
3523      ;; From here the `header' was already present in the
3524      ;; `dependencies' table.
3525      (force-new
3526       ;; Overrides an existing entry;
3527       ;; just set the header part of the entry.
3528       (setcar (symbol-value id-dep) header))
3529
3530      ;; Renames the existing `header' to a unique Message-ID.
3531      ((not gnus-summary-ignore-duplicates)
3532       ;; An article with this Message-ID has already been seen.
3533       ;; We rename the Message-ID.
3534       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3535            (list header))
3536       (mail-header-set-id header id))
3537
3538      ;; The last case ignores an existing entry, except it adds any
3539      ;; additional Xrefs (in case the two articles came from different
3540      ;; servers.
3541      ;; Also sets `header' to `nil' meaning that the `dependencies'
3542      ;; table was *not* modified.
3543      (t
3544       (mail-header-set-xref
3545        (car (symbol-value id-dep))
3546        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3547                    "")
3548                (or (mail-header-xref header) "")))
3549       (setq header nil)))
3550
3551     (when header
3552       ;; First check if that we are not creating a References loop.
3553       (setq ref (gnus-parent-id (mail-header-references header)))
3554       (while (and ref
3555                   (setq ref-dep (intern-soft ref dependencies))
3556                   (boundp ref-dep)
3557                   (setq ref-header (car (symbol-value ref-dep))))
3558         (if (string= id ref)
3559             ;; Yuk!  This is a reference loop.  Make the article be a
3560             ;; root article.
3561             (progn
3562               (mail-header-set-references (car (symbol-value id-dep)) "none")
3563               (setq ref nil))
3564           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3565       (setq ref (gnus-parent-id (mail-header-references header)))
3566       (setq ref-dep (intern (or ref "none") dependencies))
3567       (if (boundp ref-dep)
3568           (setcdr (symbol-value ref-dep)
3569                   (nconc (cdr (symbol-value ref-dep))
3570                          (list (symbol-value id-dep))))
3571         (set ref-dep (list nil (symbol-value id-dep)))))
3572     header))
3573
3574 (defun gnus-build-sparse-threads ()
3575   (let ((headers gnus-newsgroup-headers)
3576         (mail-parse-charset gnus-newsgroup-charset)
3577         (gnus-summary-ignore-duplicates t)
3578         header references generation relations
3579         subject child end new-child date)
3580     ;; First we create an alist of generations/relations, where
3581     ;; generations is how much we trust the relation, and the relation
3582     ;; is parent/child.
3583     (gnus-message 7 "Making sparse threads...")
3584     (save-excursion
3585       (nnheader-set-temp-buffer " *gnus sparse threads*")
3586       (while (setq header (pop headers))
3587         (when (and (setq references (mail-header-references header))
3588                    (not (string= references "")))
3589           (insert references)
3590           (setq child (mail-header-id header)
3591                 subject (mail-header-subject header)
3592                 date (mail-header-date header)
3593                 generation 0)
3594           (while (search-backward ">" nil t)
3595             (setq end (1+ (point)))
3596             (when (search-backward "<" nil t)
3597               (setq new-child (buffer-substring (point) end))
3598               (push (list (incf generation)
3599                           child (setq child new-child)
3600                           subject date)
3601                     relations)))
3602           (when child
3603             (push (list (1+ generation) child nil subject) relations))
3604           (erase-buffer)))
3605       (kill-buffer (current-buffer)))
3606     ;; Sort over trustworthiness.
3607     (mapcar
3608      (lambda (relation)
3609        (when (gnus-dependencies-add-header
3610               (make-full-mail-header-from-decoded-header
3611                gnus-reffed-article-number
3612                (nth 3 relation) "" (or (nth 4 relation) "")
3613                (nth 1 relation)
3614                (or (nth 2 relation) "") 0 0 "")
3615               gnus-newsgroup-dependencies nil)
3616          (push gnus-reffed-article-number gnus-newsgroup-limit)
3617          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3618          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3619                gnus-newsgroup-reads)
3620          (decf gnus-reffed-article-number)))
3621      (sort relations 'car-less-than-car))
3622     (gnus-message 7 "Making sparse threads...done")))
3623
3624 (defun gnus-build-old-threads ()
3625   ;; Look at all the articles that refer back to old articles, and
3626   ;; fetch the headers for the articles that aren't there.  This will
3627   ;; build complete threads - if the roots haven't been expired by the
3628   ;; server, that is.
3629   (let ((mail-parse-charset gnus-newsgroup-charset)
3630         id heads)
3631     (mapatoms
3632      (lambda (refs)
3633        (when (not (car (symbol-value refs)))
3634          (setq heads (cdr (symbol-value refs)))
3635          (while heads
3636            (if (memq (mail-header-number (caar heads))
3637                      gnus-newsgroup-dormant)
3638                (setq heads (cdr heads))
3639              (setq id (symbol-name refs))
3640              (while (and (setq id (gnus-build-get-header id))
3641                          (not (car (gnus-id-to-thread id)))))
3642              (setq heads nil)))))
3643      gnus-newsgroup-dependencies)))
3644
3645 ;; This function has to be called with point after the article number
3646 ;; on the beginning of the line.
3647 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3648   (let ((eol (gnus-point-at-eol))
3649         (buffer (current-buffer))
3650         header)
3651
3652     ;; overview: [num subject from date id refs chars lines misc]
3653     (unwind-protect
3654         (progn
3655           (narrow-to-region (point) eol)
3656           (unless (eobp)
3657             (forward-char))
3658
3659           (setq header
3660                 (make-full-mail-header
3661                  number                         ; number
3662                  (nnheader-nov-field)           ; subject
3663                  (nnheader-nov-field)           ; from
3664                  (nnheader-nov-field)           ; date
3665                  (nnheader-nov-read-message-id) ; id
3666                  (nnheader-nov-field)           ; refs
3667                  (nnheader-nov-read-integer)    ; chars
3668                  (nnheader-nov-read-integer)    ; lines
3669                  (unless (eobp)
3670                    (if (looking-at "Xref: ")
3671                        (goto-char (match-end 0)))
3672                    (nnheader-nov-field))        ; Xref
3673                  (nnheader-nov-parse-extra))))  ; extra
3674
3675       (widen))
3676
3677     (when gnus-alter-header-function
3678       (funcall gnus-alter-header-function header))
3679     (gnus-dependencies-add-header header dependencies force-new)))
3680
3681 (defun gnus-build-get-header (id)
3682   "Look through the buffer of NOV lines and find the header to ID.
3683 Enter this line into the dependencies hash table, and return
3684 the id of the parent article (if any)."
3685   (let ((deps gnus-newsgroup-dependencies)
3686         found header)
3687     (prog1
3688         (save-excursion
3689           (set-buffer nntp-server-buffer)
3690           (let ((case-fold-search nil))
3691             (goto-char (point-min))
3692             (while (and (not found)
3693                         (search-forward id nil t))
3694               (beginning-of-line)
3695               (setq found (looking-at
3696                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3697                                    (regexp-quote id))))
3698               (or found (beginning-of-line 2)))
3699             (when found
3700               (beginning-of-line)
3701               (and
3702                (setq header (gnus-nov-parse-line
3703                              (read (current-buffer)) deps))
3704                (gnus-parent-id (mail-header-references header))))))
3705       (when header
3706         (let ((number (mail-header-number header)))
3707           (push number gnus-newsgroup-limit)
3708           (push header gnus-newsgroup-headers)
3709           (if (memq number gnus-newsgroup-unselected)
3710               (progn
3711                 (push number gnus-newsgroup-unreads)
3712                 (setq gnus-newsgroup-unselected
3713                       (delq number gnus-newsgroup-unselected)))
3714             (push number gnus-newsgroup-ancient)))))))
3715
3716 (defun gnus-build-all-threads ()
3717   "Read all the headers."
3718   (let ((gnus-summary-ignore-duplicates t)
3719         (mail-parse-charset gnus-newsgroup-charset)
3720         (dependencies gnus-newsgroup-dependencies)
3721         header article)
3722     (save-excursion
3723       (set-buffer nntp-server-buffer)
3724       (let ((case-fold-search nil))
3725         (goto-char (point-min))
3726         (while (not (eobp))
3727           (ignore-errors
3728             (setq article (read (current-buffer))
3729                   header (gnus-nov-parse-line article dependencies)))
3730           (when header
3731             (save-excursion
3732               (set-buffer gnus-summary-buffer)
3733               (push header gnus-newsgroup-headers)
3734               (if (memq (setq article (mail-header-number header))
3735                         gnus-newsgroup-unselected)
3736                   (progn
3737                     (push article gnus-newsgroup-unreads)
3738                     (setq gnus-newsgroup-unselected
3739                           (delq article gnus-newsgroup-unselected)))
3740                 (push article gnus-newsgroup-ancient)))
3741             (forward-line 1)))))))
3742
3743 (defun gnus-summary-update-article-line (article header)
3744   "Update the line for ARTICLE using HEADERS."
3745   (let* ((id (mail-header-id header))
3746          (thread (gnus-id-to-thread id)))
3747     (unless thread
3748       (error "Article in no thread"))
3749     ;; Update the thread.
3750     (setcar thread header)
3751     (gnus-summary-goto-subject article)
3752     (let* ((datal (gnus-data-find-list article))
3753            (data (car datal))
3754            (length (when (cdr datal)
3755                      (- (gnus-data-pos data)
3756                         (gnus-data-pos (cadr datal)))))
3757            (buffer-read-only nil)
3758            (level (gnus-summary-thread-level)))
3759       (gnus-delete-line)
3760       (gnus-summary-insert-line
3761        header level nil (gnus-article-mark article)
3762        (memq article gnus-newsgroup-replied)
3763        (memq article gnus-newsgroup-expirable)
3764        ;; Only insert the Subject string when it's different
3765        ;; from the previous Subject string.
3766        (if (and
3767             gnus-show-threads
3768             (gnus-subject-equal
3769              (condition-case ()
3770                  (mail-header-subject
3771                   (gnus-data-header
3772                    (cadr
3773                     (gnus-data-find-list
3774                      article
3775                      (gnus-data-list t)))))
3776                ;; Error on the side of excessive subjects.
3777                (error ""))
3778              (mail-header-subject header)))
3779            ""
3780          (mail-header-subject header))
3781        nil (cdr (assq article gnus-newsgroup-scored))
3782        (memq article gnus-newsgroup-processable))
3783       (when length
3784         (gnus-data-update-list
3785          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3786
3787 (defun gnus-summary-update-article (article &optional iheader)
3788   "Update ARTICLE in the summary buffer."
3789   (set-buffer gnus-summary-buffer)
3790   (let* ((header (gnus-summary-article-header article))
3791          (id (mail-header-id header))
3792          (data (gnus-data-find article))
3793          (thread (gnus-id-to-thread id))
3794          (references (mail-header-references header))
3795          (parent
3796           (gnus-id-to-thread
3797            (or (gnus-parent-id
3798                 (when (and references
3799                            (not (equal "" references)))
3800                   references))
3801                "none")))
3802          (buffer-read-only nil)
3803          (old (car thread)))
3804     (when thread
3805       (unless iheader
3806         (setcar thread nil)
3807         (when parent
3808           (delq thread parent)))
3809       (if (gnus-summary-insert-subject id header)
3810           ;; Set the (possibly) new article number in the data structure.
3811           (gnus-data-set-number data (gnus-id-to-article id))
3812         (setcar thread old)
3813         nil))))
3814
3815 (defun gnus-rebuild-thread (id &optional line)
3816   "Rebuild the thread containing ID.
3817 If LINE, insert the rebuilt thread starting on line LINE."
3818   (let ((buffer-read-only nil)
3819         old-pos current thread data)
3820     (if (not gnus-show-threads)
3821         (setq thread (list (car (gnus-id-to-thread id))))
3822       ;; Get the thread this article is part of.
3823       (setq thread (gnus-remove-thread id)))
3824     (setq old-pos (gnus-point-at-bol))
3825     (setq current (save-excursion
3826                     (and (re-search-backward "[\r\n]" nil t)
3827                          (gnus-summary-article-number))))
3828     ;; If this is a gathered thread, we have to go some re-gathering.
3829     (when (stringp (car thread))
3830       (let ((subject (car thread))
3831             roots thr)
3832         (setq thread (cdr thread))
3833         (while thread
3834           (unless (memq (setq thr (gnus-id-to-thread
3835                                    (gnus-root-id
3836                                     (mail-header-id (caar thread)))))
3837                         roots)
3838             (push thr roots))
3839           (setq thread (cdr thread)))
3840         ;; We now have all (unique) roots.
3841         (if (= (length roots) 1)
3842             ;; All the loose roots are now one solid root.
3843             (setq thread (car roots))
3844           (setq thread (cons subject (gnus-sort-threads roots))))))
3845     (let (threads)
3846       ;; We then insert this thread into the summary buffer.
3847       (when line
3848         (goto-char (point-min))
3849         (forward-line (1- line)))
3850       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3851         (if gnus-show-threads
3852             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3853           (gnus-summary-prepare-unthreaded thread))
3854         (setq data (nreverse gnus-newsgroup-data))
3855         (setq threads gnus-newsgroup-threads))
3856       ;; We splice the new data into the data structure.
3857       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3858       ;;!!! then we want to insert at the beginning of the buffer.
3859       ;;!!! That happens to be true with Gnus now, but that may
3860       ;;!!! change in the future.  Perhaps.
3861       (gnus-data-enter-list
3862        (if line nil current) data (- (point) old-pos))
3863       (setq gnus-newsgroup-threads
3864             (nconc threads gnus-newsgroup-threads))
3865       (gnus-data-compute-positions))))
3866
3867 (defun gnus-number-to-header (number)
3868   "Return the header for article NUMBER."
3869   (let ((headers gnus-newsgroup-headers))
3870     (while (and headers
3871                 (not (= number (mail-header-number (car headers)))))
3872       (pop headers))
3873     (when headers
3874       (car headers))))
3875
3876 (defun gnus-parent-headers (in-headers &optional generation)
3877   "Return the headers of the GENERATIONeth parent of HEADERS."
3878   (unless generation
3879     (setq generation 1))
3880   (let ((parent t)
3881         (headers in-headers)
3882         references)
3883     (while (and parent
3884                 (not (zerop generation))
3885                 (setq references (mail-header-references headers)))
3886       (setq headers (if (and references
3887                              (setq parent (gnus-parent-id references)))
3888                         (car (gnus-id-to-thread parent))
3889                       nil))
3890       (decf generation))
3891     (and (not (eq headers in-headers))
3892          headers)))
3893
3894 (defun gnus-id-to-thread (id)
3895   "Return the (sub-)thread where ID appears."
3896   (gnus-gethash id gnus-newsgroup-dependencies))
3897
3898 (defun gnus-id-to-article (id)
3899   "Return the article number of ID."
3900   (let ((thread (gnus-id-to-thread id)))
3901     (when (and thread
3902                (car thread))
3903       (mail-header-number (car thread)))))
3904
3905 (defun gnus-id-to-header (id)
3906   "Return the article headers of ID."
3907   (car (gnus-id-to-thread id)))
3908
3909 (defun gnus-article-displayed-root-p (article)
3910   "Say whether ARTICLE is a root(ish) article."
3911   (let ((level (gnus-summary-thread-level article))
3912         (refs (mail-header-references  (gnus-summary-article-header article)))
3913         particle)
3914     (cond
3915      ((null level) nil)
3916      ((zerop level) t)
3917      ((null refs) t)
3918      ((null (gnus-parent-id refs)) t)
3919      ((and (= 1 level)
3920            (null (setq particle (gnus-id-to-article
3921                                  (gnus-parent-id refs))))
3922            (null (gnus-summary-thread-level particle)))))))
3923
3924 (defun gnus-root-id (id)
3925   "Return the id of the root of the thread where ID appears."
3926   (let (last-id prev)
3927     (while (and id (setq prev (car (gnus-id-to-thread id))))
3928       (setq last-id id
3929             id (gnus-parent-id (mail-header-references prev))))
3930     last-id))
3931
3932 (defun gnus-articles-in-thread (thread)
3933   "Return the list of articles in THREAD."
3934   (cons (mail-header-number (car thread))
3935         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3936
3937 (defun gnus-remove-thread (id &optional dont-remove)
3938   "Remove the thread that has ID in it."
3939   (let (headers thread last-id)
3940     ;; First go up in this thread until we find the root.
3941     (setq last-id (gnus-root-id id)
3942           headers (message-flatten-list (gnus-id-to-thread last-id)))
3943     ;; We have now found the real root of this thread.  It might have
3944     ;; been gathered into some loose thread, so we have to search
3945     ;; through the threads to find the thread we wanted.
3946     (let ((threads gnus-newsgroup-threads)
3947           sub)
3948       (while threads
3949         (setq sub (car threads))
3950         (if (stringp (car sub))
3951             ;; This is a gathered thread, so we look at the roots
3952             ;; below it to find whether this article is in this
3953             ;; gathered root.
3954             (progn
3955               (setq sub (cdr sub))
3956               (while sub
3957                 (when (member (caar sub) headers)
3958                   (setq thread (car threads)
3959                         threads nil
3960                         sub nil))
3961                 (setq sub (cdr sub))))
3962           ;; It's an ordinary thread, so we check it.
3963           (when (eq (car sub) (car headers))
3964             (setq thread sub
3965                   threads nil)))
3966         (setq threads (cdr threads)))
3967       ;; If this article is in no thread, then it's a root.
3968       (if thread
3969           (unless dont-remove
3970             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3971         (setq thread (gnus-id-to-thread last-id)))
3972       (when thread
3973         (prog1
3974             thread                      ; We return this thread.
3975           (unless dont-remove
3976             (if (stringp (car thread))
3977                 (progn
3978                   ;; If we use dummy roots, then we have to remove the
3979                   ;; dummy root as well.
3980                   (when (eq gnus-summary-make-false-root 'dummy)
3981                     ;; We go to the dummy root by going to
3982                     ;; the first sub-"thread", and then one line up.
3983                     (gnus-summary-goto-article
3984                      (mail-header-number (caadr thread)))
3985                     (forward-line -1)
3986                     (gnus-delete-line)
3987                     (gnus-data-compute-positions))
3988                   (setq thread (cdr thread))
3989                   (while thread
3990                     (gnus-remove-thread-1 (car thread))
3991                     (setq thread (cdr thread))))
3992               (gnus-remove-thread-1 thread))))))))
3993
3994 (defun gnus-remove-thread-1 (thread)
3995   "Remove the thread THREAD recursively."
3996   (let ((number (mail-header-number (pop thread)))
3997         d)
3998     (setq thread (reverse thread))
3999     (while thread
4000       (gnus-remove-thread-1 (pop thread)))
4001     (when (setq d (gnus-data-find number))
4002       (goto-char (gnus-data-pos d))
4003       (gnus-summary-show-thread)
4004       (gnus-data-remove
4005        number
4006        (- (gnus-point-at-bol)
4007           (prog1
4008               (1+ (gnus-point-at-eol))
4009             (gnus-delete-line)))))))
4010
4011 (defun gnus-sort-threads-1 (threads func)
4012   (sort (mapcar (lambda (thread)
4013                   (cons (car thread)
4014                         (and (cdr thread)
4015                              (gnus-sort-threads-1 (cdr thread) func))))
4016                 threads) func))
4017
4018 (defun gnus-sort-threads (threads)
4019   "Sort THREADS."
4020   (if (not gnus-thread-sort-functions)
4021       threads
4022     (gnus-message 8 "Sorting threads...")
4023     (prog1
4024         (gnus-sort-threads-1
4025          threads
4026          (gnus-make-sort-function gnus-thread-sort-functions))
4027       (gnus-message 8 "Sorting threads...done"))))
4028
4029 (defun gnus-sort-articles (articles)
4030   "Sort ARTICLES."
4031   (when gnus-article-sort-functions
4032     (gnus-message 7 "Sorting articles...")
4033     (prog1
4034         (setq gnus-newsgroup-headers
4035               (sort articles (gnus-make-sort-function
4036                               gnus-article-sort-functions)))
4037       (gnus-message 7 "Sorting articles...done"))))
4038
4039 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4040 (defmacro gnus-thread-header (thread)
4041   "Return header of first article in THREAD.
4042 Note that THREAD must never, ever be anything else than a variable -
4043 using some other form will lead to serious barfage."
4044   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4045   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4046   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4047         (vector thread) 2))
4048
4049 (defsubst gnus-article-sort-by-number (h1 h2)
4050   "Sort articles by article number."
4051   (< (mail-header-number h1)
4052      (mail-header-number h2)))
4053
4054 (defun gnus-thread-sort-by-number (h1 h2)
4055   "Sort threads by root article number."
4056   (gnus-article-sort-by-number
4057    (gnus-thread-header h1) (gnus-thread-header h2)))
4058
4059 (defsubst gnus-article-sort-by-lines (h1 h2)
4060   "Sort articles by article Lines header."
4061   (< (mail-header-lines h1)
4062      (mail-header-lines h2)))
4063
4064 (defun gnus-thread-sort-by-lines (h1 h2)
4065   "Sort threads by root article Lines header."
4066   (gnus-article-sort-by-lines
4067    (gnus-thread-header h1) (gnus-thread-header h2)))
4068
4069 (defsubst gnus-article-sort-by-chars (h1 h2)
4070   "Sort articles by octet length."
4071   (< (mail-header-chars h1)
4072      (mail-header-chars h2)))
4073
4074 (defun gnus-thread-sort-by-chars (h1 h2)
4075   "Sort threads by root article octet length."
4076   (gnus-article-sort-by-chars
4077    (gnus-thread-header h1) (gnus-thread-header h2)))
4078
4079 (defsubst gnus-article-sort-by-author (h1 h2)
4080   "Sort articles by root author."
4081   (string-lessp
4082    (let ((addr (car (mime-entity-read-field h1 'From))))
4083      (or (std11-full-name-string addr)
4084          (std11-address-string addr)
4085          ""))
4086    (let ((addr (car (mime-entity-read-field h2 'From))))
4087      (or (std11-full-name-string addr)
4088          (std11-address-string addr)
4089          ""))
4090    ))
4091
4092 (defun gnus-thread-sort-by-author (h1 h2)
4093   "Sort threads by root author."
4094   (gnus-article-sort-by-author
4095    (gnus-thread-header h1)  (gnus-thread-header h2)))
4096
4097 (defsubst gnus-article-sort-by-subject (h1 h2)
4098   "Sort articles by root subject."
4099   (string-lessp
4100    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4101    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4102
4103 (defun gnus-thread-sort-by-subject (h1 h2)
4104   "Sort threads by root subject."
4105   (gnus-article-sort-by-subject
4106    (gnus-thread-header h1) (gnus-thread-header h2)))
4107
4108 (defsubst gnus-article-sort-by-date (h1 h2)
4109   "Sort articles by root article date."
4110   (time-less-p
4111    (gnus-date-get-time (mail-header-date h1))
4112    (gnus-date-get-time (mail-header-date h2))))
4113
4114 (defun gnus-thread-sort-by-date (h1 h2)
4115   "Sort threads by root article date."
4116   (gnus-article-sort-by-date
4117    (gnus-thread-header h1) (gnus-thread-header h2)))
4118
4119 (defsubst gnus-article-sort-by-score (h1 h2)
4120   "Sort articles by root article score.
4121 Unscored articles will be counted as having a score of zero."
4122   (> (or (cdr (assq (mail-header-number h1)
4123                     gnus-newsgroup-scored))
4124          gnus-summary-default-score 0)
4125      (or (cdr (assq (mail-header-number h2)
4126                     gnus-newsgroup-scored))
4127          gnus-summary-default-score 0)))
4128
4129 (defun gnus-thread-sort-by-score (h1 h2)
4130   "Sort threads by root article score."
4131   (gnus-article-sort-by-score
4132    (gnus-thread-header h1) (gnus-thread-header h2)))
4133
4134 (defun gnus-thread-sort-by-total-score (h1 h2)
4135   "Sort threads by the sum of all scores in the thread.
4136 Unscored articles will be counted as having a score of zero."
4137   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4138
4139 (defun gnus-thread-total-score (thread)
4140   ;; This function find the total score of THREAD.
4141   (cond ((null thread)
4142          0)
4143         ((consp thread)
4144          (if (stringp (car thread))
4145              (apply gnus-thread-score-function 0
4146                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4147            (gnus-thread-total-score-1 thread)))
4148         (t
4149          (gnus-thread-total-score-1 (list thread)))))
4150
4151 (defun gnus-thread-total-score-1 (root)
4152   ;; This function find the total score of the thread below ROOT.
4153   (setq root (car root))
4154   (apply gnus-thread-score-function
4155          (or (append
4156               (mapcar 'gnus-thread-total-score
4157                       (cdr (gnus-id-to-thread (mail-header-id root))))
4158               (when (> (mail-header-number root) 0)
4159                 (list (or (cdr (assq (mail-header-number root)
4160                                      gnus-newsgroup-scored))
4161                           gnus-summary-default-score 0))))
4162              (list gnus-summary-default-score)
4163              '(0))))
4164
4165 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4166 (defvar gnus-tmp-prev-subject nil)
4167 (defvar gnus-tmp-false-parent nil)
4168 (defvar gnus-tmp-root-expunged nil)
4169 (defvar gnus-tmp-dummy-line nil)
4170
4171 (eval-when-compile (defvar gnus-tmp-header))
4172 (defun gnus-extra-header (type &optional header)
4173   "Return the extra header of TYPE."
4174   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4175       ""))
4176
4177 (defvar gnus-tmp-thread-tree-header-string "")
4178
4179 (defvar gnus-sum-thread-tree-root "> "
4180   "With %B spec, used for the root of a thread.
4181 If nil, use subject instead.")
4182 (defvar gnus-sum-thread-tree-single-indent ""
4183   "With %B spec, used for a thread with just one message.
4184 If nil, use subject instead.")
4185 (defvar gnus-sum-thread-tree-vertical "| "
4186   "With %B spec, used for drawing a vertical line.")
4187 (defvar gnus-sum-thread-tree-indent "  "
4188   "With %B spec, used for indenting.")
4189 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4190   "With %B spec, used for a leaf with brothers.")
4191 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4192   "With %B spec, used for a leaf without brothers.")
4193
4194 (defun gnus-summary-prepare-threads (threads)
4195   "Prepare summary buffer from THREADS and indentation LEVEL.
4196 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4197 or a straight list of headers."
4198   (gnus-message 7 "Generating summary...")
4199
4200   (setq gnus-newsgroup-threads threads)
4201   (beginning-of-line)
4202
4203   (let ((gnus-tmp-level 0)
4204         (default-score (or gnus-summary-default-score 0))
4205         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4206         thread number subject stack state gnus-tmp-gathered beg-match
4207         new-roots gnus-tmp-new-adopts thread-end
4208         gnus-tmp-header gnus-tmp-unread
4209         gnus-tmp-replied gnus-tmp-subject-or-nil
4210         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4211         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4212         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4213         tree-stack)
4214
4215     (setq gnus-tmp-prev-subject nil)
4216
4217     (if (vectorp (car threads))
4218         ;; If this is a straight (sic) list of headers, then a
4219         ;; threaded summary display isn't required, so we just create
4220         ;; an unthreaded one.
4221         (gnus-summary-prepare-unthreaded threads)
4222
4223       ;; Do the threaded display.
4224
4225       (while (or threads stack gnus-tmp-new-adopts new-roots)
4226
4227         (if (and (= gnus-tmp-level 0)
4228                  (or (not stack)
4229                      (= (caar stack) 0))
4230                  (not gnus-tmp-false-parent)
4231                  (or gnus-tmp-new-adopts new-roots))
4232             (if gnus-tmp-new-adopts
4233                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4234                       thread (list (car gnus-tmp-new-adopts))
4235                       gnus-tmp-header (caar thread)
4236                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4237               (when new-roots
4238                 (setq thread (list (car new-roots))
4239                       gnus-tmp-header (caar thread)
4240                       new-roots (cdr new-roots))))
4241
4242           (if threads
4243               ;; If there are some threads, we do them before the
4244               ;; threads on the stack.
4245               (setq thread threads
4246                     gnus-tmp-header (caar thread))
4247             ;; There were no current threads, so we pop something off
4248             ;; the stack.
4249             (setq state (car stack)
4250                   gnus-tmp-level (car state)
4251                   tree-stack (cadr state)
4252                   thread (caddr state)
4253                   stack (cdr stack)
4254                   gnus-tmp-header (caar thread))))
4255
4256         (setq gnus-tmp-false-parent nil)
4257         (setq gnus-tmp-root-expunged nil)
4258         (setq thread-end nil)
4259
4260         (if (stringp gnus-tmp-header)
4261             ;; The header is a dummy root.
4262             (cond
4263              ((eq gnus-summary-make-false-root 'adopt)
4264               ;; We let the first article adopt the rest.
4265               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4266                                                (cddar thread)))
4267               (setq gnus-tmp-gathered
4268                     (nconc (mapcar
4269                             (lambda (h) (mail-header-number (car h)))
4270                             (cddar thread))
4271                            gnus-tmp-gathered))
4272               (setq thread (cons (list (caar thread)
4273                                        (cadar thread))
4274                                  (cdr thread)))
4275               (setq gnus-tmp-level -1
4276                     gnus-tmp-false-parent t))
4277              ((eq gnus-summary-make-false-root 'empty)
4278               ;; We print adopted articles with empty subject fields.
4279               (setq gnus-tmp-gathered
4280                     (nconc (mapcar
4281                             (lambda (h) (mail-header-number (car h)))
4282                             (cddar thread))
4283                            gnus-tmp-gathered))
4284               (setq gnus-tmp-level -1))
4285              ((eq gnus-summary-make-false-root 'dummy)
4286               ;; We remember that we probably want to output a dummy
4287               ;; root.
4288               (setq gnus-tmp-dummy-line gnus-tmp-header)
4289               (setq gnus-tmp-prev-subject gnus-tmp-header))
4290              (t
4291               ;; We do not make a root for the gathered
4292               ;; sub-threads at all.
4293               (setq gnus-tmp-level -1)))
4294
4295           (setq number (mail-header-number gnus-tmp-header)
4296                 subject (mail-header-subject gnus-tmp-header))
4297
4298           (cond
4299            ;; If the thread has changed subject, we might want to make
4300            ;; this subthread into a root.
4301            ((and (null gnus-thread-ignore-subject)
4302                  (not (zerop gnus-tmp-level))
4303                  gnus-tmp-prev-subject
4304                  (not (inline
4305                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4306             (setq new-roots (nconc new-roots (list (car thread)))
4307                   thread-end t
4308                   gnus-tmp-header nil))
4309            ;; If the article lies outside the current limit,
4310            ;; then we do not display it.
4311            ((not (memq number gnus-newsgroup-limit))
4312             (setq gnus-tmp-gathered
4313                   (nconc (mapcar
4314                           (lambda (h) (mail-header-number (car h)))
4315                           (cdar thread))
4316                          gnus-tmp-gathered))
4317             (setq gnus-tmp-new-adopts (if (cdar thread)
4318                                           (append gnus-tmp-new-adopts
4319                                                   (cdar thread))
4320                                         gnus-tmp-new-adopts)
4321                   thread-end t
4322                   gnus-tmp-header nil)
4323             (when (zerop gnus-tmp-level)
4324               (setq gnus-tmp-root-expunged t)))
4325            ;; Perhaps this article is to be marked as read?
4326            ((and gnus-summary-mark-below
4327                  (< (or (cdr (assq number gnus-newsgroup-scored))
4328                         default-score)
4329                     gnus-summary-mark-below)
4330                  ;; Don't touch sparse articles.
4331                  (not (gnus-summary-article-sparse-p number))
4332                  (not (gnus-summary-article-ancient-p number)))
4333             (setq gnus-newsgroup-unreads
4334                   (delq number gnus-newsgroup-unreads))
4335             (if gnus-newsgroup-auto-expire
4336                 (push number gnus-newsgroup-expirable)
4337               (push (cons number gnus-low-score-mark)
4338                     gnus-newsgroup-reads))))
4339
4340           (when gnus-tmp-header
4341             ;; We may have an old dummy line to output before this
4342             ;; article.
4343             (when (and gnus-tmp-dummy-line
4344                        (gnus-subject-equal
4345                         gnus-tmp-dummy-line
4346                         (mail-header-subject gnus-tmp-header)))
4347               (gnus-summary-insert-dummy-line
4348                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4349               (setq gnus-tmp-dummy-line nil))
4350
4351             ;; Compute the mark.
4352             (setq gnus-tmp-unread (gnus-article-mark number))
4353
4354             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4355                                   gnus-tmp-header gnus-tmp-level)
4356                   gnus-newsgroup-data)
4357
4358             ;; Actually insert the line.
4359             (setq
4360              gnus-tmp-subject-or-nil
4361              (cond
4362               ((and gnus-thread-ignore-subject
4363                     gnus-tmp-prev-subject
4364                     (not (inline (gnus-subject-equal
4365                                   gnus-tmp-prev-subject subject))))
4366                subject)
4367               ((zerop gnus-tmp-level)
4368                (if (and (eq gnus-summary-make-false-root 'empty)
4369                         (memq number gnus-tmp-gathered)
4370                         gnus-tmp-prev-subject
4371                         (inline (gnus-subject-equal
4372                                  gnus-tmp-prev-subject subject)))
4373                    gnus-summary-same-subject
4374                  subject))
4375               (t gnus-summary-same-subject)))
4376             (if (and (eq gnus-summary-make-false-root 'adopt)
4377                      (= gnus-tmp-level 1)
4378                      (memq number gnus-tmp-gathered))
4379                 (setq gnus-tmp-opening-bracket ?\<
4380                       gnus-tmp-closing-bracket ?\>)
4381               (setq gnus-tmp-opening-bracket ?\[
4382                     gnus-tmp-closing-bracket ?\]))
4383             (setq
4384              gnus-tmp-indentation
4385              (aref gnus-thread-indent-array gnus-tmp-level)
4386              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4387              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4388                                 gnus-summary-default-score 0)
4389              gnus-tmp-score-char
4390              (if (or (null gnus-summary-default-score)
4391                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4392                          gnus-summary-zcore-fuzz))
4393                  ?\ ;;;Whitespace
4394                (if (< gnus-tmp-score gnus-summary-default-score)
4395                    gnus-score-below-mark gnus-score-over-mark))
4396              gnus-tmp-replied
4397              (cond ((memq number gnus-newsgroup-processable)
4398                     gnus-process-mark)
4399                    ((memq number gnus-newsgroup-cached)
4400                     gnus-cached-mark)
4401                    ((memq number gnus-newsgroup-replied)
4402                     gnus-replied-mark)
4403                    ((memq number gnus-newsgroup-forwarded)
4404                     gnus-forwarded-mark)
4405                    ((memq number gnus-newsgroup-saved)
4406                     gnus-saved-mark)
4407                    ((memq number gnus-newsgroup-recent)
4408                     gnus-recent-mark)
4409                    ((memq number gnus-newsgroup-unseen)
4410                     gnus-unseen-mark)
4411                    (t gnus-no-mark))
4412              gnus-tmp-from (mail-header-from gnus-tmp-header)
4413              gnus-tmp-name
4414              (cond
4415               ((string-match "<[^>]+> *$" gnus-tmp-from)
4416                (setq beg-match (match-beginning 0))
4417                (or (and (string-match "^\".+\"" gnus-tmp-from)
4418                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4419                    (substring gnus-tmp-from 0 beg-match)))
4420               ((string-match "(.+)" gnus-tmp-from)
4421                (substring gnus-tmp-from
4422                           (1+ (match-beginning 0)) (1- (match-end 0))))
4423               (t gnus-tmp-from))
4424              gnus-tmp-thread-tree-header-string
4425              (cond
4426               ((not gnus-show-threads) "")
4427               ((zerop gnus-tmp-level)
4428                (if (cdar thread)
4429                    (or gnus-sum-thread-tree-root subject)
4430                  (or gnus-sum-thread-tree-single-indent subject)))
4431               (t
4432                (concat (apply 'concat
4433                               (mapcar (lambda (item)
4434                                         (if (= item 1)
4435                                             gnus-sum-thread-tree-vertical
4436                                           gnus-sum-thread-tree-indent))
4437                                       (cdr (reverse tree-stack))))
4438                        (if (nth 1 thread)
4439                            gnus-sum-thread-tree-leaf-with-other
4440                          gnus-sum-thread-tree-single-leaf)))))
4441             (when (string= gnus-tmp-name "")
4442               (setq gnus-tmp-name gnus-tmp-from))
4443             (unless (numberp gnus-tmp-lines)
4444               (setq gnus-tmp-lines -1))
4445             (if (= gnus-tmp-lines -1)
4446                 (setq gnus-tmp-lines "?")
4447               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4448             (gnus-put-text-property
4449              (point)
4450              (progn (eval gnus-summary-line-format-spec) (point))
4451              'gnus-number number)
4452             (when gnus-visual-p
4453               (forward-line -1)
4454               (gnus-run-hooks 'gnus-summary-update-hook)
4455               (forward-line 1))
4456
4457             (setq gnus-tmp-prev-subject subject)))
4458
4459         (when (nth 1 thread)
4460           (push (list (max 0 gnus-tmp-level)
4461                       (copy-list tree-stack)
4462                       (nthcdr 1 thread))
4463                 stack))
4464         (push (if (nth 1 thread) 1 0) tree-stack)
4465         (incf gnus-tmp-level)
4466         (setq threads (if thread-end nil (cdar thread)))
4467         (unless threads
4468           (setq gnus-tmp-level 0)))))
4469   (gnus-message 7 "Generating summary...done"))
4470
4471 (defun gnus-summary-prepare-unthreaded (headers)
4472   "Generate an unthreaded summary buffer based on HEADERS."
4473   (let (header number mark)
4474
4475     (beginning-of-line)
4476
4477     (while headers
4478       ;; We may have to root out some bad articles...
4479       (when (memq (setq number (mail-header-number
4480                                 (setq header (pop headers))))
4481                   gnus-newsgroup-limit)
4482         ;; Mark article as read when it has a low score.
4483         (when (and gnus-summary-mark-below
4484                    (< (or (cdr (assq number gnus-newsgroup-scored))
4485                           gnus-summary-default-score 0)
4486                       gnus-summary-mark-below)
4487                    (not (gnus-summary-article-ancient-p number)))
4488           (setq gnus-newsgroup-unreads
4489                 (delq number gnus-newsgroup-unreads))
4490           (if gnus-newsgroup-auto-expire
4491               (push number gnus-newsgroup-expirable)
4492             (push (cons number gnus-low-score-mark)
4493                   gnus-newsgroup-reads)))
4494
4495         (setq mark (gnus-article-mark number))
4496         (push (gnus-data-make number mark (1+ (point)) header 0)
4497               gnus-newsgroup-data)
4498         (gnus-summary-insert-line
4499          header 0 number
4500          mark (memq number gnus-newsgroup-replied)
4501          (memq number gnus-newsgroup-expirable)
4502          (mail-header-subject header) nil
4503          (cdr (assq number gnus-newsgroup-scored))
4504          (memq number gnus-newsgroup-processable))))))
4505
4506 (defun gnus-summary-remove-list-identifiers ()
4507   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4508   (let ((regexp (if (consp gnus-list-identifiers)
4509                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4510                   gnus-list-identifiers))
4511         changed subject)
4512     (when regexp
4513       (dolist (header gnus-newsgroup-headers)
4514         (setq subject (mail-header-subject header)
4515               changed nil)
4516         (while (string-match
4517                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4518                 subject)
4519           (setq subject
4520                 (concat (substring subject 0 (match-beginning 2))
4521                         (substring subject (match-end 0)))
4522                 changed t))
4523         (when (and changed
4524                    (string-match
4525                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4526           (setq subject
4527                 (concat (substring subject 0 (match-beginning 1))
4528                         (substring subject (match-end 1)))))
4529         (when changed
4530           (mail-header-set-subject header subject))))))
4531
4532 (defun gnus-fetch-headers (articles)
4533   "Fetch headers of ARTICLES."
4534   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4535     (gnus-message 5 "Fetching headers for %s..." name)
4536     (prog1
4537         (if (eq 'nov
4538                 (setq gnus-headers-retrieved-by
4539                       (gnus-retrieve-headers
4540                        articles gnus-newsgroup-name
4541                        ;; We might want to fetch old headers, but
4542                        ;; not if there is only 1 article.
4543                        (and (or (and
4544                                  (not (eq gnus-fetch-old-headers 'some))
4545                                  (not (numberp gnus-fetch-old-headers)))
4546                                 (> (length articles) 1))
4547                             gnus-fetch-old-headers))))
4548             (gnus-get-newsgroup-headers-xover
4549              articles nil nil gnus-newsgroup-name t)
4550           (gnus-get-newsgroup-headers))
4551       (gnus-message 5 "Fetching headers for %s...done" name))))
4552
4553 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4554   "Select newsgroup GROUP.
4555 If READ-ALL is non-nil, all articles in the group are selected.
4556 If SELECT-ARTICLES, only select those articles from GROUP."
4557   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4558          ;;!!! Dirty hack; should be removed.
4559          (gnus-summary-ignore-duplicates
4560           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4561               t
4562             gnus-summary-ignore-duplicates))
4563          (info (nth 2 entry))
4564          articles fetched-articles cached)
4565
4566     (unless (gnus-check-server
4567              (set (make-local-variable 'gnus-current-select-method)
4568                   (gnus-find-method-for-group group)))
4569       (error "Couldn't open server"))
4570
4571     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4572         (gnus-activate-group group)     ; Or we can activate it...
4573         (progn                          ; Or we bug out.
4574           (when (equal major-mode 'gnus-summary-mode)
4575             (kill-buffer (current-buffer)))
4576           (error "Couldn't activate group %s: %s"
4577                  group (gnus-status-message group))))
4578
4579     (unless (gnus-request-group group t)
4580       (when (equal major-mode 'gnus-summary-mode)
4581         (kill-buffer (current-buffer)))
4582       (error "Couldn't request group %s: %s"
4583              group (gnus-status-message group)))
4584
4585     (setq gnus-newsgroup-name group
4586           gnus-newsgroup-unselected nil
4587           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4588
4589     (let ((display (gnus-group-find-parameter group 'display)))
4590       (setq gnus-newsgroup-display
4591             (cond
4592              ((not (zerop (or (car-safe read-all) 0)))
4593               ;; The user entered the group with C-u SPC/RET, let's show
4594               ;; all articles.
4595               'gnus-not-ignore)
4596              ((eq display 'all)
4597               'gnus-not-ignore)
4598              ((arrayp display)
4599               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4600              ((numberp display)
4601               ;; The following is probably the "correct" solution, but
4602               ;; it makes Gnus fetch all headers and then limit the
4603               ;; articles (which is slow), so instead we hack the
4604               ;; select-articles parameter instead. -- Simon Josefsson
4605               ;; <jas@kth.se>
4606               ;;
4607               ;; (gnus-byte-compile
4608               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4609               ;;                         display)))))
4610               (setq select-articles
4611                     (gnus-uncompress-range
4612                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4613                              (if (> tmp 0)
4614                                  tmp
4615                                1))
4616                            (cdr (gnus-active group)))))
4617               nil)
4618              (t
4619               nil))))
4620
4621     (gnus-summary-setup-default-charset)
4622
4623     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4624     (when (gnus-virtual-group-p group)
4625       (setq cached gnus-newsgroup-cached))
4626
4627     (setq gnus-newsgroup-unreads
4628           (gnus-set-difference
4629            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4630            gnus-newsgroup-dormant))
4631
4632     (setq gnus-newsgroup-processable nil)
4633
4634     (gnus-update-read-articles group gnus-newsgroup-unreads)
4635
4636     ;; Adjust and set lists of article marks.
4637     (when info
4638       (gnus-adjust-marked-articles info))
4639
4640     (if (setq articles select-articles)
4641         (setq gnus-newsgroup-unselected
4642               (gnus-sorted-intersection
4643                gnus-newsgroup-unreads
4644                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4645       (setq articles (gnus-articles-to-read group read-all)))
4646
4647     (cond
4648      ((null articles)
4649       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4650       'quit)
4651      ((eq articles 0) nil)
4652      (t
4653       ;; Init the dependencies hash table.
4654       (setq gnus-newsgroup-dependencies
4655             (gnus-make-hashtable (length articles)))
4656       (gnus-set-global-variables)
4657       ;; Retrieve the headers and read them in.
4658       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4659
4660       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4661       (when cached
4662         (setq gnus-newsgroup-cached cached))
4663
4664       ;; Suppress duplicates?
4665       (when gnus-suppress-duplicates
4666         (gnus-dup-suppress-articles))
4667
4668       ;; Set the initial limit.
4669       (setq gnus-newsgroup-limit (copy-sequence articles))
4670       ;; Remove canceled articles from the list of unread articles.
4671       (setq fetched-articles
4672             (mapcar (lambda (headers) (mail-header-number headers))
4673                     gnus-newsgroup-headers))
4674       (setq gnus-newsgroup-articles fetched-articles)
4675       (setq gnus-newsgroup-unreads
4676             (gnus-set-sorted-intersection
4677              gnus-newsgroup-unreads fetched-articles))
4678
4679       (let ((marks (assq 'seen (gnus-info-marks info))))
4680         ;; The `seen' marks are treated specially.
4681         (when (setq gnus-newsgroup-seen (cdr marks))
4682           (dolist (article gnus-newsgroup-articles)
4683             (unless (gnus-member-of-range
4684                      article gnus-newsgroup-seen)
4685               (push article gnus-newsgroup-unseen)))))
4686
4687       ;; Removed marked articles that do not exist.
4688       (gnus-update-missing-marks
4689        (gnus-sorted-complement fetched-articles articles))
4690       ;; We might want to build some more threads first.
4691       (when (and gnus-fetch-old-headers
4692                  (eq gnus-headers-retrieved-by 'nov))
4693         (if (eq gnus-fetch-old-headers 'invisible)
4694             (gnus-build-all-threads)
4695           (gnus-build-old-threads)))
4696       ;; Let the Gnus agent mark articles as read.
4697       (when gnus-agent
4698         (gnus-agent-get-undownloaded-list))
4699       ;; Remove list identifiers from subject
4700       (when gnus-list-identifiers
4701         (gnus-summary-remove-list-identifiers))
4702       ;; Check whether auto-expire is to be done in this group.
4703       (setq gnus-newsgroup-auto-expire
4704             (gnus-group-auto-expirable-p group))
4705       ;; Set up the article buffer now, if necessary.
4706       (unless gnus-single-article-buffer
4707         (gnus-article-setup-buffer))
4708       ;; First and last article in this newsgroup.
4709       (when gnus-newsgroup-headers
4710         (setq gnus-newsgroup-begin
4711               (mail-header-number (car gnus-newsgroup-headers))
4712               gnus-newsgroup-end
4713               (mail-header-number
4714                (gnus-last-element gnus-newsgroup-headers))))
4715       ;; GROUP is successfully selected.
4716       (or gnus-newsgroup-headers t)))))
4717
4718 (defun gnus-summary-display-make-predicate (display)
4719   (require 'gnus-agent)
4720   (when (= (length display) 1)
4721     (setq display (car display)))
4722   (unless gnus-summary-display-cache
4723     (dolist (elem (append (list (cons 'read 'read)
4724                                 (cons 'unseen 'unseen))
4725                           gnus-article-mark-lists))
4726       (push (cons (cdr elem)
4727                   (gnus-byte-compile
4728                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4729             gnus-summary-display-cache)))
4730   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4731     (gnus-get-predicate display)))
4732
4733 ;; Uses the dynamically bound `number' variable.
4734 (defvar number)
4735 (defun gnus-article-marked-p (type &optional article)
4736   (let ((article (or article number)))
4737     (cond
4738      ((eq type 'tick)
4739       (memq article gnus-newsgroup-marked))
4740      ((eq type 'unsend)
4741       (memq article gnus-newsgroup-unsendable))
4742      ((eq type 'undownload)
4743       (memq article gnus-newsgroup-undownloaded))
4744      ((eq type 'download)
4745       (memq article gnus-newsgroup-downloadable))
4746      ((eq type 'unread)
4747       (memq article gnus-newsgroup-unreads))
4748      ((eq type 'read)
4749       (memq article gnus-newsgroup-reads))
4750      ((eq type 'dormant)
4751       (memq article gnus-newsgroup-dormant) )
4752      ((eq type 'expire)
4753       (memq article gnus-newsgroup-expirable))
4754      ((eq type 'reply)
4755       (memq article gnus-newsgroup-replied))
4756      ((eq type 'killed)
4757       (memq article gnus-newsgroup-killed))
4758      ((eq type 'bookmark)
4759       (assq article gnus-newsgroup-bookmarks))
4760      ((eq type 'score)
4761       (assq article gnus-newsgroup-scored))
4762      ((eq type 'save)
4763       (memq article gnus-newsgroup-saved))
4764      ((eq type 'cache)
4765       (memq article gnus-newsgroup-cached))
4766      ((eq type 'forward)
4767       (memq article gnus-newsgroup-forwarded))
4768      ((eq type 'seen)
4769       (not (memq article gnus-newsgroup-unseen)))
4770      ((eq type 'recent)
4771       (memq article gnus-newsgroup-recent))
4772      (t t))))
4773
4774 (defun gnus-articles-to-read (group &optional read-all)
4775   "Find out what articles the user wants to read."
4776   (let* ((articles
4777           ;; Select all articles if `read-all' is non-nil, or if there
4778           ;; are no unread articles.
4779           (if (or read-all
4780                   (and (zerop (length gnus-newsgroup-marked))
4781                        (zerop (length gnus-newsgroup-unreads)))
4782                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4783               ;; We want to select the headers for all the articles in
4784               ;; the group, so we select either all the active
4785               ;; articles in the group, or (if that's nil), the
4786               ;; articles in the cache.
4787               (or
4788                (gnus-uncompress-range (gnus-active group))
4789                (gnus-cache-articles-in-group group))
4790             ;; Select only the "normal" subset of articles.
4791             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4792                           (copy-sequence gnus-newsgroup-unreads))
4793                   '<)))
4794          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4795          (scored (length scored-list))
4796          (number (length articles))
4797          (marked (+ (length gnus-newsgroup-marked)
4798                     (length gnus-newsgroup-dormant)))
4799          (select
4800           (cond
4801            ((numberp read-all)
4802             read-all)
4803            (t
4804             (condition-case ()
4805                 (cond
4806                  ((and (or (<= scored marked) (= scored number))
4807                        (natnump gnus-large-newsgroup)
4808                        (> number gnus-large-newsgroup))
4809                   (let* ((cursor-in-echo-area nil)
4810                          (input
4811                           (read-from-minibuffer
4812                            (format
4813                             "How many articles from %s (max %d): "
4814                             (gnus-limit-string
4815                              (gnus-group-decoded-name gnus-newsgroup-name)
4816                              35)
4817                             number)
4818                            (cons (number-to-string gnus-large-newsgroup)
4819                                  0))))
4820                     (if (string-match "^[ \t]*$" input)
4821                         number
4822                       input)))
4823                  ((and (> scored marked) (< scored number)
4824                        (> (- scored number) 20))
4825                   (let ((input
4826                          (read-string
4827                           (format "%s %s (%d scored, %d total): "
4828                                   "How many articles from"
4829                                   (gnus-group-decoded-name group)
4830                                   scored number))))
4831                     (if (string-match "^[ \t]*$" input)
4832                         number input)))
4833                  (t number))
4834               (quit
4835                (message "Quit getting the articles to read")
4836                nil))))))
4837     (setq select (if (stringp select) (string-to-number select) select))
4838     (if (or (null select) (zerop select))
4839         select
4840       (if (and (not (zerop scored)) (<= (abs select) scored))
4841           (progn
4842             (setq articles (sort scored-list '<))
4843             (setq number (length articles)))
4844         (setq articles (copy-sequence articles)))
4845
4846       (when (< (abs select) number)
4847         (if (< select 0)
4848             ;; Select the N oldest articles.
4849             (setcdr (nthcdr (1- (abs select)) articles) nil)
4850           ;; Select the N most recent articles.
4851           (setq articles (nthcdr (- number select) articles))))
4852       (setq gnus-newsgroup-unselected
4853             (gnus-sorted-intersection
4854              gnus-newsgroup-unreads
4855              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4856       (when gnus-alter-articles-to-read-function
4857         (setq gnus-newsgroup-unreads
4858               (sort
4859                (funcall gnus-alter-articles-to-read-function
4860                         gnus-newsgroup-name gnus-newsgroup-unreads)
4861                '<)))
4862       articles)))
4863
4864 (defun gnus-killed-articles (killed articles)
4865   (let (out)
4866     (while articles
4867       (when (inline (gnus-member-of-range (car articles) killed))
4868         (push (car articles) out))
4869       (setq articles (cdr articles)))
4870     out))
4871
4872 (defun gnus-uncompress-marks (marks)
4873   "Uncompress the mark ranges in MARKS."
4874   (let ((uncompressed '(score bookmark))
4875         out)
4876     (while marks
4877       (if (memq (caar marks) uncompressed)
4878           (push (car marks) out)
4879         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4880       (setq marks (cdr marks)))
4881     out))
4882
4883 (defun gnus-article-mark-to-type (mark)
4884   "Return the type of MARK."
4885   (or (cadr (assq mark gnus-article-special-mark-lists))
4886       'list))
4887
4888 (defun gnus-article-unpropagatable-p (mark)
4889   "Return whether MARK should be propagated to backend."
4890   (memq mark gnus-article-unpropagated-mark-lists))
4891
4892 (defun gnus-adjust-marked-articles (info)
4893   "Set all article lists and remove all marks that are no longer valid."
4894   (let* ((marked-lists (gnus-info-marks info))
4895          (active (gnus-active (gnus-info-group info)))
4896          (min (car active))
4897          (max (cdr active))
4898          (types gnus-article-mark-lists)
4899          marks var articles article mark mark-type)
4900
4901     (dolist (marks marked-lists)
4902       (setq mark (car marks)
4903             mark-type (gnus-article-mark-to-type mark)
4904             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4905
4906       ;; We set the variable according to the type of the marks list,
4907       ;; and then adjust the marks to a subset of the active articles.
4908       (cond
4909        ;; Adjust "simple" lists.
4910        ((eq mark-type 'list)
4911         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4912         (when (memq mark '(tick dormant expire reply save))
4913           (while articles
4914             (when (or (< (setq article (pop articles)) min) (> article max))
4915               (set var (delq article (symbol-value var)))))))
4916        ;; Adjust assocs.
4917        ((eq mark-type 'tuple)
4918         (set var (setq articles (cdr marks)))
4919         (when (not (listp (cdr (symbol-value var))))
4920           (set var (list (symbol-value var))))
4921         (when (not (listp (cdr articles)))
4922           (setq articles (list articles)))
4923         (while articles
4924           (when (or (not (consp (setq article (pop articles))))
4925                     (< (car article) min)
4926                     (> (car article) max))
4927             (set var (delq article (symbol-value var))))))
4928        ((eq mark-type 'range)
4929         (cond
4930          ((eq mark 'seen))))))))
4931
4932 (defun gnus-update-missing-marks (missing)
4933   "Go through the list of MISSING articles and remove them from the mark lists."
4934   (when missing
4935     (let (var m)
4936       ;; Go through all types.
4937       (dolist (elem gnus-article-mark-lists)
4938         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4939           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4940           (when (symbol-value var)
4941             ;; This list has articles.  So we delete all missing
4942             ;; articles from it.
4943             (setq m missing)
4944             (while m
4945               (set var (delq (pop m) (symbol-value var))))))))))
4946
4947 (defun gnus-update-marks ()
4948   "Enter the various lists of marked articles into the newsgroup info list."
4949   (let ((types gnus-article-mark-lists)
4950         (info (gnus-get-info gnus-newsgroup-name))
4951         type list newmarked symbol delta-marks)
4952     (when info
4953       ;; Add all marks lists to the list of marks lists.
4954       (while (setq type (pop types))
4955         (setq list (symbol-value
4956                     (setq symbol
4957                           (intern (format "gnus-newsgroup-%s" (car type))))))
4958
4959         (when list
4960           ;; Get rid of the entries of the articles that have the
4961           ;; default score.
4962           (when (and (eq (cdr type) 'score)
4963                      gnus-save-score
4964                      list)
4965             (let* ((arts list)
4966                    (prev (cons nil list))
4967                    (all prev))
4968               (while arts
4969                 (if (or (not (consp (car arts)))
4970                         (= (cdar arts) gnus-summary-default-score))
4971                     (setcdr prev (cdr arts))
4972                   (setq prev arts))
4973                 (setq arts (cdr arts)))
4974               (setq list (cdr all)))))
4975
4976         (when (eq (cdr type) 'seen)
4977           (setq list
4978                 (if list
4979                     (gnus-add-to-range list gnus-newsgroup-unseen)
4980                   (gnus-compress-sequence gnus-newsgroup-articles))))
4981
4982         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
4983           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4984
4985         (when (and (gnus-check-backend-function
4986                     'request-set-mark gnus-newsgroup-name)
4987                    (not (gnus-article-unpropagatable-p (cdr type))))
4988           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4989                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4990                  (add (gnus-remove-from-range
4991                        (gnus-copy-sequence list) old)))
4992             (when add
4993               (push (list add 'add (list (cdr type))) delta-marks))
4994             (when del
4995               (push (list del 'del (list (cdr type))) delta-marks))))
4996
4997         (when list
4998           (push (cons (cdr type) list) newmarked)))
4999
5000       (when delta-marks
5001         (unless (gnus-check-group gnus-newsgroup-name)
5002           (error "Can't open server for %s" gnus-newsgroup-name))
5003         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5004
5005       ;; Enter these new marks into the info of the group.
5006       (if (nthcdr 3 info)
5007           (setcar (nthcdr 3 info) newmarked)
5008         ;; Add the marks lists to the end of the info.
5009         (when newmarked
5010           (setcdr (nthcdr 2 info) (list newmarked))))
5011
5012       ;; Cut off the end of the info if there's nothing else there.
5013       (let ((i 5))
5014         (while (and (> i 2)
5015                     (not (nth i info)))
5016           (when (nthcdr (decf i) info)
5017             (setcdr (nthcdr i info) nil)))))))
5018
5019 (defun gnus-set-mode-line (where)
5020   "Set the mode line of the article or summary buffers.
5021 If WHERE is `summary', the summary mode line format will be used."
5022   ;; Is this mode line one we keep updated?
5023   (when (and (memq where gnus-updated-mode-lines)
5024              (symbol-value
5025               (intern (format "gnus-%s-mode-line-format-spec" where))))
5026     (let (mode-string)
5027       (save-excursion
5028         ;; We evaluate this in the summary buffer since these
5029         ;; variables are buffer-local to that buffer.
5030         (set-buffer gnus-summary-buffer)
5031         ;; We bind all these variables that are used in the `eval' form
5032         ;; below.
5033         (let* ((mformat (symbol-value
5034                          (intern
5035                           (format "gnus-%s-mode-line-format-spec" where))))
5036                (gnus-tmp-group-name (gnus-group-decoded-name
5037                                      gnus-newsgroup-name))
5038                (gnus-tmp-article-number (or gnus-current-article 0))
5039                (gnus-tmp-unread gnus-newsgroup-unreads)
5040                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5041                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5042                (gnus-tmp-unread-and-unselected
5043                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5044                             (zerop gnus-tmp-unselected))
5045                        "")
5046                       ((zerop gnus-tmp-unselected)
5047                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5048                       (t (format "{%d(+%d) more}"
5049                                  gnus-tmp-unread-and-unticked
5050                                  gnus-tmp-unselected))))
5051                (gnus-tmp-subject
5052                 (if (and gnus-current-headers
5053                          (vectorp gnus-current-headers))
5054                     (gnus-mode-string-quote
5055                      (mail-header-subject gnus-current-headers))
5056                   ""))
5057                bufname-length max-len
5058                gnus-tmp-header);; passed as argument to any user-format-funcs
5059           (setq mode-string (eval mformat))
5060           (setq bufname-length (if (string-match "%b" mode-string)
5061                                    (- (length
5062                                        (buffer-name
5063                                         (if (eq where 'summary)
5064                                             nil
5065                                           (get-buffer gnus-article-buffer))))
5066                                       2)
5067                                  0))
5068           (setq max-len (max 4 (if gnus-mode-non-string-length
5069                                    (- (window-width)
5070                                       gnus-mode-non-string-length
5071                                       bufname-length)
5072                                  (length mode-string))))
5073           ;; We might have to chop a bit of the string off...
5074           (when (> (length mode-string) max-len)
5075             (setq mode-string
5076                   (concat (gnus-truncate-string mode-string (- max-len 3))
5077                           "...")))
5078           ;; Pad the mode string a bit.
5079           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5080       ;; Update the mode line.
5081       (setq mode-line-buffer-identification
5082             (gnus-mode-line-buffer-identification (list mode-string)))
5083       (set-buffer-modified-p t))))
5084
5085 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5086   "Go through the HEADERS list and add all Xrefs to a hash table.
5087 The resulting hash table is returned, or nil if no Xrefs were found."
5088   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5089          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5090          (xref-hashtb (gnus-make-hashtable))
5091          start group entry number xrefs header)
5092     (while headers
5093       (setq header (pop headers))
5094       (when (and (setq xrefs (mail-header-xref header))
5095                  (not (memq (setq number (mail-header-number header))
5096                             unreads)))
5097         (setq start 0)
5098         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5099           (setq start (match-end 0))
5100           (setq group (if prefix
5101                           (concat prefix (substring xrefs (match-beginning 1)
5102                                                     (match-end 1)))
5103                         (substring xrefs (match-beginning 1) (match-end 1))))
5104           (setq number
5105                 (string-to-int (substring xrefs (match-beginning 2)
5106                                           (match-end 2))))
5107           (if (setq entry (gnus-gethash group xref-hashtb))
5108               (setcdr entry (cons number (cdr entry)))
5109             (gnus-sethash group (cons number nil) xref-hashtb)))))
5110     (and start xref-hashtb)))
5111
5112 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5113   "Look through all the headers and mark the Xrefs as read."
5114   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5115         name entry info xref-hashtb idlist method nth4)
5116     (save-excursion
5117       (set-buffer gnus-group-buffer)
5118       (when (setq xref-hashtb
5119                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5120         (mapatoms
5121          (lambda (group)
5122            (unless (string= from-newsgroup (setq name (symbol-name group)))
5123              (setq idlist (symbol-value group))
5124              ;; Dead groups are not updated.
5125              (and (prog1
5126                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5127                             info (nth 2 entry))
5128                     (when (stringp (setq nth4 (gnus-info-method info)))
5129                       (setq nth4 (gnus-server-to-method nth4))))
5130                   ;; Only do the xrefs if the group has the same
5131                   ;; select method as the group we have just read.
5132                   (or (gnus-methods-equal-p
5133                        nth4 (gnus-find-method-for-group from-newsgroup))
5134                       virtual
5135                       (equal nth4 (setq method (gnus-find-method-for-group
5136                                                 from-newsgroup)))
5137                       (and (equal (car nth4) (car method))
5138                            (equal (nth 1 nth4) (nth 1 method))))
5139                   gnus-use-cross-reference
5140                   (or (not (eq gnus-use-cross-reference t))
5141                       virtual
5142                       ;; Only do cross-references on subscribed
5143                       ;; groups, if that is what is wanted.
5144                       (<= (gnus-info-level info) gnus-level-subscribed))
5145                   (gnus-group-make-articles-read name idlist))))
5146          xref-hashtb)))))
5147
5148 (defun gnus-compute-read-articles (group articles)
5149   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5150          (info (nth 2 entry))
5151          (active (gnus-active group))
5152          ninfo)
5153     (when entry
5154       ;; First peel off all invalid article numbers.
5155       (when active
5156         (let ((ids articles)
5157               id first)
5158           (while (setq id (pop ids))
5159             (when (and first (> id (cdr active)))
5160               ;; We'll end up in this situation in one particular
5161               ;; obscure situation.  If you re-scan a group and get
5162               ;; a new article that is cross-posted to a different
5163               ;; group that has not been re-scanned, you might get
5164               ;; crossposted article that has a higher number than
5165               ;; Gnus believes possible.  So we re-activate this
5166               ;; group as well.  This might mean doing the
5167               ;; crossposting thingy will *increase* the number
5168               ;; of articles in some groups.  Tsk, tsk.
5169               (setq active (or (gnus-activate-group group) active)))
5170             (when (or (> id (cdr active))
5171                       (< id (car active)))
5172               (setq articles (delq id articles))))))
5173       ;; If the read list is nil, we init it.
5174       (if (and active
5175                (null (gnus-info-read info))
5176                (> (car active) 1))
5177           (setq ninfo (cons 1 (1- (car active))))
5178         (setq ninfo (gnus-info-read info)))
5179       ;; Then we add the read articles to the range.
5180       (gnus-add-to-range
5181        ninfo (setq articles (sort articles '<))))))
5182
5183 (defun gnus-group-make-articles-read (group articles)
5184   "Update the info of GROUP to say that ARTICLES are read."
5185   (let* ((num 0)
5186          (entry (gnus-gethash group gnus-newsrc-hashtb))
5187          (info (nth 2 entry))
5188          (active (gnus-active group))
5189          range)
5190     (when entry
5191       (setq range (gnus-compute-read-articles group articles))
5192       (save-excursion
5193         (set-buffer gnus-group-buffer)
5194         (gnus-undo-register
5195           `(progn
5196              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5197              (gnus-info-set-read ',info ',(gnus-info-read info))
5198              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5199              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5200              (gnus-group-update-group ,group t))))
5201       ;; Add the read articles to the range.
5202       (gnus-info-set-read info range)
5203       (gnus-request-set-mark group (list (list range 'add '(read))))
5204       ;; Then we have to re-compute how many unread
5205       ;; articles there are in this group.
5206       (when active
5207         (cond
5208          ((not range)
5209           (setq num (- (1+ (cdr active)) (car active))))
5210          ((not (listp (cdr range)))
5211           (setq num (- (cdr active) (- (1+ (cdr range))
5212                                        (car range)))))
5213          (t
5214           (while range
5215             (if (numberp (car range))
5216                 (setq num (1+ num))
5217               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5218             (setq range (cdr range)))
5219           (setq num (- (cdr active) num))))
5220         ;; Update the number of unread articles.
5221         (setcar entry num)
5222         ;; Update the group buffer.
5223         (gnus-group-update-group group t)))))
5224
5225 (defvar gnus-newsgroup-none-id 0)
5226
5227 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5228   (let ((cur nntp-server-buffer)
5229         (dependencies
5230          (or dependencies
5231              (save-excursion (set-buffer gnus-summary-buffer)
5232                              gnus-newsgroup-dependencies)))
5233         headers id end ref
5234         (mail-parse-charset gnus-newsgroup-charset)
5235         (mail-parse-ignored-charsets
5236          (save-excursion (condition-case nil
5237                              (set-buffer gnus-summary-buffer)
5238                            (error))
5239                          gnus-newsgroup-ignored-charsets)))
5240     (save-excursion
5241       (set-buffer nntp-server-buffer)
5242       ;; Translate all TAB characters into SPACE characters.
5243       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5244       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5245       (gnus-run-hooks 'gnus-parse-headers-hook)
5246       (let ((case-fold-search t)
5247             in-reply-to header p lines chars ctype)
5248         (goto-char (point-min))
5249         ;; Search to the beginning of the next header.  Error messages
5250         ;; do not begin with 2 or 3.
5251         (while (re-search-forward "^[23][0-9]+ " nil t)
5252           (setq id nil
5253                 ref nil)
5254           ;; This implementation of this function, with nine
5255           ;; search-forwards instead of the one re-search-forward and
5256           ;; a case (which basically was the old function) is actually
5257           ;; about twice as fast, even though it looks messier.  You
5258           ;; can't have everything, I guess.  Speed and elegance
5259           ;; doesn't always go hand in hand.
5260           (setq
5261            header
5262            (make-full-mail-header
5263             ;; Number.
5264             (prog1
5265                 (read cur)
5266               (end-of-line)
5267               (setq p (point))
5268               (narrow-to-region (point)
5269                                 (or (and (search-forward "\n.\n" nil t)
5270                                          (- (point) 2))
5271                                     (point))))
5272             ;; Subject.
5273             (progn
5274               (goto-char p)
5275               (if (search-forward "\nsubject:" nil t)
5276                   (nnheader-header-value)
5277                 "(none)"))
5278             ;; From.
5279             (progn
5280               (goto-char p)
5281               (if (search-forward "\nfrom:" nil t)
5282                   (nnheader-header-value)
5283                 "(nobody)"))
5284             ;; Date.
5285             (progn
5286               (goto-char p)
5287               (if (search-forward "\ndate:" nil t)
5288                   (nnheader-header-value) ""))
5289             ;; Message-ID.
5290             (progn
5291               (goto-char p)
5292               (setq id (if (re-search-forward
5293                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5294                            ;; We do it this way to make sure the Message-ID
5295                            ;; is (somewhat) syntactically valid.
5296                            (buffer-substring (match-beginning 1)
5297                                              (match-end 1))
5298                          ;; If there was no message-id, we just fake one
5299                          ;; to make subsequent routines simpler.
5300                          (nnheader-generate-fake-message-id))))
5301             ;; References.
5302             (progn
5303               (goto-char p)
5304               (if (search-forward "\nreferences:" nil t)
5305                   (progn
5306                     (setq end (point))
5307                     (prog1
5308                         (nnheader-header-value)
5309                       (setq ref
5310                             (buffer-substring
5311                              (progn
5312                                ;; (end-of-line)
5313                                (search-backward ">" end t)
5314                                (1+ (point)))
5315                              (progn
5316                                (search-backward "<" end t)
5317                                (point))))))
5318                 ;; Get the references from the in-reply-to header if there
5319                 ;; were no references and the in-reply-to header looks
5320                 ;; promising.
5321                 (if (and (search-forward "\nin-reply-to:" nil t)
5322                          (setq in-reply-to (nnheader-header-value))
5323                          (string-match "<[^>]+>" in-reply-to))
5324                     (let (ref2)
5325                       (setq ref (substring in-reply-to (match-beginning 0)
5326                                            (match-end 0)))
5327                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5328                         (setq ref2 (substring in-reply-to (match-beginning 0)
5329                                               (match-end 0)))
5330                         (when (> (length ref2) (length ref))
5331                           (setq ref ref2)))
5332                       ref)
5333                   (setq ref nil))))
5334             ;; Chars.
5335             (progn
5336               (goto-char p)
5337               (if (search-forward "\nchars: " nil t)
5338                   (if (numberp (setq chars (ignore-errors (read cur))))
5339                       chars -1)
5340                 -1))
5341             ;; Lines.
5342             (progn
5343               (goto-char p)
5344               (if (search-forward "\nlines: " nil t)
5345                   (if (numberp (setq lines (ignore-errors (read cur))))
5346                       lines -1)
5347                 -1))
5348             ;; Xref.
5349             (progn
5350               (goto-char p)
5351               (and (search-forward "\nxref:" nil t)
5352                    (nnheader-header-value)))
5353             ;; Extra.
5354             (when gnus-extra-headers
5355               (let ((extra gnus-extra-headers)
5356                     out)
5357                 (while extra
5358                   (goto-char p)
5359                   (when (search-forward
5360                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5361                     (push (cons (car extra) (nnheader-header-value)) out))
5362                   (pop extra))
5363                 out))))
5364           (goto-char p)
5365           (if (and (search-forward "\ncontent-type: " nil t)
5366                    (setq ctype (nnheader-header-value)))
5367               (mime-entity-set-content-type-internal
5368                header (mime-parse-Content-Type ctype)))
5369           (when (equal id ref)
5370             (setq ref nil))
5371
5372           (when gnus-alter-header-function
5373             (funcall gnus-alter-header-function header)
5374             (setq id (mail-header-id header)
5375                   ref (gnus-parent-id (mail-header-references header))))
5376
5377           (when (setq header
5378                       (gnus-dependencies-add-header
5379                        header dependencies force-new))
5380             (push header headers))
5381           (goto-char (point-max))
5382           (widen))
5383         (nreverse headers)))))
5384
5385 ;; Goes through the xover lines and returns a list of vectors
5386 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5387                                                   force-new dependencies
5388                                                   group also-fetch-heads)
5389   "Parse the news overview data in the server buffer.
5390 Return a list of headers that match SEQUENCE (see
5391 `nntp-retrieve-headers')."
5392   ;; Get the Xref when the users reads the articles since most/some
5393   ;; NNTP servers do not include Xrefs when using XOVER.
5394   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5395   (let ((mail-parse-charset gnus-newsgroup-charset)
5396         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5397         (cur nntp-server-buffer)
5398         (dependencies (or dependencies gnus-newsgroup-dependencies))
5399         (allp (cond
5400                ((eq gnus-read-all-available-headers t)
5401                 t)
5402                ((stringp gnus-read-all-available-headers)
5403                 (string-match gnus-read-all-available-headers group))
5404                (t
5405                 nil)))
5406         number headers header)
5407     (save-excursion
5408       (set-buffer nntp-server-buffer)
5409       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5410       ;; Allow the user to mangle the headers before parsing them.
5411       (gnus-run-hooks 'gnus-parse-headers-hook)
5412       (goto-char (point-min))
5413       (while (not (eobp))
5414         (condition-case ()
5415             (while (and (or sequence allp)
5416                         (not (eobp)))
5417               (setq number (read cur))
5418               (when (not allp)
5419                 (while (and sequence
5420                             (< (car sequence) number))
5421                   (setq sequence (cdr sequence))))
5422               (when (and (or allp
5423                              (and sequence
5424                                   (eq number (car sequence))))
5425                          (progn
5426                            (setq sequence (cdr sequence))
5427                            (setq header (inline
5428                                           (gnus-nov-parse-line
5429                                            number dependencies force-new)))))
5430                 (push header headers))
5431               (forward-line 1))
5432           (error
5433            (gnus-error 4 "Strange nov line (%d)"
5434                        (count-lines (point-min) (point)))))
5435         (forward-line 1))
5436       ;; A common bug in inn is that if you have posted an article and
5437       ;; then retrieves the active file, it will answer correctly --
5438       ;; the new article is included.  However, a NOV entry for the
5439       ;; article may not have been generated yet, so this may fail.
5440       ;; We work around this problem by retrieving the last few
5441       ;; headers using HEAD.
5442       (if (or (not also-fetch-heads)
5443               (not sequence))
5444           ;; We (probably) got all the headers.
5445           (nreverse headers)
5446         (let ((gnus-nov-is-evil t))
5447           (nconc
5448            (nreverse headers)
5449            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5450              (gnus-get-newsgroup-headers))))))))
5451
5452 (defun gnus-article-get-xrefs ()
5453   "Fill in the Xref value in `gnus-current-headers', if necessary.
5454 This is meant to be called in `gnus-article-internal-prepare-hook'."
5455   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5456                                  gnus-current-headers)))
5457     (or (not gnus-use-cross-reference)
5458         (not headers)
5459         (and (mail-header-xref headers)
5460              (not (string= (mail-header-xref headers) "")))
5461         (let ((case-fold-search t)
5462               xref)
5463           (save-restriction
5464             (nnheader-narrow-to-headers)
5465             (goto-char (point-min))
5466             (when (or (and (not (eobp))
5467                            (eq (downcase (char-after)) ?x)
5468                            (looking-at "Xref:"))
5469                       (search-forward "\nXref:" nil t))
5470               (goto-char (1+ (match-end 0)))
5471               (setq xref (buffer-substring (point)
5472                                            (progn (end-of-line) (point))))
5473               (mail-header-set-xref headers xref)))))))
5474
5475 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5476   "Find article ID and insert the summary line for that article.
5477 OLD-HEADER can either be a header or a line number to insert
5478 the subject line on."
5479   (let* ((line (and (numberp old-header) old-header))
5480          (old-header (and (vectorp old-header) old-header))
5481          (header (cond ((and old-header use-old-header)
5482                         old-header)
5483                        ((and (numberp id)
5484                              (gnus-number-to-header id))
5485                         (gnus-number-to-header id))
5486                        (t
5487                         (gnus-read-header id))))
5488          (number (and (numberp id) id))
5489          d)
5490     (when header
5491       ;; Rebuild the thread that this article is part of and go to the
5492       ;; article we have fetched.
5493       (when (and (not gnus-show-threads)
5494                  old-header)
5495         (when (and number
5496                    (setq d (gnus-data-find (mail-header-number old-header))))
5497           (goto-char (gnus-data-pos d))
5498           (gnus-data-remove
5499            number
5500            (- (gnus-point-at-bol)
5501               (prog1
5502                   (1+ (gnus-point-at-eol))
5503                 (gnus-delete-line))))))
5504       (when old-header
5505         (mail-header-set-number header (mail-header-number old-header)))
5506       (setq gnus-newsgroup-sparse
5507             (delq (setq number (mail-header-number header))
5508                   gnus-newsgroup-sparse))
5509       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5510       (push number gnus-newsgroup-limit)
5511       (gnus-rebuild-thread (mail-header-id header) line)
5512       (gnus-summary-goto-subject number nil t))
5513     (when (and (numberp number)
5514                (> number 0))
5515       ;; We have to update the boundaries even if we can't fetch the
5516       ;; article if ID is a number -- so that the next `P' or `N'
5517       ;; command will fetch the previous (or next) article even
5518       ;; if the one we tried to fetch this time has been canceled.
5519       (when (> number gnus-newsgroup-end)
5520         (setq gnus-newsgroup-end number))
5521       (when (< number gnus-newsgroup-begin)
5522         (setq gnus-newsgroup-begin number))
5523       (setq gnus-newsgroup-unselected
5524             (delq number gnus-newsgroup-unselected)))
5525     ;; Report back a success?
5526     (and header (mail-header-number header))))
5527
5528 ;;; Process/prefix in the summary buffer
5529
5530 (defun gnus-summary-work-articles (n)
5531   "Return a list of articles to be worked upon.
5532 The prefix argument, the list of process marked articles, and the
5533 current article will be taken into consideration."
5534   (save-excursion
5535     (set-buffer gnus-summary-buffer)
5536     (cond
5537      (n
5538       ;; A numerical prefix has been given.
5539       (setq n (prefix-numeric-value n))
5540       (let ((backward (< n 0))
5541             (n (abs (prefix-numeric-value n)))
5542             articles article)
5543         (save-excursion
5544           (while
5545               (and (> n 0)
5546                    (push (setq article (gnus-summary-article-number))
5547                          articles)
5548                    (if backward
5549                        (gnus-summary-find-prev nil article)
5550                      (gnus-summary-find-next nil article)))
5551             (decf n)))
5552         (nreverse articles)))
5553      ((and (gnus-region-active-p) (mark))
5554       (message "region active")
5555       ;; Work on the region between point and mark.
5556       (let ((max (max (point) (mark)))
5557             articles article)
5558         (save-excursion
5559           (goto-char (min (point) (mark)))
5560           (while
5561               (and
5562                (push (setq article (gnus-summary-article-number)) articles)
5563                (gnus-summary-find-next nil article)
5564                (< (point) max)))
5565           (nreverse articles))))
5566      (gnus-newsgroup-processable
5567       ;; There are process-marked articles present.
5568       ;; Save current state.
5569       (gnus-summary-save-process-mark)
5570       ;; Return the list.
5571       (reverse gnus-newsgroup-processable))
5572      (t
5573       ;; Just return the current article.
5574       (list (gnus-summary-article-number))))))
5575
5576 (defmacro gnus-summary-iterate (arg &rest forms)
5577   "Iterate over the process/prefixed articles and do FORMS.
5578 ARG is the interactive prefix given to the command.  FORMS will be
5579 executed with point over the summary line of the articles."
5580   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5581     `(let ((,articles (gnus-summary-work-articles ,arg)))
5582        (while ,articles
5583          (gnus-summary-goto-subject (car ,articles))
5584          ,@forms
5585          (pop ,articles)))))
5586
5587 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5588 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5589
5590 (defun gnus-summary-save-process-mark ()
5591   "Push the current set of process marked articles on the stack."
5592   (interactive)
5593   (push (copy-sequence gnus-newsgroup-processable)
5594         gnus-newsgroup-process-stack))
5595
5596 (defun gnus-summary-kill-process-mark ()
5597   "Push the current set of process marked articles on the stack and unmark."
5598   (interactive)
5599   (gnus-summary-save-process-mark)
5600   (gnus-summary-unmark-all-processable))
5601
5602 (defun gnus-summary-yank-process-mark ()
5603   "Pop the last process mark state off the stack and restore it."
5604   (interactive)
5605   (unless gnus-newsgroup-process-stack
5606     (error "Empty mark stack"))
5607   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5608
5609 (defun gnus-summary-process-mark-set (set)
5610   "Make SET into the current process marked articles."
5611   (gnus-summary-unmark-all-processable)
5612   (while set
5613     (gnus-summary-set-process-mark (pop set))))
5614
5615 ;;; Searching and stuff
5616
5617 (defun gnus-summary-search-group (&optional backward use-level)
5618   "Search for next unread newsgroup.
5619 If optional argument BACKWARD is non-nil, search backward instead."
5620   (save-excursion
5621     (set-buffer gnus-group-buffer)
5622     (when (gnus-group-search-forward
5623            backward nil (if use-level (gnus-group-group-level) nil))
5624       (gnus-group-group-name))))
5625
5626 (defun gnus-summary-best-group (&optional exclude-group)
5627   "Find the name of the best unread group.
5628 If EXCLUDE-GROUP, do not go to this group."
5629   (save-excursion
5630     (set-buffer gnus-group-buffer)
5631     (save-excursion
5632       (gnus-group-best-unread-group exclude-group))))
5633
5634 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5635   (if backward (gnus-summary-find-prev)
5636     (let* ((dummy (gnus-summary-article-intangible-p))
5637            (article (or article (gnus-summary-article-number)))
5638            (arts (gnus-data-find-list article))
5639            result)
5640       (when (and (not dummy)
5641                  (or (not gnus-summary-check-current)
5642                      (not unread)
5643                      (not (gnus-data-unread-p (car arts)))))
5644         (setq arts (cdr arts)))
5645       (when (setq result
5646                   (if unread
5647                       (progn
5648                         (while arts
5649                           (when (or (and undownloaded
5650                                          (eq gnus-undownloaded-mark
5651                                              (gnus-data-mark (car arts))))
5652                                     (gnus-data-unread-p (car arts)))
5653                             (setq result (car arts)
5654                                   arts nil))
5655                           (setq arts (cdr arts)))
5656                         result)
5657                     (car arts)))
5658         (goto-char (gnus-data-pos result))
5659         (gnus-data-number result)))))
5660
5661 (defun gnus-summary-find-prev (&optional unread article)
5662   (let* ((eobp (eobp))
5663          (article (or article (gnus-summary-article-number)))
5664          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5665          result)
5666     (when (and (not eobp)
5667                (or (not gnus-summary-check-current)
5668                    (not unread)
5669                    (not (gnus-data-unread-p (car arts)))))
5670       (setq arts (cdr arts)))
5671     (when (setq result
5672                 (if unread
5673                     (progn
5674                       (while arts
5675                         (when (gnus-data-unread-p (car arts))
5676                           (setq result (car arts)
5677                                 arts nil))
5678                         (setq arts (cdr arts)))
5679                       result)
5680                   (car arts)))
5681       (goto-char (gnus-data-pos result))
5682       (gnus-data-number result))))
5683
5684 (defun gnus-summary-find-subject (subject &optional unread backward article)
5685   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5686          (article (or article (gnus-summary-article-number)))
5687          (articles (gnus-data-list backward))
5688          (arts (gnus-data-find-list article articles))
5689          result)
5690     (when (or (not gnus-summary-check-current)
5691               (not unread)
5692               (not (gnus-data-unread-p (car arts))))
5693       (setq arts (cdr arts)))
5694     (while arts
5695       (and (or (not unread)
5696                (gnus-data-unread-p (car arts)))
5697            (vectorp (gnus-data-header (car arts)))
5698            (gnus-subject-equal
5699             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5700            (setq result (car arts)
5701                  arts nil))
5702       (setq arts (cdr arts)))
5703     (and result
5704          (goto-char (gnus-data-pos result))
5705          (gnus-data-number result))))
5706
5707 (defun gnus-summary-search-forward (&optional unread subject backward)
5708   "Search forward for an article.
5709 If UNREAD, look for unread articles.  If SUBJECT, look for
5710 articles with that subject.  If BACKWARD, search backward instead."
5711   (cond (subject (gnus-summary-find-subject subject unread backward))
5712         (backward (gnus-summary-find-prev unread))
5713         (t (gnus-summary-find-next unread))))
5714
5715 (defun gnus-recenter (&optional n)
5716   "Center point in window and redisplay frame.
5717 Also do horizontal recentering."
5718   (interactive "P")
5719   (when (and gnus-auto-center-summary
5720              (not (eq gnus-auto-center-summary 'vertical)))
5721     (gnus-horizontal-recenter))
5722   (recenter n))
5723
5724 (defun gnus-summary-recenter ()
5725   "Center point in the summary window.
5726 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5727 displayed, no centering will be performed."
5728   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5729   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5730   (interactive)
5731   (let* ((top (cond ((< (window-height) 4) 0)
5732                     ((< (window-height) 7) 1)
5733                     (t (if (numberp gnus-auto-center-summary)
5734                            gnus-auto-center-summary
5735                          2))))
5736          (height (1- (window-height)))
5737          (bottom (save-excursion (goto-char (point-max))
5738                                  (forward-line (- height))
5739                                  (point)))
5740          (window (get-buffer-window (current-buffer))))
5741     ;; The user has to want it.
5742     (when gnus-auto-center-summary
5743       (when (get-buffer-window gnus-article-buffer)
5744         ;; Only do recentering when the article buffer is displayed,
5745         ;; Set the window start to either `bottom', which is the biggest
5746         ;; possible valid number, or the second line from the top,
5747         ;; whichever is the least.
5748         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5749           (if (> bottom top-pos)
5750               ;; Keep the second line from the top visible
5751               (set-window-start window top-pos t)
5752             ;; Try to keep the bottom line visible; if it's partially
5753             ;; obscured, either scroll one more line to make it fully
5754             ;; visible, or revert to using TOP-POS.
5755             (save-excursion
5756               (goto-char (point-max))
5757               (forward-line -1)
5758               (let ((last-line-start (point)))
5759                 (goto-char bottom)
5760                 (set-window-start window (point) t)
5761                 (when (not (pos-visible-in-window-p last-line-start window))
5762                   (forward-line 1)
5763                   (set-window-start window (min (point) top-pos) t)))))))
5764       ;; Do horizontal recentering while we're at it.
5765       (when (and (get-buffer-window (current-buffer) t)
5766                  (not (eq gnus-auto-center-summary 'vertical)))
5767         (let ((selected (selected-window)))
5768           (select-window (get-buffer-window (current-buffer) t))
5769           (gnus-summary-position-point)
5770           (gnus-horizontal-recenter)
5771           (select-window selected))))))
5772
5773 (defun gnus-summary-jump-to-group (newsgroup)
5774   "Move point to NEWSGROUP in group mode buffer."
5775   ;; Keep update point of group mode buffer if visible.
5776   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5777       (save-window-excursion
5778         ;; Take care of tree window mode.
5779         (when (get-buffer-window gnus-group-buffer)
5780           (pop-to-buffer gnus-group-buffer))
5781         (gnus-group-jump-to-group newsgroup))
5782     (save-excursion
5783       ;; Take care of tree window mode.
5784       (if (get-buffer-window gnus-group-buffer)
5785           (pop-to-buffer gnus-group-buffer)
5786         (set-buffer gnus-group-buffer))
5787       (gnus-group-jump-to-group newsgroup))))
5788
5789 ;; This function returns a list of article numbers based on the
5790 ;; difference between the ranges of read articles in this group and
5791 ;; the range of active articles.
5792 (defun gnus-list-of-unread-articles (group)
5793   (let* ((read (gnus-info-read (gnus-get-info group)))
5794          (active (or (gnus-active group) (gnus-activate-group group)))
5795          (last (cdr active))
5796          first nlast unread)
5797     ;; If none are read, then all are unread.
5798     (if (not read)
5799         (setq first (car active))
5800       ;; If the range of read articles is a single range, then the
5801       ;; first unread article is the article after the last read
5802       ;; article.  Sounds logical, doesn't it?
5803       (if (and (not (listp (cdr read)))
5804                (or (< (car read) (car active))
5805                    (progn (setq read (list read))
5806                           nil)))
5807           (setq first (max (car active) (1+ (cdr read))))
5808         ;; `read' is a list of ranges.
5809         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5810                                   (caar read)))
5811                   1)
5812           (setq first (car active)))
5813         (while read
5814           (when first
5815             (while (< first nlast)
5816               (push first unread)
5817               (setq first (1+ first))))
5818           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5819           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5820           (setq read (cdr read)))))
5821     ;; And add the last unread articles.
5822     (while (<= first last)
5823       (push first unread)
5824       (setq first (1+ first)))
5825     ;; Return the list of unread articles.
5826     (delq 0 (nreverse unread))))
5827
5828 (defun gnus-list-of-read-articles (group)
5829   "Return a list of unread, unticked and non-dormant articles."
5830   (let* ((info (gnus-get-info group))
5831          (marked (gnus-info-marks info))
5832          (active (gnus-active group)))
5833     (and info active
5834          (gnus-set-difference
5835           (gnus-sorted-complement
5836            (gnus-uncompress-range active)
5837            (gnus-list-of-unread-articles group))
5838           (append
5839            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5840            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5841
5842 ;; Various summary commands
5843
5844 (defun gnus-summary-select-article-buffer ()
5845   "Reconfigure windows to show article buffer."
5846   (interactive)
5847   (if (not (gnus-buffer-live-p gnus-article-buffer))
5848       (error "There is no article buffer for this summary buffer")
5849     (gnus-configure-windows 'article)
5850     (select-window (get-buffer-window gnus-article-buffer))))
5851
5852 (defun gnus-summary-universal-argument (arg)
5853   "Perform any operation on all articles that are process/prefixed."
5854   (interactive "P")
5855   (let ((articles (gnus-summary-work-articles arg))
5856         func article)
5857     (if (eq
5858          (setq
5859           func
5860           (key-binding
5861            (read-key-sequence
5862             (substitute-command-keys
5863              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5864          'undefined)
5865         (gnus-error 1 "Undefined key")
5866       (save-excursion
5867         (while articles
5868           (gnus-summary-goto-subject (setq article (pop articles)))
5869           (let (gnus-newsgroup-processable)
5870             (command-execute func))
5871           (gnus-summary-remove-process-mark article)))))
5872   (gnus-summary-position-point))
5873
5874 (defun gnus-summary-toggle-truncation (&optional arg)
5875   "Toggle truncation of summary lines.
5876 With arg, turn line truncation on iff arg is positive."
5877   (interactive "P")
5878   (setq truncate-lines
5879         (if (null arg) (not truncate-lines)
5880           (> (prefix-numeric-value arg) 0)))
5881   (redraw-display))
5882
5883 (defun gnus-summary-reselect-current-group (&optional all rescan)
5884   "Exit and then reselect the current newsgroup.
5885 The prefix argument ALL means to select all articles."
5886   (interactive "P")
5887   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5888     (error "Ephemeral groups can't be reselected"))
5889   (let ((current-subject (gnus-summary-article-number))
5890         (group gnus-newsgroup-name))
5891     (setq gnus-newsgroup-begin nil)
5892     (gnus-summary-exit)
5893     ;; We have to adjust the point of group mode buffer because
5894     ;; point was moved to the next unread newsgroup by exiting.
5895     (gnus-summary-jump-to-group group)
5896     (when rescan
5897       (save-excursion
5898         (save-window-excursion
5899           ;; Don't show group contents.
5900           (set-window-start (selected-window) (point-max))
5901           (gnus-group-get-new-news-this-group 1))))
5902     (gnus-group-read-group all t)
5903     (gnus-summary-goto-subject current-subject nil t)))
5904
5905 (defun gnus-summary-rescan-group (&optional all)
5906   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5907   (interactive "P")
5908   (gnus-summary-reselect-current-group all t))
5909
5910 (defun gnus-summary-update-info (&optional non-destructive)
5911   (save-excursion
5912     (let ((group gnus-newsgroup-name))
5913       (when group
5914         (when gnus-newsgroup-kill-headers
5915           (setq gnus-newsgroup-killed
5916                 (gnus-compress-sequence
5917                  (nconc
5918                   (gnus-set-sorted-intersection
5919                    (gnus-uncompress-range gnus-newsgroup-killed)
5920                    (setq gnus-newsgroup-unselected
5921                          (sort gnus-newsgroup-unselected '<)))
5922                   (setq gnus-newsgroup-unreads
5923                         (sort gnus-newsgroup-unreads '<)))
5924                  t)))
5925         (unless (listp (cdr gnus-newsgroup-killed))
5926           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5927         (let ((headers gnus-newsgroup-headers))
5928           ;; Set the new ranges of read articles.
5929           (save-excursion
5930             (set-buffer gnus-group-buffer)
5931             (gnus-undo-force-boundary))
5932           (gnus-update-read-articles
5933            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5934           ;; Set the current article marks.
5935           (let ((gnus-newsgroup-scored
5936                  (if (and (not gnus-save-score)
5937                           (not non-destructive))
5938                      nil
5939                    gnus-newsgroup-scored)))
5940             (save-excursion
5941               (gnus-update-marks)))
5942           ;; Do the cross-ref thing.
5943           (when gnus-use-cross-reference
5944             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5945           ;; Do not switch windows but change the buffer to work.
5946           (set-buffer gnus-group-buffer)
5947           (unless (gnus-ephemeral-group-p group)
5948             (gnus-group-update-group group)))))))
5949
5950 (defun gnus-summary-save-newsrc (&optional force)
5951   "Save the current number of read/marked articles in the dribble buffer.
5952 The dribble buffer will then be saved.
5953 If FORCE (the prefix), also save the .newsrc file(s)."
5954   (interactive "P")
5955   (gnus-summary-update-info t)
5956   (if force
5957       (gnus-save-newsrc-file)
5958     (gnus-dribble-save)))
5959
5960 (defun gnus-summary-exit (&optional temporary)
5961   "Exit reading current newsgroup, and then return to group selection mode.
5962 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5963   (interactive)
5964   (gnus-set-global-variables)
5965   (gnus-kill-save-kill-buffer)
5966   (gnus-async-halt-prefetch)
5967   (let* ((group gnus-newsgroup-name)
5968          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5969          (mode major-mode)
5970          (group-point nil)
5971          (buf (current-buffer)))
5972     (unless quit-config
5973       ;; Do adaptive scoring, and possibly save score files.
5974       (when gnus-newsgroup-adaptive
5975         (gnus-score-adaptive))
5976       (when gnus-use-scoring
5977         (gnus-score-save)))
5978     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5979     ;; If we have several article buffers, we kill them at exit.
5980     (unless gnus-single-article-buffer
5981       (gnus-kill-buffer gnus-original-article-buffer)
5982       (setq gnus-article-current nil))
5983     (when gnus-use-cache
5984       (gnus-cache-possibly-remove-articles)
5985       (gnus-cache-save-buffers))
5986     (gnus-async-prefetch-remove-group group)
5987     (when gnus-suppress-duplicates
5988       (gnus-dup-enter-articles))
5989     (when gnus-use-trees
5990       (gnus-tree-close group))
5991     (when gnus-use-cache
5992       (gnus-cache-write-active))
5993     ;; Remove entries for this group.
5994     (nnmail-purge-split-history (gnus-group-real-name group))
5995     ;; Make all changes in this group permanent.
5996     (unless quit-config
5997       (gnus-run-hooks 'gnus-exit-group-hook)
5998       (gnus-summary-update-info))
5999     (gnus-close-group group)
6000     ;; Make sure where we were, and go to next newsgroup.
6001     (set-buffer gnus-group-buffer)
6002     (unless quit-config
6003       (gnus-group-jump-to-group group))
6004     (gnus-run-hooks 'gnus-summary-exit-hook)
6005     (unless (or quit-config
6006                 ;; If this group has disappeared from the summary
6007                 ;; buffer, don't skip forwards.
6008                 (not (string= group (gnus-group-group-name))))
6009       (gnus-group-next-unread-group 1))
6010     (setq group-point (point))
6011     (if temporary
6012         nil                             ;Nothing to do.
6013       ;; If we have several article buffers, we kill them at exit.
6014       (unless gnus-single-article-buffer
6015         (gnus-kill-buffer gnus-article-buffer)
6016         (gnus-kill-buffer gnus-original-article-buffer)
6017         (setq gnus-article-current nil))
6018       (set-buffer buf)
6019       (if (not gnus-kill-summary-on-exit)
6020           (progn
6021             (gnus-deaden-summary)
6022             (setq mode nil))
6023         ;; We set all buffer-local variables to nil.  It is unclear why
6024         ;; this is needed, but if we don't, buffer-local variables are
6025         ;; not garbage-collected, it seems.  This would the lead to en
6026         ;; ever-growing Emacs.
6027         (gnus-summary-clear-local-variables)
6028         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6029           (gnus-summary-clear-local-variables))
6030         (when (get-buffer gnus-article-buffer)
6031           (bury-buffer gnus-article-buffer))
6032         ;; We clear the global counterparts of the buffer-local
6033         ;; variables as well, just to be on the safe side.
6034         (set-buffer gnus-group-buffer)
6035         (gnus-summary-clear-local-variables)
6036         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6037           (gnus-summary-clear-local-variables)))
6038       (setq gnus-current-select-method gnus-select-method)
6039       (pop-to-buffer gnus-group-buffer)
6040       (if (not quit-config)
6041           (progn
6042             (goto-char group-point)
6043             (gnus-configure-windows 'group 'force)
6044             (unless (pos-visible-in-window-p)
6045               (forward-line (/ (static-if (featurep 'xemacs)
6046                                    (window-displayed-height)
6047                                  (1- (window-height)))
6048                                -2))
6049               (set-window-start (selected-window) (point))
6050               (goto-char group-point)))
6051         (gnus-handle-ephemeral-exit quit-config))
6052       ;; Return to group mode buffer.
6053       (when (eq mode 'gnus-summary-mode)
6054         (gnus-kill-buffer buf))
6055       ;; Clear the current group name.
6056       (unless quit-config
6057         (setq gnus-newsgroup-name nil)))))
6058
6059 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6060 (defun gnus-summary-exit-no-update (&optional no-questions)
6061   "Quit reading current newsgroup without updating read article info."
6062   (interactive)
6063   (let* ((group gnus-newsgroup-name)
6064          (quit-config (gnus-group-quit-config group)))
6065     (when (or no-questions
6066               gnus-expert-user
6067               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6068       (gnus-async-halt-prefetch)
6069       (mapcar 'funcall
6070               (delq 'gnus-summary-expire-articles
6071                     (copy-sequence gnus-summary-prepare-exit-hook)))
6072       ;; If we have several article buffers, we kill them at exit.
6073       (unless gnus-single-article-buffer
6074         (gnus-kill-buffer gnus-article-buffer)
6075         (gnus-kill-buffer gnus-original-article-buffer)
6076         (setq gnus-article-current nil))
6077       (if (not gnus-kill-summary-on-exit)
6078           (gnus-deaden-summary)
6079         (gnus-close-group group)
6080         (gnus-summary-clear-local-variables)
6081         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6082           (gnus-summary-clear-local-variables))
6083         (set-buffer gnus-group-buffer)
6084         (gnus-summary-clear-local-variables)
6085         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6086           (gnus-summary-clear-local-variables))
6087         (when (get-buffer gnus-summary-buffer)
6088           (kill-buffer gnus-summary-buffer)))
6089       (unless gnus-single-article-buffer
6090         (setq gnus-article-current nil))
6091       (when gnus-use-trees
6092         (gnus-tree-close group))
6093       (gnus-async-prefetch-remove-group group)
6094       (when (get-buffer gnus-article-buffer)
6095         (bury-buffer gnus-article-buffer))
6096       ;; Return to the group buffer.
6097       (gnus-configure-windows 'group 'force)
6098       ;; Clear the current group name.
6099       (setq gnus-newsgroup-name nil)
6100       (when (equal (gnus-group-group-name) group)
6101         (gnus-group-next-unread-group 1))
6102       (when quit-config
6103         (gnus-handle-ephemeral-exit quit-config)))))
6104
6105 (defun gnus-handle-ephemeral-exit (quit-config)
6106   "Handle movement when leaving an ephemeral group.
6107 The state which existed when entering the ephemeral is reset."
6108   (if (not (buffer-name (car quit-config)))
6109       (gnus-configure-windows 'group 'force)
6110     (set-buffer (car quit-config))
6111     (cond ((eq major-mode 'gnus-summary-mode)
6112            (gnus-set-global-variables))
6113           ((eq major-mode 'gnus-article-mode)
6114            (save-excursion
6115              ;; The `gnus-summary-buffer' variable may point
6116              ;; to the old summary buffer when using a single
6117              ;; article buffer.
6118              (unless (gnus-buffer-live-p gnus-summary-buffer)
6119                (set-buffer gnus-group-buffer))
6120              (set-buffer gnus-summary-buffer)
6121              (gnus-set-global-variables))))
6122     (if (or (eq (cdr quit-config) 'article)
6123             (eq (cdr quit-config) 'pick))
6124         (progn
6125           ;; The current article may be from the ephemeral group
6126           ;; thus it is best that we reload this article
6127           (gnus-summary-show-article)
6128           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6129               (gnus-configure-windows 'pick 'force)
6130             (gnus-configure-windows (cdr quit-config) 'force)))
6131       (gnus-configure-windows (cdr quit-config) 'force))
6132     (when (eq major-mode 'gnus-summary-mode)
6133       (gnus-summary-next-subject 1 nil t)
6134       (gnus-summary-recenter)
6135       (gnus-summary-position-point))))
6136
6137 (defun gnus-summary-preview-mime-message ()
6138   "MIME decode and play this message."
6139   (interactive)
6140   (let ((gnus-break-pages nil)
6141         (gnus-show-mime t))
6142     (gnus-summary-select-article gnus-show-all-headers t))
6143   (select-window (get-buffer-window gnus-article-buffer)))
6144
6145 ;;; Dead summaries.
6146
6147 (defvar gnus-dead-summary-mode-map nil)
6148
6149 (unless gnus-dead-summary-mode-map
6150   (setq gnus-dead-summary-mode-map (make-keymap))
6151   (suppress-keymap gnus-dead-summary-mode-map)
6152   (substitute-key-definition
6153    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6154   (let ((keys '("\C-d" "\r" "\177" [delete])))
6155     (while keys
6156       (define-key gnus-dead-summary-mode-map
6157         (pop keys) 'gnus-summary-wake-up-the-dead))))
6158
6159 (defvar gnus-dead-summary-mode nil
6160   "Minor mode for Gnus summary buffers.")
6161
6162 (defun gnus-dead-summary-mode (&optional arg)
6163   "Minor mode for Gnus summary buffers."
6164   (interactive "P")
6165   (when (eq major-mode 'gnus-summary-mode)
6166     (make-local-variable 'gnus-dead-summary-mode)
6167     (setq gnus-dead-summary-mode
6168           (if (null arg) (not gnus-dead-summary-mode)
6169             (> (prefix-numeric-value arg) 0)))
6170     (when gnus-dead-summary-mode
6171       (gnus-add-minor-mode
6172        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6173
6174 (defun gnus-deaden-summary ()
6175   "Make the current summary buffer into a dead summary buffer."
6176   ;; Kill any previous dead summary buffer.
6177   (when (and gnus-dead-summary
6178              (buffer-name gnus-dead-summary))
6179     (save-excursion
6180       (set-buffer gnus-dead-summary)
6181       (when gnus-dead-summary-mode
6182         (kill-buffer (current-buffer)))))
6183   ;; Make this the current dead summary.
6184   (setq gnus-dead-summary (current-buffer))
6185   (gnus-dead-summary-mode 1)
6186   (let ((name (buffer-name)))
6187     (when (string-match "Summary" name)
6188       (rename-buffer
6189        (concat (substring name 0 (match-beginning 0)) "Dead "
6190                (substring name (match-beginning 0)))
6191        t)
6192       (bury-buffer))))
6193
6194 (defun gnus-kill-or-deaden-summary (buffer)
6195   "Kill or deaden the summary BUFFER."
6196   (save-excursion
6197     (when (and (buffer-name buffer)
6198                (not gnus-single-article-buffer))
6199       (save-excursion
6200         (set-buffer buffer)
6201         (gnus-kill-buffer gnus-article-buffer)
6202         (gnus-kill-buffer gnus-original-article-buffer)))
6203     (cond (gnus-kill-summary-on-exit
6204            (when (and gnus-use-trees
6205                       (gnus-buffer-exists-p buffer))
6206              (save-excursion
6207                (set-buffer buffer)
6208                (gnus-tree-close gnus-newsgroup-name)))
6209            (gnus-kill-buffer buffer))
6210           ((gnus-buffer-exists-p buffer)
6211            (save-excursion
6212              (set-buffer buffer)
6213              (gnus-deaden-summary))))))
6214
6215 (defun gnus-summary-wake-up-the-dead (&rest args)
6216   "Wake up the dead summary buffer."
6217   (interactive)
6218   (gnus-dead-summary-mode -1)
6219   (let ((name (buffer-name)))
6220     (when (string-match "Dead " name)
6221       (rename-buffer
6222        (concat (substring name 0 (match-beginning 0))
6223                (substring name (match-end 0)))
6224        t)))
6225   (gnus-message 3 "This dead summary is now alive again"))
6226
6227 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6228 (defun gnus-summary-fetch-faq (&optional faq-dir)
6229   "Fetch the FAQ for the current group.
6230 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6231 in."
6232   (interactive
6233    (list
6234     (when current-prefix-arg
6235       (completing-read
6236        "Faq dir: " (and (listp gnus-group-faq-directory)
6237                         (mapcar (lambda (file) (list file))
6238                                 gnus-group-faq-directory))))))
6239   (let (gnus-faq-buffer)
6240     (when (setq gnus-faq-buffer
6241                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6242       (gnus-configure-windows 'summary-faq))))
6243
6244 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6245 (defun gnus-summary-describe-group (&optional force)
6246   "Describe the current newsgroup."
6247   (interactive "P")
6248   (gnus-group-describe-group force gnus-newsgroup-name))
6249
6250 (defun gnus-summary-describe-briefly ()
6251   "Describe summary mode commands briefly."
6252   (interactive)
6253   (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")))
6254
6255 ;; Walking around group mode buffer from summary mode.
6256
6257 (defun gnus-summary-next-group (&optional no-article target-group backward)
6258   "Exit current newsgroup and then select next unread newsgroup.
6259 If prefix argument NO-ARTICLE is non-nil, no article is selected
6260 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6261 previous group instead."
6262   (interactive "P")
6263   ;; Stop pre-fetching.
6264   (gnus-async-halt-prefetch)
6265   (let ((current-group gnus-newsgroup-name)
6266         (current-buffer (current-buffer))
6267         entered)
6268     ;; First we semi-exit this group to update Xrefs and all variables.
6269     ;; We can't do a real exit, because the window conf must remain
6270     ;; the same in case the user is prompted for info, and we don't
6271     ;; want the window conf to change before that...
6272     (gnus-summary-exit t)
6273     (while (not entered)
6274       ;; Then we find what group we are supposed to enter.
6275       (set-buffer gnus-group-buffer)
6276       (gnus-group-jump-to-group current-group)
6277       (setq target-group
6278             (or target-group
6279                 (if (eq gnus-keep-same-level 'best)
6280                     (gnus-summary-best-group gnus-newsgroup-name)
6281                   (gnus-summary-search-group backward gnus-keep-same-level))))
6282       (if (not target-group)
6283           ;; There are no further groups, so we return to the group
6284           ;; buffer.
6285           (progn
6286             (gnus-message 5 "Returning to the group buffer")
6287             (setq entered t)
6288             (when (gnus-buffer-live-p current-buffer)
6289               (set-buffer current-buffer)
6290               (gnus-summary-exit))
6291             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6292         ;; We try to enter the target group.
6293         (gnus-group-jump-to-group target-group)
6294         (let ((unreads (gnus-group-group-unread)))
6295           (if (and (or (eq t unreads)
6296                        (and unreads (not (zerop unreads))))
6297                    (gnus-summary-read-group
6298                     target-group nil no-article
6299                     (and (buffer-name current-buffer) current-buffer)
6300                     nil backward))
6301               (setq entered t)
6302             (setq current-group target-group
6303                   target-group nil)))))))
6304
6305 (defun gnus-summary-prev-group (&optional no-article)
6306   "Exit current newsgroup and then select previous unread newsgroup.
6307 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6308   (interactive "P")
6309   (gnus-summary-next-group no-article nil t))
6310
6311 ;; Walking around summary lines.
6312
6313 (defun gnus-summary-first-subject (&optional unread undownloaded)
6314   "Go to the first unread subject.
6315 If UNREAD is non-nil, go to the first unread article.
6316 Returns the article selected or nil if there are no unread articles."
6317   (interactive "P")
6318   (prog1
6319       (cond
6320        ;; Empty summary.
6321        ((null gnus-newsgroup-data)
6322         (gnus-message 3 "No articles in the group")
6323         nil)
6324        ;; Pick the first article.
6325        ((not unread)
6326         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6327         (gnus-data-number (car gnus-newsgroup-data)))
6328        ;; No unread articles.
6329        ((null gnus-newsgroup-unreads)
6330         (gnus-message 3 "No more unread articles")
6331         nil)
6332        ;; Find the first unread article.
6333        (t
6334         (let ((data gnus-newsgroup-data))
6335           (while (and data
6336                       (and (not (and undownloaded
6337                                      (eq gnus-undownloaded-mark
6338                                          (gnus-data-mark (car data)))))
6339                            (not (gnus-data-unread-p (car data)))))
6340             (setq data (cdr data)))
6341           (when data
6342             (goto-char (gnus-data-pos (car data)))
6343             (gnus-data-number (car data))))))
6344     (gnus-summary-position-point)))
6345
6346 (defun gnus-summary-next-subject (n &optional unread dont-display)
6347   "Go to next N'th summary line.
6348 If N is negative, go to the previous N'th subject line.
6349 If UNREAD is non-nil, only unread articles are selected.
6350 The difference between N and the actual number of steps taken is
6351 returned."
6352   (interactive "p")
6353   (let ((backward (< n 0))
6354         (n (abs n)))
6355     (while (and (> n 0)
6356                 (if backward
6357                     (gnus-summary-find-prev unread)
6358                   (gnus-summary-find-next unread)))
6359       (unless (zerop (setq n (1- n)))
6360         (gnus-summary-show-thread)))
6361     (when (/= 0 n)
6362       (gnus-message 7 "No more%s articles"
6363                     (if unread " unread" "")))
6364     (unless dont-display
6365       (gnus-summary-recenter)
6366       (gnus-summary-position-point))
6367     n))
6368
6369 (defun gnus-summary-next-unread-subject (n)
6370   "Go to next N'th unread summary line."
6371   (interactive "p")
6372   (gnus-summary-next-subject n t))
6373
6374 (defun gnus-summary-prev-subject (n &optional unread)
6375   "Go to previous N'th summary line.
6376 If optional argument UNREAD is non-nil, only unread article is selected."
6377   (interactive "p")
6378   (gnus-summary-next-subject (- n) unread))
6379
6380 (defun gnus-summary-prev-unread-subject (n)
6381   "Go to previous N'th unread summary line."
6382   (interactive "p")
6383   (gnus-summary-next-subject (- n) t))
6384
6385 (defun gnus-summary-goto-subject (article &optional force silent)
6386   "Go the subject line of ARTICLE.
6387 If FORCE, also allow jumping to articles not currently shown."
6388   (interactive "nArticle number: ")
6389   (let ((b (point))
6390         (data (gnus-data-find article)))
6391     ;; We read in the article if we have to.
6392     (and (not data)
6393          force
6394          (gnus-summary-insert-subject
6395           article
6396           (if (or (numberp force) (vectorp force)) force)
6397           t)
6398          (setq data (gnus-data-find article)))
6399     (goto-char b)
6400     (if (not data)
6401         (progn
6402           (unless silent
6403             (gnus-message 3 "Can't find article %d" article))
6404           nil)
6405       (let ((pt (gnus-data-pos data)))
6406         (goto-char pt)
6407         (gnus-summary-set-article-display-arrow pt))
6408       (gnus-summary-position-point)
6409       article)))
6410
6411 ;; Walking around summary lines with displaying articles.
6412
6413 (defun gnus-summary-expand-window (&optional arg)
6414   "Make the summary buffer take up the entire Emacs frame.
6415 Given a prefix, will force an `article' buffer configuration."
6416   (interactive "P")
6417   (if arg
6418       (gnus-configure-windows 'article 'force)
6419     (gnus-configure-windows 'summary 'force)))
6420
6421 (defun gnus-summary-display-article (article &optional all-header)
6422   "Display ARTICLE in article buffer."
6423   (when (gnus-buffer-live-p gnus-article-buffer)
6424     (with-current-buffer gnus-article-buffer
6425       (set-buffer-multibyte t)))
6426   (gnus-set-global-variables)
6427   (when (gnus-buffer-live-p gnus-article-buffer)
6428     (with-current-buffer gnus-article-buffer
6429       (setq gnus-article-charset gnus-newsgroup-charset)
6430       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6431   (if (null article)
6432       nil
6433     (prog1
6434         (if gnus-summary-display-article-function
6435             (funcall gnus-summary-display-article-function article all-header)
6436           (gnus-article-prepare article all-header))
6437       (with-current-buffer gnus-article-buffer
6438         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6439              nil))
6440       (gnus-run-hooks 'gnus-select-article-hook)
6441       (when (and gnus-current-article
6442                  (not (zerop gnus-current-article)))
6443         (gnus-summary-goto-subject gnus-current-article))
6444       (gnus-summary-recenter)
6445       (when (and gnus-use-trees gnus-show-threads)
6446         (gnus-possibly-generate-tree article)
6447         (gnus-highlight-selected-tree article))
6448       ;; Successfully display article.
6449       (gnus-article-set-window-start
6450        (cdr (assq article gnus-newsgroup-bookmarks))))))
6451
6452 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6453   "Select the current article.
6454 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6455 non-nil, the article will be re-fetched even if it already present in
6456 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6457 be displayed."
6458   ;; Make sure we are in the summary buffer to work around bbdb bug.
6459   (unless (eq major-mode 'gnus-summary-mode)
6460     (set-buffer gnus-summary-buffer))
6461   (let ((article (or article (gnus-summary-article-number)))
6462         (all-headers (not (not all-headers))) ;Must be T or NIL.
6463         gnus-summary-display-article-function)
6464     (and (not pseudo)
6465          (gnus-summary-article-pseudo-p article)
6466          (error "This is a pseudo-article"))
6467     (save-excursion
6468       (set-buffer gnus-summary-buffer)
6469       (if (or (and gnus-single-article-buffer
6470                    (or (null gnus-current-article)
6471                        (null gnus-article-current)
6472                        (null (get-buffer gnus-article-buffer))
6473                        (not (eq article (cdr gnus-article-current)))
6474                        (not (equal (car gnus-article-current)
6475                                    gnus-newsgroup-name))))
6476               (and (not gnus-single-article-buffer)
6477                    (or (null gnus-current-article)
6478                        (not (eq gnus-current-article article))))
6479               force)
6480           ;; The requested article is different from the current article.
6481           (progn
6482             (gnus-summary-display-article article all-headers)
6483             (when (or all-headers gnus-show-all-headers)
6484               (gnus-article-show-all-headers))
6485             (gnus-article-set-window-start
6486              (cdr (assq article gnus-newsgroup-bookmarks)))
6487             article)
6488         (when (or all-headers gnus-show-all-headers)
6489           (gnus-article-show-all-headers))
6490         'old))))
6491
6492 (defun gnus-summary-force-verify-and-decrypt ()
6493   (interactive)
6494   (let ((mm-verify-option 'known)
6495         (mm-decrypt-option 'known))
6496     (gnus-summary-select-article nil 'force)))
6497
6498 (defun gnus-summary-set-current-mark (&optional current-mark)
6499   "Obsolete function."
6500   nil)
6501
6502 (defun gnus-summary-next-article (&optional unread subject backward push)
6503   "Select the next article.
6504 If UNREAD, only unread articles are selected.
6505 If SUBJECT, only articles with SUBJECT are selected.
6506 If BACKWARD, the previous article is selected instead of the next."
6507   (interactive "P")
6508   (cond
6509    ;; Is there such an article?
6510    ((and (gnus-summary-search-forward unread subject backward)
6511          (or (gnus-summary-display-article (gnus-summary-article-number))
6512              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6513     (gnus-summary-position-point))
6514    ;; If not, we try the first unread, if that is wanted.
6515    ((and subject
6516          gnus-auto-select-same
6517          (gnus-summary-first-unread-article))
6518     (gnus-summary-position-point)
6519     (gnus-message 6 "Wrapped"))
6520    ;; Try to get next/previous article not displayed in this group.
6521    ((and gnus-auto-extend-newsgroup
6522          (not unread) (not subject))
6523     (gnus-summary-goto-article
6524      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6525      nil (count-lines (point-min) (point))))
6526    ;; Go to next/previous group.
6527    (t
6528     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6529       (gnus-summary-jump-to-group gnus-newsgroup-name))
6530     (let ((cmd last-command-char)
6531           (point
6532            (save-excursion
6533              (set-buffer gnus-group-buffer)
6534              (point)))
6535           (group
6536            (if (eq gnus-keep-same-level 'best)
6537                (gnus-summary-best-group gnus-newsgroup-name)
6538              (gnus-summary-search-group backward gnus-keep-same-level))))
6539       ;; For some reason, the group window gets selected.  We change
6540       ;; it back.
6541       (select-window (get-buffer-window (current-buffer)))
6542       ;; Select next unread newsgroup automagically.
6543       (cond
6544        ((or (not gnus-auto-select-next)
6545             (not cmd))
6546         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6547        ((or (eq gnus-auto-select-next 'quietly)
6548             (and (eq gnus-auto-select-next 'slightly-quietly)
6549                  push)
6550             (and (eq gnus-auto-select-next 'almost-quietly)
6551                  (gnus-summary-last-article-p)))
6552         ;; Select quietly.
6553         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6554             (gnus-summary-exit)
6555           (gnus-message 7 "No more%s articles (%s)..."
6556                         (if unread " unread" "")
6557                         (if group (concat "selecting " group)
6558                           "exiting"))
6559           (gnus-summary-next-group nil group backward)))
6560        (t
6561         (when (gnus-key-press-event-p last-input-event)
6562           (gnus-summary-walk-group-buffer
6563            gnus-newsgroup-name cmd unread backward point))))))))
6564
6565 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6566   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6567                       (?\C-p (gnus-group-prev-unread-group 1))))
6568         (cursor-in-echo-area t)
6569         keve key group ended)
6570     (save-excursion
6571       (set-buffer gnus-group-buffer)
6572       (goto-char start)
6573       (setq group
6574             (if (eq gnus-keep-same-level 'best)
6575                 (gnus-summary-best-group gnus-newsgroup-name)
6576               (gnus-summary-search-group backward gnus-keep-same-level))))
6577     (while (not ended)
6578       (gnus-message
6579        5 "No more%s articles%s" (if unread " unread" "")
6580        (if (and group
6581                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6582            (format " (Type %s for %s [%s])"
6583                    (single-key-description cmd) group
6584                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6585          (format " (Type %s to exit %s)"
6586                  (single-key-description cmd)
6587                  gnus-newsgroup-name)))
6588       ;; Confirm auto selection.
6589       (setq key (car (setq keve (gnus-read-event-char))))
6590       (setq ended t)
6591       (cond
6592        ((assq key keystrokes)
6593         (let ((obuf (current-buffer)))
6594           (switch-to-buffer gnus-group-buffer)
6595           (when group
6596             (gnus-group-jump-to-group group))
6597           (eval (cadr (assq key keystrokes)))
6598           (setq group (gnus-group-group-name))
6599           (switch-to-buffer obuf))
6600         (setq ended nil))
6601        ((equal key cmd)
6602         (if (or (not group)
6603                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6604             (gnus-summary-exit)
6605           (gnus-summary-next-group nil group backward)))
6606        (t
6607         (push (cdr keve) unread-command-events))))))
6608
6609 (defun gnus-summary-next-unread-article ()
6610   "Select unread article after current one."
6611   (interactive)
6612   (gnus-summary-next-article
6613    (or (not (eq gnus-summary-goto-unread 'never))
6614        (gnus-summary-last-article-p (gnus-summary-article-number)))
6615    (and gnus-auto-select-same
6616         (gnus-summary-article-subject))))
6617
6618 (defun gnus-summary-prev-article (&optional unread subject)
6619   "Select the article after the current one.
6620 If UNREAD is non-nil, only unread articles are selected."
6621   (interactive "P")
6622   (gnus-summary-next-article unread subject t))
6623
6624 (defun gnus-summary-prev-unread-article ()
6625   "Select unread article before current one."
6626   (interactive)
6627   (gnus-summary-prev-article
6628    (or (not (eq gnus-summary-goto-unread 'never))
6629        (gnus-summary-first-article-p (gnus-summary-article-number)))
6630    (and gnus-auto-select-same
6631         (gnus-summary-article-subject))))
6632
6633 (defun gnus-summary-next-page (&optional lines circular)
6634   "Show next page of the selected article.
6635 If at the end of the current article, select the next article.
6636 LINES says how many lines should be scrolled up.
6637
6638 If CIRCULAR is non-nil, go to the start of the article instead of
6639 selecting the next article when reaching the end of the current
6640 article."
6641   (interactive "P")
6642   (setq gnus-summary-buffer (current-buffer))
6643   (gnus-set-global-variables)
6644   (let ((article (gnus-summary-article-number))
6645         (article-window (get-buffer-window gnus-article-buffer t))
6646         endp)
6647     ;; If the buffer is empty, we have no article.
6648     (unless article
6649       (error "No article to select"))
6650     (gnus-configure-windows 'article)
6651     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6652         (if (and (eq gnus-summary-goto-unread 'never)
6653                  (not (gnus-summary-last-article-p article)))
6654             (gnus-summary-next-article)
6655           (gnus-summary-next-unread-article))
6656       (if (or (null gnus-current-article)
6657               (null gnus-article-current)
6658               (/= article (cdr gnus-article-current))
6659               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6660           ;; Selected subject is different from current article's.
6661           (gnus-summary-display-article article)
6662         (when article-window
6663           (gnus-eval-in-buffer-window gnus-article-buffer
6664             (setq endp (gnus-article-next-page lines)))
6665           (when endp
6666             (cond (circular
6667                    (gnus-summary-beginning-of-article))
6668                   (lines
6669                    (gnus-message 3 "End of message"))
6670                   ((null lines)
6671                    (if (and (eq gnus-summary-goto-unread 'never)
6672                             (not (gnus-summary-last-article-p article)))
6673                        (gnus-summary-next-article)
6674                      (gnus-summary-next-unread-article))))))))
6675     (gnus-summary-recenter)
6676     (gnus-summary-position-point)))
6677
6678 (defun gnus-summary-prev-page (&optional lines move)
6679   "Show previous page of selected article.
6680 Argument LINES specifies lines to be scrolled down.
6681 If MOVE, move to the previous unread article if point is at
6682 the beginning of the buffer."
6683   (interactive "P")
6684   (let ((article (gnus-summary-article-number))
6685         (article-window (get-buffer-window gnus-article-buffer t))
6686         endp)
6687     (gnus-configure-windows 'article)
6688     (if (or (null gnus-current-article)
6689             (null gnus-article-current)
6690             (/= article (cdr gnus-article-current))
6691             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6692         ;; Selected subject is different from current article's.
6693         (gnus-summary-display-article article)
6694       (gnus-summary-recenter)
6695       (when article-window
6696         (gnus-eval-in-buffer-window gnus-article-buffer
6697           (setq endp (gnus-article-prev-page lines)))
6698         (when (and move endp)
6699           (cond (lines
6700                  (gnus-message 3 "Beginning of message"))
6701                 ((null lines)
6702                  (if (and (eq gnus-summary-goto-unread 'never)
6703                           (not (gnus-summary-first-article-p article)))
6704                      (gnus-summary-prev-article)
6705                    (gnus-summary-prev-unread-article))))))))
6706   (gnus-summary-position-point))
6707
6708 (defun gnus-summary-prev-page-or-article (&optional lines)
6709   "Show previous page of selected article.
6710 Argument LINES specifies lines to be scrolled down.
6711 If at the beginning of the article, go to the next article."
6712   (interactive "P")
6713   (gnus-summary-prev-page lines t))
6714
6715 (defun gnus-summary-scroll-up (lines)
6716   "Scroll up (or down) one line current article.
6717 Argument LINES specifies lines to be scrolled up (or down if negative)."
6718   (interactive "p")
6719   (gnus-configure-windows 'article)
6720   (gnus-summary-show-thread)
6721   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6722     (gnus-eval-in-buffer-window gnus-article-buffer
6723       (cond ((> lines 0)
6724              (when (gnus-article-next-page lines)
6725                (gnus-message 3 "End of message")))
6726             ((< lines 0)
6727              (gnus-article-prev-page (- lines))))))
6728   (gnus-summary-recenter)
6729   (gnus-summary-position-point))
6730
6731 (defun gnus-summary-scroll-down (lines)
6732   "Scroll down (or up) one line current article.
6733 Argument LINES specifies lines to be scrolled down (or up if negative)."
6734   (interactive "p")
6735   (gnus-summary-scroll-up (- lines)))
6736
6737 (defun gnus-summary-next-same-subject ()
6738   "Select next article which has the same subject as current one."
6739   (interactive)
6740   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6741
6742 (defun gnus-summary-prev-same-subject ()
6743   "Select previous article which has the same subject as current one."
6744   (interactive)
6745   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6746
6747 (defun gnus-summary-next-unread-same-subject ()
6748   "Select next unread article which has the same subject as current one."
6749   (interactive)
6750   (gnus-summary-next-article t (gnus-summary-article-subject)))
6751
6752 (defun gnus-summary-prev-unread-same-subject ()
6753   "Select previous unread article which has the same subject as current one."
6754   (interactive)
6755   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6756
6757 (defun gnus-summary-first-unread-article ()
6758   "Select the first unread article.
6759 Return nil if there are no unread articles."
6760   (interactive)
6761   (prog1
6762       (when (gnus-summary-first-subject t)
6763         (gnus-summary-show-thread)
6764         (gnus-summary-first-subject t)
6765         (gnus-summary-display-article (gnus-summary-article-number)))
6766     (gnus-summary-position-point)))
6767
6768 (defun gnus-summary-first-unread-subject ()
6769   "Place the point on the subject line of the first unread article.
6770 Return nil if there are no unread articles."
6771   (interactive)
6772   (prog1
6773       (when (gnus-summary-first-subject t)
6774         (gnus-summary-show-thread)
6775         (gnus-summary-first-subject t))
6776     (gnus-summary-position-point)))
6777
6778 (defun gnus-summary-first-article ()
6779   "Select the first article.
6780 Return nil if there are no articles."
6781   (interactive)
6782   (prog1
6783       (when (gnus-summary-first-subject)
6784         (gnus-summary-show-thread)
6785         (gnus-summary-first-subject)
6786         (gnus-summary-display-article (gnus-summary-article-number)))
6787     (gnus-summary-position-point)))
6788
6789 (defun gnus-summary-best-unread-article ()
6790   "Select the unread article with the highest score."
6791   (interactive)
6792   (let ((best -1000000)
6793         (data gnus-newsgroup-data)
6794         article score)
6795     (while data
6796       (and (gnus-data-unread-p (car data))
6797            (> (setq score
6798                     (gnus-summary-article-score (gnus-data-number (car data))))
6799               best)
6800            (setq best score
6801                  article (gnus-data-number (car data))))
6802       (setq data (cdr data)))
6803     (prog1
6804         (if article
6805             (gnus-summary-goto-article article)
6806           (error "No unread articles"))
6807       (gnus-summary-position-point))))
6808
6809 (defun gnus-summary-last-subject ()
6810   "Go to the last displayed subject line in the group."
6811   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6812     (when article
6813       (gnus-summary-goto-subject article))))
6814
6815 (defun gnus-summary-goto-article (article &optional all-headers force)
6816   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6817 If ALL-HEADERS is non-nil, no header lines are hidden.
6818 If FORCE, go to the article even if it isn't displayed.  If FORCE
6819 is a number, it is the line the article is to be displayed on."
6820   (interactive
6821    (list
6822     (completing-read
6823      "Article number or Message-ID: "
6824      (mapcar (lambda (number) (list (int-to-string number)))
6825              gnus-newsgroup-limit))
6826     current-prefix-arg
6827     t))
6828   (prog1
6829       (if (and (stringp article)
6830                (string-match "@" article))
6831           (gnus-summary-refer-article article)
6832         (when (stringp article)
6833           (setq article (string-to-number article)))
6834         (if (gnus-summary-goto-subject article force)
6835             (gnus-summary-display-article article all-headers)
6836           (gnus-message 4 "Couldn't go to article %s" article) nil))
6837     (gnus-summary-position-point)))
6838
6839 (defun gnus-summary-goto-last-article ()
6840   "Go to the previously read article."
6841   (interactive)
6842   (prog1
6843       (when gnus-last-article
6844         (gnus-summary-goto-article gnus-last-article nil t))
6845     (gnus-summary-position-point)))
6846
6847 (defun gnus-summary-pop-article (number)
6848   "Pop one article off the history and go to the previous.
6849 NUMBER articles will be popped off."
6850   (interactive "p")
6851   (let (to)
6852     (setq gnus-newsgroup-history
6853           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6854     (if to
6855         (gnus-summary-goto-article (car to) nil t)
6856       (error "Article history empty")))
6857   (gnus-summary-position-point))
6858
6859 ;; Summary commands and functions for limiting the summary buffer.
6860
6861 (defun gnus-summary-limit-to-articles (n)
6862   "Limit the summary buffer to the next N articles.
6863 If not given a prefix, use the process marked articles instead."
6864   (interactive "P")
6865   (prog1
6866       (let ((articles (gnus-summary-work-articles n)))
6867         (setq gnus-newsgroup-processable nil)
6868         (gnus-summary-limit articles))
6869     (gnus-summary-position-point)))
6870
6871 (defun gnus-summary-pop-limit (&optional total)
6872   "Restore the previous limit.
6873 If given a prefix, remove all limits."
6874   (interactive "P")
6875   (when total
6876     (setq gnus-newsgroup-limits
6877           (list (mapcar (lambda (h) (mail-header-number h))
6878                         gnus-newsgroup-headers))))
6879   (unless gnus-newsgroup-limits
6880     (error "No limit to pop"))
6881   (prog1
6882       (gnus-summary-limit nil 'pop)
6883     (gnus-summary-position-point)))
6884
6885 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
6886   "Limit the summary buffer to articles that have subjects that match a regexp.
6887 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
6888   (interactive
6889    (list (read-string (if current-prefix-arg
6890                           "Exclude subject (regexp): "
6891                         "Limit to subject (regexp): "))
6892          nil current-prefix-arg))
6893   (unless header
6894     (setq header "subject"))
6895   (when (not (equal "" subject))
6896     (prog1
6897         (let ((articles (gnus-summary-find-matching
6898                          (or header "subject") subject 'all nil nil
6899                          not-matching)))
6900           (unless articles
6901             (error "Found no matches for \"%s\"" subject))
6902           (gnus-summary-limit articles))
6903       (gnus-summary-position-point))))
6904
6905 (defun gnus-summary-limit-to-author (from &optional not-matching)
6906   "Limit the summary buffer to articles that have authors that match a regexp.
6907 If NOT-MATCHING, excluding articles that have authors that match a regexp."
6908   (interactive
6909    (list (read-string (if current-prefix-arg
6910                           "Exclude author (regexp): "
6911                         "Limit to author (regexp): "))
6912          current-prefix-arg))
6913   (gnus-summary-limit-to-subject from "from" not-matching))
6914
6915 (defun gnus-summary-limit-to-age (age &optional younger-p)
6916   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6917 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6918 articles that are younger than AGE days."
6919   (interactive
6920    (let ((younger current-prefix-arg)
6921          (days-got nil)
6922          days)
6923      (while (not days-got)
6924        (setq days (if younger
6925                       (read-string "Limit to articles within (in days): ")
6926                     (read-string "Limit to articles older than (in days): ")))
6927        (when (> (length days) 0)
6928          (setq days (read days)))
6929        (if (numberp days)
6930            (setq days-got t)
6931          (message "Please enter a number.")
6932          (sleep-for 1)))
6933      (list days younger)))
6934   (prog1
6935       (let ((data gnus-newsgroup-data)
6936             (cutoff (days-to-time age))
6937             articles d date is-younger)
6938         (while (setq d (pop data))
6939           (when (and (vectorp (gnus-data-header d))
6940                      (setq date (mail-header-date (gnus-data-header d))))
6941             (setq is-younger (time-less-p
6942                               (time-since (condition-case ()
6943                                               (date-to-time date)
6944                                             (error '(0 0))))
6945                               cutoff))
6946             (when (if younger-p
6947                       is-younger
6948                     (not is-younger))
6949               (push (gnus-data-number d) articles))))
6950         (gnus-summary-limit (nreverse articles)))
6951     (gnus-summary-position-point)))
6952
6953 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
6954   "Limit the summary buffer to articles that match an 'extra' header."
6955   (interactive
6956    (let ((header
6957           (intern
6958            (gnus-completing-read
6959             (symbol-name (car gnus-extra-headers))
6960             (if current-prefix-arg
6961                 "Exclude extra header:"
6962               "Limit extra header:")
6963             (mapcar (lambda (x)
6964                       (cons (symbol-name x) x))
6965                     gnus-extra-headers)
6966             nil
6967             t))))
6968      (list header
6969            (read-string (format "%s header %s (regexp): "
6970                                 (if current-prefix-arg "Exclude" "Limit to")
6971                                 header))
6972            current-prefix-arg)))
6973   (when (not (equal "" regexp))
6974     (prog1
6975         (let ((articles (gnus-summary-find-matching
6976                          (cons 'extra header) regexp 'all nil nil
6977                          not-matching)))
6978           (unless articles
6979             (error "Found no matches for \"%s\"" regexp))
6980           (gnus-summary-limit articles))
6981       (gnus-summary-position-point))))
6982
6983 (defun gnus-summary-limit-to-display-predicate ()
6984   "Limit the summary buffer to the predicated in the `display' group parameter."
6985   (interactive)
6986   (unless gnus-newsgroup-display
6987     (error "There is no `display' group parameter"))
6988   (let (articles)
6989     (dolist (number gnus-newsgroup-articles)
6990       (when (funcall gnus-newsgroup-display)
6991         (push number articles)))
6992     (gnus-summary-limit articles))
6993   (gnus-summary-position-point))
6994
6995 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6996 (make-obsolete
6997  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6998
6999 (defun gnus-summary-limit-to-unread (&optional all)
7000   "Limit the summary buffer to articles that are not marked as read.
7001 If ALL is non-nil, limit strictly to unread articles."
7002   (interactive "P")
7003   (if all
7004       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7005     (gnus-summary-limit-to-marks
7006      ;; Concat all the marks that say that an article is read and have
7007      ;; those removed.
7008      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7009            gnus-killed-mark gnus-kill-file-mark
7010            gnus-low-score-mark gnus-expirable-mark
7011            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7012            gnus-duplicate-mark gnus-souped-mark)
7013      'reverse)))
7014
7015 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7016 (make-obsolete 'gnus-summary-delete-marked-with
7017                'gnus-summary-limit-exlude-marks)
7018
7019 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7020   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7021 If REVERSE, limit the summary buffer to articles that are marked
7022 with MARKS.  MARKS can either be a string of marks or a list of marks.
7023 Returns how many articles were removed."
7024   (interactive "sMarks: ")
7025   (gnus-summary-limit-to-marks marks t))
7026
7027 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7028   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7029 If REVERSE (the prefix), limit the summary buffer to articles that are
7030 not marked with MARKS.  MARKS can either be a string of marks or a
7031 list of marks.
7032 Returns how many articles were removed."
7033   (interactive "sMarks: \nP")
7034   (prog1
7035       (let ((data gnus-newsgroup-data)
7036             (marks (if (listp marks) marks
7037                      (append marks nil))) ; Transform to list.
7038             articles)
7039         (while data
7040           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7041                   (memq (gnus-data-mark (car data)) marks))
7042             (push (gnus-data-number (car data)) articles))
7043           (setq data (cdr data)))
7044         (gnus-summary-limit articles))
7045     (gnus-summary-position-point)))
7046
7047 (defun gnus-summary-limit-to-score (score)
7048   "Limit to articles with score at or above SCORE."
7049   (interactive "NLimit to articles with score of at least: ")
7050   (let ((data gnus-newsgroup-data)
7051         articles)
7052     (while data
7053       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7054                 score)
7055         (push (gnus-data-number (car data)) articles))
7056       (setq data (cdr data)))
7057     (prog1
7058         (gnus-summary-limit articles)
7059       (gnus-summary-position-point))))
7060
7061 (defun gnus-summary-limit-include-thread (id)
7062   "Display all the hidden articles that is in the thread with ID in it.
7063 When called interactively, ID is the Message-ID of the current
7064 article."
7065   (interactive (list (mail-header-id (gnus-summary-article-header))))
7066   (let ((articles (gnus-articles-in-thread
7067                    (gnus-id-to-thread (gnus-root-id id)))))
7068     (prog1
7069         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7070       (gnus-summary-limit-include-matching-articles
7071        "subject"
7072        (regexp-quote (gnus-simplify-subject-re
7073                       (mail-header-subject (gnus-id-to-header id)))))
7074       (gnus-summary-position-point))))
7075
7076 (defun gnus-summary-limit-include-matching-articles (header regexp)
7077   "Display all the hidden articles that have HEADERs that match REGEXP."
7078   (interactive (list (read-string "Match on header: ")
7079                      (read-string "Regexp: ")))
7080   (let ((articles (gnus-find-matching-articles header regexp)))
7081     (prog1
7082         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7083       (gnus-summary-position-point))))
7084
7085 (defun gnus-summary-limit-include-dormant ()
7086   "Display all the hidden articles that are marked as dormant.
7087 Note that this command only works on a subset of the articles currently
7088 fetched for this group."
7089   (interactive)
7090   (unless gnus-newsgroup-dormant
7091     (error "There are no dormant articles in this group"))
7092   (prog1
7093       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7094     (gnus-summary-position-point)))
7095
7096 (defun gnus-summary-limit-exclude-dormant ()
7097   "Hide all dormant articles."
7098   (interactive)
7099   (prog1
7100       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7101     (gnus-summary-position-point)))
7102
7103 (defun gnus-summary-limit-exclude-childless-dormant ()
7104   "Hide all dormant articles that have no children."
7105   (interactive)
7106   (let ((data (gnus-data-list t))
7107         articles d children)
7108     ;; Find all articles that are either not dormant or have
7109     ;; children.
7110     (while (setq d (pop data))
7111       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7112                 (and (setq children
7113                            (gnus-article-children (gnus-data-number d)))
7114                      (let (found)
7115                        (while children
7116                          (when (memq (car children) articles)
7117                            (setq children nil
7118                                  found t))
7119                          (pop children))
7120                        found)))
7121         (push (gnus-data-number d) articles)))
7122     ;; Do the limiting.
7123     (prog1
7124         (gnus-summary-limit articles)
7125       (gnus-summary-position-point))))
7126
7127 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7128   "Mark all unread excluded articles as read.
7129 If ALL, mark even excluded ticked and dormants as read."
7130   (interactive "P")
7131   (let ((articles (gnus-sorted-complement
7132                    (sort
7133                     (mapcar (lambda (h) (mail-header-number h))
7134                             gnus-newsgroup-headers)
7135                     '<)
7136                    (sort gnus-newsgroup-limit '<)))
7137         article)
7138     (setq gnus-newsgroup-unreads
7139           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7140     (if all
7141         (setq gnus-newsgroup-dormant nil
7142               gnus-newsgroup-marked nil
7143               gnus-newsgroup-reads
7144               (nconc
7145                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7146                gnus-newsgroup-reads))
7147       (while (setq article (pop articles))
7148         (unless (or (memq article gnus-newsgroup-dormant)
7149                     (memq article gnus-newsgroup-marked))
7150           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7151
7152 (defun gnus-summary-limit (articles &optional pop)
7153   (if pop
7154       ;; We pop the previous limit off the stack and use that.
7155       (setq articles (car gnus-newsgroup-limits)
7156             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7157     ;; We use the new limit, so we push the old limit on the stack.
7158     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7159   ;; Set the limit.
7160   (setq gnus-newsgroup-limit articles)
7161   (let ((total (length gnus-newsgroup-data))
7162         (data (gnus-data-find-list (gnus-summary-article-number)))
7163         (gnus-summary-mark-below nil)   ; Inhibit this.
7164         found)
7165     ;; This will do all the work of generating the new summary buffer
7166     ;; according to the new limit.
7167     (gnus-summary-prepare)
7168     ;; Hide any threads, possibly.
7169     (and gnus-show-threads
7170          gnus-thread-hide-subtree
7171          (gnus-summary-hide-all-threads))
7172     ;; Try to return to the article you were at, or one in the
7173     ;; neighborhood.
7174     (when data
7175       ;; We try to find some article after the current one.
7176       (while data
7177         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7178           (setq data nil
7179                 found t))
7180         (setq data (cdr data))))
7181     (unless found
7182       ;; If there is no data, that means that we were after the last
7183       ;; article.  The same goes when we can't find any articles
7184       ;; after the current one.
7185       (goto-char (point-max))
7186       (gnus-summary-find-prev))
7187     (gnus-set-mode-line 'summary)
7188     ;; We return how many articles were removed from the summary
7189     ;; buffer as a result of the new limit.
7190     (- total (length gnus-newsgroup-data))))
7191
7192 (defsubst gnus-invisible-cut-children (threads)
7193   (let ((num 0))
7194     (while threads
7195       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7196         (incf num))
7197       (pop threads))
7198     (< num 2)))
7199
7200 (defsubst gnus-cut-thread (thread)
7201   "Go forwards in the thread until we find an article that we want to display."
7202   (when (or (eq gnus-fetch-old-headers 'some)
7203             (eq gnus-fetch-old-headers 'invisible)
7204             (numberp gnus-fetch-old-headers)
7205             (eq gnus-build-sparse-threads 'some)
7206             (eq gnus-build-sparse-threads 'more))
7207     ;; Deal with old-fetched headers and sparse threads.
7208     (while (and
7209             thread
7210             (or
7211              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7212              (gnus-summary-article-ancient-p
7213               (mail-header-number (car thread))))
7214             (if (or (<= (length (cdr thread)) 1)
7215                     (eq gnus-fetch-old-headers 'invisible))
7216                 (setq gnus-newsgroup-limit
7217                       (delq (mail-header-number (car thread))
7218                             gnus-newsgroup-limit)
7219                       thread (cadr thread))
7220               (when (gnus-invisible-cut-children (cdr thread))
7221                 (let ((th (cdr thread)))
7222                   (while th
7223                     (if (memq (mail-header-number (caar th))
7224                               gnus-newsgroup-limit)
7225                         (setq thread (car th)
7226                               th nil)
7227                       (setq th (cdr th))))))))))
7228   thread)
7229
7230 (defun gnus-cut-threads (threads)
7231   "Cut off all uninteresting articles from the beginning of threads."
7232   (when (or (eq gnus-fetch-old-headers 'some)
7233             (eq gnus-fetch-old-headers 'invisible)
7234             (numberp gnus-fetch-old-headers)
7235             (eq gnus-build-sparse-threads 'some)
7236             (eq gnus-build-sparse-threads 'more))
7237     (let ((th threads))
7238       (while th
7239         (setcar th (gnus-cut-thread (car th)))
7240         (setq th (cdr th)))))
7241   ;; Remove nixed out threads.
7242   (delq nil threads))
7243
7244 (defun gnus-summary-initial-limit (&optional show-if-empty)
7245   "Figure out what the initial limit is supposed to be on group entry.
7246 This entails weeding out unwanted dormants, low-scored articles,
7247 fetch-old-headers verbiage, and so on."
7248   ;; Most groups have nothing to remove.
7249   (if (or gnus-inhibit-limiting
7250           (and (null gnus-newsgroup-dormant)
7251                (eq gnus-newsgroup-display 'gnus-not-ignore)
7252                (not (eq gnus-fetch-old-headers 'some))
7253                (not (numberp gnus-fetch-old-headers))
7254                (not (eq gnus-fetch-old-headers 'invisible))
7255                (null gnus-summary-expunge-below)
7256                (not (eq gnus-build-sparse-threads 'some))
7257                (not (eq gnus-build-sparse-threads 'more))
7258                (null gnus-thread-expunge-below)
7259                (not gnus-use-nocem)))
7260       ()                                ; Do nothing.
7261     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7262     (setq gnus-newsgroup-limit nil)
7263     (mapatoms
7264      (lambda (node)
7265        (unless (car (symbol-value node))
7266          ;; These threads have no parents -- they are roots.
7267          (let ((nodes (cdr (symbol-value node)))
7268                thread)
7269            (while nodes
7270              (if (and gnus-thread-expunge-below
7271                       (< (gnus-thread-total-score (car nodes))
7272                          gnus-thread-expunge-below))
7273                  (gnus-expunge-thread (pop nodes))
7274                (setq thread (pop nodes))
7275                (gnus-summary-limit-children thread))))))
7276      gnus-newsgroup-dependencies)
7277     ;; If this limitation resulted in an empty group, we might
7278     ;; pop the previous limit and use it instead.
7279     (when (and (not gnus-newsgroup-limit)
7280                show-if-empty)
7281       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7282     gnus-newsgroup-limit))
7283
7284 (defun gnus-summary-limit-children (thread)
7285   "Return 1 if this subthread is visible and 0 if it is not."
7286   ;; First we get the number of visible children to this thread.  This
7287   ;; is done by recursing down the thread using this function, so this
7288   ;; will really go down to a leaf article first, before slowly
7289   ;; working its way up towards the root.
7290   (when thread
7291     (let ((children
7292            (if (cdr thread)
7293                (apply '+ (mapcar 'gnus-summary-limit-children
7294                                  (cdr thread)))
7295              0))
7296           (number (mail-header-number (car thread)))
7297           score)
7298       (if (and
7299            (not (memq number gnus-newsgroup-marked))
7300            (or
7301             ;; If this article is dormant and has absolutely no visible
7302             ;; children, then this article isn't visible.
7303             (and (memq number gnus-newsgroup-dormant)
7304                  (zerop children))
7305             ;; If this is "fetch-old-headered" and there is no
7306             ;; visible children, then we don't want this article.
7307             (and (or (eq gnus-fetch-old-headers 'some)
7308                      (numberp gnus-fetch-old-headers))
7309                  (gnus-summary-article-ancient-p number)
7310                  (zerop children))
7311             ;; If this is "fetch-old-headered" and `invisible', then
7312             ;; we don't want this article.
7313             (and (eq gnus-fetch-old-headers 'invisible)
7314                  (gnus-summary-article-ancient-p number))
7315             ;; If this is a sparsely inserted article with no children,
7316             ;; we don't want it.
7317             (and (eq gnus-build-sparse-threads 'some)
7318                  (gnus-summary-article-sparse-p number)
7319                  (zerop children))
7320             ;; If we use expunging, and this article is really
7321             ;; low-scored, then we don't want this article.
7322             (when (and gnus-summary-expunge-below
7323                        (< (setq score
7324                                 (or (cdr (assq number gnus-newsgroup-scored))
7325                                     gnus-summary-default-score))
7326                           gnus-summary-expunge-below))
7327               ;; We increase the expunge-tally here, but that has
7328               ;; nothing to do with the limits, really.
7329               (incf gnus-newsgroup-expunged-tally)
7330               ;; We also mark as read here, if that's wanted.
7331               (when (and gnus-summary-mark-below
7332                          (< score gnus-summary-mark-below))
7333                 (setq gnus-newsgroup-unreads
7334                       (delq number gnus-newsgroup-unreads))
7335                 (if gnus-newsgroup-auto-expire
7336                     (push number gnus-newsgroup-expirable)
7337                   (push (cons number gnus-low-score-mark)
7338                         gnus-newsgroup-reads)))
7339               t)
7340             ;; Do the `display' group parameter.
7341             (and gnus-newsgroup-display
7342                  (not (funcall gnus-newsgroup-display)))
7343             ;; Check NoCeM things.
7344             (if (and gnus-use-nocem
7345                      (gnus-nocem-unwanted-article-p
7346                       (mail-header-id (car thread))))
7347                 (progn
7348                   (setq gnus-newsgroup-unreads
7349                         (delq number gnus-newsgroup-unreads))
7350                   t))))
7351           ;; Nope, invisible article.
7352           0
7353         ;; Ok, this article is to be visible, so we add it to the limit
7354         ;; and return 1.
7355         (push number gnus-newsgroup-limit)
7356         1))))
7357
7358 (defun gnus-expunge-thread (thread)
7359   "Mark all articles in THREAD as read."
7360   (let* ((number (mail-header-number (car thread))))
7361     (incf gnus-newsgroup-expunged-tally)
7362     ;; We also mark as read here, if that's wanted.
7363     (setq gnus-newsgroup-unreads
7364           (delq number gnus-newsgroup-unreads))
7365     (if gnus-newsgroup-auto-expire
7366         (push number gnus-newsgroup-expirable)
7367       (push (cons number gnus-low-score-mark)
7368             gnus-newsgroup-reads)))
7369   ;; Go recursively through all subthreads.
7370   (mapcar 'gnus-expunge-thread (cdr thread)))
7371
7372 ;; Summary article oriented commands
7373
7374 (defun gnus-summary-refer-parent-article (n)
7375   "Refer parent article N times.
7376 If N is negative, go to ancestor -N instead.
7377 The difference between N and the number of articles fetched is returned."
7378   (interactive "p")
7379   (let ((skip 1)
7380         error header ref)
7381     (when (not (natnump n))
7382       (setq skip (abs n)
7383             n 1))
7384     (while (and (> n 0)
7385                 (not error))
7386       (setq header (gnus-summary-article-header))
7387       (if (and (eq (mail-header-number header)
7388                    (cdr gnus-article-current))
7389                (equal gnus-newsgroup-name
7390                       (car gnus-article-current)))
7391           ;; If we try to find the parent of the currently
7392           ;; displayed article, then we take a look at the actual
7393           ;; References header, since this is slightly more
7394           ;; reliable than the References field we got from the
7395           ;; server.
7396           (save-excursion
7397             (set-buffer gnus-original-article-buffer)
7398             (nnheader-narrow-to-headers)
7399             (unless (setq ref (message-fetch-field "references"))
7400               (setq ref (message-fetch-field "in-reply-to")))
7401             (widen))
7402         (setq ref
7403               ;; It's not the current article, so we take a bet on
7404               ;; the value we got from the server.
7405               (mail-header-references header)))
7406       (if (and ref
7407                (not (equal ref "")))
7408           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7409             (gnus-message 1 "Couldn't find parent"))
7410         (gnus-message 1 "No references in article %d"
7411                       (gnus-summary-article-number))
7412         (setq error t))
7413       (decf n))
7414     (gnus-summary-position-point)
7415     n))
7416
7417 (defun gnus-summary-refer-references ()
7418   "Fetch all articles mentioned in the References header.
7419 Return the number of articles fetched."
7420   (interactive)
7421   (let ((ref (mail-header-references (gnus-summary-article-header)))
7422         (current (gnus-summary-article-number))
7423         (n 0))
7424     (if (or (not ref)
7425             (equal ref ""))
7426         (error "No References in the current article")
7427       ;; For each Message-ID in the References header...
7428       (while (string-match "<[^>]*>" ref)
7429         (incf n)
7430         ;; ... fetch that article.
7431         (gnus-summary-refer-article
7432          (prog1 (match-string 0 ref)
7433            (setq ref (substring ref (match-end 0))))))
7434       (gnus-summary-goto-subject current)
7435       (gnus-summary-position-point)
7436       n)))
7437
7438 (defun gnus-summary-refer-thread (&optional limit)
7439   "Fetch all articles in the current thread.
7440 If LIMIT (the numerical prefix), fetch that many old headers instead
7441 of what's specified by the `gnus-refer-thread-limit' variable."
7442   (interactive "P")
7443   (let ((id (mail-header-id (gnus-summary-article-header)))
7444         (limit (if limit (prefix-numeric-value limit)
7445                  gnus-refer-thread-limit)))
7446     ;; We want to fetch LIMIT *old* headers, but we also have to
7447     ;; re-fetch all the headers in the current buffer, because many of
7448     ;; them may be undisplayed.  So we adjust LIMIT.
7449     (when (numberp limit)
7450       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7451     (unless (eq gnus-fetch-old-headers 'invisible)
7452       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7453       ;; Retrieve the headers and read them in.
7454       (if (eq (gnus-retrieve-headers
7455                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7456               'nov)
7457           (gnus-build-all-threads)
7458         (error "Can't fetch thread from backends that don't support NOV"))
7459       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7460     (gnus-summary-limit-include-thread id)))
7461
7462 (defun gnus-summary-refer-article (message-id)
7463   "Fetch an article specified by MESSAGE-ID."
7464   (interactive "sMessage-ID: ")
7465   (when (and (stringp message-id)
7466              (not (zerop (length message-id))))
7467     ;; Construct the correct Message-ID if necessary.
7468     ;; Suggested by tale@pawl.rpi.edu.
7469     (unless (string-match "^<" message-id)
7470       (setq message-id (concat "<" message-id)))
7471     (unless (string-match ">$" message-id)
7472       (setq message-id (concat message-id ">")))
7473     (let* ((header (gnus-id-to-header message-id))
7474            (sparse (and header
7475                         (gnus-summary-article-sparse-p
7476                          (mail-header-number header))
7477                         (memq (mail-header-number header)
7478                               gnus-newsgroup-limit)))
7479            number)
7480       (cond
7481        ;; If the article is present in the buffer we just go to it.
7482        ((and header
7483              (or (not (gnus-summary-article-sparse-p
7484                        (mail-header-number header)))
7485                  sparse))
7486         (prog1
7487             (gnus-summary-goto-article
7488              (mail-header-number header) nil t)
7489           (when sparse
7490             (gnus-summary-update-article (mail-header-number header)))))
7491        (t
7492         ;; We fetch the article.
7493         (catch 'found
7494           (dolist (gnus-override-method (gnus-refer-article-methods))
7495             (gnus-check-server gnus-override-method)
7496             ;; Fetch the header, and display the article.
7497             (when (setq number (gnus-summary-insert-subject message-id))
7498               (gnus-summary-select-article nil nil nil number)
7499               (throw 'found t)))
7500           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7501
7502 (defun gnus-refer-article-methods ()
7503   "Return a list of referrable methods."
7504   (cond
7505    ;; No method, so we default to current and native.
7506    ((null gnus-refer-article-method)
7507     (list gnus-current-select-method gnus-select-method))
7508    ;; Current.
7509    ((eq 'current gnus-refer-article-method)
7510     (list gnus-current-select-method))
7511    ;; List of select methods.
7512    ((not (and (symbolp (car gnus-refer-article-method))
7513               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7514     (let (out)
7515       (dolist (method gnus-refer-article-method)
7516         (push (if (eq 'current method)
7517                   gnus-current-select-method
7518                 method)
7519               out))
7520       (nreverse out)))
7521    ;; One single select method.
7522    (t
7523     (list gnus-refer-article-method))))
7524
7525 (defun gnus-summary-edit-parameters ()
7526   "Edit the group parameters of the current group."
7527   (interactive)
7528   (gnus-group-edit-group gnus-newsgroup-name 'params))
7529
7530 (defun gnus-summary-customize-parameters ()
7531   "Customize the group parameters of the current group."
7532   (interactive)
7533   (gnus-group-customize gnus-newsgroup-name))
7534
7535 (defun gnus-summary-enter-digest-group (&optional force)
7536   "Enter an nndoc group based on the current article.
7537 If FORCE, force a digest interpretation.  If not, try
7538 to guess what the document format is."
7539   (interactive "P")
7540   (let ((conf gnus-current-window-configuration))
7541     (save-excursion
7542       (gnus-summary-select-article))
7543     (setq gnus-current-window-configuration conf)
7544     (let* ((name (format "%s-%d"
7545                          (gnus-group-prefixed-name
7546                           gnus-newsgroup-name (list 'nndoc ""))
7547                          (save-excursion
7548                            (set-buffer gnus-summary-buffer)
7549                            gnus-current-article)))
7550            (ogroup gnus-newsgroup-name)
7551            (params (append (gnus-info-params (gnus-get-info ogroup))
7552                            (list (cons 'to-group ogroup))
7553                            (list (cons 'save-article-group ogroup))))
7554            (case-fold-search t)
7555            (buf (current-buffer))
7556            dig to-address)
7557       (save-excursion
7558         (set-buffer gnus-original-article-buffer)
7559         ;; Have the digest group inherit the main mail address of
7560         ;; the parent article.
7561         (when (setq to-address (or (message-fetch-field "reply-to")
7562                                    (message-fetch-field "from")))
7563           (setq params (append
7564                         (list (cons 'to-address
7565                                     (funcall gnus-decode-encoded-word-function
7566                                              to-address))))))
7567         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7568         (insert-buffer-substring gnus-original-article-buffer)
7569         ;; Remove lines that may lead nndoc to misinterpret the
7570         ;; document type.
7571         (narrow-to-region
7572          (goto-char (point-min))
7573          (or (search-forward "\n\n" nil t) (point)))
7574         (goto-char (point-min))
7575         (delete-matching-lines "^Path:\\|^From ")
7576         (widen))
7577       (unwind-protect
7578           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7579                     (gnus-newsgroup-ephemeral-ignored-charsets
7580                      gnus-newsgroup-ignored-charsets))
7581                 (gnus-group-read-ephemeral-group
7582                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7583                               (nndoc-article-type
7584                                ,(if force 'mbox 'guess))) t))
7585               ;; Make all postings to this group go to the parent group.
7586               (nconc (gnus-info-params (gnus-get-info name))
7587                      params)
7588             ;; Couldn't select this doc group.
7589             (switch-to-buffer buf)
7590             (gnus-set-global-variables)
7591             (gnus-configure-windows 'summary)
7592             (gnus-message 3 "Article couldn't be entered?"))
7593         (kill-buffer dig)))))
7594
7595 (defun gnus-summary-read-document (n)
7596   "Open a new group based on the current article(s).
7597 This will allow you to read digests and other similar
7598 documents as newsgroups.
7599 Obeys the standard process/prefix convention."
7600   (interactive "P")
7601   (let* ((articles (gnus-summary-work-articles n))
7602          (ogroup gnus-newsgroup-name)
7603          (params (append (gnus-info-params (gnus-get-info ogroup))
7604                          (list (cons 'to-group ogroup))))
7605          article group egroup groups vgroup)
7606     (while (setq article (pop articles))
7607       (setq group (format "%s-%d" gnus-newsgroup-name article))
7608       (gnus-summary-remove-process-mark article)
7609       (when (gnus-summary-display-article article)
7610         (save-excursion
7611           (with-temp-buffer
7612             (insert-buffer-substring gnus-original-article-buffer)
7613             ;; Remove some headers that may lead nndoc to make
7614             ;; the wrong guess.
7615             (message-narrow-to-head)
7616             (goto-char (point-min))
7617             (delete-matching-lines "^\\(Path\\):\\|^From ")
7618             (widen)
7619             (if (setq egroup
7620                       (gnus-group-read-ephemeral-group
7621                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7622                                      (nndoc-article-type guess))
7623                        t nil t))
7624                 (progn
7625                   ;; Make all postings to this group go to the parent group.
7626                   (nconc (gnus-info-params (gnus-get-info egroup))
7627                          params)
7628                   (push egroup groups))
7629               ;; Couldn't select this doc group.
7630               (gnus-error 3 "Article couldn't be entered"))))))
7631     ;; Now we have selected all the documents.
7632     (cond
7633      ((not groups)
7634       (error "None of the articles could be interpreted as documents"))
7635      ((gnus-group-read-ephemeral-group
7636        (setq vgroup (format
7637                      "nnvirtual:%s-%s" gnus-newsgroup-name
7638                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7639        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7640        t
7641        (cons (current-buffer) 'summary)))
7642      (t
7643       (error "Couldn't select virtual nndoc group")))))
7644
7645 (defun gnus-summary-isearch-article (&optional regexp-p)
7646   "Do incremental search forward on the current article.
7647 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7648   (interactive "P")
7649   (let* ((gnus-inhibit-treatment t)
7650          (old (gnus-summary-select-article)))
7651     (gnus-configure-windows 'article)
7652     (gnus-eval-in-buffer-window gnus-article-buffer
7653       (save-restriction
7654         (widen)
7655         (when (eq 'old old)
7656           (gnus-article-show-all-headers))
7657         (goto-char (point-min))
7658         (isearch-forward regexp-p)))))
7659
7660 (defun gnus-summary-search-article-forward (regexp &optional backward)
7661   "Search for an article containing REGEXP forward.
7662 If BACKWARD, search backward instead."
7663   (interactive
7664    (list (read-string
7665           (format "Search article %s (regexp%s): "
7666                   (if current-prefix-arg "backward" "forward")
7667                   (if gnus-last-search-regexp
7668                       (concat ", default " gnus-last-search-regexp)
7669                     "")))
7670          current-prefix-arg))
7671   (if (string-equal regexp "")
7672       (setq regexp (or gnus-last-search-regexp ""))
7673     (setq gnus-last-search-regexp regexp)
7674     (setq gnus-article-before-search gnus-current-article))
7675   ;; Intentionally set gnus-last-article.
7676   (setq gnus-last-article gnus-article-before-search)
7677   (let ((gnus-last-article gnus-last-article))
7678     (if (gnus-summary-search-article regexp backward)
7679         (gnus-summary-show-thread)
7680       (error "Search failed: \"%s\"" regexp))))
7681
7682 (defun gnus-summary-search-article-backward (regexp)
7683   "Search for an article containing REGEXP backward."
7684   (interactive
7685    (list (read-string
7686           (format "Search article backward (regexp%s): "
7687                   (if gnus-last-search-regexp
7688                       (concat ", default " gnus-last-search-regexp)
7689                     "")))))
7690   (gnus-summary-search-article-forward regexp 'backward))
7691
7692 (eval-when-compile
7693   (defmacro gnus-summary-search-article-position-point (regexp backward)
7694     "Dehighlight the last matched text and goto the beginning position."
7695     (` (if (and gnus-summary-search-article-matched-data
7696                 (let ((text (caddr gnus-summary-search-article-matched-data))
7697                       (inhibit-read-only t)
7698                       buffer-read-only)
7699                   (delete-region
7700                    (goto-char (car gnus-summary-search-article-matched-data))
7701                    (cadr gnus-summary-search-article-matched-data))
7702                   (insert text)
7703                   (string-match (, regexp) text)))
7704            (if (, backward) (beginning-of-line) (end-of-line))
7705          (goto-char (if (, backward) (point-max) (point-min))))))
7706
7707   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7708     "Place point where X-Face image is displayed."
7709     (if (featurep 'xemacs)
7710         (` (let ((end (if (search-forward "\n\n" nil t)
7711                           (goto-char (1- (point)))
7712                         (point-min)))
7713                  extent)
7714              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7715              (unless (and (re-search-forward "^From:" end t)
7716                           (setq extent (extent-at (point)))
7717                           (extent-begin-glyph extent))
7718                (goto-char (, opoint)))))
7719       (` (let ((end (if (search-forward "\n\n" nil t)
7720                         (goto-char (1- (point)))
7721                       (point-min)))
7722                (start (or (search-backward "\n\n" nil t) (point-min))))
7723            (goto-char
7724             (or (text-property-any start end 'x-face-image t);; x-face-e21
7725                 (text-property-any start end 'x-face-mule-bitmap-image t)
7726                 (, opoint)))))))
7727
7728   (defmacro gnus-summary-search-article-highlight-matched-text
7729     (backward treated x-face)
7730     "Highlight matched text in the function `gnus-summary-search-article'."
7731     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7732              (end (set-marker (make-marker) (match-end 0)))
7733              (inhibit-read-only t)
7734              buffer-read-only)
7735          (unless treated
7736            (let ((,@
7737                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7738                     (mapcar
7739                      (lambda (item) (setq items (delq item items)))
7740                      '(gnus-treat-buttonize
7741                        gnus-treat-fill-article
7742                        gnus-treat-fill-long-lines
7743                        gnus-treat-emphasize
7744                        gnus-treat-highlight-headers
7745                        gnus-treat-highlight-citation
7746                        gnus-treat-highlight-signature
7747                        gnus-treat-overstrike
7748                        gnus-treat-display-xface
7749                        gnus-treat-buttonize-head
7750                        gnus-treat-decode-article-as-default-mime-charset))
7751                     (static-if (featurep 'xemacs)
7752                         items
7753                       (cons '(x-face-mule-delete-x-face-field
7754                               (quote never))
7755                             items))))
7756                  (gnus-treat-display-xface
7757                   (when (, x-face) gnus-treat-display-xface)))
7758              (gnus-article-prepare-mime-display)))
7759          (goto-char (if (, backward) start end))
7760          (when (, x-face)
7761            (gnus-summary-search-article-highlight-goto-x-face (point)))
7762          (setq gnus-summary-search-article-matched-data
7763                (list start end (buffer-substring start end)))
7764          (unless (eq start end);; matched text has been deleted. :-<
7765            (put-text-property start end 'face
7766                               (or (find-face 'isearch)
7767                                   'secondary-selection))))))
7768   )
7769
7770 (defun gnus-summary-search-article (regexp &optional backward)
7771   "Search for an article containing REGEXP.
7772 Optional argument BACKWARD means do search for backward.
7773 `gnus-select-article-hook' is not called during the search."
7774   ;; We have to require this here to make sure that the following
7775   ;; dynamic binding isn't shadowed by autoloading.
7776   (require 'gnus-async)
7777   (require 'gnus-art)
7778   (let ((gnus-select-article-hook nil)  ;Disable hook.
7779         (gnus-article-prepare-hook nil)
7780         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7781         (gnus-use-article-prefetch nil)
7782         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7783         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7784         (sum (current-buffer))
7785         (found nil)
7786         point treated)
7787     (gnus-save-hidden-threads
7788       (static-if (featurep 'xemacs)
7789           (let ((gnus-inhibit-treatment t))
7790             (setq treated (eq 'old (gnus-summary-select-article)))
7791             (when (and treated
7792                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7793                                  (window-live-p (get-buffer-window
7794                                                  gnus-article-buffer t)))))
7795               (gnus-summary-select-article nil t)
7796               (setq treated nil)))
7797         (let ((gnus-inhibit-treatment t)
7798               (x-face-mule-delete-x-face-field 'never))
7799           (setq treated (eq 'old (gnus-summary-select-article)))
7800           (when (and treated
7801                      (not
7802                       (and (gnus-buffer-live-p gnus-article-buffer)
7803                            (window-live-p (get-buffer-window
7804                                            gnus-article-buffer t))
7805                            (or (not (string-match "^\\^X-Face:" regexp))
7806                                (with-current-buffer gnus-article-buffer
7807                                  gnus-summary-search-article-matched-data)))))
7808             (gnus-summary-select-article nil t)
7809             (setq treated nil))))
7810       (set-buffer gnus-article-buffer)
7811       (widen)
7812       (if treated
7813           (progn
7814             (gnus-article-show-all-headers)
7815             (gnus-summary-search-article-position-point regexp backward))
7816         (goto-char (if backward (point-max) (point-min))))
7817       (while (not found)
7818         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7819         (if (if backward
7820                 (re-search-backward regexp nil t)
7821               (re-search-forward regexp nil t))
7822             ;; We found the regexp.
7823             (progn
7824               (gnus-summary-search-article-highlight-matched-text
7825                backward treated (string-match "^\\^X-Face:" regexp))
7826               (setq found 'found)
7827               (forward-line
7828                (/ (- 2 (window-height
7829                         (get-buffer-window gnus-article-buffer t)))
7830                   2))
7831               (set-window-start
7832                (get-buffer-window (current-buffer))
7833                (point))
7834               (set-buffer sum)
7835               (setq point (point)))
7836           ;; We didn't find it, so we go to the next article.
7837           (set-buffer sum)
7838           (setq found 'not)
7839           (while (eq found 'not)
7840             (if (not (if backward (gnus-summary-find-prev)
7841                        (gnus-summary-find-next)))
7842                 ;; No more articles.
7843                 (setq found t)
7844               ;; Select the next article and adjust point.
7845               (unless (gnus-summary-article-sparse-p
7846                        (gnus-summary-article-number))
7847                 (setq found nil)
7848                 (let ((gnus-inhibit-treatment t))
7849                   (gnus-summary-select-article))
7850                 (setq treated nil)
7851                 (set-buffer gnus-article-buffer)
7852                 (widen)
7853                 (goto-char (if backward (point-max) (point-min))))))))
7854       (gnus-message 7 ""))
7855     ;; Return whether we found the regexp.
7856     (when (eq found 'found)
7857       (goto-char point)
7858       (gnus-summary-show-thread)
7859       (gnus-summary-goto-subject gnus-current-article)
7860       (gnus-summary-position-point)
7861       t)))
7862
7863 (defun gnus-find-matching-articles (header regexp)
7864   "Return a list of all articles that match REGEXP on HEADER.
7865 This search includes all articles in the current group that Gnus has
7866 fetched headers for, whether they are displayed or not."
7867   (let ((articles nil)
7868         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7869         (case-fold-search t))
7870     (dolist (header gnus-newsgroup-headers)
7871       (when (string-match regexp (funcall func header))
7872         (push (mail-header-number header) articles)))
7873     (nreverse articles)))
7874
7875 (defun gnus-summary-find-matching (header regexp &optional backward unread
7876                                           not-case-fold not-matching)
7877   "Return a list of all articles that match REGEXP on HEADER.
7878 The search stars on the current article and goes forwards unless
7879 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7880 If UNREAD is non-nil, only unread articles will
7881 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7882 in the comparisons. If NOT-MATCHING, return a list of all articles that
7883 not match REGEXP on HEADER."
7884   (let ((case-fold-search (not not-case-fold))
7885         articles d func)
7886     (if (consp header)
7887         (if (eq (car header) 'extra)
7888             (setq func
7889                   `(lambda (h)
7890                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7891                          "")))
7892           (error "%s is an invalid header" header))
7893       (unless (fboundp (intern (concat "mail-header-" header)))
7894         (error "%s is not a valid header" header))
7895       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7896     (dolist (d (if (eq backward 'all)
7897                    gnus-newsgroup-data
7898                  (gnus-data-find-list
7899                   (gnus-summary-article-number)
7900                   (gnus-data-list backward))))
7901       (when (and (or (not unread)       ; We want all articles...
7902                      (gnus-data-unread-p d)) ; Or just unreads.
7903                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7904                  (if not-matching
7905                      (not (string-match
7906                            regexp
7907                            (funcall func (gnus-data-header d))))
7908                    (string-match regexp
7909                                  (funcall func (gnus-data-header d)))))
7910         (push (gnus-data-number d) articles))) ; Success!
7911     (nreverse articles)))
7912
7913 (defun gnus-summary-execute-command (header regexp command &optional backward)
7914   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7915 If HEADER is an empty string (or nil), the match is done on the entire
7916 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7917   (interactive
7918    (list (let ((completion-ignore-case t))
7919            (completing-read
7920             "Header name: "
7921             (mapcar (lambda (header) (list (format "%s" header)))
7922                     (append
7923                      '("Number" "Subject" "From" "Lines" "Date"
7924                        "Message-ID" "Xref" "References" "Body")
7925                      gnus-extra-headers))
7926             nil 'require-match))
7927          (read-string "Regexp: ")
7928          (read-key-sequence "Command: ")
7929          current-prefix-arg))
7930   (when (equal header "Body")
7931     (setq header ""))
7932   ;; Hidden thread subtrees must be searched as well.
7933   (gnus-summary-show-all-threads)
7934   ;; We don't want to change current point nor window configuration.
7935   (save-excursion
7936     (save-window-excursion
7937       (gnus-message 6 "Executing %s..." (key-description command))
7938       ;; We'd like to execute COMMAND interactively so as to give arguments.
7939       (gnus-execute header regexp
7940                     `(call-interactively ',(key-binding command))
7941                     backward)
7942       (gnus-message 6 "Executing %s...done" (key-description command)))))
7943
7944 (defun gnus-summary-beginning-of-article ()
7945   "Scroll the article back to the beginning."
7946   (interactive)
7947   (gnus-summary-select-article)
7948   (gnus-configure-windows 'article)
7949   (gnus-eval-in-buffer-window gnus-article-buffer
7950     (widen)
7951     (goto-char (point-min))
7952     (when gnus-page-broken
7953       (gnus-narrow-to-page))))
7954
7955 (defun gnus-summary-end-of-article ()
7956   "Scroll to the end of the article."
7957   (interactive)
7958   (gnus-summary-select-article)
7959   (gnus-configure-windows 'article)
7960   (gnus-eval-in-buffer-window gnus-article-buffer
7961     (widen)
7962     (goto-char (point-max))
7963     (recenter -3)
7964     (when gnus-page-broken
7965       (gnus-narrow-to-page))))
7966
7967 (defun gnus-summary-print-truncate-and-quote (string &optional len)
7968   "Truncate to LEN and quote all \"(\"'s in STRING."
7969   (gnus-replace-in-string (if (and len (> (length string) len))
7970                               (substring string 0 len)
7971                             string)
7972                           "[()]" "\\\\\\&"))
7973
7974 (defun gnus-summary-print-article (&optional filename n)
7975   "Generate and print a PostScript image of the N next (mail) articles.
7976
7977 If N is negative, print the N previous articles.  If N is nil and articles
7978 have been marked with the process mark, print these instead.
7979
7980 If the optional first argument FILENAME is nil, send the image to the
7981 printer.  If FILENAME is a string, save the PostScript image in a file with
7982 that name.  If FILENAME is a number, prompt the user for the name of the file
7983 to save in."
7984   (interactive (list (ps-print-preprint current-prefix-arg)))
7985   (dolist (article (gnus-summary-work-articles n))
7986     (gnus-summary-select-article nil nil 'pseudo article)
7987     (gnus-eval-in-buffer-window gnus-article-buffer
7988       (let ((buffer (generate-new-buffer " *print*")))
7989         (unwind-protect
7990             (progn
7991               (copy-to-buffer buffer (point-min) (point-max))
7992               (set-buffer buffer)
7993               (gnus-article-delete-invisible-text)
7994               (when (gnus-visual-p 'article-highlight 'highlight)
7995                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7996                 ;; highlight.
7997                 (let ((gnus-article-buffer buffer))
7998                   (gnus-article-highlight-citation t)
7999                   (gnus-article-highlight-signature)))
8000               (let ((ps-left-header
8001                      (list
8002                       (concat "("
8003                               (gnus-summary-print-truncate-and-quote
8004                                (mail-header-subject gnus-current-headers)
8005                                66) ")")
8006                       (concat "("
8007                               (gnus-summary-print-truncate-and-quote
8008                                (mail-header-from gnus-current-headers)
8009                                45) ")")))
8010                     (ps-right-header
8011                      (list
8012                       "/pagenumberstring load"
8013                       (concat "("
8014                               (mail-header-date gnus-current-headers) ")"))))
8015                 (gnus-run-hooks 'gnus-ps-print-hook)
8016                 (save-excursion
8017                   (if window-system
8018                       (ps-spool-buffer-with-faces)
8019                     (ps-spool-buffer)))))
8020           (kill-buffer buffer))))
8021     (gnus-summary-remove-process-mark article))
8022   (ps-despool filename))
8023
8024 (defun gnus-summary-show-article (&optional arg)
8025   "Force redisplaying of the current article.
8026 If ARG (the prefix) is a number, show the article with the charset
8027 defined in `gnus-summary-show-article-charset-alist', or the charset
8028 input.
8029 If ARG (the prefix) is non-nil and not a number, show the raw article
8030 without any article massaging functions being run.  Normally, the key strokes
8031 are `C-u g'."
8032   (interactive "P")
8033   (cond
8034    ((numberp arg)
8035     (gnus-summary-show-article t)
8036     (let ((gnus-newsgroup-charset
8037            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8038                (mm-read-coding-system
8039                 "View as charset: "
8040                 (save-excursion
8041                   (set-buffer gnus-article-buffer)
8042                   (let ((coding-systems
8043                          (detect-coding-region (point) (point-max))))
8044                     (or (car-safe coding-systems)
8045                         coding-systems))))))
8046           (gnus-newsgroup-ignored-charsets 'gnus-all))
8047       (gnus-summary-select-article nil 'force)
8048       (let ((deps gnus-newsgroup-dependencies)
8049             head header lines)
8050         (save-excursion
8051           (set-buffer gnus-original-article-buffer)
8052           (save-restriction
8053             (message-narrow-to-head)
8054             (setq head (buffer-string))
8055             (goto-char (point-min))
8056             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8057               (goto-char (point-max))
8058               (widen)
8059               (setq lines (1- (count-lines (point) (point-max))))))
8060           (with-temp-buffer
8061             (insert (format "211 %d Article retrieved.\n"
8062                             (cdr gnus-article-current)))
8063             (insert head)
8064             (if lines (insert (format "Lines: %d\n" lines)))
8065             (insert ".\n")
8066             (let ((nntp-server-buffer (current-buffer)))
8067               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8068         (gnus-data-set-header
8069          (gnus-data-find (cdr gnus-article-current))
8070          header)
8071         (gnus-summary-update-article-line
8072          (cdr gnus-article-current) header)
8073         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8074           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8075    ((not arg)
8076     ;; Select the article the normal way.
8077     (gnus-summary-select-article nil 'force))
8078    (t
8079     ;; We have to require this here to make sure that the following
8080     ;; dynamic binding isn't shadowed by autoloading.
8081     (require 'gnus-async)
8082     (require 'gnus-art)
8083     ;; Bind the article treatment functions to nil.
8084     (let ((gnus-have-all-headers t)
8085           gnus-article-prepare-hook
8086           gnus-article-decode-hook
8087           gnus-break-pages
8088           gnus-show-mime
8089           (gnus-inhibit-treatment t))
8090       (gnus-summary-select-article nil 'force))))
8091   (gnus-summary-goto-subject gnus-current-article)
8092   (gnus-summary-position-point))
8093
8094 (defun gnus-summary-show-raw-article ()
8095   "Show the raw article without any article massaging functions being run."
8096   (interactive)
8097   (gnus-summary-show-article t))
8098
8099 (defun gnus-summary-verbose-headers (&optional arg)
8100   "Toggle permanent full header display.
8101 If ARG is a positive number, turn header display on.
8102 If ARG is a negative number, turn header display off."
8103   (interactive "P")
8104   (setq gnus-show-all-headers
8105         (cond ((or (not (numberp arg))
8106                    (zerop arg))
8107                (not gnus-show-all-headers))
8108               ((natnump arg)
8109                t)))
8110   (gnus-summary-show-article))
8111
8112 (defun gnus-summary-toggle-header (&optional arg)
8113   "Show the headers if they are hidden, or hide them if they are shown.
8114 If ARG is a positive number, show the entire header.
8115 If ARG is a negative number, hide the unwanted header lines."
8116   (interactive "P")
8117   (save-excursion
8118     (set-buffer gnus-article-buffer)
8119     (save-restriction
8120       (let* ((buffer-read-only nil)
8121              (inhibit-point-motion-hooks t)
8122              hidden e)
8123         (setq hidden
8124               (if (numberp arg)
8125                   (>= arg 0)
8126                 (save-restriction
8127                   (article-narrow-to-head)
8128                   (gnus-article-hidden-text-p 'headers))))
8129         (goto-char (point-min))
8130         (when (search-forward "\n\n" nil t)
8131           (delete-region (point-min) (1- (point))))
8132         (goto-char (point-min))
8133         (save-excursion
8134           (set-buffer gnus-original-article-buffer)
8135           (goto-char (point-min))
8136           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8137         (insert-buffer-substring gnus-original-article-buffer 1 e)
8138         (save-restriction
8139           (narrow-to-region (point-min) (point))
8140           (article-decode-encoded-words)
8141           (if  hidden
8142               (let ((gnus-treat-hide-headers nil)
8143                     (gnus-treat-hide-boring-headers nil))
8144                 (setq gnus-article-wash-types
8145                       (delq 'headers gnus-article-wash-types))
8146                 (gnus-treat-article 'head))
8147             (gnus-treat-article 'head)))
8148         (gnus-set-mode-line 'article)))))
8149
8150 (defun gnus-summary-show-all-headers ()
8151   "Make all header lines visible."
8152   (interactive)
8153   (gnus-summary-toggle-header 1))
8154
8155 (defun gnus-summary-toggle-mime (&optional arg)
8156   "Toggle MIME processing.
8157 If ARG is a positive number, turn MIME processing on."
8158   (interactive "P")
8159   (setq gnus-show-mime
8160         (if (null arg)
8161             (not gnus-show-mime)
8162           (> (prefix-numeric-value arg) 0)))
8163   (gnus-summary-select-article t 'force))
8164
8165 (defun gnus-summary-caesar-message (&optional arg)
8166   "Caesar rotate the current article by 13.
8167 The numerical prefix specifies how many places to rotate each letter
8168 forward."
8169   (interactive "P")
8170   (gnus-summary-select-article)
8171   (let ((mail-header-separator ""))
8172     (gnus-eval-in-buffer-window gnus-article-buffer
8173       (save-restriction
8174         (widen)
8175         (let ((start (window-start))
8176               buffer-read-only)
8177           (message-caesar-buffer-body arg)
8178           (set-window-start (get-buffer-window (current-buffer)) start))))))
8179
8180 (defun gnus-summary-stop-page-breaking ()
8181   "Stop page breaking in the current article."
8182   (interactive)
8183   (gnus-summary-select-article)
8184   (gnus-eval-in-buffer-window gnus-article-buffer
8185     (widen)
8186     (when (gnus-visual-p 'page-marker)
8187       (let ((buffer-read-only nil))
8188         (gnus-remove-text-with-property 'gnus-prev)
8189         (gnus-remove-text-with-property 'gnus-next))
8190       (setq gnus-page-broken nil))))
8191
8192 (defun gnus-summary-move-article (&optional n to-newsgroup
8193                                             select-method action)
8194   "Move the current article to a different newsgroup.
8195 If N is a positive number, move the N next articles.
8196 If N is a negative number, move the N previous articles.
8197 If N is nil and any articles have been marked with the process mark,
8198 move those articles instead.
8199 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8200 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8201 re-spool using this method.
8202
8203 For this function to work, both the current newsgroup and the
8204 newsgroup that you want to move to have to support the `request-move'
8205 and `request-accept' functions.
8206
8207 ACTION can be either `move' (the default), `crosspost' or `copy'."
8208   (interactive "P")
8209   (unless action
8210     (setq action 'move))
8211   ;; Check whether the source group supports the required functions.
8212   (cond ((and (eq action 'move)
8213               (not (gnus-check-backend-function
8214                     'request-move-article gnus-newsgroup-name)))
8215          (error "The current group does not support article moving"))
8216         ((and (eq action 'crosspost)
8217               (not (gnus-check-backend-function
8218                     'request-replace-article gnus-newsgroup-name)))
8219          (error "The current group does not support article editing")))
8220   (let ((articles (gnus-summary-work-articles n))
8221         (prefix (if (gnus-check-backend-function
8222                      'request-move-article gnus-newsgroup-name)
8223                     (gnus-group-real-prefix gnus-newsgroup-name)
8224                   ""))
8225         (names '((move "Move" "Moving")
8226                  (copy "Copy" "Copying")
8227                  (crosspost "Crosspost" "Crossposting")))
8228         (copy-buf (save-excursion
8229                     (nnheader-set-temp-buffer " *copy article*")))
8230         (default-marks gnus-article-mark-lists)
8231         (no-expire-marks (delete '(expirable . expire)
8232                                  (copy-sequence gnus-article-mark-lists)))
8233         art-group to-method new-xref article to-groups)
8234     (unless (assq action names)
8235       (error "Unknown action %s" action))
8236     ;; Read the newsgroup name.
8237     (when (and (not to-newsgroup)
8238                (not select-method))
8239       (setq to-newsgroup
8240             (gnus-read-move-group-name
8241              (cadr (assq action names))
8242              (symbol-value (intern (format "gnus-current-%s-group" action)))
8243              articles prefix))
8244       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8245     (setq to-method (or select-method
8246                         (gnus-server-to-method
8247                          (gnus-group-method to-newsgroup))))
8248     ;; Check the method we are to move this article to...
8249     (unless (gnus-check-backend-function
8250              'request-accept-article (car to-method))
8251       (error "%s does not support article copying" (car to-method)))
8252     (unless (gnus-check-server to-method)
8253       (error "Can't open server %s" (car to-method)))
8254     (gnus-message 6 "%s to %s: %s..."
8255                   (caddr (assq action names))
8256                   (or (car select-method) to-newsgroup) articles)
8257     (while articles
8258       (setq article (pop articles))
8259       (setq
8260        art-group
8261        (cond
8262         ;; Move the article.
8263         ((eq action 'move)
8264          ;; Remove this article from future suppression.
8265          (gnus-dup-unsuppress-article article)
8266          (gnus-request-move-article
8267           article                       ; Article to move
8268           gnus-newsgroup-name           ; From newsgroup
8269           (nth 1 (gnus-find-method-for-group
8270                   gnus-newsgroup-name)) ; Server
8271           (list 'gnus-request-accept-article
8272                 to-newsgroup (list 'quote select-method)
8273                 (not articles) t)       ; Accept form
8274           (not articles)))              ; Only save nov last time
8275         ;; Copy the article.
8276         ((eq action 'copy)
8277          (save-excursion
8278            (set-buffer copy-buf)
8279            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8280              (gnus-request-accept-article
8281               to-newsgroup select-method (not articles) t))))
8282         ;; Crosspost the article.
8283         ((eq action 'crosspost)
8284          (let ((xref (message-tokenize-header
8285                       (mail-header-xref (gnus-summary-article-header article))
8286                       " ")))
8287            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8288                                   ":" (number-to-string article)))
8289            (unless xref
8290              (setq xref (list (system-name))))
8291            (setq new-xref
8292                  (concat
8293                   (mapconcat 'identity
8294                              (delete "Xref:" (delete new-xref xref))
8295                              " ")
8296                   " " new-xref))
8297            (save-excursion
8298              (set-buffer copy-buf)
8299              ;; First put the article in the destination group.
8300              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8301              (when (consp (setq art-group
8302                                 (gnus-request-accept-article
8303                                  to-newsgroup select-method (not articles))))
8304                (setq new-xref (concat new-xref " " (car art-group)
8305                                       ":"
8306                                       (number-to-string (cdr art-group))))
8307                ;; Now we have the new Xrefs header, so we insert
8308                ;; it and replace the new article.
8309                (nnheader-replace-header "Xref" new-xref)
8310                (gnus-request-replace-article
8311                 (cdr art-group) to-newsgroup (current-buffer))
8312                art-group))))))
8313       (cond
8314        ((not art-group)
8315         (gnus-message 1 "Couldn't %s article %s: %s"
8316                       (cadr (assq action names)) article
8317                       (nnheader-get-report (car to-method))))
8318        ((eq art-group 'junk)
8319         (when (eq action 'move)
8320           (gnus-summary-mark-article article gnus-canceled-mark)
8321           (gnus-message 4 "Deleted article %s" article)))
8322        (t
8323         (let* ((pto-group (gnus-group-prefixed-name
8324                            (car art-group) to-method))
8325                (entry
8326                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8327                (info (nth 2 entry))
8328                (to-group (gnus-info-group info))
8329                to-marks)
8330           ;; Update the group that has been moved to.
8331           (when (and info
8332                      (memq action '(move copy)))
8333             (unless (member to-group to-groups)
8334               (push to-group to-groups))
8335
8336             (unless (memq article gnus-newsgroup-unreads)
8337               (push 'read to-marks)
8338               (gnus-info-set-read
8339                info (gnus-add-to-range (gnus-info-read info)
8340                                        (list (cdr art-group)))))
8341
8342             ;; See whether the article is to be put in the cache.
8343             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8344                              default-marks
8345                            no-expire-marks))
8346                   (to-article (cdr art-group)))
8347
8348               ;; Enter the article into the cache in the new group,
8349               ;; if that is required.
8350               (when gnus-use-cache
8351                 (gnus-cache-possibly-enter-article
8352                  to-group to-article
8353                  (let ((header (copy-sequence
8354                                 (gnus-summary-article-header article))))
8355                    (mail-header-set-number header to-article)
8356                    header)
8357                  (memq article gnus-newsgroup-marked)
8358                  (memq article gnus-newsgroup-dormant)
8359                  (memq article gnus-newsgroup-unreads)))
8360
8361               (when gnus-preserve-marks
8362                 ;; Copy any marks over to the new group.
8363                 (when (and (equal to-group gnus-newsgroup-name)
8364                            (not (memq article gnus-newsgroup-unreads)))
8365                   ;; Mark this article as read in this group.
8366                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8367                   (setcdr (gnus-active to-group) to-article)
8368                   (setcdr gnus-newsgroup-active to-article))
8369
8370                 (while marks
8371                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8372                     (when (memq article (symbol-value
8373                                          (intern (format "gnus-newsgroup-%s"
8374                                                          (caar marks)))))
8375                       (push (cdar marks) to-marks)
8376                       ;; If the other group is the same as this group,
8377                       ;; then we have to add the mark to the list.
8378                       (when (equal to-group gnus-newsgroup-name)
8379                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8380                              (cons to-article
8381                                    (symbol-value
8382                                     (intern (format "gnus-newsgroup-%s"
8383                                                     (caar marks)))))))
8384                       ;; Copy the marks to other group.
8385                       (gnus-add-marked-articles
8386                        to-group (cdar marks) (list to-article) info)))
8387                   (setq marks (cdr marks)))
8388
8389                 (gnus-request-set-mark to-group (list (list (list to-article)
8390                                                             'add
8391                                                             to-marks))))
8392
8393               (gnus-dribble-enter
8394                (concat "(gnus-group-set-info '"
8395                        (gnus-prin1-to-string (gnus-get-info to-group))
8396                        ")"))))
8397
8398           ;; Update the Xref header in this article to point to
8399           ;; the new crossposted article we have just created.
8400           (when (eq action 'crosspost)
8401             (save-excursion
8402               (set-buffer copy-buf)
8403               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8404               (nnheader-replace-header "Xref" new-xref)
8405               (gnus-request-replace-article
8406                article gnus-newsgroup-name (current-buffer)))))
8407
8408         ;;;!!!Why is this necessary?
8409         (set-buffer gnus-summary-buffer)
8410
8411         (gnus-summary-goto-subject article)
8412         (when (eq action 'move)
8413           (gnus-summary-mark-article article gnus-canceled-mark))))
8414       (gnus-summary-remove-process-mark article))
8415     ;; Re-activate all groups that have been moved to.
8416     (save-excursion
8417       (set-buffer gnus-group-buffer)
8418       (let ((gnus-group-marked to-groups))
8419         (gnus-group-get-new-news-this-group nil t)))
8420
8421     (gnus-kill-buffer copy-buf)
8422     (gnus-summary-position-point)
8423     (gnus-set-mode-line 'summary)))
8424
8425 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8426   "Move the current article to a different newsgroup.
8427 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8428 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8429 re-spool using this method."
8430   (interactive "P")
8431   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8432
8433 (defun gnus-summary-crosspost-article (&optional n)
8434   "Crosspost the current article to some other group."
8435   (interactive "P")
8436   (gnus-summary-move-article n nil nil 'crosspost))
8437
8438 (defcustom gnus-summary-respool-default-method nil
8439   "Default method type for respooling an article.
8440 If nil, use to the current newsgroup method."
8441   :type 'symbol
8442   :group 'gnus-summary-mail)
8443
8444 (defun gnus-summary-respool-article (&optional n method)
8445   "Respool the current article.
8446 The article will be squeezed through the mail spooling process again,
8447 which means that it will be put in some mail newsgroup or other
8448 depending on `nnmail-split-methods'.
8449 If N is a positive number, respool the N next articles.
8450 If N is a negative number, respool the N previous articles.
8451 If N is nil and any articles have been marked with the process mark,
8452 respool those articles instead.
8453
8454 Respooling can be done both from mail groups and \"real\" newsgroups.
8455 In the former case, the articles in question will be moved from the
8456 current group into whatever groups they are destined to.  In the
8457 latter case, they will be copied into the relevant groups."
8458   (interactive
8459    (list current-prefix-arg
8460          (let* ((methods (gnus-methods-using 'respool))
8461                 (methname
8462                  (symbol-name (or gnus-summary-respool-default-method
8463                                   (car (gnus-find-method-for-group
8464                                         gnus-newsgroup-name)))))
8465                 (method
8466                  (gnus-completing-read
8467                   methname "What backend do you want to use when respooling?"
8468                   methods nil t nil 'gnus-mail-method-history))
8469                 ms)
8470            (cond
8471             ((zerop (length (setq ms (gnus-servers-using-backend
8472                                       (intern method)))))
8473              (list (intern method) ""))
8474             ((= 1 (length ms))
8475              (car ms))
8476             (t
8477              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8478                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8479                            ms-alist))))))))
8480   (unless method
8481     (error "No method given for respooling"))
8482   (if (assoc (symbol-name
8483               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8484              (gnus-methods-using 'respool))
8485       (gnus-summary-move-article n nil method)
8486     (gnus-summary-copy-article n nil method)))
8487
8488 (defun gnus-summary-import-article (file &optional edit)
8489   "Import an arbitrary file into a mail newsgroup."
8490   (interactive "fImport file: \nP")
8491   (let ((group gnus-newsgroup-name)
8492         (now (current-time))
8493         atts lines group-art)
8494     (unless (gnus-check-backend-function 'request-accept-article group)
8495       (error "%s does not support article importing" group))
8496     (or (file-readable-p file)
8497         (not (file-regular-p file))
8498         (error "Can't read %s" file))
8499     (save-excursion
8500       (set-buffer (gnus-get-buffer-create " *import file*"))
8501       (erase-buffer)
8502       (nnheader-insert-file-contents file)
8503       (goto-char (point-min))
8504       (if (nnheader-article-p)
8505           (save-restriction
8506             (goto-char (point-min))
8507             (search-forward "\n\n" nil t)
8508             (narrow-to-region (point-min) (1- (point)))
8509             (goto-char (point-min))
8510             (unless (re-search-forward "^date:" nil t)
8511               (goto-char (point-max))
8512               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8513         ;; This doesn't look like an article, so we fudge some headers.
8514         (setq atts (file-attributes file)
8515               lines (count-lines (point-min) (point-max)))
8516         (insert "From: " (read-string "From: ") "\n"
8517                 "Subject: " (read-string "Subject: ") "\n"
8518                 "Date: " (message-make-date (nth 5 atts)) "\n"
8519                 "Message-ID: " (message-make-message-id) "\n"
8520                 "Lines: " (int-to-string lines) "\n"
8521                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8522       (setq group-art (gnus-request-accept-article group nil t))
8523       (kill-buffer (current-buffer)))
8524     (setq gnus-newsgroup-active (gnus-activate-group group))
8525     (forward-line 1)
8526     (gnus-summary-goto-article (cdr group-art) nil t)
8527     (when edit
8528       (gnus-summary-edit-article))))
8529
8530 (defun gnus-summary-create-article ()
8531   "Create an article in a mail newsgroup."
8532   (interactive)
8533   (let ((group gnus-newsgroup-name)
8534         (now (current-time))
8535         group-art)
8536     (unless (gnus-check-backend-function 'request-accept-article group)
8537       (error "%s does not support article importing" group))
8538     (save-excursion
8539       (set-buffer (gnus-get-buffer-create " *import file*"))
8540       (erase-buffer)
8541       (goto-char (point-min))
8542       ;; This doesn't look like an article, so we fudge some headers.
8543       (insert "From: " (read-string "From: ") "\n"
8544               "Subject: " (read-string "Subject: ") "\n"
8545               "Date: " (message-make-date now) "\n"
8546               "Message-ID: " (message-make-message-id) "\n")
8547       (setq group-art (gnus-request-accept-article group nil t))
8548       (kill-buffer (current-buffer)))
8549     (setq gnus-newsgroup-active (gnus-activate-group group))
8550     (forward-line 1)
8551     (gnus-summary-goto-article (cdr group-art) nil t)
8552     (gnus-summary-edit-article)))
8553
8554 (defun gnus-summary-article-posted-p ()
8555   "Say whether the current (mail) article is available from news as well.
8556 This will be the case if the article has both been mailed and posted."
8557   (interactive)
8558   (let ((id (mail-header-references (gnus-summary-article-header)))
8559         (gnus-override-method (car (gnus-refer-article-methods))))
8560     (if (gnus-request-head id "")
8561         (gnus-message 2 "The current message was found on %s"
8562                       gnus-override-method)
8563       (gnus-message 2 "The current message couldn't be found on %s"
8564                     gnus-override-method)
8565       nil)))
8566
8567 (defun gnus-summary-expire-articles (&optional now)
8568   "Expire all articles that are marked as expirable in the current group."
8569   (interactive)
8570   (when (gnus-check-backend-function
8571          'request-expire-articles gnus-newsgroup-name)
8572     ;; This backend supports expiry.
8573     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8574            (expirable (if total
8575                           (progn
8576                             ;; We need to update the info for
8577                             ;; this group for `gnus-list-of-read-articles'
8578                             ;; to give us the right answer.
8579                             (gnus-run-hooks 'gnus-exit-group-hook)
8580                             (gnus-summary-update-info)
8581                             (gnus-list-of-read-articles gnus-newsgroup-name))
8582                         (setq gnus-newsgroup-expirable
8583                               (sort gnus-newsgroup-expirable '<))))
8584            (expiry-wait (if now 'immediate
8585                           (gnus-group-find-parameter
8586                            gnus-newsgroup-name 'expiry-wait)))
8587            (nnmail-expiry-target
8588             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8589                 nnmail-expiry-target))
8590            es)
8591       (when expirable
8592         ;; There are expirable articles in this group, so we run them
8593         ;; through the expiry process.
8594         (gnus-message 6 "Expiring articles...")
8595         (unless (gnus-check-group gnus-newsgroup-name)
8596           (error "Can't open server for %s" gnus-newsgroup-name))
8597         ;; The list of articles that weren't expired is returned.
8598         (save-excursion
8599           (if expiry-wait
8600               (let ((nnmail-expiry-wait-function nil)
8601                     (nnmail-expiry-wait expiry-wait))
8602                 (setq es (gnus-request-expire-articles
8603                           expirable gnus-newsgroup-name)))
8604             (setq es (gnus-request-expire-articles
8605                       expirable gnus-newsgroup-name)))
8606           (unless total
8607             (setq gnus-newsgroup-expirable es))
8608           ;; We go through the old list of expirable, and mark all
8609           ;; really expired articles as nonexistent.
8610           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8611             (let ((gnus-use-cache nil))
8612               (while expirable
8613                 (unless (memq (car expirable) es)
8614                   (when (gnus-data-find (car expirable))
8615                     (gnus-summary-mark-article
8616                      (car expirable) gnus-canceled-mark)))
8617                 (setq expirable (cdr expirable))))))
8618         (gnus-message 6 "Expiring articles...done")))))
8619
8620 (defun gnus-summary-expire-articles-now ()
8621   "Expunge all expirable articles in the current group.
8622 This means that *all* articles that are marked as expirable will be
8623 deleted forever, right now."
8624   (interactive)
8625   (or gnus-expert-user
8626       (gnus-yes-or-no-p
8627        "Are you really, really, really sure you want to delete all these messages? ")
8628       (error "Phew!"))
8629   (gnus-summary-expire-articles t))
8630
8631 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8632 (defun gnus-summary-delete-article (&optional n)
8633   "Delete the N next (mail) articles.
8634 This command actually deletes articles.  This is not a marking
8635 command.  The article will disappear forever from your life, never to
8636 return.
8637 If N is negative, delete backwards.
8638 If N is nil and articles have been marked with the process mark,
8639 delete these instead."
8640   (interactive "P")
8641   (unless (gnus-check-backend-function 'request-expire-articles
8642                                        gnus-newsgroup-name)
8643     (error "The current newsgroup does not support article deletion"))
8644   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8645     (error "Couldn't open server"))
8646   ;; Compute the list of articles to delete.
8647   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8648         not-deleted)
8649     (if (and gnus-novice-user
8650              (not (gnus-yes-or-no-p
8651                    (format "Do you really want to delete %s forever? "
8652                            (if (> (length articles) 1)
8653                                (format "these %s articles" (length articles))
8654                              "this article")))))
8655         ()
8656       ;; Delete the articles.
8657       (setq not-deleted (gnus-request-expire-articles
8658                          articles gnus-newsgroup-name 'force))
8659       (while articles
8660         (gnus-summary-remove-process-mark (car articles))
8661         ;; The backend might not have been able to delete the article
8662         ;; after all.
8663         (unless (memq (car articles) not-deleted)
8664           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8665         (setq articles (cdr articles)))
8666       (when not-deleted
8667         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8668     (gnus-summary-position-point)
8669     (gnus-set-mode-line 'summary)
8670     not-deleted))
8671
8672 (defun gnus-summary-edit-article (&optional force)
8673   "Edit the current article.
8674 This will have permanent effect only in mail groups.
8675 If FORCE is non-nil, allow editing of articles even in read-only
8676 groups."
8677   (interactive "P")
8678   (save-excursion
8679     (set-buffer gnus-summary-buffer)
8680     (let ((mail-parse-charset gnus-newsgroup-charset)
8681           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8682       (gnus-set-global-variables)
8683       (when (and (not force)
8684                  (gnus-group-read-only-p))
8685         (error "The current newsgroup does not support article editing"))
8686       (gnus-summary-show-article t)
8687       (gnus-article-edit-article
8688        'ignore
8689        `(lambda (no-highlight)
8690           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8691                 (message-options message-options)
8692                 (message-options-set-recipient)
8693                 (mail-parse-ignored-charsets
8694                  ',gnus-newsgroup-ignored-charsets))
8695             (gnus-summary-edit-article-done
8696              ,(or (mail-header-references gnus-current-headers) "")
8697              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8698
8699 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8700
8701 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8702                                                  no-highlight)
8703   "Make edits to the current article permanent."
8704   (interactive)
8705   (save-excursion
8706     ;; The buffer restriction contains the entire article if it exists.
8707     (when (article-goto-body)
8708       (let ((lines (count-lines (point) (point-max)))
8709             (length (- (point-max) (point)))
8710             (case-fold-search t)
8711             (body (copy-marker (point))))
8712         (goto-char (point-min))
8713         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8714           (delete-region (match-beginning 1) (match-end 1))
8715           (insert (number-to-string length)))
8716         (goto-char (point-min))
8717         (when (re-search-forward
8718                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8719           (delete-region (match-beginning 1) (match-end 1))
8720           (insert (number-to-string length)))
8721         (goto-char (point-min))
8722         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8723           (delete-region (match-beginning 1) (match-end 1))
8724           (insert (number-to-string lines))))))
8725   ;; Replace the article.
8726   (let ((buf (current-buffer)))
8727     (with-temp-buffer
8728       (insert-buffer-substring buf)
8729
8730       (if (and (not read-only)
8731                (not (gnus-request-replace-article
8732                      (cdr gnus-article-current) (car gnus-article-current)
8733                      (current-buffer) t)))
8734           (error "Couldn't replace article")
8735         ;; Update the summary buffer.
8736         (if (and references
8737                  (equal (message-tokenize-header references " ")
8738                         (message-tokenize-header
8739                          (or (message-fetch-field "references") "") " ")))
8740             ;; We only have to update this line.
8741             (save-excursion
8742               (save-restriction
8743                 (message-narrow-to-head)
8744                 (let ((head (buffer-string))
8745                       header)
8746                   (with-temp-buffer
8747                     (insert (format "211 %d Article retrieved.\n"
8748                                     (cdr gnus-article-current)))
8749                     (insert head)
8750                     (insert ".\n")
8751                     (let ((nntp-server-buffer (current-buffer)))
8752                       (setq header (car (gnus-get-newsgroup-headers
8753                                          (save-excursion
8754                                            (set-buffer gnus-summary-buffer)
8755                                            gnus-newsgroup-dependencies)
8756                                          t))))
8757                     (save-excursion
8758                       (set-buffer gnus-summary-buffer)
8759                       (gnus-data-set-header
8760                        (gnus-data-find (cdr gnus-article-current))
8761                        header)
8762                       (gnus-summary-update-article-line
8763                        (cdr gnus-article-current) header)
8764                       (if (gnus-summary-goto-subject
8765                            (cdr gnus-article-current) nil t)
8766                           (gnus-summary-update-secondary-mark
8767                            (cdr gnus-article-current))))))))
8768           ;; Update threads.
8769           (set-buffer (or buffer gnus-summary-buffer))
8770           (gnus-summary-update-article (cdr gnus-article-current))
8771           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8772               (gnus-summary-update-secondary-mark
8773                (cdr gnus-article-current))))
8774         ;; Prettify the article buffer again.
8775         (unless no-highlight
8776           (save-excursion
8777             (set-buffer gnus-article-buffer)
8778             ;;;!!! Fix this -- article should be rehighlighted.
8779             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8780             (set-buffer gnus-original-article-buffer)
8781             (gnus-request-article
8782              (cdr gnus-article-current)
8783              (car gnus-article-current) (current-buffer))))
8784         ;; Prettify the summary buffer line.
8785         (when (gnus-visual-p 'summary-highlight 'highlight)
8786           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8787
8788 (defun gnus-summary-edit-wash (key)
8789   "Perform editing command KEY in the article buffer."
8790   (interactive
8791    (list
8792     (progn
8793       (message "%s" (concat (this-command-keys) "- "))
8794       (read-char))))
8795   (message "")
8796   (gnus-summary-edit-article)
8797   (execute-kbd-macro (concat (this-command-keys) key))
8798   (gnus-article-edit-done))
8799
8800 (defun gnus-summary-toggle-smiley (&optional arg)
8801   "Toggle the display of smilies as small graphical icons."
8802   (interactive "P")
8803   (save-excursion
8804     (set-buffer gnus-article-buffer)
8805     (gnus-smiley-display arg)))
8806
8807 ;;; Respooling
8808
8809 (defun gnus-summary-respool-query (&optional silent trace)
8810   "Query where the respool algorithm would put this article."
8811   (interactive)
8812   (let (gnus-mark-article-hook)
8813     (gnus-summary-select-article)
8814     (save-excursion
8815       (set-buffer gnus-original-article-buffer)
8816       (save-restriction
8817         (message-narrow-to-head)
8818         (let ((groups (nnmail-article-group 'identity trace)))
8819           (unless silent
8820             (if groups
8821                 (message "This message would go to %s"
8822                          (mapconcat 'car groups ", "))
8823               (message "This message would go to no groups"))
8824             groups))))))
8825
8826 (defun gnus-summary-respool-trace ()
8827   "Trace where the respool algorithm would put this article.
8828 Display a buffer showing all fancy splitting patterns which matched."
8829   (interactive)
8830   (gnus-summary-respool-query nil t))
8831
8832 ;; Summary marking commands.
8833
8834 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8835   "Mark articles which has the same subject as read, and then select the next.
8836 If UNMARK is positive, remove any kind of mark.
8837 If UNMARK is negative, tick articles."
8838   (interactive "P")
8839   (when unmark
8840     (setq unmark (prefix-numeric-value unmark)))
8841   (let ((count
8842          (gnus-summary-mark-same-subject
8843           (gnus-summary-article-subject) unmark)))
8844     ;; Select next unread article.  If auto-select-same mode, should
8845     ;; select the first unread article.
8846     (gnus-summary-next-article t (and gnus-auto-select-same
8847                                       (gnus-summary-article-subject)))
8848     (gnus-message 7 "%d article%s marked as %s"
8849                   count (if (= count 1) " is" "s are")
8850                   (if unmark "unread" "read"))))
8851
8852 (defun gnus-summary-kill-same-subject (&optional unmark)
8853   "Mark articles which has the same subject as read.
8854 If UNMARK is positive, remove any kind of mark.
8855 If UNMARK is negative, tick articles."
8856   (interactive "P")
8857   (when unmark
8858     (setq unmark (prefix-numeric-value unmark)))
8859   (let ((count
8860          (gnus-summary-mark-same-subject
8861           (gnus-summary-article-subject) unmark)))
8862     ;; If marked as read, go to next unread subject.
8863     (when (null unmark)
8864       ;; Go to next unread subject.
8865       (gnus-summary-next-subject 1 t))
8866     (gnus-message 7 "%d articles are marked as %s"
8867                   count (if unmark "unread" "read"))))
8868
8869 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8870   "Mark articles with same SUBJECT as read, and return marked number.
8871 If optional argument UNMARK is positive, remove any kinds of marks.
8872 If optional argument UNMARK is negative, mark articles as unread instead."
8873   (let ((count 1))
8874     (save-excursion
8875       (cond
8876        ((null unmark)                   ; Mark as read.
8877         (while (and
8878                 (progn
8879                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8880                   (gnus-summary-show-thread) t)
8881                 (gnus-summary-find-subject subject))
8882           (setq count (1+ count))))
8883        ((> unmark 0)                    ; Tick.
8884         (while (and
8885                 (progn
8886                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8887                   (gnus-summary-show-thread) t)
8888                 (gnus-summary-find-subject subject))
8889           (setq count (1+ count))))
8890        (t                               ; Mark as unread.
8891         (while (and
8892                 (progn
8893                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8894                   (gnus-summary-show-thread) t)
8895                 (gnus-summary-find-subject subject))
8896           (setq count (1+ count)))))
8897       (gnus-set-mode-line 'summary)
8898       ;; Return the number of marked articles.
8899       count)))
8900
8901 (defun gnus-summary-mark-as-processable (n &optional unmark)
8902   "Set the process mark on the next N articles.
8903 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8904 the process mark instead.  The difference between N and the actual
8905 number of articles marked is returned."
8906   (interactive "P")
8907   (if (and (null n) (gnus-region-active-p))
8908       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8909     (setq n (prefix-numeric-value n))
8910     (let ((backward (< n 0))
8911           (n (abs n)))
8912       (while (and
8913               (> n 0)
8914               (if unmark
8915                   (gnus-summary-remove-process-mark
8916                    (gnus-summary-article-number))
8917                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8918               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8919         (setq n (1- n)))
8920       (when (/= 0 n)
8921         (gnus-message 7 "No more articles"))
8922       (gnus-summary-recenter)
8923       (gnus-summary-position-point)
8924       n)))
8925
8926 (defun gnus-summary-unmark-as-processable (n)
8927   "Remove the process mark from the next N articles.
8928 If N is negative, unmark backward instead.  The difference between N and
8929 the actual number of articles unmarked is returned."
8930   (interactive "P")
8931   (gnus-summary-mark-as-processable n t))
8932
8933 (defun gnus-summary-unmark-all-processable ()
8934   "Remove the process mark from all articles."
8935   (interactive)
8936   (save-excursion
8937     (while gnus-newsgroup-processable
8938       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8939   (gnus-summary-position-point))
8940
8941 (defun gnus-summary-add-mark (article type)
8942   "Mark ARTICLE with a mark of TYPE."
8943   (let ((vtype (car (assq type gnus-article-mark-lists)))
8944         var)
8945     (if (not vtype)
8946         (error "No such mark type: %s" type)
8947       (setq var (intern (format "gnus-newsgroup-%s" type)))
8948       (set var (cons article (symbol-value var)))
8949       (if (memq type '(processable cached replied forwarded recent saved))
8950           (gnus-summary-update-secondary-mark article)
8951         ;;; !!! This is bogus.  We should find out what primary
8952         ;;; !!! mark we want to set.
8953         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8954
8955 (defun gnus-summary-mark-as-expirable (n)
8956   "Mark N articles forward as expirable.
8957 If N is negative, mark backward instead.  The difference between N and
8958 the actual number of articles marked is returned."
8959   (interactive "p")
8960   (gnus-summary-mark-forward n gnus-expirable-mark))
8961
8962 (defun gnus-summary-mark-article-as-replied (article)
8963   "Mark ARTICLE as replied to and update the summary line.
8964 ARTICLE can also be a list of articles."
8965   (interactive (list (gnus-summary-article-number)))
8966   (let ((articles (if (listp article) article (list article))))
8967     (dolist (article articles)
8968       (push article gnus-newsgroup-replied)
8969       (let ((buffer-read-only nil))
8970         (when (gnus-summary-goto-subject article nil t)
8971           (gnus-summary-update-secondary-mark article))))))
8972
8973 (defun gnus-summary-mark-article-as-forwarded (article)
8974   "Mark ARTICLE as forwarded and update the summary line.
8975 ARTICLE can also be a list of articles."
8976   (let ((articles (if (listp article) article (list article))))
8977     (dolist (article articles)
8978       (push article gnus-newsgroup-forwarded)
8979       (let ((buffer-read-only nil))
8980         (when (gnus-summary-goto-subject article nil t)
8981           (gnus-summary-update-secondary-mark article))))))
8982
8983 (defun gnus-summary-set-bookmark (article)
8984   "Set a bookmark in current article."
8985   (interactive (list (gnus-summary-article-number)))
8986   (when (or (not (get-buffer gnus-article-buffer))
8987             (not gnus-current-article)
8988             (not gnus-article-current)
8989             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8990     (error "No current article selected"))
8991   ;; Remove old bookmark, if one exists.
8992   (let ((old (assq article gnus-newsgroup-bookmarks)))
8993     (when old
8994       (setq gnus-newsgroup-bookmarks
8995             (delq old gnus-newsgroup-bookmarks))))
8996   ;; Set the new bookmark, which is on the form
8997   ;; (article-number . line-number-in-body).
8998   (push
8999    (cons article
9000          (save-excursion
9001            (set-buffer gnus-article-buffer)
9002            (count-lines
9003             (min (point)
9004                  (save-excursion
9005                    (goto-char (point-min))
9006                    (search-forward "\n\n" nil t)
9007                    (point)))
9008             (point))))
9009    gnus-newsgroup-bookmarks)
9010   (gnus-message 6 "A bookmark has been added to the current article."))
9011
9012 (defun gnus-summary-remove-bookmark (article)
9013   "Remove the bookmark from the current article."
9014   (interactive (list (gnus-summary-article-number)))
9015   ;; Remove old bookmark, if one exists.
9016   (let ((old (assq article gnus-newsgroup-bookmarks)))
9017     (if old
9018         (progn
9019           (setq gnus-newsgroup-bookmarks
9020                 (delq old gnus-newsgroup-bookmarks))
9021           (gnus-message 6 "Removed bookmark."))
9022       (gnus-message 6 "No bookmark in current article."))))
9023
9024 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9025 (defun gnus-summary-mark-as-dormant (n)
9026   "Mark N articles forward as dormant.
9027 If N is negative, mark backward instead.  The difference between N and
9028 the actual number of articles marked is returned."
9029   (interactive "p")
9030   (gnus-summary-mark-forward n gnus-dormant-mark))
9031
9032 (defun gnus-summary-set-process-mark (article)
9033   "Set the process mark on ARTICLE and update the summary line."
9034   (setq gnus-newsgroup-processable
9035         (cons article
9036               (delq article gnus-newsgroup-processable)))
9037   (when (gnus-summary-goto-subject article)
9038     (gnus-summary-show-thread)
9039     (gnus-summary-goto-subject article)
9040     (gnus-summary-update-secondary-mark article)))
9041
9042 (defun gnus-summary-remove-process-mark (article)
9043   "Remove the process mark from ARTICLE and update the summary line."
9044   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9045   (when (gnus-summary-goto-subject article)
9046     (gnus-summary-show-thread)
9047     (gnus-summary-goto-subject article)
9048     (gnus-summary-update-secondary-mark article)))
9049
9050 (defun gnus-summary-set-saved-mark (article)
9051   "Set the process mark on ARTICLE and update the summary line."
9052   (push article gnus-newsgroup-saved)
9053   (when (gnus-summary-goto-subject article)
9054     (gnus-summary-update-secondary-mark article)))
9055
9056 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9057   "Mark N articles as read forwards.
9058 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9059 The difference between N and the actual number of articles marked is
9060 returned.
9061 Iff NO-EXPIRE, auto-expiry will be inhibited."
9062   (interactive "p")
9063   (gnus-summary-show-thread)
9064   (let ((backward (< n 0))
9065         (gnus-summary-goto-unread
9066          (and gnus-summary-goto-unread
9067               (not (eq gnus-summary-goto-unread 'never))
9068               (not (memq mark (list gnus-unread-mark
9069                                     gnus-ticked-mark gnus-dormant-mark)))))
9070         (n (abs n))
9071         (mark (or mark gnus-del-mark)))
9072     (while (and (> n 0)
9073                 (gnus-summary-mark-article nil mark no-expire)
9074                 (zerop (gnus-summary-next-subject
9075                         (if backward -1 1)
9076                         (and gnus-summary-goto-unread
9077                              (not (eq gnus-summary-goto-unread 'never)))
9078                         t)))
9079       (setq n (1- n)))
9080     (when (/= 0 n)
9081       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9082     (gnus-summary-recenter)
9083     (gnus-summary-position-point)
9084     (gnus-set-mode-line 'summary)
9085     n))
9086
9087 (defun gnus-summary-mark-article-as-read (mark)
9088   "Mark the current article quickly as read with MARK."
9089   (let ((article (gnus-summary-article-number)))
9090     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9091     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9092     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9093     (push (cons article mark) gnus-newsgroup-reads)
9094     ;; Possibly remove from cache, if that is used.
9095     (when gnus-use-cache
9096       (gnus-cache-enter-remove-article article))
9097     ;; Allow the backend to change the mark.
9098     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9099     ;; Check for auto-expiry.
9100     (when (and gnus-newsgroup-auto-expire
9101                (memq mark gnus-auto-expirable-marks))
9102       (setq mark gnus-expirable-mark)
9103       ;; Let the backend know about the mark change.
9104       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9105       (push article gnus-newsgroup-expirable))
9106     ;; Set the mark in the buffer.
9107     (gnus-summary-update-mark mark 'unread)
9108     t))
9109
9110 (defun gnus-summary-mark-article-as-unread (mark)
9111   "Mark the current article quickly as unread with MARK."
9112   (let* ((article (gnus-summary-article-number))
9113          (old-mark (gnus-summary-article-mark article)))
9114     ;; Allow the backend to change the mark.
9115     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9116     (if (eq mark old-mark)
9117         t
9118       (if (<= article 0)
9119           (progn
9120             (gnus-error 1 "Can't mark negative article numbers")
9121             nil)
9122         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9123         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9124         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9125         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9126         (cond ((= mark gnus-ticked-mark)
9127                (push article gnus-newsgroup-marked))
9128               ((= mark gnus-dormant-mark)
9129                (push article gnus-newsgroup-dormant))
9130               (t
9131                (push article gnus-newsgroup-unreads)))
9132         (gnus-pull article gnus-newsgroup-reads)
9133
9134         ;; See whether the article is to be put in the cache.
9135         (and gnus-use-cache
9136              (vectorp (gnus-summary-article-header article))
9137              (save-excursion
9138                (gnus-cache-possibly-enter-article
9139                 gnus-newsgroup-name article
9140                 (gnus-summary-article-header article)
9141                 (= mark gnus-ticked-mark)
9142                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9143
9144         ;; Fix the mark.
9145         (gnus-summary-update-mark mark 'unread)
9146         t))))
9147
9148 (defun gnus-summary-mark-article (&optional article mark no-expire)
9149   "Mark ARTICLE with MARK.  MARK can be any character.
9150 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9151 `??' (dormant) and `?E' (expirable).
9152 If MARK is nil, then the default character `?r' is used.
9153 If ARTICLE is nil, then the article on the current line will be
9154 marked.
9155 Iff NO-EXPIRE, auto-expiry will be inhibited."
9156   ;; The mark might be a string.
9157   (when (stringp mark)
9158     (setq mark (aref mark 0)))
9159   ;; If no mark is given, then we check auto-expiring.
9160   (when (null mark)
9161     (setq mark gnus-del-mark))
9162   (when (and (not no-expire)
9163              gnus-newsgroup-auto-expire
9164              (memq mark gnus-auto-expirable-marks))
9165     (setq mark gnus-expirable-mark))
9166   (let ((article (or article (gnus-summary-article-number)))
9167         (old-mark (gnus-summary-article-mark article)))
9168     ;; Allow the backend to change the mark.
9169     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9170     (if (eq mark old-mark)
9171         t
9172       (unless article
9173         (error "No article on current line"))
9174       (if (not (if (or (= mark gnus-unread-mark)
9175                        (= mark gnus-ticked-mark)
9176                        (= mark gnus-dormant-mark))
9177                    (gnus-mark-article-as-unread article mark)
9178                  (gnus-mark-article-as-read article mark)))
9179           t
9180         ;; See whether the article is to be put in the cache.
9181         (and gnus-use-cache
9182              (not (= mark gnus-canceled-mark))
9183              (vectorp (gnus-summary-article-header article))
9184              (save-excursion
9185                (gnus-cache-possibly-enter-article
9186                 gnus-newsgroup-name article
9187                 (gnus-summary-article-header article)
9188                 (= mark gnus-ticked-mark)
9189                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9190
9191         (when (gnus-summary-goto-subject article nil t)
9192           (let ((buffer-read-only nil))
9193             (gnus-summary-show-thread)
9194             ;; Fix the mark.
9195             (gnus-summary-update-mark mark 'unread)
9196             t))))))
9197
9198 (defun gnus-summary-update-secondary-mark (article)
9199   "Update the secondary (read, process, cache) mark."
9200   (gnus-summary-update-mark
9201    (cond ((memq article gnus-newsgroup-processable)
9202           gnus-process-mark)
9203          ((memq article gnus-newsgroup-cached)
9204           gnus-cached-mark)
9205          ((memq article gnus-newsgroup-replied)
9206           gnus-replied-mark)
9207          ((memq article gnus-newsgroup-forwarded)
9208           gnus-forwarded-mark)
9209          ((memq article gnus-newsgroup-saved)
9210           gnus-saved-mark)
9211          ((memq article gnus-newsgroup-recent)
9212           gnus-recent-mark)
9213          ((memq article gnus-newsgroup-unseen)
9214           gnus-unseen-mark)
9215          (t gnus-no-mark))
9216    'replied)
9217   (when (gnus-visual-p 'summary-highlight 'highlight)
9218     (gnus-run-hooks 'gnus-summary-update-hook))
9219   t)
9220
9221 (defun gnus-summary-update-mark (mark type)
9222   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9223         (buffer-read-only nil))
9224     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9225     (when forward
9226       (when (looking-at "\r")
9227         (incf forward))
9228       (when (<= (+ forward (point)) (point-max))
9229         ;; Go to the right position on the line.
9230         (goto-char (+ forward (point)))
9231         ;; Replace the old mark with the new mark.
9232         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9233         ;; Optionally update the marks by some user rule.
9234         (when (eq type 'unread)
9235           (gnus-data-set-mark
9236            (gnus-data-find (gnus-summary-article-number)) mark)
9237           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9238
9239 (defun gnus-mark-article-as-read (article &optional mark)
9240   "Enter ARTICLE in the pertinent lists and remove it from others."
9241   ;; Make the article expirable.
9242   (let ((mark (or mark gnus-del-mark)))
9243     (if (= mark gnus-expirable-mark)
9244         (push article gnus-newsgroup-expirable)
9245       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9246     ;; Remove from unread and marked lists.
9247     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9248     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9249     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9250     (push (cons article mark) gnus-newsgroup-reads)
9251     ;; Possibly remove from cache, if that is used.
9252     (when gnus-use-cache
9253       (gnus-cache-enter-remove-article article))
9254     t))
9255
9256 (defun gnus-mark-article-as-unread (article &optional mark)
9257   "Enter ARTICLE in the pertinent lists and remove it from others."
9258   (let ((mark (or mark gnus-ticked-mark)))
9259     (if (<= article 0)
9260         (progn
9261           (gnus-error 1 "Can't mark negative article numbers")
9262           nil)
9263       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9264             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9265             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9266             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9267
9268       ;; Unsuppress duplicates?
9269       (when gnus-suppress-duplicates
9270         (gnus-dup-unsuppress-article article))
9271
9272       (cond ((= mark gnus-ticked-mark)
9273              (push article gnus-newsgroup-marked))
9274             ((= mark gnus-dormant-mark)
9275              (push article gnus-newsgroup-dormant))
9276             (t
9277              (push article gnus-newsgroup-unreads)))
9278       (gnus-pull article gnus-newsgroup-reads)
9279       t)))
9280
9281 (defalias 'gnus-summary-mark-as-unread-forward
9282   'gnus-summary-tick-article-forward)
9283 (make-obsolete 'gnus-summary-mark-as-unread-forward
9284                'gnus-summary-tick-article-forward)
9285 (defun gnus-summary-tick-article-forward (n)
9286   "Tick N articles forwards.
9287 If N is negative, tick backwards instead.
9288 The difference between N and the number of articles ticked is returned."
9289   (interactive "p")
9290   (gnus-summary-mark-forward n gnus-ticked-mark))
9291
9292 (defalias 'gnus-summary-mark-as-unread-backward
9293   'gnus-summary-tick-article-backward)
9294 (make-obsolete 'gnus-summary-mark-as-unread-backward
9295                'gnus-summary-tick-article-backward)
9296 (defun gnus-summary-tick-article-backward (n)
9297   "Tick N articles backwards.
9298 The difference between N and the number of articles ticked is returned."
9299   (interactive "p")
9300   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9301
9302 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9303 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9304 (defun gnus-summary-tick-article (&optional article clear-mark)
9305   "Mark current article as unread.
9306 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9307 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9308   (interactive)
9309   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9310                                        gnus-ticked-mark)))
9311
9312 (defun gnus-summary-mark-as-read-forward (n)
9313   "Mark N articles as read forwards.
9314 If N is negative, mark backwards instead.
9315 The difference between N and the actual number of articles marked is
9316 returned."
9317   (interactive "p")
9318   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9319
9320 (defun gnus-summary-mark-as-read-backward (n)
9321   "Mark the N articles as read backwards.
9322 The difference between N and the actual number of articles marked is
9323 returned."
9324   (interactive "p")
9325   (gnus-summary-mark-forward
9326    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9327
9328 (defun gnus-summary-mark-as-read (&optional article mark)
9329   "Mark current article as read.
9330 ARTICLE specifies the article to be marked as read.
9331 MARK specifies a string to be inserted at the beginning of the line."
9332   (gnus-summary-mark-article article mark))
9333
9334 (defun gnus-summary-clear-mark-forward (n)
9335   "Clear marks from N articles forward.
9336 If N is negative, clear backward instead.
9337 The difference between N and the number of marks cleared is returned."
9338   (interactive "p")
9339   (gnus-summary-mark-forward n gnus-unread-mark))
9340
9341 (defun gnus-summary-clear-mark-backward (n)
9342   "Clear marks from N articles backward.
9343 The difference between N and the number of marks cleared is returned."
9344   (interactive "p")
9345   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9346
9347 (defun gnus-summary-mark-unread-as-read ()
9348   "Intended to be used by `gnus-summary-mark-article-hook'."
9349   (when (memq gnus-current-article gnus-newsgroup-unreads)
9350     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9351
9352 (defun gnus-summary-mark-read-and-unread-as-read ()
9353   "Intended to be used by `gnus-summary-mark-article-hook'."
9354   (let ((mark (gnus-summary-article-mark)))
9355     (when (or (gnus-unread-mark-p mark)
9356               (gnus-read-mark-p mark))
9357       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9358
9359 (defun gnus-summary-mark-unread-as-ticked ()
9360   "Intended to be used by `gnus-summary-mark-article-hook'."
9361   (when (memq gnus-current-article gnus-newsgroup-unreads)
9362     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9363
9364 (defun gnus-summary-mark-region-as-read (point mark all)
9365   "Mark all unread articles between point and mark as read.
9366 If given a prefix, mark all articles between point and mark as read,
9367 even ticked and dormant ones."
9368   (interactive "r\nP")
9369   (save-excursion
9370     (let (article)
9371       (goto-char point)
9372       (beginning-of-line)
9373       (while (and
9374               (< (point) mark)
9375               (progn
9376                 (when (or all
9377                           (memq (setq article (gnus-summary-article-number))
9378                                 gnus-newsgroup-unreads))
9379                   (gnus-summary-mark-article article gnus-del-mark))
9380                 t)
9381               (gnus-summary-find-next))))))
9382
9383 (defun gnus-summary-mark-below (score mark)
9384   "Mark articles with score less than SCORE with MARK."
9385   (interactive "P\ncMark: ")
9386   (setq score (if score
9387                   (prefix-numeric-value score)
9388                 (or gnus-summary-default-score 0)))
9389   (save-excursion
9390     (set-buffer gnus-summary-buffer)
9391     (goto-char (point-min))
9392     (while
9393         (progn
9394           (and (< (gnus-summary-article-score) score)
9395                (gnus-summary-mark-article nil mark))
9396           (gnus-summary-find-next)))))
9397
9398 (defun gnus-summary-kill-below (&optional score)
9399   "Mark articles with score below SCORE as read."
9400   (interactive "P")
9401   (gnus-summary-mark-below score gnus-killed-mark))
9402
9403 (defun gnus-summary-clear-above (&optional score)
9404   "Clear all marks from articles with score above SCORE."
9405   (interactive "P")
9406   (gnus-summary-mark-above score gnus-unread-mark))
9407
9408 (defun gnus-summary-tick-above (&optional score)
9409   "Tick all articles with score above SCORE."
9410   (interactive "P")
9411   (gnus-summary-mark-above score gnus-ticked-mark))
9412
9413 (defun gnus-summary-mark-above (score mark)
9414   "Mark articles with score over SCORE with MARK."
9415   (interactive "P\ncMark: ")
9416   (setq score (if score
9417                   (prefix-numeric-value score)
9418                 (or gnus-summary-default-score 0)))
9419   (save-excursion
9420     (set-buffer gnus-summary-buffer)
9421     (goto-char (point-min))
9422     (while (and (progn
9423                   (when (> (gnus-summary-article-score) score)
9424                     (gnus-summary-mark-article nil mark))
9425                   t)
9426                 (gnus-summary-find-next)))))
9427
9428 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9429 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9430 (defun gnus-summary-limit-include-expunged (&optional no-error)
9431   "Display all the hidden articles that were expunged for low scores."
9432   (interactive)
9433   (let ((buffer-read-only nil))
9434     (let ((scored gnus-newsgroup-scored)
9435           headers h)
9436       (while scored
9437         (unless (gnus-summary-article-header (caar scored))
9438           (and (setq h (gnus-number-to-header (caar scored)))
9439                (< (cdar scored) gnus-summary-expunge-below)
9440                (push h headers)))
9441         (setq scored (cdr scored)))
9442       (if (not headers)
9443           (when (not no-error)
9444             (error "No expunged articles hidden"))
9445         (goto-char (point-min))
9446         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9447         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9448         (mapcar (lambda (x) (push (mail-header-number x)
9449                                   gnus-newsgroup-limit))
9450                 headers)
9451         (gnus-summary-prepare-unthreaded (nreverse headers))
9452         (goto-char (point-min))
9453         (gnus-summary-position-point)
9454         t))))
9455
9456 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9457   "Mark all unread articles in this newsgroup as read.
9458 If prefix argument ALL is non-nil, ticked and dormant articles will
9459 also be marked as read.
9460 If QUIETLY is non-nil, no questions will be asked.
9461 If TO-HERE is non-nil, it should be a point in the buffer.  All
9462 articles before (after, if REVERSE is set) this point will be marked as read.
9463 Note that this function will only catch up the unread article
9464 in the current summary buffer limitation.
9465 The number of articles marked as read is returned."
9466   (interactive "P")
9467   (prog1
9468       (save-excursion
9469         (when (or quietly
9470                   (not gnus-interactive-catchup) ;Without confirmation?
9471                   gnus-expert-user
9472                   (gnus-y-or-n-p
9473                    (if all
9474                        "Mark absolutely all articles as read? "
9475                      "Mark all unread articles as read? ")))
9476           (if (and not-mark
9477                    (not gnus-newsgroup-adaptive)
9478                    (not gnus-newsgroup-auto-expire)
9479                    (not gnus-suppress-duplicates)
9480                    (or (not gnus-use-cache)
9481                        (eq gnus-use-cache 'passive)))
9482               (progn
9483                 (when all
9484                   (setq gnus-newsgroup-marked nil
9485                         gnus-newsgroup-dormant nil))
9486                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9487             ;; We actually mark all articles as canceled, which we
9488             ;; have to do when using auto-expiry or adaptive scoring.
9489             (gnus-summary-show-all-threads)
9490             (if (and to-here reverse)
9491                 (progn
9492                   (goto-char to-here)
9493                   (while (and
9494                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9495                           (gnus-summary-find-next (not all) nil nil t))))
9496               (when (gnus-summary-first-subject (not all) t)
9497                 (while (and
9498                         (if to-here (< (point) to-here) t)
9499                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9500                         (gnus-summary-find-next (not all) nil nil t)))))
9501             (gnus-set-mode-line 'summary))
9502           t))
9503     (gnus-summary-position-point)))
9504
9505 (defun gnus-summary-catchup-to-here (&optional all)
9506   "Mark all unticked articles before the current one as read.
9507 If ALL is non-nil, also mark ticked and dormant articles as read."
9508   (interactive "P")
9509   (save-excursion
9510     (gnus-save-hidden-threads
9511       (let ((beg (point)))
9512         ;; We check that there are unread articles.
9513         (when (or all (gnus-summary-find-prev))
9514           (gnus-summary-catchup all t beg)))))
9515   (gnus-summary-position-point))
9516
9517 (defun gnus-summary-catchup-from-here (&optional all)
9518   "Mark all unticked articles after the current one as read.
9519 If ALL is non-nil, also mark ticked and dormant articles as read."
9520   (interactive "P")
9521   (save-excursion
9522     (gnus-save-hidden-threads
9523       (let ((beg (point)))
9524         ;; We check that there are unread articles.
9525         (when (or all (gnus-summary-find-next))
9526           (gnus-summary-catchup all t beg nil t)))))
9527   (gnus-summary-position-point))
9528
9529 (defun gnus-summary-catchup-all (&optional quietly)
9530   "Mark all articles in this newsgroup as read."
9531   (interactive "P")
9532   (gnus-summary-catchup t quietly))
9533
9534 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9535   "Mark all unread articles in this group as read, then exit.
9536 If prefix argument ALL is non-nil, all articles are marked as read.
9537 If QUIETLY is non-nil, no questions will be asked."
9538   (interactive "P")
9539   (when (gnus-summary-catchup all quietly nil 'fast)
9540     ;; Select next newsgroup or exit.
9541     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9542              (eq gnus-auto-select-next 'quietly))
9543         (gnus-summary-next-group nil)
9544       (gnus-summary-exit))))
9545
9546 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9547   "Mark all articles in this newsgroup as read, and then exit."
9548   (interactive "P")
9549   (gnus-summary-catchup-and-exit t quietly))
9550
9551 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9552   "Mark all articles in this group as read and select the next group.
9553 If given a prefix, mark all articles, unread as well as ticked, as
9554 read."
9555   (interactive "P")
9556   (save-excursion
9557     (gnus-summary-catchup all))
9558   (gnus-summary-next-group))
9559
9560 ;;;
9561 ;;; with article
9562 ;;;
9563
9564 (defmacro gnus-with-article (article &rest forms)
9565   "Select ARTICLE and perform FORMS in the original article buffer.
9566 Then replace the article with the result."
9567   `(progn
9568      ;; We don't want the article to be marked as read.
9569      (let (gnus-mark-article-hook)
9570        (gnus-summary-select-article t t nil ,article))
9571      (set-buffer gnus-original-article-buffer)
9572      ,@forms
9573      (if (not (gnus-check-backend-function
9574                'request-replace-article (car gnus-article-current)))
9575          (gnus-message 5 "Read-only group; not replacing")
9576        (unless (gnus-request-replace-article
9577                 ,article (car gnus-article-current)
9578                 (current-buffer) t)
9579          (error "Couldn't replace article")))
9580      ;; The cache and backlog have to be flushed somewhat.
9581      (when gnus-keep-backlog
9582        (gnus-backlog-remove-article
9583         (car gnus-article-current) (cdr gnus-article-current)))
9584      (when gnus-use-cache
9585        (gnus-cache-update-article
9586         (car gnus-article-current) (cdr gnus-article-current)))))
9587
9588 (put 'gnus-with-article 'lisp-indent-function 1)
9589 (put 'gnus-with-article 'edebug-form-spec '(form body))
9590
9591 ;; Thread-based commands.
9592
9593 (defun gnus-summary-articles-in-thread (&optional article)
9594   "Return a list of all articles in the current thread.
9595 If ARTICLE is non-nil, return all articles in the thread that starts
9596 with that article."
9597   (let* ((article (or article (gnus-summary-article-number)))
9598          (data (gnus-data-find-list article))
9599          (top-level (gnus-data-level (car data)))
9600          (top-subject
9601           (cond ((null gnus-thread-operation-ignore-subject)
9602                  (gnus-simplify-subject-re
9603                   (mail-header-subject (gnus-data-header (car data)))))
9604                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9605                  (gnus-simplify-subject-fuzzy
9606                   (mail-header-subject (gnus-data-header (car data)))))
9607                 (t nil)))
9608          (end-point (save-excursion
9609                       (if (gnus-summary-go-to-next-thread)
9610                           (point) (point-max))))
9611          articles)
9612     (while (and data
9613                 (< (gnus-data-pos (car data)) end-point))
9614       (when (or (not top-subject)
9615                 (string= top-subject
9616                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9617                              (gnus-simplify-subject-fuzzy
9618                               (mail-header-subject
9619                                (gnus-data-header (car data))))
9620                            (gnus-simplify-subject-re
9621                             (mail-header-subject
9622                              (gnus-data-header (car data)))))))
9623         (push (gnus-data-number (car data)) articles))
9624       (unless (and (setq data (cdr data))
9625                    (> (gnus-data-level (car data)) top-level))
9626         (setq data nil)))
9627     ;; Return the list of articles.
9628     (nreverse articles)))
9629
9630 (defun gnus-summary-rethread-current ()
9631   "Rethread the thread the current article is part of."
9632   (interactive)
9633   (let* ((gnus-show-threads t)
9634          (article (gnus-summary-article-number))
9635          (id (mail-header-id (gnus-summary-article-header)))
9636          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9637     (unless id
9638       (error "No article on the current line"))
9639     (gnus-rebuild-thread id)
9640     (gnus-summary-goto-subject article)))
9641
9642 (defun gnus-summary-reparent-thread ()
9643   "Make the current article child of the marked (or previous) article.
9644
9645 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9646 is non-nil or the Subject: of both articles are the same."
9647   (interactive)
9648   (unless (not (gnus-group-read-only-p))
9649     (error "The current newsgroup does not support article editing"))
9650   (unless (<= (length gnus-newsgroup-processable) 1)
9651     (error "No more than one article may be marked"))
9652   (save-window-excursion
9653     (let ((gnus-article-buffer " *reparent*")
9654           (current-article (gnus-summary-article-number))
9655           ;; First grab the marked article, otherwise one line up.
9656           (parent-article (if (not (null gnus-newsgroup-processable))
9657                               (car gnus-newsgroup-processable)
9658                             (save-excursion
9659                               (if (eq (forward-line -1) 0)
9660                                   (gnus-summary-article-number)
9661                                 (error "Beginning of summary buffer"))))))
9662       (unless (not (eq current-article parent-article))
9663         (error "An article may not be self-referential"))
9664       (let ((message-id (mail-header-id
9665                          (gnus-summary-article-header parent-article))))
9666         (unless (and message-id (not (equal message-id "")))
9667           (error "No message-id in desired parent"))
9668         (gnus-with-article current-article
9669           (save-restriction
9670             (goto-char (point-min))
9671             (message-narrow-to-head)
9672             (if (re-search-forward "^References: " nil t)
9673                 (progn
9674                   (re-search-forward "^[^ \t]" nil t)
9675                   (forward-line -1)
9676                   (end-of-line)
9677                   (insert " " message-id))
9678               (insert "References: " message-id "\n"))))
9679         (set-buffer gnus-summary-buffer)
9680         (gnus-summary-unmark-all-processable)
9681         (gnus-summary-update-article current-article)
9682         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9683             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9684         (gnus-summary-rethread-current)
9685         (gnus-message 3 "Article %d is now the child of article %d"
9686                       current-article parent-article)))))
9687
9688 (defun gnus-summary-toggle-threads (&optional arg)
9689   "Toggle showing conversation threads.
9690 If ARG is positive number, turn showing conversation threads on."
9691   (interactive "P")
9692   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9693     (setq gnus-show-threads
9694           (if (null arg) (not gnus-show-threads)
9695             (> (prefix-numeric-value arg) 0)))
9696     (gnus-summary-prepare)
9697     (gnus-summary-goto-subject current)
9698     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9699     (gnus-summary-position-point)))
9700
9701 (defun gnus-summary-show-all-threads ()
9702   "Show all threads."
9703   (interactive)
9704   (save-excursion
9705     (let ((buffer-read-only nil))
9706       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9707   (gnus-summary-position-point))
9708
9709 (defun gnus-summary-show-thread ()
9710   "Show thread subtrees.
9711 Returns nil if no thread was there to be shown."
9712   (interactive)
9713   (let ((buffer-read-only nil)
9714         (orig (point))
9715         ;; first goto end then to beg, to have point at beg after let
9716         (end (progn (end-of-line) (point)))
9717         (beg (progn (beginning-of-line) (point))))
9718     (prog1
9719         ;; Any hidden lines here?
9720         (search-forward "\r" end t)
9721       (subst-char-in-region beg end ?\^M ?\n t)
9722       (goto-char orig)
9723       (gnus-summary-position-point))))
9724
9725 (defun gnus-summary-hide-all-threads ()
9726   "Hide all thread subtrees."
9727   (interactive)
9728   (save-excursion
9729     (goto-char (point-min))
9730     (gnus-summary-hide-thread)
9731     (while (zerop (gnus-summary-next-thread 1 t))
9732       (gnus-summary-hide-thread)))
9733   (gnus-summary-position-point))
9734
9735 (defun gnus-summary-hide-thread ()
9736   "Hide thread subtrees.
9737 Returns nil if no threads were there to be hidden."
9738   (interactive)
9739   (let ((buffer-read-only nil)
9740         (start (point))
9741         (article (gnus-summary-article-number)))
9742     (goto-char start)
9743     ;; Go forward until either the buffer ends or the subthread
9744     ;; ends.
9745     (when (and (not (eobp))
9746                (or (zerop (gnus-summary-next-thread 1 t))
9747                    (goto-char (point-max))))
9748       (prog1
9749           (if (and (> (point) start)
9750                    (search-backward "\n" start t))
9751               (progn
9752                 (subst-char-in-region start (point) ?\n ?\^M)
9753                 (gnus-summary-goto-subject article))
9754             (goto-char start)
9755             nil)))))
9756
9757 (defun gnus-summary-go-to-next-thread (&optional previous)
9758   "Go to the same level (or less) next thread.
9759 If PREVIOUS is non-nil, go to previous thread instead.
9760 Return the article number moved to, or nil if moving was impossible."
9761   (let ((level (gnus-summary-thread-level))
9762         (way (if previous -1 1))
9763         (beg (point)))
9764     (forward-line way)
9765     (while (and (not (eobp))
9766                 (< level (gnus-summary-thread-level)))
9767       (forward-line way))
9768     (if (eobp)
9769         (progn
9770           (goto-char beg)
9771           nil)
9772       (setq beg (point))
9773       (prog1
9774           (gnus-summary-article-number)
9775         (goto-char beg)))))
9776
9777 (defun gnus-summary-next-thread (n &optional silent)
9778   "Go to the same level next N'th thread.
9779 If N is negative, search backward instead.
9780 Returns the difference between N and the number of skips actually
9781 done.
9782
9783 If SILENT, don't output messages."
9784   (interactive "p")
9785   (let ((backward (< n 0))
9786         (n (abs n)))
9787     (while (and (> n 0)
9788                 (gnus-summary-go-to-next-thread backward))
9789       (decf n))
9790     (unless silent
9791       (gnus-summary-position-point))
9792     (when (and (not silent) (/= 0 n))
9793       (gnus-message 7 "No more threads"))
9794     n))
9795
9796 (defun gnus-summary-prev-thread (n)
9797   "Go to the same level previous N'th thread.
9798 Returns the difference between N and the number of skips actually
9799 done."
9800   (interactive "p")
9801   (gnus-summary-next-thread (- n)))
9802
9803 (defun gnus-summary-go-down-thread ()
9804   "Go down one level in the current thread."
9805   (let ((children (gnus-summary-article-children)))
9806     (when children
9807       (gnus-summary-goto-subject (car children)))))
9808
9809 (defun gnus-summary-go-up-thread ()
9810   "Go up one level in the current thread."
9811   (let ((parent (gnus-summary-article-parent)))
9812     (when parent
9813       (gnus-summary-goto-subject parent))))
9814
9815 (defun gnus-summary-down-thread (n)
9816   "Go down thread N steps.
9817 If N is negative, go up instead.
9818 Returns the difference between N and how many steps down that were
9819 taken."
9820   (interactive "p")
9821   (let ((up (< n 0))
9822         (n (abs n)))
9823     (while (and (> n 0)
9824                 (if up (gnus-summary-go-up-thread)
9825                   (gnus-summary-go-down-thread)))
9826       (setq n (1- n)))
9827     (gnus-summary-position-point)
9828     (when (/= 0 n)
9829       (gnus-message 7 "Can't go further"))
9830     n))
9831
9832 (defun gnus-summary-up-thread (n)
9833   "Go up thread N steps.
9834 If N is negative, go down instead.
9835 Returns the difference between N and how many steps down that were
9836 taken."
9837   (interactive "p")
9838   (gnus-summary-down-thread (- n)))
9839
9840 (defun gnus-summary-top-thread ()
9841   "Go to the top of the thread."
9842   (interactive)
9843   (while (gnus-summary-go-up-thread))
9844   (gnus-summary-article-number))
9845
9846 (defun gnus-summary-kill-thread (&optional unmark)
9847   "Mark articles under current thread as read.
9848 If the prefix argument is positive, remove any kinds of marks.
9849 If the prefix argument is negative, tick articles instead."
9850   (interactive "P")
9851   (when unmark
9852     (setq unmark (prefix-numeric-value unmark)))
9853   (let ((articles (gnus-summary-articles-in-thread)))
9854     (save-excursion
9855       ;; Expand the thread.
9856       (gnus-summary-show-thread)
9857       ;; Mark all the articles.
9858       (while articles
9859         (gnus-summary-goto-subject (car articles))
9860         (cond ((null unmark)
9861                (gnus-summary-mark-article-as-read gnus-killed-mark))
9862               ((> unmark 0)
9863                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9864               (t
9865                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9866         (setq articles (cdr articles))))
9867     ;; Hide killed subtrees.
9868     (and (null unmark)
9869          gnus-thread-hide-killed
9870          (gnus-summary-hide-thread))
9871     ;; If marked as read, go to next unread subject.
9872     (when (null unmark)
9873       ;; Go to next unread subject.
9874       (gnus-summary-next-subject 1 t)))
9875   (gnus-set-mode-line 'summary))
9876
9877 ;; Summary sorting commands
9878
9879 (defun gnus-summary-sort-by-number (&optional reverse)
9880   "Sort the summary buffer by article number.
9881 Argument REVERSE means reverse order."
9882   (interactive "P")
9883   (gnus-summary-sort 'number reverse))
9884
9885 (defun gnus-summary-sort-by-author (&optional reverse)
9886   "Sort the summary buffer by author name alphabetically.
9887 If `case-fold-search' is non-nil, case of letters is ignored.
9888 Argument REVERSE means reverse order."
9889   (interactive "P")
9890   (gnus-summary-sort 'author reverse))
9891
9892 (defun gnus-summary-sort-by-subject (&optional reverse)
9893   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9894 If `case-fold-search' is non-nil, case of letters is ignored.
9895 Argument REVERSE means reverse order."
9896   (interactive "P")
9897   (gnus-summary-sort 'subject reverse))
9898
9899 (defun gnus-summary-sort-by-date (&optional reverse)
9900   "Sort the summary buffer by date.
9901 Argument REVERSE means reverse order."
9902   (interactive "P")
9903   (gnus-summary-sort 'date reverse))
9904
9905 (defun gnus-summary-sort-by-score (&optional reverse)
9906   "Sort the summary buffer by score.
9907 Argument REVERSE means reverse order."
9908   (interactive "P")
9909   (gnus-summary-sort 'score reverse))
9910
9911 (defun gnus-summary-sort-by-lines (&optional reverse)
9912   "Sort the summary buffer by the number of lines.
9913 Argument REVERSE means reverse order."
9914   (interactive "P")
9915   (gnus-summary-sort 'lines reverse))
9916
9917 (defun gnus-summary-sort-by-chars (&optional reverse)
9918   "Sort the summary buffer by article length.
9919 Argument REVERSE means reverse order."
9920   (interactive "P")
9921   (gnus-summary-sort 'chars reverse))
9922
9923 (defun gnus-summary-sort-by-original (&optional reverse)
9924   "Sort the summary buffer using the default sorting method.
9925 Argument REVERSE means reverse order."
9926   (interactive "P")
9927   (let* ((buffer-read-only)
9928          (gnus-summary-prepare-hook nil))
9929     ;; We do the sorting by regenerating the threads.
9930     (gnus-summary-prepare)
9931     ;; Hide subthreads if needed.
9932     (when (and gnus-show-threads gnus-thread-hide-subtree)
9933       (gnus-summary-hide-all-threads))))
9934
9935 (defun gnus-summary-sort (predicate reverse)
9936   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9937   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9938          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9939          (gnus-thread-sort-functions
9940           (if (not reverse)
9941               thread
9942             `(lambda (t1 t2)
9943                (,thread t2 t1))))
9944          (gnus-sort-gathered-threads-function
9945           gnus-thread-sort-functions)
9946          (gnus-article-sort-functions
9947           (if (not reverse)
9948               article
9949             `(lambda (t1 t2)
9950                (,article t2 t1))))
9951          (buffer-read-only)
9952          (gnus-summary-prepare-hook nil))
9953     ;; We do the sorting by regenerating the threads.
9954     (gnus-summary-prepare)
9955     ;; Hide subthreads if needed.
9956     (when (and gnus-show-threads gnus-thread-hide-subtree)
9957       (gnus-summary-hide-all-threads))))
9958
9959 ;; Summary saving commands.
9960
9961 (defun gnus-summary-save-article (&optional n not-saved)
9962   "Save the current article using the default saver function.
9963 If N is a positive number, save the N next articles.
9964 If N is a negative number, save the N previous articles.
9965 If N is nil and any articles have been marked with the process mark,
9966 save those articles instead.
9967 The variable `gnus-default-article-saver' specifies the saver function."
9968   (interactive "P")
9969   (let* ((articles (gnus-summary-work-articles n))
9970          (save-buffer (save-excursion
9971                         (nnheader-set-temp-buffer " *Gnus Save*")))
9972          (num (length articles))
9973          header file)
9974     (dolist (article articles)
9975       (setq header (gnus-summary-article-header article))
9976       (if (not (vectorp header))
9977           ;; This is a pseudo-article.
9978           (if (assq 'name header)
9979               (gnus-copy-file (cdr (assq 'name header)))
9980             (gnus-message 1 "Article %d is unsaveable" article))
9981         ;; This is a real article.
9982         (save-window-excursion
9983           (let ((gnus-display-mime-function nil)
9984                 (gnus-article-prepare-hook nil))
9985             (gnus-summary-select-article t nil nil article)))
9986         (save-excursion
9987           (set-buffer save-buffer)
9988           (erase-buffer)
9989           (insert-buffer-substring gnus-original-article-buffer))
9990         (setq file (gnus-article-save save-buffer file num))
9991         (gnus-summary-remove-process-mark article)
9992         (unless not-saved
9993           (gnus-summary-set-saved-mark article))))
9994     (gnus-kill-buffer save-buffer)
9995     (gnus-summary-position-point)
9996     (gnus-set-mode-line 'summary)
9997     n))
9998
9999 (defun gnus-summary-pipe-output (&optional arg)
10000   "Pipe the current article to a subprocess.
10001 If N is a positive number, pipe the N next articles.
10002 If N is a negative number, pipe the N previous articles.
10003 If N is nil and any articles have been marked with the process mark,
10004 pipe those articles instead."
10005   (interactive "P")
10006   (require 'gnus-art)
10007   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10008     (gnus-summary-save-article arg t))
10009   (let ((buffer (get-buffer "*Shell Command Output*")))
10010     (if (and buffer
10011              (with-current-buffer buffer (> (point-max) (point-min))))
10012         (gnus-configure-windows 'pipe))))
10013
10014 (defun gnus-summary-save-article-mail (&optional arg)
10015   "Append the current article to an mail file.
10016 If N is a positive number, save the N next articles.
10017 If N is a negative number, save the N previous articles.
10018 If N is nil and any articles have been marked with the process mark,
10019 save those articles instead."
10020   (interactive "P")
10021   (require 'gnus-art)
10022   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10023     (gnus-summary-save-article arg)))
10024
10025 (defun gnus-summary-save-article-rmail (&optional arg)
10026   "Append the current article to an rmail file.
10027 If N is a positive number, save the N next articles.
10028 If N is a negative number, save the N previous articles.
10029 If N is nil and any articles have been marked with the process mark,
10030 save those articles instead."
10031   (interactive "P")
10032   (require 'gnus-art)
10033   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10034     (gnus-summary-save-article arg)))
10035
10036 (defun gnus-summary-save-article-file (&optional arg)
10037   "Append the current article to a file.
10038 If N is a positive number, save the N next articles.
10039 If N is a negative number, save the N previous articles.
10040 If N is nil and any articles have been marked with the process mark,
10041 save those articles instead."
10042   (interactive "P")
10043   (require 'gnus-art)
10044   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10045     (gnus-summary-save-article arg)))
10046
10047 (defun gnus-summary-write-article-file (&optional arg)
10048   "Write the current article to a file, deleting the previous file.
10049 If N is a positive number, save the N next articles.
10050 If N is a negative number, save the N previous articles.
10051 If N is nil and any articles have been marked with the process mark,
10052 save those articles instead."
10053   (interactive "P")
10054   (require 'gnus-art)
10055   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10056     (gnus-summary-save-article arg)))
10057
10058 (defun gnus-summary-save-article-body-file (&optional arg)
10059   "Append the current article body to a file.
10060 If N is a positive number, save the N next articles.
10061 If N is a negative number, save the N previous articles.
10062 If N is nil and any articles have been marked with the process mark,
10063 save those articles instead."
10064   (interactive "P")
10065   (require 'gnus-art)
10066   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10067     (gnus-summary-save-article arg)))
10068
10069 (defun gnus-summary-muttprint (&optional arg)
10070   "Print the current article using Muttprint.
10071 If N is a positive number, save the N next articles.
10072 If N is a negative number, save the N previous articles.
10073 If N is nil and any articles have been marked with the process mark,
10074 save those articles instead."
10075   (interactive "P")
10076   (require 'gnus-art)
10077   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10078     (gnus-summary-save-article arg t)))
10079
10080 (defun gnus-summary-pipe-message (program)
10081   "Pipe the current article through PROGRAM."
10082   (interactive "sProgram: ")
10083   (gnus-summary-select-article)
10084   (let ((mail-header-separator ""))
10085     (gnus-eval-in-buffer-window gnus-article-buffer
10086       (save-restriction
10087         (widen)
10088         (let ((start (window-start))
10089               buffer-read-only)
10090           (message-pipe-buffer-body program)
10091           (set-window-start (get-buffer-window (current-buffer)) start))))))
10092
10093 (defun gnus-get-split-value (methods)
10094   "Return a value based on the split METHODS."
10095   (let (split-name method result match)
10096     (when methods
10097       (save-excursion
10098         (set-buffer gnus-original-article-buffer)
10099         (save-restriction
10100           (nnheader-narrow-to-headers)
10101           (while (and methods (not split-name))
10102             (goto-char (point-min))
10103             (setq method (pop methods))
10104             (setq match (car method))
10105             (when (cond
10106                    ((stringp match)
10107                     ;; Regular expression.
10108                     (ignore-errors
10109                       (re-search-forward match nil t)))
10110                    ((gnus-functionp match)
10111                     ;; Function.
10112                     (save-restriction
10113                       (widen)
10114                       (setq result (funcall match gnus-newsgroup-name))))
10115                    ((consp match)
10116                     ;; Form.
10117                     (save-restriction
10118                       (widen)
10119                       (setq result (eval match)))))
10120               (setq split-name (cdr method))
10121               (cond ((stringp result)
10122                      (push (expand-file-name
10123                             result gnus-article-save-directory)
10124                            split-name))
10125                     ((consp result)
10126                      (setq split-name (append result split-name)))))))))
10127     (nreverse split-name)))
10128
10129 (defun gnus-valid-move-group-p (group)
10130   (and (boundp group)
10131        (symbol-name group)
10132        (symbol-value group)
10133        (gnus-get-function (gnus-find-method-for-group
10134                            (symbol-name group)) 'request-accept-article t)))
10135
10136 (defun gnus-read-move-group-name (prompt default articles prefix)
10137   "Read a group name."
10138   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10139          (minibuffer-confirm-incomplete nil) ; XEmacs
10140          (prom
10141           (format "%s %s to:"
10142                   prompt
10143                   (if (> (length articles) 1)
10144                       (format "these %d articles" (length articles))
10145                     "this article")))
10146          (to-newsgroup
10147           (cond
10148            ((null split-name)
10149             (gnus-completing-read default prom
10150                                   gnus-active-hashtb
10151                                   'gnus-valid-move-group-p
10152                                   nil prefix
10153                                   'gnus-group-history))
10154            ((= 1 (length split-name))
10155             (gnus-completing-read (car split-name) prom
10156                                   gnus-active-hashtb
10157                                   'gnus-valid-move-group-p
10158                                   nil nil
10159                                   'gnus-group-history))
10160            (t
10161             (gnus-completing-read nil prom
10162                                   (mapcar (lambda (el) (list el))
10163                                           (nreverse split-name))
10164                                   nil nil nil
10165                                   'gnus-group-history))))
10166          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10167     (when to-newsgroup
10168       (if (or (string= to-newsgroup "")
10169               (string= to-newsgroup prefix))
10170           (setq to-newsgroup default))
10171       (unless to-newsgroup
10172         (error "No group name entered"))
10173       (or (gnus-active to-newsgroup)
10174           (gnus-activate-group to-newsgroup nil nil to-method)
10175           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10176                                      to-newsgroup))
10177               (or (and (gnus-request-create-group to-newsgroup to-method)
10178                        (gnus-activate-group
10179                         to-newsgroup nil nil to-method)
10180                        (gnus-subscribe-group to-newsgroup))
10181                   (error "Couldn't create group %s" to-newsgroup)))
10182           (error "No such group: %s" to-newsgroup)))
10183     to-newsgroup))
10184
10185 (defun gnus-summary-save-parts (type dir n &optional reverse)
10186   "Save parts matching TYPE to DIR.
10187 If REVERSE, save parts that do not match TYPE."
10188   (interactive
10189    (list (read-string "Save parts of type: "
10190                       (or (car gnus-summary-save-parts-type-history)
10191                           gnus-summary-save-parts-default-mime)
10192                       'gnus-summary-save-parts-type-history)
10193          (setq gnus-summary-save-parts-last-directory
10194                (read-file-name "Save to directory: "
10195                                gnus-summary-save-parts-last-directory
10196                                nil t))
10197          current-prefix-arg))
10198   (gnus-summary-iterate n
10199     (let ((gnus-display-mime-function nil)
10200           (gnus-inhibit-treatment t))
10201       (gnus-summary-select-article))
10202     (save-excursion
10203       (set-buffer gnus-article-buffer)
10204       (let ((handles (or gnus-article-mime-handles
10205                          (mm-dissect-buffer) (mm-uu-dissect))))
10206         (when handles
10207           (gnus-summary-save-parts-1 type dir handles reverse)
10208           (unless gnus-article-mime-handles ;; Don't destroy this case.
10209             (mm-destroy-parts handles)))))))
10210
10211 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10212   (if (stringp (car handle))
10213       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10214               (cdr handle))
10215     (when (if reverse
10216               (not (string-match type (mm-handle-media-type handle)))
10217             (string-match type (mm-handle-media-type handle)))
10218       (let ((file (expand-file-name
10219                    (file-name-nondirectory
10220                     (or
10221                      (mail-content-type-get
10222                       (mm-handle-disposition handle) 'filename)
10223                      (concat gnus-newsgroup-name
10224                              "." (number-to-string
10225                                   (cdr gnus-article-current)))))
10226                    dir)))
10227         (unless (file-exists-p file)
10228           (mm-save-part-to-file handle file))))))
10229
10230 ;; Summary extract commands
10231
10232 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10233   (let ((buffer-read-only nil)
10234         (article (gnus-summary-article-number))
10235         after-article b e)
10236     (unless (gnus-summary-goto-subject article)
10237       (error "No such article: %d" article))
10238     (gnus-summary-position-point)
10239     ;; If all commands are to be bunched up on one line, we collect
10240     ;; them here.
10241     (unless gnus-view-pseudos-separately
10242       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10243             files action)
10244         (while ps
10245           (setq action (cdr (assq 'action (car ps))))
10246           (setq files (list (cdr (assq 'name (car ps)))))
10247           (while (and ps (cdr ps)
10248                       (string= (or action "1")
10249                                (or (cdr (assq 'action (cadr ps))) "2")))
10250             (push (cdr (assq 'name (cadr ps))) files)
10251             (setcdr ps (cddr ps)))
10252           (when files
10253             (when (not (string-match "%s" action))
10254               (push " " files))
10255             (push " " files)
10256             (when (assq 'execute (car ps))
10257               (setcdr (assq 'execute (car ps))
10258                       (funcall (if (string-match "%s" action)
10259                                    'format 'concat)
10260                                action
10261                                (mapconcat
10262                                 (lambda (f)
10263                                   (if (equal f " ")
10264                                       f
10265                                     (gnus-quote-arg-for-sh-or-csh f)))
10266                                 files " ")))))
10267           (setq ps (cdr ps)))))
10268     (if (and gnus-view-pseudos (not not-view))
10269         (while pslist
10270           (when (assq 'execute (car pslist))
10271             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10272                                   (eq gnus-view-pseudos 'not-confirm)))
10273           (setq pslist (cdr pslist)))
10274       (save-excursion
10275         (while pslist
10276           (setq after-article (or (cdr (assq 'article (car pslist)))
10277                                   (gnus-summary-article-number)))
10278           (gnus-summary-goto-subject after-article)
10279           (forward-line 1)
10280           (setq b (point))
10281           (insert "    " (file-name-nondirectory
10282                           (cdr (assq 'name (car pslist))))
10283                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10284           (setq e (point))
10285           (forward-line -1)             ; back to `b'
10286           (gnus-add-text-properties
10287            b (1- e) (list 'gnus-number gnus-reffed-article-number
10288                           gnus-mouse-face-prop gnus-mouse-face))
10289           (gnus-data-enter
10290            after-article gnus-reffed-article-number
10291            gnus-unread-mark b (car pslist) 0 (- e b))
10292           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10293           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10294           (setq pslist (cdr pslist)))))))
10295
10296 (defun gnus-pseudos< (p1 p2)
10297   (let ((c1 (cdr (assq 'action p1)))
10298         (c2 (cdr (assq 'action p2))))
10299     (and c1 c2 (string< c1 c2))))
10300
10301 (defun gnus-request-pseudo-article (props)
10302   (cond ((assq 'execute props)
10303          (gnus-execute-command (cdr (assq 'execute props)))))
10304   (let ((gnus-current-article (gnus-summary-article-number)))
10305     (gnus-run-hooks 'gnus-mark-article-hook)))
10306
10307 (defun gnus-execute-command (command &optional automatic)
10308   (save-excursion
10309     (gnus-article-setup-buffer)
10310     (set-buffer gnus-article-buffer)
10311     (setq buffer-read-only nil)
10312     (let ((command (if automatic command
10313                      (read-string "Command: " (cons command 0)))))
10314       (erase-buffer)
10315       (insert "$ " command "\n\n")
10316       (if gnus-view-pseudo-asynchronously
10317           (start-process "gnus-execute" (current-buffer) shell-file-name
10318                          shell-command-switch command)
10319         (call-process shell-file-name nil t nil
10320                       shell-command-switch command)))))
10321
10322 ;; Summary kill commands.
10323
10324 (defun gnus-summary-edit-global-kill (article)
10325   "Edit the \"global\" kill file."
10326   (interactive (list (gnus-summary-article-number)))
10327   (gnus-group-edit-global-kill article))
10328
10329 (defun gnus-summary-edit-local-kill ()
10330   "Edit a local kill file applied to the current newsgroup."
10331   (interactive)
10332   (setq gnus-current-headers (gnus-summary-article-header))
10333   (gnus-group-edit-local-kill
10334    (gnus-summary-article-number) gnus-newsgroup-name))
10335
10336 ;;; Header reading.
10337
10338 (defun gnus-read-header (id &optional header)
10339   "Read the headers of article ID and enter them into the Gnus system."
10340   (let ((group gnus-newsgroup-name)
10341         (gnus-override-method
10342          (or
10343           gnus-override-method
10344           (and (gnus-news-group-p gnus-newsgroup-name)
10345                (car (gnus-refer-article-methods)))))
10346         where)
10347     ;; First we check to see whether the header in question is already
10348     ;; fetched.
10349     (if (stringp id)
10350         ;; This is a Message-ID.
10351         (setq header (or header (gnus-id-to-header id)))
10352       ;; This is an article number.
10353       (setq header (or header (gnus-summary-article-header id))))
10354     (if (and header
10355              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10356         ;; We have found the header.
10357         header
10358       ;; If this is a sparse article, we have to nix out its
10359       ;; previous entry in the thread hashtb.
10360       (when (and header
10361                  (gnus-summary-article-sparse-p (mail-header-number header)))
10362         (let* ((parent (gnus-parent-id (mail-header-references header)))
10363                (thread (and parent (gnus-id-to-thread parent))))
10364           (when thread
10365             (delq (assq header thread) thread))))
10366       ;; We have to really fetch the header to this article.
10367       (save-excursion
10368         (set-buffer nntp-server-buffer)
10369         (when (setq where (gnus-request-head id group))
10370           (nnheader-fold-continuation-lines)
10371           (goto-char (point-max))
10372           (insert ".\n")
10373           (goto-char (point-min))
10374           (insert "211 ")
10375           (princ (cond
10376                   ((numberp id) id)
10377                   ((cdr where) (cdr where))
10378                   (header (mail-header-number header))
10379                   (t gnus-reffed-article-number))
10380                  (current-buffer))
10381           (insert " Article retrieved.\n"))
10382         (if (or (not where)
10383                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10384             ()                          ; Malformed head.
10385           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10386             (when (and (stringp id)
10387                        (not (string= (gnus-group-real-name group)
10388                                      (car where))))
10389               ;; If we fetched by Message-ID and the article came
10390               ;; from a different group, we fudge some bogus article
10391               ;; numbers for this article.
10392               (mail-header-set-number header gnus-reffed-article-number))
10393             (save-excursion
10394               (set-buffer gnus-summary-buffer)
10395               (decf gnus-reffed-article-number)
10396               (gnus-remove-header (mail-header-number header))
10397               (push header gnus-newsgroup-headers)
10398               (setq gnus-current-headers header)
10399               (push (mail-header-number header) gnus-newsgroup-limit)))
10400           header)))))
10401
10402 (defun gnus-remove-header (number)
10403   "Remove header NUMBER from `gnus-newsgroup-headers'."
10404   (if (and gnus-newsgroup-headers
10405            (= number (mail-header-number (car gnus-newsgroup-headers))))
10406       (pop gnus-newsgroup-headers)
10407     (let ((headers gnus-newsgroup-headers))
10408       (while (and (cdr headers)
10409                   (not (= number (mail-header-number (cadr headers)))))
10410         (pop headers))
10411       (when (cdr headers)
10412         (setcdr headers (cddr headers))))))
10413
10414 ;;;
10415 ;;; summary highlights
10416 ;;;
10417
10418 (defun gnus-highlight-selected-summary ()
10419   "Highlight selected article in summary buffer."
10420   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10421   (when gnus-summary-selected-face
10422     (save-excursion
10423       (let* ((beg (progn (beginning-of-line) (point)))
10424              (end (progn (end-of-line) (point)))
10425              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10426              (from (if (get-text-property beg gnus-mouse-face-prop)
10427                        beg
10428                      (or (next-single-property-change
10429                           beg gnus-mouse-face-prop nil end)
10430                          beg)))
10431              (to
10432               (if (= from end)
10433                   (- from 2)
10434                 (or (next-single-property-change
10435                      from gnus-mouse-face-prop nil end)
10436                     end))))
10437         ;; If no mouse-face prop on line we will have to = from = end,
10438         ;; so we highlight the entire line instead.
10439         (when (= (+ to 2) from)
10440           (setq from beg)
10441           (setq to end))
10442         (if gnus-newsgroup-selected-overlay
10443             ;; Move old overlay.
10444             (gnus-move-overlay
10445              gnus-newsgroup-selected-overlay from to (current-buffer))
10446           ;; Create new overlay.
10447           (gnus-overlay-put
10448            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10449            'face gnus-summary-selected-face))))))
10450
10451 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10452 (defun gnus-summary-highlight-line ()
10453   "Highlight current line according to `gnus-summary-highlight'."
10454   (let* ((list gnus-summary-highlight)
10455          (p (point))
10456          (end (progn (end-of-line) (point)))
10457          ;; now find out where the line starts and leave point there.
10458          (beg (progn (beginning-of-line) (point)))
10459          (article (gnus-summary-article-number))
10460          (score (or (cdr (assq (or article gnus-current-article)
10461                                gnus-newsgroup-scored))
10462                     gnus-summary-default-score 0))
10463          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10464          (inhibit-read-only t))
10465     ;; Eval the cars of the lists until we find a match.
10466     (let ((default gnus-summary-default-score)
10467           (default-high gnus-summary-default-high-score)
10468           (default-low gnus-summary-default-low-score))
10469       (while (and list
10470                   (not (eval (caar list))))
10471         (setq list (cdr list))))
10472     (let ((face (cdar list)))
10473       (unless (eq face (get-text-property beg 'face))
10474         (gnus-put-text-property-excluding-characters-with-faces
10475          beg end 'face
10476          (setq face (if (boundp face) (symbol-value face) face)))
10477         (when gnus-summary-highlight-line-function
10478           (funcall gnus-summary-highlight-line-function article face))))
10479     (goto-char p)))
10480
10481 (defun gnus-update-read-articles (group unread &optional compute)
10482   "Update the list of read articles in GROUP."
10483   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10484          (entry (gnus-gethash group gnus-newsrc-hashtb))
10485          (info (nth 2 entry))
10486          (prev 1)
10487          (unread (sort (copy-sequence unread) '<))
10488          read)
10489     (if (or (not info) (not active))
10490         ;; There is no info on this group if it was, in fact,
10491         ;; killed.  Gnus stores no information on killed groups, so
10492         ;; there's nothing to be done.
10493         ;; One could store the information somewhere temporarily,
10494         ;; perhaps...  Hmmm...
10495         ()
10496       ;; Remove any negative articles numbers.
10497       (while (and unread (< (car unread) 0))
10498         (setq unread (cdr unread)))
10499       ;; Remove any expired article numbers
10500       (while (and unread (< (car unread) (car active)))
10501         (setq unread (cdr unread)))
10502       ;; Compute the ranges of read articles by looking at the list of
10503       ;; unread articles.
10504       (while unread
10505         (when (/= (car unread) prev)
10506           (push (if (= prev (1- (car unread))) prev
10507                   (cons prev (1- (car unread))))
10508                 read))
10509         (setq prev (1+ (car unread)))
10510         (setq unread (cdr unread)))
10511       (when (<= prev (cdr active))
10512         (push (cons prev (cdr active)) read))
10513       (setq read (if (> (length read) 1) (nreverse read) read))
10514       (if compute
10515           read
10516         (save-excursion
10517           (let (setmarkundo)
10518             ;; Propagate the read marks to the backend.
10519             (when (gnus-check-backend-function 'request-set-mark group)
10520               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10521                     (add (gnus-remove-from-range read (gnus-info-read info))))
10522                 (when (or add del)
10523                   (unless (gnus-check-group group)
10524                     (error "Can't open server for %s" group))
10525                   (gnus-request-set-mark
10526                    group (delq nil (list (if add (list add 'add '(read)))
10527                                          (if del (list del 'del '(read))))))
10528                   (setq setmarkundo
10529                         `(gnus-request-set-mark
10530                           ,group
10531                           ',(delq nil (list
10532                                        (if del (list del 'add '(read)))
10533                                        (if add (list add 'del '(read))))))))))
10534             (set-buffer gnus-group-buffer)
10535             (gnus-undo-register
10536               `(progn
10537                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10538                  (gnus-info-set-read ',info ',(gnus-info-read info))
10539                  (gnus-get-unread-articles-in-group ',info
10540                                                     (gnus-active ,group))
10541                  (gnus-group-update-group ,group t)
10542                  ,setmarkundo))))
10543         ;; Enter this list into the group info.
10544         (gnus-info-set-read info read)
10545         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10546         (gnus-get-unread-articles-in-group info (gnus-active group))
10547         t))))
10548
10549 (defun gnus-offer-save-summaries ()
10550   "Offer to save all active summary buffers."
10551   (let (buffers)
10552     ;; Go through all buffers and find all summaries.
10553     (dolist (buffer (buffer-list))
10554       (when (and (setq buffer (buffer-name buffer))
10555                  (string-match "Summary" buffer)
10556                  (save-excursion
10557                    (set-buffer buffer)
10558                    ;; We check that this is, indeed, a summary buffer.
10559                    (and (eq major-mode 'gnus-summary-mode)
10560                         ;; Also make sure this isn't bogus.
10561                         gnus-newsgroup-prepared
10562                         ;; Also make sure that this isn't a
10563                         ;; dead summary buffer.
10564                         (not gnus-dead-summary-mode))))
10565         (push buffer buffers)))
10566     ;; Go through all these summary buffers and offer to save them.
10567     (when buffers
10568       (save-excursion
10569         (map-y-or-n-p
10570          "Update summary buffer %s? "
10571          (lambda (buf)
10572            (switch-to-buffer buf)
10573            (gnus-summary-exit))
10574          buffers)))))
10575
10576
10577 ;;; @ for mime-partial
10578 ;;;
10579
10580 (defun gnus-request-partial-message ()
10581   (save-excursion
10582     (let ((number (gnus-summary-article-number))
10583           (group gnus-newsgroup-name)
10584           (mother gnus-article-buffer))
10585       (set-buffer (get-buffer-create " *Partial Article*"))
10586       (erase-buffer)
10587       (setq mime-preview-buffer mother)
10588       (gnus-request-article-this-buffer number group)
10589       (mime-parse-buffer)
10590       )))
10591
10592 (autoload 'mime-combine-message/partial-pieces-automatically
10593   "mime-partial"
10594   "Internal method to combine message/partial messages automatically.")
10595
10596 (mime-add-condition
10597  'action '((type . message)(subtype . partial)
10598            (major-mode . gnus-original-article-mode)
10599            (method . mime-combine-message/partial-pieces-automatically)
10600            (summary-buffer-exp . gnus-summary-buffer)
10601            (request-partial-message-method . gnus-request-partial-message)
10602            ))
10603
10604
10605 ;;; @ for message/rfc822
10606 ;;;
10607
10608 (defun gnus-mime-extract-message/rfc822 (entity situation)
10609   (let (group article num cwin swin cur)
10610     (with-temp-buffer
10611       (mime-insert-entity-content entity)
10612       (setq group (or (cdr (assq 'group situation))
10613                       (completing-read "Group: "
10614                                        gnus-active-hashtb
10615                                        nil
10616                                        (gnus-read-active-file-p)
10617                                        gnus-newsgroup-name))
10618             article (gnus-request-accept-article group)))
10619     (when (and (consp article)
10620                (numberp (setq article (cdr article))))
10621       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10622             cwin (get-buffer-window (current-buffer) t))
10623       (save-window-excursion
10624         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10625             (select-window swin)
10626           (set-buffer gnus-summary-buffer))
10627         (setq cur gnus-current-article)
10628         (forward-line num)
10629         (let (gnus-show-threads)
10630           (gnus-summary-goto-subject article t))
10631         (gnus-summary-clear-mark-forward 1)
10632         (gnus-summary-goto-subject cur))
10633       (when (and cwin (window-frame cwin))
10634         (select-frame (window-frame cwin)))
10635       (when (boundp 'mime-acting-situation-to-override)
10636         (set-alist 'mime-acting-situation-to-override
10637                    'group
10638                    group)
10639         (set-alist 'mime-acting-situation-to-override
10640                    'after-method
10641                    `(progn
10642                       (save-current-buffer
10643                         (set-buffer gnus-group-buffer)
10644                         (gnus-activate-group ,group))
10645                       (gnus-summary-goto-article ,cur
10646                                                  gnus-show-all-headers)))
10647         (set-alist 'mime-acting-situation-to-override
10648                    'number num)))))
10649
10650 (mime-add-condition
10651  'action '((type . message)(subtype . rfc822)
10652            (major-mode . gnus-original-article-mode)
10653            (method . gnus-mime-extract-message/rfc822)
10654            (mode . "extract")
10655            ))
10656
10657 (mime-add-condition
10658  'action '((type . message)(subtype . news)
10659            (major-mode . gnus-original-article-mode)
10660            (method . gnus-mime-extract-message/rfc822)
10661            (mode . "extract")
10662            ))
10663
10664 (defun gnus-mime-extract-multipart (entity situation)
10665   (let ((children (mime-entity-children entity))
10666         mime-acting-situation-to-override
10667         f)
10668     (while children
10669       (mime-play-entity (car children)
10670                         (cons (assq 'mode situation)
10671                               mime-acting-situation-to-override))
10672       (setq children (cdr children)))
10673     (if (setq f (cdr (assq 'after-method
10674                            mime-acting-situation-to-override)))
10675         (eval f)
10676       )))
10677
10678 (mime-add-condition
10679  'action '((type . multipart)
10680            (method . gnus-mime-extract-multipart)
10681            (mode . "extract")
10682            )
10683  'with-default)
10684
10685
10686 ;;; @ end
10687 ;;;
10688
10689 (defun gnus-summary-setup-default-charset ()
10690   "Setup newsgroup default charset."
10691   (if (equal gnus-newsgroup-name "nndraft:drafts")
10692       (setq gnus-newsgroup-charset nil)
10693     (let* ((ignored-charsets
10694             (or gnus-newsgroup-ephemeral-ignored-charsets
10695                 (append
10696                  (and gnus-newsgroup-name
10697                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10698                  gnus-newsgroup-ignored-charsets))))
10699       (setq gnus-newsgroup-charset
10700             (or gnus-newsgroup-ephemeral-charset
10701                 (and gnus-newsgroup-name
10702                      (gnus-parameter-charset gnus-newsgroup-name))
10703                 gnus-default-charset))
10704       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10705            ignored-charsets))))
10706
10707 ;;;
10708 ;;; Mime Commands
10709 ;;;
10710
10711 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10712   "Display the current article buffer fully MIME-buttonized.
10713 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10714 treated as multipart/mixed."
10715   (interactive "P")
10716   (require 'gnus-art)
10717   (let ((gnus-unbuttonized-mime-types nil)
10718         (gnus-mime-display-multipart-as-mixed show-all-parts))
10719     (gnus-summary-show-article)))
10720
10721 (defun gnus-summary-repair-multipart (article)
10722   "Add a Content-Type header to a multipart article without one."
10723   (interactive (list (gnus-summary-article-number)))
10724   (gnus-with-article article
10725     (message-narrow-to-head)
10726     (message-remove-header "Mime-Version")
10727     (goto-char (point-max))
10728     (insert "Mime-Version: 1.0\n")
10729     (widen)
10730     (when (search-forward "\n--" nil t)
10731       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10732         (message-narrow-to-head)
10733         (message-remove-header "Content-Type")
10734         (goto-char (point-max))
10735         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10736                         separator))
10737         (widen))))
10738   (let (gnus-mark-article-hook)
10739     (gnus-summary-select-article t t nil article)))
10740
10741 (defun gnus-summary-toggle-display-buttonized ()
10742   "Toggle the buttonizing of the article buffer."
10743   (interactive)
10744   (require 'gnus-art)
10745   (if (setq gnus-inhibit-mime-unbuttonizing
10746             (not gnus-inhibit-mime-unbuttonizing))
10747       (let ((gnus-unbuttonized-mime-types nil))
10748         (gnus-summary-show-article))
10749     (gnus-summary-show-article)))
10750
10751 ;;;
10752 ;;; Intelli-mouse commmands
10753 ;;;
10754
10755 (defun gnus-wheel-summary-scroll (event)
10756   (interactive "e")
10757   (let ((amount (if (memq 'shift (event-modifiers event))
10758                     (car gnus-wheel-scroll-amount)
10759                   (cdr gnus-wheel-scroll-amount)))
10760         (direction (- (* (static-if (featurep 'xemacs)
10761                              (event-button event)
10762                            (cond ((eq 'mouse-4 (event-basic-type event))
10763                                   4)
10764                                  ((eq 'mouse-5 (event-basic-type event))
10765                                   5)))
10766                          2) 9))
10767         edge)
10768     (gnus-summary-scroll-up (* amount direction))
10769     (when (gnus-eval-in-buffer-window gnus-article-buffer
10770             (save-restriction
10771               (widen)
10772               (and (if (< 0 direction)
10773                        (gnus-article-next-page 0)
10774                      (gnus-article-prev-page 0)
10775                      (bobp))
10776                    (if (setq edge (get-text-property
10777                                    (point-min) 'gnus-wheel-edge))
10778                        (setq edge (* edge direction))
10779                      (setq edge -1))
10780                    (or (plusp edge)
10781                        (let ((buffer-read-only nil)
10782                              (inhibit-read-only t))
10783                          (put-text-property (point-min) (point-max)
10784                                             'gnus-wheel-edge direction)
10785                          nil))
10786                    (or (> edge gnus-wheel-edge-resistance)
10787                        (let ((buffer-read-only nil)
10788                              (inhibit-read-only t))
10789                          (put-text-property (point-min) (point-max)
10790                                             'gnus-wheel-edge
10791                                             (* (1+ edge) direction))
10792                          nil))
10793                    (eq last-command 'gnus-wheel-summary-scroll))))
10794       (gnus-summary-next-article nil nil (minusp direction)))))
10795
10796 (defun gnus-wheel-install ()
10797   "Enable mouse wheel support on summary window."
10798   (when gnus-use-wheel
10799     (let ((keys
10800            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10801       (dolist (key keys)
10802         (define-key gnus-summary-mode-map key
10803           'gnus-wheel-summary-scroll)))))
10804
10805 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10806
10807 ;;;
10808 ;;; Traditional PGP commmands
10809 ;;;
10810
10811 (defun gnus-summary-decrypt-article (&optional force)
10812   "Decrypt the current article in traditional PGP way.
10813 This will have permanent effect only in mail groups.
10814 If FORCE is non-nil, allow editing of articles even in read-only
10815 groups."
10816   (interactive "P")
10817   (gnus-summary-select-article t)
10818   (gnus-eval-in-buffer-window gnus-article-buffer
10819     (save-excursion
10820       (save-restriction
10821         (widen)
10822         (goto-char (point-min))
10823         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10824           (error "Not a traditional PGP message!"))
10825         (let ((armor-start (match-beginning 0)))
10826           (if (and (pgg-decrypt-region armor-start (point-max))
10827                    (or force (not (gnus-group-read-only-p))))
10828               (let ((inhibit-read-only t)
10829                     buffer-read-only)
10830                 (delete-region armor-start
10831                                (progn
10832                                  (re-search-forward "^-+END PGP" nil t)
10833                                  (beginning-of-line 2)
10834                                  (point)))
10835                 (insert-buffer-substring pgg-output-buffer))))))))
10836
10837 (defun gnus-summary-verify-article ()
10838   "Verify the current article in traditional PGP way."
10839   (interactive)
10840   (save-excursion
10841     (set-buffer gnus-original-article-buffer)
10842     (goto-char (point-min))
10843     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10844       (error "Not a traditional PGP message!"))
10845     (re-search-forward "^-+END PGP" nil t)
10846     (beginning-of-line 2)
10847     (call-interactively (function pgg-verify-region))))
10848
10849 ;;;
10850 ;;; Generic summary marking commands
10851 ;;;
10852
10853 (defvar gnus-summary-marking-alist
10854   '((read gnus-del-mark "d")
10855     (unread gnus-unread-mark "u")
10856     (ticked gnus-ticked-mark "!")
10857     (dormant gnus-dormant-mark "?")
10858     (expirable gnus-expirable-mark "e"))
10859   "An alist of names/marks/keystrokes.")
10860
10861 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10862 (defvar gnus-summary-mark-map)
10863
10864 (defun gnus-summary-make-all-marking-commands ()
10865   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10866   (dolist (elem gnus-summary-marking-alist)
10867     (apply 'gnus-summary-make-marking-command elem)))
10868
10869 (defun gnus-summary-make-marking-command (name mark keystroke)
10870   (let ((map (make-sparse-keymap)))
10871     (define-key gnus-summary-generic-mark-map keystroke map)
10872     (dolist (lway `((next "next" next nil "n")
10873                     (next-unread "next unread" next t "N")
10874                     (prev "previous" prev nil "p")
10875                     (prev-unread "previous unread" prev t "P")
10876                     (nomove "" nil nil ,keystroke)))
10877       (let ((func (gnus-summary-make-marking-command-1
10878                    mark (car lway) lway name)))
10879         (setq func (eval func))
10880         (define-key map (nth 4 lway) func)))))
10881
10882 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10883   `(defun ,(intern
10884             (format "gnus-summary-put-mark-as-%s%s"
10885                     name (if (eq way 'nomove)
10886                              ""
10887                            (concat "-" (symbol-name way)))))
10888      (n)
10889      ,(format
10890        "Mark the current article as %s%s.
10891 If N, the prefix, then repeat N times.
10892 If N is negative, move in reverse order.
10893 The difference between N and the actual number of articles marked is
10894 returned."
10895        name (car (cdr lway)))
10896      (interactive "p")
10897      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10898
10899 (defun gnus-summary-generic-mark (n mark move unread)
10900   "Mark N articles with MARK."
10901   (unless (eq major-mode 'gnus-summary-mode)
10902     (error "This command can only be used in the summary buffer"))
10903   (gnus-summary-show-thread)
10904   (let ((nummove
10905          (cond
10906           ((eq move 'next) 1)
10907           ((eq move 'prev) -1)
10908           (t 0))))
10909     (if (zerop nummove)
10910         (setq n 1)
10911       (when (< n 0)
10912         (setq n (abs n)
10913               nummove (* -1 nummove))))
10914     (while (and (> n 0)
10915                 (gnus-summary-mark-article nil mark)
10916                 (zerop (gnus-summary-next-subject nummove unread t)))
10917       (setq n (1- n)))
10918     (when (/= 0 n)
10919       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10920     (gnus-summary-recenter)
10921     (gnus-summary-position-point)
10922     (gnus-set-mode-line 'summary)
10923     n))
10924
10925 (defun gnus-summary-insert-articles (articles)
10926   (when (setq articles
10927               (gnus-set-difference articles
10928                                    (mapcar (lambda (h) (mail-header-number h))
10929                                            gnus-newsgroup-headers)))
10930     (setq gnus-newsgroup-headers
10931           (merge 'list
10932                  gnus-newsgroup-headers
10933                  (gnus-fetch-headers articles)
10934                  'gnus-article-sort-by-number))
10935     ;; Suppress duplicates?
10936     (when gnus-suppress-duplicates
10937       (gnus-dup-suppress-articles))
10938
10939     ;; We might want to build some more threads first.
10940     (when (and gnus-fetch-old-headers
10941                (eq gnus-headers-retrieved-by 'nov))
10942       (if (eq gnus-fetch-old-headers 'invisible)
10943           (gnus-build-all-threads)
10944         (gnus-build-old-threads)))
10945     ;; Let the Gnus agent mark articles as read.
10946     (when gnus-agent
10947       (gnus-agent-get-undownloaded-list))
10948     ;; Remove list identifiers from subject
10949     (when gnus-list-identifiers
10950       (gnus-summary-remove-list-identifiers))
10951     ;; First and last article in this newsgroup.
10952     (when gnus-newsgroup-headers
10953       (setq gnus-newsgroup-begin
10954             (mail-header-number (car gnus-newsgroup-headers))
10955             gnus-newsgroup-end
10956             (mail-header-number
10957              (gnus-last-element gnus-newsgroup-headers))))
10958     (when gnus-use-scoring
10959       (gnus-possibly-score-headers))))
10960
10961 (defun gnus-summary-insert-old-articles (&optional all)
10962   "Insert all old articles in this group.
10963 If ALL is non-nil, already read articles become readable.
10964 If ALL is a number, fetch this number of articles."
10965   (interactive "P")
10966   (prog1
10967       (let ((old (mapcar 'car gnus-newsgroup-data))
10968             (i (car gnus-newsgroup-active))
10969             older len)
10970         (while (<= i (cdr gnus-newsgroup-active))
10971           (or (memq i old) (push i older))
10972           (incf i))
10973         (setq len (length older))
10974         (cond
10975          ((null older) nil)
10976          ((numberp all)
10977           (if (< all len)
10978               (setq older (subseq older 0 all))))
10979          (all nil)
10980          (t
10981           (if (and (numberp gnus-large-newsgroup)
10982                    (> len gnus-large-newsgroup))
10983               (let ((input
10984                      (read-string
10985                       (format
10986                        "How many articles from %s (default %d): "
10987                        (gnus-limit-string
10988                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10989                        len))))
10990                 (unless (string-match "^[ \t]*$" input)
10991                   (setq all (string-to-number input))
10992                   (if (< all len)
10993                       (setq older (subseq older 0 all))))))))
10994         (if (not older)
10995             (message "No old news.")
10996           (gnus-summary-insert-articles older)
10997           (gnus-summary-limit (gnus-union older old))))
10998     (gnus-summary-position-point)))
10999
11000 (defun gnus-summary-insert-new-articles ()
11001   "Insert all new articles in this group."
11002   (interactive)
11003   (prog1
11004       (let ((old (mapcar 'car gnus-newsgroup-data))
11005             (old-active gnus-newsgroup-active)
11006             (nnmail-fetched-sources (list t))
11007             i new)
11008         (setq gnus-newsgroup-active
11009               (gnus-activate-group gnus-newsgroup-name 'scan))
11010         (setq i (1+ (cdr old-active)))
11011         (while (<= i (cdr gnus-newsgroup-active))
11012           (push i new)
11013           (incf i))
11014         (if (not new)
11015             (message "No gnus is bad news.")
11016           (setq new (nreverse new))
11017           (gnus-summary-insert-articles new)
11018           (setq gnus-newsgroup-unreads
11019                 (append gnus-newsgroup-unreads new))
11020           (gnus-summary-limit (gnus-union old new))))
11021     (gnus-summary-position-point)))
11022
11023 (gnus-summary-make-all-marking-commands)
11024
11025 (gnus-ems-redefine)
11026
11027 (provide 'gnus-sum)
11028
11029 (run-hooks 'gnus-sum-load-hook)
11030
11031 ;;; gnus-sum.el ends here