7a9c82ee9ce42fb68711bca9987e79de32b1383c
[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 'gnus-cache-articles-in-group "gnus-cache")
52   (autoload 'pgg-decrypt-region "pgg" nil t)
53   (autoload 'pgg-verify-region "pgg" nil t))
54
55 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
56 (autoload 'gnus-cache-write-active "gnus-cache")
57 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
58 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
59 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
60 (autoload 'mm-uu-dissect "mm-uu")
61
62 (defcustom gnus-kill-summary-on-exit t
63   "*If non-nil, kill the summary buffer when you exit from it.
64 If nil, the summary will become a \"*Dead Summary*\" buffer, and
65 it will be killed sometime later."
66   :group 'gnus-summary-exit
67   :type 'boolean)
68
69 (defcustom gnus-fetch-old-headers nil
70   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
71 If an unread article in the group refers to an older, already read (or
72 just marked as read) article, the old article will not normally be
73 displayed in the Summary buffer.  If this variable is non-nil, Gnus
74 will attempt to grab the headers to the old articles, and thereby
75 build complete threads.  If it has the value `some', only enough
76 headers to connect otherwise loose threads will be displayed.  This
77 variable can also be a number.  In that case, no more than that number
78 of old headers will be fetched.  If it has the value `invisible', all
79 old headers will be fetched, but none will be displayed.
80
81 The server has to support NOV for any of this to work."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const some)
85                  number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-refer-thread-limit 200
89   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
90 If t, fetch all the available old headers."
91   :group 'gnus-thread
92   :type '(choice number
93                  (sexp :menu-tag "other" t)))
94
95 (defcustom gnus-summary-make-false-root 'adopt
96   "*nil means that Gnus won't gather loose threads.
97 If the root of a thread has expired or been read in a previous
98 session, the information necessary to build a complete thread has been
99 lost.  Instead of having many small sub-threads from this original thread
100 scattered all over the summary buffer, Gnus can gather them.
101
102 If non-nil, Gnus will try to gather all loose sub-threads from an
103 original thread into one large thread.
104
105 If this variable is non-nil, it should be one of `none', `adopt',
106 `dummy' or `empty'.
107
108 If this variable is `none', Gnus will not make a false root, but just
109 present the sub-threads after another.
110 If this variable is `dummy', Gnus will create a dummy root that will
111 have all the sub-threads as children.
112 If this variable is `adopt', Gnus will make one of the \"children\"
113 the parent and mark all the step-children as such.
114 If this variable is `empty', the \"children\" are printed with empty
115 subject fields.  (Or rather, they will be printed with a string
116 given by the `gnus-summary-same-subject' variable.)"
117   :group 'gnus-thread
118   :type '(choice (const :tag "off" nil)
119                  (const none)
120                  (const dummy)
121                  (const adopt)
122                  (const empty)))
123
124 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
125   "*A regexp to match subjects to be excluded from loose thread gathering.
126 As loose thread gathering is done on subjects only, that means that
127 there can be many false gatherings performed.  By rooting out certain
128 common subjects, gathering might become saner."
129   :group 'gnus-thread
130   :type 'regexp)
131
132 (defcustom gnus-summary-gather-subject-limit nil
133   "*Maximum length of subject comparisons when gathering loose threads.
134 Use nil to compare full subjects.  Setting this variable to a low
135 number will help gather threads that have been corrupted by
136 newsreaders chopping off subject lines, but it might also mean that
137 unrelated articles that have subject that happen to begin with the
138 same few characters will be incorrectly gathered.
139
140 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
141 comparing subjects."
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const fuzzy)
145                  (sexp :menu-tag "on" t)))
146
147 (defcustom gnus-simplify-subject-functions nil
148   "List of functions taking a string argument that simplify subjects.
149 The functions are applied recursively.
150
151 Useful functions to put in this list include: `gnus-simplify-subject-re',
152 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 (they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-zcore-fuzz 0
217   "*Fuzziness factor for the zcore in the summary buffer.
218 Articles with scores closer than this to `gnus-summary-default-score'
219 will not be marked."
220   :group 'gnus-summary-format
221   :type 'integer)
222
223 (defcustom gnus-simplify-subject-fuzzy-regexp nil
224   "*Strings to be removed when doing fuzzy matches.
225 This can either be a regular expression or list of regular expressions
226 that will be removed from subject strings if fuzzy subject
227 simplification is selected."
228   :group 'gnus-thread
229   :type '(repeat regexp))
230
231 (defcustom gnus-show-threads t
232   "*If non-nil, display threads in summary mode."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-subtree nil
237   "*If non-nil, hide all threads initially.
238 If threads are hidden, you have to run the command
239 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
240 to expose hidden threads."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-killed t
245   "*If non-nil, hide killed threads automatically."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-ignore-subject t
250   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
251 If nil, articles that have different subjects from their parents will
252 start separate threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-operation-ignore-subject t
257   "*If non-nil, subjects will be ignored when doing thread commands.
258 This affects commands like `gnus-summary-kill-thread' and
259 `gnus-summary-lower-thread'.
260
261 If this variable is nil, articles in the same thread with different
262 subjects will not be included in the operation in question.  If this
263 variable is `fuzzy', only articles that have subjects that are fuzzily
264 equal will be included."
265   :group 'gnus-thread
266   :type '(choice (const :tag "off" nil)
267                  (const fuzzy)
268                  (sexp :tag "on" t)))
269
270 (defcustom gnus-thread-indent-level 4
271   "*Number that says how much each sub-thread should be indented."
272   :group 'gnus-thread
273   :type 'integer)
274
275 (defcustom gnus-auto-extend-newsgroup t
276   "*If non-nil, extend newsgroup forward and backward when requested."
277   :group 'gnus-summary-choose
278   :type 'boolean)
279
280 (defcustom gnus-auto-select-first t
281   "*If nil, don't select the first unread article when entering a group.
282 If this variable is `best', select the highest-scored unread article
283 in the group.  If t, select the first unread article.
284
285 This variable can also be a function to place point on a likely
286 subject line.  Useful values include `gnus-summary-first-unread-subject',
287 `gnus-summary-first-unread-article' and
288 `gnus-summary-best-unread-article'.
289
290 If you want to prevent automatic selection of the first unread article
291 in some newsgroups, set the variable to nil in
292 `gnus-select-group-hook'."
293   :group 'gnus-group-select
294   :type '(choice (const :tag "none" nil)
295                  (const best)
296                  (sexp :menu-tag "first" t)
297                  (function-item gnus-summary-first-unread-subject)
298                  (function-item gnus-summary-first-unread-article)
299                  (function-item gnus-summary-best-unread-article)))
300
301 (defcustom gnus-dont-select-after-jump-to-other-group nil
302   "If non-nil, don't select the first unread article after entering the
303 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
304 it is depend on the value of `gnus-auto-select-first' whether to select
305 or not."
306   :group 'gnus-group-select
307   :type 'boolean)
308
309 (defcustom gnus-auto-select-next t
310   "*If non-nil, offer to go to the next group from the end of the previous.
311 If the value is t and the next newsgroup is empty, Gnus will exit
312 summary mode and go back to group mode.  If the value is neither nil
313 nor t, Gnus will select the following unread newsgroup.  In
314 particular, if the value is the symbol `quietly', the next unread
315 newsgroup will be selected without any confirmation, and if it is
316 `almost-quietly', the next group will be selected without any
317 confirmation if you are located on the last article in the group.
318 Finally, if this variable is `slightly-quietly', the `Z n' command
319 will go to the next group without confirmation."
320   :group 'gnus-summary-maneuvering
321   :type '(choice (const :tag "off" nil)
322                  (const quietly)
323                  (const almost-quietly)
324                  (const slightly-quietly)
325                  (sexp :menu-tag "on" t)))
326
327 (defcustom gnus-auto-select-same nil
328   "*If non-nil, select the next article with the same subject.
329 If there are no more articles with the same subject, go to
330 the first unread article."
331   :group 'gnus-summary-maneuvering
332   :type 'boolean)
333
334 (defcustom gnus-summary-check-current nil
335   "*If non-nil, consider the current article when moving.
336 The \"unread\" movement commands will stay on the same line if the
337 current article is unread."
338   :group 'gnus-summary-maneuvering
339   :type 'boolean)
340
341 (defcustom gnus-auto-center-summary t
342   "*If non-nil, always center the current summary buffer.
343 In particular, if `vertical' do only vertical recentering.  If non-nil
344 and non-`vertical', do both horizontal and vertical recentering."
345   :group 'gnus-summary-maneuvering
346   :type '(choice (const :tag "none" nil)
347                  (const vertical)
348                  (integer :tag "height")
349                  (sexp :menu-tag "both" t)))
350
351 (defcustom gnus-show-all-headers nil
352   "*If non-nil, don't hide any headers."
353   :group 'gnus-article-hiding
354   :group 'gnus-article-headers
355   :type 'boolean)
356
357 (defcustom gnus-summary-ignore-duplicates nil
358   "*If non-nil, ignore articles with identical Message-ID headers."
359   :group 'gnus-summary
360   :type 'boolean)
361
362 (defcustom gnus-single-article-buffer t
363   "*If non-nil, display all articles in the same buffer.
364 If nil, each group will get its own article buffer."
365   :group 'gnus-article-various
366   :type 'boolean)
367
368 (defcustom gnus-break-pages t
369   "*If non-nil, do page breaking on articles.
370 The page delimiter is specified by the `gnus-page-delimiter'
371 variable."
372   :group 'gnus-article-various
373   :type 'boolean)
374
375 (defcustom gnus-show-mime t
376   "*If non-nil, do mime processing of articles.
377 The articles will simply be fed to the function given by
378 `gnus-article-display-method-for-mime'."
379   :group 'gnus-article-mime
380   :type 'boolean)
381
382 (defcustom gnus-move-split-methods nil
383   "*Variable used to suggest where articles are to be moved to.
384 It uses the same syntax as the `gnus-split-methods' variable.
385 However, whereas `gnus-split-methods' specifies file names as targets,
386 this variable specifies group names."
387   :group 'gnus-summary-mail
388   :type '(repeat (choice (list :value (fun) function)
389                          (cons :value ("" "") regexp (repeat string))
390                          (sexp :value nil))))
391
392 (defcustom gnus-unread-mark ?  ;Whitespace
393   "*Mark used for unread articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-ticked-mark ?!
398   "*Mark used for ticked articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-dormant-mark ??
403   "*Mark used for dormant articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-del-mark ?r
408   "*Mark used for del'd articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-read-mark ?R
413   "*Mark used for read articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-expirable-mark ?E
418   "*Mark used for expirable articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-killed-mark ?K
423   "*Mark used for killed articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-souped-mark ?F
428   "*Mark used for killed articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-kill-file-mark ?X
433   "*Mark used for articles killed by kill files."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-low-score-mark ?Y
438   "*Mark used for articles with a low score."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-catchup-mark ?C
443   "*Mark used for articles that are caught up."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-replied-mark ?A
448   "*Mark used for articles that have been replied to."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-cached-mark ?*
453   "*Mark used for articles that are in the cache."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-saved-mark ?S
458   "*Mark used for articles that have been saved to."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-no-mark ?  ;Whitespace
463   "*Mark used for articles that have no other secondary mark."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-ancient-mark ?O
468   "*Mark used for ancient articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-sparse-mark ?Q
473   "*Mark used for sparsely reffed articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-canceled-mark ?G
478   "*Mark used for canceled articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-duplicate-mark ?M
483   "*Mark used for duplicate articles."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-undownloaded-mark ?@
488   "*Mark used for articles that weren't downloaded."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-downloadable-mark ?%
493   "*Mark used for articles that are to be downloaded."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-unsendable-mark ?=
498   "*Mark used for articles that won't be sent."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-score-over-mark ?+
503   "*Score mark used for articles with high scores."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-score-below-mark ?-
508   "*Score mark used for articles with low scores."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-empty-thread-mark ?  ;Whitespace
513   "*There is no thread under the article."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-not-empty-thread-mark ?=
518   "*There is a thread under the article."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-view-pseudo-asynchronously nil
523   "*If non-nil, Gnus will view pseudo-articles asynchronously."
524   :group 'gnus-extract-view
525   :type 'boolean)
526
527 (defcustom gnus-auto-expirable-marks
528   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
529         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
530         gnus-souped-mark gnus-duplicate-mark)
531   "*The list of marks converted into expiration if a group is auto-expirable."
532   :version "21.1"
533   :group 'gnus-summary
534   :type '(repeat character))
535
536 (defcustom gnus-inhibit-user-auto-expire t
537   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
538   :version "21.1"
539   :group 'gnus-summary
540   :type 'boolean)
541
542 (defcustom gnus-view-pseudos nil
543   "*If `automatic', pseudo-articles will be viewed automatically.
544 If `not-confirm', pseudos will be viewed automatically, and the user
545 will not be asked to confirm the command."
546   :group 'gnus-extract-view
547   :type '(choice (const :tag "off" nil)
548                  (const automatic)
549                  (const not-confirm)))
550
551 (defcustom gnus-view-pseudos-separately t
552   "*If non-nil, one pseudo-article will be created for each file to be viewed.
553 If nil, all files that use the same viewing command will be given as a
554 list of parameters to that command."
555   :group 'gnus-extract-view
556   :type 'boolean)
557
558 (defcustom gnus-insert-pseudo-articles t
559   "*If non-nil, insert pseudo-articles when decoding articles."
560   :group 'gnus-extract-view
561   :type 'boolean)
562
563 (defcustom gnus-summary-dummy-line-format
564   "  %(:                          :%) %S\n"
565   "*The format specification for the dummy roots in the summary buffer.
566 It works along the same lines as a normal formatting string,
567 with some simple extensions.
568
569 %S  The subject"
570   :group 'gnus-threading
571   :type 'string)
572
573 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
574   "*The format specification for the summary mode line.
575 It works along the same lines as a normal formatting string,
576 with some simple extensions:
577
578 %G  Group name
579 %p  Unprefixed group name
580 %A  Current article number
581 %z  Current article score
582 %V  Gnus version
583 %U  Number of unread articles in the group
584 %e  Number of unselected articles in the group
585 %Z  A string with unread/unselected article counts
586 %g  Shortish group name
587 %S  Subject of the current article
588 %u  User-defined spec
589 %s  Current score file name
590 %d  Number of dormant articles
591 %r  Number of articles that have been marked as read in this session
592 %E  Number of articles expunged by the score files"
593   :group 'gnus-summary-format
594   :type 'string)
595
596 (defcustom gnus-list-identifiers nil
597   "Regexp that matches list identifiers to be removed from subject.
598 This can also be a list of regexps."
599   :version "21.1"
600   :group 'gnus-summary-format
601   :group 'gnus-article-hiding
602   :type '(choice (const :tag "none" nil)
603                  (regexp :value ".*")
604                  (repeat :value (".*") regexp)))
605
606 (defcustom gnus-summary-mark-below 0
607   "*Mark all articles with a score below this variable as read.
608 This variable is local to each summary buffer and usually set by the
609 score file."
610   :group 'gnus-score-default
611   :type 'integer)
612
613 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
614   "*List of functions used for sorting articles in the summary buffer.
615
616 Each function takes two articles and returns non-nil if the first
617 article should be sorted before the other.  If you use more than one
618 function, the primary sort function should be the last.  You should
619 probably always include `gnus-article-sort-by-number' in the list of
620 sorting functions -- preferably first.  Also note that sorting by date
621 is often much slower than sorting by number, and the sorting order is
622 very similar.  (Sorting by date means sorting by the time the message
623 was sent, sorting by number means sorting by arrival time.)
624
625 Ready-made functions include `gnus-article-sort-by-number',
626 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
627 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
628
629 When threading is turned on, the variable `gnus-thread-sort-functions'
630 controls how articles are sorted."
631   :group 'gnus-summary-sort
632   :type '(repeat (choice (function-item gnus-article-sort-by-number)
633                          (function-item gnus-article-sort-by-author)
634                          (function-item gnus-article-sort-by-subject)
635                          (function-item gnus-article-sort-by-date)
636                          (function-item gnus-article-sort-by-score)
637                          (function :tag "other"))))
638
639 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
640   "*List of functions used for sorting threads in the summary buffer.
641 By default, threads are sorted by article number.
642
643 Each function takes two threads and returns non-nil if the first
644 thread should be sorted before the other.  If you use more than one
645 function, the primary sort function should be the last.  You should
646 probably always include `gnus-thread-sort-by-number' in the list of
647 sorting functions -- preferably first.  Also note that sorting by date
648 is often much slower than sorting by number, and the sorting order is
649 very similar.  (Sorting by date means sorting by the time the message
650 was sent, sorting by number means sorting by arrival time.)
651
652 Ready-made functions include `gnus-thread-sort-by-number',
653 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
654 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
655 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
656
657 When threading is turned off, the variable
658 `gnus-article-sort-functions' controls how articles are sorted."
659   :group 'gnus-summary-sort
660   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
661                          (function-item gnus-thread-sort-by-author)
662                          (function-item gnus-thread-sort-by-subject)
663                          (function-item gnus-thread-sort-by-date)
664                          (function-item gnus-thread-sort-by-score)
665                          (function-item gnus-thread-sort-by-total-score)
666                          (function :tag "other"))))
667
668 (defcustom gnus-thread-score-function '+
669   "*Function used for calculating the total score of a thread.
670
671 The function is called with the scores of the article and each
672 subthread and should then return the score of the thread.
673
674 Some functions you can use are `+', `max', or `min'."
675   :group 'gnus-summary-sort
676   :type 'function)
677
678 (defcustom gnus-summary-expunge-below nil
679   "All articles that have a score less than this variable will be expunged.
680 This variable is local to the summary buffers."
681   :group 'gnus-score-default
682   :type '(choice (const :tag "off" nil)
683                  integer))
684
685 (defcustom gnus-thread-expunge-below nil
686   "All threads that have a total score less than this variable will be expunged.
687 See `gnus-thread-score-function' for en explanation of what a
688 \"thread score\" is.
689
690 This variable is local to the summary buffers."
691   :group 'gnus-threading
692   :group 'gnus-score-default
693   :type '(choice (const :tag "off" nil)
694                  integer))
695
696 (defcustom gnus-summary-mode-hook nil
697   "*A hook for Gnus summary mode.
698 This hook is run before any variables are set in the summary buffer."
699   :options '(turn-on-gnus-mailing-list-mode)
700   :group 'gnus-summary-various
701   :type 'hook)
702
703 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
704 (when (featurep 'xemacs)
705   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
706   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
707   (add-hook 'gnus-summary-mode-hook
708             'gnus-xmas-switch-horizontal-scrollbar-off))
709
710 (defcustom gnus-summary-menu-hook nil
711   "*Hook run after the creation of the summary mode menu."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-summary-exit-hook nil
716   "*A hook called on exit from the summary buffer.
717 It will be called with point in the group buffer."
718   :group 'gnus-summary-exit
719   :type 'hook)
720
721 (defcustom gnus-summary-prepare-hook nil
722   "*A hook called after the summary buffer has been generated.
723 If you want to modify the summary buffer, you can use this hook."
724   :group 'gnus-summary-various
725   :type 'hook)
726
727 (defcustom gnus-summary-prepared-hook nil
728   "*A hook called as the last thing after the summary buffer has been generated."
729   :group 'gnus-summary-various
730   :type 'hook)
731
732 (defcustom gnus-summary-generate-hook nil
733   "*A hook run just before generating the summary buffer.
734 This hook is commonly used to customize threading variables and the
735 like."
736   :group 'gnus-summary-various
737   :type 'hook)
738
739 (defcustom gnus-select-group-hook nil
740   "*A hook called when a newsgroup is selected.
741
742 If you'd like to simplify subjects like the
743 `gnus-summary-next-same-subject' command does, you can use the
744 following hook:
745
746  (setq gnus-select-group-hook
747       (list
748         (lambda ()
749           (mapcar (lambda (header)
750                      (mail-header-set-subject
751                       header
752                       (gnus-simplify-subject
753                        (mail-header-subject header) 're-only)))
754                   gnus-newsgroup-headers))))"
755   :group 'gnus-group-select
756   :type 'hook)
757
758 (defcustom gnus-select-article-hook nil
759   "*A hook called when an article is selected."
760   :group 'gnus-summary-choose
761   :type 'hook)
762
763 (defcustom gnus-visual-mark-article-hook
764   (list 'gnus-highlight-selected-summary)
765   "*Hook run after selecting an article in the summary buffer.
766 It is meant to be used for highlighting the article in some way.  It
767 is not run if `gnus-visual' is nil."
768   :group 'gnus-summary-visual
769   :type 'hook)
770
771 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
772   "*A hook called before parsing the headers."
773   :group 'gnus-various
774   :type 'hook)
775
776 (defcustom gnus-exit-group-hook nil
777   "*A hook called when exiting summary mode.
778 This hook is not called from the non-updating exit commands like `Q'."
779   :group 'gnus-various
780   :type 'hook)
781
782 (defcustom gnus-summary-update-hook
783   (list 'gnus-summary-highlight-line)
784   "*A hook called when a summary line is changed.
785 The hook will not be called if `gnus-visual' is nil.
786
787 The default function `gnus-summary-highlight-line' will
788 highlight the line according to the `gnus-summary-highlight'
789 variable."
790   :group 'gnus-summary-visual
791   :type 'hook)
792
793 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
794   "*A hook called when an article is selected for the first time.
795 The hook is intended to mark an article as read (or unread)
796 automatically when it is selected."
797   :group 'gnus-summary-choose
798   :type 'hook)
799
800 (defcustom gnus-group-no-more-groups-hook nil
801   "*A hook run when returning to group mode having no more (unread) groups."
802   :group 'gnus-group-select
803   :type 'hook)
804
805 (defcustom gnus-ps-print-hook nil
806   "*A hook run before ps-printing something from Gnus."
807   :group 'gnus-summary
808   :type 'hook)
809
810 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
811   "Face used for highlighting the current article in the summary buffer."
812   :group 'gnus-summary-visual
813   :type 'face)
814
815 (defcustom gnus-summary-highlight
816   '(((= mark gnus-canceled-mark)
817      . gnus-summary-cancelled-face)
818     ((and (> score default)
819           (or (= mark gnus-dormant-mark)
820               (= mark gnus-ticked-mark)))
821      . gnus-summary-high-ticked-face)
822     ((and (< score default)
823           (or (= mark gnus-dormant-mark)
824               (= mark gnus-ticked-mark)))
825      . gnus-summary-low-ticked-face)
826     ((or (= mark gnus-dormant-mark)
827          (= mark gnus-ticked-mark))
828      . gnus-summary-normal-ticked-face)
829     ((and (> score default) (= mark gnus-ancient-mark))
830      . gnus-summary-high-ancient-face)
831     ((and (< score default) (= mark gnus-ancient-mark))
832      . gnus-summary-low-ancient-face)
833     ((= mark gnus-ancient-mark)
834      . gnus-summary-normal-ancient-face)
835     ((and (> score default) (= mark gnus-unread-mark))
836      . gnus-summary-high-unread-face)
837     ((and (< score default) (= mark gnus-unread-mark))
838      . gnus-summary-low-unread-face)
839     ((and (memq article gnus-newsgroup-incorporated)
840           (= mark gnus-unread-mark))
841      . gnus-summary-incorporated-face)
842     ((= mark gnus-unread-mark)
843      . gnus-summary-normal-unread-face)
844     ((and (> score default) (memq mark (list gnus-downloadable-mark
845                                              gnus-undownloaded-mark)))
846      . gnus-summary-high-unread-face)
847     ((and (< score default) (memq mark (list gnus-downloadable-mark
848                                              gnus-undownloaded-mark)))
849      . gnus-summary-low-unread-face)
850     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
851      . gnus-summary-normal-unread-face)
852     ((> score default)
853      . gnus-summary-high-read-face)
854     ((< score default)
855      . gnus-summary-low-read-face)
856     (t
857      . gnus-summary-normal-read-face))
858   "*Controls the highlighting of summary buffer lines.
859
860 A list of (FORM . FACE) pairs.  When deciding how a a particular
861 summary line should be displayed, each form is evaluated.  The content
862 of the face field after the first true form is used.  You can change
863 how those summary lines are displayed, by editing the face field.
864
865 You can use the following variables in the FORM field.
866
867 score:   The articles score
868 default: The default article score.
869 below:   The score below which articles are automatically marked as read.
870 mark:    The articles mark."
871   :group 'gnus-summary-visual
872   :type '(repeat (cons (sexp :tag "Form" nil)
873                        face)))
874
875 (defcustom gnus-alter-header-function nil
876   "Function called to allow alteration of article header structures.
877 The function is called with one parameter, the article header vector,
878 which it may alter in any way.")
879
880 (defvar gnus-decode-encoded-word-function
881   (mime-find-field-decoder 'From 'nov)
882   "Variable that says which function should be used to decode a string with encoded words.")
883
884 (defcustom gnus-extra-headers nil
885   "*Extra headers to parse."
886   :version "21.1"
887   :group 'gnus-summary
888   :type '(repeat symbol))
889
890 (defcustom gnus-ignored-from-addresses
891   (and user-mail-address (regexp-quote user-mail-address))
892   "*Regexp of From headers that may be suppressed in favor of To headers."
893   :version "21.1"
894   :group 'gnus-summary
895   :type 'regexp)
896
897 (gnus-define-group-parameter
898  charset
899  :function-document
900  "Return the default charset of GROUP."
901  :variable gnus-group-charset-alist
902  :variable-default 
903  '(("\\(^\\|:\\)hk\\>\\|\\(^\\|:\\)tw\\>\\|\\<big5\\>" cn-big5)
904    ("\\(^\\|:\\)cn\\>\\|\\<chinese\\>" cn-gb-2312)
905    ("\\(^\\|:\\)fj\\>\\|\\(^\\|:\\)japan\\>" iso-2022-jp-2)
906    ("\\(^\\|:\\)tnn\\>\\|\\(^\\|:\\)pin\\>\\|\\(^\\|:\\)sci.lang.japan" iso-2022-7bit)
907    ("\\(^\\|:\\)relcom\\>" koi8-r)
908    ("\\(^\\|:\\)fido7\\>" koi8-r)
909    ("\\(^\\|:\\)\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
910    ("\\(^\\|:\\)israel\\>" iso-8859-1)
911    ("\\(^\\|:\\)han\\>" euc-kr)
912    ("\\(^\\|:\\)alt.chinese.text.big5\\>" chinese-big5)
913    ("\\(^\\|:\\)soc.culture.vietnamese\\>" vietnamese-viqr)
914    ("\\(^\\|:\\)\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
915    (".*" iso-8859-1))
916  :variable-document
917   "Alist of regexps (to match group names) and default charsets to be used when reading."
918   :variable-group gnus-charset
919   :variable-type '(repeat (list (regexp :tag "Group")
920                                 (symbol :tag "Charset")))
921   :parameter-type '(symbol :tag "Charset")
922   :parameter-document "\
923 The default charset to use in the group.")
924
925 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
926   "List of charsets that should be ignored.
927 When these charsets are used in the \"charset\" parameter, the
928 default charset will be used instead."
929   :version "21.1"
930   :type '(repeat symbol)
931   :group 'gnus-charset)
932
933 (gnus-define-group-parameter
934  ignored-charsets
935  :type list
936  :function-document
937  "Return the ignored charsets of GROUP."
938  :variable gnus-group-ignored-charsets-alist
939  :variable-default 
940  '(("alt\\.chinese\\.text" iso-8859-1))
941  :variable-document
942  "Alist of regexps (to match group names) and charsets that should be ignored.
943 When these charsets are used in the \"charset\" parameter, the
944 default charset will be used instead."
945  :variable-group gnus-charset
946  :variable-type '(repeat (cons (regexp :tag "Group")
947                                (repeat symbol)))
948  :parameter-type '(choice :tag "Ignored charsets" 
949                           :value nil
950                           (repeat (symbol)))
951  :parameter-document       "\
952 List of charsets that should be ignored.
953
954 When these charsets are used in the \"charset\" parameter, the
955 default charset will be used instead.")
956
957 (defcustom gnus-group-highlight-words-alist nil
958   "Alist of group regexps and highlight regexps.
959 This variable uses the same syntax as `gnus-emphasis-alist'."
960   :version "21.1"
961   :type '(repeat (cons (regexp :tag "Group")
962                        (repeat (list (regexp :tag "Highlight regexp")
963                                      (number :tag "Group for entire word" 0)
964                                      (number :tag "Group for displayed part" 0)
965                                      (symbol :tag "Face"
966                                              gnus-emphasis-highlight-words)))))
967   :group 'gnus-summary-visual)
968
969 (defcustom gnus-use-wheel nil
970   "Use Intelli-mouse on summary movement"
971   :type 'boolean
972   :group 'gnus-summary-maneuvering)
973
974 (defcustom gnus-wheel-scroll-amount '(5 . 1)
975   "Amount to scroll messages by spinning the mouse wheel.
976 This is actually a cons cell, where the first item is the amount to scroll
977 on a normal wheel event, and the second is the amount to scroll when the
978 wheel is moved with the shift key depressed."
979   :type '(cons (integer :tag "Shift") integer)
980   :group 'gnus-summary-maneuvering)
981
982 (defcustom gnus-wheel-edge-resistance 2
983   "How hard it should be to change the current article
984 by moving the mouse over the edge of the article window."
985   :type 'integer
986   :group 'gnus-summary-maneuvering)
987
988 (defcustom gnus-summary-show-article-charset-alist
989   nil
990   "Alist of number and charset.
991 The article will be shown with the charset corresponding to the
992 numbered argument.
993 For example: ((1 . cn-gb-2312) (2 . big5))."
994   :version "21.1"
995   :type '(repeat (cons (number :tag "Argument" 1)
996                        (symbol :tag "Charset")))
997   :group 'gnus-charset)
998
999 (defcustom gnus-preserve-marks t
1000   "Whether marks are preserved when moving, copying and respooling messages."
1001   :version "21.1"
1002   :type 'boolean
1003   :group 'gnus-summary-marks)
1004
1005 (defcustom gnus-alter-articles-to-read-function nil
1006   "Function to be called to alter the list of articles to be selected."
1007   :type 'function
1008   :group 'gnus-summary)
1009
1010 (defcustom gnus-orphan-score nil
1011   "*All orphans get this score added.  Set in the score file."
1012   :group 'gnus-score-default
1013   :type '(choice (const nil)
1014                  integer))
1015
1016 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1017   "*A regexp to match MIME parts when saving multiple parts of a message
1018 with gnus-summary-save-parts (X m). This regexp will be used by default
1019 when prompting the user for which type of files to save."
1020   :group 'gnus-summary
1021   :type 'regexp)
1022
1023
1024 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1025   "*A regexp to match MIME parts when saving multiple parts of a message
1026 with gnus-summary-save-parts (X m). This regexp will be used by default
1027 when prompting the user for which type of files to save."
1028   :group 'gnus-summary
1029   :type 'regexp)
1030
1031
1032 ;;; Internal variables
1033
1034 (defvar gnus-article-mime-handles nil)
1035 (defvar gnus-article-decoded-p nil)
1036 (defvar gnus-article-charset nil)
1037 (defvar gnus-article-ignored-charsets nil)
1038 (defvar gnus-scores-exclude-files nil)
1039 (defvar gnus-page-broken nil)
1040 (defvar gnus-inhibit-mime-unbuttonizing nil)
1041
1042 (defvar gnus-original-article nil)
1043 (defvar gnus-article-internal-prepare-hook nil)
1044 (defvar gnus-newsgroup-process-stack nil)
1045
1046 (defvar gnus-thread-indent-array nil)
1047 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1048 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1049   "Function called to sort the articles within a thread after it has been gathered together.")
1050
1051 (defvar gnus-summary-save-parts-type-history nil)
1052 (defvar gnus-summary-save-parts-last-directory nil)
1053
1054 (defvar gnus-summary-save-parts-type-history nil)
1055 (defvar gnus-summary-save-parts-last-directory nil)
1056
1057 ;; Avoid highlighting in kill files.
1058 (defvar gnus-summary-inhibit-highlight nil)
1059 (defvar gnus-newsgroup-selected-overlay nil)
1060 (defvar gnus-inhibit-limiting nil)
1061 (defvar gnus-newsgroup-adaptive-score-file nil)
1062 (defvar gnus-current-score-file nil)
1063 (defvar gnus-current-move-group nil)
1064 (defvar gnus-current-copy-group nil)
1065 (defvar gnus-current-crosspost-group nil)
1066
1067 (defvar gnus-newsgroup-dependencies nil)
1068 (defvar gnus-newsgroup-adaptive nil)
1069 (defvar gnus-summary-display-article-function nil)
1070 (defvar gnus-summary-highlight-line-function nil
1071   "Function called after highlighting a summary line.")
1072
1073 (defvar gnus-summary-line-format-alist
1074   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1075     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1076     (?s gnus-tmp-subject-or-nil ?s)
1077     (?n gnus-tmp-name ?s)
1078     (?A (std11-address-string
1079          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1080     (?a (or (std11-full-name-string
1081              (car (mime-entity-read-field gnus-tmp-header 'From)))
1082             gnus-tmp-from) ?s)
1083     (?F gnus-tmp-from ?s)
1084     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1085     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1086     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1087     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1088     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1089     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1090     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1091     (?L gnus-tmp-lines ?d)
1092     (?I gnus-tmp-indentation ?s)
1093     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1094     (?R gnus-tmp-replied ?c)
1095     (?\[ gnus-tmp-opening-bracket ?c)
1096     (?\] gnus-tmp-closing-bracket ?c)
1097     (?\> (make-string gnus-tmp-level ? ) ?s)
1098     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1099     (?i gnus-tmp-score ?d)
1100     (?z gnus-tmp-score-char ?c)
1101     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1102     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1103     (?U gnus-tmp-unread ?c)
1104     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1105     (?t (gnus-summary-number-of-articles-in-thread
1106          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1107         ?d)
1108     (?e (gnus-summary-number-of-articles-in-thread
1109          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1110         ?c)
1111     (?u gnus-tmp-user-defined ?s)
1112     (?P (gnus-pick-line-number) ?d))
1113   "An alist of format specifications that can appear in summary lines.
1114 These are paired with what variables they correspond with, along with
1115 the type of the variable (string, integer, character, etc).")
1116
1117 (defvar gnus-summary-dummy-line-format-alist
1118   `((?S gnus-tmp-subject ?s)
1119     (?N gnus-tmp-number ?d)
1120     (?u gnus-tmp-user-defined ?s)))
1121
1122 (defvar gnus-summary-mode-line-format-alist
1123   `((?G gnus-tmp-group-name ?s)
1124     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1125     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1126     (?A gnus-tmp-article-number ?d)
1127     (?Z gnus-tmp-unread-and-unselected ?s)
1128     (?V gnus-version ?s)
1129     (?U gnus-tmp-unread-and-unticked ?d)
1130     (?S gnus-tmp-subject ?s)
1131     (?e gnus-tmp-unselected ?d)
1132     (?u gnus-tmp-user-defined ?s)
1133     (?d (length gnus-newsgroup-dormant) ?d)
1134     (?t (length gnus-newsgroup-marked) ?d)
1135     (?r (length gnus-newsgroup-reads) ?d)
1136     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1137     (?E gnus-newsgroup-expunged-tally ?d)
1138     (?s (gnus-current-score-file-nondirectory) ?s)))
1139
1140 (defvar gnus-last-search-regexp nil
1141   "Default regexp for article search command.")
1142
1143 (defvar gnus-summary-search-article-matched-data nil
1144   "Last matched data of article search command.  It is the local variable
1145 in `gnus-article-buffer' which consists of the list of start position,
1146 end position and text.")
1147
1148 (defvar gnus-last-shell-command nil
1149   "Default shell command on article.")
1150
1151 (defvar gnus-newsgroup-begin nil)
1152 (defvar gnus-newsgroup-end nil)
1153 (defvar gnus-newsgroup-last-rmail nil)
1154 (defvar gnus-newsgroup-last-mail nil)
1155 (defvar gnus-newsgroup-last-folder nil)
1156 (defvar gnus-newsgroup-last-file nil)
1157 (defvar gnus-newsgroup-auto-expire nil)
1158 (defvar gnus-newsgroup-active nil)
1159
1160 (defvar gnus-newsgroup-data nil)
1161 (defvar gnus-newsgroup-data-reverse nil)
1162 (defvar gnus-newsgroup-limit nil)
1163 (defvar gnus-newsgroup-limits nil)
1164
1165 (defvar gnus-newsgroup-unreads nil
1166   "List of unread articles in the current newsgroup.")
1167
1168 (defvar gnus-newsgroup-unselected nil
1169   "List of unselected unread articles in the current newsgroup.")
1170
1171 (defvar gnus-newsgroup-reads nil
1172   "Alist of read articles and article marks in the current newsgroup.")
1173
1174 (defvar gnus-newsgroup-expunged-tally nil)
1175
1176 (defvar gnus-newsgroup-marked nil
1177   "List of ticked articles in the current newsgroup (a subset of unread art).")
1178
1179 (defvar gnus-newsgroup-killed nil
1180   "List of ranges of articles that have been through the scoring process.")
1181
1182 (defvar gnus-newsgroup-cached nil
1183   "List of articles that come from the article cache.")
1184
1185 (defvar gnus-newsgroup-saved nil
1186   "List of articles that have been saved.")
1187
1188 (defvar gnus-newsgroup-kill-headers nil)
1189
1190 (defvar gnus-newsgroup-replied nil
1191   "List of articles that have been replied to in the current newsgroup.")
1192
1193 (defvar gnus-newsgroup-expirable nil
1194   "List of articles in the current newsgroup that can be expired.")
1195
1196 (defvar gnus-newsgroup-processable nil
1197   "List of articles in the current newsgroup that can be processed.")
1198
1199 (defvar gnus-newsgroup-downloadable nil
1200   "List of articles in the current newsgroup that can be processed.")
1201
1202 (defvar gnus-newsgroup-undownloaded nil
1203   "List of articles in the current newsgroup that haven't been downloaded..")
1204
1205 (defvar gnus-newsgroup-unsendable nil
1206   "List of articles in the current newsgroup that won't be sent.")
1207
1208 (defvar gnus-newsgroup-bookmarks nil
1209   "List of articles in the current newsgroup that have bookmarks.")
1210
1211 (defvar gnus-newsgroup-dormant nil
1212   "List of dormant articles in the current newsgroup.")
1213
1214 (defvar gnus-newsgroup-scored nil
1215   "List of scored articles in the current newsgroup.")
1216
1217 (defvar gnus-newsgroup-incorporated nil
1218   "List of incorporated articles in the current newsgroup.")
1219
1220 (defvar gnus-newsgroup-headers nil
1221   "List of article headers in the current newsgroup.")
1222
1223 (defvar gnus-newsgroup-threads nil)
1224
1225 (defvar gnus-newsgroup-prepared nil
1226   "Whether the current group has been prepared properly.")
1227
1228 (defvar gnus-newsgroup-ancient nil
1229   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1230
1231 (defvar gnus-newsgroup-sparse nil)
1232
1233 (defvar gnus-current-article nil)
1234 (defvar gnus-article-current nil)
1235 (defvar gnus-current-headers nil)
1236 (defvar gnus-have-all-headers nil)
1237 (defvar gnus-last-article nil)
1238 (defvar gnus-newsgroup-history nil)
1239 (defvar gnus-newsgroup-charset nil)
1240 (defvar gnus-newsgroup-ephemeral-charset nil)
1241 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1242
1243 (defvar gnus-article-before-search nil)
1244
1245 (defconst gnus-summary-local-variables
1246   '(gnus-newsgroup-name
1247     gnus-newsgroup-begin gnus-newsgroup-end
1248     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1249     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1250     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1251     gnus-newsgroup-unselected gnus-newsgroup-marked
1252     gnus-newsgroup-reads gnus-newsgroup-saved
1253     gnus-newsgroup-replied gnus-newsgroup-expirable
1254     gnus-newsgroup-processable gnus-newsgroup-killed
1255     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1256     gnus-newsgroup-unsendable
1257     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1258     gnus-newsgroup-headers gnus-newsgroup-threads
1259     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1260     gnus-current-article gnus-current-headers gnus-have-all-headers
1261     gnus-last-article gnus-article-internal-prepare-hook
1262     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1263     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1264     gnus-thread-expunge-below
1265     gnus-score-alist gnus-current-score-file
1266     (gnus-summary-expunge-below . global)
1267     (gnus-summary-mark-below . global)
1268     (gnus-orphan-score . global)
1269     gnus-newsgroup-active gnus-scores-exclude-files
1270     gnus-newsgroup-history gnus-newsgroup-ancient
1271     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1272     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1273     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1274     (gnus-newsgroup-expunged-tally . 0)
1275     gnus-cache-removable-articles gnus-newsgroup-cached
1276     gnus-newsgroup-data gnus-newsgroup-data-reverse
1277     gnus-newsgroup-limit gnus-newsgroup-limits
1278     gnus-newsgroup-charset
1279     gnus-newsgroup-incorporated)
1280   "Variables that are buffer-local to the summary buffers.")
1281
1282 (defvar gnus-newsgroup-variables nil
1283   "Variables that have separate values in the newsgroups.")
1284
1285 ;; Byte-compiler warning.
1286 (eval-when-compile (defvar gnus-article-mode-map))
1287
1288 ;; Subject simplification.
1289
1290 (defun gnus-simplify-whitespace (str)
1291   "Remove excessive whitespace from STR."
1292   (let ((mystr str))
1293     ;; Multiple spaces.
1294     (while (string-match "[ \t][ \t]+" mystr)
1295       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1296                           " "
1297                           (substring mystr (match-end 0)))))
1298     ;; Leading spaces.
1299     (when (string-match "^[ \t]+" mystr)
1300       (setq mystr (substring mystr (match-end 0))))
1301     ;; Trailing spaces.
1302     (when (string-match "[ \t]+$" mystr)
1303       (setq mystr (substring mystr 0 (match-beginning 0))))
1304     mystr))
1305
1306 (defsubst gnus-simplify-subject-re (subject)
1307   "Remove \"Re:\" from subject lines."
1308   (if (string-match message-subject-re-regexp subject)
1309       (substring subject (match-end 0))
1310     subject))
1311
1312 (defun gnus-simplify-subject (subject &optional re-only)
1313   "Remove `Re:' and words in parentheses.
1314 If RE-ONLY is non-nil, strip leading `Re:'s only."
1315   (let ((case-fold-search t))           ;Ignore case.
1316     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1317     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1318       (setq subject (substring subject (match-end 0))))
1319     ;; Remove uninteresting prefixes.
1320     (when (and (not re-only)
1321                gnus-simplify-ignored-prefixes
1322                (string-match gnus-simplify-ignored-prefixes subject))
1323       (setq subject (substring subject (match-end 0))))
1324     ;; Remove words in parentheses from end.
1325     (unless re-only
1326       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1327         (setq subject (substring subject 0 (match-beginning 0)))))
1328     ;; Return subject string.
1329     subject))
1330
1331 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1332 ;; all whitespace.
1333 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1334   (goto-char (point-min))
1335   (while (re-search-forward regexp nil t)
1336     (replace-match (or newtext ""))))
1337
1338 (defun gnus-simplify-buffer-fuzzy ()
1339   "Simplify string in the buffer fuzzily.
1340 The string in the accessible portion of the current buffer is simplified.
1341 It is assumed to be a single-line subject.
1342 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1343 matter is removed.  Additional things can be deleted by setting
1344 `gnus-simplify-subject-fuzzy-regexp'."
1345   (let ((case-fold-search t)
1346         (modified-tick))
1347     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1348
1349     (while (not (eq modified-tick (buffer-modified-tick)))
1350       (setq modified-tick (buffer-modified-tick))
1351       (cond
1352        ((listp gnus-simplify-subject-fuzzy-regexp)
1353         (mapcar 'gnus-simplify-buffer-fuzzy-step
1354                 gnus-simplify-subject-fuzzy-regexp))
1355        (gnus-simplify-subject-fuzzy-regexp
1356         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1357       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1358       (gnus-simplify-buffer-fuzzy-step
1359        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1360       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1361
1362     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1363     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1364     (gnus-simplify-buffer-fuzzy-step " $")
1365     (gnus-simplify-buffer-fuzzy-step "^ +")))
1366
1367 (defun gnus-simplify-subject-fuzzy (subject)
1368   "Simplify a subject string fuzzily.
1369 See `gnus-simplify-buffer-fuzzy' for details."
1370   (save-excursion
1371     (gnus-set-work-buffer)
1372     (let ((case-fold-search t))
1373       ;; Remove uninteresting prefixes.
1374       (when (and gnus-simplify-ignored-prefixes
1375                  (string-match gnus-simplify-ignored-prefixes subject))
1376         (setq subject (substring subject (match-end 0))))
1377       (insert subject)
1378       (inline (gnus-simplify-buffer-fuzzy))
1379       (buffer-string))))
1380
1381 (defsubst gnus-simplify-subject-fully (subject)
1382   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1383   (cond
1384    (gnus-simplify-subject-functions
1385     (gnus-map-function gnus-simplify-subject-functions subject))
1386    ((null gnus-summary-gather-subject-limit)
1387     (gnus-simplify-subject-re subject))
1388    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1389     (gnus-simplify-subject-fuzzy subject))
1390    ((numberp gnus-summary-gather-subject-limit)
1391     (gnus-limit-string (gnus-simplify-subject-re subject)
1392                        gnus-summary-gather-subject-limit))
1393    (t
1394     subject)))
1395
1396 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1397   "Check whether two subjects are equal.
1398 If optional argument simple-first is t, first argument is already
1399 simplified."
1400   (cond
1401    ((null simple-first)
1402     (equal (gnus-simplify-subject-fully s1)
1403            (gnus-simplify-subject-fully s2)))
1404    (t
1405     (equal s1
1406            (gnus-simplify-subject-fully s2)))))
1407
1408 (defun gnus-summary-bubble-group ()
1409   "Increase the score of the current group.
1410 This is a handy function to add to `gnus-summary-exit-hook' to
1411 increase the score of each group you read."
1412   (gnus-group-add-score gnus-newsgroup-name))
1413
1414 \f
1415 ;;;
1416 ;;; Gnus summary mode
1417 ;;;
1418
1419 (put 'gnus-summary-mode 'mode-class 'special)
1420
1421 (defvar gnus-article-commands-menu)
1422
1423 (when t
1424   ;; Non-orthogonal keys
1425
1426   (gnus-define-keys gnus-summary-mode-map
1427     " " gnus-summary-next-page
1428     "\177" gnus-summary-prev-page
1429     [delete] gnus-summary-prev-page
1430     [backspace] gnus-summary-prev-page
1431     "\r" gnus-summary-scroll-up
1432     "\M-\r" gnus-summary-scroll-down
1433     "n" gnus-summary-next-unread-article
1434     "p" gnus-summary-prev-unread-article
1435     "N" gnus-summary-next-article
1436     "P" gnus-summary-prev-article
1437     "\M-\C-n" gnus-summary-next-same-subject
1438     "\M-\C-p" gnus-summary-prev-same-subject
1439     "\M-n" gnus-summary-next-unread-subject
1440     "\M-p" gnus-summary-prev-unread-subject
1441     "." gnus-summary-first-unread-article
1442     "," gnus-summary-best-unread-article
1443     "\M-s" gnus-summary-search-article-forward
1444     "\M-r" gnus-summary-search-article-backward
1445     "<" gnus-summary-beginning-of-article
1446     ">" gnus-summary-end-of-article
1447     "j" gnus-summary-goto-article
1448     "^" gnus-summary-refer-parent-article
1449     "\M-^" gnus-summary-refer-article
1450     "u" gnus-summary-tick-article-forward
1451     "!" gnus-summary-tick-article-forward
1452     "U" gnus-summary-tick-article-backward
1453     "d" gnus-summary-mark-as-read-forward
1454     "D" gnus-summary-mark-as-read-backward
1455     "E" gnus-summary-mark-as-expirable
1456     "\M-u" gnus-summary-clear-mark-forward
1457     "\M-U" gnus-summary-clear-mark-backward
1458     "k" gnus-summary-kill-same-subject-and-select
1459     "\C-k" gnus-summary-kill-same-subject
1460     "\M-\C-k" gnus-summary-kill-thread
1461     "\M-\C-l" gnus-summary-lower-thread
1462     "e" gnus-summary-edit-article
1463     "#" gnus-summary-mark-as-processable
1464     "\M-#" gnus-summary-unmark-as-processable
1465     "\M-\C-t" gnus-summary-toggle-threads
1466     "\M-\C-s" gnus-summary-show-thread
1467     "\M-\C-h" gnus-summary-hide-thread
1468     "\M-\C-f" gnus-summary-next-thread
1469     "\M-\C-b" gnus-summary-prev-thread
1470     [(meta down)] gnus-summary-next-thread
1471     [(meta up)] gnus-summary-prev-thread
1472     "\M-\C-u" gnus-summary-up-thread
1473     "\M-\C-d" gnus-summary-down-thread
1474     "&" gnus-summary-execute-command
1475     "c" gnus-summary-catchup-and-exit
1476     "\C-w" gnus-summary-mark-region-as-read
1477     "\C-t" gnus-summary-toggle-truncation
1478     "?" gnus-summary-mark-as-dormant
1479     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1480     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1481     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1482     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1483     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1484     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1485     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1486     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1487     "=" gnus-summary-expand-window
1488     "\C-x\C-s" gnus-summary-reselect-current-group
1489     "\M-g" gnus-summary-rescan-group
1490     "w" gnus-summary-stop-page-breaking
1491     "\C-c\C-r" gnus-summary-caesar-message
1492     "\M-t" gnus-summary-toggle-mime
1493     "f" gnus-summary-followup
1494     "F" gnus-summary-followup-with-original
1495     "C" gnus-summary-cancel-article
1496     "r" gnus-summary-reply
1497     "R" gnus-summary-reply-with-original
1498     "\C-c\C-f" gnus-summary-mail-forward
1499     "o" gnus-summary-save-article
1500     "\C-o" gnus-summary-save-article-mail
1501     "|" gnus-summary-pipe-output
1502     "\M-k" gnus-summary-edit-local-kill
1503     "\M-K" gnus-summary-edit-global-kill
1504     ;; "V" gnus-version
1505     "\C-c\C-d" gnus-summary-describe-group
1506     "q" gnus-summary-exit
1507     "Q" gnus-summary-exit-no-update
1508     "\C-c\C-i" gnus-info-find-node
1509     gnus-mouse-2 gnus-mouse-pick-article
1510     "m" gnus-summary-mail-other-window
1511     "a" gnus-summary-post-news
1512     "x" gnus-summary-limit-to-unread
1513     "s" gnus-summary-isearch-article
1514     "t" gnus-article-toggle-headers
1515     "g" gnus-summary-show-article
1516     "l" gnus-summary-goto-last-article
1517     "v" gnus-summary-preview-mime-message
1518     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1519     "\C-d" gnus-summary-enter-digest-group
1520     "\M-\C-d" gnus-summary-read-document
1521     "\M-\C-e" gnus-summary-edit-parameters
1522     "\M-\C-a" gnus-summary-customize-parameters
1523     "\C-c\C-b" gnus-bug
1524     "*" gnus-cache-enter-article
1525     "\M-*" gnus-cache-remove-article
1526     "\M-&" gnus-summary-universal-argument
1527     "\C-l" gnus-recenter
1528     "I" gnus-summary-increase-score
1529     "L" gnus-summary-lower-score
1530     "\M-i" gnus-symbolic-argument
1531     "h" gnus-summary-select-article-buffer
1532
1533     "V" gnus-summary-score-map
1534     "X" gnus-uu-extract-map
1535     "S" gnus-summary-send-map)
1536
1537   ;; Sort of orthogonal keymap
1538   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1539     "t" gnus-summary-tick-article-forward
1540     "!" gnus-summary-tick-article-forward
1541     "d" gnus-summary-mark-as-read-forward
1542     "r" gnus-summary-mark-as-read-forward
1543     "c" gnus-summary-clear-mark-forward
1544     " " gnus-summary-clear-mark-forward
1545     "e" gnus-summary-mark-as-expirable
1546     "x" gnus-summary-mark-as-expirable
1547     "?" gnus-summary-mark-as-dormant
1548     "b" gnus-summary-set-bookmark
1549     "B" gnus-summary-remove-bookmark
1550     "#" gnus-summary-mark-as-processable
1551     "\M-#" gnus-summary-unmark-as-processable
1552     "S" gnus-summary-limit-include-expunged
1553     "C" gnus-summary-catchup
1554     "H" gnus-summary-catchup-to-here
1555     "\C-c" gnus-summary-catchup-all
1556     "k" gnus-summary-kill-same-subject-and-select
1557     "K" gnus-summary-kill-same-subject
1558     "P" gnus-uu-mark-map)
1559
1560   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1561     "c" gnus-summary-clear-above
1562     "u" gnus-summary-tick-above
1563     "m" gnus-summary-mark-above
1564     "k" gnus-summary-kill-below)
1565
1566   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1567     "/" gnus-summary-limit-to-subject
1568     "n" gnus-summary-limit-to-articles
1569     "w" gnus-summary-pop-limit
1570     "s" gnus-summary-limit-to-subject
1571     "a" gnus-summary-limit-to-author
1572     "u" gnus-summary-limit-to-unread
1573     "m" gnus-summary-limit-to-marks
1574     "M" gnus-summary-limit-exclude-marks
1575     "v" gnus-summary-limit-to-score
1576     "*" gnus-summary-limit-include-cached
1577     "D" gnus-summary-limit-include-dormant
1578     "T" gnus-summary-limit-include-thread
1579     "d" gnus-summary-limit-exclude-dormant
1580     "t" gnus-summary-limit-to-age
1581     "x" gnus-summary-limit-to-extra
1582     "E" gnus-summary-limit-include-expunged
1583     "c" gnus-summary-limit-exclude-childless-dormant
1584     "C" gnus-summary-limit-mark-excluded-as-read
1585     "o" gnus-summary-insert-old-articles
1586     "N" gnus-summary-insert-new-articles)
1587
1588   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1589     "n" gnus-summary-next-unread-article
1590     "p" gnus-summary-prev-unread-article
1591     "N" gnus-summary-next-article
1592     "P" gnus-summary-prev-article
1593     "\C-n" gnus-summary-next-same-subject
1594     "\C-p" gnus-summary-prev-same-subject
1595     "\M-n" gnus-summary-next-unread-subject
1596     "\M-p" gnus-summary-prev-unread-subject
1597     "f" gnus-summary-first-unread-article
1598     "b" gnus-summary-best-unread-article
1599     "j" gnus-summary-goto-article
1600     "g" gnus-summary-goto-subject
1601     "l" gnus-summary-goto-last-article
1602     "o" gnus-summary-pop-article)
1603
1604   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1605     "k" gnus-summary-kill-thread
1606     "l" gnus-summary-lower-thread
1607     "i" gnus-summary-raise-thread
1608     "T" gnus-summary-toggle-threads
1609     "t" gnus-summary-rethread-current
1610     "^" gnus-summary-reparent-thread
1611     "s" gnus-summary-show-thread
1612     "S" gnus-summary-show-all-threads
1613     "h" gnus-summary-hide-thread
1614     "H" gnus-summary-hide-all-threads
1615     "n" gnus-summary-next-thread
1616     "p" gnus-summary-prev-thread
1617     "u" gnus-summary-up-thread
1618     "o" gnus-summary-top-thread
1619     "d" gnus-summary-down-thread
1620     "#" gnus-uu-mark-thread
1621     "\M-#" gnus-uu-unmark-thread)
1622
1623   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1624     "g" gnus-summary-prepare
1625     "c" gnus-summary-insert-cached-articles)
1626
1627   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1628     "c" gnus-summary-catchup-and-exit
1629     "C" gnus-summary-catchup-all-and-exit
1630     "E" gnus-summary-exit-no-update
1631     "J" gnus-summary-jump-to-other-group
1632     "Q" gnus-summary-exit
1633     "Z" gnus-summary-exit
1634     "n" gnus-summary-catchup-and-goto-next-group
1635     "R" gnus-summary-reselect-current-group
1636     "G" gnus-summary-rescan-group
1637     "N" gnus-summary-next-group
1638     "s" gnus-summary-save-newsrc
1639     "P" gnus-summary-prev-group)
1640
1641   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1642     " " gnus-summary-next-page
1643     "n" gnus-summary-next-page
1644     "\177" gnus-summary-prev-page
1645     [delete] gnus-summary-prev-page
1646     "p" gnus-summary-prev-page
1647     "\r" gnus-summary-scroll-up
1648     "\M-\r" gnus-summary-scroll-down
1649     "<" gnus-summary-beginning-of-article
1650     ">" gnus-summary-end-of-article
1651     "b" gnus-summary-beginning-of-article
1652     "e" gnus-summary-end-of-article
1653     "^" gnus-summary-refer-parent-article
1654     "r" gnus-summary-refer-parent-article
1655     "D" gnus-summary-enter-digest-group
1656     "R" gnus-summary-refer-references
1657     "T" gnus-summary-refer-thread
1658     "g" gnus-summary-show-article
1659     "s" gnus-summary-isearch-article
1660     "P" gnus-summary-print-article
1661     "M" gnus-mailing-list-insinuate
1662     "t" gnus-article-babel)
1663
1664   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1665     "b" gnus-article-add-buttons
1666     "B" gnus-article-add-buttons-to-head
1667     "o" gnus-article-treat-overstrike
1668     "e" gnus-article-emphasize
1669     "w" gnus-article-fill-cited-article
1670     "Q" gnus-article-fill-long-lines
1671     "C" gnus-article-capitalize-sentences
1672     "c" gnus-article-remove-cr
1673     "Z" gnus-article-decode-HZ
1674     "f" gnus-article-display-x-face
1675     "l" gnus-summary-stop-page-breaking
1676     "r" gnus-summary-caesar-message
1677     "t" gnus-article-toggle-headers
1678     "v" gnus-summary-verbose-headers
1679     "m" gnus-summary-toggle-mime
1680     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1681     "p" gnus-article-verify-x-pgp-sig
1682     "d" gnus-article-treat-dumbquotes
1683     "s" gnus-smiley-display)
1684
1685   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1686     "a" gnus-article-hide
1687     "h" gnus-article-toggle-headers
1688     "b" gnus-article-hide-boring-headers
1689     "s" gnus-article-hide-signature
1690     "c" gnus-article-hide-citation
1691     "C" gnus-article-hide-citation-in-followups
1692     "l" gnus-article-hide-list-identifiers
1693     "p" gnus-article-hide-pgp
1694     "B" gnus-article-strip-banner
1695     "P" gnus-article-hide-pem
1696     "\C-c" gnus-article-hide-citation-maybe)
1697
1698   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1699     "a" gnus-article-highlight
1700     "h" gnus-article-highlight-headers
1701     "c" gnus-article-highlight-citation
1702     "s" gnus-article-highlight-signature)
1703
1704   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1705     "z" gnus-article-date-ut
1706     "u" gnus-article-date-ut
1707     "l" gnus-article-date-local
1708     "p" gnus-article-date-english
1709     "e" gnus-article-date-lapsed
1710     "o" gnus-article-date-original
1711     "i" gnus-article-date-iso8601
1712     "s" gnus-article-date-user)
1713
1714   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1715     "t" gnus-article-remove-trailing-blank-lines
1716     "l" gnus-article-strip-leading-blank-lines
1717     "m" gnus-article-strip-multiple-blank-lines
1718     "a" gnus-article-strip-blank-lines
1719     "A" gnus-article-strip-all-blank-lines
1720     "s" gnus-article-strip-leading-space
1721     "e" gnus-article-strip-trailing-space
1722     "w" gnus-article-remove-leading-whitespace)
1723
1724   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1725     "v" gnus-version
1726     "f" gnus-summary-fetch-faq
1727     "d" gnus-summary-describe-group
1728     "h" gnus-summary-describe-briefly
1729     "i" gnus-info-find-node)
1730
1731   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1732     "e" gnus-summary-expire-articles
1733     "\M-\C-e" gnus-summary-expire-articles-now
1734     "\177" gnus-summary-delete-article
1735     [delete] gnus-summary-delete-article
1736     [backspace] gnus-summary-delete-article
1737     "m" gnus-summary-move-article
1738     "r" gnus-summary-respool-article
1739     "w" gnus-summary-edit-article
1740     "c" gnus-summary-copy-article
1741     "B" gnus-summary-crosspost-article
1742     "q" gnus-summary-respool-query
1743     "t" gnus-summary-respool-trace
1744     "i" gnus-summary-import-article
1745     "I" gnus-summary-create-article
1746     "p" gnus-summary-article-posted-p)
1747
1748   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1749     "o" gnus-summary-save-article
1750     "m" gnus-summary-save-article-mail
1751     "F" gnus-summary-write-article-file
1752     "r" gnus-summary-save-article-rmail
1753     "f" gnus-summary-save-article-file
1754     "b" gnus-summary-save-article-body-file
1755     "h" gnus-summary-save-article-folder
1756     "v" gnus-summary-save-article-vm
1757     "p" gnus-summary-pipe-output
1758     "s" gnus-soup-add-article)
1759
1760   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1761     "b" gnus-summary-display-buttonized
1762     "m" gnus-summary-repair-multipart
1763     "v" gnus-article-view-part
1764     "o" gnus-article-save-part
1765     "c" gnus-article-copy-part
1766     "C" gnus-article-view-part-as-charset
1767     "e" gnus-article-externalize-part
1768     "E" gnus-article-encrypt-body
1769     "i" gnus-article-inline-part
1770     "|" gnus-article-pipe-part))
1771
1772 (defvar gnus-article-post-menu nil)
1773
1774 (defun gnus-summary-make-menu-bar ()
1775   (gnus-turn-off-edit-menu 'summary)
1776
1777   (unless (boundp 'gnus-summary-misc-menu)
1778
1779     (easy-menu-define
1780      gnus-summary-kill-menu gnus-summary-mode-map ""
1781      (cons
1782       "Score"
1783       (nconc
1784        (list
1785         ["Customize" gnus-score-customize t])
1786        (gnus-make-score-map 'increase)
1787        (gnus-make-score-map 'lower)
1788        '(("Mark"
1789           ["Kill below" gnus-summary-kill-below t]
1790           ["Mark above" gnus-summary-mark-above t]
1791           ["Tick above" gnus-summary-tick-above t]
1792           ["Clear above" gnus-summary-clear-above t])
1793          ["Current score" gnus-summary-current-score t]
1794          ["Set score" gnus-summary-set-score t]
1795          ["Switch current score file..." gnus-score-change-score-file t]
1796          ["Set mark below..." gnus-score-set-mark-below t]
1797          ["Set expunge below..." gnus-score-set-expunge-below t]
1798          ["Edit current score file" gnus-score-edit-current-scores t]
1799          ["Edit score file" gnus-score-edit-file t]
1800          ["Trace score" gnus-score-find-trace t]
1801          ["Find words" gnus-score-find-favourite-words t]
1802          ["Rescore buffer" gnus-summary-rescore t]
1803          ["Increase score..." gnus-summary-increase-score t]
1804          ["Lower score..." gnus-summary-lower-score t]))))
1805
1806     ;; Define both the Article menu in the summary buffer and the equivalent
1807     ;; Commands menu in the article buffer here for consistency.
1808     (let ((innards
1809            `(("Hide"
1810               ["All" gnus-article-hide t]
1811               ["Headers" gnus-article-toggle-headers t]
1812               ["Signature" gnus-article-hide-signature t]
1813               ["Citation" gnus-article-hide-citation t]
1814               ["List identifiers" gnus-article-hide-list-identifiers t]
1815               ["PGP" gnus-article-hide-pgp t]
1816               ["Banner" gnus-article-strip-banner t]
1817               ["Boring headers" gnus-article-hide-boring-headers t])
1818              ("Highlight"
1819               ["All" gnus-article-highlight t]
1820               ["Headers" gnus-article-highlight-headers t]
1821               ["Signature" gnus-article-highlight-signature t]
1822               ["Citation" gnus-article-highlight-citation t])
1823              ("Date"
1824               ["Local" gnus-article-date-local t]
1825               ["ISO8601" gnus-article-date-iso8601 t]
1826               ["UT" gnus-article-date-ut t]
1827               ["Original" gnus-article-date-original t]
1828               ["Lapsed" gnus-article-date-lapsed t]
1829               ["User-defined" gnus-article-date-user t])
1830              ("Washing"
1831               ("Remove Blanks"
1832                ["Leading" gnus-article-strip-leading-blank-lines t]
1833                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1834                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1835                ["All of the above" gnus-article-strip-blank-lines t]
1836                ["All" gnus-article-strip-all-blank-lines t]
1837                ["Leading space" gnus-article-strip-leading-space t]
1838                ["Trailing space" gnus-article-strip-trailing-space t]
1839                ["Leading space in headers" 
1840                 gnus-article-remove-leading-whitespace t])
1841               ["Overstrike" gnus-article-treat-overstrike t]
1842               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1843               ["Emphasis" gnus-article-emphasize t]
1844               ["Word wrap" gnus-article-fill-cited-article t]
1845               ["Fill long lines" gnus-article-fill-long-lines t]
1846               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1847               ["CR" gnus-article-remove-cr t]
1848               ["Show X-Face" gnus-article-display-x-face t]
1849               ["Rot 13" gnus-summary-caesar-message
1850                ,@(if (featurep 'xemacs) '(t)
1851                    '(:help "\"Caesar rotate\" article by 13"))]
1852               ["Unix pipe" gnus-summary-pipe-message t]
1853               ["Add buttons" gnus-article-add-buttons t]
1854               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1855               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1856               ["Toggle MIME" gnus-summary-toggle-mime t]
1857               ["Verbose header" gnus-summary-verbose-headers t]
1858               ["Toggle header" gnus-summary-toggle-header t]
1859               ["Toggle smileys" gnus-smiley-display t]
1860               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1861               ["HZ" gnus-article-decode-HZ t])
1862              ("Output"
1863               ["Save in default format" gnus-summary-save-article
1864                ,@(if (featurep 'xemacs) '(t)
1865                    '(:help "Save article using default method"))]
1866               ["Save in file" gnus-summary-save-article-file
1867                ,@(if (featurep 'xemacs) '(t)
1868                    '(:help "Save article in file"))]
1869               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1870               ["Save in MH folder" gnus-summary-save-article-folder t]
1871               ["Save in VM folder" gnus-summary-save-article-vm t]
1872               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1873               ["Save body in file" gnus-summary-save-article-body-file t]
1874               ["Pipe through a filter" gnus-summary-pipe-output t]
1875               ["Add to SOUP packet" gnus-soup-add-article t]
1876               ["Print" gnus-summary-print-article t])
1877              ("Backend"
1878               ["Respool article..." gnus-summary-respool-article t]
1879               ["Move article..." gnus-summary-move-article
1880                (gnus-check-backend-function
1881                 'request-move-article gnus-newsgroup-name)]
1882               ["Copy article..." gnus-summary-copy-article t]
1883               ["Crosspost article..." gnus-summary-crosspost-article
1884                (gnus-check-backend-function
1885                 'request-replace-article gnus-newsgroup-name)]
1886               ["Import file..." gnus-summary-import-article t]
1887               ["Create article..." gnus-summary-create-article t]
1888               ["Check if posted" gnus-summary-article-posted-p t]
1889               ["Edit article" gnus-summary-edit-article
1890                (not (gnus-group-read-only-p))]
1891               ["Delete article" gnus-summary-delete-article
1892                (gnus-check-backend-function
1893                 'request-expire-articles gnus-newsgroup-name)]
1894               ["Query respool" gnus-summary-respool-query t]
1895               ["Trace respool" gnus-summary-respool-trace t]
1896               ["Delete expirable articles" gnus-summary-expire-articles-now
1897                (gnus-check-backend-function
1898                 'request-expire-articles gnus-newsgroup-name)])
1899              ("Extract"
1900               ["Uudecode" gnus-uu-decode-uu
1901                ,@(if (featurep 'xemacs) '(t)
1902                    '(:help "Decode uuencoded article(s)"))]
1903               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1904               ["Unshar" gnus-uu-decode-unshar t]
1905               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1906               ["Save" gnus-uu-decode-save t]
1907               ["Binhex" gnus-uu-decode-binhex t]
1908               ["Postscript" gnus-uu-decode-postscript t])
1909              ("Cache"
1910               ["Enter article" gnus-cache-enter-article t]
1911               ["Remove article" gnus-cache-remove-article t])
1912              ["Translate" gnus-article-babel t]
1913              ["Select article buffer" gnus-summary-select-article-buffer t]
1914              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1915              ["Isearch article..." gnus-summary-isearch-article t]
1916              ["Beginning of the article" gnus-summary-beginning-of-article t]
1917              ["End of the article" gnus-summary-end-of-article t]
1918              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1919              ["Fetch referenced articles" gnus-summary-refer-references t]
1920              ["Fetch current thread" gnus-summary-refer-thread t]
1921              ["Fetch article with id..." gnus-summary-refer-article t]
1922              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1923              ["Redisplay" gnus-summary-show-article t])))
1924       (easy-menu-define
1925        gnus-summary-article-menu gnus-summary-mode-map ""
1926        (cons "Article" innards))
1927
1928       (if (not (keymapp gnus-summary-article-menu))
1929           (easy-menu-define
1930             gnus-article-commands-menu gnus-article-mode-map ""
1931             (cons "Commands" innards))
1932         ;; in Emacs, don't share menu.
1933         (setq gnus-article-commands-menu 
1934               (copy-keymap gnus-summary-article-menu))
1935         (define-key gnus-article-mode-map [menu-bar commands]
1936           (cons "Commands" gnus-article-commands-menu))))
1937
1938     (easy-menu-define
1939      gnus-summary-thread-menu gnus-summary-mode-map ""
1940      '("Threads"
1941        ["Toggle threading" gnus-summary-toggle-threads t]
1942        ["Hide threads" gnus-summary-hide-all-threads t]
1943        ["Show threads" gnus-summary-show-all-threads t]
1944        ["Hide thread" gnus-summary-hide-thread t]
1945        ["Show thread" gnus-summary-show-thread t]
1946        ["Go to next thread" gnus-summary-next-thread t]
1947        ["Go to previous thread" gnus-summary-prev-thread t]
1948        ["Go down thread" gnus-summary-down-thread t]
1949        ["Go up thread" gnus-summary-up-thread t]
1950        ["Top of thread" gnus-summary-top-thread t]
1951        ["Mark thread as read" gnus-summary-kill-thread t]
1952        ["Lower thread score" gnus-summary-lower-thread t]
1953        ["Raise thread score" gnus-summary-raise-thread t]
1954        ["Rethread current" gnus-summary-rethread-current t]))
1955
1956     (easy-menu-define
1957      gnus-summary-post-menu gnus-summary-mode-map ""
1958      `("Post"
1959        ["Post an article" gnus-summary-post-news
1960         ,@(if (featurep 'xemacs) '(t)
1961             '(:help "Post an article"))]
1962        ["Followup" gnus-summary-followup
1963         ,@(if (featurep 'xemacs) '(t)
1964             '(:help "Post followup to this article"))]
1965        ["Followup and yank" gnus-summary-followup-with-original
1966         ,@(if (featurep 'xemacs) '(t)
1967             '(:help "Post followup to this article, quoting its contents"))]
1968        ["Supersede article" gnus-summary-supersede-article t]
1969        ["Cancel article" gnus-summary-cancel-article
1970         ,@(if (featurep 'xemacs) '(t)
1971             '(:help "Cancel an article you posted"))]
1972        ["Reply" gnus-summary-reply t]
1973        ["Reply and yank" gnus-summary-reply-with-original t]
1974        ["Wide reply" gnus-summary-wide-reply t]
1975        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1976         ,@(if (featurep 'xemacs) '(t)
1977             '(:help "Mail a reply, quoting this article"))]
1978        ["Mail forward" gnus-summary-mail-forward t]
1979        ["Post forward" gnus-summary-post-forward t]
1980        ["Digest and mail" gnus-summary-digest-mail-forward t]
1981        ["Digest and post" gnus-summary-digest-post-forward t]
1982        ["Resend message" gnus-summary-resend-message t]
1983        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1984        ["Send a mail" gnus-summary-mail-other-window t]
1985        ["Uuencode and post" gnus-uu-post-news
1986         ,@(if (featurep 'xemacs) '(t)
1987             '(:help "Post a uuencoded article"))]
1988        ["Followup via news" gnus-summary-followup-to-mail t]
1989        ["Followup via news and yank"
1990         gnus-summary-followup-to-mail-with-original t]
1991        ;;("Draft"
1992        ;;["Send" gnus-summary-send-draft t]
1993        ;;["Send bounced" gnus-resend-bounced-mail t])
1994        ))
1995
1996     (cond 
1997      ((not (keymapp gnus-summary-post-menu))
1998       (setq gnus-article-post-menu gnus-summary-post-menu))
1999      ((not gnus-article-post-menu)
2000       ;; Don't share post menu.
2001       (setq gnus-article-post-menu
2002             (copy-keymap gnus-summary-post-menu))))
2003     (define-key gnus-article-mode-map [menu-bar post]
2004       (cons "Post" gnus-article-post-menu))
2005
2006     (easy-menu-define
2007      gnus-summary-misc-menu gnus-summary-mode-map ""
2008      `("Misc"
2009        ("Mark Read"
2010         ["Mark as read" gnus-summary-mark-as-read-forward t]
2011         ["Mark same subject and select"
2012          gnus-summary-kill-same-subject-and-select t]
2013         ["Mark same subject" gnus-summary-kill-same-subject t]
2014         ["Catchup" gnus-summary-catchup
2015          ,@(if (featurep 'xemacs) '(t)
2016              '(:help "Mark unread articles in this group as read"))]
2017         ["Catchup all" gnus-summary-catchup-all t]
2018         ["Catchup to here" gnus-summary-catchup-to-here t]
2019         ["Catchup region" gnus-summary-mark-region-as-read t]
2020         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2021        ("Mark Various"
2022         ["Tick" gnus-summary-tick-article-forward t]
2023         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2024         ["Remove marks" gnus-summary-clear-mark-forward t]
2025         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2026         ["Set bookmark" gnus-summary-set-bookmark t]
2027         ["Remove bookmark" gnus-summary-remove-bookmark t])
2028        ("Mark Limit"
2029         ["Marks..." gnus-summary-limit-to-marks t]
2030         ["Subject..." gnus-summary-limit-to-subject t]
2031         ["Author..." gnus-summary-limit-to-author t]
2032         ["Age..." gnus-summary-limit-to-age t]
2033         ["Extra..." gnus-summary-limit-to-extra t]
2034         ["Score" gnus-summary-limit-to-score t]
2035         ["Unread" gnus-summary-limit-to-unread t]
2036         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2037         ["Articles" gnus-summary-limit-to-articles t]
2038         ["Pop limit" gnus-summary-pop-limit t]
2039         ["Show dormant" gnus-summary-limit-include-dormant t]
2040         ["Hide childless dormant"
2041          gnus-summary-limit-exclude-childless-dormant t]
2042         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2043         ["Hide marked" gnus-summary-limit-exclude-marks t]
2044         ["Show expunged" gnus-summary-limit-include-expunged t])
2045        ("Process Mark"
2046         ["Set mark" gnus-summary-mark-as-processable t]
2047         ["Remove mark" gnus-summary-unmark-as-processable t]
2048         ["Remove all marks" gnus-summary-unmark-all-processable t]
2049         ["Mark above" gnus-uu-mark-over t]
2050         ["Mark series" gnus-uu-mark-series t]
2051         ["Mark region" gnus-uu-mark-region t]
2052         ["Unmark region" gnus-uu-unmark-region t]
2053         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2054         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2055         ["Mark all" gnus-uu-mark-all t]
2056         ["Mark buffer" gnus-uu-mark-buffer t]
2057         ["Mark sparse" gnus-uu-mark-sparse t]
2058         ["Mark thread" gnus-uu-mark-thread t]
2059         ["Unmark thread" gnus-uu-unmark-thread t]
2060         ("Process Mark Sets"
2061          ["Kill" gnus-summary-kill-process-mark t]
2062          ["Yank" gnus-summary-yank-process-mark
2063           gnus-newsgroup-process-stack]
2064          ["Save" gnus-summary-save-process-mark t]))
2065        ("Scroll article"
2066         ["Page forward" gnus-summary-next-page
2067          ,@(if (featurep 'xemacs) '(t)
2068              '(:help "Show next page of article"))]
2069         ["Page backward" gnus-summary-prev-page
2070          ,@(if (featurep 'xemacs) '(t)
2071              '(:help "Show previous page of article"))]
2072         ["Line forward" gnus-summary-scroll-up t])
2073        ("Move"
2074         ["Next unread article" gnus-summary-next-unread-article t]
2075         ["Previous unread article" gnus-summary-prev-unread-article t]
2076         ["Next article" gnus-summary-next-article t]
2077         ["Previous article" gnus-summary-prev-article t]
2078         ["Next unread subject" gnus-summary-next-unread-subject t]
2079         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2080         ["Next article same subject" gnus-summary-next-same-subject t]
2081         ["Previous article same subject" gnus-summary-prev-same-subject t]
2082         ["First unread article" gnus-summary-first-unread-article t]
2083         ["Best unread article" gnus-summary-best-unread-article t]
2084         ["Go to subject number..." gnus-summary-goto-subject t]
2085         ["Go to article number..." gnus-summary-goto-article t]
2086         ["Go to the last article" gnus-summary-goto-last-article t]
2087         ["Pop article off history" gnus-summary-pop-article t])
2088        ("Sort"
2089         ["Sort by number" gnus-summary-sort-by-number t]
2090         ["Sort by author" gnus-summary-sort-by-author t]
2091         ["Sort by subject" gnus-summary-sort-by-subject t]
2092         ["Sort by date" gnus-summary-sort-by-date t]
2093         ["Sort by score" gnus-summary-sort-by-score t]
2094         ["Sort by lines" gnus-summary-sort-by-lines t]
2095         ["Sort by characters" gnus-summary-sort-by-chars t])
2096        ("Help"
2097         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2098         ["Describe group" gnus-summary-describe-group t]
2099         ["Read manual" gnus-info-find-node t])
2100        ("Modes"
2101         ["Pick and read" gnus-pick-mode t]
2102         ["Binary" gnus-binary-mode t])
2103        ("Regeneration"
2104         ["Regenerate" gnus-summary-prepare t]
2105         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2106         ["Toggle threading" gnus-summary-toggle-threads t])
2107        ["See old articles" gnus-summary-insert-old-articles t]
2108        ["See new articles" gnus-summary-insert-new-articles t]
2109        ["Filter articles..." gnus-summary-execute-command t]
2110        ["Run command on subjects..." gnus-summary-universal-argument t]
2111        ["Search articles forward..." gnus-summary-search-article-forward t]
2112        ["Search articles backward..." gnus-summary-search-article-backward t]
2113        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2114        ["Expand window" gnus-summary-expand-window t]
2115        ["Expire expirable articles" gnus-summary-expire-articles
2116         (gnus-check-backend-function
2117          'request-expire-articles gnus-newsgroup-name)]
2118        ["Edit local kill file" gnus-summary-edit-local-kill t]
2119        ["Edit main kill file" gnus-summary-edit-global-kill t]
2120        ["Edit group parameters" gnus-summary-edit-parameters t]
2121        ["Customize group parameters" gnus-summary-customize-parameters t]
2122        ["Send a bug report" gnus-bug t]
2123        ("Exit"
2124         ["Catchup and exit" gnus-summary-catchup-and-exit
2125          ,@(if (featurep 'xemacs) '(t)
2126              '(:help "Mark unread articles in this group as read, then exit"))]
2127         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2128         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2129         ["Exit group" gnus-summary-exit
2130          ,@(if (featurep 'xemacs) '(t)
2131              '(:help "Exit current group, return to group selection mode"))]
2132         ["Exit group without updating" gnus-summary-exit-no-update t]
2133         ["Exit and goto next group" gnus-summary-next-group t]
2134         ["Exit and goto prev group" gnus-summary-prev-group t]
2135         ["Reselect group" gnus-summary-reselect-current-group t]
2136         ["Rescan group" gnus-summary-rescan-group t]
2137         ["Update dribble" gnus-summary-save-newsrc t])))
2138
2139     (gnus-run-hooks 'gnus-summary-menu-hook)))
2140
2141 (defvar gnus-summary-tool-bar-map nil)
2142
2143 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2144 (defun gnus-summary-make-tool-bar ()
2145   (if (and (fboundp 'tool-bar-add-item-from-menu)
2146            (default-value 'tool-bar-mode)
2147            (not gnus-summary-tool-bar-map))
2148       (setq gnus-summary-tool-bar-map
2149             (let ((tool-bar-map (make-sparse-keymap))
2150                   (load-path (mm-image-load-path)))
2151               (tool-bar-add-item-from-menu
2152                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2153               (tool-bar-add-item-from-menu
2154                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2155               (tool-bar-add-item-from-menu
2156                'gnus-summary-post-news "post" gnus-summary-mode-map)
2157               (tool-bar-add-item-from-menu
2158                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2159               (tool-bar-add-item-from-menu
2160                'gnus-summary-followup "followup" gnus-summary-mode-map)
2161               (tool-bar-add-item-from-menu
2162                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2163               (tool-bar-add-item-from-menu
2164                'gnus-summary-reply "reply" gnus-summary-mode-map)
2165               (tool-bar-add-item-from-menu
2166                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2167               (tool-bar-add-item-from-menu
2168                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2169               (tool-bar-add-item-from-menu
2170                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2171               (tool-bar-add-item-from-menu
2172                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2173               (tool-bar-add-item-from-menu
2174                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2175               (tool-bar-add-item-from-menu
2176                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2177               (tool-bar-add-item-from-menu
2178                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2179               (tool-bar-add-item-from-menu
2180                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2181               tool-bar-map)))
2182   (if gnus-summary-tool-bar-map
2183       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2184
2185 (defun gnus-score-set-default (var value)
2186   "A version of set that updates the GNU Emacs menu-bar."
2187   (set var value)
2188   ;; It is the message that forces the active status to be updated.
2189   (message ""))
2190
2191 (defun gnus-make-score-map (type)
2192   "Make a summary score map of type TYPE."
2193   (if t
2194       nil
2195     (let ((headers '(("author" "from" string)
2196                      ("subject" "subject" string)
2197                      ("article body" "body" string)
2198                      ("article head" "head" string)
2199                      ("xref" "xref" string)
2200                      ("extra header" "extra" string)
2201                      ("lines" "lines" number)
2202                      ("followups to author" "followup" string)))
2203           (types '((number ("less than" <)
2204                            ("greater than" >)
2205                            ("equal" =))
2206                    (string ("substring" s)
2207                            ("exact string" e)
2208                            ("fuzzy string" f)
2209                            ("regexp" r))))
2210           (perms '(("temporary" (current-time-string))
2211                    ("permanent" nil)
2212                    ("immediate" now)))
2213           header)
2214       (list
2215        (apply
2216         'nconc
2217         (list
2218          (if (eq type 'lower)
2219              "Lower score"
2220            "Increase score"))
2221         (let (outh)
2222           (while headers
2223             (setq header (car headers))
2224             (setq outh
2225                   (cons
2226                    (apply
2227                     'nconc
2228                     (list (car header))
2229                     (let ((ts (cdr (assoc (nth 2 header) types)))
2230                           outt)
2231                       (while ts
2232                         (setq outt
2233                               (cons
2234                                (apply
2235                                 'nconc
2236                                 (list (caar ts))
2237                                 (let ((ps perms)
2238                                       outp)
2239                                   (while ps
2240                                     (setq outp
2241                                           (cons
2242                                            (vector
2243                                             (caar ps)
2244                                             (list
2245                                              'gnus-summary-score-entry
2246                                              (nth 1 header)
2247                                              (if (or (string= (nth 1 header)
2248                                                               "head")
2249                                                      (string= (nth 1 header)
2250                                                               "body"))
2251                                                  ""
2252                                                (list 'gnus-summary-header
2253                                                      (nth 1 header)))
2254                                              (list 'quote (nth 1 (car ts)))
2255                                              (list 'gnus-score-delta-default
2256                                                    nil)
2257                                              (nth 1 (car ps))
2258                                              t)
2259                                             t)
2260                                            outp))
2261                                     (setq ps (cdr ps)))
2262                                   (list (nreverse outp))))
2263                                outt))
2264                         (setq ts (cdr ts)))
2265                       (list (nreverse outt))))
2266                    outh))
2267             (setq headers (cdr headers)))
2268           (list (nreverse outh))))))))
2269
2270 \f
2271
2272 (defun gnus-summary-mode (&optional group)
2273   "Major mode for reading articles.
2274
2275 All normal editing commands are switched off.
2276 \\<gnus-summary-mode-map>
2277 Each line in this buffer represents one article.  To read an
2278 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2279 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2280 respectively.
2281
2282 You can also post articles and send mail from this buffer.  To
2283 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2284 of an article, type `\\[gnus-summary-reply]'.
2285
2286 There are approx. one gazillion commands you can execute in this
2287 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2288
2289 The following commands are available:
2290
2291 \\{gnus-summary-mode-map}"
2292   (interactive)
2293   (kill-all-local-variables)
2294   (when (gnus-visual-p 'summary-menu 'menu)
2295     (gnus-summary-make-menu-bar)
2296     (gnus-summary-make-tool-bar))
2297   (gnus-summary-make-local-variables)
2298   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2299     (gnus-summary-make-local-variables))
2300   (gnus-make-thread-indent-array)
2301   (gnus-simplify-mode-line)
2302   (setq major-mode 'gnus-summary-mode)
2303   (setq mode-name "Summary")
2304   (make-local-variable 'minor-mode-alist)
2305   (use-local-map gnus-summary-mode-map)
2306   (buffer-disable-undo)
2307   (setq buffer-read-only t)             ;Disable modification
2308   (setq truncate-lines t)
2309   (setq selective-display t)
2310   (setq selective-display-ellipses t)   ;Display `...'
2311   (gnus-summary-set-display-table)
2312   (gnus-set-default-directory)
2313   (setq gnus-newsgroup-name group)
2314   (unless (gnus-news-group-p group)
2315     (setq gnus-newsgroup-incorporated
2316           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2317   (make-local-variable 'gnus-summary-line-format)
2318   (make-local-variable 'gnus-summary-line-format-spec)
2319   (make-local-variable 'gnus-summary-dummy-line-format)
2320   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2321   (make-local-variable 'gnus-summary-mark-positions)
2322   (make-local-hook 'pre-command-hook)
2323   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2324   (gnus-run-hooks 'gnus-summary-mode-hook)
2325   (turn-on-gnus-mailing-list-mode)
2326   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2327   (gnus-update-summary-mark-positions))
2328
2329 (defun gnus-summary-make-local-variables ()
2330   "Make all the local summary buffer variables."
2331   (let (global)
2332     (dolist (local gnus-summary-local-variables)
2333       (if (consp local)
2334           (progn
2335             (if (eq (cdr local) 'global)
2336                 ;; Copy the global value of the variable.
2337                 (setq global (symbol-value (car local)))
2338               ;; Use the value from the list.
2339               (setq global (eval (cdr local))))
2340             (set (make-local-variable (car local)) global))
2341         ;; Simple nil-valued local variable.
2342         (set (make-local-variable local) nil)))))
2343
2344 (defun gnus-summary-clear-local-variables ()
2345   (let ((locals gnus-summary-local-variables))
2346     (while locals
2347       (if (consp (car locals))
2348           (and (vectorp (caar locals))
2349                (set (caar locals) nil))
2350         (and (vectorp (car locals))
2351              (set (car locals) nil)))
2352       (setq locals (cdr locals)))))
2353
2354 ;; Summary data functions.
2355
2356 (defmacro gnus-data-number (data)
2357   `(car ,data))
2358
2359 (defmacro gnus-data-set-number (data number)
2360   `(setcar ,data ,number))
2361
2362 (defmacro gnus-data-mark (data)
2363   `(nth 1 ,data))
2364
2365 (defmacro gnus-data-set-mark (data mark)
2366   `(setcar (nthcdr 1 ,data) ,mark))
2367
2368 (defmacro gnus-data-pos (data)
2369   `(nth 2 ,data))
2370
2371 (defmacro gnus-data-set-pos (data pos)
2372   `(setcar (nthcdr 2 ,data) ,pos))
2373
2374 (defmacro gnus-data-header (data)
2375   `(nth 3 ,data))
2376
2377 (defmacro gnus-data-set-header (data header)
2378   `(setcar (nthcdr 3 ,data) ,header))
2379
2380 (defmacro gnus-data-level (data)
2381   `(nth 4 ,data))
2382
2383 (defmacro gnus-data-unread-p (data)
2384   `(= (nth 1 ,data) gnus-unread-mark))
2385
2386 (defmacro gnus-data-read-p (data)
2387   `(/= (nth 1 ,data) gnus-unread-mark))
2388
2389 (defmacro gnus-data-pseudo-p (data)
2390   `(consp (nth 3 ,data)))
2391
2392 (defmacro gnus-data-find (number)
2393   `(assq ,number gnus-newsgroup-data))
2394
2395 (defmacro gnus-data-find-list (number &optional data)
2396   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2397      (memq (assq ,number bdata)
2398            bdata)))
2399
2400 (defmacro gnus-data-make (number mark pos header level)
2401   `(list ,number ,mark ,pos ,header ,level))
2402
2403 (defun gnus-data-enter (after-article number mark pos header level offset)
2404   (let ((data (gnus-data-find-list after-article)))
2405     (unless data
2406       (error "No such article: %d" after-article))
2407     (setcdr data (cons (gnus-data-make number mark pos header level)
2408                        (cdr data)))
2409     (setq gnus-newsgroup-data-reverse nil)
2410     (gnus-data-update-list (cddr data) offset)))
2411
2412 (defun gnus-data-enter-list (after-article list &optional offset)
2413   (when list
2414     (let ((data (and after-article (gnus-data-find-list after-article)))
2415           (ilist list))
2416       (if (not (or data
2417                    after-article))
2418           (let ((odata gnus-newsgroup-data))
2419             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2420             (when offset
2421               (gnus-data-update-list odata offset)))
2422         ;; Find the last element in the list to be spliced into the main
2423         ;; list.
2424         (while (cdr list)
2425           (setq list (cdr list)))
2426         (if (not data)
2427             (progn
2428               (setcdr list gnus-newsgroup-data)
2429               (setq gnus-newsgroup-data ilist)
2430               (when offset
2431                 (gnus-data-update-list (cdr list) offset)))
2432           (setcdr list (cdr data))
2433           (setcdr data ilist)
2434           (when offset
2435             (gnus-data-update-list (cdr list) offset))))
2436       (setq gnus-newsgroup-data-reverse nil))))
2437
2438 (defun gnus-data-remove (article &optional offset)
2439   (let ((data gnus-newsgroup-data))
2440     (if (= (gnus-data-number (car data)) article)
2441         (progn
2442           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2443                 gnus-newsgroup-data-reverse nil)
2444           (when offset
2445             (gnus-data-update-list gnus-newsgroup-data offset)))
2446       (while (cdr data)
2447         (when (= (gnus-data-number (cadr data)) article)
2448           (setcdr data (cddr data))
2449           (when offset
2450             (gnus-data-update-list (cdr data) offset))
2451           (setq data nil
2452                 gnus-newsgroup-data-reverse nil))
2453         (setq data (cdr data))))))
2454
2455 (defmacro gnus-data-list (backward)
2456   `(if ,backward
2457        (or gnus-newsgroup-data-reverse
2458            (setq gnus-newsgroup-data-reverse
2459                  (reverse gnus-newsgroup-data)))
2460      gnus-newsgroup-data))
2461
2462 (defun gnus-data-update-list (data offset)
2463   "Add OFFSET to the POS of all data entries in DATA."
2464   (setq gnus-newsgroup-data-reverse nil)
2465   (while data
2466     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2467     (setq data (cdr data))))
2468
2469 (defun gnus-summary-article-pseudo-p (article)
2470   "Say whether this article is a pseudo article or not."
2471   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2472
2473 (defmacro gnus-summary-article-sparse-p (article)
2474   "Say whether this article is a sparse article or not."
2475   `(memq ,article gnus-newsgroup-sparse))
2476
2477 (defmacro gnus-summary-article-ancient-p (article)
2478   "Say whether this article is a sparse article or not."
2479   `(memq ,article gnus-newsgroup-ancient))
2480
2481 (defun gnus-article-parent-p (number)
2482   "Say whether this article is a parent or not."
2483   (let ((data (gnus-data-find-list number)))
2484     (and (cdr data)                     ; There has to be an article after...
2485          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2486             (gnus-data-level (nth 1 data))))))
2487
2488 (defun gnus-article-children (number)
2489   "Return a list of all children to NUMBER."
2490   (let* ((data (gnus-data-find-list number))
2491          (level (gnus-data-level (car data)))
2492          children)
2493     (setq data (cdr data))
2494     (while (and data
2495                 (= (gnus-data-level (car data)) (1+ level)))
2496       (push (gnus-data-number (car data)) children)
2497       (setq data (cdr data)))
2498     children))
2499
2500 (defmacro gnus-summary-skip-intangible ()
2501   "If the current article is intangible, then jump to a different article."
2502   '(let ((to (get-text-property (point) 'gnus-intangible)))
2503      (and to (gnus-summary-goto-subject to))))
2504
2505 (defmacro gnus-summary-article-intangible-p ()
2506   "Say whether this article is intangible or not."
2507   '(get-text-property (point) 'gnus-intangible))
2508
2509 (defun gnus-article-read-p (article)
2510   "Say whether ARTICLE is read or not."
2511   (not (or (memq article gnus-newsgroup-marked)
2512            (memq article gnus-newsgroup-unreads)
2513            (memq article gnus-newsgroup-unselected)
2514            (memq article gnus-newsgroup-dormant))))
2515
2516 ;; Some summary mode macros.
2517
2518 (defmacro gnus-summary-article-number ()
2519   "The article number of the article on the current line.
2520 If there isn's an article number here, then we return the current
2521 article number."
2522   '(progn
2523      (gnus-summary-skip-intangible)
2524      (or (get-text-property (point) 'gnus-number)
2525          (gnus-summary-last-subject))))
2526
2527 (defmacro gnus-summary-article-header (&optional number)
2528   "Return the header of article NUMBER."
2529   `(gnus-data-header (gnus-data-find
2530                       ,(or number '(gnus-summary-article-number)))))
2531
2532 (defmacro gnus-summary-thread-level (&optional number)
2533   "Return the level of thread that starts with article NUMBER."
2534   `(if (and (eq gnus-summary-make-false-root 'dummy)
2535             (get-text-property (point) 'gnus-intangible))
2536        0
2537      (gnus-data-level (gnus-data-find
2538                        ,(or number '(gnus-summary-article-number))))))
2539
2540 (defmacro gnus-summary-article-mark (&optional number)
2541   "Return the mark of article NUMBER."
2542   `(gnus-data-mark (gnus-data-find
2543                     ,(or number '(gnus-summary-article-number)))))
2544
2545 (defmacro gnus-summary-article-pos (&optional number)
2546   "Return the position of the line of article NUMBER."
2547   `(gnus-data-pos (gnus-data-find
2548                    ,(or number '(gnus-summary-article-number)))))
2549
2550 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2551 (defmacro gnus-summary-article-subject (&optional number)
2552   "Return current subject string or nil if nothing."
2553   `(let ((headers
2554           ,(if number
2555                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2556              '(gnus-data-header (assq (gnus-summary-article-number)
2557                                       gnus-newsgroup-data)))))
2558      (and headers
2559           (vectorp headers)
2560           (mail-header-subject headers))))
2561
2562 (defmacro gnus-summary-article-score (&optional number)
2563   "Return current article score."
2564   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2565                   gnus-newsgroup-scored))
2566        gnus-summary-default-score 0))
2567
2568 (defun gnus-summary-article-children (&optional number)
2569   "Return a list of article numbers that are children of article NUMBER."
2570   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2571          (level (gnus-data-level (car data)))
2572          l children)
2573     (while (and (setq data (cdr data))
2574                 (> (setq l (gnus-data-level (car data))) level))
2575       (and (= (1+ level) l)
2576            (push (gnus-data-number (car data))
2577                  children)))
2578     (nreverse children)))
2579
2580 (defun gnus-summary-article-parent (&optional number)
2581   "Return the article number of the parent of article NUMBER."
2582   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2583                                     (gnus-data-list t)))
2584          (level (gnus-data-level (car data))))
2585     (if (zerop level)
2586         ()                              ; This is a root.
2587       ;; We search until we find an article with a level less than
2588       ;; this one.  That function has to be the parent.
2589       (while (and (setq data (cdr data))
2590                   (not (< (gnus-data-level (car data)) level))))
2591       (and data (gnus-data-number (car data))))))
2592
2593 (defun gnus-unread-mark-p (mark)
2594   "Say whether MARK is the unread mark."
2595   (= mark gnus-unread-mark))
2596
2597 (defun gnus-read-mark-p (mark)
2598   "Say whether MARK is one of the marks that mark as read.
2599 This is all marks except unread, ticked, dormant, and expirable."
2600   (not (or (= mark gnus-unread-mark)
2601            (= mark gnus-ticked-mark)
2602            (= mark gnus-dormant-mark)
2603            (= mark gnus-expirable-mark))))
2604
2605 (defmacro gnus-article-mark (number)
2606   "Return the MARK of article NUMBER.
2607 This macro should only be used when computing the mark the \"first\"
2608 time; i.e., when generating the summary lines.  After that,
2609 `gnus-summary-article-mark' should be used to examine the
2610 marks of articles."
2611   `(cond
2612     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2613     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2614     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2615     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2616     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2617     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2618     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2619     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2620            gnus-ancient-mark))))
2621
2622 ;; Saving hidden threads.
2623
2624 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2625 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2626
2627 (defmacro gnus-save-hidden-threads (&rest forms)
2628   "Save hidden threads, eval FORMS, and restore the hidden threads."
2629   (let ((config (make-symbol "config")))
2630     `(let ((,config (gnus-hidden-threads-configuration)))
2631        (unwind-protect
2632            (save-excursion
2633              ,@forms)
2634          (gnus-restore-hidden-threads-configuration ,config)))))
2635
2636 (defun gnus-data-compute-positions ()
2637   "Compute the positions of all articles."
2638   (setq gnus-newsgroup-data-reverse nil)
2639   (let ((data gnus-newsgroup-data))
2640     (save-excursion
2641       (gnus-save-hidden-threads
2642         (gnus-summary-show-all-threads)
2643         (goto-char (point-min))
2644         (while data
2645           (while (get-text-property (point) 'gnus-intangible)
2646             (forward-line 1))
2647           (gnus-data-set-pos (car data) (+ (point) 3))
2648           (setq data (cdr data))
2649           (forward-line 1))))))
2650
2651 (defun gnus-hidden-threads-configuration ()
2652   "Return the current hidden threads configuration."
2653   (save-excursion
2654     (let (config)
2655       (goto-char (point-min))
2656       (while (search-forward "\r" nil t)
2657         (push (1- (point)) config))
2658       config)))
2659
2660 (defun gnus-restore-hidden-threads-configuration (config)
2661   "Restore hidden threads configuration from CONFIG."
2662   (save-excursion
2663     (let (point buffer-read-only)
2664       (while (setq point (pop config))
2665         (when (and (< point (point-max))
2666                    (goto-char point)
2667                    (eq (char-after) ?\n))
2668           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2669
2670 ;; Various summary mode internalish functions.
2671
2672 (defun gnus-mouse-pick-article (e)
2673   (interactive "e")
2674   (mouse-set-point e)
2675   (gnus-summary-next-page nil t))
2676
2677 (defun gnus-summary-set-display-table ()
2678   "Change the display table.
2679 Odd characters have a tendency to mess
2680 up nicely formatted displays - we make all possible glyphs
2681 display only a single character."
2682
2683   ;; We start from the standard display table, if any.
2684   (let ((table (or (copy-sequence standard-display-table)
2685                    (make-display-table)))
2686         (i 32))
2687     ;; Nix out all the control chars...
2688     (while (>= (setq i (1- i)) 0)
2689       (aset table i [??]))
2690     ;; ... but not newline and cr, of course.  (cr is necessary for the
2691     ;; selective display).
2692     (aset table ?\n nil)
2693     (aset table ?\r nil)
2694     ;; We keep TAB as well.
2695     (aset table ?\t nil)
2696     ;; We nix out any glyphs over 126 that are not set already.
2697     (let ((i 256))
2698       (while (>= (setq i (1- i)) 127)
2699         ;; Only modify if the entry is nil.
2700         (unless (aref table i)
2701           (aset table i [??]))))
2702     (setq buffer-display-table table)))
2703
2704 (defun gnus-summary-buffer-name (group)
2705   "Return the summary buffer name of GROUP."
2706   (concat "*Summary " group "*"))
2707
2708 (defun gnus-summary-setup-buffer (group)
2709   "Initialize summary buffer."
2710   (let ((buffer (gnus-summary-buffer-name group)))
2711     (if (get-buffer buffer)
2712         (progn
2713           (set-buffer buffer)
2714           (setq gnus-summary-buffer (current-buffer))
2715           (not gnus-newsgroup-prepared))
2716       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2717       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2718       (gnus-summary-mode group)
2719       (when gnus-carpal
2720         (gnus-carpal-setup-buffer 'summary))
2721       (unless gnus-single-article-buffer
2722         (make-local-variable 'gnus-article-buffer)
2723         (make-local-variable 'gnus-article-current)
2724         (make-local-variable 'gnus-original-article-buffer))
2725       (setq gnus-newsgroup-name group)
2726       ;; Set any local variables in the group parameters.
2727       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2728       t)))
2729
2730 (defun gnus-set-global-variables ()
2731   "Set the global equivalents of the buffer-local variables.
2732 They are set to the latest values they had.  These reflect the summary
2733 buffer that was in action when the last article was fetched."
2734   (when (eq major-mode 'gnus-summary-mode)
2735     (setq gnus-summary-buffer (current-buffer))
2736     (let ((name gnus-newsgroup-name)
2737           (marked gnus-newsgroup-marked)
2738           (unread gnus-newsgroup-unreads)
2739           (headers gnus-current-headers)
2740           (data gnus-newsgroup-data)
2741           (summary gnus-summary-buffer)
2742           (article-buffer gnus-article-buffer)
2743           (original gnus-original-article-buffer)
2744           (gac gnus-article-current)
2745           (reffed gnus-reffed-article-number)
2746           (score-file gnus-current-score-file)
2747           (default-charset gnus-newsgroup-charset)
2748           vlist)
2749       (let ((locals gnus-newsgroup-variables))
2750         (while locals
2751           (if (consp (car locals))
2752               (push (eval (caar locals)) vlist)
2753             (push (eval (car locals)) vlist))
2754           (setq locals (cdr locals)))
2755         (setq vlist (nreverse vlist)))
2756       (save-excursion
2757         (set-buffer gnus-group-buffer)
2758         (setq gnus-newsgroup-name name
2759               gnus-newsgroup-marked marked
2760               gnus-newsgroup-unreads unread
2761               gnus-current-headers headers
2762               gnus-newsgroup-data data
2763               gnus-article-current gac
2764               gnus-summary-buffer summary
2765               gnus-article-buffer article-buffer
2766               gnus-original-article-buffer original
2767               gnus-reffed-article-number reffed
2768               gnus-current-score-file score-file
2769               gnus-newsgroup-charset default-charset)
2770         (let ((locals gnus-newsgroup-variables))
2771           (while locals
2772             (if (consp (car locals))
2773                 (set (caar locals) (pop vlist))
2774               (set (car locals) (pop vlist)))
2775             (setq locals (cdr locals))))
2776         ;; The article buffer also has local variables.
2777         (when (gnus-buffer-live-p gnus-article-buffer)
2778           (set-buffer gnus-article-buffer)
2779           (setq gnus-summary-buffer summary))))))
2780
2781 (defun gnus-summary-article-unread-p (article)
2782   "Say whether ARTICLE is unread or not."
2783   (memq article gnus-newsgroup-unreads))
2784
2785 (defun gnus-summary-first-article-p (&optional article)
2786   "Return whether ARTICLE is the first article in the buffer."
2787   (if (not (setq article (or article (gnus-summary-article-number))))
2788       nil
2789     (eq article (caar gnus-newsgroup-data))))
2790
2791 (defun gnus-summary-last-article-p (&optional article)
2792   "Return whether ARTICLE is the last article in the buffer."
2793   (if (not (setq article (or article (gnus-summary-article-number))))
2794       ;; All non-existent numbers are the last article.  :-)
2795       t
2796     (not (cdr (gnus-data-find-list article)))))
2797
2798 (defun gnus-make-thread-indent-array ()
2799   (let ((n 200))
2800     (unless (and gnus-thread-indent-array
2801                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2802       (setq gnus-thread-indent-array (make-vector 201 "")
2803             gnus-thread-indent-array-level gnus-thread-indent-level)
2804       (while (>= n 0)
2805         (aset gnus-thread-indent-array n
2806               (make-string (* n gnus-thread-indent-level) ? ))
2807         (setq n (1- n))))))
2808
2809 (defun gnus-update-summary-mark-positions ()
2810   "Compute where the summary marks are to go."
2811   (save-excursion
2812     (when (gnus-buffer-exists-p gnus-summary-buffer)
2813       (set-buffer gnus-summary-buffer))
2814     (let ((gnus-replied-mark 129)
2815           (gnus-score-below-mark 130)
2816           (gnus-score-over-mark 130)
2817           (gnus-download-mark 131)
2818           (spec gnus-summary-line-format-spec)
2819           gnus-visual pos)
2820       (save-excursion
2821         (gnus-set-work-buffer)
2822         (let ((gnus-summary-line-format-spec spec)
2823               (gnus-newsgroup-downloadable '((0 . t))))
2824           (gnus-summary-insert-line
2825            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2826            0 nil 128 t nil "" nil 1)
2827           (goto-char (point-min))
2828           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2829                                              (- (point) 2)))))
2830           (goto-char (point-min))
2831           (push (cons 'replied (and (search-forward "\201" nil t)
2832                                     (- (point) 2)))
2833                 pos)
2834           (goto-char (point-min))
2835           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2836                 pos)
2837           (goto-char (point-min))
2838           (push (cons 'download
2839                       (and (search-forward "\203" nil t) (- (point) 2)))
2840                 pos)))
2841       (setq gnus-summary-mark-positions pos))))
2842
2843 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2844   "Insert a dummy root in the summary buffer."
2845   (beginning-of-line)
2846   (gnus-add-text-properties
2847    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2848    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2849
2850 (defun gnus-summary-from-or-to-or-newsgroups (header)
2851   (let ((to (cdr (assq 'To (mail-header-extra header))))
2852         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2853         (default-mime-charset (with-current-buffer gnus-summary-buffer
2854                                 default-mime-charset)))
2855     (cond
2856      ((and to
2857            gnus-ignored-from-addresses
2858            (string-match gnus-ignored-from-addresses
2859                          (mail-header-from header)))
2860       (concat "-> "
2861               (or (car (funcall gnus-extract-address-components
2862                                 (funcall
2863                                  gnus-decode-encoded-word-function to)))
2864                   (funcall gnus-decode-encoded-word-function to))))
2865      ((and newsgroups
2866            gnus-ignored-from-addresses
2867            (string-match gnus-ignored-from-addresses
2868                          (mail-header-from header)))
2869       (concat "=> " newsgroups))
2870      (t
2871       (or (car (funcall gnus-extract-address-components
2872                         (mail-header-from header)))
2873           (mail-header-from header))))))
2874
2875 (defun gnus-summary-insert-line (gnus-tmp-header
2876                                  gnus-tmp-level gnus-tmp-current
2877                                  gnus-tmp-unread gnus-tmp-replied
2878                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2879                                  &optional gnus-tmp-dummy gnus-tmp-score
2880                                  gnus-tmp-process)
2881   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2882          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2883          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2884          (gnus-tmp-score-char
2885           (if (or (null gnus-summary-default-score)
2886                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2887                       gnus-summary-zcore-fuzz))
2888               ?  ;Whitespace
2889             (if (< gnus-tmp-score gnus-summary-default-score)
2890                 gnus-score-below-mark gnus-score-over-mark)))
2891          (gnus-tmp-replied
2892           (cond (gnus-tmp-process gnus-process-mark)
2893                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2894                  gnus-cached-mark)
2895                 (gnus-tmp-replied gnus-replied-mark)
2896                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2897                  gnus-saved-mark)
2898                 (t gnus-no-mark)))
2899          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2900          (gnus-tmp-name
2901           (cond
2902            ((string-match "<[^>]+> *$" gnus-tmp-from)
2903             (let ((beg (match-beginning 0)))
2904               (or (and (string-match "^\".+\"" gnus-tmp-from)
2905                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2906                   (substring gnus-tmp-from 0 beg))))
2907            ((string-match "(.+)" gnus-tmp-from)
2908             (substring gnus-tmp-from
2909                        (1+ (match-beginning 0)) (1- (match-end 0))))
2910            (t gnus-tmp-from)))
2911          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2912          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2913          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2914          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2915          (buffer-read-only nil))
2916     (when (string= gnus-tmp-name "")
2917       (setq gnus-tmp-name gnus-tmp-from))
2918     (unless (numberp gnus-tmp-lines)
2919       (setq gnus-tmp-lines 0))
2920     (gnus-put-text-property-excluding-characters-with-faces
2921      (point)
2922      (progn (eval gnus-summary-line-format-spec) (point))
2923      'gnus-number gnus-tmp-number)
2924     (when (gnus-visual-p 'summary-highlight 'highlight)
2925       (forward-line -1)
2926       (gnus-run-hooks 'gnus-summary-update-hook)
2927       (forward-line 1))))
2928
2929 (defun gnus-summary-update-line (&optional dont-update)
2930   "Update summary line after change."
2931   (when (and gnus-summary-default-score
2932              (not gnus-summary-inhibit-highlight))
2933     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2934            (article (gnus-summary-article-number))
2935            (score (gnus-summary-article-score article)))
2936       (unless dont-update
2937         (if (and gnus-summary-mark-below
2938                  (< (gnus-summary-article-score)
2939                     gnus-summary-mark-below))
2940             ;; This article has a low score, so we mark it as read.
2941             (when (memq article gnus-newsgroup-unreads)
2942               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2943           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2944             ;; This article was previously marked as read on account
2945             ;; of a low score, but now it has risen, so we mark it as
2946             ;; unread.
2947             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2948         (gnus-summary-update-mark
2949          (if (or (null gnus-summary-default-score)
2950                  (<= (abs (- score gnus-summary-default-score))
2951                      gnus-summary-zcore-fuzz))
2952              ?  ;Whitespace
2953            (if (< score gnus-summary-default-score)
2954                gnus-score-below-mark gnus-score-over-mark))
2955          'score))
2956       ;; Do visual highlighting.
2957       (when (gnus-visual-p 'summary-highlight 'highlight)
2958         (gnus-run-hooks 'gnus-summary-update-hook)))))
2959
2960 (defvar gnus-tmp-new-adopts nil)
2961
2962 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2963   "Return the number of articles in THREAD.
2964 This may be 0 in some cases -- if none of the articles in
2965 the thread are to be displayed."
2966   (let* ((number
2967           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2968           (cond
2969            ((not (listp thread))
2970             1)
2971            ((and (consp thread) (cdr thread))
2972             (apply
2973              '+ 1 (mapcar
2974                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2975            ((null thread)
2976             1)
2977            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2978             1)
2979            (t 0))))
2980     (when (and level (zerop level) gnus-tmp-new-adopts)
2981       (incf number
2982             (apply '+ (mapcar
2983                        'gnus-summary-number-of-articles-in-thread
2984                        gnus-tmp-new-adopts))))
2985     (if char
2986         (if (> number 1) gnus-not-empty-thread-mark
2987           gnus-empty-thread-mark)
2988       number)))
2989
2990 (defun gnus-summary-set-local-parameters (group)
2991   "Go through the local params of GROUP and set all variable specs in that list."
2992   (let ((params (gnus-group-find-parameter group))
2993         elem)
2994     (while params
2995       (setq elem (car params)
2996             params (cdr params))
2997       (and (consp elem)                 ; Has to be a cons.
2998            (consp (cdr elem))           ; The cdr has to be a list.
2999            (symbolp (car elem))         ; Has to be a symbol in there.
3000            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
3001            (ignore-errors               ; So we set it.
3002              (make-local-variable (car elem))
3003              (set (car elem) (eval (nth 1 elem))))))))
3004
3005 (defun gnus-summary-read-group (group &optional show-all no-article
3006                                       kill-buffer no-display backward
3007                                       select-articles)
3008   "Start reading news in newsgroup GROUP.
3009 If SHOW-ALL is non-nil, already read articles are also listed.
3010 If NO-ARTICLE is non-nil, no article is selected initially.
3011 If NO-DISPLAY, don't generate a summary buffer."
3012   (let (result)
3013     (while (and group
3014                 (null (setq result
3015                             (let ((gnus-auto-select-next nil))
3016                               (or (gnus-summary-read-group-1
3017                                    group show-all no-article
3018                                    kill-buffer no-display
3019                                    select-articles)
3020                                   (setq show-all nil
3021                                         select-articles nil)))))
3022                 (eq gnus-auto-select-next 'quietly))
3023       (set-buffer gnus-group-buffer)
3024       ;; The entry function called above goes to the next
3025       ;; group automatically, so we go two groups back
3026       ;; if we are searching for the previous group.
3027       (when backward
3028         (gnus-group-prev-unread-group 2))
3029       (if (not (equal group (gnus-group-group-name)))
3030           (setq group (gnus-group-group-name))
3031         (setq group nil)))
3032     result))
3033
3034 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3035   "Directly jump to the other GROUP from summary buffer.
3036 If SHOW-ALL is non-nil, already read articles are also listed."
3037   (interactive
3038    (if (eq gnus-summary-buffer (current-buffer))
3039        (list (completing-read
3040               "Group: " gnus-active-hashtb nil t
3041               (when (and gnus-newsgroup-name
3042                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3043                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3044               'gnus-group-history)
3045              current-prefix-arg)
3046      (error "%s must be invoked from a gnus summary buffer." this-command)))
3047   (unless (or (zerop (length group))
3048               (and gnus-newsgroup-name
3049                    (string-equal gnus-newsgroup-name group)))
3050     (gnus-summary-exit)
3051     (gnus-summary-read-group group show-all
3052                              gnus-dont-select-after-jump-to-other-group)))
3053
3054 (defun gnus-summary-read-group-1 (group show-all no-article
3055                                         kill-buffer no-display
3056                                         &optional select-articles)
3057   ;; Killed foreign groups can't be entered.
3058   ;;  (when (and (not (gnus-group-native-p group))
3059   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3060   ;;    (error "Dead non-native groups can't be entered"))
3061   (gnus-message 5 "Retrieving newsgroup: %s..."
3062                 (gnus-group-decoded-name group))
3063   (let* ((new-group (gnus-summary-setup-buffer group))
3064          (quit-config (gnus-group-quit-config group))
3065          (did-select (and new-group (gnus-select-newsgroup
3066                                      group show-all select-articles))))
3067     (cond
3068      ;; This summary buffer exists already, so we just select it.
3069      ((not new-group)
3070       (gnus-set-global-variables)
3071       (when kill-buffer
3072         (gnus-kill-or-deaden-summary kill-buffer))
3073       (gnus-configure-windows 'summary 'force)
3074       (gnus-set-mode-line 'summary)
3075       (gnus-summary-position-point)
3076       (message "")
3077       t)
3078      ;; We couldn't select this group.
3079      ((null did-select)
3080       (when (and (eq major-mode 'gnus-summary-mode)
3081                  (not (equal (current-buffer) kill-buffer)))
3082         (kill-buffer (current-buffer))
3083         (if (not quit-config)
3084             (progn
3085               ;; Update the info -- marks might need to be removed,
3086               ;; for instance.
3087               (gnus-summary-update-info)
3088               (set-buffer gnus-group-buffer)
3089               (gnus-group-jump-to-group group)
3090               (gnus-group-next-unread-group 1))
3091           (gnus-handle-ephemeral-exit quit-config)))
3092       (let ((grpinfo (gnus-get-info group)))
3093         (if (null (gnus-info-read grpinfo))
3094             (gnus-message 3 "Group %s contains no messages" 
3095                           (gnus-group-decoded-name group))
3096           (gnus-message 3 "Can't select group")))
3097       nil)
3098      ;; The user did a `C-g' while prompting for number of articles,
3099      ;; so we exit this group.
3100      ((eq did-select 'quit)
3101       (and (eq major-mode 'gnus-summary-mode)
3102            (not (equal (current-buffer) kill-buffer))
3103            (kill-buffer (current-buffer)))
3104       (when kill-buffer
3105         (gnus-kill-or-deaden-summary kill-buffer))
3106       (if (not quit-config)
3107           (progn
3108             (set-buffer gnus-group-buffer)
3109             (gnus-group-jump-to-group group)
3110             (gnus-group-next-unread-group 1)
3111             (gnus-configure-windows 'group 'force))
3112         (gnus-handle-ephemeral-exit quit-config))
3113       ;; Finally signal the quit.
3114       (signal 'quit nil))
3115      ;; The group was successfully selected.
3116      (t
3117       (gnus-set-global-variables)
3118       ;; Save the active value in effect when the group was entered.
3119       (setq gnus-newsgroup-active
3120             (gnus-copy-sequence
3121              (gnus-active gnus-newsgroup-name)))
3122       ;; You can change the summary buffer in some way with this hook.
3123       (gnus-run-hooks 'gnus-select-group-hook)
3124       (gnus-update-format-specifications
3125        nil 'summary 'summary-mode 'summary-dummy)
3126       (gnus-update-summary-mark-positions)
3127       ;; Do score processing.
3128       (when gnus-use-scoring
3129         (gnus-possibly-score-headers))
3130       ;; Check whether to fill in the gaps in the threads.
3131       (when gnus-build-sparse-threads
3132         (gnus-build-sparse-threads))
3133       ;; Find the initial limit.
3134       (if gnus-show-threads
3135           (if show-all
3136               (let ((gnus-newsgroup-dormant nil))
3137                 (gnus-summary-initial-limit show-all))
3138             (gnus-summary-initial-limit show-all))
3139         ;; When untreaded, all articles are always shown.
3140         (setq gnus-newsgroup-limit
3141               (mapcar
3142                (lambda (header) (mail-header-number header))
3143                gnus-newsgroup-headers)))
3144       ;; Generate the summary buffer.
3145       (unless no-display
3146         (gnus-summary-prepare))
3147       (when gnus-use-trees
3148         (gnus-tree-open group)
3149         (setq gnus-summary-highlight-line-function
3150               'gnus-tree-highlight-article))
3151       ;; If the summary buffer is empty, but there are some low-scored
3152       ;; articles or some excluded dormants, we include these in the
3153       ;; buffer.
3154       (when (and (zerop (buffer-size))
3155                  (not no-display))
3156         (cond (gnus-newsgroup-dormant
3157                (gnus-summary-limit-include-dormant))
3158               ((and gnus-newsgroup-scored show-all)
3159                (gnus-summary-limit-include-expunged t))))
3160       ;; Function `gnus-apply-kill-file' must be called in this hook.
3161       (gnus-run-hooks 'gnus-apply-kill-hook)
3162       (if (and (zerop (buffer-size))
3163                (not no-display))
3164           (progn
3165             ;; This newsgroup is empty.
3166             (gnus-summary-catchup-and-exit nil t)
3167             (gnus-message 6 "No unread news")
3168             (when kill-buffer
3169               (gnus-kill-or-deaden-summary kill-buffer))
3170             ;; Return nil from this function.
3171             nil)
3172         ;; Hide conversation thread subtrees.  We cannot do this in
3173         ;; gnus-summary-prepare-hook since kill processing may not
3174         ;; work with hidden articles.
3175         (and gnus-show-threads
3176              gnus-thread-hide-subtree
3177              (gnus-summary-hide-all-threads))
3178         (when kill-buffer
3179           (gnus-kill-or-deaden-summary kill-buffer))
3180         ;; Show first unread article if requested.
3181         (if (and (not no-article)
3182                  (not no-display)
3183                  gnus-newsgroup-unreads
3184                  gnus-auto-select-first)
3185             (progn
3186               (gnus-configure-windows 'summary)
3187               (cond
3188                ((eq gnus-auto-select-first 'best)
3189                 (gnus-summary-best-unread-article))
3190                ((eq gnus-auto-select-first t)
3191                 (gnus-summary-first-unread-article))
3192                ((gnus-functionp gnus-auto-select-first)
3193                 (funcall gnus-auto-select-first))))
3194           ;; Don't select any articles, just move point to the first
3195           ;; article in the group.
3196           (goto-char (point-min))
3197           (gnus-summary-position-point)
3198           (gnus-configure-windows 'summary 'force)
3199           (gnus-set-mode-line 'summary))
3200         (when (get-buffer-window gnus-group-buffer t)
3201           ;; Gotta use windows, because recenter does weird stuff if
3202           ;; the current buffer ain't the displayed window.
3203           (let ((owin (selected-window)))
3204             (select-window (get-buffer-window gnus-group-buffer t))
3205             (when (gnus-group-goto-group group)
3206               (recenter))
3207             (select-window owin)))
3208         ;; Mark this buffer as "prepared".
3209         (setq gnus-newsgroup-prepared t)
3210         (gnus-run-hooks 'gnus-summary-prepared-hook)
3211         t)))))
3212
3213 (defun gnus-summary-prepare ()
3214   "Generate the summary buffer."
3215   (interactive)
3216   (let ((buffer-read-only nil))
3217     (erase-buffer)
3218     (setq gnus-newsgroup-data nil
3219           gnus-newsgroup-data-reverse nil)
3220     (gnus-run-hooks 'gnus-summary-generate-hook)
3221     ;; Generate the buffer, either with threads or without.
3222     (when gnus-newsgroup-headers
3223       (gnus-summary-prepare-threads
3224        (if gnus-show-threads
3225            (gnus-sort-gathered-threads
3226             (funcall gnus-summary-thread-gathering-function
3227                      (gnus-sort-threads
3228                       (gnus-cut-threads (gnus-make-threads)))))
3229          ;; Unthreaded display.
3230          (gnus-sort-articles gnus-newsgroup-headers))))
3231     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3232     ;; Call hooks for modifying summary buffer.
3233     (goto-char (point-min))
3234     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3235
3236 (defsubst gnus-general-simplify-subject (subject)
3237   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3238   (setq subject
3239         (cond
3240          ;; Truncate the subject.
3241          (gnus-simplify-subject-functions
3242           (gnus-map-function gnus-simplify-subject-functions subject))
3243          ((numberp gnus-summary-gather-subject-limit)
3244           (setq subject (gnus-simplify-subject-re subject))
3245           (if (> (length subject) gnus-summary-gather-subject-limit)
3246               (substring subject 0 gnus-summary-gather-subject-limit)
3247             subject))
3248          ;; Fuzzily simplify it.
3249          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3250           (gnus-simplify-subject-fuzzy subject))
3251          ;; Just remove the leading "Re:".
3252          (t
3253           (gnus-simplify-subject-re subject))))
3254
3255   (if (and gnus-summary-gather-exclude-subject
3256            (string-match gnus-summary-gather-exclude-subject subject))
3257       nil                               ; This article shouldn't be gathered
3258     subject))
3259
3260 (defun gnus-summary-simplify-subject-query ()
3261   "Query where the respool algorithm would put this article."
3262   (interactive)
3263   (gnus-summary-select-article)
3264   (message "%s"
3265            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3266
3267 (defun gnus-gather-threads-by-subject (threads)
3268   "Gather threads by looking at Subject headers."
3269   (if (not gnus-summary-make-false-root)
3270       threads
3271     (let ((hashtb (gnus-make-hashtable 1024))
3272           (prev threads)
3273           (result threads)
3274           subject hthread whole-subject)
3275       (while threads
3276         (setq subject (gnus-general-simplify-subject
3277                        (setq whole-subject (mail-header-subject
3278                                             (caar threads)))))
3279         (when subject
3280           (if (setq hthread (gnus-gethash subject hashtb))
3281               (progn
3282                 ;; We enter a dummy root into the thread, if we
3283                 ;; haven't done that already.
3284                 (unless (stringp (caar hthread))
3285                   (setcar hthread (list whole-subject (car hthread))))
3286                 ;; We add this new gathered thread to this gathered
3287                 ;; thread.
3288                 (setcdr (car hthread)
3289                         (nconc (cdar hthread) (list (car threads))))
3290                 ;; Remove it from the list of threads.
3291                 (setcdr prev (cdr threads))
3292                 (setq threads prev))
3293             ;; Enter this thread into the hash table.
3294             (gnus-sethash subject threads hashtb)))
3295         (setq prev threads)
3296         (setq threads (cdr threads)))
3297       result)))
3298
3299 (defun gnus-gather-threads-by-references (threads)
3300   "Gather threads by looking at References headers."
3301   (let ((idhashtb (gnus-make-hashtable 1024))
3302         (thhashtb (gnus-make-hashtable 1024))
3303         (prev threads)
3304         (result threads)
3305         ids references id gthread gid entered ref)
3306     (while threads
3307       (when (setq references (mail-header-references (caar threads)))
3308         (setq id (mail-header-id (caar threads))
3309               ids (gnus-split-references references)
3310               entered nil)
3311         (while (setq ref (pop ids))
3312           (setq ids (delete ref ids))
3313           (if (not (setq gid (gnus-gethash ref idhashtb)))
3314               (progn
3315                 (gnus-sethash ref id idhashtb)
3316                 (gnus-sethash id threads thhashtb))
3317             (setq gthread (gnus-gethash gid thhashtb))
3318             (unless entered
3319               ;; We enter a dummy root into the thread, if we
3320               ;; haven't done that already.
3321               (unless (stringp (caar gthread))
3322                 (setcar gthread (list (mail-header-subject (caar gthread))
3323                                       (car gthread))))
3324               ;; We add this new gathered thread to this gathered
3325               ;; thread.
3326               (setcdr (car gthread)
3327                       (nconc (cdar gthread) (list (car threads)))))
3328             ;; Add it into the thread hash table.
3329             (gnus-sethash id gthread thhashtb)
3330             (setq entered t)
3331             ;; Remove it from the list of threads.
3332             (setcdr prev (cdr threads))
3333             (setq threads prev))))
3334       (setq prev threads)
3335       (setq threads (cdr threads)))
3336     result))
3337
3338 (defun gnus-sort-gathered-threads (threads)
3339   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3340   (let ((result threads))
3341     (while threads
3342       (when (stringp (caar threads))
3343         (setcdr (car threads)
3344                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3345       (setq threads (cdr threads)))
3346     result))
3347
3348 (defun gnus-thread-loop-p (root thread)
3349   "Say whether ROOT is in THREAD."
3350   (let ((stack (list thread))
3351         (infloop 0)
3352         th)
3353     (while (setq thread (pop stack))
3354       (setq th (cdr thread))
3355       (while (and th
3356                   (not (eq (caar th) root)))
3357         (pop th))
3358       (if th
3359           ;; We have found a loop.
3360           (let (ref-dep)
3361             (setcdr thread (delq (car th) (cdr thread)))
3362             (if (boundp (setq ref-dep (intern "none"
3363                                               gnus-newsgroup-dependencies)))
3364                 (setcdr (symbol-value ref-dep)
3365                         (nconc (cdr (symbol-value ref-dep))
3366                                (list (car th))))
3367               (set ref-dep (list nil (car th))))
3368             (setq infloop 1
3369                   stack nil))
3370         ;; Push all the subthreads onto the stack.
3371         (push (cdr thread) stack)))
3372     infloop))
3373
3374 (defun gnus-make-threads ()
3375   "Go through the dependency hashtb and find the roots.  Return all threads."
3376   (let (threads)
3377     (while (catch 'infloop
3378              (mapatoms
3379               (lambda (refs)
3380                 ;; Deal with self-referencing References loops.
3381                 (when (and (car (symbol-value refs))
3382                            (not (zerop
3383                                  (apply
3384                                   '+
3385                                   (mapcar
3386                                    (lambda (thread)
3387                                      (gnus-thread-loop-p
3388                                       (car (symbol-value refs)) thread))
3389                                    (cdr (symbol-value refs)))))))
3390                   (setq threads nil)
3391                   (throw 'infloop t))
3392                 (unless (car (symbol-value refs))
3393                   ;; These threads do not refer back to any other articles,
3394                   ;; so they're roots.
3395                   (setq threads (append (cdr (symbol-value refs)) threads))))
3396               gnus-newsgroup-dependencies)))
3397     threads))
3398
3399 ;; Build the thread tree.
3400 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3401   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3402
3403 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3404 if it was already present.
3405
3406 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3407 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3408 Message-IDs will be renamed be renamed to a unique Message-ID before
3409 being entered.
3410
3411 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3412   (let* ((id (mail-header-id header))
3413          (id-dep (and id (intern id dependencies)))
3414          ref ref-dep ref-header)
3415     ;; Enter this `header' in the `dependencies' table.
3416     (cond
3417      ((not id-dep)
3418       (setq header nil))
3419      ;; The first two cases do the normal part: enter a new `header'
3420      ;; in the `dependencies' table.
3421      ((not (boundp id-dep))
3422       (set id-dep (list header)))
3423      ((null (car (symbol-value id-dep)))
3424       (setcar (symbol-value id-dep) header))
3425
3426      ;; From here the `header' was already present in the
3427      ;; `dependencies' table.
3428      (force-new
3429       ;; Overrides an existing entry;
3430       ;; just set the header part of the entry.
3431       (setcar (symbol-value id-dep) header))
3432
3433      ;; Renames the existing `header' to a unique Message-ID.
3434      ((not gnus-summary-ignore-duplicates)
3435       ;; An article with this Message-ID has already been seen.
3436       ;; We rename the Message-ID.
3437       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3438            (list header))
3439       (mail-header-set-id header id))
3440
3441      ;; The last case ignores an existing entry, except it adds any
3442      ;; additional Xrefs (in case the two articles came from different
3443      ;; servers.
3444      ;; Also sets `header' to `nil' meaning that the `dependencies'
3445      ;; table was *not* modified.
3446      (t
3447       (mail-header-set-xref
3448        (car (symbol-value id-dep))
3449        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3450                    "")
3451                (or (mail-header-xref header) "")))
3452       (setq header nil)))
3453
3454     (when header
3455       ;; First check if that we are not creating a References loop.
3456       (setq ref (gnus-parent-id (mail-header-references header)))
3457       (while (and ref
3458                   (setq ref-dep (intern-soft ref dependencies))
3459                   (boundp ref-dep)
3460                   (setq ref-header (car (symbol-value ref-dep))))
3461         (if (string= id ref)
3462             ;; Yuk!  This is a reference loop.  Make the article be a
3463             ;; root article.
3464             (progn
3465               (mail-header-set-references (car (symbol-value id-dep)) "none")
3466               (setq ref nil))
3467           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3468       (setq ref (gnus-parent-id (mail-header-references header)))
3469       (setq ref-dep (intern (or ref "none") dependencies))
3470       (if (boundp ref-dep)
3471           (setcdr (symbol-value ref-dep)
3472                   (nconc (cdr (symbol-value ref-dep))
3473                          (list (symbol-value id-dep))))
3474         (set ref-dep (list nil (symbol-value id-dep)))))
3475     header))
3476
3477 (defun gnus-build-sparse-threads ()
3478   (let ((headers gnus-newsgroup-headers)
3479         (mail-parse-charset gnus-newsgroup-charset)
3480         (gnus-summary-ignore-duplicates t)
3481         header references generation relations
3482         subject child end new-child date)
3483     ;; First we create an alist of generations/relations, where
3484     ;; generations is how much we trust the relation, and the relation
3485     ;; is parent/child.
3486     (gnus-message 7 "Making sparse threads...")
3487     (save-excursion
3488       (nnheader-set-temp-buffer " *gnus sparse threads*")
3489       (while (setq header (pop headers))
3490         (when (and (setq references (mail-header-references header))
3491                    (not (string= references "")))
3492           (insert references)
3493           (setq child (mail-header-id header)
3494                 subject (mail-header-subject header)
3495                 date (mail-header-date header)
3496                 generation 0)
3497           (while (search-backward ">" nil t)
3498             (setq end (1+ (point)))
3499             (when (search-backward "<" nil t)
3500               (setq new-child (buffer-substring (point) end))
3501               (push (list (incf generation)
3502                           child (setq child new-child)
3503                           subject date)
3504                     relations)))
3505           (when child
3506             (push (list (1+ generation) child nil subject) relations))
3507           (erase-buffer)))
3508       (kill-buffer (current-buffer)))
3509     ;; Sort over trustworthiness.
3510     (mapcar
3511      (lambda (relation)
3512        (when (gnus-dependencies-add-header
3513               (make-full-mail-header-from-decoded-header
3514                gnus-reffed-article-number
3515                (nth 3 relation) "" (or (nth 4 relation) "")
3516                (nth 1 relation)
3517                (or (nth 2 relation) "") 0 0 "")
3518               gnus-newsgroup-dependencies nil)
3519          (push gnus-reffed-article-number gnus-newsgroup-limit)
3520          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3521          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3522                gnus-newsgroup-reads)
3523          (decf gnus-reffed-article-number)))
3524      (sort relations 'car-less-than-car))
3525     (gnus-message 7 "Making sparse threads...done")))
3526
3527 (defun gnus-build-old-threads ()
3528   ;; Look at all the articles that refer back to old articles, and
3529   ;; fetch the headers for the articles that aren't there.  This will
3530   ;; build complete threads - if the roots haven't been expired by the
3531   ;; server, that is.
3532   (let ((mail-parse-charset gnus-newsgroup-charset)
3533         id heads)
3534     (mapatoms
3535      (lambda (refs)
3536        (when (not (car (symbol-value refs)))
3537          (setq heads (cdr (symbol-value refs)))
3538          (while heads
3539            (if (memq (mail-header-number (caar heads))
3540                      gnus-newsgroup-dormant)
3541                (setq heads (cdr heads))
3542              (setq id (symbol-name refs))
3543              (while (and (setq id (gnus-build-get-header id))
3544                          (not (car (gnus-id-to-thread id)))))
3545              (setq heads nil)))))
3546      gnus-newsgroup-dependencies)))
3547
3548 ;; This function has to be called with point after the article number
3549 ;; on the beginning of the line.
3550 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3551   (let ((eol (gnus-point-at-eol))
3552         (buffer (current-buffer))
3553         header)
3554
3555     ;; overview: [num subject from date id refs chars lines misc]
3556     (unwind-protect
3557         (progn
3558           (narrow-to-region (point) eol)
3559           (unless (eobp)
3560             (forward-char))
3561
3562           (setq header
3563                 (make-full-mail-header
3564                  number                         ; number
3565                  (nnheader-nov-field)           ; subject
3566                  (nnheader-nov-field)           ; from
3567                  (nnheader-nov-field)           ; date
3568                  (nnheader-nov-read-message-id) ; id
3569                  (nnheader-nov-field)           ; refs
3570                  (nnheader-nov-read-integer)    ; chars
3571                  (nnheader-nov-read-integer)    ; lines
3572                  (unless (eobp)
3573                    (if (looking-at "Xref: ")
3574                        (goto-char (match-end 0)))
3575                    (nnheader-nov-field))        ; Xref
3576                  (nnheader-nov-parse-extra))))  ; extra
3577
3578       (widen))
3579
3580     (when gnus-alter-header-function
3581       (funcall gnus-alter-header-function header))
3582     (gnus-dependencies-add-header header dependencies force-new)))
3583
3584 (defun gnus-build-get-header (id)
3585   "Look through the buffer of NOV lines and find the header to ID.
3586 Enter this line into the dependencies hash table, and return
3587 the id of the parent article (if any)."
3588   (let ((deps gnus-newsgroup-dependencies)
3589         found header)
3590     (prog1
3591         (save-excursion
3592           (set-buffer nntp-server-buffer)
3593           (let ((case-fold-search nil))
3594             (goto-char (point-min))
3595             (while (and (not found)
3596                         (search-forward id nil t))
3597               (beginning-of-line)
3598               (setq found (looking-at
3599                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3600                                    (regexp-quote id))))
3601               (or found (beginning-of-line 2)))
3602             (when found
3603               (beginning-of-line)
3604               (and
3605                (setq header (gnus-nov-parse-line
3606                              (read (current-buffer)) deps))
3607                (gnus-parent-id (mail-header-references header))))))
3608       (when header
3609         (let ((number (mail-header-number header)))
3610           (push number gnus-newsgroup-limit)
3611           (push header gnus-newsgroup-headers)
3612           (if (memq number gnus-newsgroup-unselected)
3613               (progn
3614                 (push number gnus-newsgroup-unreads)
3615                 (setq gnus-newsgroup-unselected
3616                       (delq number gnus-newsgroup-unselected)))
3617             (push number gnus-newsgroup-ancient)))))))
3618
3619 (defun gnus-build-all-threads ()
3620   "Read all the headers."
3621   (let ((gnus-summary-ignore-duplicates t)
3622         (mail-parse-charset gnus-newsgroup-charset)
3623         (dependencies gnus-newsgroup-dependencies)
3624         header article)
3625     (save-excursion
3626       (set-buffer nntp-server-buffer)
3627       (let ((case-fold-search nil))
3628         (goto-char (point-min))
3629         (while (not (eobp))
3630           (ignore-errors
3631             (setq article (read (current-buffer))
3632                   header (gnus-nov-parse-line article dependencies)))
3633           (when header
3634             (save-excursion
3635               (set-buffer gnus-summary-buffer)
3636               (push header gnus-newsgroup-headers)
3637               (if (memq (setq article (mail-header-number header))
3638                         gnus-newsgroup-unselected)
3639                   (progn
3640                     (push article gnus-newsgroup-unreads)
3641                     (setq gnus-newsgroup-unselected
3642                           (delq article gnus-newsgroup-unselected)))
3643                 (push article gnus-newsgroup-ancient)))
3644             (forward-line 1)))))))
3645
3646 (defun gnus-summary-update-article-line (article header)
3647   "Update the line for ARTICLE using HEADERS."
3648   (let* ((id (mail-header-id header))
3649          (thread (gnus-id-to-thread id)))
3650     (unless thread
3651       (error "Article in no thread"))
3652     ;; Update the thread.
3653     (setcar thread header)
3654     (gnus-summary-goto-subject article)
3655     (let* ((datal (gnus-data-find-list article))
3656            (data (car datal))
3657            (length (when (cdr datal)
3658                      (- (gnus-data-pos data)
3659                         (gnus-data-pos (cadr datal)))))
3660            (buffer-read-only nil)
3661            (level (gnus-summary-thread-level)))
3662       (gnus-delete-line)
3663       (gnus-summary-insert-line
3664        header level nil (gnus-article-mark article)
3665        (memq article gnus-newsgroup-replied)
3666        (memq article gnus-newsgroup-expirable)
3667        ;; Only insert the Subject string when it's different
3668        ;; from the previous Subject string.
3669        (if (and
3670             gnus-show-threads
3671             (gnus-subject-equal
3672              (condition-case ()
3673                  (mail-header-subject
3674                   (gnus-data-header
3675                    (cadr
3676                     (gnus-data-find-list
3677                      article
3678                      (gnus-data-list t)))))
3679                ;; Error on the side of excessive subjects.
3680                (error ""))
3681              (mail-header-subject header)))
3682            ""
3683          (mail-header-subject header))
3684        nil (cdr (assq article gnus-newsgroup-scored))
3685        (memq article gnus-newsgroup-processable))
3686       (when length
3687         (gnus-data-update-list
3688          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3689
3690 (defun gnus-summary-update-article (article &optional iheader)
3691   "Update ARTICLE in the summary buffer."
3692   (set-buffer gnus-summary-buffer)
3693   (let* ((header (gnus-summary-article-header article))
3694          (id (mail-header-id header))
3695          (data (gnus-data-find article))
3696          (thread (gnus-id-to-thread id))
3697          (references (mail-header-references header))
3698          (parent
3699           (gnus-id-to-thread
3700            (or (gnus-parent-id
3701                 (when (and references
3702                            (not (equal "" references)))
3703                   references))
3704                "none")))
3705          (buffer-read-only nil)
3706          (old (car thread)))
3707     (when thread
3708       (unless iheader
3709         (setcar thread nil)
3710         (when parent
3711           (delq thread parent)))
3712       (if (gnus-summary-insert-subject id header)
3713           ;; Set the (possibly) new article number in the data structure.
3714           (gnus-data-set-number data (gnus-id-to-article id))
3715         (setcar thread old)
3716         nil))))
3717
3718 (defun gnus-rebuild-thread (id &optional line)
3719   "Rebuild the thread containing ID.
3720 If LINE, insert the rebuilt thread starting on line LINE."
3721   (let ((buffer-read-only nil)
3722         old-pos current thread data)
3723     (if (not gnus-show-threads)
3724         (setq thread (list (car (gnus-id-to-thread id))))
3725       ;; Get the thread this article is part of.
3726       (setq thread (gnus-remove-thread id)))
3727     (setq old-pos (gnus-point-at-bol))
3728     (setq current (save-excursion
3729                     (and (zerop (forward-line -1))
3730                          (gnus-summary-article-number))))
3731     ;; If this is a gathered thread, we have to go some re-gathering.
3732     (when (stringp (car thread))
3733       (let ((subject (car thread))
3734             roots thr)
3735         (setq thread (cdr thread))
3736         (while thread
3737           (unless (memq (setq thr (gnus-id-to-thread
3738                                    (gnus-root-id
3739                                     (mail-header-id (caar thread)))))
3740                         roots)
3741             (push thr roots))
3742           (setq thread (cdr thread)))
3743         ;; We now have all (unique) roots.
3744         (if (= (length roots) 1)
3745             ;; All the loose roots are now one solid root.
3746             (setq thread (car roots))
3747           (setq thread (cons subject (gnus-sort-threads roots))))))
3748     (let (threads)
3749       ;; We then insert this thread into the summary buffer.
3750       (when line
3751         (goto-char (point-min))
3752         (forward-line (1- line)))
3753       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3754         (if gnus-show-threads
3755             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3756           (gnus-summary-prepare-unthreaded thread))
3757         (setq data (nreverse gnus-newsgroup-data))
3758         (setq threads gnus-newsgroup-threads))
3759       ;; We splice the new data into the data structure.
3760       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3761       ;;!!! then we want to insert at the beginning of the buffer.
3762       ;;!!! That happens to be true with Gnus now, but that may
3763       ;;!!! change in the future.  Perhaps.
3764       (gnus-data-enter-list
3765        (if line nil current) data (- (point) old-pos))
3766       (setq gnus-newsgroup-threads
3767             (nconc threads gnus-newsgroup-threads))
3768       (gnus-data-compute-positions))))
3769
3770 (defun gnus-number-to-header (number)
3771   "Return the header for article NUMBER."
3772   (let ((headers gnus-newsgroup-headers))
3773     (while (and headers
3774                 (not (= number (mail-header-number (car headers)))))
3775       (pop headers))
3776     (when headers
3777       (car headers))))
3778
3779 (defun gnus-parent-headers (in-headers &optional generation)
3780   "Return the headers of the GENERATIONeth parent of HEADERS."
3781   (unless generation
3782     (setq generation 1))
3783   (let ((parent t)
3784         (headers in-headers)
3785         references)
3786     (while (and parent
3787                 (not (zerop generation))
3788                 (setq references (mail-header-references headers)))
3789       (setq headers (if (and references
3790                              (setq parent (gnus-parent-id references)))
3791                         (car (gnus-id-to-thread parent))
3792                       nil))
3793       (decf generation))
3794     (and (not (eq headers in-headers))
3795          headers)))
3796
3797 (defun gnus-id-to-thread (id)
3798   "Return the (sub-)thread where ID appears."
3799   (gnus-gethash id gnus-newsgroup-dependencies))
3800
3801 (defun gnus-id-to-article (id)
3802   "Return the article number of ID."
3803   (let ((thread (gnus-id-to-thread id)))
3804     (when (and thread
3805                (car thread))
3806       (mail-header-number (car thread)))))
3807
3808 (defun gnus-id-to-header (id)
3809   "Return the article headers of ID."
3810   (car (gnus-id-to-thread id)))
3811
3812 (defun gnus-article-displayed-root-p (article)
3813   "Say whether ARTICLE is a root(ish) article."
3814   (let ((level (gnus-summary-thread-level article))
3815         (refs (mail-header-references  (gnus-summary-article-header article)))
3816         particle)
3817     (cond
3818      ((null level) nil)
3819      ((zerop level) t)
3820      ((null refs) t)
3821      ((null (gnus-parent-id refs)) t)
3822      ((and (= 1 level)
3823            (null (setq particle (gnus-id-to-article
3824                                  (gnus-parent-id refs))))
3825            (null (gnus-summary-thread-level particle)))))))
3826
3827 (defun gnus-root-id (id)
3828   "Return the id of the root of the thread where ID appears."
3829   (let (last-id prev)
3830     (while (and id (setq prev (car (gnus-id-to-thread id))))
3831       (setq last-id id
3832             id (gnus-parent-id (mail-header-references prev))))
3833     last-id))
3834
3835 (defun gnus-articles-in-thread (thread)
3836   "Return the list of articles in THREAD."
3837   (cons (mail-header-number (car thread))
3838         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3839
3840 (defun gnus-remove-thread (id &optional dont-remove)
3841   "Remove the thread that has ID in it."
3842   (let (headers thread last-id)
3843     ;; First go up in this thread until we find the root.
3844     (setq last-id (gnus-root-id id)
3845           headers (message-flatten-list (gnus-id-to-thread last-id)))
3846     ;; We have now found the real root of this thread.  It might have
3847     ;; been gathered into some loose thread, so we have to search
3848     ;; through the threads to find the thread we wanted.
3849     (let ((threads gnus-newsgroup-threads)
3850           sub)
3851       (while threads
3852         (setq sub (car threads))
3853         (if (stringp (car sub))
3854             ;; This is a gathered thread, so we look at the roots
3855             ;; below it to find whether this article is in this
3856             ;; gathered root.
3857             (progn
3858               (setq sub (cdr sub))
3859               (while sub
3860                 (when (member (caar sub) headers)
3861                   (setq thread (car threads)
3862                         threads nil
3863                         sub nil))
3864                 (setq sub (cdr sub))))
3865           ;; It's an ordinary thread, so we check it.
3866           (when (eq (car sub) (car headers))
3867             (setq thread sub
3868                   threads nil)))
3869         (setq threads (cdr threads)))
3870       ;; If this article is in no thread, then it's a root.
3871       (if thread
3872           (unless dont-remove
3873             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3874         (setq thread (gnus-id-to-thread last-id)))
3875       (when thread
3876         (prog1
3877             thread                      ; We return this thread.
3878           (unless dont-remove
3879             (if (stringp (car thread))
3880                 (progn
3881                   ;; If we use dummy roots, then we have to remove the
3882                   ;; dummy root as well.
3883                   (when (eq gnus-summary-make-false-root 'dummy)
3884                     ;; We go to the dummy root by going to
3885                     ;; the first sub-"thread", and then one line up.
3886                     (gnus-summary-goto-article
3887                      (mail-header-number (caadr thread)))
3888                     (forward-line -1)
3889                     (gnus-delete-line)
3890                     (gnus-data-compute-positions))
3891                   (setq thread (cdr thread))
3892                   (while thread
3893                     (gnus-remove-thread-1 (car thread))
3894                     (setq thread (cdr thread))))
3895               (gnus-remove-thread-1 thread))))))))
3896
3897 (defun gnus-remove-thread-1 (thread)
3898   "Remove the thread THREAD recursively."
3899   (let ((number (mail-header-number (pop thread)))
3900         d)
3901     (setq thread (reverse thread))
3902     (while thread
3903       (gnus-remove-thread-1 (pop thread)))
3904     (when (setq d (gnus-data-find number))
3905       (goto-char (gnus-data-pos d))
3906       (gnus-summary-show-thread)
3907       (gnus-data-remove
3908        number
3909        (- (gnus-point-at-bol)
3910           (prog1
3911               (1+ (gnus-point-at-eol))
3912             (gnus-delete-line)))))))
3913
3914 (defun gnus-sort-threads-1 (threads func)
3915   (sort (mapcar (lambda (thread)
3916                   (cons (car thread)
3917                         (and (cdr thread)
3918                              (gnus-sort-threads-1 (cdr thread) func))))
3919                 threads) func))
3920
3921 (defun gnus-sort-threads (threads)
3922   "Sort THREADS."
3923   (if (not gnus-thread-sort-functions)
3924       threads
3925     (gnus-message 8 "Sorting threads...")
3926     (prog1
3927         (gnus-sort-threads-1
3928          threads
3929          (gnus-make-sort-function gnus-thread-sort-functions))
3930       (gnus-message 8 "Sorting threads...done"))))
3931
3932 (defun gnus-sort-articles (articles)
3933   "Sort ARTICLES."
3934   (when gnus-article-sort-functions
3935     (gnus-message 7 "Sorting articles...")
3936     (prog1
3937         (setq gnus-newsgroup-headers
3938               (sort articles (gnus-make-sort-function
3939                               gnus-article-sort-functions)))
3940       (gnus-message 7 "Sorting articles...done"))))
3941
3942 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3943 (defmacro gnus-thread-header (thread)
3944   "Return header of first article in THREAD.
3945 Note that THREAD must never, ever be anything else than a variable -
3946 using some other form will lead to serious barfage."
3947   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3948   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3949   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3950         (vector thread) 2))
3951
3952 (defsubst gnus-article-sort-by-number (h1 h2)
3953   "Sort articles by article number."
3954   (< (mail-header-number h1)
3955      (mail-header-number h2)))
3956
3957 (defun gnus-thread-sort-by-number (h1 h2)
3958   "Sort threads by root article number."
3959   (gnus-article-sort-by-number
3960    (gnus-thread-header h1) (gnus-thread-header h2)))
3961
3962 (defsubst gnus-article-sort-by-lines (h1 h2)
3963   "Sort articles by article Lines header."
3964   (< (mail-header-lines h1)
3965      (mail-header-lines h2)))
3966
3967 (defun gnus-thread-sort-by-lines (h1 h2)
3968   "Sort threads by root article Lines header."
3969   (gnus-article-sort-by-lines
3970    (gnus-thread-header h1) (gnus-thread-header h2)))
3971
3972 (defsubst gnus-article-sort-by-chars (h1 h2)
3973   "Sort articles by octet length."
3974   (< (mail-header-chars h1)
3975      (mail-header-chars h2)))
3976
3977 (defun gnus-thread-sort-by-chars (h1 h2)
3978   "Sort threads by root article octet length."
3979   (gnus-article-sort-by-chars
3980    (gnus-thread-header h1) (gnus-thread-header h2)))
3981
3982 (defsubst gnus-article-sort-by-author (h1 h2)
3983   "Sort articles by root author."
3984   (string-lessp
3985    (let ((addr (car (mime-entity-read-field h1 'From))))
3986      (or (std11-full-name-string addr)
3987          (std11-address-string addr)
3988          ""))
3989    (let ((addr (car (mime-entity-read-field h2 'From))))
3990      (or (std11-full-name-string addr)
3991          (std11-address-string addr)
3992          ""))
3993    ))
3994
3995 (defun gnus-thread-sort-by-author (h1 h2)
3996   "Sort threads by root author."
3997   (gnus-article-sort-by-author
3998    (gnus-thread-header h1)  (gnus-thread-header h2)))
3999
4000 (defsubst gnus-article-sort-by-subject (h1 h2)
4001   "Sort articles by root subject."
4002   (string-lessp
4003    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4004    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4005
4006 (defun gnus-thread-sort-by-subject (h1 h2)
4007   "Sort threads by root subject."
4008   (gnus-article-sort-by-subject
4009    (gnus-thread-header h1) (gnus-thread-header h2)))
4010
4011 (defsubst gnus-article-sort-by-date (h1 h2)
4012   "Sort articles by root article date."
4013   (time-less-p
4014    (gnus-date-get-time (mail-header-date h1))
4015    (gnus-date-get-time (mail-header-date h2))))
4016
4017 (defun gnus-thread-sort-by-date (h1 h2)
4018   "Sort threads by root article date."
4019   (gnus-article-sort-by-date
4020    (gnus-thread-header h1) (gnus-thread-header h2)))
4021
4022 (defsubst gnus-article-sort-by-score (h1 h2)
4023   "Sort articles by root article score.
4024 Unscored articles will be counted as having a score of zero."
4025   (> (or (cdr (assq (mail-header-number h1)
4026                     gnus-newsgroup-scored))
4027          gnus-summary-default-score 0)
4028      (or (cdr (assq (mail-header-number h2)
4029                     gnus-newsgroup-scored))
4030          gnus-summary-default-score 0)))
4031
4032 (defun gnus-thread-sort-by-score (h1 h2)
4033   "Sort threads by root article score."
4034   (gnus-article-sort-by-score
4035    (gnus-thread-header h1) (gnus-thread-header h2)))
4036
4037 (defun gnus-thread-sort-by-total-score (h1 h2)
4038   "Sort threads by the sum of all scores in the thread.
4039 Unscored articles will be counted as having a score of zero."
4040   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4041
4042 (defun gnus-thread-total-score (thread)
4043   ;; This function find the total score of THREAD.
4044   (cond ((null thread)
4045          0)
4046         ((consp thread)
4047          (if (stringp (car thread))
4048              (apply gnus-thread-score-function 0
4049                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4050            (gnus-thread-total-score-1 thread)))
4051         (t
4052          (gnus-thread-total-score-1 (list thread)))))
4053
4054 (defun gnus-thread-total-score-1 (root)
4055   ;; This function find the total score of the thread below ROOT.
4056   (setq root (car root))
4057   (apply gnus-thread-score-function
4058          (or (append
4059               (mapcar 'gnus-thread-total-score
4060                       (cdr (gnus-id-to-thread (mail-header-id root))))
4061               (when (> (mail-header-number root) 0)
4062                 (list (or (cdr (assq (mail-header-number root)
4063                                      gnus-newsgroup-scored))
4064                           gnus-summary-default-score 0))))
4065              (list gnus-summary-default-score)
4066              '(0))))
4067
4068 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4069 (defvar gnus-tmp-prev-subject nil)
4070 (defvar gnus-tmp-false-parent nil)
4071 (defvar gnus-tmp-root-expunged nil)
4072 (defvar gnus-tmp-dummy-line nil)
4073
4074 (eval-when-compile (defvar gnus-tmp-header))
4075 (defun gnus-extra-header (type &optional header)
4076   "Return the extra header of TYPE."
4077   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4078       ""))
4079
4080 (defun gnus-summary-prepare-threads (threads)
4081   "Prepare summary buffer from THREADS and indentation LEVEL.
4082 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4083 or a straight list of headers."
4084   (gnus-message 7 "Generating summary...")
4085
4086   (setq gnus-newsgroup-threads threads)
4087   (beginning-of-line)
4088
4089   (let ((gnus-tmp-level 0)
4090         (default-score (or gnus-summary-default-score 0))
4091         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4092         thread number subject stack state gnus-tmp-gathered beg-match
4093         new-roots gnus-tmp-new-adopts thread-end
4094         gnus-tmp-header gnus-tmp-unread
4095         gnus-tmp-replied gnus-tmp-subject-or-nil
4096         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4097         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4098         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4099
4100     (setq gnus-tmp-prev-subject nil)
4101
4102     (if (vectorp (car threads))
4103         ;; If this is a straight (sic) list of headers, then a
4104         ;; threaded summary display isn't required, so we just create
4105         ;; an unthreaded one.
4106         (gnus-summary-prepare-unthreaded threads)
4107
4108       ;; Do the threaded display.
4109
4110       (while (or threads stack gnus-tmp-new-adopts new-roots)
4111
4112         (if (and (= gnus-tmp-level 0)
4113                  (or (not stack)
4114                      (= (caar stack) 0))
4115                  (not gnus-tmp-false-parent)
4116                  (or gnus-tmp-new-adopts new-roots))
4117             (if gnus-tmp-new-adopts
4118                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4119                       thread (list (car gnus-tmp-new-adopts))
4120                       gnus-tmp-header (caar thread)
4121                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4122               (when new-roots
4123                 (setq thread (list (car new-roots))
4124                       gnus-tmp-header (caar thread)
4125                       new-roots (cdr new-roots))))
4126
4127           (if threads
4128               ;; If there are some threads, we do them before the
4129               ;; threads on the stack.
4130               (setq thread threads
4131                     gnus-tmp-header (caar thread))
4132             ;; There were no current threads, so we pop something off
4133             ;; the stack.
4134             (setq state (car stack)
4135                   gnus-tmp-level (car state)
4136                   thread (cdr state)
4137                   stack (cdr stack)
4138                   gnus-tmp-header (caar thread))))
4139
4140         (setq gnus-tmp-false-parent nil)
4141         (setq gnus-tmp-root-expunged nil)
4142         (setq thread-end nil)
4143
4144         (if (stringp gnus-tmp-header)
4145             ;; The header is a dummy root.
4146             (cond
4147              ((eq gnus-summary-make-false-root 'adopt)
4148               ;; We let the first article adopt the rest.
4149               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4150                                                (cddar thread)))
4151               (setq gnus-tmp-gathered
4152                     (nconc (mapcar
4153                             (lambda (h) (mail-header-number (car h)))
4154                             (cddar thread))
4155                            gnus-tmp-gathered))
4156               (setq thread (cons (list (caar thread)
4157                                        (cadar thread))
4158                                  (cdr thread)))
4159               (setq gnus-tmp-level -1
4160                     gnus-tmp-false-parent t))
4161              ((eq gnus-summary-make-false-root 'empty)
4162               ;; We print adopted articles with empty subject fields.
4163               (setq gnus-tmp-gathered
4164                     (nconc (mapcar
4165                             (lambda (h) (mail-header-number (car h)))
4166                             (cddar thread))
4167                            gnus-tmp-gathered))
4168               (setq gnus-tmp-level -1))
4169              ((eq gnus-summary-make-false-root 'dummy)
4170               ;; We remember that we probably want to output a dummy
4171               ;; root.
4172               (setq gnus-tmp-dummy-line gnus-tmp-header)
4173               (setq gnus-tmp-prev-subject gnus-tmp-header))
4174              (t
4175               ;; We do not make a root for the gathered
4176               ;; sub-threads at all.
4177               (setq gnus-tmp-level -1)))
4178
4179           (setq number (mail-header-number gnus-tmp-header)
4180                 subject (mail-header-subject gnus-tmp-header))
4181
4182           (cond
4183            ;; If the thread has changed subject, we might want to make
4184            ;; this subthread into a root.
4185            ((and (null gnus-thread-ignore-subject)
4186                  (not (zerop gnus-tmp-level))
4187                  gnus-tmp-prev-subject
4188                  (not (inline
4189                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4190             (setq new-roots (nconc new-roots (list (car thread)))
4191                   thread-end t
4192                   gnus-tmp-header nil))
4193            ;; If the article lies outside the current limit,
4194            ;; then we do not display it.
4195            ((not (memq number gnus-newsgroup-limit))
4196             (setq gnus-tmp-gathered
4197                   (nconc (mapcar
4198                           (lambda (h) (mail-header-number (car h)))
4199                           (cdar thread))
4200                          gnus-tmp-gathered))
4201             (setq gnus-tmp-new-adopts (if (cdar thread)
4202                                           (append gnus-tmp-new-adopts
4203                                                   (cdar thread))
4204                                         gnus-tmp-new-adopts)
4205                   thread-end t
4206                   gnus-tmp-header nil)
4207             (when (zerop gnus-tmp-level)
4208               (setq gnus-tmp-root-expunged t)))
4209            ;; Perhaps this article is to be marked as read?
4210            ((and gnus-summary-mark-below
4211                  (< (or (cdr (assq number gnus-newsgroup-scored))
4212                         default-score)
4213                     gnus-summary-mark-below)
4214                  ;; Don't touch sparse articles.
4215                  (not (gnus-summary-article-sparse-p number))
4216                  (not (gnus-summary-article-ancient-p number)))
4217             (setq gnus-newsgroup-unreads
4218                   (delq number gnus-newsgroup-unreads))
4219             (if gnus-newsgroup-auto-expire
4220                 (push number gnus-newsgroup-expirable)
4221               (push (cons number gnus-low-score-mark)
4222                     gnus-newsgroup-reads))))
4223
4224           (when gnus-tmp-header
4225             ;; We may have an old dummy line to output before this
4226             ;; article.
4227             (when (and gnus-tmp-dummy-line
4228                        (gnus-subject-equal
4229                         gnus-tmp-dummy-line
4230                         (mail-header-subject gnus-tmp-header)))
4231               (gnus-summary-insert-dummy-line
4232                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4233               (setq gnus-tmp-dummy-line nil))
4234
4235             ;; Compute the mark.
4236             (setq gnus-tmp-unread (gnus-article-mark number))
4237
4238             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4239                                   gnus-tmp-header gnus-tmp-level)
4240                   gnus-newsgroup-data)
4241
4242             ;; Actually insert the line.
4243             (setq
4244              gnus-tmp-subject-or-nil
4245              (cond
4246               ((and gnus-thread-ignore-subject
4247                     gnus-tmp-prev-subject
4248                     (not (inline (gnus-subject-equal
4249                                   gnus-tmp-prev-subject subject))))
4250                subject)
4251               ((zerop gnus-tmp-level)
4252                (if (and (eq gnus-summary-make-false-root 'empty)
4253                         (memq number gnus-tmp-gathered)
4254                         gnus-tmp-prev-subject
4255                         (inline (gnus-subject-equal
4256                                  gnus-tmp-prev-subject subject)))
4257                    gnus-summary-same-subject
4258                  subject))
4259               (t gnus-summary-same-subject)))
4260             (if (and (eq gnus-summary-make-false-root 'adopt)
4261                      (= gnus-tmp-level 1)
4262                      (memq number gnus-tmp-gathered))
4263                 (setq gnus-tmp-opening-bracket ?\<
4264                       gnus-tmp-closing-bracket ?\>)
4265               (setq gnus-tmp-opening-bracket ?\[
4266                     gnus-tmp-closing-bracket ?\]))
4267             (setq
4268              gnus-tmp-indentation
4269              (aref gnus-thread-indent-array gnus-tmp-level)
4270              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4271              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4272                                 gnus-summary-default-score 0)
4273              gnus-tmp-score-char
4274              (if (or (null gnus-summary-default-score)
4275                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4276                          gnus-summary-zcore-fuzz))
4277                  ?  ;Whitespace
4278                (if (< gnus-tmp-score gnus-summary-default-score)
4279                    gnus-score-below-mark gnus-score-over-mark))
4280              gnus-tmp-replied
4281              (cond ((memq number gnus-newsgroup-processable)
4282                     gnus-process-mark)
4283                    ((memq number gnus-newsgroup-cached)
4284                     gnus-cached-mark)
4285                    ((memq number gnus-newsgroup-replied)
4286                     gnus-replied-mark)
4287                    ((memq number gnus-newsgroup-saved)
4288                     gnus-saved-mark)
4289                    (t gnus-no-mark))
4290              gnus-tmp-from (mail-header-from gnus-tmp-header)
4291              gnus-tmp-name
4292              (cond
4293               ((string-match "<[^>]+> *$" gnus-tmp-from)
4294                (setq beg-match (match-beginning 0))
4295                (or (and (string-match "^\".+\"" gnus-tmp-from)
4296                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4297                    (substring gnus-tmp-from 0 beg-match)))
4298               ((string-match "(.+)" gnus-tmp-from)
4299                (substring gnus-tmp-from
4300                           (1+ (match-beginning 0)) (1- (match-end 0))))
4301               (t gnus-tmp-from)))
4302             (when (string= gnus-tmp-name "")
4303               (setq gnus-tmp-name gnus-tmp-from))
4304             (unless (numberp gnus-tmp-lines)
4305               (setq gnus-tmp-lines 0))
4306             (gnus-put-text-property
4307              (point)
4308              (progn (eval gnus-summary-line-format-spec) (point))
4309              'gnus-number number)
4310             (when gnus-visual-p
4311               (forward-line -1)
4312               (gnus-run-hooks 'gnus-summary-update-hook)
4313               (forward-line 1))
4314
4315             (setq gnus-tmp-prev-subject subject)))
4316
4317         (when (nth 1 thread)
4318           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4319         (incf gnus-tmp-level)
4320         (setq threads (if thread-end nil (cdar thread)))
4321         (unless threads
4322           (setq gnus-tmp-level 0)))))
4323   (gnus-message 7 "Generating summary...done"))
4324
4325 (defun gnus-summary-prepare-unthreaded (headers)
4326   "Generate an unthreaded summary buffer based on HEADERS."
4327   (let (header number mark)
4328
4329     (beginning-of-line)
4330
4331     (while headers
4332       ;; We may have to root out some bad articles...
4333       (when (memq (setq number (mail-header-number
4334                                 (setq header (pop headers))))
4335                   gnus-newsgroup-limit)
4336         ;; Mark article as read when it has a low score.
4337         (when (and gnus-summary-mark-below
4338                    (< (or (cdr (assq number gnus-newsgroup-scored))
4339                           gnus-summary-default-score 0)
4340                       gnus-summary-mark-below)
4341                    (not (gnus-summary-article-ancient-p number)))
4342           (setq gnus-newsgroup-unreads
4343                 (delq number gnus-newsgroup-unreads))
4344           (if gnus-newsgroup-auto-expire
4345               (push number gnus-newsgroup-expirable)
4346             (push (cons number gnus-low-score-mark)
4347                   gnus-newsgroup-reads)))
4348
4349         (setq mark (gnus-article-mark number))
4350         (push (gnus-data-make number mark (1+ (point)) header 0)
4351               gnus-newsgroup-data)
4352         (gnus-summary-insert-line
4353          header 0 number
4354          mark (memq number gnus-newsgroup-replied)
4355          (memq number gnus-newsgroup-expirable)
4356          (mail-header-subject header) nil
4357          (cdr (assq number gnus-newsgroup-scored))
4358          (memq number gnus-newsgroup-processable))))))
4359
4360 (defun gnus-summary-remove-list-identifiers ()
4361   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4362   (let ((regexp (if (consp gnus-list-identifiers)
4363                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4364                   gnus-list-identifiers))
4365         changed subject)
4366     (when regexp
4367       (dolist (header gnus-newsgroup-headers)
4368         (setq subject (mail-header-subject header)
4369               changed nil)
4370         (while (string-match
4371                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4372                 subject)
4373           (setq subject
4374                 (concat (substring subject 0 (match-beginning 2))
4375                         (substring subject (match-end 0)))
4376                 changed t))
4377         (when (and changed
4378                    (string-match
4379                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4380           (setq subject
4381                 (concat (substring subject 0 (match-beginning 1))
4382                         (substring subject (match-end 1)))))
4383         (when changed
4384           (mail-header-set-subject header subject))))))
4385
4386 (defun gnus-fetch-headers (articles)
4387   "Fetch headers of ARTICLES."
4388   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4389     (gnus-message 5 "Fetching headers for %s..." name)
4390     (prog1
4391         ;;;!!! FIXME: temporary fix for an infloop on nnimap.
4392         (if (eq 'nnimap (car (gnus-find-method-for-group name)))
4393             (if (eq 'nov
4394                     (setq gnus-headers-retrieved-by
4395                           (gnus-retrieve-headers
4396                            articles gnus-newsgroup-name
4397                            ;; We might want to fetch old headers, but
4398                            ;; not if there is only 1 article.
4399                            (and (or (and
4400                                      (not (eq gnus-fetch-old-headers 'some))
4401                                      (not (numberp gnus-fetch-old-headers)))
4402                                     (> (length articles) 1))
4403                                 gnus-fetch-old-headers))))
4404                 (gnus-get-newsgroup-headers-xover
4405                  articles nil nil gnus-newsgroup-name t)
4406               (gnus-get-newsgroup-headers))
4407           (gnus-retrieve-parsed-headers
4408            articles gnus-newsgroup-name
4409            ;; We might want to fetch old headers, but
4410            ;; not if there is only 1 article.
4411            (and (or (and (not (eq gnus-fetch-old-headers 'some))
4412                          (not (numberp gnus-fetch-old-headers)))
4413                     (> (length articles) 1))
4414                 gnus-fetch-old-headers)))
4415       (gnus-message 5 "Fetching headers for %s...done" name))))
4416
4417 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4418   "Select newsgroup GROUP.
4419 If READ-ALL is non-nil, all articles in the group are selected.
4420 If SELECT-ARTICLES, only select those articles from GROUP."
4421   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4422          ;;!!! Dirty hack; should be removed.
4423          (gnus-summary-ignore-duplicates
4424           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4425               t
4426             gnus-summary-ignore-duplicates))
4427          (info (nth 2 entry))
4428          articles fetched-articles cached)
4429
4430     (unless (gnus-check-server
4431              (setq gnus-current-select-method
4432                    (gnus-find-method-for-group group)))
4433       (error "Couldn't open server"))
4434
4435     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4436         (gnus-activate-group group)     ; Or we can activate it...
4437         (progn                          ; Or we bug out.
4438           (when (equal major-mode 'gnus-summary-mode)
4439             (kill-buffer (current-buffer)))
4440           (error "Couldn't activate group %s: %s"
4441                  group (gnus-status-message group))))
4442
4443     (unless (gnus-request-group group t)
4444       (when (equal major-mode 'gnus-summary-mode)
4445         (kill-buffer (current-buffer)))
4446       (error "Couldn't request group %s: %s"
4447              group (gnus-status-message group)))
4448
4449     (setq gnus-newsgroup-name group)
4450     (setq gnus-newsgroup-unselected nil)
4451     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4452     (gnus-summary-setup-default-charset)
4453
4454     ;; Adjust and set lists of article marks.
4455     (when info
4456       (gnus-adjust-marked-articles info))
4457
4458     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4459     (setq cached
4460           (if (gnus-virtual-group-p group)
4461               gnus-newsgroup-cached
4462             (gnus-cache-articles-in-group group)))
4463
4464     (setq gnus-newsgroup-unreads
4465           (gnus-set-difference
4466            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4467            gnus-newsgroup-dormant))
4468
4469     (setq gnus-newsgroup-processable nil)
4470
4471     (gnus-update-read-articles group gnus-newsgroup-unreads)
4472
4473     (if (setq articles select-articles)
4474         (setq gnus-newsgroup-unselected
4475               (gnus-sorted-intersection
4476                gnus-newsgroup-unreads
4477                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4478       (setq articles (gnus-articles-to-read group read-all)))
4479
4480     (cond
4481      ((null articles)
4482       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4483       'quit)
4484      ((eq articles 0) nil)
4485      (t
4486       ;; Init the dependencies hash table.
4487       (setq gnus-newsgroup-dependencies
4488             (gnus-make-hashtable (length articles)))
4489       (gnus-set-global-variables)
4490       ;; Retrieve the headers and read them in.
4491       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4492
4493       ;; Suppress duplicates?
4494       (when gnus-suppress-duplicates
4495         (gnus-dup-suppress-articles))
4496
4497       ;; Set the initial limit.
4498       (setq gnus-newsgroup-limit (copy-sequence articles))
4499       ;; Remove canceled articles from the list of unread articles.
4500       (setq gnus-newsgroup-unreads
4501             (gnus-set-sorted-intersection
4502              gnus-newsgroup-unreads
4503              (setq fetched-articles
4504                    (mapcar (lambda (headers) (mail-header-number headers))
4505                            gnus-newsgroup-headers))))
4506       ;; Removed marked articles that do not exist.
4507       (gnus-update-missing-marks
4508        (gnus-sorted-complement fetched-articles articles))
4509
4510       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4511       (when cached
4512         (setq gnus-newsgroup-cached cached))
4513
4514       ;; We might want to build some more threads first.
4515       (when (and gnus-fetch-old-headers
4516                  (eq gnus-headers-retrieved-by 'nov))
4517         (if (eq gnus-fetch-old-headers 'invisible)
4518             (gnus-build-all-threads)
4519           (gnus-build-old-threads)))
4520       ;; Let the Gnus agent mark articles as read.
4521       (when gnus-agent
4522         (gnus-agent-get-undownloaded-list))
4523       ;; Remove list identifiers from subject
4524       (when gnus-list-identifiers
4525         (gnus-summary-remove-list-identifiers))
4526       ;; Check whether auto-expire is to be done in this group.
4527       (setq gnus-newsgroup-auto-expire
4528             (gnus-group-auto-expirable-p group))
4529       ;; Set up the article buffer now, if necessary.
4530       (unless gnus-single-article-buffer
4531         (gnus-article-setup-buffer))
4532       ;; First and last article in this newsgroup.
4533       (when gnus-newsgroup-headers
4534         (setq gnus-newsgroup-begin
4535               (mail-header-number (car gnus-newsgroup-headers))
4536               gnus-newsgroup-end
4537               (mail-header-number
4538                (gnus-last-element gnus-newsgroup-headers))))
4539       ;; GROUP is successfully selected.
4540       (or gnus-newsgroup-headers t)))))
4541
4542 (defun gnus-articles-to-read (group &optional read-all)
4543   "Find out what articles the user wants to read."
4544   (let* ((articles
4545           ;; Select all articles if `read-all' is non-nil, or if there
4546           ;; are no unread articles.
4547           (if (or read-all
4548                   (and (zerop (length gnus-newsgroup-marked))
4549                        (zerop (length gnus-newsgroup-unreads)))
4550                   (eq (gnus-group-find-parameter group 'display)
4551                       'all))
4552               (or
4553                (gnus-uncompress-range (gnus-active group))
4554                (gnus-cache-articles-in-group group))
4555             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4556                           (copy-sequence gnus-newsgroup-unreads))
4557                   '<)))
4558          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4559          (scored (length scored-list))
4560          (number (length articles))
4561          (marked (+ (length gnus-newsgroup-marked)
4562                     (length gnus-newsgroup-dormant)))
4563          (select
4564           (cond
4565            ((numberp read-all)
4566             read-all)
4567            (t
4568             (condition-case ()
4569                 (cond
4570                  ((and (or (<= scored marked) (= scored number))
4571                        (natnump gnus-large-newsgroup)
4572                        (> number gnus-large-newsgroup))
4573                   (let* ((cursor-in-echo-area nil)
4574                          (input (read-from-minibuffer
4575                                  (format
4576                                   "How many articles from %s (max %d): "
4577                                   (gnus-limit-string gnus-newsgroup-name 35)
4578                                   number)
4579                                  (cons (number-to-string gnus-large-newsgroup)
4580                                        0))))
4581                     (if (string-match "^[ \t]*$" input)
4582                         number
4583                       input)))
4584                  ((and (> scored marked) (< scored number)
4585                        (> (- scored number) 20))
4586                   (let ((input
4587                          (read-string
4588                           (format "%s %s (%d scored, %d total): "
4589                                   "How many articles from"
4590                                   group scored number))))
4591                     (if (string-match "^[ \t]*$" input)
4592                         number input)))
4593                  (t number))
4594               (quit
4595                (message "Quit getting the articles to read")
4596                nil))))))
4597     (setq select (if (stringp select) (string-to-number select) select))
4598     (if (or (null select) (zerop select))
4599         select
4600       (if (and (not (zerop scored)) (<= (abs select) scored))
4601           (progn
4602             (setq articles (sort scored-list '<))
4603             (setq number (length articles)))
4604         (setq articles (copy-sequence articles)))
4605
4606       (when (< (abs select) number)
4607         (if (< select 0)
4608             ;; Select the N oldest articles.
4609             (setcdr (nthcdr (1- (abs select)) articles) nil)
4610           ;; Select the N most recent articles.
4611           (setq articles (nthcdr (- number select) articles))))
4612       (setq gnus-newsgroup-unselected
4613             (gnus-sorted-intersection
4614              gnus-newsgroup-unreads
4615              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4616       (when gnus-alter-articles-to-read-function
4617         (setq gnus-newsgroup-unreads
4618               (sort
4619                (funcall gnus-alter-articles-to-read-function
4620                         gnus-newsgroup-name gnus-newsgroup-unreads)
4621                '<)))
4622       articles)))
4623
4624 (defun gnus-killed-articles (killed articles)
4625   (let (out)
4626     (while articles
4627       (when (inline (gnus-member-of-range (car articles) killed))
4628         (push (car articles) out))
4629       (setq articles (cdr articles)))
4630     out))
4631
4632 (defun gnus-uncompress-marks (marks)
4633   "Uncompress the mark ranges in MARKS."
4634   (let ((uncompressed '(score bookmark))
4635         out)
4636     (while marks
4637       (if (memq (caar marks) uncompressed)
4638           (push (car marks) out)
4639         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4640       (setq marks (cdr marks)))
4641     out))
4642
4643 (defun gnus-adjust-marked-articles (info)
4644   "Set all article lists and remove all marks that are no longer valid."
4645   (let* ((marked-lists (gnus-info-marks info))
4646          (active (gnus-active (gnus-info-group info)))
4647          (min (car active))
4648          (max (cdr active))
4649          (types gnus-article-mark-lists)
4650          (uncompressed '(score bookmark killed))
4651          marks var articles article mark)
4652
4653     (while marked-lists
4654       (setq marks (pop marked-lists))
4655       (set (setq var (intern (format "gnus-newsgroup-%s"
4656                                      (car (rassq (setq mark (car marks))
4657                                                  types)))))
4658            (if (memq (car marks) uncompressed) (cdr marks)
4659              (gnus-uncompress-range (cdr marks))))
4660
4661       (setq articles (symbol-value var))
4662
4663       ;; All articles have to be subsets of the active articles.
4664       (cond
4665        ;; Adjust "simple" lists.
4666        ((memq mark '(tick dormant expire reply save))
4667         (while articles
4668           (when (or (< (setq article (pop articles)) min) (> article max))
4669             (set var (delq article (symbol-value var))))))
4670        ;; Adjust assocs.
4671        ((memq mark uncompressed)
4672         (when (not (listp (cdr (symbol-value var))))
4673           (set var (list (symbol-value var))))
4674         (when (not (listp (cdr articles)))
4675           (setq articles (list articles)))
4676         (while articles
4677           (when (or (not (consp (setq article (pop articles))))
4678                     (< (car article) min)
4679                     (> (car article) max))
4680             (set var (delq article (symbol-value var))))))))))
4681
4682 (defun gnus-update-missing-marks (missing)
4683   "Go through the list of MISSING articles and remove them from the mark lists."
4684   (when missing
4685     (let ((types gnus-article-mark-lists)
4686           var m)
4687       ;; Go through all types.
4688       (while types
4689         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4690         (when (symbol-value var)
4691           ;; This list has articles.  So we delete all missing articles
4692           ;; from it.
4693           (setq m missing)
4694           (while m
4695             (set var (delq (pop m) (symbol-value var)))))))))
4696
4697 (defun gnus-update-marks ()
4698   "Enter the various lists of marked articles into the newsgroup info list."
4699   (let ((types gnus-article-mark-lists)
4700         (info (gnus-get-info gnus-newsgroup-name))
4701         (uncompressed '(score bookmark killed))
4702         type list newmarked symbol delta-marks)
4703     (when info
4704       ;; Add all marks lists to the list of marks lists.
4705       (while (setq type (pop types))
4706         (setq list (symbol-value
4707                     (setq symbol
4708                           (intern (format "gnus-newsgroup-%s"
4709                                           (car type))))))
4710
4711         (when list
4712           ;; Get rid of the entries of the articles that have the
4713           ;; default score.
4714           (when (and (eq (cdr type) 'score)
4715                      gnus-save-score
4716                      list)
4717             (let* ((arts list)
4718                    (prev (cons nil list))
4719                    (all prev))
4720               (while arts
4721                 (if (or (not (consp (car arts)))
4722                         (= (cdar arts) gnus-summary-default-score))
4723                     (setcdr prev (cdr arts))
4724                   (setq prev arts))
4725                 (setq arts (cdr arts)))
4726               (setq list (cdr all)))))
4727
4728         (unless (memq (cdr type) uncompressed)
4729           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4730
4731         (when (gnus-check-backend-function
4732                'request-set-mark gnus-newsgroup-name)
4733           ;; propagate flags to server, with the following exceptions:
4734           ;; uncompressed:s are not proper flags (they are cons cells)
4735           ;; cache is a internal gnus flag
4736           ;; download are local to one gnus installation (well)
4737           ;; unsend are for nndraft groups only
4738           ;; xxx: generality of this?  this suits nnimap anyway
4739           (unless (memq (cdr type) (append '(cache download unsend)
4740                                            uncompressed))
4741             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4742                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4743                    (add (gnus-remove-from-range
4744                          (gnus-copy-sequence list) old)))
4745               (when add
4746                 (push (list add 'add (list (cdr type))) delta-marks))
4747               (when del
4748                 (push (list del 'del (list (cdr type))) delta-marks)))))
4749
4750         (when list
4751           (push (cons (cdr type) list) newmarked)))
4752
4753       (when delta-marks
4754         (unless (gnus-check-group gnus-newsgroup-name)
4755           (error "Can't open server for %s" gnus-newsgroup-name))
4756         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4757
4758       ;; Enter these new marks into the info of the group.
4759       (if (nthcdr 3 info)
4760           (setcar (nthcdr 3 info) newmarked)
4761         ;; Add the marks lists to the end of the info.
4762         (when newmarked
4763           (setcdr (nthcdr 2 info) (list newmarked))))
4764
4765       ;; Cut off the end of the info if there's nothing else there.
4766       (let ((i 5))
4767         (while (and (> i 2)
4768                     (not (nth i info)))
4769           (when (nthcdr (decf i) info)
4770             (setcdr (nthcdr i info) nil)))))))
4771
4772 (defun gnus-set-mode-line (where)
4773   "Set the mode line of the article or summary buffers.
4774 If WHERE is `summary', the summary mode line format will be used."
4775   ;; Is this mode line one we keep updated?
4776   (when (and (memq where gnus-updated-mode-lines)
4777              (symbol-value
4778               (intern (format "gnus-%s-mode-line-format-spec" where))))
4779     (let (mode-string)
4780       (save-excursion
4781         ;; We evaluate this in the summary buffer since these
4782         ;; variables are buffer-local to that buffer.
4783         (set-buffer gnus-summary-buffer)
4784         ;; We bind all these variables that are used in the `eval' form
4785         ;; below.
4786         (let* ((mformat (symbol-value
4787                          (intern
4788                           (format "gnus-%s-mode-line-format-spec" where))))
4789                (gnus-tmp-group-name (gnus-group-decoded-name 
4790                                      gnus-newsgroup-name))
4791                (gnus-tmp-article-number (or gnus-current-article 0))
4792                (gnus-tmp-unread gnus-newsgroup-unreads)
4793                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4794                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4795                (gnus-tmp-unread-and-unselected
4796                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4797                             (zerop gnus-tmp-unselected))
4798                        "")
4799                       ((zerop gnus-tmp-unselected)
4800                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4801                       (t (format "{%d(+%d) more}"
4802                                  gnus-tmp-unread-and-unticked
4803                                  gnus-tmp-unselected))))
4804                (gnus-tmp-subject
4805                 (if (and gnus-current-headers
4806                          (vectorp gnus-current-headers))
4807                     (gnus-mode-string-quote
4808                      (mail-header-subject gnus-current-headers))
4809                   ""))
4810                bufname-length max-len
4811                gnus-tmp-header);; passed as argument to any user-format-funcs
4812           (setq mode-string (eval mformat))
4813           (setq bufname-length (if (string-match "%b" mode-string)
4814                                    (- (length
4815                                        (buffer-name
4816                                         (if (eq where 'summary)
4817                                             nil
4818                                           (get-buffer gnus-article-buffer))))
4819                                       2)
4820                                  0))
4821           (setq max-len (max 4 (if gnus-mode-non-string-length
4822                                    (- (window-width)
4823                                       gnus-mode-non-string-length
4824                                       bufname-length)
4825                                  (length mode-string))))
4826           ;; We might have to chop a bit of the string off...
4827           (when (> (length mode-string) max-len)
4828             (setq mode-string
4829                   (concat (gnus-truncate-string mode-string (- max-len 3))
4830                           "...")))
4831           ;; Pad the mode string a bit.
4832           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4833       ;; Update the mode line.
4834       (setq mode-line-buffer-identification
4835             (gnus-mode-line-buffer-identification (list mode-string)))
4836       (set-buffer-modified-p t))))
4837
4838 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4839   "Go through the HEADERS list and add all Xrefs to a hash table.
4840 The resulting hash table is returned, or nil if no Xrefs were found."
4841   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4842          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4843          (xref-hashtb (gnus-make-hashtable))
4844          start group entry number xrefs header)
4845     (while headers
4846       (setq header (pop headers))
4847       (when (and (setq xrefs (mail-header-xref header))
4848                  (not (memq (setq number (mail-header-number header))
4849                             unreads)))
4850         (setq start 0)
4851         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4852           (setq start (match-end 0))
4853           (setq group (if prefix
4854                           (concat prefix (substring xrefs (match-beginning 1)
4855                                                     (match-end 1)))
4856                         (substring xrefs (match-beginning 1) (match-end 1))))
4857           (setq number
4858                 (string-to-int (substring xrefs (match-beginning 2)
4859                                           (match-end 2))))
4860           (if (setq entry (gnus-gethash group xref-hashtb))
4861               (setcdr entry (cons number (cdr entry)))
4862             (gnus-sethash group (cons number nil) xref-hashtb)))))
4863     (and start xref-hashtb)))
4864
4865 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4866   "Look through all the headers and mark the Xrefs as read."
4867   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4868         name entry info xref-hashtb idlist method nth4)
4869     (save-excursion
4870       (set-buffer gnus-group-buffer)
4871       (when (setq xref-hashtb
4872                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4873         (mapatoms
4874          (lambda (group)
4875            (unless (string= from-newsgroup (setq name (symbol-name group)))
4876              (setq idlist (symbol-value group))
4877              ;; Dead groups are not updated.
4878              (and (prog1
4879                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4880                             info (nth 2 entry))
4881                     (when (stringp (setq nth4 (gnus-info-method info)))
4882                       (setq nth4 (gnus-server-to-method nth4))))
4883                   ;; Only do the xrefs if the group has the same
4884                   ;; select method as the group we have just read.
4885                   (or (gnus-methods-equal-p
4886                        nth4 (gnus-find-method-for-group from-newsgroup))
4887                       virtual
4888                       (equal nth4 (setq method (gnus-find-method-for-group
4889                                                 from-newsgroup)))
4890                       (and (equal (car nth4) (car method))
4891                            (equal (nth 1 nth4) (nth 1 method))))
4892                   gnus-use-cross-reference
4893                   (or (not (eq gnus-use-cross-reference t))
4894                       virtual
4895                       ;; Only do cross-references on subscribed
4896                       ;; groups, if that is what is wanted.
4897                       (<= (gnus-info-level info) gnus-level-subscribed))
4898                   (gnus-group-make-articles-read name idlist))))
4899          xref-hashtb)))))
4900
4901 (defun gnus-compute-read-articles (group articles)
4902   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4903          (info (nth 2 entry))
4904          (active (gnus-active group))
4905          ninfo)
4906     (when entry
4907       ;; First peel off all invalid article numbers.
4908       (when active
4909         (let ((ids articles)
4910               id first)
4911           (while (setq id (pop ids))
4912             (when (and first (> id (cdr active)))
4913               ;; We'll end up in this situation in one particular
4914               ;; obscure situation.  If you re-scan a group and get
4915               ;; a new article that is cross-posted to a different
4916               ;; group that has not been re-scanned, you might get
4917               ;; crossposted article that has a higher number than
4918               ;; Gnus believes possible.  So we re-activate this
4919               ;; group as well.  This might mean doing the
4920               ;; crossposting thingy will *increase* the number
4921               ;; of articles in some groups.  Tsk, tsk.
4922               (setq active (or (gnus-activate-group group) active)))
4923             (when (or (> id (cdr active))
4924                       (< id (car active)))
4925               (setq articles (delq id articles))))))
4926       ;; If the read list is nil, we init it.
4927       (if (and active
4928                (null (gnus-info-read info))
4929                (> (car active) 1))
4930           (setq ninfo (cons 1 (1- (car active))))
4931         (setq ninfo (gnus-info-read info)))
4932       ;; Then we add the read articles to the range.
4933       (gnus-add-to-range
4934        ninfo (setq articles (sort articles '<))))))
4935
4936 (defun gnus-group-make-articles-read (group articles)
4937   "Update the info of GROUP to say that ARTICLES are read."
4938   (let* ((num 0)
4939          (entry (gnus-gethash group gnus-newsrc-hashtb))
4940          (info (nth 2 entry))
4941          (active (gnus-active group))
4942          range)
4943     (when entry
4944       (setq range (gnus-compute-read-articles group articles))
4945       (save-excursion
4946         (set-buffer gnus-group-buffer)
4947         (gnus-undo-register
4948           `(progn
4949              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4950              (gnus-info-set-read ',info ',(gnus-info-read info))
4951              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4952              (gnus-group-update-group ,group t))))
4953       ;; Add the read articles to the range.
4954       (gnus-info-set-read info range)
4955       ;; Then we have to re-compute how many unread
4956       ;; articles there are in this group.
4957       (when active
4958         (cond
4959          ((not range)
4960           (setq num (- (1+ (cdr active)) (car active))))
4961          ((not (listp (cdr range)))
4962           (setq num (- (cdr active) (- (1+ (cdr range))
4963                                        (car range)))))
4964          (t
4965           (while range
4966             (if (numberp (car range))
4967                 (setq num (1+ num))
4968               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4969             (setq range (cdr range)))
4970           (setq num (- (cdr active) num))))
4971         ;; Update the number of unread articles.
4972         (setcar entry num)
4973         ;; Update the group buffer.
4974         (gnus-group-update-group group t)))))
4975
4976 (defvar gnus-newsgroup-none-id 0)
4977
4978 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4979   (let ((cur nntp-server-buffer)
4980         (dependencies
4981          (or dependencies
4982              (save-excursion (set-buffer gnus-summary-buffer)
4983                              gnus-newsgroup-dependencies)))
4984         headers id end ref
4985         (mail-parse-charset gnus-newsgroup-charset)
4986         (mail-parse-ignored-charsets
4987          (save-excursion (condition-case nil
4988                              (set-buffer gnus-summary-buffer)
4989                            (error))
4990                          gnus-newsgroup-ignored-charsets)))
4991     (save-excursion
4992       (set-buffer nntp-server-buffer)
4993       ;; Translate all TAB characters into SPACE characters.
4994       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4995       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4996       (gnus-run-hooks 'gnus-parse-headers-hook)
4997       (let ((case-fold-search t)
4998             in-reply-to header p lines chars ctype)
4999         (goto-char (point-min))
5000         ;; Search to the beginning of the next header.  Error messages
5001         ;; do not begin with 2 or 3.
5002         (while (re-search-forward "^[23][0-9]+ " nil t)
5003           (setq id nil
5004                 ref nil)
5005           ;; This implementation of this function, with nine
5006           ;; search-forwards instead of the one re-search-forward and
5007           ;; a case (which basically was the old function) is actually
5008           ;; about twice as fast, even though it looks messier.  You
5009           ;; can't have everything, I guess.  Speed and elegance
5010           ;; doesn't always go hand in hand.
5011           (setq
5012            header
5013            (make-full-mail-header
5014             ;; Number.
5015             (prog1
5016                 (read cur)
5017               (end-of-line)
5018               (setq p (point))
5019               (narrow-to-region (point)
5020                                 (or (and (search-forward "\n.\n" nil t)
5021                                          (- (point) 2))
5022                                     (point))))
5023             ;; Subject.
5024             (progn
5025               (goto-char p)
5026               (if (search-forward "\nsubject: " nil t)
5027                   (nnheader-header-value)
5028                 "(none)"))
5029             ;; From.
5030             (progn
5031               (goto-char p)
5032               (if (or (search-forward "\nfrom: " nil t)
5033                       (search-forward "\nfrom:" nil t))
5034                   (nnheader-header-value)
5035                 "(nobody)"))
5036             ;; Date.
5037             (progn
5038               (goto-char p)
5039               (if (search-forward "\ndate: " nil t)
5040                   (nnheader-header-value)
5041                 ""))
5042             ;; Message-ID.
5043             (progn
5044               (goto-char p)
5045               (setq id (if (re-search-forward
5046                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5047                            ;; We do it this way to make sure the Message-ID
5048                            ;; is (somewhat) syntactically valid.
5049                            (buffer-substring (match-beginning 1)
5050                                              (match-end 1))
5051                          ;; If there was no message-id, we just fake one
5052                          ;; to make subsequent routines simpler.
5053                          (nnheader-generate-fake-message-id))))
5054             ;; References.
5055             (progn
5056               (goto-char p)
5057               (if (search-forward "\nreferences: " nil t)
5058                   (progn
5059                     (setq end (point))
5060                     (prog1
5061                         (nnheader-header-value)
5062                       (setq ref
5063                             (buffer-substring
5064                              (progn
5065                                ;; (end-of-line)
5066                                (search-backward ">" end t)
5067                                (1+ (point)))
5068                              (progn
5069                                (search-backward "<" end t)
5070                                (point))))))
5071                 ;; Get the references from the in-reply-to header if there
5072                 ;; were no references and the in-reply-to header looks
5073                 ;; promising.
5074                 (if (and (search-forward "\nin-reply-to: " nil t)
5075                          (setq in-reply-to (nnheader-header-value))
5076                          (string-match "<[^>]+>" in-reply-to))
5077                     (let (ref2)
5078                       (setq ref (substring in-reply-to (match-beginning 0)
5079                                            (match-end 0)))
5080                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5081                         (setq ref2 (substring in-reply-to (match-beginning 0)
5082                                               (match-end 0)))
5083                         (when (> (length ref2) (length ref))
5084                           (setq ref ref2)))
5085                       ref)
5086                   (setq ref nil))))
5087             ;; Chars.
5088             (progn
5089               (goto-char p)
5090               (if (search-forward "\nchars: " nil t)
5091                   (if (numberp (setq chars (ignore-errors (read cur))))
5092                       chars 0)
5093                 0))
5094             ;; Lines.
5095             (progn
5096               (goto-char p)
5097               (if (search-forward "\nlines: " nil t)
5098                   (if (numberp (setq lines (ignore-errors (read cur))))
5099                       lines 0)
5100                 0))
5101             ;; Xref.
5102             (progn
5103               (goto-char p)
5104               (and (search-forward "\nxref: " nil t)
5105                    (nnheader-header-value)))
5106             ;; Extra.
5107             (when gnus-extra-headers
5108               (let ((extra gnus-extra-headers)
5109                     out)
5110                 (while extra
5111                   (goto-char p)
5112                   (when (search-forward
5113                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5114                     (push (cons (car extra) (nnheader-header-value)) out))
5115                   (pop extra))
5116                 out))))
5117           (goto-char p)
5118           (if (and (search-forward "\ncontent-type: " nil t)
5119                    (setq ctype (nnheader-header-value)))
5120               (mime-entity-set-content-type-internal
5121                header (mime-parse-Content-Type ctype)))
5122           (when (equal id ref)
5123             (setq ref nil))
5124
5125           (when gnus-alter-header-function
5126             (funcall gnus-alter-header-function header)
5127             (setq id (mail-header-id header)
5128                   ref (gnus-parent-id (mail-header-references header))))
5129
5130           (when (setq header
5131                       (gnus-dependencies-add-header
5132                        header dependencies force-new))
5133             (push header headers))
5134           (goto-char (point-max))
5135           (widen))
5136         (nreverse headers)))))
5137
5138 ;; Goes through the xover lines and returns a list of vectors
5139 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5140                                                   force-new dependencies
5141                                                   group also-fetch-heads)
5142   "Parse the news overview data in the server buffer.
5143 Return a list of headers that match SEQUENCE (see
5144 `nntp-retrieve-headers')."
5145   ;; Get the Xref when the users reads the articles since most/some
5146   ;; NNTP servers do not include Xrefs when using XOVER.
5147   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5148   (let ((mail-parse-charset gnus-newsgroup-charset)
5149         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5150         (cur nntp-server-buffer)
5151         (dependencies (or dependencies gnus-newsgroup-dependencies))
5152         number headers header)
5153     (save-excursion
5154       (set-buffer nntp-server-buffer)
5155       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5156       ;; Allow the user to mangle the headers before parsing them.
5157       (gnus-run-hooks 'gnus-parse-headers-hook)
5158       (goto-char (point-min))
5159       (while (not (eobp))
5160         (condition-case ()
5161             (while (and sequence (not (eobp)))
5162               (setq number (read cur))
5163               (while (and sequence
5164                           (< (car sequence) number))
5165                 (setq sequence (cdr sequence)))
5166               (and sequence
5167                    (eq number (car sequence))
5168                    (progn
5169                      (setq sequence (cdr sequence))
5170                      (setq header (inline
5171                                     (gnus-nov-parse-line
5172                                      number dependencies force-new))))
5173                    (push header headers))
5174               (forward-line 1))
5175           (error
5176            (gnus-error 4 "Strange nov line (%d)"
5177                        (count-lines (point-min) (point)))))
5178         (forward-line 1))
5179       ;; A common bug in inn is that if you have posted an article and
5180       ;; then retrieves the active file, it will answer correctly --
5181       ;; the new article is included.  However, a NOV entry for the
5182       ;; article may not have been generated yet, so this may fail.
5183       ;; We work around this problem by retrieving the last few
5184       ;; headers using HEAD.
5185       (if (or (not also-fetch-heads)
5186               (not sequence))
5187           ;; We (probably) got all the headers.
5188           (nreverse headers)
5189         (let ((gnus-nov-is-evil t))
5190           (nconc
5191            (nreverse headers)
5192            ;;;!!! FIXME: temporary fix for an infloop on nnimap.
5193            (if (eq 'nnimap (car (gnus-find-method-for-group group)))
5194                (when (gnus-retrieve-headers sequence group)
5195                  (gnus-get-newsgroup-headers))
5196              (gnus-retrieve-parsed-headers sequence group))))))))
5197
5198 (defun gnus-article-get-xrefs ()
5199   "Fill in the Xref value in `gnus-current-headers', if necessary.
5200 This is meant to be called in `gnus-article-internal-prepare-hook'."
5201   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5202                                  gnus-current-headers)))
5203     (or (not gnus-use-cross-reference)
5204         (not headers)
5205         (and (mail-header-xref headers)
5206              (not (string= (mail-header-xref headers) "")))
5207         (let ((case-fold-search t)
5208               xref)
5209           (save-restriction
5210             (nnheader-narrow-to-headers)
5211             (goto-char (point-min))
5212             (when (or (and (not (eobp))
5213                            (eq (downcase (char-after)) ?x)
5214                            (looking-at "Xref:"))
5215                       (search-forward "\nXref:" nil t))
5216               (goto-char (1+ (match-end 0)))
5217               (setq xref (buffer-substring (point)
5218                                            (progn (end-of-line) (point))))
5219               (mail-header-set-xref headers xref)))))))
5220
5221 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5222   "Find article ID and insert the summary line for that article.
5223 OLD-HEADER can either be a header or a line number to insert
5224 the subject line on."
5225   (let* ((line (and (numberp old-header) old-header))
5226          (old-header (and (vectorp old-header) old-header))
5227          (header (cond ((and old-header use-old-header)
5228                         old-header)
5229                        ((and (numberp id)
5230                              (gnus-number-to-header id))
5231                         (gnus-number-to-header id))
5232                        (t
5233                         (gnus-read-header id))))
5234          (number (and (numberp id) id))
5235          d)
5236     (when header
5237       ;; Rebuild the thread that this article is part of and go to the
5238       ;; article we have fetched.
5239       (when (and (not gnus-show-threads)
5240                  old-header)
5241         (when (and number
5242                    (setq d (gnus-data-find (mail-header-number old-header))))
5243           (goto-char (gnus-data-pos d))
5244           (gnus-data-remove
5245            number
5246            (- (gnus-point-at-bol)
5247               (prog1
5248                   (1+ (gnus-point-at-eol))
5249                 (gnus-delete-line))))))
5250       (when old-header
5251         (mail-header-set-number header (mail-header-number old-header)))
5252       (setq gnus-newsgroup-sparse
5253             (delq (setq number (mail-header-number header))
5254                   gnus-newsgroup-sparse))
5255       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5256       (push number gnus-newsgroup-limit)
5257       (gnus-rebuild-thread (mail-header-id header) line)
5258       (gnus-summary-goto-subject number nil t))
5259     (when (and (numberp number)
5260                (> number 0))
5261       ;; We have to update the boundaries even if we can't fetch the
5262       ;; article if ID is a number -- so that the next `P' or `N'
5263       ;; command will fetch the previous (or next) article even
5264       ;; if the one we tried to fetch this time has been canceled.
5265       (when (> number gnus-newsgroup-end)
5266         (setq gnus-newsgroup-end number))
5267       (when (< number gnus-newsgroup-begin)
5268         (setq gnus-newsgroup-begin number))
5269       (setq gnus-newsgroup-unselected
5270             (delq number gnus-newsgroup-unselected)))
5271     ;; Report back a success?
5272     (and header (mail-header-number header))))
5273
5274 ;;; Process/prefix in the summary buffer
5275
5276 (defun gnus-summary-work-articles (n)
5277   "Return a list of articles to be worked upon.
5278 The prefix argument, the list of process marked articles, and the
5279 current article will be taken into consideration."
5280   (save-excursion
5281     (set-buffer gnus-summary-buffer)
5282     (cond
5283      (n
5284       ;; A numerical prefix has been given.
5285       (setq n (prefix-numeric-value n))
5286       (let ((backward (< n 0))
5287             (n (abs (prefix-numeric-value n)))
5288             articles article)
5289         (save-excursion
5290           (while
5291               (and (> n 0)
5292                    (push (setq article (gnus-summary-article-number))
5293                          articles)
5294                    (if backward
5295                        (gnus-summary-find-prev nil article)
5296                      (gnus-summary-find-next nil article)))
5297             (decf n)))
5298         (nreverse articles)))
5299      ((and (gnus-region-active-p) (mark))
5300       (message "region active")
5301       ;; Work on the region between point and mark.
5302       (let ((max (max (point) (mark)))
5303             articles article)
5304         (save-excursion
5305           (goto-char (min (point) (mark)))
5306           (while
5307               (and
5308                (push (setq article (gnus-summary-article-number)) articles)
5309                (gnus-summary-find-next nil article)
5310                (< (point) max)))
5311           (nreverse articles))))
5312      (gnus-newsgroup-processable
5313       ;; There are process-marked articles present.
5314       ;; Save current state.
5315       (gnus-summary-save-process-mark)
5316       ;; Return the list.
5317       (reverse gnus-newsgroup-processable))
5318      (t
5319       ;; Just return the current article.
5320       (list (gnus-summary-article-number))))))
5321
5322 (defmacro gnus-summary-iterate (arg &rest forms)
5323   "Iterate over the process/prefixed articles and do FORMS.
5324 ARG is the interactive prefix given to the command.  FORMS will be
5325 executed with point over the summary line of the articles."
5326   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5327     `(let ((,articles (gnus-summary-work-articles ,arg)))
5328        (while ,articles
5329          (gnus-summary-goto-subject (car ,articles))
5330          ,@forms
5331          (pop ,articles)))))
5332
5333 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5334 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5335
5336 (defun gnus-summary-save-process-mark ()
5337   "Push the current set of process marked articles on the stack."
5338   (interactive)
5339   (push (copy-sequence gnus-newsgroup-processable)
5340         gnus-newsgroup-process-stack))
5341
5342 (defun gnus-summary-kill-process-mark ()
5343   "Push the current set of process marked articles on the stack and unmark."
5344   (interactive)
5345   (gnus-summary-save-process-mark)
5346   (gnus-summary-unmark-all-processable))
5347
5348 (defun gnus-summary-yank-process-mark ()
5349   "Pop the last process mark state off the stack and restore it."
5350   (interactive)
5351   (unless gnus-newsgroup-process-stack
5352     (error "Empty mark stack"))
5353   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5354
5355 (defun gnus-summary-process-mark-set (set)
5356   "Make SET into the current process marked articles."
5357   (gnus-summary-unmark-all-processable)
5358   (while set
5359     (gnus-summary-set-process-mark (pop set))))
5360
5361 ;;; Searching and stuff
5362
5363 (defun gnus-summary-search-group (&optional backward use-level)
5364   "Search for next unread newsgroup.
5365 If optional argument BACKWARD is non-nil, search backward instead."
5366   (save-excursion
5367     (set-buffer gnus-group-buffer)
5368     (when (gnus-group-search-forward
5369            backward nil (if use-level (gnus-group-group-level) nil))
5370       (gnus-group-group-name))))
5371
5372 (defun gnus-summary-best-group (&optional exclude-group)
5373   "Find the name of the best unread group.
5374 If EXCLUDE-GROUP, do not go to this group."
5375   (save-excursion
5376     (set-buffer gnus-group-buffer)
5377     (save-excursion
5378       (gnus-group-best-unread-group exclude-group))))
5379
5380 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5381   (if backward (gnus-summary-find-prev)
5382     (let* ((dummy (gnus-summary-article-intangible-p))
5383            (article (or article (gnus-summary-article-number)))
5384            (arts (gnus-data-find-list article))
5385            result)
5386       (when (and (not dummy)
5387                  (or (not gnus-summary-check-current)
5388                      (not unread)
5389                      (not (gnus-data-unread-p (car arts)))))
5390         (setq arts (cdr arts)))
5391       (when (setq result
5392                   (if unread
5393                       (progn
5394                         (while arts
5395                           (when (or (and undownloaded
5396                                          (eq gnus-undownloaded-mark
5397                                              (gnus-data-mark (car arts))))
5398                                     (gnus-data-unread-p (car arts)))
5399                             (setq result (car arts)
5400                                   arts nil))
5401                           (setq arts (cdr arts)))
5402                         result)
5403                     (car arts)))
5404         (goto-char (gnus-data-pos result))
5405         (gnus-data-number result)))))
5406
5407 (defun gnus-summary-find-prev (&optional unread article)
5408   (let* ((eobp (eobp))
5409          (article (or article (gnus-summary-article-number)))
5410          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5411          result)
5412     (when (and (not eobp)
5413                (or (not gnus-summary-check-current)
5414                    (not unread)
5415                    (not (gnus-data-unread-p (car arts)))))
5416       (setq arts (cdr arts)))
5417     (when (setq result
5418                 (if unread
5419                     (progn
5420                       (while arts
5421                         (when (gnus-data-unread-p (car arts))
5422                           (setq result (car arts)
5423                                 arts nil))
5424                         (setq arts (cdr arts)))
5425                       result)
5426                   (car arts)))
5427       (goto-char (gnus-data-pos result))
5428       (gnus-data-number result))))
5429
5430 (defun gnus-summary-find-subject (subject &optional unread backward article)
5431   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5432          (article (or article (gnus-summary-article-number)))
5433          (articles (gnus-data-list backward))
5434          (arts (gnus-data-find-list article articles))
5435          result)
5436     (when (or (not gnus-summary-check-current)
5437               (not unread)
5438               (not (gnus-data-unread-p (car arts))))
5439       (setq arts (cdr arts)))
5440     (while arts
5441       (and (or (not unread)
5442                (gnus-data-unread-p (car arts)))
5443            (vectorp (gnus-data-header (car arts)))
5444            (gnus-subject-equal
5445             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5446            (setq result (car arts)
5447                  arts nil))
5448       (setq arts (cdr arts)))
5449     (and result
5450          (goto-char (gnus-data-pos result))
5451          (gnus-data-number result))))
5452
5453 (defun gnus-summary-search-forward (&optional unread subject backward)
5454   "Search forward for an article.
5455 If UNREAD, look for unread articles.  If SUBJECT, look for
5456 articles with that subject.  If BACKWARD, search backward instead."
5457   (cond (subject (gnus-summary-find-subject subject unread backward))
5458         (backward (gnus-summary-find-prev unread))
5459         (t (gnus-summary-find-next unread))))
5460
5461 (defun gnus-recenter (&optional n)
5462   "Center point in window and redisplay frame.
5463 Also do horizontal recentering."
5464   (interactive "P")
5465   (when (and gnus-auto-center-summary
5466              (not (eq gnus-auto-center-summary 'vertical)))
5467     (gnus-horizontal-recenter))
5468   (recenter n))
5469
5470 (defun gnus-summary-recenter ()
5471   "Center point in the summary window.
5472 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5473 displayed, no centering will be performed."
5474   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5475   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5476   (interactive)
5477   (let* ((top (cond ((< (window-height) 4) 0)
5478                     ((< (window-height) 7) 1)
5479                     (t (if (numberp gnus-auto-center-summary)
5480                            gnus-auto-center-summary
5481                          2))))
5482          (height (1- (window-height)))
5483          (bottom (save-excursion (goto-char (point-max))
5484                                  (forward-line (- height))
5485                                  (point)))
5486          (window (get-buffer-window (current-buffer))))
5487     ;; The user has to want it.
5488     (when gnus-auto-center-summary
5489       (when (get-buffer-window gnus-article-buffer)
5490         ;; Only do recentering when the article buffer is displayed,
5491         ;; Set the window start to either `bottom', which is the biggest
5492         ;; possible valid number, or the second line from the top,
5493         ;; whichever is the least.
5494         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5495           (if (> bottom top-pos)
5496               ;; Keep the second line from the top visible
5497               (set-window-start window top-pos t)
5498             ;; Try to keep the bottom line visible; if it's partially
5499             ;; obscured, either scroll one more line to make it fully
5500             ;; visible, or revert to using TOP-POS.
5501             (save-excursion
5502               (goto-char (point-max))
5503               (forward-line -1)
5504               (let ((last-line-start (point)))
5505                 (goto-char bottom)
5506                 (set-window-start window (point) t)
5507                 (when (not (pos-visible-in-window-p last-line-start window))
5508                   (forward-line 1)
5509                   (set-window-start window (min (point) top-pos) t)))))))
5510       ;; Do horizontal recentering while we're at it.
5511       (when (and (get-buffer-window (current-buffer) t)
5512                  (not (eq gnus-auto-center-summary 'vertical)))
5513         (let ((selected (selected-window)))
5514           (select-window (get-buffer-window (current-buffer) t))
5515           (gnus-summary-position-point)
5516           (gnus-horizontal-recenter)
5517           (select-window selected))))))
5518
5519 (defun gnus-summary-jump-to-group (newsgroup)
5520   "Move point to NEWSGROUP in group mode buffer."
5521   ;; Keep update point of group mode buffer if visible.
5522   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5523       (save-window-excursion
5524         ;; Take care of tree window mode.
5525         (when (get-buffer-window gnus-group-buffer)
5526           (pop-to-buffer gnus-group-buffer))
5527         (gnus-group-jump-to-group newsgroup))
5528     (save-excursion
5529       ;; Take care of tree window mode.
5530       (if (get-buffer-window gnus-group-buffer)
5531           (pop-to-buffer gnus-group-buffer)
5532         (set-buffer gnus-group-buffer))
5533       (gnus-group-jump-to-group newsgroup))))
5534
5535 ;; This function returns a list of article numbers based on the
5536 ;; difference between the ranges of read articles in this group and
5537 ;; the range of active articles.
5538 (defun gnus-list-of-unread-articles (group)
5539   (let* ((read (gnus-info-read (gnus-get-info group)))
5540          (active (or (gnus-active group) (gnus-activate-group group)))
5541          (last (cdr active))
5542          first nlast unread)
5543     ;; If none are read, then all are unread.
5544     (if (not read)
5545         (setq first (car active))
5546       ;; If the range of read articles is a single range, then the
5547       ;; first unread article is the article after the last read
5548       ;; article.  Sounds logical, doesn't it?
5549       (if (and (not (listp (cdr read)))
5550                (or (< (car read) (car active))
5551                    (progn (setq read (list read))
5552                           nil)))
5553           (setq first (max (car active) (1+ (cdr read))))
5554         ;; `read' is a list of ranges.
5555         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5556                                   (caar read)))
5557                   1)
5558           (setq first (car active)))
5559         (while read
5560           (when first
5561             (while (< first nlast)
5562               (push first unread)
5563               (setq first (1+ first))))
5564           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5565           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5566           (setq read (cdr read)))))
5567     ;; And add the last unread articles.
5568     (while (<= first last)
5569       (push first unread)
5570       (setq first (1+ first)))
5571     ;; Return the list of unread articles.
5572     (delq 0 (nreverse unread))))
5573
5574 (defun gnus-list-of-read-articles (group)
5575   "Return a list of unread, unticked and non-dormant articles."
5576   (let* ((info (gnus-get-info group))
5577          (marked (gnus-info-marks info))
5578          (active (gnus-active group)))
5579     (and info active
5580          (gnus-set-difference
5581           (gnus-sorted-complement
5582            (gnus-uncompress-range active)
5583            (gnus-list-of-unread-articles group))
5584           (append
5585            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5586            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5587
5588 ;; Various summary commands
5589
5590 (defun gnus-summary-select-article-buffer ()
5591   "Reconfigure windows to show article buffer."
5592   (interactive)
5593   (if (not (gnus-buffer-live-p gnus-article-buffer))
5594       (error "There is no article buffer for this summary buffer")
5595     (gnus-configure-windows 'article)
5596     (select-window (get-buffer-window gnus-article-buffer))))
5597
5598 (defun gnus-summary-universal-argument (arg)
5599   "Perform any operation on all articles that are process/prefixed."
5600   (interactive "P")
5601   (let ((articles (gnus-summary-work-articles arg))
5602         func article)
5603     (if (eq
5604          (setq
5605           func
5606           (key-binding
5607            (read-key-sequence
5608             (substitute-command-keys
5609              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5610          'undefined)
5611         (gnus-error 1 "Undefined key")
5612       (save-excursion
5613         (while articles
5614           (gnus-summary-goto-subject (setq article (pop articles)))
5615           (let (gnus-newsgroup-processable)
5616             (command-execute func))
5617           (gnus-summary-remove-process-mark article)))))
5618   (gnus-summary-position-point))
5619
5620 (defun gnus-summary-toggle-truncation (&optional arg)
5621   "Toggle truncation of summary lines.
5622 With arg, turn line truncation on iff arg is positive."
5623   (interactive "P")
5624   (setq truncate-lines
5625         (if (null arg) (not truncate-lines)
5626           (> (prefix-numeric-value arg) 0)))
5627   (redraw-display))
5628
5629 (defun gnus-summary-reselect-current-group (&optional all rescan)
5630   "Exit and then reselect the current newsgroup.
5631 The prefix argument ALL means to select all articles."
5632   (interactive "P")
5633   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5634     (error "Ephemeral groups can't be reselected"))
5635   (let ((current-subject (gnus-summary-article-number))
5636         (group gnus-newsgroup-name))
5637     (setq gnus-newsgroup-begin nil)
5638     (gnus-summary-exit)
5639     ;; We have to adjust the point of group mode buffer because
5640     ;; point was moved to the next unread newsgroup by exiting.
5641     (gnus-summary-jump-to-group group)
5642     (when rescan
5643       (save-excursion
5644         (save-window-excursion
5645           ;; Don't show group contents.
5646           (set-window-start (selected-window) (point-max))
5647           (gnus-group-get-new-news-this-group 1))))
5648     (gnus-group-read-group all t)
5649     (gnus-summary-goto-subject current-subject nil t)))
5650
5651 (defun gnus-summary-rescan-group (&optional all)
5652   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5653   (interactive "P")
5654   (gnus-summary-reselect-current-group all t))
5655
5656 (defun gnus-summary-update-info (&optional non-destructive)
5657   (save-excursion
5658     (let ((group gnus-newsgroup-name))
5659       (when group
5660         (when gnus-newsgroup-kill-headers
5661           (setq gnus-newsgroup-killed
5662                 (gnus-compress-sequence
5663                  (nconc
5664                   (gnus-set-sorted-intersection
5665                    (gnus-uncompress-range gnus-newsgroup-killed)
5666                    (setq gnus-newsgroup-unselected
5667                          (sort gnus-newsgroup-unselected '<)))
5668                   (setq gnus-newsgroup-unreads
5669                         (sort gnus-newsgroup-unreads '<)))
5670                  t)))
5671         (unless (listp (cdr gnus-newsgroup-killed))
5672           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5673         (let ((headers gnus-newsgroup-headers))
5674           ;; Set the new ranges of read articles.
5675           (save-excursion
5676             (set-buffer gnus-group-buffer)
5677             (gnus-undo-force-boundary))
5678           (gnus-update-read-articles
5679            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5680           ;; Set the current article marks.
5681           (let ((gnus-newsgroup-scored
5682                  (if (and (not gnus-save-score)
5683                           (not non-destructive))
5684                      nil
5685                    gnus-newsgroup-scored)))
5686             (save-excursion
5687               (gnus-update-marks)))
5688           ;; Do the cross-ref thing.
5689           (when gnus-use-cross-reference
5690             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5691           ;; Do not switch windows but change the buffer to work.
5692           (set-buffer gnus-group-buffer)
5693           (unless (gnus-ephemeral-group-p group)
5694             (gnus-group-update-group group)))))))
5695
5696 (defun gnus-summary-save-newsrc (&optional force)
5697   "Save the current number of read/marked articles in the dribble buffer.
5698 The dribble buffer will then be saved.
5699 If FORCE (the prefix), also save the .newsrc file(s)."
5700   (interactive "P")
5701   (gnus-summary-update-info t)
5702   (if force
5703       (gnus-save-newsrc-file)
5704     (gnus-dribble-save)))
5705
5706 (defun gnus-summary-exit (&optional temporary)
5707   "Exit reading current newsgroup, and then return to group selection mode.
5708 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5709   (interactive)
5710   (gnus-set-global-variables)
5711   (gnus-kill-save-kill-buffer)
5712   (gnus-async-halt-prefetch)
5713   (let* ((group gnus-newsgroup-name)
5714          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5715          (mode major-mode)
5716          (group-point nil)
5717          (buf (current-buffer)))
5718     (unless quit-config
5719       ;; Do adaptive scoring, and possibly save score files.
5720       (when gnus-newsgroup-adaptive
5721         (gnus-score-adaptive))
5722       (when gnus-use-scoring
5723         (gnus-score-save)))
5724     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5725     ;; If we have several article buffers, we kill them at exit.
5726     (unless gnus-single-article-buffer
5727       (gnus-kill-buffer gnus-original-article-buffer)
5728       (setq gnus-article-current nil))
5729     (when gnus-use-cache
5730       (gnus-cache-possibly-remove-articles)
5731       (gnus-cache-save-buffers))
5732     (gnus-async-prefetch-remove-group group)
5733     (when gnus-suppress-duplicates
5734       (gnus-dup-enter-articles))
5735     (when gnus-use-trees
5736       (gnus-tree-close group))
5737     (when gnus-use-cache
5738       (gnus-cache-write-active))
5739     ;; Remove entries for this group.
5740     (nnmail-purge-split-history (gnus-group-real-name group))
5741     ;; Make all changes in this group permanent.
5742     (unless quit-config
5743       (gnus-run-hooks 'gnus-exit-group-hook)
5744       (gnus-summary-update-info))
5745     (gnus-close-group group)
5746     ;; Make sure where we were, and go to next newsgroup.
5747     (set-buffer gnus-group-buffer)
5748     (unless quit-config
5749       (gnus-group-jump-to-group group))
5750     (gnus-run-hooks 'gnus-summary-exit-hook)
5751     (unless (or quit-config
5752                 ;; If this group has disappeared from the summary
5753                 ;; buffer, don't skip forwards.
5754                 (not (string= group (gnus-group-group-name))))
5755       (gnus-group-next-unread-group 1))
5756     (setq group-point (point))
5757     (if temporary
5758         nil                             ;Nothing to do.
5759       ;; If we have several article buffers, we kill them at exit.
5760       (unless gnus-single-article-buffer
5761         (gnus-kill-buffer gnus-article-buffer)
5762         (gnus-kill-buffer gnus-original-article-buffer)
5763         (setq gnus-article-current nil))
5764       (set-buffer buf)
5765       (if (not gnus-kill-summary-on-exit)
5766           (gnus-deaden-summary)
5767         ;; We set all buffer-local variables to nil.  It is unclear why
5768         ;; this is needed, but if we don't, buffer-local variables are
5769         ;; not garbage-collected, it seems.  This would the lead to en
5770         ;; ever-growing Emacs.
5771         (gnus-summary-clear-local-variables)
5772         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5773           (gnus-summary-clear-local-variables))
5774         (when (get-buffer gnus-article-buffer)
5775           (bury-buffer gnus-article-buffer))
5776         ;; We clear the global counterparts of the buffer-local
5777         ;; variables as well, just to be on the safe side.
5778         (set-buffer gnus-group-buffer)
5779         (gnus-summary-clear-local-variables)
5780         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5781           (gnus-summary-clear-local-variables))
5782         ;; Return to group mode buffer.
5783         (when (eq mode 'gnus-summary-mode)
5784           (gnus-kill-buffer buf)))
5785       (setq gnus-current-select-method gnus-select-method)
5786       (pop-to-buffer gnus-group-buffer)
5787       (if (not quit-config)
5788           (progn
5789             (goto-char group-point)
5790             (gnus-configure-windows 'group 'force)
5791             (unless (pos-visible-in-window-p)
5792               (forward-line (/ (static-if (featurep 'xemacs)
5793                                    (window-displayed-height)
5794                                  (1- (window-height)))
5795                                -2))
5796               (set-window-start (selected-window) (point))
5797               (goto-char group-point)))
5798         (gnus-handle-ephemeral-exit quit-config))
5799       ;; Clear the current group name.
5800       (unless quit-config
5801         (setq gnus-newsgroup-name nil)))))
5802
5803 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5804 (defun gnus-summary-exit-no-update (&optional no-questions)
5805   "Quit reading current newsgroup without updating read article info."
5806   (interactive)
5807   (let* ((group gnus-newsgroup-name)
5808          (quit-config (gnus-group-quit-config group)))
5809     (when (or no-questions
5810               gnus-expert-user
5811               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5812       (gnus-async-halt-prefetch)
5813       (mapcar 'funcall
5814               (delq 'gnus-summary-expire-articles
5815                     (copy-sequence gnus-summary-prepare-exit-hook)))
5816       ;; If we have several article buffers, we kill them at exit.
5817       (unless gnus-single-article-buffer
5818         (gnus-kill-buffer gnus-article-buffer)
5819         (gnus-kill-buffer gnus-original-article-buffer)
5820         (setq gnus-article-current nil))
5821       (if (not gnus-kill-summary-on-exit)
5822           (gnus-deaden-summary)
5823         (gnus-close-group group)
5824         (gnus-summary-clear-local-variables)
5825         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5826           (gnus-summary-clear-local-variables))
5827         (set-buffer gnus-group-buffer)
5828         (gnus-summary-clear-local-variables)
5829         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5830           (gnus-summary-clear-local-variables))
5831         (when (get-buffer gnus-summary-buffer)
5832           (kill-buffer gnus-summary-buffer)))
5833       (unless gnus-single-article-buffer
5834         (setq gnus-article-current nil))
5835       (when gnus-use-trees
5836         (gnus-tree-close group))
5837       (gnus-async-prefetch-remove-group group)
5838       (when (get-buffer gnus-article-buffer)
5839         (bury-buffer gnus-article-buffer))
5840       ;; Return to the group buffer.
5841       (gnus-configure-windows 'group 'force)
5842       ;; Clear the current group name.
5843       (setq gnus-newsgroup-name nil)
5844       (when (equal (gnus-group-group-name) group)
5845         (gnus-group-next-unread-group 1))
5846       (when quit-config
5847         (gnus-handle-ephemeral-exit quit-config)))))
5848
5849 (defun gnus-handle-ephemeral-exit (quit-config)
5850   "Handle movement when leaving an ephemeral group.
5851 The state which existed when entering the ephemeral is reset."
5852   (if (not (buffer-name (car quit-config)))
5853       (gnus-configure-windows 'group 'force)
5854     (set-buffer (car quit-config))
5855     (cond ((eq major-mode 'gnus-summary-mode)
5856            (gnus-set-global-variables))
5857           ((eq major-mode 'gnus-article-mode)
5858            (save-excursion
5859              ;; The `gnus-summary-buffer' variable may point
5860              ;; to the old summary buffer when using a single
5861              ;; article buffer.
5862              (unless (gnus-buffer-live-p gnus-summary-buffer)
5863                (set-buffer gnus-group-buffer))
5864              (set-buffer gnus-summary-buffer)
5865              (gnus-set-global-variables))))
5866     (if (or (eq (cdr quit-config) 'article)
5867             (eq (cdr quit-config) 'pick))
5868         (progn
5869           ;; The current article may be from the ephemeral group
5870           ;; thus it is best that we reload this article
5871           (gnus-summary-show-article)
5872           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5873               (gnus-configure-windows 'pick 'force)
5874             (gnus-configure-windows (cdr quit-config) 'force)))
5875       (gnus-configure-windows (cdr quit-config) 'force))
5876     (when (eq major-mode 'gnus-summary-mode)
5877       (gnus-summary-next-subject 1 nil t)
5878       (gnus-summary-recenter)
5879       (gnus-summary-position-point))))
5880
5881 (defun gnus-summary-preview-mime-message ()
5882   "MIME decode and play this message."
5883   (interactive)
5884   (let ((gnus-break-pages nil)
5885         (gnus-show-mime t))
5886     (gnus-summary-select-article gnus-show-all-headers t))
5887   (select-window (get-buffer-window gnus-article-buffer)))
5888
5889 ;;; Dead summaries.
5890
5891 (defvar gnus-dead-summary-mode-map nil)
5892
5893 (unless gnus-dead-summary-mode-map
5894   (setq gnus-dead-summary-mode-map (make-keymap))
5895   (suppress-keymap gnus-dead-summary-mode-map)
5896   (substitute-key-definition
5897    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5898   (let ((keys '("\C-d" "\r" "\177" [delete])))
5899     (while keys
5900       (define-key gnus-dead-summary-mode-map
5901         (pop keys) 'gnus-summary-wake-up-the-dead))))
5902
5903 (defvar gnus-dead-summary-mode nil
5904   "Minor mode for Gnus summary buffers.")
5905
5906 (defun gnus-dead-summary-mode (&optional arg)
5907   "Minor mode for Gnus summary buffers."
5908   (interactive "P")
5909   (when (eq major-mode 'gnus-summary-mode)
5910     (make-local-variable 'gnus-dead-summary-mode)
5911     (setq gnus-dead-summary-mode
5912           (if (null arg) (not gnus-dead-summary-mode)
5913             (> (prefix-numeric-value arg) 0)))
5914     (when gnus-dead-summary-mode
5915       (gnus-add-minor-mode
5916        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5917
5918 (defun gnus-deaden-summary ()
5919   "Make the current summary buffer into a dead summary buffer."
5920   ;; Kill any previous dead summary buffer.
5921   (when (and gnus-dead-summary
5922              (buffer-name gnus-dead-summary))
5923     (save-excursion
5924       (set-buffer gnus-dead-summary)
5925       (when gnus-dead-summary-mode
5926         (kill-buffer (current-buffer)))))
5927   ;; Make this the current dead summary.
5928   (setq gnus-dead-summary (current-buffer))
5929   (gnus-dead-summary-mode 1)
5930   (let ((name (buffer-name)))
5931     (when (string-match "Summary" name)
5932       (rename-buffer
5933        (concat (substring name 0 (match-beginning 0)) "Dead "
5934                (substring name (match-beginning 0)))
5935        t)
5936       (bury-buffer))))
5937
5938 (defun gnus-kill-or-deaden-summary (buffer)
5939   "Kill or deaden the summary BUFFER."
5940   (save-excursion
5941     (when (and (buffer-name buffer)
5942                (not gnus-single-article-buffer))
5943       (save-excursion
5944         (set-buffer buffer)
5945         (gnus-kill-buffer gnus-article-buffer)
5946         (gnus-kill-buffer gnus-original-article-buffer)))
5947     (cond (gnus-kill-summary-on-exit
5948            (when (and gnus-use-trees
5949                       (gnus-buffer-exists-p buffer))
5950              (save-excursion
5951                (set-buffer buffer)
5952                (gnus-tree-close gnus-newsgroup-name)))
5953            (gnus-kill-buffer buffer))
5954           ((gnus-buffer-exists-p buffer)
5955            (save-excursion
5956              (set-buffer buffer)
5957              (gnus-deaden-summary))))))
5958
5959 (defun gnus-summary-wake-up-the-dead (&rest args)
5960   "Wake up the dead summary buffer."
5961   (interactive)
5962   (gnus-dead-summary-mode -1)
5963   (let ((name (buffer-name)))
5964     (when (string-match "Dead " name)
5965       (rename-buffer
5966        (concat (substring name 0 (match-beginning 0))
5967                (substring name (match-end 0)))
5968        t)))
5969   (gnus-message 3 "This dead summary is now alive again"))
5970
5971 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5972 (defun gnus-summary-fetch-faq (&optional faq-dir)
5973   "Fetch the FAQ for the current group.
5974 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5975 in."
5976   (interactive
5977    (list
5978     (when current-prefix-arg
5979       (completing-read
5980        "Faq dir: " (and (listp gnus-group-faq-directory)
5981                         (mapcar (lambda (file) (list file))
5982                                 gnus-group-faq-directory))))))
5983   (let (gnus-faq-buffer)
5984     (when (setq gnus-faq-buffer
5985                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5986       (gnus-configure-windows 'summary-faq))))
5987
5988 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5989 (defun gnus-summary-describe-group (&optional force)
5990   "Describe the current newsgroup."
5991   (interactive "P")
5992   (gnus-group-describe-group force gnus-newsgroup-name))
5993
5994 (defun gnus-summary-describe-briefly ()
5995   "Describe summary mode commands briefly."
5996   (interactive)
5997   (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")))
5998
5999 ;; Walking around group mode buffer from summary mode.
6000
6001 (defun gnus-summary-next-group (&optional no-article target-group backward)
6002   "Exit current newsgroup and then select next unread newsgroup.
6003 If prefix argument NO-ARTICLE is non-nil, no article is selected
6004 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6005 previous group instead."
6006   (interactive "P")
6007   ;; Stop pre-fetching.
6008   (gnus-async-halt-prefetch)
6009   (let ((current-group gnus-newsgroup-name)
6010         (current-buffer (current-buffer))
6011         entered)
6012     ;; First we semi-exit this group to update Xrefs and all variables.
6013     ;; We can't do a real exit, because the window conf must remain
6014     ;; the same in case the user is prompted for info, and we don't
6015     ;; want the window conf to change before that...
6016     (gnus-summary-exit t)
6017     (while (not entered)
6018       ;; Then we find what group we are supposed to enter.
6019       (set-buffer gnus-group-buffer)
6020       (gnus-group-jump-to-group current-group)
6021       (setq target-group
6022             (or target-group
6023                 (if (eq gnus-keep-same-level 'best)
6024                     (gnus-summary-best-group gnus-newsgroup-name)
6025                   (gnus-summary-search-group backward gnus-keep-same-level))))
6026       (if (not target-group)
6027           ;; There are no further groups, so we return to the group
6028           ;; buffer.
6029           (progn
6030             (gnus-message 5 "Returning to the group buffer")
6031             (setq entered t)
6032             (when (gnus-buffer-live-p current-buffer)
6033               (set-buffer current-buffer)
6034               (gnus-summary-exit))
6035             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6036         ;; We try to enter the target group.
6037         (gnus-group-jump-to-group target-group)
6038         (let ((unreads (gnus-group-group-unread)))
6039           (if (and (or (eq t unreads)
6040                        (and unreads (not (zerop unreads))))
6041                    (gnus-summary-read-group
6042                     target-group nil no-article
6043                     (and (buffer-name current-buffer) current-buffer)
6044                     nil backward))
6045               (setq entered t)
6046             (setq current-group target-group
6047                   target-group nil)))))))
6048
6049 (defun gnus-summary-prev-group (&optional no-article)
6050   "Exit current newsgroup and then select previous unread newsgroup.
6051 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6052   (interactive "P")
6053   (gnus-summary-next-group no-article nil t))
6054
6055 ;; Walking around summary lines.
6056
6057 (defun gnus-summary-first-subject (&optional unread undownloaded)
6058   "Go to the first unread subject.
6059 If UNREAD is non-nil, go to the first unread article.
6060 Returns the article selected or nil if there are no unread articles."
6061   (interactive "P")
6062   (prog1
6063       (cond
6064        ;; Empty summary.
6065        ((null gnus-newsgroup-data)
6066         (gnus-message 3 "No articles in the group")
6067         nil)
6068        ;; Pick the first article.
6069        ((not unread)
6070         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6071         (gnus-data-number (car gnus-newsgroup-data)))
6072        ;; No unread articles.
6073        ((null gnus-newsgroup-unreads)
6074         (gnus-message 3 "No more unread articles")
6075         nil)
6076        ;; Find the first unread article.
6077        (t
6078         (let ((data gnus-newsgroup-data))
6079           (while (and data
6080                       (and (not (and undownloaded
6081                                      (eq gnus-undownloaded-mark
6082                                          (gnus-data-mark (car data)))))
6083                            (not (gnus-data-unread-p (car data)))))
6084             (setq data (cdr data)))
6085           (when data
6086             (goto-char (gnus-data-pos (car data)))
6087             (gnus-data-number (car data))))))
6088     (gnus-summary-position-point)))
6089
6090 (defun gnus-summary-next-subject (n &optional unread dont-display)
6091   "Go to next N'th summary line.
6092 If N is negative, go to the previous N'th subject line.
6093 If UNREAD is non-nil, only unread articles are selected.
6094 The difference between N and the actual number of steps taken is
6095 returned."
6096   (interactive "p")
6097   (let ((backward (< n 0))
6098         (n (abs n)))
6099     (while (and (> n 0)
6100                 (if backward
6101                     (gnus-summary-find-prev unread)
6102                   (gnus-summary-find-next unread)))
6103       (unless (zerop (setq n (1- n)))
6104         (gnus-summary-show-thread)))
6105     (when (/= 0 n)
6106       (gnus-message 7 "No more%s articles"
6107                     (if unread " unread" "")))
6108     (unless dont-display
6109       (gnus-summary-recenter)
6110       (gnus-summary-position-point))
6111     n))
6112
6113 (defun gnus-summary-next-unread-subject (n)
6114   "Go to next N'th unread summary line."
6115   (interactive "p")
6116   (gnus-summary-next-subject n t))
6117
6118 (defun gnus-summary-prev-subject (n &optional unread)
6119   "Go to previous N'th summary line.
6120 If optional argument UNREAD is non-nil, only unread article is selected."
6121   (interactive "p")
6122   (gnus-summary-next-subject (- n) unread))
6123
6124 (defun gnus-summary-prev-unread-subject (n)
6125   "Go to previous N'th unread summary line."
6126   (interactive "p")
6127   (gnus-summary-next-subject (- n) t))
6128
6129 (defun gnus-summary-goto-subject (article &optional force silent)
6130   "Go the subject line of ARTICLE.
6131 If FORCE, also allow jumping to articles not currently shown."
6132   (interactive "nArticle number: ")
6133   (let ((b (point))
6134         (data (gnus-data-find article)))
6135     ;; We read in the article if we have to.
6136     (and (not data)
6137          force
6138          (gnus-summary-insert-subject
6139           article
6140           (if (or (numberp force) (vectorp force)) force)
6141           t)
6142          (setq data (gnus-data-find article)))
6143     (goto-char b)
6144     (if (not data)
6145         (progn
6146           (unless silent
6147             (gnus-message 3 "Can't find article %d" article))
6148           nil)
6149       (goto-char (gnus-data-pos data))
6150       (gnus-summary-position-point)
6151       article)))
6152
6153 ;; Walking around summary lines with displaying articles.
6154
6155 (defun gnus-summary-expand-window (&optional arg)
6156   "Make the summary buffer take up the entire Emacs frame.
6157 Given a prefix, will force an `article' buffer configuration."
6158   (interactive "P")
6159   (if arg
6160       (gnus-configure-windows 'article 'force)
6161     (gnus-configure-windows 'summary 'force)))
6162
6163 (defun gnus-summary-display-article (article &optional all-header)
6164   "Display ARTICLE in article buffer."
6165   (when (gnus-buffer-live-p gnus-article-buffer)
6166     (with-current-buffer gnus-article-buffer
6167       (set-buffer-multibyte t)))
6168   (gnus-set-global-variables)
6169   (when (gnus-buffer-live-p gnus-article-buffer)
6170     (with-current-buffer gnus-article-buffer
6171       (setq gnus-article-charset gnus-newsgroup-charset)
6172       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6173   (if (null article)
6174       nil
6175     (prog1
6176         (if gnus-summary-display-article-function
6177             (funcall gnus-summary-display-article-function article all-header)
6178           (gnus-article-prepare article all-header))
6179       (with-current-buffer gnus-article-buffer
6180         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6181              nil))
6182       (gnus-run-hooks 'gnus-select-article-hook)
6183       (when (and gnus-current-article
6184                  (not (zerop gnus-current-article)))
6185         (gnus-summary-goto-subject gnus-current-article))
6186       (gnus-summary-recenter)
6187       (when (and gnus-use-trees gnus-show-threads)
6188         (gnus-possibly-generate-tree article)
6189         (gnus-highlight-selected-tree article))
6190       ;; Successfully display article.
6191       (gnus-article-set-window-start
6192        (cdr (assq article gnus-newsgroup-bookmarks))))))
6193
6194 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6195   "Select the current article.
6196 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6197 non-nil, the article will be re-fetched even if it already present in
6198 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6199 be displayed."
6200   ;; Make sure we are in the summary buffer to work around bbdb bug.
6201   (unless (eq major-mode 'gnus-summary-mode)
6202     (set-buffer gnus-summary-buffer))
6203   (let ((article (or article (gnus-summary-article-number)))
6204         (all-headers (not (not all-headers))) ;Must be T or NIL.
6205         gnus-summary-display-article-function)
6206     (and (not pseudo)
6207          (gnus-summary-article-pseudo-p article)
6208          (error "This is a pseudo-article"))
6209     (save-excursion
6210       (set-buffer gnus-summary-buffer)
6211       (if (or (and gnus-single-article-buffer
6212                    (or (null gnus-current-article)
6213                        (null gnus-article-current)
6214                        (null (get-buffer gnus-article-buffer))
6215                        (not (eq article (cdr gnus-article-current)))
6216                        (not (equal (car gnus-article-current)
6217                                    gnus-newsgroup-name))))
6218               (and (not gnus-single-article-buffer)
6219                    (or (null gnus-current-article)
6220                        (not (eq gnus-current-article article))))
6221               force)
6222           ;; The requested article is different from the current article.
6223           (progn
6224             (gnus-summary-display-article article all-headers)
6225             (when (or all-headers gnus-show-all-headers)
6226               (gnus-article-show-all-headers))
6227             (gnus-article-set-window-start
6228              (cdr (assq article gnus-newsgroup-bookmarks)))
6229             article)
6230         (when (or all-headers gnus-show-all-headers)
6231           (gnus-article-show-all-headers))
6232         'old))))
6233
6234 (defun gnus-summary-force-verify-and-decrypt ()
6235   (interactive)
6236   (let ((mm-verify-option 'known)
6237         (mm-decrypt-option 'known))
6238     (gnus-summary-select-article nil 'force)))
6239
6240 (defun gnus-summary-set-current-mark (&optional current-mark)
6241   "Obsolete function."
6242   nil)
6243
6244 (defun gnus-summary-next-article (&optional unread subject backward push)
6245   "Select the next article.
6246 If UNREAD, only unread articles are selected.
6247 If SUBJECT, only articles with SUBJECT are selected.
6248 If BACKWARD, the previous article is selected instead of the next."
6249   (interactive "P")
6250   (cond
6251    ;; Is there such an article?
6252    ((and (gnus-summary-search-forward unread subject backward)
6253          (or (gnus-summary-display-article (gnus-summary-article-number))
6254              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6255     (gnus-summary-position-point))
6256    ;; If not, we try the first unread, if that is wanted.
6257    ((and subject
6258          gnus-auto-select-same
6259          (gnus-summary-first-unread-article))
6260     (gnus-summary-position-point)
6261     (gnus-message 6 "Wrapped"))
6262    ;; Try to get next/previous article not displayed in this group.
6263    ((and gnus-auto-extend-newsgroup
6264          (not unread) (not subject))
6265     (gnus-summary-goto-article
6266      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6267      nil (count-lines (point-min) (point))))
6268    ;; Go to next/previous group.
6269    (t
6270     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6271       (gnus-summary-jump-to-group gnus-newsgroup-name))
6272     (let ((cmd last-command-char)
6273           (point
6274            (save-excursion
6275              (set-buffer gnus-group-buffer)
6276              (point)))
6277           (group
6278            (if (eq gnus-keep-same-level 'best)
6279                (gnus-summary-best-group gnus-newsgroup-name)
6280              (gnus-summary-search-group backward gnus-keep-same-level))))
6281       ;; For some reason, the group window gets selected.  We change
6282       ;; it back.
6283       (select-window (get-buffer-window (current-buffer)))
6284       ;; Select next unread newsgroup automagically.
6285       (cond
6286        ((or (not gnus-auto-select-next)
6287             (not cmd))
6288         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6289        ((or (eq gnus-auto-select-next 'quietly)
6290             (and (eq gnus-auto-select-next 'slightly-quietly)
6291                  push)
6292             (and (eq gnus-auto-select-next 'almost-quietly)
6293                  (gnus-summary-last-article-p)))
6294         ;; Select quietly.
6295         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6296             (gnus-summary-exit)
6297           (gnus-message 7 "No more%s articles (%s)..."
6298                         (if unread " unread" "")
6299                         (if group (concat "selecting " group)
6300                           "exiting"))
6301           (gnus-summary-next-group nil group backward)))
6302        (t
6303         (when (gnus-key-press-event-p last-input-event)
6304           (gnus-summary-walk-group-buffer
6305            gnus-newsgroup-name cmd unread backward point))))))))
6306
6307 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6308   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6309                       (?\C-p (gnus-group-prev-unread-group 1))))
6310         (cursor-in-echo-area t)
6311         keve key group ended)
6312     (save-excursion
6313       (set-buffer gnus-group-buffer)
6314       (goto-char start)
6315       (setq group
6316             (if (eq gnus-keep-same-level 'best)
6317                 (gnus-summary-best-group gnus-newsgroup-name)
6318               (gnus-summary-search-group backward gnus-keep-same-level))))
6319     (while (not ended)
6320       (gnus-message
6321        5 "No more%s articles%s" (if unread " unread" "")
6322        (if (and group
6323                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6324            (format " (Type %s for %s [%s])"
6325                    (single-key-description cmd) group
6326                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6327          (format " (Type %s to exit %s)"
6328                  (single-key-description cmd)
6329                  gnus-newsgroup-name)))
6330       ;; Confirm auto selection.
6331       (setq key (car (setq keve (gnus-read-event-char))))
6332       (setq ended t)
6333       (cond
6334        ((assq key keystrokes)
6335         (let ((obuf (current-buffer)))
6336           (switch-to-buffer gnus-group-buffer)
6337           (when group
6338             (gnus-group-jump-to-group group))
6339           (eval (cadr (assq key keystrokes)))
6340           (setq group (gnus-group-group-name))
6341           (switch-to-buffer obuf))
6342         (setq ended nil))
6343        ((equal key cmd)
6344         (if (or (not group)
6345                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6346             (gnus-summary-exit)
6347           (gnus-summary-next-group nil group backward)))
6348        (t
6349         (push (cdr keve) unread-command-events))))))
6350
6351 (defun gnus-summary-next-unread-article ()
6352   "Select unread article after current one."
6353   (interactive)
6354   (gnus-summary-next-article
6355    (or (not (eq gnus-summary-goto-unread 'never))
6356        (gnus-summary-last-article-p (gnus-summary-article-number)))
6357    (and gnus-auto-select-same
6358         (gnus-summary-article-subject))))
6359
6360 (defun gnus-summary-prev-article (&optional unread subject)
6361   "Select the article after the current one.
6362 If UNREAD is non-nil, only unread articles are selected."
6363   (interactive "P")
6364   (gnus-summary-next-article unread subject t))
6365
6366 (defun gnus-summary-prev-unread-article ()
6367   "Select unread article before current one."
6368   (interactive)
6369   (gnus-summary-prev-article
6370    (or (not (eq gnus-summary-goto-unread 'never))
6371        (gnus-summary-first-article-p (gnus-summary-article-number)))
6372    (and gnus-auto-select-same
6373         (gnus-summary-article-subject))))
6374
6375 (defun gnus-summary-next-page (&optional lines circular)
6376   "Show next page of the selected article.
6377 If at the end of the current article, select the next article.
6378 LINES says how many lines should be scrolled up.
6379
6380 If CIRCULAR is non-nil, go to the start of the article instead of
6381 selecting the next article when reaching the end of the current
6382 article."
6383   (interactive "P")
6384   (setq gnus-summary-buffer (current-buffer))
6385   (gnus-set-global-variables)
6386   (let ((article (gnus-summary-article-number))
6387         (article-window (get-buffer-window gnus-article-buffer t))
6388         endp)
6389     ;; If the buffer is empty, we have no article.
6390     (unless article
6391       (error "No article to select"))
6392     (gnus-configure-windows 'article)
6393     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6394         (if (and (eq gnus-summary-goto-unread 'never)
6395                  (not (gnus-summary-last-article-p article)))
6396             (gnus-summary-next-article)
6397           (gnus-summary-next-unread-article))
6398       (if (or (null gnus-current-article)
6399               (null gnus-article-current)
6400               (/= article (cdr gnus-article-current))
6401               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6402           ;; Selected subject is different from current article's.
6403           (gnus-summary-display-article article)
6404         (when article-window
6405           (gnus-eval-in-buffer-window gnus-article-buffer
6406             (setq endp (gnus-article-next-page lines)))
6407           (when endp
6408             (cond (circular
6409                    (gnus-summary-beginning-of-article))
6410                   (lines
6411                    (gnus-message 3 "End of message"))
6412                   ((null lines)
6413                    (if (and (eq gnus-summary-goto-unread 'never)
6414                             (not (gnus-summary-last-article-p article)))
6415                        (gnus-summary-next-article)
6416                      (gnus-summary-next-unread-article))))))))
6417     (gnus-summary-recenter)
6418     (gnus-summary-position-point)))
6419
6420 (defun gnus-summary-prev-page (&optional lines move)
6421   "Show previous page of selected article.
6422 Argument LINES specifies lines to be scrolled down.
6423 If MOVE, move to the previous unread article if point is at
6424 the beginning of the buffer."
6425   (interactive "P")
6426   (let ((article (gnus-summary-article-number))
6427         (article-window (get-buffer-window gnus-article-buffer t))
6428         endp)
6429     (gnus-configure-windows 'article)
6430     (if (or (null gnus-current-article)
6431             (null gnus-article-current)
6432             (/= article (cdr gnus-article-current))
6433             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6434         ;; Selected subject is different from current article's.
6435         (gnus-summary-display-article article)
6436       (gnus-summary-recenter)
6437       (when article-window
6438         (gnus-eval-in-buffer-window gnus-article-buffer
6439           (setq endp (gnus-article-prev-page lines)))
6440         (when (and move endp)
6441           (cond (lines
6442                  (gnus-message 3 "Beginning of message"))
6443                 ((null lines)
6444                  (if (and (eq gnus-summary-goto-unread 'never)
6445                           (not (gnus-summary-first-article-p article)))
6446                      (gnus-summary-prev-article)
6447                    (gnus-summary-prev-unread-article))))))))
6448   (gnus-summary-position-point))
6449
6450 (defun gnus-summary-prev-page-or-article (&optional lines)
6451   "Show previous page of selected article.
6452 Argument LINES specifies lines to be scrolled down.
6453 If at the beginning of the article, go to the next article."
6454   (interactive "P")
6455   (gnus-summary-prev-page lines t))
6456
6457 (defun gnus-summary-scroll-up (lines)
6458   "Scroll up (or down) one line current article.
6459 Argument LINES specifies lines to be scrolled up (or down if negative)."
6460   (interactive "p")
6461   (gnus-configure-windows 'article)
6462   (gnus-summary-show-thread)
6463   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6464     (gnus-eval-in-buffer-window gnus-article-buffer
6465       (cond ((> lines 0)
6466              (when (gnus-article-next-page lines)
6467                (gnus-message 3 "End of message")))
6468             ((< lines 0)
6469              (gnus-article-prev-page (- lines))))))
6470   (gnus-summary-recenter)
6471   (gnus-summary-position-point))
6472
6473 (defun gnus-summary-scroll-down (lines)
6474   "Scroll down (or up) one line current article.
6475 Argument LINES specifies lines to be scrolled down (or up if negative)."
6476   (interactive "p")
6477   (gnus-summary-scroll-up (- lines)))
6478
6479 (defun gnus-summary-next-same-subject ()
6480   "Select next article which has the same subject as current one."
6481   (interactive)
6482   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6483
6484 (defun gnus-summary-prev-same-subject ()
6485   "Select previous article which has the same subject as current one."
6486   (interactive)
6487   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6488
6489 (defun gnus-summary-next-unread-same-subject ()
6490   "Select next unread article which has the same subject as current one."
6491   (interactive)
6492   (gnus-summary-next-article t (gnus-summary-article-subject)))
6493
6494 (defun gnus-summary-prev-unread-same-subject ()
6495   "Select previous unread article which has the same subject as current one."
6496   (interactive)
6497   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6498
6499 (defun gnus-summary-first-unread-article ()
6500   "Select the first unread article.
6501 Return nil if there are no unread articles."
6502   (interactive)
6503   (prog1
6504       (when (gnus-summary-first-subject t)
6505         (gnus-summary-show-thread)
6506         (gnus-summary-first-subject t)
6507         (gnus-summary-display-article (gnus-summary-article-number)))
6508     (gnus-summary-position-point)))
6509
6510 (defun gnus-summary-first-unread-subject ()
6511   "Place the point on the subject line of the first unread article.
6512 Return nil if there are no unread articles."
6513   (interactive)
6514   (prog1
6515       (when (gnus-summary-first-subject t)
6516         (gnus-summary-show-thread)
6517         (gnus-summary-first-subject t))
6518     (gnus-summary-position-point)))
6519
6520 (defun gnus-summary-first-article ()
6521   "Select the first article.
6522 Return nil if there are no articles."
6523   (interactive)
6524   (prog1
6525       (when (gnus-summary-first-subject)
6526         (gnus-summary-show-thread)
6527         (gnus-summary-first-subject)
6528         (gnus-summary-display-article (gnus-summary-article-number)))
6529     (gnus-summary-position-point)))
6530
6531 (defun gnus-summary-best-unread-article ()
6532   "Select the unread article with the highest score."
6533   (interactive)
6534   (let ((best -1000000)
6535         (data gnus-newsgroup-data)
6536         article score)
6537     (while data
6538       (and (gnus-data-unread-p (car data))
6539            (> (setq score
6540                     (gnus-summary-article-score (gnus-data-number (car data))))
6541               best)
6542            (setq best score
6543                  article (gnus-data-number (car data))))
6544       (setq data (cdr data)))
6545     (prog1
6546         (if article
6547             (gnus-summary-goto-article article)
6548           (error "No unread articles"))
6549       (gnus-summary-position-point))))
6550
6551 (defun gnus-summary-last-subject ()
6552   "Go to the last displayed subject line in the group."
6553   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6554     (when article
6555       (gnus-summary-goto-subject article))))
6556
6557 (defun gnus-summary-goto-article (article &optional all-headers force)
6558   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6559 If ALL-HEADERS is non-nil, no header lines are hidden.
6560 If FORCE, go to the article even if it isn't displayed.  If FORCE
6561 is a number, it is the line the article is to be displayed on."
6562   (interactive
6563    (list
6564     (completing-read
6565      "Article number or Message-ID: "
6566      (mapcar (lambda (number) (list (int-to-string number)))
6567              gnus-newsgroup-limit))
6568     current-prefix-arg
6569     t))
6570   (prog1
6571       (if (and (stringp article)
6572                (string-match "@" article))
6573           (gnus-summary-refer-article article)
6574         (when (stringp article)
6575           (setq article (string-to-number article)))
6576         (if (gnus-summary-goto-subject article force)
6577             (gnus-summary-display-article article all-headers)
6578           (gnus-message 4 "Couldn't go to article %s" article) nil))
6579     (gnus-summary-position-point)))
6580
6581 (defun gnus-summary-goto-last-article ()
6582   "Go to the previously read article."
6583   (interactive)
6584   (prog1
6585       (when gnus-last-article
6586         (gnus-summary-goto-article gnus-last-article nil t))
6587     (gnus-summary-position-point)))
6588
6589 (defun gnus-summary-pop-article (number)
6590   "Pop one article off the history and go to the previous.
6591 NUMBER articles will be popped off."
6592   (interactive "p")
6593   (let (to)
6594     (setq gnus-newsgroup-history
6595           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6596     (if to
6597         (gnus-summary-goto-article (car to) nil t)
6598       (error "Article history empty")))
6599   (gnus-summary-position-point))
6600
6601 ;; Summary commands and functions for limiting the summary buffer.
6602
6603 (defun gnus-summary-limit-to-articles (n)
6604   "Limit the summary buffer to the next N articles.
6605 If not given a prefix, use the process marked articles instead."
6606   (interactive "P")
6607   (prog1
6608       (let ((articles (gnus-summary-work-articles n)))
6609         (setq gnus-newsgroup-processable nil)
6610         (gnus-summary-limit articles))
6611     (gnus-summary-position-point)))
6612
6613 (defun gnus-summary-pop-limit (&optional total)
6614   "Restore the previous limit.
6615 If given a prefix, remove all limits."
6616   (interactive "P")
6617   (when total
6618     (setq gnus-newsgroup-limits
6619           (list (mapcar (lambda (h) (mail-header-number h))
6620                         gnus-newsgroup-headers))))
6621   (unless gnus-newsgroup-limits
6622     (error "No limit to pop"))
6623   (prog1
6624       (gnus-summary-limit nil 'pop)
6625     (gnus-summary-position-point)))
6626
6627 (defun gnus-summary-limit-to-subject (subject &optional header)
6628   "Limit the summary buffer to articles that have subjects that match a regexp."
6629   (interactive "sLimit to subject (regexp): ")
6630   (unless header
6631     (setq header "subject"))
6632   (when (not (equal "" subject))
6633     (prog1
6634         (let ((articles (gnus-summary-find-matching
6635                          (or header "subject") subject 'all)))
6636           (unless articles
6637             (error "Found no matches for \"%s\"" subject))
6638           (gnus-summary-limit articles))
6639       (gnus-summary-position-point))))
6640
6641 (defun gnus-summary-limit-to-author (from)
6642   "Limit the summary buffer to articles that have authors that match a regexp."
6643   (interactive "sLimit to author (regexp): ")
6644   (gnus-summary-limit-to-subject from "from"))
6645
6646 (defun gnus-summary-limit-to-age (age &optional younger-p)
6647   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6648 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6649 articles that are younger than AGE days."
6650   (interactive
6651    (let ((younger current-prefix-arg)
6652          (days-got nil)
6653          days)
6654      (while (not days-got)
6655        (setq days (if younger
6656                       (read-string "Limit to articles within (in days): ")
6657                     (read-string "Limit to articles older than (in days): ")))
6658        (when (> (length days) 0)
6659          (setq days (read days)))
6660        (if (numberp days)
6661            (setq days-got t)
6662          (message "Please enter a number.")
6663          (sleep-for 1)))
6664      (list days younger)))
6665   (prog1
6666       (let ((data gnus-newsgroup-data)
6667             (cutoff (days-to-time age))
6668             articles d date is-younger)
6669         (while (setq d (pop data))
6670           (when (and (vectorp (gnus-data-header d))
6671                      (setq date (mail-header-date (gnus-data-header d))))
6672             (setq is-younger (time-less-p
6673                               (time-since (condition-case ()
6674                                               (date-to-time date)
6675                                             (error '(0 0))))
6676                               cutoff))
6677             (when (if younger-p
6678                       is-younger
6679                     (not is-younger))
6680               (push (gnus-data-number d) articles))))
6681         (gnus-summary-limit (nreverse articles)))
6682     (gnus-summary-position-point)))
6683
6684 (defun gnus-summary-limit-to-extra (header regexp)
6685   "Limit the summary buffer to articles that match an 'extra' header."
6686   (interactive
6687    (let ((header
6688           (intern
6689            (gnus-completing-read
6690             (symbol-name (car gnus-extra-headers))
6691             "Limit extra header:"
6692             (mapcar (lambda (x)
6693                       (cons (symbol-name x) x))
6694                     gnus-extra-headers)
6695             nil
6696             t))))
6697      (list header
6698            (read-string (format "Limit to header %s (regexp): " header)))))
6699   (when (not (equal "" regexp))
6700     (prog1
6701         (let ((articles (gnus-summary-find-matching
6702                          (cons 'extra header) regexp 'all)))
6703           (unless articles
6704             (error "Found no matches for \"%s\"" regexp))
6705           (gnus-summary-limit articles))
6706       (gnus-summary-position-point))))
6707
6708 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6709 (make-obsolete
6710  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6711
6712 (defun gnus-summary-limit-to-unread (&optional all)
6713   "Limit the summary buffer to articles that are not marked as read.
6714 If ALL is non-nil, limit strictly to unread articles."
6715   (interactive "P")
6716   (if all
6717       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6718     (gnus-summary-limit-to-marks
6719      ;; Concat all the marks that say that an article is read and have
6720      ;; those removed.
6721      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6722            gnus-killed-mark gnus-kill-file-mark
6723            gnus-low-score-mark gnus-expirable-mark
6724            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6725            gnus-duplicate-mark gnus-souped-mark)
6726      'reverse)))
6727
6728 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6729 (make-obsolete 'gnus-summary-delete-marked-with
6730                'gnus-summary-limit-exlude-marks)
6731
6732 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6733   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6734 If REVERSE, limit the summary buffer to articles that are marked
6735 with MARKS.  MARKS can either be a string of marks or a list of marks.
6736 Returns how many articles were removed."
6737   (interactive "sMarks: ")
6738   (gnus-summary-limit-to-marks marks t))
6739
6740 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6741   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6742 If REVERSE (the prefix), limit the summary buffer to articles that are
6743 not marked with MARKS.  MARKS can either be a string of marks or a
6744 list of marks.
6745 Returns how many articles were removed."
6746   (interactive "sMarks: \nP")
6747   (prog1
6748       (let ((data gnus-newsgroup-data)
6749             (marks (if (listp marks) marks
6750                      (append marks nil))) ; Transform to list.
6751             articles)
6752         (while data
6753           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6754                   (memq (gnus-data-mark (car data)) marks))
6755             (push (gnus-data-number (car data)) articles))
6756           (setq data (cdr data)))
6757         (gnus-summary-limit articles))
6758     (gnus-summary-position-point)))
6759
6760 (defun gnus-summary-limit-to-score (&optional score)
6761   "Limit to articles with score at or above SCORE."
6762   (interactive "P")
6763   (setq score (if score
6764                   (prefix-numeric-value score)
6765                 (or gnus-summary-default-score 0)))
6766   (let ((data gnus-newsgroup-data)
6767         articles)
6768     (while data
6769       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6770                 score)
6771         (push (gnus-data-number (car data)) articles))
6772       (setq data (cdr data)))
6773     (prog1
6774         (gnus-summary-limit articles)
6775       (gnus-summary-position-point))))
6776
6777 (defun gnus-summary-limit-include-thread (id)
6778   "Display all the hidden articles that in the current thread."
6779   (interactive (list (mail-header-id (gnus-summary-article-header))))
6780   (let ((articles (gnus-articles-in-thread
6781                    (gnus-id-to-thread (gnus-root-id id)))))
6782     (prog1
6783         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6784       (gnus-summary-position-point))))
6785
6786 (defun gnus-summary-limit-include-dormant ()
6787   "Display all the hidden articles that are marked as dormant.
6788 Note that this command only works on a subset of the articles currently
6789 fetched for this group."
6790   (interactive)
6791   (unless gnus-newsgroup-dormant
6792     (error "There are no dormant articles in this group"))
6793   (prog1
6794       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6795     (gnus-summary-position-point)))
6796
6797 (defun gnus-summary-limit-exclude-dormant ()
6798   "Hide all dormant articles."
6799   (interactive)
6800   (prog1
6801       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6802     (gnus-summary-position-point)))
6803
6804 (defun gnus-summary-limit-exclude-childless-dormant ()
6805   "Hide all dormant articles that have no children."
6806   (interactive)
6807   (let ((data (gnus-data-list t))
6808         articles d children)
6809     ;; Find all articles that are either not dormant or have
6810     ;; children.
6811     (while (setq d (pop data))
6812       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6813                 (and (setq children
6814                            (gnus-article-children (gnus-data-number d)))
6815                      (let (found)
6816                        (while children
6817                          (when (memq (car children) articles)
6818                            (setq children nil
6819                                  found t))
6820                          (pop children))
6821                        found)))
6822         (push (gnus-data-number d) articles)))
6823     ;; Do the limiting.
6824     (prog1
6825         (gnus-summary-limit articles)
6826       (gnus-summary-position-point))))
6827
6828 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6829   "Mark all unread excluded articles as read.
6830 If ALL, mark even excluded ticked and dormants as read."
6831   (interactive "P")
6832   (let ((articles (gnus-sorted-complement
6833                    (sort
6834                     (mapcar (lambda (h) (mail-header-number h))
6835                             gnus-newsgroup-headers)
6836                     '<)
6837                    (sort gnus-newsgroup-limit '<)))
6838         article)
6839     (setq gnus-newsgroup-unreads
6840           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6841     (if all
6842         (setq gnus-newsgroup-dormant nil
6843               gnus-newsgroup-marked nil
6844               gnus-newsgroup-reads
6845               (nconc
6846                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6847                gnus-newsgroup-reads))
6848       (while (setq article (pop articles))
6849         (unless (or (memq article gnus-newsgroup-dormant)
6850                     (memq article gnus-newsgroup-marked))
6851           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6852
6853 (defun gnus-summary-limit (articles &optional pop)
6854   (if pop
6855       ;; We pop the previous limit off the stack and use that.
6856       (setq articles (car gnus-newsgroup-limits)
6857             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6858     ;; We use the new limit, so we push the old limit on the stack.
6859     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6860   ;; Set the limit.
6861   (setq gnus-newsgroup-limit articles)
6862   (let ((total (length gnus-newsgroup-data))
6863         (data (gnus-data-find-list (gnus-summary-article-number)))
6864         (gnus-summary-mark-below nil)   ; Inhibit this.
6865         found)
6866     ;; This will do all the work of generating the new summary buffer
6867     ;; according to the new limit.
6868     (gnus-summary-prepare)
6869     ;; Hide any threads, possibly.
6870     (and gnus-show-threads
6871          gnus-thread-hide-subtree
6872          (gnus-summary-hide-all-threads))
6873     ;; Try to return to the article you were at, or one in the
6874     ;; neighborhood.
6875     (when data
6876       ;; We try to find some article after the current one.
6877       (while data
6878         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6879           (setq data nil
6880                 found t))
6881         (setq data (cdr data))))
6882     (unless found
6883       ;; If there is no data, that means that we were after the last
6884       ;; article.  The same goes when we can't find any articles
6885       ;; after the current one.
6886       (goto-char (point-max))
6887       (gnus-summary-find-prev))
6888     (gnus-set-mode-line 'summary)
6889     ;; We return how many articles were removed from the summary
6890     ;; buffer as a result of the new limit.
6891     (- total (length gnus-newsgroup-data))))
6892
6893 (defsubst gnus-invisible-cut-children (threads)
6894   (let ((num 0))
6895     (while threads
6896       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6897         (incf num))
6898       (pop threads))
6899     (< num 2)))
6900
6901 (defsubst gnus-cut-thread (thread)
6902   "Go forwards in the thread until we find an article that we want to display."
6903   (when (or (eq gnus-fetch-old-headers 'some)
6904             (eq gnus-fetch-old-headers 'invisible)
6905             (numberp gnus-fetch-old-headers)
6906             (eq gnus-build-sparse-threads 'some)
6907             (eq gnus-build-sparse-threads 'more))
6908     ;; Deal with old-fetched headers and sparse threads.
6909     (while (and
6910             thread
6911             (or
6912              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6913              (gnus-summary-article-ancient-p
6914               (mail-header-number (car thread))))
6915             (if (or (<= (length (cdr thread)) 1)
6916                     (eq gnus-fetch-old-headers 'invisible))
6917                 (setq gnus-newsgroup-limit
6918                       (delq (mail-header-number (car thread))
6919                             gnus-newsgroup-limit)
6920                       thread (cadr thread))
6921               (when (gnus-invisible-cut-children (cdr thread))
6922                 (let ((th (cdr thread)))
6923                   (while th
6924                     (if (memq (mail-header-number (caar th))
6925                               gnus-newsgroup-limit)
6926                         (setq thread (car th)
6927                               th nil)
6928                       (setq th (cdr th))))))))))
6929   thread)
6930
6931 (defun gnus-cut-threads (threads)
6932   "Cut off all uninteresting articles from the beginning of threads."
6933   (when (or (eq gnus-fetch-old-headers 'some)
6934             (eq gnus-fetch-old-headers 'invisible)
6935             (numberp gnus-fetch-old-headers)
6936             (eq gnus-build-sparse-threads 'some)
6937             (eq gnus-build-sparse-threads 'more))
6938     (let ((th threads))
6939       (while th
6940         (setcar th (gnus-cut-thread (car th)))
6941         (setq th (cdr th)))))
6942   ;; Remove nixed out threads.
6943   (delq nil threads))
6944
6945 (defun gnus-summary-initial-limit (&optional show-if-empty)
6946   "Figure out what the initial limit is supposed to be on group entry.
6947 This entails weeding out unwanted dormants, low-scored articles,
6948 fetch-old-headers verbiage, and so on."
6949   ;; Most groups have nothing to remove.
6950   (if (or gnus-inhibit-limiting
6951           (and (null gnus-newsgroup-dormant)
6952                (not (eq gnus-fetch-old-headers 'some))
6953                (not (numberp gnus-fetch-old-headers))
6954                (not (eq gnus-fetch-old-headers 'invisible))
6955                (null gnus-summary-expunge-below)
6956                (not (eq gnus-build-sparse-threads 'some))
6957                (not (eq gnus-build-sparse-threads 'more))
6958                (null gnus-thread-expunge-below)
6959                (not gnus-use-nocem)))
6960       ()                                ; Do nothing.
6961     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6962     (setq gnus-newsgroup-limit nil)
6963     (mapatoms
6964      (lambda (node)
6965        (unless (car (symbol-value node))
6966          ;; These threads have no parents -- they are roots.
6967          (let ((nodes (cdr (symbol-value node)))
6968                thread)
6969            (while nodes
6970              (if (and gnus-thread-expunge-below
6971                       (< (gnus-thread-total-score (car nodes))
6972                          gnus-thread-expunge-below))
6973                  (gnus-expunge-thread (pop nodes))
6974                (setq thread (pop nodes))
6975                (gnus-summary-limit-children thread))))))
6976      gnus-newsgroup-dependencies)
6977     ;; If this limitation resulted in an empty group, we might
6978     ;; pop the previous limit and use it instead.
6979     (when (and (not gnus-newsgroup-limit)
6980                show-if-empty)
6981       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6982     gnus-newsgroup-limit))
6983
6984 (defun gnus-summary-limit-children (thread)
6985   "Return 1 if this subthread is visible and 0 if it is not."
6986   ;; First we get the number of visible children to this thread.  This
6987   ;; is done by recursing down the thread using this function, so this
6988   ;; will really go down to a leaf article first, before slowly
6989   ;; working its way up towards the root.
6990   (when thread
6991     (let ((children
6992            (if (cdr thread)
6993                (apply '+ (mapcar 'gnus-summary-limit-children
6994                                  (cdr thread)))
6995              0))
6996           (number (mail-header-number (car thread)))
6997           score)
6998       (if (and
6999            (not (memq number gnus-newsgroup-marked))
7000            (or
7001             ;; If this article is dormant and has absolutely no visible
7002             ;; children, then this article isn't visible.
7003             (and (memq number gnus-newsgroup-dormant)
7004                  (zerop children))
7005             ;; If this is "fetch-old-headered" and there is no
7006             ;; visible children, then we don't want this article.
7007             (and (or (eq gnus-fetch-old-headers 'some)
7008                      (numberp gnus-fetch-old-headers))
7009                  (gnus-summary-article-ancient-p number)
7010                  (zerop children))
7011             ;; If this is "fetch-old-headered" and `invisible', then
7012             ;; we don't want this article.
7013             (and (eq gnus-fetch-old-headers 'invisible)
7014                  (gnus-summary-article-ancient-p number))
7015             ;; If this is a sparsely inserted article with no children,
7016             ;; we don't want it.
7017             (and (eq gnus-build-sparse-threads 'some)
7018                  (gnus-summary-article-sparse-p number)
7019                  (zerop children))
7020             ;; If we use expunging, and this article is really
7021             ;; low-scored, then we don't want this article.
7022             (when (and gnus-summary-expunge-below
7023                        (< (setq score
7024                                 (or (cdr (assq number gnus-newsgroup-scored))
7025                                     gnus-summary-default-score))
7026                           gnus-summary-expunge-below))
7027               ;; We increase the expunge-tally here, but that has
7028               ;; nothing to do with the limits, really.
7029               (incf gnus-newsgroup-expunged-tally)
7030               ;; We also mark as read here, if that's wanted.
7031               (when (and gnus-summary-mark-below
7032                          (< score gnus-summary-mark-below))
7033                 (setq gnus-newsgroup-unreads
7034                       (delq number gnus-newsgroup-unreads))
7035                 (if gnus-newsgroup-auto-expire
7036                     (push number gnus-newsgroup-expirable)
7037                   (push (cons number gnus-low-score-mark)
7038                         gnus-newsgroup-reads)))
7039               t)
7040             ;; Check NoCeM things.
7041             (if (and gnus-use-nocem
7042                      (gnus-nocem-unwanted-article-p
7043                       (mail-header-id (car thread))))
7044                 (progn
7045                   (setq gnus-newsgroup-unreads
7046                         (delq number gnus-newsgroup-unreads))
7047                   t))))
7048           ;; Nope, invisible article.
7049           0
7050         ;; Ok, this article is to be visible, so we add it to the limit
7051         ;; and return 1.
7052         (push number gnus-newsgroup-limit)
7053         1))))
7054
7055 (defun gnus-expunge-thread (thread)
7056   "Mark all articles in THREAD as read."
7057   (let* ((number (mail-header-number (car thread))))
7058     (incf gnus-newsgroup-expunged-tally)
7059     ;; We also mark as read here, if that's wanted.
7060     (setq gnus-newsgroup-unreads
7061           (delq number gnus-newsgroup-unreads))
7062     (if gnus-newsgroup-auto-expire
7063         (push number gnus-newsgroup-expirable)
7064       (push (cons number gnus-low-score-mark)
7065             gnus-newsgroup-reads)))
7066   ;; Go recursively through all subthreads.
7067   (mapcar 'gnus-expunge-thread (cdr thread)))
7068
7069 ;; Summary article oriented commands
7070
7071 (defun gnus-summary-refer-parent-article (n)
7072   "Refer parent article N times.
7073 If N is negative, go to ancestor -N instead.
7074 The difference between N and the number of articles fetched is returned."
7075   (interactive "p")
7076   (let ((skip 1)
7077         error header ref)
7078     (when (not (natnump n))
7079       (setq skip (abs n)
7080             n 1))
7081     (while (and (> n 0)
7082                 (not error))
7083       (setq header (gnus-summary-article-header))
7084       (if (and (eq (mail-header-number header)
7085                    (cdr gnus-article-current))
7086                (equal gnus-newsgroup-name
7087                       (car gnus-article-current)))
7088           ;; If we try to find the parent of the currently
7089           ;; displayed article, then we take a look at the actual
7090           ;; References header, since this is slightly more
7091           ;; reliable than the References field we got from the
7092           ;; server.
7093           (save-excursion
7094             (set-buffer gnus-original-article-buffer)
7095             (nnheader-narrow-to-headers)
7096             (unless (setq ref (message-fetch-field "references"))
7097               (setq ref (message-fetch-field "in-reply-to")))
7098             (widen))
7099         (setq ref
7100               ;; It's not the current article, so we take a bet on
7101               ;; the value we got from the server.
7102               (mail-header-references header)))
7103       (if (and ref
7104                (not (equal ref "")))
7105           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7106             (gnus-message 1 "Couldn't find parent"))
7107         (gnus-message 1 "No references in article %d"
7108                       (gnus-summary-article-number))
7109         (setq error t))
7110       (decf n))
7111     (gnus-summary-position-point)
7112     n))
7113
7114 (defun gnus-summary-refer-references ()
7115   "Fetch all articles mentioned in the References header.
7116 Return the number of articles fetched."
7117   (interactive)
7118   (let ((ref (mail-header-references (gnus-summary-article-header)))
7119         (current (gnus-summary-article-number))
7120         (n 0))
7121     (if (or (not ref)
7122             (equal ref ""))
7123         (error "No References in the current article")
7124       ;; For each Message-ID in the References header...
7125       (while (string-match "<[^>]*>" ref)
7126         (incf n)
7127         ;; ... fetch that article.
7128         (gnus-summary-refer-article
7129          (prog1 (match-string 0 ref)
7130            (setq ref (substring ref (match-end 0))))))
7131       (gnus-summary-goto-subject current)
7132       (gnus-summary-position-point)
7133       n)))
7134
7135 (defun gnus-summary-refer-thread (&optional limit)
7136   "Fetch all articles in the current thread.
7137 If LIMIT (the numerical prefix), fetch that many old headers instead
7138 of what's specified by the `gnus-refer-thread-limit' variable."
7139   (interactive "P")
7140   (let ((id (mail-header-id (gnus-summary-article-header)))
7141         (limit (if limit (prefix-numeric-value limit)
7142                  gnus-refer-thread-limit)))
7143     ;; We want to fetch LIMIT *old* headers, but we also have to
7144     ;; re-fetch all the headers in the current buffer, because many of
7145     ;; them may be undisplayed.  So we adjust LIMIT.
7146     (when (numberp limit)
7147       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7148     (unless (eq gnus-fetch-old-headers 'invisible)
7149       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7150       ;; Retrieve the headers and read them in.
7151       (if (eq (gnus-retrieve-headers
7152                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7153               'nov)
7154           (gnus-build-all-threads)
7155         (error "Can't fetch thread from backends that don't support NOV"))
7156       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7157     (gnus-summary-limit-include-thread id)))
7158
7159 (defun gnus-summary-refer-article (message-id)
7160   "Fetch an article specified by MESSAGE-ID."
7161   (interactive "sMessage-ID: ")
7162   (when (and (stringp message-id)
7163              (not (zerop (length message-id))))
7164     ;; Construct the correct Message-ID if necessary.
7165     ;; Suggested by tale@pawl.rpi.edu.
7166     (unless (string-match "^<" message-id)
7167       (setq message-id (concat "<" message-id)))
7168     (unless (string-match ">$" message-id)
7169       (setq message-id (concat message-id ">")))
7170     (let* ((header (gnus-id-to-header message-id))
7171            (sparse (and header
7172                         (gnus-summary-article-sparse-p
7173                          (mail-header-number header))
7174                         (memq (mail-header-number header)
7175                               gnus-newsgroup-limit)))
7176            number)
7177       (cond
7178        ;; If the article is present in the buffer we just go to it.
7179        ((and header
7180              (or (not (gnus-summary-article-sparse-p
7181                        (mail-header-number header)))
7182                  sparse))
7183         (prog1
7184             (gnus-summary-goto-article
7185              (mail-header-number header) nil t)
7186           (when sparse
7187             (gnus-summary-update-article (mail-header-number header)))))
7188        (t
7189         ;; We fetch the article.
7190         (catch 'found
7191           (dolist (gnus-override-method (gnus-refer-article-methods))
7192             (gnus-check-server gnus-override-method)
7193             ;; Fetch the header, and display the article.
7194             (when (setq number (gnus-summary-insert-subject message-id))
7195               (gnus-summary-select-article nil nil nil number)
7196               (throw 'found t)))
7197           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7198
7199 (defun gnus-refer-article-methods ()
7200   "Return a list of referrable methods."
7201   (cond
7202    ;; No method, so we default to current and native.
7203    ((null gnus-refer-article-method)
7204     (list gnus-current-select-method gnus-select-method))
7205    ;; Current.
7206    ((eq 'current gnus-refer-article-method)
7207     (list gnus-current-select-method))
7208    ;; List of select methods.
7209    ((not (and (symbolp (car gnus-refer-article-method))
7210               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7211     (let (out)
7212       (dolist (method gnus-refer-article-method)
7213         (push (if (eq 'current method)
7214                   gnus-current-select-method
7215                 method)
7216               out))
7217       (nreverse out)))
7218    ;; One single select method.
7219    (t
7220     (list gnus-refer-article-method))))
7221
7222 (defun gnus-summary-edit-parameters ()
7223   "Edit the group parameters of the current group."
7224   (interactive)
7225   (gnus-group-edit-group gnus-newsgroup-name 'params))
7226
7227 (defun gnus-summary-customize-parameters ()
7228   "Customize the group parameters of the current group."
7229   (interactive)
7230   (gnus-group-customize gnus-newsgroup-name))
7231
7232 (defun gnus-summary-enter-digest-group (&optional force)
7233   "Enter an nndoc group based on the current article.
7234 If FORCE, force a digest interpretation.  If not, try
7235 to guess what the document format is."
7236   (interactive "P")
7237   (let ((conf gnus-current-window-configuration))
7238     (save-excursion
7239       (gnus-summary-select-article))
7240     (setq gnus-current-window-configuration conf)
7241     (let* ((name (format "%s-%d"
7242                          (gnus-group-prefixed-name
7243                           gnus-newsgroup-name (list 'nndoc ""))
7244                          (save-excursion
7245                            (set-buffer gnus-summary-buffer)
7246                            gnus-current-article)))
7247            (ogroup gnus-newsgroup-name)
7248            (params (append (gnus-info-params (gnus-get-info ogroup))
7249                            (list (cons 'to-group ogroup))
7250                            (list (cons 'save-article-group ogroup))))
7251            (case-fold-search t)
7252            (buf (current-buffer))
7253            dig to-address)
7254       (save-excursion
7255         (set-buffer gnus-original-article-buffer)
7256         ;; Have the digest group inherit the main mail address of
7257         ;; the parent article.
7258         (when (setq to-address (or (message-fetch-field "reply-to")
7259                                    (message-fetch-field "from")))
7260           (setq params (append
7261                         (list (cons 'to-address
7262                                     (funcall gnus-decode-encoded-word-function
7263                                              to-address))))))
7264         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7265         (insert-buffer-substring gnus-original-article-buffer)
7266         ;; Remove lines that may lead nndoc to misinterpret the
7267         ;; document type.
7268         (narrow-to-region
7269          (goto-char (point-min))
7270          (or (search-forward "\n\n" nil t) (point)))
7271         (goto-char (point-min))
7272         (delete-matching-lines "^Path:\\|^From ")
7273         (widen))
7274       (unwind-protect
7275           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7276                     (gnus-newsgroup-ephemeral-ignored-charsets
7277                      gnus-newsgroup-ignored-charsets))
7278                 (gnus-group-read-ephemeral-group
7279                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7280                               (nndoc-article-type
7281                                ,(if force 'mbox 'guess))) t))
7282               ;; Make all postings to this group go to the parent group.
7283               (nconc (gnus-info-params (gnus-get-info name))
7284                      params)
7285             ;; Couldn't select this doc group.
7286             (switch-to-buffer buf)
7287             (gnus-set-global-variables)
7288             (gnus-configure-windows 'summary)
7289             (gnus-message 3 "Article couldn't be entered?"))
7290         (kill-buffer dig)))))
7291
7292 (defun gnus-summary-read-document (n)
7293   "Open a new group based on the current article(s).
7294 This will allow you to read digests and other similar
7295 documents as newsgroups.
7296 Obeys the standard process/prefix convention."
7297   (interactive "P")
7298   (let* ((articles (gnus-summary-work-articles n))
7299          (ogroup gnus-newsgroup-name)
7300          (params (append (gnus-info-params (gnus-get-info ogroup))
7301                          (list (cons 'to-group ogroup))))
7302          article group egroup groups vgroup)
7303     (while (setq article (pop articles))
7304       (setq group (format "%s-%d" gnus-newsgroup-name article))
7305       (gnus-summary-remove-process-mark article)
7306       (when (gnus-summary-display-article article)
7307         (save-excursion
7308           (with-temp-buffer
7309             (insert-buffer-substring gnus-original-article-buffer)
7310             ;; Remove some headers that may lead nndoc to make
7311             ;; the wrong guess.
7312             (message-narrow-to-head)
7313             (goto-char (point-min))
7314             (delete-matching-lines "^\\(Path\\):\\|^From ")
7315             (widen)
7316             (if (setq egroup
7317                       (gnus-group-read-ephemeral-group
7318                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7319                                      (nndoc-article-type guess))
7320                        t nil t))
7321                 (progn
7322                   ;; Make all postings to this group go to the parent group.
7323                   (nconc (gnus-info-params (gnus-get-info egroup))
7324                          params)
7325                   (push egroup groups))
7326               ;; Couldn't select this doc group.
7327               (gnus-error 3 "Article couldn't be entered"))))))
7328     ;; Now we have selected all the documents.
7329     (cond
7330      ((not groups)
7331       (error "None of the articles could be interpreted as documents"))
7332      ((gnus-group-read-ephemeral-group
7333        (setq vgroup (format
7334                      "nnvirtual:%s-%s" gnus-newsgroup-name
7335                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7336        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7337        t
7338        (cons (current-buffer) 'summary)))
7339      (t
7340       (error "Couldn't select virtual nndoc group")))))
7341
7342 (defun gnus-summary-isearch-article (&optional regexp-p)
7343   "Do incremental search forward on the current article.
7344 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7345   (interactive "P")
7346   (let* ((gnus-inhibit-treatment t)
7347          (old (gnus-summary-select-article)))
7348     (gnus-configure-windows 'article)
7349     (gnus-eval-in-buffer-window gnus-article-buffer
7350       (save-restriction
7351         (widen)
7352         (when (eq 'old old)
7353           (gnus-article-show-all-headers))
7354         (goto-char (point-min))
7355         (isearch-forward regexp-p)))))
7356
7357 (defun gnus-summary-search-article-forward (regexp &optional backward)
7358   "Search for an article containing REGEXP forward.
7359 If BACKWARD, search backward instead."
7360   (interactive
7361    (list (read-string
7362           (format "Search article %s (regexp%s): "
7363                   (if current-prefix-arg "backward" "forward")
7364                   (if gnus-last-search-regexp
7365                       (concat ", default " gnus-last-search-regexp)
7366                     "")))
7367          current-prefix-arg))
7368   (if (string-equal regexp "")
7369       (setq regexp (or gnus-last-search-regexp ""))
7370     (setq gnus-last-search-regexp regexp)
7371     (setq gnus-article-before-search gnus-current-article))
7372   ;; Intentionally set gnus-last-article.
7373   (setq gnus-last-article gnus-article-before-search)
7374   (let ((gnus-last-article gnus-last-article))
7375     (if (gnus-summary-search-article regexp backward)
7376         (gnus-summary-show-thread)
7377       (error "Search failed: \"%s\"" regexp))))
7378
7379 (defun gnus-summary-search-article-backward (regexp)
7380   "Search for an article containing REGEXP backward."
7381   (interactive
7382    (list (read-string
7383           (format "Search article backward (regexp%s): "
7384                   (if gnus-last-search-regexp
7385                       (concat ", default " gnus-last-search-regexp)
7386                     "")))))
7387   (gnus-summary-search-article-forward regexp 'backward))
7388
7389 (eval-when-compile
7390   (defmacro gnus-summary-search-article-position-point (regexp backward)
7391     "Dehighlight the last matched text and goto the beginning position."
7392     (` (if (and gnus-summary-search-article-matched-data
7393                 (let ((text (caddr gnus-summary-search-article-matched-data))
7394                       (inhibit-read-only t)
7395                       buffer-read-only)
7396                   (delete-region
7397                    (goto-char (car gnus-summary-search-article-matched-data))
7398                    (cadr gnus-summary-search-article-matched-data))
7399                   (insert text)
7400                   (string-match (, regexp) text)))
7401            (if (, backward) (beginning-of-line) (end-of-line))
7402          (goto-char (if (, backward) (point-max) (point-min))))))
7403
7404   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7405     "Place point where X-Face image is displayed."
7406     (if (featurep 'xemacs)
7407         (` (let ((end (if (search-forward "\n\n" nil t)
7408                           (goto-char (1- (point)))
7409                         (point-min)))
7410                  extent)
7411              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7412              (unless (and (re-search-forward "^From:" end t)
7413                           (setq extent (extent-at (point)))
7414                           (extent-begin-glyph extent))
7415                (goto-char (, opoint)))))
7416       (` (let ((end (if (search-forward "\n\n" nil t)
7417                         (goto-char (1- (point)))
7418                       (point-min)))
7419                (start (or (search-backward "\n\n" nil t) (point-min))))
7420            (goto-char
7421             (or (text-property-any start end 'x-face-image t);; x-face-e21
7422                 (text-property-any start end 'x-face-mule-bitmap-image t)
7423                 (, opoint)))))))
7424
7425   (defmacro gnus-summary-search-article-highlight-matched-text
7426     (backward treated x-face)
7427     "Highlight matched text in the function `gnus-summary-search-article'."
7428     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7429              (end (set-marker (make-marker) (match-end 0)))
7430              (inhibit-read-only t)
7431              buffer-read-only)
7432          (unless treated
7433            (let ((,@
7434                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7435                     (mapcar
7436                      (lambda (item) (setq items (delq item items)))
7437                      '(gnus-treat-buttonize
7438                        gnus-treat-fill-article
7439                        gnus-treat-fill-long-lines
7440                        gnus-treat-emphasize
7441                        gnus-treat-highlight-headers
7442                        gnus-treat-highlight-citation
7443                        gnus-treat-highlight-signature
7444                        gnus-treat-overstrike
7445                        gnus-treat-display-xface
7446                        gnus-treat-buttonize-head
7447                        gnus-treat-decode-article-as-default-mime-charset))
7448                     (static-if (featurep 'xemacs)
7449                         items
7450                       (cons '(x-face-mule-delete-x-face-field
7451                               (quote never))
7452                             items))))
7453                  (gnus-treat-display-xface
7454                   (when (, x-face) gnus-treat-display-xface)))
7455              (gnus-article-prepare-mime-display)))
7456          (goto-char (if (, backward) start end))
7457          (when (, x-face)
7458            (gnus-summary-search-article-highlight-goto-x-face (point)))
7459          (setq gnus-summary-search-article-matched-data
7460                (list start end (buffer-substring start end)))
7461          (unless (eq start end);; matched text has been deleted. :-<
7462            (put-text-property start end 'face
7463                               (or (find-face 'isearch)
7464                                   'secondary-selection))))))
7465   )
7466
7467 (defun gnus-summary-search-article (regexp &optional backward)
7468   "Search for an article containing REGEXP.
7469 Optional argument BACKWARD means do search for backward.
7470 `gnus-select-article-hook' is not called during the search."
7471   ;; We have to require this here to make sure that the following
7472   ;; dynamic binding isn't shadowed by autoloading.
7473   (require 'gnus-async)
7474   (require 'gnus-art)
7475   (let ((gnus-select-article-hook nil)  ;Disable hook.
7476         (gnus-article-prepare-hook nil)
7477         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7478         (gnus-use-article-prefetch nil)
7479         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7480         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7481         (sum (current-buffer))
7482         (found nil)
7483         point treated)
7484     (gnus-save-hidden-threads
7485       (static-if (featurep 'xemacs)
7486           (let ((gnus-inhibit-treatment t))
7487             (setq treated (eq 'old (gnus-summary-select-article)))
7488             (when (and treated
7489                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7490                                  (window-live-p (get-buffer-window
7491                                                  gnus-article-buffer t)))))
7492               (gnus-summary-select-article nil t)
7493               (setq treated nil)))
7494         (let ((gnus-inhibit-treatment t)
7495               (x-face-mule-delete-x-face-field 'never))
7496           (setq treated (eq 'old (gnus-summary-select-article)))
7497           (when (and treated
7498                      (not
7499                       (and (gnus-buffer-live-p gnus-article-buffer)
7500                            (window-live-p (get-buffer-window
7501                                            gnus-article-buffer t))
7502                            (or (not (string-match "^\\^X-Face:" regexp))
7503                                (with-current-buffer gnus-article-buffer
7504                                  gnus-summary-search-article-matched-data)))))
7505             (gnus-summary-select-article nil t)
7506             (setq treated nil))))
7507       (set-buffer gnus-article-buffer)
7508       (widen)
7509       (if treated
7510           (progn
7511             (gnus-article-show-all-headers)
7512             (gnus-summary-search-article-position-point regexp backward))
7513         (goto-char (if backward (point-max) (point-min))))
7514       (while (not found)
7515         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7516         (if (if backward
7517                 (re-search-backward regexp nil t)
7518               (re-search-forward regexp nil t))
7519             ;; We found the regexp.
7520             (progn
7521               (gnus-summary-search-article-highlight-matched-text
7522                backward treated (string-match "^\\^X-Face:" regexp))
7523               (setq found 'found)
7524               (forward-line
7525                (/ (- 2 (window-height
7526                         (get-buffer-window gnus-article-buffer t)))
7527                   2))
7528               (set-window-start
7529                (get-buffer-window (current-buffer))
7530                (point))
7531               (set-buffer sum)
7532               (setq point (point)))
7533           ;; We didn't find it, so we go to the next article.
7534           (set-buffer sum)
7535           (setq found 'not)
7536           (while (eq found 'not)
7537             (if (not (if backward (gnus-summary-find-prev)
7538                        (gnus-summary-find-next)))
7539                 ;; No more articles.
7540                 (setq found t)
7541               ;; Select the next article and adjust point.
7542               (unless (gnus-summary-article-sparse-p
7543                        (gnus-summary-article-number))
7544                 (setq found nil)
7545                 (let ((gnus-inhibit-treatment t))
7546                   (gnus-summary-select-article))
7547                 (setq treated nil)
7548                 (set-buffer gnus-article-buffer)
7549                 (widen)
7550                 (goto-char (if backward (point-max) (point-min))))))))
7551       (gnus-message 7 ""))
7552     ;; Return whether we found the regexp.
7553     (when (eq found 'found)
7554       (goto-char point)
7555       (gnus-summary-show-thread)
7556       (gnus-summary-goto-subject gnus-current-article)
7557       (gnus-summary-position-point)
7558       t)))
7559
7560 (defun gnus-summary-find-matching (header regexp &optional backward unread
7561                                           not-case-fold)
7562   "Return a list of all articles that match REGEXP on HEADER.
7563 The search stars on the current article and goes forwards unless
7564 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7565 If UNREAD is non-nil, only unread articles will
7566 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7567 in the comparisons."
7568   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7569                 (gnus-data-find-list
7570                  (gnus-summary-article-number) (gnus-data-list backward))))
7571         (case-fold-search (not not-case-fold))
7572         articles d func)
7573     (if (consp header)
7574         (if (eq (car header) 'extra)
7575             (setq func
7576                   `(lambda (h)
7577                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7578                          "")))
7579           (error "%s is an invalid header" header))
7580       (unless (fboundp (intern (concat "mail-header-" header)))
7581         (error "%s is not a valid header" header))
7582       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7583     (while data
7584       (setq d (car data))
7585       (and (or (not unread)             ; We want all articles...
7586                (gnus-data-unread-p d))  ; Or just unreads.
7587            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7588            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7589            (push (gnus-data-number d) articles)) ; Success!
7590       (setq data (cdr data)))
7591     (nreverse articles)))
7592
7593 (defun gnus-summary-execute-command (header regexp command &optional backward)
7594   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7595 If HEADER is an empty string (or nil), the match is done on the entire
7596 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7597   (interactive
7598    (list (let ((completion-ignore-case t))
7599            (completing-read
7600             "Header name: "
7601             (mapcar (lambda (string) (list string))
7602                     '("Number" "Subject" "From" "Lines" "Date"
7603                       "Message-ID" "Xref" "References" "Body"))
7604             nil 'require-match))
7605          (read-string "Regexp: ")
7606          (read-key-sequence "Command: ")
7607          current-prefix-arg))
7608   (when (equal header "Body")
7609     (setq header ""))
7610   ;; Hidden thread subtrees must be searched as well.
7611   (gnus-summary-show-all-threads)
7612   ;; We don't want to change current point nor window configuration.
7613   (save-excursion
7614     (save-window-excursion
7615       (gnus-message 6 "Executing %s..." (key-description command))
7616       ;; We'd like to execute COMMAND interactively so as to give arguments.
7617       (gnus-execute header regexp
7618                     `(call-interactively ',(key-binding command))
7619                     backward)
7620       (gnus-message 6 "Executing %s...done" (key-description command)))))
7621
7622 (defun gnus-summary-beginning-of-article ()
7623   "Scroll the article back to the beginning."
7624   (interactive)
7625   (gnus-summary-select-article)
7626   (gnus-configure-windows 'article)
7627   (gnus-eval-in-buffer-window gnus-article-buffer
7628     (widen)
7629     (goto-char (point-min))
7630     (when gnus-page-broken
7631       (gnus-narrow-to-page))))
7632
7633 (defun gnus-summary-end-of-article ()
7634   "Scroll to the end of the article."
7635   (interactive)
7636   (gnus-summary-select-article)
7637   (gnus-configure-windows 'article)
7638   (gnus-eval-in-buffer-window gnus-article-buffer
7639     (widen)
7640     (goto-char (point-max))
7641     (recenter -3)
7642     (when gnus-page-broken
7643       (gnus-narrow-to-page))))
7644
7645 (defun gnus-summary-print-article (&optional filename n)
7646   "Generate and print a PostScript image of the N next (mail) articles.
7647
7648 If N is negative, print the N previous articles.  If N is nil and articles
7649 have been marked with the process mark, print these instead.
7650
7651 If the optional first argument FILENAME is nil, send the image to the
7652 printer.  If FILENAME is a string, save the PostScript image in a file with
7653 that name.  If FILENAME is a number, prompt the user for the name of the file
7654 to save in."
7655   (interactive (list (ps-print-preprint current-prefix-arg)))
7656   (dolist (article (gnus-summary-work-articles n))
7657     (gnus-summary-select-article nil nil 'pseudo article)
7658     (gnus-eval-in-buffer-window gnus-article-buffer
7659       (let ((buffer (generate-new-buffer " *print*")))
7660         (unwind-protect
7661             (progn
7662               (copy-to-buffer buffer (point-min) (point-max))
7663               (set-buffer buffer)
7664               (gnus-article-delete-invisible-text)
7665               (when (gnus-visual-p 'article-highlight 'highlight)
7666                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7667                 ;; highlight.
7668                 (let ((gnus-article-buffer buffer))
7669                   (gnus-article-highlight-citation t)
7670                   (gnus-article-highlight-signature)))
7671               (let ((ps-left-header
7672                      (list
7673                       (concat "("
7674                               (mail-header-subject gnus-current-headers) ")")
7675                       (concat "("
7676                               (mail-header-from gnus-current-headers) ")")))
7677                     (ps-right-header
7678                      (list
7679                       "/pagenumberstring load"
7680                       (concat "("
7681                               (mail-header-date gnus-current-headers) ")"))))
7682                 (gnus-run-hooks 'gnus-ps-print-hook)
7683                 (save-excursion
7684                   (ps-spool-buffer-with-faces))))
7685           (kill-buffer buffer))))
7686     (gnus-summary-remove-process-mark article))
7687   (ps-despool filename))
7688
7689 (defun gnus-summary-show-article (&optional arg)
7690   "Force re-fetching of the current article.
7691 If ARG (the prefix) is a number, show the article with the charset
7692 defined in `gnus-summary-show-article-charset-alist', or the charset
7693 inputed.
7694 If ARG (the prefix) is non-nil and not a number, show the raw article
7695 without any article massaging functions being run."
7696   (interactive "P")
7697   (cond
7698    ((numberp arg)
7699     (let ((gnus-newsgroup-charset
7700            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7701                (read-coding-system "Charset: ")))
7702           (gnus-newsgroup-ignored-charsets 'gnus-all))
7703       (gnus-summary-select-article nil 'force)
7704       (let ((deps gnus-newsgroup-dependencies)
7705             head header)
7706         (save-excursion
7707           (set-buffer gnus-original-article-buffer)
7708           (save-restriction
7709             (message-narrow-to-head)
7710             (setq head (buffer-string)))
7711           (with-temp-buffer
7712             (insert (format "211 %d Article retrieved.\n"
7713                             (cdr gnus-article-current)))
7714             (insert head)
7715             (insert ".\n")
7716             (let ((nntp-server-buffer (current-buffer)))
7717               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7718         (gnus-data-set-header
7719          (gnus-data-find (cdr gnus-article-current))
7720          header)
7721         (gnus-summary-update-article-line
7722          (cdr gnus-article-current) header))))
7723    ((not arg)
7724     ;; Select the article the normal way.
7725     (gnus-summary-select-article nil 'force))
7726    (t
7727     ;; We have to require this here to make sure that the following
7728     ;; dynamic binding isn't shadowed by autoloading.
7729     (require 'gnus-async)
7730     (require 'gnus-art)
7731     ;; Bind the article treatment functions to nil.
7732     (let ((gnus-have-all-headers t)
7733           gnus-article-prepare-hook
7734           gnus-article-decode-hook
7735           gnus-break-pages
7736           gnus-show-mime
7737           (gnus-inhibit-treatment t))
7738       (gnus-summary-select-article nil 'force))))
7739   (gnus-summary-goto-subject gnus-current-article)
7740   (gnus-summary-position-point))
7741
7742 (defun gnus-summary-verbose-headers (&optional arg)
7743   "Toggle permanent full header display.
7744 If ARG is a positive number, turn header display on.
7745 If ARG is a negative number, turn header display off."
7746   (interactive "P")
7747   (setq gnus-show-all-headers
7748         (cond ((or (not (numberp arg))
7749                    (zerop arg))
7750                (not gnus-show-all-headers))
7751               ((natnump arg)
7752                t)))
7753   (gnus-summary-show-article))
7754
7755 (defun gnus-summary-toggle-header (&optional arg)
7756   "Show the headers if they are hidden, or hide them if they are shown.
7757 If ARG is a positive number, show the entire header.
7758 If ARG is a negative number, hide the unwanted header lines."
7759   (interactive "P")
7760   (save-excursion
7761     (set-buffer gnus-article-buffer)
7762     (save-restriction
7763       (let* ((buffer-read-only nil)
7764              (inhibit-point-motion-hooks t)
7765              hidden e)
7766         (setq hidden
7767               (if (numberp arg)
7768                   (>= arg 0)
7769                 (save-restriction
7770                   (article-narrow-to-head)
7771                   (gnus-article-hidden-text-p 'headers))))
7772         (goto-char (point-min))
7773         (when (search-forward "\n\n" nil t)
7774           (delete-region (point-min) (1- (point))))
7775         (goto-char (point-min))
7776         (save-excursion
7777           (set-buffer gnus-original-article-buffer)
7778           (goto-char (point-min))
7779           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7780         (insert-buffer-substring gnus-original-article-buffer 1 e)
7781         (save-restriction
7782           (narrow-to-region (point-min) (point))
7783           (article-decode-encoded-words)
7784           (if  hidden
7785               (let ((gnus-treat-hide-headers nil)
7786                     (gnus-treat-hide-boring-headers nil))
7787                 (setq gnus-article-wash-types
7788                       (delq 'headers gnus-article-wash-types))
7789                 (gnus-treat-article 'head))
7790             (gnus-treat-article 'head)))
7791         (gnus-set-mode-line 'article)))))
7792
7793 (defun gnus-summary-show-all-headers ()
7794   "Make all header lines visible."
7795   (interactive)
7796   (gnus-article-show-all-headers))
7797
7798 (defun gnus-summary-toggle-mime (&optional arg)
7799   "Toggle MIME processing.
7800 If ARG is a positive number, turn MIME processing on."
7801   (interactive "P")
7802   (setq gnus-show-mime
7803         (if (null arg)
7804             (not gnus-show-mime)
7805           (> (prefix-numeric-value arg) 0)))
7806   (gnus-summary-select-article t 'force))
7807
7808 (defun gnus-summary-caesar-message (&optional arg)
7809   "Caesar rotate the current article by 13.
7810 The numerical prefix specifies how many places to rotate each letter
7811 forward."
7812   (interactive "P")
7813   (gnus-summary-select-article)
7814   (let ((mail-header-separator ""))
7815     (gnus-eval-in-buffer-window gnus-article-buffer
7816       (save-restriction
7817         (widen)
7818         (let ((start (window-start))
7819               buffer-read-only)
7820           (message-caesar-buffer-body arg)
7821           (set-window-start (get-buffer-window (current-buffer)) start))))))
7822
7823 (defun gnus-summary-stop-page-breaking ()
7824   "Stop page breaking in the current article."
7825   (interactive)
7826   (gnus-summary-select-article)
7827   (gnus-eval-in-buffer-window gnus-article-buffer
7828     (widen)
7829     (when (gnus-visual-p 'page-marker)
7830       (let ((buffer-read-only nil))
7831         (gnus-remove-text-with-property 'gnus-prev)
7832         (gnus-remove-text-with-property 'gnus-next))
7833       (setq gnus-page-broken nil))))
7834
7835 (defun gnus-summary-move-article (&optional n to-newsgroup
7836                                             select-method action)
7837   "Move the current article to a different newsgroup.
7838 If N is a positive number, move the N next articles.
7839 If N is a negative number, move the N previous articles.
7840 If N is nil and any articles have been marked with the process mark,
7841 move those articles instead.
7842 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7843 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7844 re-spool using this method.
7845
7846 For this function to work, both the current newsgroup and the
7847 newsgroup that you want to move to have to support the `request-move'
7848 and `request-accept' functions.
7849
7850 ACTION can be either `move' (the default), `crosspost' or `copy'."
7851   (interactive "P")
7852   (unless action
7853     (setq action 'move))
7854   ;; Disable marking as read.
7855   (let (gnus-mark-article-hook)
7856     (save-window-excursion
7857       (gnus-summary-select-article)))
7858   ;; Check whether the source group supports the required functions.
7859   (cond ((and (eq action 'move)
7860               (not (gnus-check-backend-function
7861                     'request-move-article gnus-newsgroup-name)))
7862          (error "The current group does not support article moving"))
7863         ((and (eq action 'crosspost)
7864               (not (gnus-check-backend-function
7865                     'request-replace-article gnus-newsgroup-name)))
7866          (error "The current group does not support article editing")))
7867   (let ((articles (gnus-summary-work-articles n))
7868         (prefix (if (gnus-check-backend-function
7869                     'request-move-article gnus-newsgroup-name)
7870                     (gnus-group-real-prefix gnus-newsgroup-name)
7871                   ""))
7872         (names '((move "Move" "Moving")
7873                  (copy "Copy" "Copying")
7874                  (crosspost "Crosspost" "Crossposting")))
7875         (copy-buf (save-excursion
7876                     (nnheader-set-temp-buffer " *copy article*")))
7877         (default-marks gnus-article-mark-lists)
7878         (no-expire-marks (delete '(expirable . expire)
7879                                  (copy-sequence gnus-article-mark-lists)))
7880         art-group to-method new-xref article to-groups)
7881     (unless (assq action names)
7882       (error "Unknown action %s" action))
7883     ;; Read the newsgroup name.
7884     (when (and (not to-newsgroup)
7885                (not select-method))
7886       (setq to-newsgroup
7887             (gnus-read-move-group-name
7888              (cadr (assq action names))
7889              (symbol-value (intern (format "gnus-current-%s-group" action)))
7890              articles prefix))
7891       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7892     (setq to-method (or select-method
7893                         (gnus-server-to-method
7894                          (gnus-group-method to-newsgroup))))
7895     ;; Check the method we are to move this article to...
7896     (unless (gnus-check-backend-function
7897              'request-accept-article (car to-method))
7898       (error "%s does not support article copying" (car to-method)))
7899     (unless (gnus-check-server to-method)
7900       (error "Can't open server %s" (car to-method)))
7901     (gnus-message 6 "%s to %s: %s..."
7902                   (caddr (assq action names))
7903                   (or (car select-method) to-newsgroup) articles)
7904     (while articles
7905       (setq article (pop articles))
7906       (setq
7907        art-group
7908        (cond
7909         ;; Move the article.
7910         ((eq action 'move)
7911          ;; Remove this article from future suppression.
7912          (gnus-dup-unsuppress-article article)
7913          (gnus-request-move-article
7914           article                       ; Article to move
7915           gnus-newsgroup-name           ; From newsgroup
7916           (nth 1 (gnus-find-method-for-group
7917                   gnus-newsgroup-name)) ; Server
7918           (list 'gnus-request-accept-article
7919                 to-newsgroup (list 'quote select-method)
7920                 (not articles) t)       ; Accept form
7921           (not articles)))              ; Only save nov last time
7922         ;; Copy the article.
7923         ((eq action 'copy)
7924          (save-excursion
7925            (set-buffer copy-buf)
7926            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7927              (gnus-request-accept-article
7928               to-newsgroup select-method (not articles) t))))
7929         ;; Crosspost the article.
7930         ((eq action 'crosspost)
7931          (let ((xref (message-tokenize-header
7932                       (mail-header-xref (gnus-summary-article-header article))
7933                       " ")))
7934            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7935                                   ":" article))
7936            (unless xref
7937              (setq xref (list (system-name))))
7938            (setq new-xref
7939                  (concat
7940                   (mapconcat 'identity
7941                              (delete "Xref:" (delete new-xref xref))
7942                              " ")
7943                   " " new-xref))
7944            (save-excursion
7945              (set-buffer copy-buf)
7946              ;; First put the article in the destination group.
7947              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7948              (when (consp (setq art-group
7949                                 (gnus-request-accept-article
7950                                  to-newsgroup select-method (not articles))))
7951                (setq new-xref (concat new-xref " " (car art-group)
7952                                       ":" (cdr art-group)))
7953                ;; Now we have the new Xrefs header, so we insert
7954                ;; it and replace the new article.
7955                (nnheader-replace-header "Xref" new-xref)
7956                (gnus-request-replace-article
7957                 (cdr art-group) to-newsgroup (current-buffer))
7958                art-group))))))
7959       (cond
7960        ((not art-group)
7961         (gnus-message 1 "Couldn't %s article %s: %s"
7962                       (cadr (assq action names)) article
7963                       (nnheader-get-report (car to-method))))
7964        ((eq art-group 'junk)
7965         (when (eq action 'move)
7966           (gnus-summary-mark-article article gnus-canceled-mark)
7967           (gnus-message 4 "Deleted article %s" article)))
7968        (t
7969         (let* ((pto-group (gnus-group-prefixed-name
7970                            (car art-group) to-method))
7971                (entry
7972                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7973                (info (nth 2 entry))
7974                (to-group (gnus-info-group info))
7975                to-marks)
7976           ;; Update the group that has been moved to.
7977           (when (and info
7978                      (memq action '(move copy)))
7979             (unless (member to-group to-groups)
7980               (push to-group to-groups))
7981
7982             (unless (memq article gnus-newsgroup-unreads)
7983               (push 'read to-marks)
7984               (gnus-info-set-read
7985                info (gnus-add-to-range (gnus-info-read info)
7986                                        (list (cdr art-group)))))
7987
7988             ;; See whether the article is to be put in the cache.
7989             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7990                              default-marks
7991                            no-expire-marks))
7992                   (to-article (cdr art-group)))
7993
7994               ;; Enter the article into the cache in the new group,
7995               ;; if that is required.
7996               (when gnus-use-cache
7997                 (gnus-cache-possibly-enter-article
7998                  to-group to-article
7999                  (let ((header (copy-sequence
8000                                 (gnus-summary-article-header article))))
8001                    (mail-header-set-number header to-article)
8002                    header)
8003                  (memq article gnus-newsgroup-marked)
8004                  (memq article gnus-newsgroup-dormant)
8005                  (memq article gnus-newsgroup-unreads)))
8006
8007               (when gnus-preserve-marks
8008                 ;; Copy any marks over to the new group.
8009                 (when (and (equal to-group gnus-newsgroup-name)
8010                            (not (memq article gnus-newsgroup-unreads)))
8011                   ;; Mark this article as read in this group.
8012                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8013                   (setcdr (gnus-active to-group) to-article)
8014                   (setcdr gnus-newsgroup-active to-article))
8015
8016                 (while marks
8017                   (when (memq article (symbol-value
8018                                        (intern (format "gnus-newsgroup-%s"
8019                                                        (caar marks)))))
8020                     (push (cdar marks) to-marks)
8021                     ;; If the other group is the same as this group,
8022                     ;; then we have to add the mark to the list.
8023                     (when (equal to-group gnus-newsgroup-name)
8024                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8025                            (cons to-article
8026                                  (symbol-value
8027                                   (intern (format "gnus-newsgroup-%s"
8028                                                   (caar marks)))))))
8029                     ;; Copy the marks to other group.
8030                     (gnus-add-marked-articles
8031                      to-group (cdar marks) (list to-article) info))
8032                   (setq marks (cdr marks)))
8033
8034                 (gnus-request-set-mark to-group (list (list (list to-article)
8035                                                             'set
8036                                                             to-marks))))
8037
8038               (gnus-dribble-enter
8039                (concat "(gnus-group-set-info '"
8040                        (gnus-prin1-to-string (gnus-get-info to-group))
8041                        ")"))))
8042
8043           ;; Update the Xref header in this article to point to
8044           ;; the new crossposted article we have just created.
8045           (when (eq action 'crosspost)
8046             (save-excursion
8047               (set-buffer copy-buf)
8048               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8049               (nnheader-replace-header "Xref" new-xref)
8050               (gnus-request-replace-article
8051                article gnus-newsgroup-name (current-buffer)))))
8052
8053         ;;;!!!Why is this necessary?
8054         (set-buffer gnus-summary-buffer)
8055
8056         (gnus-summary-goto-subject article)
8057         (when (eq action 'move)
8058           (gnus-summary-mark-article article gnus-canceled-mark))))
8059       (gnus-summary-remove-process-mark article))
8060     ;; Re-activate all groups that have been moved to.
8061     (while to-groups
8062       (save-excursion
8063         (set-buffer gnus-group-buffer)
8064         (when (gnus-group-goto-group (car to-groups) t)
8065           (gnus-group-get-new-news-this-group 1 t))
8066         (pop to-groups)))
8067
8068     (gnus-kill-buffer copy-buf)
8069     (gnus-summary-position-point)
8070     (gnus-set-mode-line 'summary)))
8071
8072 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8073   "Move the current article to a different newsgroup.
8074 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8075 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8076 re-spool using this method."
8077   (interactive "P")
8078   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8079
8080 (defun gnus-summary-crosspost-article (&optional n)
8081   "Crosspost the current article to some other group."
8082   (interactive "P")
8083   (gnus-summary-move-article n nil nil 'crosspost))
8084
8085 (defcustom gnus-summary-respool-default-method nil
8086   "Default method for respooling an article.
8087 If nil, use to the current newsgroup method."
8088   :type '(choice (gnus-select-method :value (nnml ""))
8089                  (const nil))
8090   :group 'gnus-summary-mail)
8091
8092 (defun gnus-summary-respool-article (&optional n method)
8093   "Respool the current article.
8094 The article will be squeezed through the mail spooling process again,
8095 which means that it will be put in some mail newsgroup or other
8096 depending on `nnmail-split-methods'.
8097 If N is a positive number, respool the N next articles.
8098 If N is a negative number, respool the N previous articles.
8099 If N is nil and any articles have been marked with the process mark,
8100 respool those articles instead.
8101
8102 Respooling can be done both from mail groups and \"real\" newsgroups.
8103 In the former case, the articles in question will be moved from the
8104 current group into whatever groups they are destined to.  In the
8105 latter case, they will be copied into the relevant groups."
8106   (interactive
8107    (list current-prefix-arg
8108          (let* ((methods (gnus-methods-using 'respool))
8109                 (methname
8110                  (symbol-name (or gnus-summary-respool-default-method
8111                                   (car (gnus-find-method-for-group
8112                                         gnus-newsgroup-name)))))
8113                 (method
8114                  (gnus-completing-read
8115                   methname "What backend do you want to use when respooling?"
8116                   methods nil t nil 'gnus-mail-method-history))
8117                 ms)
8118            (cond
8119             ((zerop (length (setq ms (gnus-servers-using-backend
8120                                       (intern method)))))
8121              (list (intern method) ""))
8122             ((= 1 (length ms))
8123              (car ms))
8124             (t
8125              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8126                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8127                            ms-alist))))))))
8128   (unless method
8129     (error "No method given for respooling"))
8130   (if (assoc (symbol-name
8131               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8132              (gnus-methods-using 'respool))
8133       (gnus-summary-move-article n nil method)
8134     (gnus-summary-copy-article n nil method)))
8135
8136 (defun gnus-summary-import-article (file &optional edit)
8137   "Import an arbitrary file into a mail newsgroup."
8138   (interactive "fImport file: \nP")
8139   (let ((group gnus-newsgroup-name)
8140         (now (current-time))
8141         atts lines group-art)
8142     (unless (gnus-check-backend-function 'request-accept-article group)
8143       (error "%s does not support article importing" group))
8144     (or (file-readable-p file)
8145         (not (file-regular-p file))
8146         (error "Can't read %s" file))
8147     (save-excursion
8148       (set-buffer (gnus-get-buffer-create " *import file*"))
8149       (erase-buffer)
8150       (nnheader-insert-file-contents file)
8151       (goto-char (point-min))
8152       (unless (nnheader-article-p)
8153         ;; This doesn't look like an article, so we fudge some headers.
8154         (setq atts (file-attributes file)
8155               lines (count-lines (point-min) (point-max)))
8156         (insert "From: " (read-string "From: ") "\n"
8157                 "Subject: " (read-string "Subject: ") "\n"
8158                 "Date: " (message-make-date (nth 5 atts)) "\n"
8159                 "Message-ID: " (message-make-message-id) "\n"
8160                 "Lines: " (int-to-string lines) "\n"
8161                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8162       (setq group-art (gnus-request-accept-article group nil t))
8163       (kill-buffer (current-buffer)))
8164     (setq gnus-newsgroup-active (gnus-activate-group group))
8165     (forward-line 1)
8166     (gnus-summary-goto-article (cdr group-art) nil t)
8167     (when edit
8168       (gnus-summary-edit-article))))
8169
8170 (defun gnus-summary-create-article ()
8171   "Create an article in a mail newsgroup."
8172   (interactive)
8173   (let ((group gnus-newsgroup-name)
8174         (now (current-time))
8175         group-art)
8176     (unless (gnus-check-backend-function 'request-accept-article group)
8177       (error "%s does not support article importing" group))
8178     (save-excursion
8179       (set-buffer (gnus-get-buffer-create " *import file*"))
8180       (erase-buffer)
8181       (goto-char (point-min))
8182       ;; This doesn't look like an article, so we fudge some headers.
8183       (insert "From: " (read-string "From: ") "\n"
8184               "Subject: " (read-string "Subject: ") "\n"
8185               "Date: " (message-make-date now) "\n"
8186               "Message-ID: " (message-make-message-id) "\n")
8187       (setq group-art (gnus-request-accept-article group nil t))
8188       (kill-buffer (current-buffer)))
8189     (setq gnus-newsgroup-active (gnus-activate-group group))
8190     (forward-line 1)
8191     (gnus-summary-goto-article (cdr group-art) nil t)
8192     (gnus-summary-edit-article)))
8193
8194 (defun gnus-summary-article-posted-p ()
8195   "Say whether the current (mail) article is available from news as well.
8196 This will be the case if the article has both been mailed and posted."
8197   (interactive)
8198   (let ((id (mail-header-references (gnus-summary-article-header)))
8199         (gnus-override-method (car (gnus-refer-article-methods))))
8200     (if (gnus-request-head id "")
8201         (gnus-message 2 "The current message was found on %s"
8202                       gnus-override-method)
8203       (gnus-message 2 "The current message couldn't be found on %s"
8204                     gnus-override-method)
8205       nil)))
8206
8207 (defun gnus-summary-expire-articles (&optional now)
8208   "Expire all articles that are marked as expirable in the current group."
8209   (interactive)
8210   (when (gnus-check-backend-function
8211          'request-expire-articles gnus-newsgroup-name)
8212     ;; This backend supports expiry.
8213     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8214            (expirable (if total
8215                           (progn
8216                             ;; We need to update the info for
8217                             ;; this group for `gnus-list-of-read-articles'
8218                             ;; to give us the right answer.
8219                             (gnus-run-hooks 'gnus-exit-group-hook)
8220                             (gnus-summary-update-info)
8221                             (gnus-list-of-read-articles gnus-newsgroup-name))
8222                         (setq gnus-newsgroup-expirable
8223                               (sort gnus-newsgroup-expirable '<))))
8224            (expiry-wait (if now 'immediate
8225                           (gnus-group-find-parameter
8226                            gnus-newsgroup-name 'expiry-wait)))
8227            (nnmail-expiry-target
8228             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8229                 nnmail-expiry-target))
8230            es)
8231       (when expirable
8232         ;; There are expirable articles in this group, so we run them
8233         ;; through the expiry process.
8234         (gnus-message 6 "Expiring articles...")
8235         (unless (gnus-check-group gnus-newsgroup-name)
8236           (error "Can't open server for %s" gnus-newsgroup-name))
8237         ;; The list of articles that weren't expired is returned.
8238         (save-excursion
8239           (if expiry-wait
8240               (let ((nnmail-expiry-wait-function nil)
8241                     (nnmail-expiry-wait expiry-wait))
8242                 (setq es (gnus-request-expire-articles
8243                           expirable gnus-newsgroup-name)))
8244             (setq es (gnus-request-expire-articles
8245                       expirable gnus-newsgroup-name)))
8246           (unless total
8247             (setq gnus-newsgroup-expirable es))
8248           ;; We go through the old list of expirable, and mark all
8249           ;; really expired articles as nonexistent.
8250           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8251             (let ((gnus-use-cache nil))
8252               (while expirable
8253                 (unless (memq (car expirable) es)
8254                   (when (gnus-data-find (car expirable))
8255                     (gnus-summary-mark-article
8256                      (car expirable) gnus-canceled-mark)))
8257                 (setq expirable (cdr expirable))))))
8258         (gnus-message 6 "Expiring articles...done")))))
8259
8260 (defun gnus-summary-expire-articles-now ()
8261   "Expunge all expirable articles in the current group.
8262 This means that *all* articles that are marked as expirable will be
8263 deleted forever, right now."
8264   (interactive)
8265   (or gnus-expert-user
8266       (gnus-yes-or-no-p
8267        "Are you really, really, really sure you want to delete all these messages? ")
8268       (error "Phew!"))
8269   (gnus-summary-expire-articles t))
8270
8271 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8272 (defun gnus-summary-delete-article (&optional n)
8273   "Delete the N next (mail) articles.
8274 This command actually deletes articles.  This is not a marking
8275 command.  The article will disappear forever from your life, never to
8276 return.
8277 If N is negative, delete backwards.
8278 If N is nil and articles have been marked with the process mark,
8279 delete these instead."
8280   (interactive "P")
8281   (unless (gnus-check-backend-function 'request-expire-articles
8282                                        gnus-newsgroup-name)
8283     (error "The current newsgroup does not support article deletion"))
8284   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8285     (error "Couldn't open server"))
8286   ;; Compute the list of articles to delete.
8287   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8288         not-deleted)
8289     (if (and gnus-novice-user
8290              (not (gnus-yes-or-no-p
8291                    (format "Do you really want to delete %s forever? "
8292                            (if (> (length articles) 1)
8293                                (format "these %s articles" (length articles))
8294                              "this article")))))
8295         ()
8296       ;; Delete the articles.
8297       (setq not-deleted (gnus-request-expire-articles
8298                          articles gnus-newsgroup-name 'force))
8299       (while articles
8300         (gnus-summary-remove-process-mark (car articles))
8301         ;; The backend might not have been able to delete the article
8302         ;; after all.
8303         (unless (memq (car articles) not-deleted)
8304           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8305         (setq articles (cdr articles)))
8306       (when not-deleted
8307         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8308     (gnus-summary-position-point)
8309     (gnus-set-mode-line 'summary)
8310     not-deleted))
8311
8312 (defun gnus-summary-edit-article (&optional force)
8313   "Edit the current article.
8314 This will have permanent effect only in mail groups.
8315 If FORCE is non-nil, allow editing of articles even in read-only
8316 groups."
8317   (interactive "P")
8318   (save-excursion
8319     (set-buffer gnus-summary-buffer)
8320     (let ((mail-parse-charset gnus-newsgroup-charset)
8321           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8322       (gnus-set-global-variables)
8323       (when (and (not force)
8324                  (gnus-group-read-only-p))
8325         (error "The current newsgroup does not support article editing"))
8326       (gnus-summary-show-article t)
8327       (gnus-article-edit-article
8328        'ignore
8329        `(lambda (no-highlight)
8330           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8331                 (message-options message-options)
8332                 (message-options-set-recipient)
8333                 (mail-parse-ignored-charsets
8334                  ',gnus-newsgroup-ignored-charsets))
8335             (gnus-summary-edit-article-done
8336              ,(or (mail-header-references gnus-current-headers) "")
8337              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8338
8339 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8340
8341 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8342                                                  no-highlight)
8343   "Make edits to the current article permanent."
8344   (interactive)
8345   (save-excursion
8346     ;; The buffer restriction contains the entire article if it exists.
8347     (when (article-goto-body)
8348       (let ((lines (count-lines (point) (point-max)))
8349             (length (- (point-max) (point)))
8350             (case-fold-search t)
8351             (body (copy-marker (point))))
8352         (goto-char (point-min))
8353         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8354           (delete-region (match-beginning 1) (match-end 1))
8355           (insert (number-to-string length)))
8356         (goto-char (point-min))
8357         (when (re-search-forward
8358                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8359           (delete-region (match-beginning 1) (match-end 1))
8360           (insert (number-to-string length)))
8361         (goto-char (point-min))
8362         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8363           (delete-region (match-beginning 1) (match-end 1))
8364           (insert (number-to-string lines))))))
8365   ;; Replace the article.
8366   (let ((buf (current-buffer)))
8367     (with-temp-buffer
8368       (insert-buffer-substring buf)
8369
8370       (if (and (not read-only)
8371                (not (gnus-request-replace-article
8372                      (cdr gnus-article-current) (car gnus-article-current)
8373                      (current-buffer) t)))
8374           (error "Couldn't replace article")
8375         ;; Update the summary buffer.
8376         (if (and references
8377                  (equal (message-tokenize-header references " ")
8378                         (message-tokenize-header
8379                          (or (message-fetch-field "references") "") " ")))
8380             ;; We only have to update this line.
8381             (save-excursion
8382               (save-restriction
8383                 (message-narrow-to-head)
8384                 (let ((head (buffer-string))
8385                       header)
8386                   (with-temp-buffer
8387                     (insert (format "211 %d Article retrieved.\n"
8388                                     (cdr gnus-article-current)))
8389                     (insert head)
8390                     (insert ".\n")
8391                     (let ((nntp-server-buffer (current-buffer)))
8392                       (setq header (car (gnus-get-newsgroup-headers
8393                                          (save-excursion
8394                                            (set-buffer gnus-summary-buffer)
8395                                            gnus-newsgroup-dependencies)
8396                                          t))))
8397                     (save-excursion
8398                       (set-buffer gnus-summary-buffer)
8399                       (gnus-data-set-header
8400                        (gnus-data-find (cdr gnus-article-current))
8401                        header)
8402                       (gnus-summary-update-article-line
8403                        (cdr gnus-article-current) header))))))
8404           ;; Update threads.
8405           (set-buffer (or buffer gnus-summary-buffer))
8406           (gnus-summary-update-article (cdr gnus-article-current)))
8407         ;; Prettify the article buffer again.
8408         (unless no-highlight
8409           (save-excursion
8410             (set-buffer gnus-article-buffer)
8411             ;;;!!! Fix this -- article should be rehighlighted.
8412             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8413             (set-buffer gnus-original-article-buffer)
8414             (gnus-request-article
8415              (cdr gnus-article-current)
8416              (car gnus-article-current) (current-buffer))))
8417         ;; Prettify the summary buffer line.
8418         (when (gnus-visual-p 'summary-highlight 'highlight)
8419           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8420
8421 (defun gnus-summary-edit-wash (key)
8422   "Perform editing command KEY in the article buffer."
8423   (interactive
8424    (list
8425     (progn
8426       (message "%s" (concat (this-command-keys) "- "))
8427       (read-char))))
8428   (message "")
8429   (gnus-summary-edit-article)
8430   (execute-kbd-macro (concat (this-command-keys) key))
8431   (gnus-article-edit-done))
8432
8433 ;;; Respooling
8434
8435 (defun gnus-summary-respool-query (&optional silent trace)
8436   "Query where the respool algorithm would put this article."
8437   (interactive)
8438   (let (gnus-mark-article-hook)
8439     (gnus-summary-select-article)
8440     (save-excursion
8441       (set-buffer gnus-original-article-buffer)
8442       (save-restriction
8443         (message-narrow-to-head)
8444         (let ((groups (nnmail-article-group 'identity trace)))
8445           (unless silent
8446             (if groups
8447                 (message "This message would go to %s"
8448                          (mapconcat 'car groups ", "))
8449               (message "This message would go to no groups"))
8450             groups))))))
8451
8452 (defun gnus-summary-respool-trace ()
8453   "Trace where the respool algorithm would put this article.
8454 Display a buffer showing all fancy splitting patterns which matched."
8455   (interactive)
8456   (gnus-summary-respool-query nil t))
8457
8458 ;; Summary marking commands.
8459
8460 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8461   "Mark articles which has the same subject as read, and then select the next.
8462 If UNMARK is positive, remove any kind of mark.
8463 If UNMARK is negative, tick articles."
8464   (interactive "P")
8465   (when unmark
8466     (setq unmark (prefix-numeric-value unmark)))
8467   (let ((count
8468          (gnus-summary-mark-same-subject
8469           (gnus-summary-article-subject) unmark)))
8470     ;; Select next unread article.  If auto-select-same mode, should
8471     ;; select the first unread article.
8472     (gnus-summary-next-article t (and gnus-auto-select-same
8473                                       (gnus-summary-article-subject)))
8474     (gnus-message 7 "%d article%s marked as %s"
8475                   count (if (= count 1) " is" "s are")
8476                   (if unmark "unread" "read"))))
8477
8478 (defun gnus-summary-kill-same-subject (&optional unmark)
8479   "Mark articles which has the same subject as read.
8480 If UNMARK is positive, remove any kind of mark.
8481 If UNMARK is negative, tick articles."
8482   (interactive "P")
8483   (when unmark
8484     (setq unmark (prefix-numeric-value unmark)))
8485   (let ((count
8486          (gnus-summary-mark-same-subject
8487           (gnus-summary-article-subject) unmark)))
8488     ;; If marked as read, go to next unread subject.
8489     (when (null unmark)
8490       ;; Go to next unread subject.
8491       (gnus-summary-next-subject 1 t))
8492     (gnus-message 7 "%d articles are marked as %s"
8493                   count (if unmark "unread" "read"))))
8494
8495 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8496   "Mark articles with same SUBJECT as read, and return marked number.
8497 If optional argument UNMARK is positive, remove any kinds of marks.
8498 If optional argument UNMARK is negative, mark articles as unread instead."
8499   (let ((count 1))
8500     (save-excursion
8501       (cond
8502        ((null unmark)                   ; Mark as read.
8503         (while (and
8504                 (progn
8505                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8506                   (gnus-summary-show-thread) t)
8507                 (gnus-summary-find-subject subject))
8508           (setq count (1+ count))))
8509        ((> unmark 0)                    ; Tick.
8510         (while (and
8511                 (progn
8512                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8513                   (gnus-summary-show-thread) t)
8514                 (gnus-summary-find-subject subject))
8515           (setq count (1+ count))))
8516        (t                               ; Mark as unread.
8517         (while (and
8518                 (progn
8519                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8520                   (gnus-summary-show-thread) t)
8521                 (gnus-summary-find-subject subject))
8522           (setq count (1+ count)))))
8523       (gnus-set-mode-line 'summary)
8524       ;; Return the number of marked articles.
8525       count)))
8526
8527 (defun gnus-summary-mark-as-processable (n &optional unmark)
8528   "Set the process mark on the next N articles.
8529 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8530 the process mark instead.  The difference between N and the actual
8531 number of articles marked is returned."
8532   (interactive "P")
8533   (if (and (null n) (gnus-region-active-p))
8534       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8535     (setq n (prefix-numeric-value n))
8536     (let ((backward (< n 0))
8537           (n (abs n)))
8538       (while (and
8539               (> n 0)
8540               (if unmark
8541                 (gnus-summary-remove-process-mark
8542                  (gnus-summary-article-number))
8543                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8544               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8545         (setq n (1- n)))
8546       (when (/= 0 n)
8547         (gnus-message 7 "No more articles"))
8548       (gnus-summary-recenter)
8549       (gnus-summary-position-point)
8550       n)))
8551
8552 (defun gnus-summary-unmark-as-processable (n)
8553   "Remove the process mark from the next N articles.
8554 If N is negative, unmark backward instead.  The difference between N and
8555 the actual number of articles unmarked is returned."
8556   (interactive "P")
8557   (gnus-summary-mark-as-processable n t))
8558
8559 (defun gnus-summary-unmark-all-processable ()
8560   "Remove the process mark from all articles."
8561   (interactive)
8562   (save-excursion
8563     (while gnus-newsgroup-processable
8564       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8565   (gnus-summary-position-point))
8566
8567 (defun gnus-summary-add-mark (article type)
8568   "Mark ARTICLE with a mark of TYPE."
8569   (let ((vtype (car (assq type gnus-article-mark-lists)))
8570         var)
8571     (if (not vtype)
8572         (error "No such mark type: %s" type)
8573       (setq var (intern (format "gnus-newsgroup-%s" type)))
8574       (set var (cons article (symbol-value var)))
8575       (if (memq type '(processable cached replied saved))
8576           (gnus-summary-update-secondary-mark article)
8577         ;;; !!! This is bobus.  We should find out what primary
8578         ;;; !!! mark we want to set.
8579         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8580
8581 (defun gnus-summary-mark-as-expirable (n)
8582   "Mark N articles forward as expirable.
8583 If N is negative, mark backward instead.  The difference between N and
8584 the actual number of articles marked is returned."
8585   (interactive "p")
8586   (gnus-summary-mark-forward n gnus-expirable-mark))
8587
8588 (defun gnus-summary-mark-article-as-replied (article)
8589   "Mark ARTICLE replied and update the summary line."
8590   (push article gnus-newsgroup-replied)
8591   (let ((buffer-read-only nil))
8592     (when (gnus-summary-goto-subject article nil t)
8593       (gnus-summary-update-secondary-mark article))))
8594
8595 (defun gnus-summary-set-bookmark (article)
8596   "Set a bookmark in current article."
8597   (interactive (list (gnus-summary-article-number)))
8598   (when (or (not (get-buffer gnus-article-buffer))
8599             (not gnus-current-article)
8600             (not gnus-article-current)
8601             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8602     (error "No current article selected"))
8603   ;; Remove old bookmark, if one exists.
8604   (let ((old (assq article gnus-newsgroup-bookmarks)))
8605     (when old
8606       (setq gnus-newsgroup-bookmarks
8607             (delq old gnus-newsgroup-bookmarks))))
8608   ;; Set the new bookmark, which is on the form
8609   ;; (article-number . line-number-in-body).
8610   (push
8611    (cons article
8612          (save-excursion
8613            (set-buffer gnus-article-buffer)
8614            (count-lines
8615             (min (point)
8616                  (save-excursion
8617                    (goto-char (point-min))
8618                    (search-forward "\n\n" nil t)
8619                    (point)))
8620             (point))))
8621    gnus-newsgroup-bookmarks)
8622   (gnus-message 6 "A bookmark has been added to the current article."))
8623
8624 (defun gnus-summary-remove-bookmark (article)
8625   "Remove the bookmark from the current article."
8626   (interactive (list (gnus-summary-article-number)))
8627   ;; Remove old bookmark, if one exists.
8628   (let ((old (assq article gnus-newsgroup-bookmarks)))
8629     (if old
8630         (progn
8631           (setq gnus-newsgroup-bookmarks
8632                 (delq old gnus-newsgroup-bookmarks))
8633           (gnus-message 6 "Removed bookmark."))
8634       (gnus-message 6 "No bookmark in current article."))))
8635
8636 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8637 (defun gnus-summary-mark-as-dormant (n)
8638   "Mark N articles forward as dormant.
8639 If N is negative, mark backward instead.  The difference between N and
8640 the actual number of articles marked is returned."
8641   (interactive "p")
8642   (gnus-summary-mark-forward n gnus-dormant-mark))
8643
8644 (defun gnus-summary-set-process-mark (article)
8645   "Set the process mark on ARTICLE and update the summary line."
8646   (setq gnus-newsgroup-processable
8647         (cons article
8648               (delq article gnus-newsgroup-processable)))
8649   (when (gnus-summary-goto-subject article)
8650     (gnus-summary-show-thread)
8651     (gnus-summary-goto-subject article)
8652     (gnus-summary-update-secondary-mark article)))
8653
8654 (defun gnus-summary-remove-process-mark (article)
8655   "Remove the process mark from ARTICLE and update the summary line."
8656   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8657   (when (gnus-summary-goto-subject article)
8658     (gnus-summary-show-thread)
8659     (gnus-summary-goto-subject article)
8660     (gnus-summary-update-secondary-mark article)))
8661
8662 (defun gnus-summary-set-saved-mark (article)
8663   "Set the process mark on ARTICLE and update the summary line."
8664   (push article gnus-newsgroup-saved)
8665   (when (gnus-summary-goto-subject article)
8666     (gnus-summary-update-secondary-mark article)))
8667
8668 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8669   "Mark N articles as read forwards.
8670 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8671 The difference between N and the actual number of articles marked is
8672 returned.
8673 Iff NO-EXPIRE, auto-expiry will be inhibited."
8674   (interactive "p")
8675   (gnus-summary-show-thread)
8676   (let ((backward (< n 0))
8677         (gnus-summary-goto-unread
8678          (and gnus-summary-goto-unread
8679               (not (eq gnus-summary-goto-unread 'never))
8680               (not (memq mark (list gnus-unread-mark
8681                                     gnus-ticked-mark gnus-dormant-mark)))))
8682         (n (abs n))
8683         (mark (or mark gnus-del-mark)))
8684     (while (and (> n 0)
8685                 (gnus-summary-mark-article nil mark no-expire)
8686                 (zerop (gnus-summary-next-subject
8687                         (if backward -1 1)
8688                         (and gnus-summary-goto-unread
8689                              (not (eq gnus-summary-goto-unread 'never)))
8690                         t)))
8691       (setq n (1- n)))
8692     (when (/= 0 n)
8693       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8694     (gnus-summary-recenter)
8695     (gnus-summary-position-point)
8696     (gnus-set-mode-line 'summary)
8697     n))
8698
8699 (defun gnus-summary-mark-article-as-read (mark)
8700   "Mark the current article quickly as read with MARK."
8701   (let ((article (gnus-summary-article-number)))
8702     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8703     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8704     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8705     (push (cons article mark) gnus-newsgroup-reads)
8706     ;; Possibly remove from cache, if that is used.
8707     (when gnus-use-cache
8708       (gnus-cache-enter-remove-article article))
8709     ;; Allow the backend to change the mark.
8710     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8711     ;; Check for auto-expiry.
8712     (when (and gnus-newsgroup-auto-expire
8713                (memq mark gnus-auto-expirable-marks))
8714       (setq mark gnus-expirable-mark)
8715       ;; Let the backend know about the mark change.
8716       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8717       (push article gnus-newsgroup-expirable))
8718     ;; Set the mark in the buffer.
8719     (gnus-summary-update-mark mark 'unread)
8720     t))
8721
8722 (defun gnus-summary-mark-article-as-unread (mark)
8723   "Mark the current article quickly as unread with MARK."
8724   (let* ((article (gnus-summary-article-number))
8725          (old-mark (gnus-summary-article-mark article)))
8726     ;; Allow the backend to change the mark.
8727     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8728     (if (eq mark old-mark)
8729         t
8730       (if (<= article 0)
8731           (progn
8732             (gnus-error 1 "Can't mark negative article numbers")
8733             nil)
8734         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8735         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8736         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8737         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8738         (cond ((= mark gnus-ticked-mark)
8739                (push article gnus-newsgroup-marked))
8740               ((= mark gnus-dormant-mark)
8741                (push article gnus-newsgroup-dormant))
8742               (t
8743                (push article gnus-newsgroup-unreads)))
8744         (gnus-pull article gnus-newsgroup-reads)
8745
8746         ;; See whether the article is to be put in the cache.
8747         (and gnus-use-cache
8748              (vectorp (gnus-summary-article-header article))
8749              (save-excursion
8750                (gnus-cache-possibly-enter-article
8751                 gnus-newsgroup-name article
8752                 (gnus-summary-article-header article)
8753                 (= mark gnus-ticked-mark)
8754                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8755
8756         ;; Fix the mark.
8757         (gnus-summary-update-mark mark 'unread)
8758         t))))
8759
8760 (defun gnus-summary-mark-article (&optional article mark no-expire)
8761   "Mark ARTICLE with MARK.  MARK can be any character.
8762 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8763 `??' (dormant) and `?E' (expirable).
8764 If MARK is nil, then the default character `?r' is used.
8765 If ARTICLE is nil, then the article on the current line will be
8766 marked.
8767 Iff NO-EXPIRE, auto-expiry will be inhibited."
8768   ;; The mark might be a string.
8769   (when (stringp mark)
8770     (setq mark (aref mark 0)))
8771   ;; If no mark is given, then we check auto-expiring.
8772   (when (null mark)
8773     (setq mark gnus-del-mark))
8774   (when (and (not no-expire)
8775              gnus-newsgroup-auto-expire
8776              (memq mark gnus-auto-expirable-marks))
8777     (setq mark gnus-expirable-mark))
8778   (let ((article (or article (gnus-summary-article-number)))
8779         (old-mark (gnus-summary-article-mark article)))
8780     ;; Allow the backend to change the mark.
8781     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8782     (if (eq mark old-mark)
8783         t
8784       (unless article
8785         (error "No article on current line"))
8786       (if (not (if (or (= mark gnus-unread-mark)
8787                        (= mark gnus-ticked-mark)
8788                        (= mark gnus-dormant-mark))
8789                    (gnus-mark-article-as-unread article mark)
8790                  (gnus-mark-article-as-read article mark)))
8791           t
8792         ;; See whether the article is to be put in the cache.
8793         (and gnus-use-cache
8794              (not (= mark gnus-canceled-mark))
8795              (vectorp (gnus-summary-article-header article))
8796              (save-excursion
8797                (gnus-cache-possibly-enter-article
8798                 gnus-newsgroup-name article
8799                 (gnus-summary-article-header article)
8800                 (= mark gnus-ticked-mark)
8801                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8802
8803         (when (gnus-summary-goto-subject article nil t)
8804           (let ((buffer-read-only nil))
8805             (gnus-summary-show-thread)
8806             ;; Fix the mark.
8807             (gnus-summary-update-mark mark 'unread)
8808             t))))))
8809
8810 (defun gnus-summary-update-secondary-mark (article)
8811   "Update the secondary (read, process, cache) mark."
8812   (gnus-summary-update-mark
8813    (cond ((memq article gnus-newsgroup-processable)
8814           gnus-process-mark)
8815          ((memq article gnus-newsgroup-cached)
8816           gnus-cached-mark)
8817          ((memq article gnus-newsgroup-replied)
8818           gnus-replied-mark)
8819          ((memq article gnus-newsgroup-saved)
8820           gnus-saved-mark)
8821          (t gnus-no-mark))
8822    'replied)
8823   (when (gnus-visual-p 'summary-highlight 'highlight)
8824     (gnus-run-hooks 'gnus-summary-update-hook))
8825   t)
8826
8827 (defun gnus-summary-update-mark (mark type)
8828   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8829         (buffer-read-only nil))
8830     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8831     (when forward
8832       (when (looking-at "\r")
8833         (incf forward))
8834       (when (<= (+ forward (point)) (point-max))
8835         ;; Go to the right position on the line.
8836         (goto-char (+ forward (point)))
8837         ;; Replace the old mark with the new mark.
8838         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8839         ;; Optionally update the marks by some user rule.
8840         (when (eq type 'unread)
8841           (gnus-data-set-mark
8842            (gnus-data-find (gnus-summary-article-number)) mark)
8843           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8844
8845 (defun gnus-mark-article-as-read (article &optional mark)
8846   "Enter ARTICLE in the pertinent lists and remove it from others."
8847   ;; Make the article expirable.
8848   (let ((mark (or mark gnus-del-mark)))
8849     (if (= mark gnus-expirable-mark)
8850         (push article gnus-newsgroup-expirable)
8851       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8852     ;; Remove from unread and marked lists.
8853     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8854     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8855     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8856     (push (cons article mark) gnus-newsgroup-reads)
8857     ;; Possibly remove from cache, if that is used.
8858     (when gnus-use-cache
8859       (gnus-cache-enter-remove-article article))
8860     t))
8861
8862 (defun gnus-mark-article-as-unread (article &optional mark)
8863   "Enter ARTICLE in the pertinent lists and remove it from others."
8864   (let ((mark (or mark gnus-ticked-mark)))
8865     (if (<= article 0)
8866         (progn
8867           (gnus-error 1 "Can't mark negative article numbers")
8868           nil)
8869       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8870             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8871             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8872             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8873
8874       ;; Unsuppress duplicates?
8875       (when gnus-suppress-duplicates
8876         (gnus-dup-unsuppress-article article))
8877
8878       (cond ((= mark gnus-ticked-mark)
8879              (push article gnus-newsgroup-marked))
8880             ((= mark gnus-dormant-mark)
8881              (push article gnus-newsgroup-dormant))
8882             (t
8883              (push article gnus-newsgroup-unreads)))
8884       (gnus-pull article gnus-newsgroup-reads)
8885       t)))
8886
8887 (defalias 'gnus-summary-mark-as-unread-forward
8888   'gnus-summary-tick-article-forward)
8889 (make-obsolete 'gnus-summary-mark-as-unread-forward
8890                'gnus-summary-tick-article-forward)
8891 (defun gnus-summary-tick-article-forward (n)
8892   "Tick N articles forwards.
8893 If N is negative, tick backwards instead.
8894 The difference between N and the number of articles ticked is returned."
8895   (interactive "p")
8896   (gnus-summary-mark-forward n gnus-ticked-mark))
8897
8898 (defalias 'gnus-summary-mark-as-unread-backward
8899   'gnus-summary-tick-article-backward)
8900 (make-obsolete 'gnus-summary-mark-as-unread-backward
8901                'gnus-summary-tick-article-backward)
8902 (defun gnus-summary-tick-article-backward (n)
8903   "Tick N articles backwards.
8904 The difference between N and the number of articles ticked is returned."
8905   (interactive "p")
8906   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8907
8908 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8909 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8910 (defun gnus-summary-tick-article (&optional article clear-mark)
8911   "Mark current article as unread.
8912 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8913 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8914   (interactive)
8915   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8916                                        gnus-ticked-mark)))
8917
8918 (defun gnus-summary-mark-as-read-forward (n)
8919   "Mark N articles as read forwards.
8920 If N is negative, mark backwards instead.
8921 The difference between N and the actual number of articles marked is
8922 returned."
8923   (interactive "p")
8924   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8925
8926 (defun gnus-summary-mark-as-read-backward (n)
8927   "Mark the N articles as read backwards.
8928 The difference between N and the actual number of articles marked is
8929 returned."
8930   (interactive "p")
8931   (gnus-summary-mark-forward
8932    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8933
8934 (defun gnus-summary-mark-as-read (&optional article mark)
8935   "Mark current article as read.
8936 ARTICLE specifies the article to be marked as read.
8937 MARK specifies a string to be inserted at the beginning of the line."
8938   (gnus-summary-mark-article article mark))
8939
8940 (defun gnus-summary-clear-mark-forward (n)
8941   "Clear marks from N articles forward.
8942 If N is negative, clear backward instead.
8943 The difference between N and the number of marks cleared is returned."
8944   (interactive "p")
8945   (gnus-summary-mark-forward n gnus-unread-mark))
8946
8947 (defun gnus-summary-clear-mark-backward (n)
8948   "Clear marks from N articles backward.
8949 The difference between N and the number of marks cleared is returned."
8950   (interactive "p")
8951   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8952
8953 (defun gnus-summary-mark-unread-as-read ()
8954   "Intended to be used by `gnus-summary-mark-article-hook'."
8955   (when (memq gnus-current-article gnus-newsgroup-unreads)
8956     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8957
8958 (defun gnus-summary-mark-read-and-unread-as-read ()
8959   "Intended to be used by `gnus-summary-mark-article-hook'."
8960   (let ((mark (gnus-summary-article-mark)))
8961     (when (or (gnus-unread-mark-p mark)
8962               (gnus-read-mark-p mark))
8963       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8964
8965 (defun gnus-summary-mark-unread-as-ticked ()
8966    "Intended to be used by `gnus-summary-mark-article-hook'."
8967   (when (memq gnus-current-article gnus-newsgroup-unreads)
8968     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8969
8970 (defun gnus-summary-mark-region-as-read (point mark all)
8971   "Mark all unread articles between point and mark as read.
8972 If given a prefix, mark all articles between point and mark as read,
8973 even ticked and dormant ones."
8974   (interactive "r\nP")
8975   (save-excursion
8976     (let (article)
8977       (goto-char point)
8978       (beginning-of-line)
8979       (while (and
8980               (< (point) mark)
8981               (progn
8982                 (when (or all
8983                           (memq (setq article (gnus-summary-article-number))
8984                                 gnus-newsgroup-unreads))
8985                   (gnus-summary-mark-article article gnus-del-mark))
8986                 t)
8987               (gnus-summary-find-next))))))
8988
8989 (defun gnus-summary-mark-below (score mark)
8990   "Mark articles with score less than SCORE with MARK."
8991   (interactive "P\ncMark: ")
8992   (setq score (if score
8993                   (prefix-numeric-value score)
8994                 (or gnus-summary-default-score 0)))
8995   (save-excursion
8996     (set-buffer gnus-summary-buffer)
8997     (goto-char (point-min))
8998     (while
8999         (progn
9000           (and (< (gnus-summary-article-score) score)
9001                (gnus-summary-mark-article nil mark))
9002           (gnus-summary-find-next)))))
9003
9004 (defun gnus-summary-kill-below (&optional score)
9005   "Mark articles with score below SCORE as read."
9006   (interactive "P")
9007   (gnus-summary-mark-below score gnus-killed-mark))
9008
9009 (defun gnus-summary-clear-above (&optional score)
9010   "Clear all marks from articles with score above SCORE."
9011   (interactive "P")
9012   (gnus-summary-mark-above score gnus-unread-mark))
9013
9014 (defun gnus-summary-tick-above (&optional score)
9015   "Tick all articles with score above SCORE."
9016   (interactive "P")
9017   (gnus-summary-mark-above score gnus-ticked-mark))
9018
9019 (defun gnus-summary-mark-above (score mark)
9020   "Mark articles with score over SCORE with MARK."
9021   (interactive "P\ncMark: ")
9022   (setq score (if score
9023                   (prefix-numeric-value score)
9024                 (or gnus-summary-default-score 0)))
9025   (save-excursion
9026     (set-buffer gnus-summary-buffer)
9027     (goto-char (point-min))
9028     (while (and (progn
9029                   (when (> (gnus-summary-article-score) score)
9030                     (gnus-summary-mark-article nil mark))
9031                   t)
9032                 (gnus-summary-find-next)))))
9033
9034 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9035 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9036 (defun gnus-summary-limit-include-expunged (&optional no-error)
9037   "Display all the hidden articles that were expunged for low scores."
9038   (interactive)
9039   (let ((buffer-read-only nil))
9040     (let ((scored gnus-newsgroup-scored)
9041           headers h)
9042       (while scored
9043         (unless (gnus-summary-article-header (caar scored))
9044           (and (setq h (gnus-number-to-header (caar scored)))
9045                (< (cdar scored) gnus-summary-expunge-below)
9046                (push h headers)))
9047         (setq scored (cdr scored)))
9048       (if (not headers)
9049           (when (not no-error)
9050             (error "No expunged articles hidden"))
9051         (goto-char (point-min))
9052         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9053         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9054         (mapcar (lambda (x) (push (mail-header-number x) 
9055                                   gnus-newsgroup-limit))
9056                 headers)
9057         (gnus-summary-prepare-unthreaded (nreverse headers))
9058         (goto-char (point-min))
9059         (gnus-summary-position-point)
9060         t))))
9061
9062 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9063   "Mark all unread articles in this newsgroup as read.
9064 If prefix argument ALL is non-nil, ticked and dormant articles will
9065 also be marked as read.
9066 If QUIETLY is non-nil, no questions will be asked.
9067 If TO-HERE is non-nil, it should be a point in the buffer.  All
9068 articles before this point will be marked as read.
9069 Note that this function will only catch up the unread article
9070 in the current summary buffer limitation.
9071 The number of articles marked as read is returned."
9072   (interactive "P")
9073   (prog1
9074       (save-excursion
9075         (when (or quietly
9076                   (not gnus-interactive-catchup) ;Without confirmation?
9077                   gnus-expert-user
9078                   (gnus-y-or-n-p
9079                    (if all
9080                        "Mark absolutely all articles as read? "
9081                      "Mark all unread articles as read? ")))
9082           (if (and not-mark
9083                    (not gnus-newsgroup-adaptive)
9084                    (not gnus-newsgroup-auto-expire)
9085                    (not gnus-suppress-duplicates)
9086                    (or (not gnus-use-cache)
9087                        (eq gnus-use-cache 'passive)))
9088               (progn
9089                 (when all
9090                   (setq gnus-newsgroup-marked nil
9091                         gnus-newsgroup-dormant nil))
9092                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9093             ;; We actually mark all articles as canceled, which we
9094             ;; have to do when using auto-expiry or adaptive scoring.
9095             (gnus-summary-show-all-threads)
9096             (when (gnus-summary-first-subject (not all) t)
9097               (while (and
9098                       (if to-here (< (point) to-here) t)
9099                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
9100                       (gnus-summary-find-next (not all) nil nil t))))
9101             (gnus-set-mode-line 'summary))
9102           t))
9103     (gnus-summary-position-point)))
9104
9105 (defun gnus-summary-catchup-to-here (&optional all)
9106   "Mark all unticked articles before the current one as read.
9107 If ALL is non-nil, also mark ticked and dormant articles as read."
9108   (interactive "P")
9109   (save-excursion
9110     (gnus-save-hidden-threads
9111       (let ((beg (point)))
9112         ;; We check that there are unread articles.
9113         (when (or all (gnus-summary-find-prev))
9114           (gnus-summary-catchup all t beg)))))
9115   (gnus-summary-position-point))
9116
9117 (defun gnus-summary-catchup-all (&optional quietly)
9118   "Mark all articles in this newsgroup as read."
9119   (interactive "P")
9120   (gnus-summary-catchup t quietly))
9121
9122 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9123   "Mark all unread articles in this group as read, then exit.
9124 If prefix argument ALL is non-nil, all articles are marked as read."
9125   (interactive "P")
9126   (when (gnus-summary-catchup all quietly nil 'fast)
9127     ;; Select next newsgroup or exit.
9128     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9129              (eq gnus-auto-select-next 'quietly))
9130         (gnus-summary-next-group nil)
9131       (gnus-summary-exit))))
9132
9133 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9134   "Mark all articles in this newsgroup as read, and then exit."
9135   (interactive "P")
9136   (gnus-summary-catchup-and-exit t quietly))
9137
9138 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9139   "Mark all articles in this group as read and select the next group.
9140 If given a prefix, mark all articles, unread as well as ticked, as
9141 read."
9142   (interactive "P")
9143   (save-excursion
9144     (gnus-summary-catchup all))
9145   (gnus-summary-next-group))
9146
9147 ;;;
9148 ;;; with article
9149 ;;;
9150
9151 (defmacro gnus-with-article (article &rest forms)
9152   "Select ARTICLE and perform FORMS in the original article buffer.
9153 Then replace the article with the result."
9154   `(progn
9155      ;; We don't want the article to be marked as read.
9156      (let (gnus-mark-article-hook)
9157        (gnus-summary-select-article t t nil ,article))
9158      (set-buffer gnus-original-article-buffer)
9159      ,@forms
9160      (if (not (gnus-check-backend-function
9161                'request-replace-article (car gnus-article-current)))
9162          (gnus-message 5 "Read-only group; not replacing")
9163        (unless (gnus-request-replace-article
9164                 ,article (car gnus-article-current)
9165                 (current-buffer) t)
9166          (error "Couldn't replace article")))
9167      ;; The cache and backlog have to be flushed somewhat.
9168      (when gnus-keep-backlog
9169        (gnus-backlog-remove-article
9170         (car gnus-article-current) (cdr gnus-article-current)))
9171      (when gnus-use-cache
9172        (gnus-cache-update-article
9173         (car gnus-article-current) (cdr gnus-article-current)))))
9174
9175 (put 'gnus-with-article 'lisp-indent-function 1)
9176 (put 'gnus-with-article 'edebug-form-spec '(form body))
9177
9178 ;; Thread-based commands.
9179
9180 (defun gnus-summary-articles-in-thread (&optional article)
9181   "Return a list of all articles in the current thread.
9182 If ARTICLE is non-nil, return all articles in the thread that starts
9183 with that article."
9184   (let* ((article (or article (gnus-summary-article-number)))
9185          (data (gnus-data-find-list article))
9186          (top-level (gnus-data-level (car data)))
9187          (top-subject
9188           (cond ((null gnus-thread-operation-ignore-subject)
9189                  (gnus-simplify-subject-re
9190                   (mail-header-subject (gnus-data-header (car data)))))
9191                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9192                  (gnus-simplify-subject-fuzzy
9193                   (mail-header-subject (gnus-data-header (car data)))))
9194                 (t nil)))
9195          (end-point (save-excursion
9196                       (if (gnus-summary-go-to-next-thread)
9197                           (point) (point-max))))
9198          articles)
9199     (while (and data
9200                 (< (gnus-data-pos (car data)) end-point))
9201       (when (or (not top-subject)
9202                 (string= top-subject
9203                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9204                              (gnus-simplify-subject-fuzzy
9205                               (mail-header-subject
9206                                (gnus-data-header (car data))))
9207                            (gnus-simplify-subject-re
9208                             (mail-header-subject
9209                              (gnus-data-header (car data)))))))
9210         (push (gnus-data-number (car data)) articles))
9211       (unless (and (setq data (cdr data))
9212                    (> (gnus-data-level (car data)) top-level))
9213         (setq data nil)))
9214     ;; Return the list of articles.
9215     (nreverse articles)))
9216
9217 (defun gnus-summary-rethread-current ()
9218   "Rethread the thread the current article is part of."
9219   (interactive)
9220   (let* ((gnus-show-threads t)
9221          (article (gnus-summary-article-number))
9222          (id (mail-header-id (gnus-summary-article-header)))
9223          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9224     (unless id
9225       (error "No article on the current line"))
9226     (gnus-rebuild-thread id)
9227     (gnus-summary-goto-subject article)))
9228
9229 (defun gnus-summary-reparent-thread ()
9230   "Make the current article child of the marked (or previous) article.
9231
9232 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9233 is non-nil or the Subject: of both articles are the same."
9234   (interactive)
9235   (unless (not (gnus-group-read-only-p))
9236     (error "The current newsgroup does not support article editing"))
9237   (unless (<= (length gnus-newsgroup-processable) 1)
9238     (error "No more than one article may be marked"))
9239   (save-window-excursion
9240     (let ((gnus-article-buffer " *reparent*")
9241           (current-article (gnus-summary-article-number))
9242           ;; First grab the marked article, otherwise one line up.
9243           (parent-article (if (not (null gnus-newsgroup-processable))
9244                               (car gnus-newsgroup-processable)
9245                             (save-excursion
9246                               (if (eq (forward-line -1) 0)
9247                                   (gnus-summary-article-number)
9248                                 (error "Beginning of summary buffer"))))))
9249       (unless (not (eq current-article parent-article))
9250         (error "An article may not be self-referential"))
9251       (let ((message-id (mail-header-id
9252                          (gnus-summary-article-header parent-article))))
9253         (unless (and message-id (not (equal message-id "")))
9254           (error "No message-id in desired parent"))
9255         (gnus-with-article current-article
9256           (save-restriction
9257             (goto-char (point-min))
9258             (message-narrow-to-head)
9259             (if (re-search-forward "^References: " nil t)
9260                 (progn
9261                   (re-search-forward "^[^ \t]" nil t)
9262                   (forward-line -1)
9263                   (end-of-line)
9264                   (insert " " message-id))
9265               (insert "References: " message-id "\n"))))
9266         (set-buffer gnus-summary-buffer)
9267         (gnus-summary-unmark-all-processable)
9268         (gnus-summary-update-article current-article)
9269         (gnus-summary-rethread-current)
9270         (gnus-message 3 "Article %d is now the child of article %d"
9271                       current-article parent-article)))))
9272
9273 (defun gnus-summary-toggle-threads (&optional arg)
9274   "Toggle showing conversation threads.
9275 If ARG is positive number, turn showing conversation threads on."
9276   (interactive "P")
9277   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9278     (setq gnus-show-threads
9279           (if (null arg) (not gnus-show-threads)
9280             (> (prefix-numeric-value arg) 0)))
9281     (gnus-summary-prepare)
9282     (gnus-summary-goto-subject current)
9283     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9284     (gnus-summary-position-point)))
9285
9286 (defun gnus-summary-show-all-threads ()
9287   "Show all threads."
9288   (interactive)
9289   (save-excursion
9290     (let ((buffer-read-only nil))
9291       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9292   (gnus-summary-position-point))
9293
9294 (defun gnus-summary-show-thread ()
9295   "Show thread subtrees.
9296 Returns nil if no thread was there to be shown."
9297   (interactive)
9298   (let ((buffer-read-only nil)
9299         (orig (point))
9300         ;; first goto end then to beg, to have point at beg after let
9301         (end (progn (end-of-line) (point)))
9302         (beg (progn (beginning-of-line) (point))))
9303     (prog1
9304         ;; Any hidden lines here?
9305         (search-forward "\r" end t)
9306       (subst-char-in-region beg end ?\^M ?\n t)
9307       (goto-char orig)
9308       (gnus-summary-position-point))))
9309
9310 (defun gnus-summary-hide-all-threads ()
9311   "Hide all thread subtrees."
9312   (interactive)
9313   (save-excursion
9314     (goto-char (point-min))
9315     (gnus-summary-hide-thread)
9316     (while (zerop (gnus-summary-next-thread 1 t))
9317       (gnus-summary-hide-thread)))
9318   (gnus-summary-position-point))
9319
9320 (defun gnus-summary-hide-thread ()
9321   "Hide thread subtrees.
9322 Returns nil if no threads were there to be hidden."
9323   (interactive)
9324   (let ((buffer-read-only nil)
9325         (start (point))
9326         (article (gnus-summary-article-number)))
9327     (goto-char start)
9328     ;; Go forward until either the buffer ends or the subthread
9329     ;; ends.
9330     (when (and (not (eobp))
9331                (or (zerop (gnus-summary-next-thread 1 t))
9332                    (goto-char (point-max))))
9333       (prog1
9334           (if (and (> (point) start)
9335                    (search-backward "\n" start t))
9336               (progn
9337                 (subst-char-in-region start (point) ?\n ?\^M)
9338                 (gnus-summary-goto-subject article))
9339             (goto-char start)
9340             nil)))))
9341
9342 (defun gnus-summary-go-to-next-thread (&optional previous)
9343   "Go to the same level (or less) next thread.
9344 If PREVIOUS is non-nil, go to previous thread instead.
9345 Return the article number moved to, or nil if moving was impossible."
9346   (let ((level (gnus-summary-thread-level))
9347         (way (if previous -1 1))
9348         (beg (point)))
9349     (forward-line way)
9350     (while (and (not (eobp))
9351                 (< level (gnus-summary-thread-level)))
9352       (forward-line way))
9353     (if (eobp)
9354         (progn
9355           (goto-char beg)
9356           nil)
9357       (setq beg (point))
9358       (prog1
9359           (gnus-summary-article-number)
9360         (goto-char beg)))))
9361
9362 (defun gnus-summary-next-thread (n &optional silent)
9363   "Go to the same level next N'th thread.
9364 If N is negative, search backward instead.
9365 Returns the difference between N and the number of skips actually
9366 done.
9367
9368 If SILENT, don't output messages."
9369   (interactive "p")
9370   (let ((backward (< n 0))
9371         (n (abs n)))
9372     (while (and (> n 0)
9373                 (gnus-summary-go-to-next-thread backward))
9374       (decf n))
9375     (unless silent
9376       (gnus-summary-position-point))
9377     (when (and (not silent) (/= 0 n))
9378       (gnus-message 7 "No more threads"))
9379     n))
9380
9381 (defun gnus-summary-prev-thread (n)
9382   "Go to the same level previous N'th thread.
9383 Returns the difference between N and the number of skips actually
9384 done."
9385   (interactive "p")
9386   (gnus-summary-next-thread (- n)))
9387
9388 (defun gnus-summary-go-down-thread ()
9389   "Go down one level in the current thread."
9390   (let ((children (gnus-summary-article-children)))
9391     (when children
9392       (gnus-summary-goto-subject (car children)))))
9393
9394 (defun gnus-summary-go-up-thread ()
9395   "Go up one level in the current thread."
9396   (let ((parent (gnus-summary-article-parent)))
9397     (when parent
9398       (gnus-summary-goto-subject parent))))
9399
9400 (defun gnus-summary-down-thread (n)
9401   "Go down thread N steps.
9402 If N is negative, go up instead.
9403 Returns the difference between N and how many steps down that were
9404 taken."
9405   (interactive "p")
9406   (let ((up (< n 0))
9407         (n (abs n)))
9408     (while (and (> n 0)
9409                 (if up (gnus-summary-go-up-thread)
9410                   (gnus-summary-go-down-thread)))
9411       (setq n (1- n)))
9412     (gnus-summary-position-point)
9413     (when (/= 0 n)
9414       (gnus-message 7 "Can't go further"))
9415     n))
9416
9417 (defun gnus-summary-up-thread (n)
9418   "Go up thread N steps.
9419 If N is negative, go up instead.
9420 Returns the difference between N and how many steps down that were
9421 taken."
9422   (interactive "p")
9423   (gnus-summary-down-thread (- n)))
9424
9425 (defun gnus-summary-top-thread ()
9426   "Go to the top of the thread."
9427   (interactive)
9428   (while (gnus-summary-go-up-thread))
9429   (gnus-summary-article-number))
9430
9431 (defun gnus-summary-kill-thread (&optional unmark)
9432   "Mark articles under current thread as read.
9433 If the prefix argument is positive, remove any kinds of marks.
9434 If the prefix argument is negative, tick articles instead."
9435   (interactive "P")
9436   (when unmark
9437     (setq unmark (prefix-numeric-value unmark)))
9438   (let ((articles (gnus-summary-articles-in-thread)))
9439     (save-excursion
9440       ;; Expand the thread.
9441       (gnus-summary-show-thread)
9442       ;; Mark all the articles.
9443       (while articles
9444         (gnus-summary-goto-subject (car articles))
9445         (cond ((null unmark)
9446                (gnus-summary-mark-article-as-read gnus-killed-mark))
9447               ((> unmark 0)
9448                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9449               (t
9450                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9451         (setq articles (cdr articles))))
9452     ;; Hide killed subtrees.
9453     (and (null unmark)
9454          gnus-thread-hide-killed
9455          (gnus-summary-hide-thread))
9456     ;; If marked as read, go to next unread subject.
9457     (when (null unmark)
9458       ;; Go to next unread subject.
9459       (gnus-summary-next-subject 1 t)))
9460   (gnus-set-mode-line 'summary))
9461
9462 ;; Summary sorting commands
9463
9464 (defun gnus-summary-sort-by-number (&optional reverse)
9465   "Sort the summary buffer by article number.
9466 Argument REVERSE means reverse order."
9467   (interactive "P")
9468   (gnus-summary-sort 'number reverse))
9469
9470 (defun gnus-summary-sort-by-author (&optional reverse)
9471   "Sort the summary buffer by author name alphabetically.
9472 If `case-fold-search' is non-nil, case of letters is ignored.
9473 Argument REVERSE means reverse order."
9474   (interactive "P")
9475   (gnus-summary-sort 'author reverse))
9476
9477 (defun gnus-summary-sort-by-subject (&optional reverse)
9478   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9479 If `case-fold-search' is non-nil, case of letters is ignored.
9480 Argument REVERSE means reverse order."
9481   (interactive "P")
9482   (gnus-summary-sort 'subject reverse))
9483
9484 (defun gnus-summary-sort-by-date (&optional reverse)
9485   "Sort the summary buffer by date.
9486 Argument REVERSE means reverse order."
9487   (interactive "P")
9488   (gnus-summary-sort 'date reverse))
9489
9490 (defun gnus-summary-sort-by-score (&optional reverse)
9491   "Sort the summary buffer by score.
9492 Argument REVERSE means reverse order."
9493   (interactive "P")
9494   (gnus-summary-sort 'score reverse))
9495
9496 (defun gnus-summary-sort-by-lines (&optional reverse)
9497   "Sort the summary buffer by the number of lines.
9498 Argument REVERSE means reverse order."
9499   (interactive "P")
9500   (gnus-summary-sort 'lines reverse))
9501
9502 (defun gnus-summary-sort-by-chars (&optional reverse)
9503   "Sort the summary buffer by article length.
9504 Argument REVERSE means reverse order."
9505   (interactive "P")
9506   (gnus-summary-sort 'chars reverse))
9507
9508 (defun gnus-summary-sort (predicate reverse)
9509   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9510   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9511          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9512          (gnus-thread-sort-functions
9513           (if (not reverse)
9514               thread
9515             `(lambda (t1 t2)
9516                (,thread t2 t1))))
9517          (gnus-sort-gathered-threads-function
9518           gnus-thread-sort-functions)
9519          (gnus-article-sort-functions
9520           (if (not reverse)
9521               article
9522             `(lambda (t1 t2)
9523                (,article t2 t1))))
9524          (buffer-read-only)
9525          (gnus-summary-prepare-hook nil))
9526     ;; We do the sorting by regenerating the threads.
9527     (gnus-summary-prepare)
9528     ;; Hide subthreads if needed.
9529     (when (and gnus-show-threads gnus-thread-hide-subtree)
9530       (gnus-summary-hide-all-threads))))
9531
9532 ;; Summary saving commands.
9533
9534 (defun gnus-summary-save-article (&optional n not-saved)
9535   "Save the current article using the default saver function.
9536 If N is a positive number, save the N next articles.
9537 If N is a negative number, save the N previous articles.
9538 If N is nil and any articles have been marked with the process mark,
9539 save those articles instead.
9540 The variable `gnus-default-article-saver' specifies the saver function."
9541   (interactive "P")
9542   (let* ((articles (gnus-summary-work-articles n))
9543          (save-buffer (save-excursion
9544                         (nnheader-set-temp-buffer " *Gnus Save*")))
9545          (num (length articles))
9546          header file)
9547     (dolist (article articles)
9548       (setq header (gnus-summary-article-header article))
9549       (if (not (vectorp header))
9550           ;; This is a pseudo-article.
9551           (if (assq 'name header)
9552               (gnus-copy-file (cdr (assq 'name header)))
9553             (gnus-message 1 "Article %d is unsaveable" article))
9554         ;; This is a real article.
9555         (save-window-excursion
9556           (gnus-summary-select-article t nil nil article))
9557         (save-excursion
9558           (set-buffer save-buffer)
9559           (erase-buffer)
9560           (insert-buffer-substring gnus-original-article-buffer))
9561         (setq file (gnus-article-save save-buffer file num))
9562         (gnus-summary-remove-process-mark article)
9563         (unless not-saved
9564           (gnus-summary-set-saved-mark article))))
9565     (gnus-kill-buffer save-buffer)
9566     (gnus-summary-position-point)
9567     (gnus-set-mode-line 'summary)
9568     n))
9569
9570 (defun gnus-summary-pipe-output (&optional arg)
9571   "Pipe the current article to a subprocess.
9572 If N is a positive number, pipe the N next articles.
9573 If N is a negative number, pipe the N previous articles.
9574 If N is nil and any articles have been marked with the process mark,
9575 pipe those articles instead."
9576   (interactive "P")
9577   (require 'gnus-art)
9578   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9579     (gnus-summary-save-article arg t))
9580   (gnus-configure-windows 'pipe))
9581
9582 (defun gnus-summary-save-article-mail (&optional arg)
9583   "Append the current article to an mail file.
9584 If N is a positive number, save the N next articles.
9585 If N is a negative number, save the N previous articles.
9586 If N is nil and any articles have been marked with the process mark,
9587 save those articles instead."
9588   (interactive "P")
9589   (require 'gnus-art)
9590   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9591     (gnus-summary-save-article arg)))
9592
9593 (defun gnus-summary-save-article-rmail (&optional arg)
9594   "Append the current article to an rmail file.
9595 If N is a positive number, save the N next articles.
9596 If N is a negative number, save the N previous articles.
9597 If N is nil and any articles have been marked with the process mark,
9598 save those articles instead."
9599   (interactive "P")
9600   (require 'gnus-art)
9601   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9602     (gnus-summary-save-article arg)))
9603
9604 (defun gnus-summary-save-article-file (&optional arg)
9605   "Append the current article to a file.
9606 If N is a positive number, save the N next articles.
9607 If N is a negative number, save the N previous articles.
9608 If N is nil and any articles have been marked with the process mark,
9609 save those articles instead."
9610   (interactive "P")
9611   (require 'gnus-art)
9612   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9613     (gnus-summary-save-article arg)))
9614
9615 (defun gnus-summary-write-article-file (&optional arg)
9616   "Write the current article to a file, deleting the previous file.
9617 If N is a positive number, save the N next articles.
9618 If N is a negative number, save the N previous articles.
9619 If N is nil and any articles have been marked with the process mark,
9620 save those articles instead."
9621   (interactive "P")
9622   (require 'gnus-art)
9623   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9624     (gnus-summary-save-article arg)))
9625
9626 (defun gnus-summary-save-article-body-file (&optional arg)
9627   "Append the current article body to a file.
9628 If N is a positive number, save the N next articles.
9629 If N is a negative number, save the N previous articles.
9630 If N is nil and any articles have been marked with the process mark,
9631 save those articles instead."
9632   (interactive "P")
9633   (require 'gnus-art)
9634   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9635     (gnus-summary-save-article arg)))
9636
9637 (defun gnus-summary-pipe-message (program)
9638   "Pipe the current article through PROGRAM."
9639   (interactive "sProgram: ")
9640   (gnus-summary-select-article)
9641   (let ((mail-header-separator ""))
9642     (gnus-eval-in-buffer-window gnus-article-buffer
9643       (save-restriction
9644         (widen)
9645         (let ((start (window-start))
9646               buffer-read-only)
9647           (message-pipe-buffer-body program)
9648           (set-window-start (get-buffer-window (current-buffer)) start))))))
9649
9650 (defun gnus-get-split-value (methods)
9651   "Return a value based on the split METHODS."
9652   (let (split-name method result match)
9653     (when methods
9654       (save-excursion
9655         (set-buffer gnus-original-article-buffer)
9656         (save-restriction
9657           (nnheader-narrow-to-headers)
9658           (while (and methods (not split-name))
9659             (goto-char (point-min))
9660             (setq method (pop methods))
9661             (setq match (car method))
9662             (when (cond
9663                    ((stringp match)
9664                     ;; Regular expression.
9665                     (ignore-errors
9666                       (re-search-forward match nil t)))
9667                    ((gnus-functionp match)
9668                     ;; Function.
9669                     (save-restriction
9670                       (widen)
9671                       (setq result (funcall match gnus-newsgroup-name))))
9672                    ((consp match)
9673                     ;; Form.
9674                     (save-restriction
9675                       (widen)
9676                       (setq result (eval match)))))
9677               (setq split-name (cdr method))
9678               (cond ((stringp result)
9679                      (push (expand-file-name
9680                             result gnus-article-save-directory)
9681                            split-name))
9682                     ((consp result)
9683                      (setq split-name (append result split-name)))))))))
9684     (nreverse split-name)))
9685
9686 (defun gnus-valid-move-group-p (group)
9687   (and (boundp group)
9688        (symbol-name group)
9689        (symbol-value group)
9690        (gnus-get-function (gnus-find-method-for-group
9691                            (symbol-name group)) 'request-accept-article t)))
9692
9693 (defun gnus-read-move-group-name (prompt default articles prefix)
9694   "Read a group name."
9695   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9696          (minibuffer-confirm-incomplete nil) ; XEmacs
9697          (prom
9698           (format "%s %s to:"
9699                   prompt
9700                   (if (> (length articles) 1)
9701                       (format "these %d articles" (length articles))
9702                     "this article")))
9703          (to-newsgroup
9704           (cond
9705            ((null split-name)
9706             (gnus-completing-read default prom
9707                                   gnus-active-hashtb
9708                                   'gnus-valid-move-group-p
9709                                   nil prefix
9710                                   'gnus-group-history))
9711            ((= 1 (length split-name))
9712             (gnus-completing-read (car split-name) prom
9713                                   gnus-active-hashtb
9714                                   'gnus-valid-move-group-p
9715                                   nil nil
9716                                   'gnus-group-history))
9717            (t
9718             (gnus-completing-read nil prom
9719                                   (mapcar (lambda (el) (list el))
9720                                           (nreverse split-name))
9721                                   nil nil nil
9722                                   'gnus-group-history))))
9723          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9724     (when to-newsgroup
9725       (if (or (string= to-newsgroup "")
9726               (string= to-newsgroup prefix))
9727           (setq to-newsgroup default))
9728       (unless to-newsgroup
9729         (error "No group name entered"))
9730       (or (gnus-active to-newsgroup)
9731           (gnus-activate-group to-newsgroup nil nil to-method)
9732           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9733                                      to-newsgroup))
9734               (or (and (gnus-request-create-group to-newsgroup to-method)
9735                        (gnus-activate-group
9736                         to-newsgroup nil nil to-method)
9737                        (gnus-subscribe-group to-newsgroup))
9738                   (error "Couldn't create group %s" to-newsgroup)))
9739           (error "No such group: %s" to-newsgroup)))
9740     to-newsgroup))
9741
9742 (defun gnus-summary-save-parts (type dir n &optional reverse)
9743   "Save parts matching TYPE to DIR.
9744 If REVERSE, save parts that do not match TYPE."
9745   (interactive
9746    (list (read-string "Save parts of type: "
9747                       (or (car gnus-summary-save-parts-type-history)
9748                           gnus-summary-save-parts-default-mime)
9749                       'gnus-summary-save-parts-type-history)
9750          (setq gnus-summary-save-parts-last-directory
9751                (read-file-name "Save to directory: "
9752                                gnus-summary-save-parts-last-directory
9753                                nil t))
9754          current-prefix-arg))
9755   (gnus-summary-iterate n
9756     (let ((gnus-display-mime-function nil)
9757           (gnus-inhibit-treatment t))
9758       (gnus-summary-select-article))
9759     (save-excursion
9760       (set-buffer gnus-article-buffer)
9761       (let ((handles (or gnus-article-mime-handles
9762                          (mm-dissect-buffer) (mm-uu-dissect))))
9763         (when handles
9764           (gnus-summary-save-parts-1 type dir handles reverse)
9765           (unless gnus-article-mime-handles ;; Don't destroy this case.
9766             (mm-destroy-parts handles)))))))
9767
9768 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9769   (if (stringp (car handle))
9770       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9771               (cdr handle))
9772     (when (if reverse
9773               (not (string-match type (mm-handle-media-type handle)))
9774             (string-match type (mm-handle-media-type handle)))
9775       (let ((file (expand-file-name
9776                    (file-name-nondirectory
9777                     (or
9778                      (mail-content-type-get
9779                       (mm-handle-disposition handle) 'filename)
9780                      (concat gnus-newsgroup-name
9781                              "." (number-to-string
9782                                   (cdr gnus-article-current)))))
9783                    dir)))
9784         (unless (file-exists-p file)
9785           (mm-save-part-to-file handle file))))))
9786
9787 ;; Summary extract commands
9788
9789 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9790   (let ((buffer-read-only nil)
9791         (article (gnus-summary-article-number))
9792         after-article b e)
9793     (unless (gnus-summary-goto-subject article)
9794       (error "No such article: %d" article))
9795     (gnus-summary-position-point)
9796     ;; If all commands are to be bunched up on one line, we collect
9797     ;; them here.
9798     (unless gnus-view-pseudos-separately
9799       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9800             files action)
9801         (while ps
9802           (setq action (cdr (assq 'action (car ps))))
9803           (setq files (list (cdr (assq 'name (car ps)))))
9804           (while (and ps (cdr ps)
9805                       (string= (or action "1")
9806                                (or (cdr (assq 'action (cadr ps))) "2")))
9807             (push (cdr (assq 'name (cadr ps))) files)
9808             (setcdr ps (cddr ps)))
9809           (when files
9810             (when (not (string-match "%s" action))
9811               (push " " files))
9812             (push " " files)
9813             (when (assq 'execute (car ps))
9814               (setcdr (assq 'execute (car ps))
9815                       (funcall (if (string-match "%s" action)
9816                                    'format 'concat)
9817                                action
9818                                (mapconcat
9819                                 (lambda (f)
9820                                   (if (equal f " ")
9821                                       f
9822                                     (gnus-quote-arg-for-sh-or-csh f)))
9823                                 files " ")))))
9824           (setq ps (cdr ps)))))
9825     (if (and gnus-view-pseudos (not not-view))
9826         (while pslist
9827           (when (assq 'execute (car pslist))
9828             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9829                                   (eq gnus-view-pseudos 'not-confirm)))
9830           (setq pslist (cdr pslist)))
9831       (save-excursion
9832         (while pslist
9833           (setq after-article (or (cdr (assq 'article (car pslist)))
9834                                   (gnus-summary-article-number)))
9835           (gnus-summary-goto-subject after-article)
9836           (forward-line 1)
9837           (setq b (point))
9838           (insert "    " (file-name-nondirectory
9839                           (cdr (assq 'name (car pslist))))
9840                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9841           (setq e (point))
9842           (forward-line -1)             ; back to `b'
9843           (gnus-add-text-properties
9844            b (1- e) (list 'gnus-number gnus-reffed-article-number
9845                           gnus-mouse-face-prop gnus-mouse-face))
9846           (gnus-data-enter
9847            after-article gnus-reffed-article-number
9848            gnus-unread-mark b (car pslist) 0 (- e b))
9849           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9850           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9851           (setq pslist (cdr pslist)))))))
9852
9853 (defun gnus-pseudos< (p1 p2)
9854   (let ((c1 (cdr (assq 'action p1)))
9855         (c2 (cdr (assq 'action p2))))
9856     (and c1 c2 (string< c1 c2))))
9857
9858 (defun gnus-request-pseudo-article (props)
9859   (cond ((assq 'execute props)
9860          (gnus-execute-command (cdr (assq 'execute props)))))
9861   (let ((gnus-current-article (gnus-summary-article-number)))
9862     (gnus-run-hooks 'gnus-mark-article-hook)))
9863
9864 (defun gnus-execute-command (command &optional automatic)
9865   (save-excursion
9866     (gnus-article-setup-buffer)
9867     (set-buffer gnus-article-buffer)
9868     (setq buffer-read-only nil)
9869     (let ((command (if automatic command
9870                      (read-string "Command: " (cons command 0)))))
9871       (erase-buffer)
9872       (insert "$ " command "\n\n")
9873       (if gnus-view-pseudo-asynchronously
9874           (start-process "gnus-execute" (current-buffer) shell-file-name
9875                          shell-command-switch command)
9876         (call-process shell-file-name nil t nil
9877                       shell-command-switch command)))))
9878
9879 ;; Summary kill commands.
9880
9881 (defun gnus-summary-edit-global-kill (article)
9882   "Edit the \"global\" kill file."
9883   (interactive (list (gnus-summary-article-number)))
9884   (gnus-group-edit-global-kill article))
9885
9886 (defun gnus-summary-edit-local-kill ()
9887   "Edit a local kill file applied to the current newsgroup."
9888   (interactive)
9889   (setq gnus-current-headers (gnus-summary-article-header))
9890   (gnus-group-edit-local-kill
9891    (gnus-summary-article-number) gnus-newsgroup-name))
9892
9893 ;;; Header reading.
9894
9895 (defun gnus-read-header (id &optional header)
9896   "Read the headers of article ID and enter them into the Gnus system."
9897   (let ((group gnus-newsgroup-name)
9898         (gnus-override-method
9899          (or
9900           gnus-override-method
9901           (and (gnus-news-group-p gnus-newsgroup-name)
9902                (car (gnus-refer-article-methods)))))
9903         where)
9904     ;; First we check to see whether the header in question is already
9905     ;; fetched.
9906     (if (stringp id)
9907         ;; This is a Message-ID.
9908         (setq header (or header (gnus-id-to-header id)))
9909       ;; This is an article number.
9910       (setq header (or header (gnus-summary-article-header id))))
9911     (if (and header
9912              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9913         ;; We have found the header.
9914         header
9915       ;; If this is a sparse article, we have to nix out its
9916       ;; previous entry in the thread hashtb.
9917       (when (and header
9918                  (gnus-summary-article-sparse-p (mail-header-number header)))
9919         (let* ((parent (gnus-parent-id (mail-header-references header)))
9920                (thread (and parent (gnus-id-to-thread parent))))
9921           (when thread
9922             (delq (assq header thread) thread))))
9923       ;; We have to really fetch the header to this article.
9924       (save-excursion
9925         (set-buffer nntp-server-buffer)
9926         (when (setq where (gnus-request-head id group))
9927           (nnheader-fold-continuation-lines)
9928           (goto-char (point-max))
9929           (insert ".\n")
9930           (goto-char (point-min))
9931           (insert "211 ")
9932           (princ (cond
9933                   ((numberp id) id)
9934                   ((cdr where) (cdr where))
9935                   (header (mail-header-number header))
9936                   (t gnus-reffed-article-number))
9937                  (current-buffer))
9938           (insert " Article retrieved.\n"))
9939         (if (or (not where)
9940                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9941             ()                          ; Malformed head.
9942           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9943             (when (and (stringp id)
9944                        (not (string= (gnus-group-real-name group)
9945                                      (car where))))
9946               ;; If we fetched by Message-ID and the article came
9947               ;; from a different group, we fudge some bogus article
9948               ;; numbers for this article.
9949               (mail-header-set-number header gnus-reffed-article-number))
9950             (save-excursion
9951               (set-buffer gnus-summary-buffer)
9952               (decf gnus-reffed-article-number)
9953               (gnus-remove-header (mail-header-number header))
9954               (push header gnus-newsgroup-headers)
9955               (setq gnus-current-headers header)
9956               (push (mail-header-number header) gnus-newsgroup-limit)))
9957           header)))))
9958
9959 (defun gnus-remove-header (number)
9960   "Remove header NUMBER from `gnus-newsgroup-headers'."
9961   (if (and gnus-newsgroup-headers
9962            (= number (mail-header-number (car gnus-newsgroup-headers))))
9963       (pop gnus-newsgroup-headers)
9964     (let ((headers gnus-newsgroup-headers))
9965       (while (and (cdr headers)
9966                   (not (= number (mail-header-number (cadr headers)))))
9967         (pop headers))
9968       (when (cdr headers)
9969         (setcdr headers (cddr headers))))))
9970
9971 ;;;
9972 ;;; summary highlights
9973 ;;;
9974
9975 (defun gnus-highlight-selected-summary ()
9976   "Highlight selected article in summary buffer."
9977   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9978   (when gnus-summary-selected-face
9979     (save-excursion
9980       (let* ((beg (progn (beginning-of-line) (point)))
9981              (end (progn (end-of-line) (point)))
9982              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9983              (from (if (get-text-property beg gnus-mouse-face-prop)
9984                        beg
9985                      (or (next-single-property-change
9986                           beg gnus-mouse-face-prop nil end)
9987                          beg)))
9988              (to
9989               (if (= from end)
9990                   (- from 2)
9991                 (or (next-single-property-change
9992                      from gnus-mouse-face-prop nil end)
9993                     end))))
9994         ;; If no mouse-face prop on line we will have to = from = end,
9995         ;; so we highlight the entire line instead.
9996         (when (= (+ to 2) from)
9997           (setq from beg)
9998           (setq to end))
9999         (if gnus-newsgroup-selected-overlay
10000             ;; Move old overlay.
10001             (gnus-move-overlay
10002              gnus-newsgroup-selected-overlay from to (current-buffer))
10003           ;; Create new overlay.
10004           (gnus-overlay-put
10005            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10006            'face gnus-summary-selected-face))))))
10007
10008 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10009 (defun gnus-summary-highlight-line ()
10010   "Highlight current line according to `gnus-summary-highlight'."
10011   (let* ((list gnus-summary-highlight)
10012          (p (point))
10013          (end (progn (end-of-line) (point)))
10014          ;; now find out where the line starts and leave point there.
10015          (beg (progn (beginning-of-line) (point)))
10016          (article (gnus-summary-article-number))
10017          (score (or (cdr (assq (or article gnus-current-article)
10018                                gnus-newsgroup-scored))
10019                     gnus-summary-default-score 0))
10020          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10021          (inhibit-read-only t))
10022     ;; Eval the cars of the lists until we find a match.
10023     (let ((default gnus-summary-default-score))
10024       (while (and list
10025                   (not (eval (caar list))))
10026         (setq list (cdr list))))
10027     (let ((face (cdar list)))
10028       (unless (eq face (get-text-property beg 'face))
10029         (gnus-put-text-property-excluding-characters-with-faces
10030          beg end 'face
10031          (setq face (if (boundp face) (symbol-value face) face)))
10032         (when gnus-summary-highlight-line-function
10033           (funcall gnus-summary-highlight-line-function article face))))
10034     (goto-char p)))
10035
10036 (defun gnus-update-read-articles (group unread &optional compute)
10037   "Update the list of read articles in GROUP."
10038   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10039          (entry (gnus-gethash group gnus-newsrc-hashtb))
10040          (info (nth 2 entry))
10041          (prev 1)
10042          (unread (sort (copy-sequence unread) '<))
10043          read)
10044     (if (or (not info) (not active))
10045         ;; There is no info on this group if it was, in fact,
10046         ;; killed.  Gnus stores no information on killed groups, so
10047         ;; there's nothing to be done.
10048         ;; One could store the information somewhere temporarily,
10049         ;; perhaps...  Hmmm...
10050         ()
10051       ;; Remove any negative articles numbers.
10052       (while (and unread (< (car unread) 0))
10053         (setq unread (cdr unread)))
10054       ;; Remove any expired article numbers
10055       (while (and unread (< (car unread) (car active)))
10056         (setq unread (cdr unread)))
10057       ;; Compute the ranges of read articles by looking at the list of
10058       ;; unread articles.
10059       (while unread
10060         (when (/= (car unread) prev)
10061           (push (if (= prev (1- (car unread))) prev
10062                   (cons prev (1- (car unread))))
10063                 read))
10064         (setq prev (1+ (car unread)))
10065         (setq unread (cdr unread)))
10066       (when (<= prev (cdr active))
10067         (push (cons prev (cdr active)) read))
10068       (setq read (if (> (length read) 1) (nreverse read) read))
10069       (if compute
10070           read
10071         (save-excursion
10072           (let (setmarkundo)
10073             ;; Propagate the read marks to the backend.
10074             (when (gnus-check-backend-function 'request-set-mark group)
10075               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10076                     (add (gnus-remove-from-range read (gnus-info-read info))))
10077                 (when (or add del)
10078                   (unless (gnus-check-group group)
10079                     (error "Can't open server for %s" group))
10080                   (gnus-request-set-mark
10081                    group (delq nil (list (if add (list add 'add '(read)))
10082                                          (if del (list del 'del '(read))))))
10083                   (setq setmarkundo
10084                         `(gnus-request-set-mark
10085                           ,group
10086                           ',(delq nil (list
10087                                        (if del (list del 'add '(read)))
10088                                        (if add (list add 'del '(read))))))))))
10089             (set-buffer gnus-group-buffer)
10090             (gnus-undo-register
10091               `(progn
10092                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10093                  (gnus-info-set-read ',info ',(gnus-info-read info))
10094                  (gnus-get-unread-articles-in-group ',info
10095                                                     (gnus-active ,group))
10096                  (gnus-group-update-group ,group t)
10097                  ,setmarkundo))))
10098         ;; Enter this list into the group info.
10099         (gnus-info-set-read info read)
10100         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10101         (gnus-get-unread-articles-in-group info (gnus-active group))
10102         t))))
10103
10104 (defun gnus-offer-save-summaries ()
10105   "Offer to save all active summary buffers."
10106   (save-excursion
10107     (let ((buflist (buffer-list))
10108           buffers bufname)
10109       ;; Go through all buffers and find all summaries.
10110       (while buflist
10111         (and (setq bufname (buffer-name (car buflist)))
10112              (string-match "Summary" bufname)
10113              (save-excursion
10114                (set-buffer bufname)
10115                ;; We check that this is, indeed, a summary buffer.
10116                (and (eq major-mode 'gnus-summary-mode)
10117                     ;; Also make sure this isn't bogus.
10118                     gnus-newsgroup-prepared
10119                     ;; Also make sure that this isn't a dead summary buffer.
10120                     (not gnus-dead-summary-mode)))
10121              (push bufname buffers))
10122         (setq buflist (cdr buflist)))
10123       ;; Go through all these summary buffers and offer to save them.
10124       (when buffers
10125         (map-y-or-n-p
10126          "Update summary buffer %s? "
10127          (lambda (buf)
10128            (switch-to-buffer buf)
10129            (gnus-summary-exit))
10130          buffers)))))
10131
10132
10133 ;;; @ for mime-partial
10134 ;;;
10135
10136 (defun gnus-request-partial-message ()
10137   (save-excursion
10138     (let ((number (gnus-summary-article-number))
10139           (group gnus-newsgroup-name)
10140           (mother gnus-article-buffer))
10141       (set-buffer (get-buffer-create " *Partial Article*"))
10142       (erase-buffer)
10143       (setq mime-preview-buffer mother)
10144       (gnus-request-article-this-buffer number group)
10145       (mime-parse-buffer)
10146       )))
10147
10148 (autoload 'mime-combine-message/partial-pieces-automatically
10149   "mime-partial"
10150   "Internal method to combine message/partial messages automatically.")
10151
10152 (mime-add-condition
10153  'action '((type . message)(subtype . partial)
10154            (major-mode . gnus-original-article-mode)
10155            (method . mime-combine-message/partial-pieces-automatically)
10156            (summary-buffer-exp . gnus-summary-buffer)
10157            (request-partial-message-method . gnus-request-partial-message)
10158            ))
10159
10160
10161 ;;; @ for message/rfc822
10162 ;;;
10163
10164 (defun gnus-mime-extract-message/rfc822 (entity situation)
10165   (let (group article num cwin swin cur)
10166     (with-temp-buffer
10167       (mime-insert-entity-content entity)
10168       (setq group (or (cdr (assq 'group situation))
10169                       (completing-read "Group: "
10170                                        gnus-active-hashtb
10171                                        nil
10172                                        (gnus-read-active-file-p)
10173                                        gnus-newsgroup-name))
10174             article (gnus-request-accept-article group)))
10175     (when (and (consp article)
10176                (numberp (setq article (cdr article))))
10177       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10178             cwin (get-buffer-window (current-buffer) t))
10179       (save-window-excursion
10180         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10181             (select-window swin)
10182           (set-buffer gnus-summary-buffer))
10183         (setq cur gnus-current-article)
10184         (forward-line num)
10185         (let (gnus-show-threads)
10186           (gnus-summary-goto-subject article t))
10187         (gnus-summary-clear-mark-forward 1)
10188         (gnus-summary-goto-subject cur))
10189       (when (and cwin (window-frame cwin))
10190         (select-frame (window-frame cwin)))
10191       (when (boundp 'mime-acting-situation-to-override)
10192         (set-alist 'mime-acting-situation-to-override
10193                    'group
10194                    group)
10195         (set-alist 'mime-acting-situation-to-override
10196                    'after-method
10197                    `(progn
10198                       (save-current-buffer
10199                         (set-buffer gnus-group-buffer)
10200                         (gnus-activate-group ,group))
10201                       (gnus-summary-goto-article ,cur
10202                                                  gnus-show-all-headers)))
10203         (set-alist 'mime-acting-situation-to-override
10204                    'number num)))))
10205
10206 (mime-add-condition
10207  'action '((type . message)(subtype . rfc822)
10208            (major-mode . gnus-original-article-mode)
10209            (method . gnus-mime-extract-message/rfc822)
10210            (mode . "extract")
10211            ))
10212
10213 (mime-add-condition
10214  'action '((type . message)(subtype . news)
10215            (major-mode . gnus-original-article-mode)
10216            (method . gnus-mime-extract-message/rfc822)
10217            (mode . "extract")
10218            ))
10219
10220 (defun gnus-mime-extract-multipart (entity situation)
10221   (let ((children (mime-entity-children entity))
10222         mime-acting-situation-to-override
10223         f)
10224     (while children
10225       (mime-play-entity (car children)
10226                         (cons (assq 'mode situation)
10227                               mime-acting-situation-to-override))
10228       (setq children (cdr children)))
10229     (if (setq f (cdr (assq 'after-method
10230                            mime-acting-situation-to-override)))
10231         (eval f)
10232       )))
10233
10234 (mime-add-condition
10235  'action '((type . multipart)
10236            (method . gnus-mime-extract-multipart)
10237            (mode . "extract")
10238            )
10239  'with-default)
10240
10241
10242 ;;; @ end
10243 ;;;
10244
10245 (defun gnus-summary-setup-default-charset ()
10246   "Setup newsgroup default charset."
10247   (if (equal gnus-newsgroup-name "nndraft:drafts")
10248       (setq gnus-newsgroup-charset nil)
10249     (let* ((ignored-charsets
10250             (or gnus-newsgroup-ephemeral-ignored-charsets
10251                 (append
10252                  (and gnus-newsgroup-name
10253                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10254                  gnus-newsgroup-ignored-charsets))))
10255       (setq gnus-newsgroup-charset
10256             (or gnus-newsgroup-ephemeral-charset
10257                 (and gnus-newsgroup-name
10258                      (gnus-parameter-charset gnus-newsgroup-name))
10259                 gnus-default-charset))
10260       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10261            ignored-charsets))))
10262
10263 ;;;
10264 ;;; Mime Commands
10265 ;;;
10266
10267 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10268   "Display the current article buffer fully MIME-buttonized.
10269 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10270 treated as multipart/mixed."
10271   (interactive "P")
10272   (require 'gnus-art)
10273   (let ((gnus-unbuttonized-mime-types nil)
10274         (gnus-mime-display-multipart-as-mixed show-all-parts))
10275     (gnus-summary-show-article)))
10276
10277 (defun gnus-summary-repair-multipart (article)
10278   "Add a Content-Type header to a multipart article without one."
10279   (interactive (list (gnus-summary-article-number)))
10280   (gnus-with-article article
10281     (message-narrow-to-head)
10282     (message-remove-header "Mime-Version")
10283     (goto-char (point-max))
10284     (insert "Mime-Version: 1.0\n")
10285     (widen)
10286     (when (search-forward "\n--" nil t)
10287       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10288         (message-narrow-to-head)
10289         (message-remove-header "Content-Type")
10290         (goto-char (point-max))
10291         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10292                         separator))
10293         (widen))))
10294   (let (gnus-mark-article-hook)
10295     (gnus-summary-select-article t t nil article)))
10296
10297 (defun gnus-summary-toggle-display-buttonized ()
10298   "Toggle the buttonizing of the article buffer."
10299   (interactive)
10300   (require 'gnus-art)
10301   (if (setq gnus-inhibit-mime-unbuttonizing
10302             (not gnus-inhibit-mime-unbuttonizing))
10303       (let ((gnus-unbuttonized-mime-types nil))
10304         (gnus-summary-show-article))
10305     (gnus-summary-show-article)))
10306
10307 ;;;
10308 ;;; Intelli-mouse commmands
10309 ;;;
10310
10311 (defun gnus-wheel-summary-scroll (event)
10312   (interactive "e")
10313   (let ((amount (if (memq 'shift (event-modifiers event))
10314                     (car gnus-wheel-scroll-amount)
10315                   (cdr gnus-wheel-scroll-amount)))
10316         (direction (- (* (static-if (featurep 'xemacs)
10317                              (event-button event)
10318                            (cond ((eq 'mouse-4 (event-basic-type event))
10319                                   4)
10320                                  ((eq 'mouse-5 (event-basic-type event))
10321                                   5)))
10322                          2) 9))
10323         edge)
10324     (gnus-summary-scroll-up (* amount direction))
10325     (when (gnus-eval-in-buffer-window gnus-article-buffer
10326             (save-restriction
10327               (widen)
10328               (and (if (< 0 direction)
10329                        (gnus-article-next-page 0)
10330                      (gnus-article-prev-page 0)
10331                      (bobp))
10332                    (if (setq edge (get-text-property
10333                                    (point-min) 'gnus-wheel-edge))
10334                        (setq edge (* edge direction))
10335                      (setq edge -1))
10336                    (or (plusp edge)
10337                        (let ((buffer-read-only nil)
10338                              (inhibit-read-only t))
10339                          (put-text-property (point-min) (point-max)
10340                                             'gnus-wheel-edge direction)
10341                          nil))
10342                    (or (> edge gnus-wheel-edge-resistance)
10343                        (let ((buffer-read-only nil)
10344                              (inhibit-read-only t))
10345                          (put-text-property (point-min) (point-max)
10346                                             'gnus-wheel-edge
10347                                             (* (1+ edge) direction))
10348                          nil))
10349                    (eq last-command 'gnus-wheel-summary-scroll))))
10350       (gnus-summary-next-article nil nil (minusp direction)))))
10351
10352 (defun gnus-wheel-install ()
10353   "Enable mouse wheel support on summary window."
10354   (when gnus-use-wheel
10355     (let ((keys
10356            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10357       (dolist (key keys)
10358         (define-key gnus-summary-mode-map key
10359           'gnus-wheel-summary-scroll)))))
10360
10361 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10362
10363 ;;;
10364 ;;; Traditional PGP commmands
10365 ;;;
10366
10367 (defun gnus-summary-decrypt-article (&optional force)
10368   "Decrypt the current article in traditional PGP way.
10369 This will have permanent effect only in mail groups.
10370 If FORCE is non-nil, allow editing of articles even in read-only
10371 groups."
10372   (interactive "P")
10373   (gnus-summary-select-article t)
10374   (gnus-eval-in-buffer-window gnus-article-buffer
10375     (save-excursion
10376       (save-restriction
10377         (widen)
10378         (goto-char (point-min))
10379         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10380           (error "Not a traditional PGP message!"))
10381         (let ((armor-start (match-beginning 0)))
10382           (if (and (pgg-decrypt-region armor-start (point-max))
10383                    (or force (not (gnus-group-read-only-p))))
10384               (let ((inhibit-read-only t)
10385                     buffer-read-only)
10386                 (delete-region armor-start
10387                                (progn
10388                                  (re-search-forward "^-+END PGP" nil t)
10389                                  (beginning-of-line 2)
10390                                  (point)))
10391                 (insert-buffer-substring pgg-output-buffer))))))))
10392
10393 (defun gnus-summary-verify-article ()
10394   "Verify the current article in traditional PGP way."
10395   (interactive)
10396   (save-excursion
10397     (set-buffer gnus-original-article-buffer)
10398     (goto-char (point-min))
10399     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10400       (error "Not a traditional PGP message!"))
10401     (re-search-forward "^-+END PGP" nil t)
10402     (beginning-of-line 2)
10403     (call-interactively (function pgg-verify-region))))
10404
10405 ;;;
10406 ;;; Generic summary marking commands
10407 ;;;
10408
10409 (defvar gnus-summary-marking-alist
10410   '((read gnus-del-mark "d")
10411     (unread gnus-unread-mark "u")
10412     (ticked gnus-ticked-mark "!")
10413     (dormant gnus-dormant-mark "?")
10414     (expirable gnus-expirable-mark "e"))
10415   "An alist of names/marks/keystrokes.")
10416
10417 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10418 (defvar gnus-summary-mark-map)
10419
10420 (defun gnus-summary-make-all-marking-commands ()
10421   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10422   (dolist (elem gnus-summary-marking-alist)
10423     (apply 'gnus-summary-make-marking-command elem)))
10424
10425 (defun gnus-summary-make-marking-command (name mark keystroke)
10426   (let ((map (make-sparse-keymap)))
10427     (define-key gnus-summary-generic-mark-map keystroke map)
10428     (dolist (lway `((next "next" next nil "n")
10429                     (next-unread "next unread" next t "N")
10430                     (prev "previous" prev nil "p")
10431                     (prev-unread "previous unread" prev t "P")
10432                     (nomove "" nil nil ,keystroke)))
10433       (let ((func (gnus-summary-make-marking-command-1
10434                    mark (car lway) lway name)))
10435         (setq func (eval func))
10436         (define-key map (nth 4 lway) func)))))
10437
10438 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10439   `(defun ,(intern
10440             (format "gnus-summary-put-mark-as-%s%s"
10441                     name (if (eq way 'nomove)
10442                              ""
10443                            (concat "-" (symbol-name way)))))
10444      (n)
10445      ,(format
10446        "Mark the current article as %s%s.
10447 If N, the prefix, then repeat N times.
10448 If N is negative, move in reverse order.
10449 The difference between N and the actual number of articles marked is
10450 returned."
10451        name (car (cdr lway)))
10452      (interactive "p")
10453      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10454
10455 (defun gnus-summary-generic-mark (n mark move unread)
10456   "Mark N articles with MARK."
10457   (unless (eq major-mode 'gnus-summary-mode)
10458     (error "This command can only be used in the summary buffer"))
10459   (gnus-summary-show-thread)
10460   (let ((nummove
10461          (cond
10462           ((eq move 'next) 1)
10463           ((eq move 'prev) -1)
10464           (t 0))))
10465     (if (zerop nummove)
10466         (setq n 1)
10467       (when (< n 0)
10468         (setq n (abs n)
10469               nummove (* -1 nummove))))
10470     (while (and (> n 0)
10471                 (gnus-summary-mark-article nil mark)
10472                 (zerop (gnus-summary-next-subject nummove unread t)))
10473       (setq n (1- n)))
10474     (when (/= 0 n)
10475       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10476     (gnus-summary-recenter)
10477     (gnus-summary-position-point)
10478     (gnus-set-mode-line 'summary)
10479     n))
10480
10481 (defun gnus-summary-insert-articles (articles)
10482   (when (setq articles
10483               (gnus-set-difference articles
10484                                    (mapcar (lambda (h) (mail-header-number h))
10485                                            gnus-newsgroup-headers)))
10486     (setq gnus-newsgroup-headers 
10487           (merge 'list
10488                  gnus-newsgroup-headers
10489                  (gnus-fetch-headers articles)
10490                  'gnus-article-sort-by-number))
10491     ;; Suppress duplicates?
10492     (when gnus-suppress-duplicates
10493       (gnus-dup-suppress-articles))
10494     
10495     ;; We might want to build some more threads first.
10496     (when (and gnus-fetch-old-headers
10497                (eq gnus-headers-retrieved-by 'nov))
10498       (if (eq gnus-fetch-old-headers 'invisible)
10499         (gnus-build-all-threads)
10500         (gnus-build-old-threads)))
10501     ;; Let the Gnus agent mark articles as read.
10502     (when gnus-agent
10503       (gnus-agent-get-undownloaded-list))
10504     ;; Remove list identifiers from subject
10505     (when gnus-list-identifiers
10506       (gnus-summary-remove-list-identifiers))
10507     ;; First and last article in this newsgroup.
10508     (when gnus-newsgroup-headers
10509       (setq gnus-newsgroup-begin
10510             (mail-header-number (car gnus-newsgroup-headers))
10511             gnus-newsgroup-end
10512             (mail-header-number
10513              (gnus-last-element gnus-newsgroup-headers))))
10514     (when gnus-use-scoring
10515       (gnus-possibly-score-headers))))
10516
10517 (defun gnus-summary-insert-old-articles (&optional all)
10518   "Insert all old articles in this group.
10519 If ALL is non-nil, already read articles become readable.
10520 If ALL is a number, fetch this number of articles."
10521   (interactive "P")
10522   (prog1
10523       (let ((old (mapcar 'car gnus-newsgroup-data))
10524             (i (car gnus-newsgroup-active))
10525             older len)
10526         (while (<= i (cdr gnus-newsgroup-active))
10527           (or (memq i old) (push i older))
10528           (incf i))
10529         (setq len (length older))
10530         (cond 
10531          ((null older) nil)
10532          ((numberp all) 
10533           (if (< all len)
10534               (setq older (subseq older 0 all))))
10535          (all nil)
10536          (t
10537           (if (and (numberp gnus-large-newsgroup)
10538                    (> len gnus-large-newsgroup))
10539               (let ((input
10540                      (read-string
10541                       (format
10542                        "How many articles from %s (default %d): "
10543                        (gnus-limit-string 
10544                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10545                        len))))
10546                 (unless (string-match "^[ \t]*$" input) 
10547                   (setq all (string-to-number input))
10548                   (if (< all len)
10549                       (setq older (subseq older 0 all))))))))
10550         (if (not older)
10551             (message "No old news.")
10552           (gnus-summary-insert-articles older)
10553           (gnus-summary-limit (gnus-union older old))))
10554     (gnus-summary-position-point)))
10555
10556 (defun gnus-summary-insert-new-articles ()
10557   "Insert all new articles in this group."
10558   (interactive)
10559   (prog1
10560       (let ((old (mapcar 'car gnus-newsgroup-data))
10561             (old-active gnus-newsgroup-active)
10562             (nnmail-fetched-sources (list t))
10563             i new)
10564         (setq gnus-newsgroup-active 
10565               (gnus-activate-group gnus-newsgroup-name 'scan))
10566         (setq i (1+ (cdr old-active)))
10567         (while (<= i (cdr gnus-newsgroup-active))
10568           (push i new)
10569           (incf i))
10570         (if (not new)
10571             (message "No gnus is bad news.")
10572           (gnus-summary-insert-articles new)
10573           (setq gnus-newsgroup-unreads
10574                 (append gnus-newsgroup-unreads new))
10575           (gnus-summary-limit (gnus-union old new))))
10576     (gnus-summary-position-point)))
10577
10578 (gnus-summary-make-all-marking-commands)
10579
10580 (gnus-ems-redefine)
10581
10582 (provide 'gnus-sum)
10583
10584 (run-hooks 'gnus-sum-load-hook)
10585
10586 ;;; gnus-sum.el ends here