504b09befab74e2856ce1ba01b76f92db9aaac0a
[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 souped 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-forwarded-mark ?O
453   "*Mark used for articles that have been forwarded."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-cached-mark ?*
458   "*Mark used for articles that are in the cache."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-saved-mark ?S
463   "*Mark used for articles that have been saved to."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-no-mark ?  ;Whitespace
468   "*Mark used for articles that have no other secondary mark."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-ancient-mark ?O
473   "*Mark used for ancient articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-sparse-mark ?Q
478   "*Mark used for sparsely reffed articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-canceled-mark ?G
483   "*Mark used for canceled articles."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-duplicate-mark ?M
488   "*Mark used for duplicate articles."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-undownloaded-mark ?@
493   "*Mark used for articles that weren't downloaded."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-downloadable-mark ?%
498   "*Mark used for articles that are to be downloaded."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-unsendable-mark ?=
503   "*Mark used for articles that won't be sent."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-score-over-mark ?+
508   "*Score mark used for articles with high scores."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-score-below-mark ?-
513   "*Score mark used for articles with low scores."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-empty-thread-mark ?  ;Whitespace
518   "*There is no thread under the article."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-not-empty-thread-mark ?=
523   "*There is a thread under the article."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-view-pseudo-asynchronously nil
528   "*If non-nil, Gnus will view pseudo-articles asynchronously."
529   :group 'gnus-extract-view
530   :type 'boolean)
531
532 (defcustom gnus-auto-expirable-marks
533   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
534         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
535         gnus-souped-mark gnus-duplicate-mark)
536   "*The list of marks converted into expiration if a group is auto-expirable."
537   :version "21.1"
538   :group 'gnus-summary
539   :type '(repeat character))
540
541 (defcustom gnus-inhibit-user-auto-expire t
542   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
543   :version "21.1"
544   :group 'gnus-summary
545   :type 'boolean)
546
547 (defcustom gnus-view-pseudos nil
548   "*If `automatic', pseudo-articles will be viewed automatically.
549 If `not-confirm', pseudos will be viewed automatically, and the user
550 will not be asked to confirm the command."
551   :group 'gnus-extract-view
552   :type '(choice (const :tag "off" nil)
553                  (const automatic)
554                  (const not-confirm)))
555
556 (defcustom gnus-view-pseudos-separately t
557   "*If non-nil, one pseudo-article will be created for each file to be viewed.
558 If nil, all files that use the same viewing command will be given as a
559 list of parameters to that command."
560   :group 'gnus-extract-view
561   :type 'boolean)
562
563 (defcustom gnus-insert-pseudo-articles t
564   "*If non-nil, insert pseudo-articles when decoding articles."
565   :group 'gnus-extract-view
566   :type 'boolean)
567
568 (defcustom gnus-summary-dummy-line-format
569   "  %(:                          :%) %S\n"
570   "*The format specification for the dummy roots in the summary buffer.
571 It works along the same lines as a normal formatting string,
572 with some simple extensions.
573
574 %S  The subject"
575   :group 'gnus-threading
576   :type 'string)
577
578 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
579   "*The format specification for the summary mode line.
580 It works along the same lines as a normal formatting string,
581 with some simple extensions:
582
583 %G  Group name
584 %p  Unprefixed group name
585 %A  Current article number
586 %z  Current article score
587 %V  Gnus version
588 %U  Number of unread articles in the group
589 %e  Number of unselected articles in the group
590 %Z  A string with unread/unselected article counts
591 %g  Shortish group name
592 %S  Subject of the current article
593 %u  User-defined spec
594 %s  Current score file name
595 %d  Number of dormant articles
596 %r  Number of articles that have been marked as read in this session
597 %E  Number of articles expunged by the score files"
598   :group 'gnus-summary-format
599   :type 'string)
600
601 (defcustom gnus-list-identifiers nil
602   "Regexp that matches list identifiers to be removed from subject.
603 This can also be a list of regexps."
604   :version "21.1"
605   :group 'gnus-summary-format
606   :group 'gnus-article-hiding
607   :type '(choice (const :tag "none" nil)
608                  (regexp :value ".*")
609                  (repeat :value (".*") regexp)))
610
611 (defcustom gnus-summary-mark-below 0
612   "*Mark all articles with a score below this variable as read.
613 This variable is local to each summary buffer and usually set by the
614 score file."
615   :group 'gnus-score-default
616   :type 'integer)
617
618 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
619   "*List of functions used for sorting articles in the summary buffer.
620
621 Each function takes two articles and returns non-nil if the first
622 article should be sorted before the other.  If you use more than one
623 function, the primary sort function should be the last.  You should
624 probably always include `gnus-article-sort-by-number' in the list of
625 sorting functions -- preferably first.  Also note that sorting by date
626 is often much slower than sorting by number, and the sorting order is
627 very similar.  (Sorting by date means sorting by the time the message
628 was sent, sorting by number means sorting by arrival time.)
629
630 Ready-made functions include `gnus-article-sort-by-number',
631 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
632 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
633
634 When threading is turned on, the variable `gnus-thread-sort-functions'
635 controls how articles are sorted."
636   :group 'gnus-summary-sort
637   :type '(repeat (choice (function-item gnus-article-sort-by-number)
638                          (function-item gnus-article-sort-by-author)
639                          (function-item gnus-article-sort-by-subject)
640                          (function-item gnus-article-sort-by-date)
641                          (function-item gnus-article-sort-by-score)
642                          (function :tag "other"))))
643
644 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
645   "*List of functions used for sorting threads in the summary buffer.
646 By default, threads are sorted by article number.
647
648 Each function takes two threads and returns non-nil if the first
649 thread should be sorted before the other.  If you use more than one
650 function, the primary sort function should be the last.  You should
651 probably always include `gnus-thread-sort-by-number' in the list of
652 sorting functions -- preferably first.  Also note that sorting by date
653 is often much slower than sorting by number, and the sorting order is
654 very similar.  (Sorting by date means sorting by the time the message
655 was sent, sorting by number means sorting by arrival time.)
656
657 Ready-made functions include `gnus-thread-sort-by-number',
658 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
659 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
660 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
661
662 When threading is turned off, the variable
663 `gnus-article-sort-functions' controls how articles are sorted."
664   :group 'gnus-summary-sort
665   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
666                          (function-item gnus-thread-sort-by-author)
667                          (function-item gnus-thread-sort-by-subject)
668                          (function-item gnus-thread-sort-by-date)
669                          (function-item gnus-thread-sort-by-score)
670                          (function-item gnus-thread-sort-by-total-score)
671                          (function :tag "other"))))
672
673 (defcustom gnus-thread-score-function '+
674   "*Function used for calculating the total score of a thread.
675
676 The function is called with the scores of the article and each
677 subthread and should then return the score of the thread.
678
679 Some functions you can use are `+', `max', or `min'."
680   :group 'gnus-summary-sort
681   :type 'function)
682
683 (defcustom gnus-summary-expunge-below nil
684   "All articles that have a score less than this variable will be expunged.
685 This variable is local to the summary buffers."
686   :group 'gnus-score-default
687   :type '(choice (const :tag "off" nil)
688                  integer))
689
690 (defcustom gnus-thread-expunge-below nil
691   "All threads that have a total score less than this variable will be expunged.
692 See `gnus-thread-score-function' for en explanation of what a
693 \"thread score\" is.
694
695 This variable is local to the summary buffers."
696   :group 'gnus-threading
697   :group 'gnus-score-default
698   :type '(choice (const :tag "off" nil)
699                  integer))
700
701 (defcustom gnus-summary-mode-hook nil
702   "*A hook for Gnus summary mode.
703 This hook is run before any variables are set in the summary buffer."
704   :options '(turn-on-gnus-mailing-list-mode)
705   :group 'gnus-summary-various
706   :type 'hook)
707
708 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
709 (when (featurep 'xemacs)
710   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
711   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
712   (add-hook 'gnus-summary-mode-hook
713             'gnus-xmas-switch-horizontal-scrollbar-off))
714
715 (defcustom gnus-summary-menu-hook nil
716   "*Hook run after the creation of the summary mode menu."
717   :group 'gnus-summary-visual
718   :type 'hook)
719
720 (defcustom gnus-summary-exit-hook nil
721   "*A hook called on exit from the summary buffer.
722 It will be called with point in the group buffer."
723   :group 'gnus-summary-exit
724   :type 'hook)
725
726 (defcustom gnus-summary-prepare-hook nil
727   "*A hook called after the summary buffer has been generated.
728 If you want to modify the summary buffer, you can use this hook."
729   :group 'gnus-summary-various
730   :type 'hook)
731
732 (defcustom gnus-summary-prepared-hook nil
733   "*A hook called as the last thing after the summary buffer has been generated."
734   :group 'gnus-summary-various
735   :type 'hook)
736
737 (defcustom gnus-summary-generate-hook nil
738   "*A hook run just before generating the summary buffer.
739 This hook is commonly used to customize threading variables and the
740 like."
741   :group 'gnus-summary-various
742   :type 'hook)
743
744 (defcustom gnus-select-group-hook nil
745   "*A hook called when a newsgroup is selected.
746
747 If you'd like to simplify subjects like the
748 `gnus-summary-next-same-subject' command does, you can use the
749 following hook:
750
751  (setq gnus-select-group-hook
752       (list
753         (lambda ()
754           (mapcar (lambda (header)
755                      (mail-header-set-subject
756                       header
757                       (gnus-simplify-subject
758                        (mail-header-subject header) 're-only)))
759                   gnus-newsgroup-headers))))"
760   :group 'gnus-group-select
761   :type 'hook)
762
763 (defcustom gnus-select-article-hook nil
764   "*A hook called when an article is selected."
765   :group 'gnus-summary-choose
766   :type 'hook)
767
768 (defcustom gnus-visual-mark-article-hook
769   (list 'gnus-highlight-selected-summary)
770   "*Hook run after selecting an article in the summary buffer.
771 It is meant to be used for highlighting the article in some way.  It
772 is not run if `gnus-visual' is nil."
773   :group 'gnus-summary-visual
774   :type 'hook)
775
776 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
777   "*A hook called before parsing the headers."
778   :group 'gnus-various
779   :type 'hook)
780
781 (defcustom gnus-exit-group-hook nil
782   "*A hook called when exiting summary mode.
783 This hook is not called from the non-updating exit commands like `Q'."
784   :group 'gnus-various
785   :type 'hook)
786
787 (defcustom gnus-summary-update-hook
788   (list 'gnus-summary-highlight-line)
789   "*A hook called when a summary line is changed.
790 The hook will not be called if `gnus-visual' is nil.
791
792 The default function `gnus-summary-highlight-line' will
793 highlight the line according to the `gnus-summary-highlight'
794 variable."
795   :group 'gnus-summary-visual
796   :type 'hook)
797
798 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
799   "*A hook called when an article is selected for the first time.
800 The hook is intended to mark an article as read (or unread)
801 automatically when it is selected."
802   :group 'gnus-summary-choose
803   :type 'hook)
804
805 (defcustom gnus-group-no-more-groups-hook nil
806   "*A hook run when returning to group mode having no more (unread) groups."
807   :group 'gnus-group-select
808   :type 'hook)
809
810 (defcustom gnus-ps-print-hook nil
811   "*A hook run before ps-printing something from Gnus."
812   :group 'gnus-summary
813   :type 'hook)
814
815 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
816   "Face used for highlighting the current article in the summary buffer."
817   :group 'gnus-summary-visual
818   :type 'face)
819
820 (defcustom gnus-summary-highlight
821   '(((= mark gnus-canceled-mark)
822      . gnus-summary-cancelled-face)
823     ((and (> score default)
824           (or (= mark gnus-dormant-mark)
825               (= mark gnus-ticked-mark)))
826      . gnus-summary-high-ticked-face)
827     ((and (< score default)
828           (or (= mark gnus-dormant-mark)
829               (= mark gnus-ticked-mark)))
830      . gnus-summary-low-ticked-face)
831     ((or (= mark gnus-dormant-mark)
832          (= mark gnus-ticked-mark))
833      . gnus-summary-normal-ticked-face)
834     ((and (> score default) (= mark gnus-ancient-mark))
835      . gnus-summary-high-ancient-face)
836     ((and (< score default) (= mark gnus-ancient-mark))
837      . gnus-summary-low-ancient-face)
838     ((= mark gnus-ancient-mark)
839      . gnus-summary-normal-ancient-face)
840     ((and (> score default) (= mark gnus-unread-mark))
841      . gnus-summary-high-unread-face)
842     ((and (< score default) (= mark gnus-unread-mark))
843      . gnus-summary-low-unread-face)
844     ((and (memq article gnus-newsgroup-incorporated)
845           (= mark gnus-unread-mark))
846      . gnus-summary-incorporated-face)
847     ((= mark gnus-unread-mark)
848      . gnus-summary-normal-unread-face)
849     ((and (> score default) (memq mark (list gnus-downloadable-mark
850                                              gnus-undownloaded-mark)))
851      . gnus-summary-high-unread-face)
852     ((and (< score default) (memq mark (list gnus-downloadable-mark
853                                              gnus-undownloaded-mark)))
854      . gnus-summary-low-unread-face)
855     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
856           (memq article gnus-newsgroup-unreads))
857      . gnus-summary-normal-unread-face)
858     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
859      . gnus-summary-normal-read-face)
860     ((> score default)
861      . gnus-summary-high-read-face)
862     ((< score default)
863      . gnus-summary-low-read-face)
864     (t
865      . gnus-summary-normal-read-face))
866   "*Controls the highlighting of summary buffer lines.
867
868 A list of (FORM . FACE) pairs.  When deciding how a a particular
869 summary line should be displayed, each form is evaluated.  The content
870 of the face field after the first true form is used.  You can change
871 how those summary lines are displayed, by editing the face field.
872
873 You can use the following variables in the FORM field.
874
875 score:   The articles score
876 default: The default article score.
877 below:   The score below which articles are automatically marked as read.
878 mark:    The articles mark."
879   :group 'gnus-summary-visual
880   :type '(repeat (cons (sexp :tag "Form" nil)
881                        face)))
882
883 (defcustom gnus-alter-header-function nil
884   "Function called to allow alteration of article header structures.
885 The function is called with one parameter, the article header vector,
886 which it may alter in any way.")
887
888 (defvar gnus-decode-encoded-word-function
889   (mime-find-field-decoder 'From 'nov)
890   "Variable that says which function should be used to decode a string with encoded words.")
891
892 (defcustom gnus-extra-headers nil
893   "*Extra headers to parse."
894   :version "21.1"
895   :group 'gnus-summary
896   :type '(repeat symbol))
897
898 (defcustom gnus-ignored-from-addresses
899   (and user-mail-address (regexp-quote user-mail-address))
900   "*Regexp of From headers that may be suppressed in favor of To headers."
901   :version "21.1"
902   :group 'gnus-summary
903   :type 'regexp)
904
905 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
906   "List of charsets that should be ignored.
907 When these charsets are used in the \"charset\" parameter, the
908 default charset will be used instead."
909   :version "21.1"
910   :type '(repeat symbol)
911   :group 'gnus-charset)
912
913 (gnus-define-group-parameter
914  ignored-charsets
915  :type list
916  :function-document
917  "Return the ignored charsets of GROUP."
918  :variable gnus-group-ignored-charsets-alist
919  :variable-default 
920  '(("alt\\.chinese\\.text" iso-8859-1))
921  :variable-document
922  "Alist of regexps (to match group names) and charsets that should be ignored.
923 When these charsets are used in the \"charset\" parameter, the
924 default charset will be used instead."
925  :variable-group gnus-charset
926  :variable-type '(repeat (cons (regexp :tag "Group")
927                                (repeat symbol)))
928  :parameter-type '(choice :tag "Ignored charsets" 
929                           :value nil
930                           (repeat (symbol)))
931  :parameter-document       "\
932 List of charsets that should be ignored.
933
934 When these charsets are used in the \"charset\" parameter, the
935 default charset will be used instead.")
936
937 (defcustom gnus-group-highlight-words-alist nil
938   "Alist of group regexps and highlight regexps.
939 This variable uses the same syntax as `gnus-emphasis-alist'."
940   :version "21.1"
941   :type '(repeat (cons (regexp :tag "Group")
942                        (repeat (list (regexp :tag "Highlight regexp")
943                                      (number :tag "Group for entire word" 0)
944                                      (number :tag "Group for displayed part" 0)
945                                      (symbol :tag "Face"
946                                              gnus-emphasis-highlight-words)))))
947   :group 'gnus-summary-visual)
948
949 (defcustom gnus-use-wheel nil
950   "Use Intelli-mouse on summary movement"
951   :type 'boolean
952   :group 'gnus-summary-maneuvering)
953
954 (defcustom gnus-wheel-scroll-amount '(5 . 1)
955   "Amount to scroll messages by spinning the mouse wheel.
956 This is actually a cons cell, where the first item is the amount to scroll
957 on a normal wheel event, and the second is the amount to scroll when the
958 wheel is moved with the shift key depressed."
959   :type '(cons (integer :tag "Shift") integer)
960   :group 'gnus-summary-maneuvering)
961
962 (defcustom gnus-wheel-edge-resistance 2
963   "How hard it should be to change the current article
964 by moving the mouse over the edge of the article window."
965   :type 'integer
966   :group 'gnus-summary-maneuvering)
967
968 (defcustom gnus-summary-show-article-charset-alist
969   nil
970   "Alist of number and charset.
971 The article will be shown with the charset corresponding to the
972 numbered argument.
973 For example: ((1 . cn-gb-2312) (2 . big5))."
974   :version "21.1"
975   :type '(repeat (cons (number :tag "Argument" 1)
976                        (symbol :tag "Charset")))
977   :group 'gnus-charset)
978
979 (defcustom gnus-preserve-marks t
980   "Whether marks are preserved when moving, copying and respooling messages."
981   :version "21.1"
982   :type 'boolean
983   :group 'gnus-summary-marks)
984
985 (defcustom gnus-alter-articles-to-read-function nil
986   "Function to be called to alter the list of articles to be selected."
987   :type '(choice (const nil) function)
988   :group 'gnus-summary)
989
990 (defcustom gnus-orphan-score nil
991   "*All orphans get this score added.  Set in the score file."
992   :group 'gnus-score-default
993   :type '(choice (const nil)
994                  integer))
995
996 (defcustom gnus-summary-save-parts-default-mime "image/.*"
997   "*A regexp to match MIME parts when saving multiple parts of a message
998 with gnus-summary-save-parts (X m). This regexp will be used by default
999 when prompting the user for which type of files to save."
1000   :group 'gnus-summary
1001   :type 'regexp)
1002
1003
1004 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1005   "*A regexp to match MIME parts when saving multiple parts of a message
1006 with gnus-summary-save-parts (X m). This regexp will be used by default
1007 when prompting the user for which type of files to save."
1008   :group 'gnus-summary
1009   :type 'regexp)
1010
1011 (defcustom gnus-read-all-available-headers nil
1012   "Whether Gnus should parse all headers made available to it.
1013 This is mostly relevant for slow backends where the user may
1014 wish to widen the summary buffer to include all headers
1015 that were fetched.  Say, for nnultimate groups."
1016   :group 'gnus-summary
1017   :type '(choice boolean regexp))
1018
1019 ;;; Internal variables
1020
1021 (defvar gnus-article-mime-handles nil)
1022 (defvar gnus-article-decoded-p nil)
1023 (defvar gnus-article-charset nil)
1024 (defvar gnus-article-ignored-charsets nil)
1025 (defvar gnus-scores-exclude-files nil)
1026 (defvar gnus-page-broken nil)
1027 (defvar gnus-inhibit-mime-unbuttonizing nil)
1028
1029 (defvar gnus-original-article nil)
1030 (defvar gnus-article-internal-prepare-hook nil)
1031 (defvar gnus-newsgroup-process-stack nil)
1032
1033 (defvar gnus-thread-indent-array nil)
1034 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1035 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1036   "Function called to sort the articles within a thread after it has been gathered together.")
1037
1038 (defvar gnus-summary-save-parts-type-history nil)
1039 (defvar gnus-summary-save-parts-last-directory nil)
1040
1041 (defvar gnus-summary-save-parts-type-history nil)
1042 (defvar gnus-summary-save-parts-last-directory nil)
1043
1044 ;; Avoid highlighting in kill files.
1045 (defvar gnus-summary-inhibit-highlight nil)
1046 (defvar gnus-newsgroup-selected-overlay nil)
1047 (defvar gnus-inhibit-limiting nil)
1048 (defvar gnus-newsgroup-adaptive-score-file nil)
1049 (defvar gnus-current-score-file nil)
1050 (defvar gnus-current-move-group nil)
1051 (defvar gnus-current-copy-group nil)
1052 (defvar gnus-current-crosspost-group nil)
1053
1054 (defvar gnus-newsgroup-dependencies nil)
1055 (defvar gnus-newsgroup-adaptive nil)
1056 (defvar gnus-summary-display-article-function nil)
1057 (defvar gnus-summary-highlight-line-function nil
1058   "Function called after highlighting a summary line.")
1059
1060 (defvar gnus-summary-line-format-alist
1061   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1062     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1063     (?s gnus-tmp-subject-or-nil ?s)
1064     (?n gnus-tmp-name ?s)
1065     (?A (std11-address-string
1066          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1067     (?a (or (std11-full-name-string
1068              (car (mime-entity-read-field gnus-tmp-header 'From)))
1069             gnus-tmp-from) ?s)
1070     (?F gnus-tmp-from ?s)
1071     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1072     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1073     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1074     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1075     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1076     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1077     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1078     (?L gnus-tmp-lines ?s)
1079     (?I gnus-tmp-indentation ?s)
1080     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1081     (?R gnus-tmp-replied ?c)
1082     (?\[ gnus-tmp-opening-bracket ?c)
1083     (?\] gnus-tmp-closing-bracket ?c)
1084     (?\> (make-string gnus-tmp-level ? ) ?s)
1085     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1086     (?i gnus-tmp-score ?d)
1087     (?z gnus-tmp-score-char ?c)
1088     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1089     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1090     (?U gnus-tmp-unread ?c)
1091     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1092     (?t (gnus-summary-number-of-articles-in-thread
1093          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1094         ?d)
1095     (?e (gnus-summary-number-of-articles-in-thread
1096          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1097         ?c)
1098     (?u gnus-tmp-user-defined ?s)
1099     (?P (gnus-pick-line-number) ?d))
1100   "An alist of format specifications that can appear in summary lines.
1101 These are paired with what variables they correspond with, along with
1102 the type of the variable (string, integer, character, etc).")
1103
1104 (defvar gnus-summary-dummy-line-format-alist
1105   `((?S gnus-tmp-subject ?s)
1106     (?N gnus-tmp-number ?d)
1107     (?u gnus-tmp-user-defined ?s)))
1108
1109 (defvar gnus-summary-mode-line-format-alist
1110   `((?G gnus-tmp-group-name ?s)
1111     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1112     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1113     (?A gnus-tmp-article-number ?d)
1114     (?Z gnus-tmp-unread-and-unselected ?s)
1115     (?V gnus-version ?s)
1116     (?U gnus-tmp-unread-and-unticked ?d)
1117     (?S gnus-tmp-subject ?s)
1118     (?e gnus-tmp-unselected ?d)
1119     (?u gnus-tmp-user-defined ?s)
1120     (?d (length gnus-newsgroup-dormant) ?d)
1121     (?t (length gnus-newsgroup-marked) ?d)
1122     (?r (length gnus-newsgroup-reads) ?d)
1123     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1124     (?E gnus-newsgroup-expunged-tally ?d)
1125     (?s (gnus-current-score-file-nondirectory) ?s)))
1126
1127 (defvar gnus-last-search-regexp nil
1128   "Default regexp for article search command.")
1129
1130 (defvar gnus-summary-search-article-matched-data nil
1131   "Last matched data of article search command.  It is the local variable
1132 in `gnus-article-buffer' which consists of the list of start position,
1133 end position and text.")
1134
1135 (defvar gnus-last-shell-command nil
1136   "Default shell command on article.")
1137
1138 (defvar gnus-newsgroup-begin nil)
1139 (defvar gnus-newsgroup-end nil)
1140 (defvar gnus-newsgroup-last-rmail nil)
1141 (defvar gnus-newsgroup-last-mail nil)
1142 (defvar gnus-newsgroup-last-folder nil)
1143 (defvar gnus-newsgroup-last-file nil)
1144 (defvar gnus-newsgroup-auto-expire nil)
1145 (defvar gnus-newsgroup-active nil)
1146
1147 (defvar gnus-newsgroup-data nil)
1148 (defvar gnus-newsgroup-data-reverse nil)
1149 (defvar gnus-newsgroup-limit nil)
1150 (defvar gnus-newsgroup-limits nil)
1151
1152 (defvar gnus-newsgroup-unreads nil
1153   "List of unread articles in the current newsgroup.")
1154
1155 (defvar gnus-newsgroup-unselected nil
1156   "List of unselected unread articles in the current newsgroup.")
1157
1158 (defvar gnus-newsgroup-reads nil
1159   "Alist of read articles and article marks in the current newsgroup.")
1160
1161 (defvar gnus-newsgroup-expunged-tally nil)
1162
1163 (defvar gnus-newsgroup-marked nil
1164   "List of ticked articles in the current newsgroup (a subset of unread art).")
1165
1166 (defvar gnus-newsgroup-killed nil
1167   "List of ranges of articles that have been through the scoring process.")
1168
1169 (defvar gnus-newsgroup-cached nil
1170   "List of articles that come from the article cache.")
1171
1172 (defvar gnus-newsgroup-saved nil
1173   "List of articles that have been saved.")
1174
1175 (defvar gnus-newsgroup-kill-headers nil)
1176
1177 (defvar gnus-newsgroup-replied nil
1178   "List of articles that have been replied to in the current newsgroup.")
1179
1180 (defvar gnus-newsgroup-forwarded nil
1181   "List of articles that have been forwarded in the current newsgroup.")
1182
1183 (defvar gnus-newsgroup-expirable nil
1184   "List of articles in the current newsgroup that can be expired.")
1185
1186 (defvar gnus-newsgroup-processable nil
1187   "List of articles in the current newsgroup that can be processed.")
1188
1189 (defvar gnus-newsgroup-downloadable nil
1190   "List of articles in the current newsgroup that can be processed.")
1191
1192 (defvar gnus-newsgroup-undownloaded nil
1193   "List of articles in the current newsgroup that haven't been downloaded..")
1194
1195 (defvar gnus-newsgroup-unsendable nil
1196   "List of articles in the current newsgroup that won't be sent.")
1197
1198 (defvar gnus-newsgroup-bookmarks nil
1199   "List of articles in the current newsgroup that have bookmarks.")
1200
1201 (defvar gnus-newsgroup-dormant nil
1202   "List of dormant articles in the current newsgroup.")
1203
1204 (defvar gnus-newsgroup-scored nil
1205   "List of scored articles in the current newsgroup.")
1206
1207 (defvar gnus-newsgroup-incorporated nil
1208   "List of incorporated articles in the current newsgroup.")
1209
1210 (defvar gnus-newsgroup-headers nil
1211   "List of article headers in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-threads nil)
1214
1215 (defvar gnus-newsgroup-prepared nil
1216   "Whether the current group has been prepared properly.")
1217
1218 (defvar gnus-newsgroup-ancient nil
1219   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1220
1221 (defvar gnus-newsgroup-sparse nil)
1222
1223 (defvar gnus-current-article nil)
1224 (defvar gnus-article-current nil)
1225 (defvar gnus-current-headers nil)
1226 (defvar gnus-have-all-headers nil)
1227 (defvar gnus-last-article nil)
1228 (defvar gnus-newsgroup-history nil)
1229 (defvar gnus-newsgroup-charset nil)
1230 (defvar gnus-newsgroup-ephemeral-charset nil)
1231 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1232
1233 (defvar gnus-article-before-search nil)
1234
1235 (defconst gnus-summary-local-variables
1236   '(gnus-newsgroup-name
1237     gnus-newsgroup-begin gnus-newsgroup-end
1238     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1239     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1240     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1241     gnus-newsgroup-unselected gnus-newsgroup-marked
1242     gnus-newsgroup-reads gnus-newsgroup-saved
1243     gnus-newsgroup-replied gnus-newsgroup-forwarded
1244     gnus-newsgroup-expirable
1245     gnus-newsgroup-processable gnus-newsgroup-killed
1246     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1247     gnus-newsgroup-unsendable
1248     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1249     gnus-newsgroup-headers gnus-newsgroup-threads
1250     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1251     gnus-current-article gnus-current-headers gnus-have-all-headers
1252     gnus-last-article gnus-article-internal-prepare-hook
1253     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1254     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1255     gnus-thread-expunge-below
1256     gnus-score-alist gnus-current-score-file
1257     (gnus-summary-expunge-below . global)
1258     (gnus-summary-mark-below . global)
1259     (gnus-orphan-score . global)
1260     gnus-newsgroup-active gnus-scores-exclude-files
1261     gnus-newsgroup-history gnus-newsgroup-ancient
1262     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1263     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1264     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1265     (gnus-newsgroup-expunged-tally . 0)
1266     gnus-cache-removable-articles gnus-newsgroup-cached
1267     gnus-newsgroup-data gnus-newsgroup-data-reverse
1268     gnus-newsgroup-limit gnus-newsgroup-limits
1269     gnus-newsgroup-charset
1270     gnus-newsgroup-incorporated)
1271   "Variables that are buffer-local to the summary buffers.")
1272
1273 (defvar gnus-newsgroup-variables nil
1274   "Variables that have separate values in the newsgroups.")
1275
1276 ;; Byte-compiler warning.
1277 (eval-when-compile (defvar gnus-article-mode-map))
1278
1279 ;; Subject simplification.
1280
1281 (defun gnus-simplify-whitespace (str)
1282   "Remove excessive whitespace from STR."
1283   (let ((mystr str))
1284     ;; Multiple spaces.
1285     (while (string-match "[ \t][ \t]+" mystr)
1286       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1287                           " "
1288                           (substring mystr (match-end 0)))))
1289     ;; Leading spaces.
1290     (when (string-match "^[ \t]+" mystr)
1291       (setq mystr (substring mystr (match-end 0))))
1292     ;; Trailing spaces.
1293     (when (string-match "[ \t]+$" mystr)
1294       (setq mystr (substring mystr 0 (match-beginning 0))))
1295     mystr))
1296
1297 (defsubst gnus-simplify-subject-re (subject)
1298   "Remove \"Re:\" from subject lines."
1299   (if (string-match message-subject-re-regexp subject)
1300       (substring subject (match-end 0))
1301     subject))
1302
1303 (defun gnus-simplify-subject (subject &optional re-only)
1304   "Remove `Re:' and words in parentheses.
1305 If RE-ONLY is non-nil, strip leading `Re:'s only."
1306   (let ((case-fold-search t))           ;Ignore case.
1307     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1308     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1309       (setq subject (substring subject (match-end 0))))
1310     ;; Remove uninteresting prefixes.
1311     (when (and (not re-only)
1312                gnus-simplify-ignored-prefixes
1313                (string-match gnus-simplify-ignored-prefixes subject))
1314       (setq subject (substring subject (match-end 0))))
1315     ;; Remove words in parentheses from end.
1316     (unless re-only
1317       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1318         (setq subject (substring subject 0 (match-beginning 0)))))
1319     ;; Return subject string.
1320     subject))
1321
1322 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1323 ;; all whitespace.
1324 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1325   (goto-char (point-min))
1326   (while (re-search-forward regexp nil t)
1327     (replace-match (or newtext ""))))
1328
1329 (defun gnus-simplify-buffer-fuzzy ()
1330   "Simplify string in the buffer fuzzily.
1331 The string in the accessible portion of the current buffer is simplified.
1332 It is assumed to be a single-line subject.
1333 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1334 matter is removed.  Additional things can be deleted by setting
1335 `gnus-simplify-subject-fuzzy-regexp'."
1336   (let ((case-fold-search t)
1337         (modified-tick))
1338     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1339
1340     (while (not (eq modified-tick (buffer-modified-tick)))
1341       (setq modified-tick (buffer-modified-tick))
1342       (cond
1343        ((listp gnus-simplify-subject-fuzzy-regexp)
1344         (mapcar 'gnus-simplify-buffer-fuzzy-step
1345                 gnus-simplify-subject-fuzzy-regexp))
1346        (gnus-simplify-subject-fuzzy-regexp
1347         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1348       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1349       (gnus-simplify-buffer-fuzzy-step
1350        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1351       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1352
1353     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1354     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1355     (gnus-simplify-buffer-fuzzy-step " $")
1356     (gnus-simplify-buffer-fuzzy-step "^ +")))
1357
1358 (defun gnus-simplify-subject-fuzzy (subject)
1359   "Simplify a subject string fuzzily.
1360 See `gnus-simplify-buffer-fuzzy' for details."
1361   (save-excursion
1362     (gnus-set-work-buffer)
1363     (let ((case-fold-search t))
1364       ;; Remove uninteresting prefixes.
1365       (when (and gnus-simplify-ignored-prefixes
1366                  (string-match gnus-simplify-ignored-prefixes subject))
1367         (setq subject (substring subject (match-end 0))))
1368       (insert subject)
1369       (inline (gnus-simplify-buffer-fuzzy))
1370       (buffer-string))))
1371
1372 (defsubst gnus-simplify-subject-fully (subject)
1373   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1374   (cond
1375    (gnus-simplify-subject-functions
1376     (gnus-map-function gnus-simplify-subject-functions subject))
1377    ((null gnus-summary-gather-subject-limit)
1378     (gnus-simplify-subject-re subject))
1379    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1380     (gnus-simplify-subject-fuzzy subject))
1381    ((numberp gnus-summary-gather-subject-limit)
1382     (gnus-limit-string (gnus-simplify-subject-re subject)
1383                        gnus-summary-gather-subject-limit))
1384    (t
1385     subject)))
1386
1387 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1388   "Check whether two subjects are equal.
1389 If optional argument simple-first is t, first argument is already
1390 simplified."
1391   (cond
1392    ((null simple-first)
1393     (equal (gnus-simplify-subject-fully s1)
1394            (gnus-simplify-subject-fully s2)))
1395    (t
1396     (equal s1
1397            (gnus-simplify-subject-fully s2)))))
1398
1399 (defun gnus-summary-bubble-group ()
1400   "Increase the score of the current group.
1401 This is a handy function to add to `gnus-summary-exit-hook' to
1402 increase the score of each group you read."
1403   (gnus-group-add-score gnus-newsgroup-name))
1404
1405 \f
1406 ;;;
1407 ;;; Gnus summary mode
1408 ;;;
1409
1410 (put 'gnus-summary-mode 'mode-class 'special)
1411
1412 (defvar gnus-article-commands-menu)
1413
1414 (when t
1415   ;; Non-orthogonal keys
1416
1417   (gnus-define-keys gnus-summary-mode-map
1418     " " gnus-summary-next-page
1419     "\177" gnus-summary-prev-page
1420     [delete] gnus-summary-prev-page
1421     [backspace] gnus-summary-prev-page
1422     "\r" gnus-summary-scroll-up
1423     "\M-\r" gnus-summary-scroll-down
1424     "n" gnus-summary-next-unread-article
1425     "p" gnus-summary-prev-unread-article
1426     "N" gnus-summary-next-article
1427     "P" gnus-summary-prev-article
1428     "\M-\C-n" gnus-summary-next-same-subject
1429     "\M-\C-p" gnus-summary-prev-same-subject
1430     "\M-n" gnus-summary-next-unread-subject
1431     "\M-p" gnus-summary-prev-unread-subject
1432     "." gnus-summary-first-unread-article
1433     "," gnus-summary-best-unread-article
1434     "\M-s" gnus-summary-search-article-forward
1435     "\M-r" gnus-summary-search-article-backward
1436     "<" gnus-summary-beginning-of-article
1437     ">" gnus-summary-end-of-article
1438     "j" gnus-summary-goto-article
1439     "^" gnus-summary-refer-parent-article
1440     "\M-^" gnus-summary-refer-article
1441     "u" gnus-summary-tick-article-forward
1442     "!" gnus-summary-tick-article-forward
1443     "U" gnus-summary-tick-article-backward
1444     "d" gnus-summary-mark-as-read-forward
1445     "D" gnus-summary-mark-as-read-backward
1446     "E" gnus-summary-mark-as-expirable
1447     "\M-u" gnus-summary-clear-mark-forward
1448     "\M-U" gnus-summary-clear-mark-backward
1449     "k" gnus-summary-kill-same-subject-and-select
1450     "\C-k" gnus-summary-kill-same-subject
1451     "\M-\C-k" gnus-summary-kill-thread
1452     "\M-\C-l" gnus-summary-lower-thread
1453     "e" gnus-summary-edit-article
1454     "#" gnus-summary-mark-as-processable
1455     "\M-#" gnus-summary-unmark-as-processable
1456     "\M-\C-t" gnus-summary-toggle-threads
1457     "\M-\C-s" gnus-summary-show-thread
1458     "\M-\C-h" gnus-summary-hide-thread
1459     "\M-\C-f" gnus-summary-next-thread
1460     "\M-\C-b" gnus-summary-prev-thread
1461     [(meta down)] gnus-summary-next-thread
1462     [(meta up)] gnus-summary-prev-thread
1463     "\M-\C-u" gnus-summary-up-thread
1464     "\M-\C-d" gnus-summary-down-thread
1465     "&" gnus-summary-execute-command
1466     "c" gnus-summary-catchup-and-exit
1467     "\C-w" gnus-summary-mark-region-as-read
1468     "\C-t" gnus-summary-toggle-truncation
1469     "?" gnus-summary-mark-as-dormant
1470     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1471     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1472     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1473     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1474     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1475     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1476     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1477     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1478     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1479     "=" gnus-summary-expand-window
1480     "\C-x\C-s" gnus-summary-reselect-current-group
1481     "\M-g" gnus-summary-rescan-group
1482     "w" gnus-summary-stop-page-breaking
1483     "\C-c\C-r" gnus-summary-caesar-message
1484     "\M-t" gnus-summary-toggle-mime
1485     "f" gnus-summary-followup
1486     "F" gnus-summary-followup-with-original
1487     "C" gnus-summary-cancel-article
1488     "r" gnus-summary-reply
1489     "R" gnus-summary-reply-with-original
1490     "\C-c\C-f" gnus-summary-mail-forward
1491     "o" gnus-summary-save-article
1492     "\C-o" gnus-summary-save-article-mail
1493     "|" gnus-summary-pipe-output
1494     "\M-k" gnus-summary-edit-local-kill
1495     "\M-K" gnus-summary-edit-global-kill
1496     ;; "V" gnus-version
1497     "\C-c\C-d" gnus-summary-describe-group
1498     "q" gnus-summary-exit
1499     "Q" gnus-summary-exit-no-update
1500     "\C-c\C-i" gnus-info-find-node
1501     gnus-mouse-2 gnus-mouse-pick-article
1502     "m" gnus-summary-mail-other-window
1503     "a" gnus-summary-post-news
1504     "x" gnus-summary-limit-to-unread
1505     "s" gnus-summary-isearch-article
1506     "t" gnus-article-toggle-headers
1507     "g" gnus-summary-show-article
1508     "l" gnus-summary-goto-last-article
1509     "v" gnus-summary-preview-mime-message
1510     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1511     "\C-d" gnus-summary-enter-digest-group
1512     "\M-\C-d" gnus-summary-read-document
1513     "\M-\C-e" gnus-summary-edit-parameters
1514     "\M-\C-a" gnus-summary-customize-parameters
1515     "\C-c\C-b" gnus-bug
1516     "*" gnus-cache-enter-article
1517     "\M-*" gnus-cache-remove-article
1518     "\M-&" gnus-summary-universal-argument
1519     "\C-l" gnus-recenter
1520     "I" gnus-summary-increase-score
1521     "L" gnus-summary-lower-score
1522     "\M-i" gnus-symbolic-argument
1523     "h" gnus-summary-select-article-buffer
1524
1525     "V" gnus-summary-score-map
1526     "X" gnus-uu-extract-map
1527     "S" gnus-summary-send-map)
1528
1529   ;; Sort of orthogonal keymap
1530   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1531     "t" gnus-summary-tick-article-forward
1532     "!" gnus-summary-tick-article-forward
1533     "d" gnus-summary-mark-as-read-forward
1534     "r" gnus-summary-mark-as-read-forward
1535     "c" gnus-summary-clear-mark-forward
1536     " " gnus-summary-clear-mark-forward
1537     "e" gnus-summary-mark-as-expirable
1538     "x" gnus-summary-mark-as-expirable
1539     "?" gnus-summary-mark-as-dormant
1540     "b" gnus-summary-set-bookmark
1541     "B" gnus-summary-remove-bookmark
1542     "#" gnus-summary-mark-as-processable
1543     "\M-#" gnus-summary-unmark-as-processable
1544     "S" gnus-summary-limit-include-expunged
1545     "C" gnus-summary-catchup
1546     "H" gnus-summary-catchup-to-here
1547     "\C-c" gnus-summary-catchup-all
1548     "k" gnus-summary-kill-same-subject-and-select
1549     "K" gnus-summary-kill-same-subject
1550     "P" gnus-uu-mark-map)
1551
1552   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1553     "c" gnus-summary-clear-above
1554     "u" gnus-summary-tick-above
1555     "m" gnus-summary-mark-above
1556     "k" gnus-summary-kill-below)
1557
1558   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1559     "/" gnus-summary-limit-to-subject
1560     "n" gnus-summary-limit-to-articles
1561     "w" gnus-summary-pop-limit
1562     "s" gnus-summary-limit-to-subject
1563     "a" gnus-summary-limit-to-author
1564     "u" gnus-summary-limit-to-unread
1565     "m" gnus-summary-limit-to-marks
1566     "M" gnus-summary-limit-exclude-marks
1567     "v" gnus-summary-limit-to-score
1568     "*" gnus-summary-limit-include-cached
1569     "D" gnus-summary-limit-include-dormant
1570     "T" gnus-summary-limit-include-thread
1571     "d" gnus-summary-limit-exclude-dormant
1572     "t" gnus-summary-limit-to-age
1573     "x" gnus-summary-limit-to-extra
1574     "E" gnus-summary-limit-include-expunged
1575     "c" gnus-summary-limit-exclude-childless-dormant
1576     "C" gnus-summary-limit-mark-excluded-as-read
1577     "o" gnus-summary-insert-old-articles
1578     "N" gnus-summary-insert-new-articles)
1579
1580   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1581     "n" gnus-summary-next-unread-article
1582     "p" gnus-summary-prev-unread-article
1583     "N" gnus-summary-next-article
1584     "P" gnus-summary-prev-article
1585     "\C-n" gnus-summary-next-same-subject
1586     "\C-p" gnus-summary-prev-same-subject
1587     "\M-n" gnus-summary-next-unread-subject
1588     "\M-p" gnus-summary-prev-unread-subject
1589     "f" gnus-summary-first-unread-article
1590     "b" gnus-summary-best-unread-article
1591     "j" gnus-summary-goto-article
1592     "g" gnus-summary-goto-subject
1593     "l" gnus-summary-goto-last-article
1594     "o" gnus-summary-pop-article)
1595
1596   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1597     "k" gnus-summary-kill-thread
1598     "l" gnus-summary-lower-thread
1599     "i" gnus-summary-raise-thread
1600     "T" gnus-summary-toggle-threads
1601     "t" gnus-summary-rethread-current
1602     "^" gnus-summary-reparent-thread
1603     "s" gnus-summary-show-thread
1604     "S" gnus-summary-show-all-threads
1605     "h" gnus-summary-hide-thread
1606     "H" gnus-summary-hide-all-threads
1607     "n" gnus-summary-next-thread
1608     "p" gnus-summary-prev-thread
1609     "u" gnus-summary-up-thread
1610     "o" gnus-summary-top-thread
1611     "d" gnus-summary-down-thread
1612     "#" gnus-uu-mark-thread
1613     "\M-#" gnus-uu-unmark-thread)
1614
1615   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1616     "g" gnus-summary-prepare
1617     "c" gnus-summary-insert-cached-articles)
1618
1619   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1620     "c" gnus-summary-catchup-and-exit
1621     "C" gnus-summary-catchup-all-and-exit
1622     "E" gnus-summary-exit-no-update
1623     "J" gnus-summary-jump-to-other-group
1624     "Q" gnus-summary-exit
1625     "Z" gnus-summary-exit
1626     "n" gnus-summary-catchup-and-goto-next-group
1627     "R" gnus-summary-reselect-current-group
1628     "G" gnus-summary-rescan-group
1629     "N" gnus-summary-next-group
1630     "s" gnus-summary-save-newsrc
1631     "P" gnus-summary-prev-group)
1632
1633   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1634     " " gnus-summary-next-page
1635     "n" gnus-summary-next-page
1636     "\177" gnus-summary-prev-page
1637     [delete] gnus-summary-prev-page
1638     "p" gnus-summary-prev-page
1639     "\r" gnus-summary-scroll-up
1640     "\M-\r" gnus-summary-scroll-down
1641     "<" gnus-summary-beginning-of-article
1642     ">" gnus-summary-end-of-article
1643     "b" gnus-summary-beginning-of-article
1644     "e" gnus-summary-end-of-article
1645     "^" gnus-summary-refer-parent-article
1646     "r" gnus-summary-refer-parent-article
1647     "D" gnus-summary-enter-digest-group
1648     "R" gnus-summary-refer-references
1649     "T" gnus-summary-refer-thread
1650     "g" gnus-summary-show-article
1651     "s" gnus-summary-isearch-article
1652     "P" gnus-summary-print-article
1653     "M" gnus-mailing-list-insinuate
1654     "t" gnus-article-babel)
1655
1656   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1657     "b" gnus-article-add-buttons
1658     "B" gnus-article-add-buttons-to-head
1659     "o" gnus-article-treat-overstrike
1660     "e" gnus-article-emphasize
1661     "w" gnus-article-fill-cited-article
1662     "Q" gnus-article-fill-long-lines
1663     "C" gnus-article-capitalize-sentences
1664     "c" gnus-article-remove-cr
1665     "Z" gnus-article-decode-HZ
1666     "f" gnus-article-display-x-face
1667     "l" gnus-summary-stop-page-breaking
1668     "r" gnus-summary-caesar-message
1669     "t" gnus-article-toggle-headers
1670     "v" gnus-summary-verbose-headers
1671     "m" gnus-summary-toggle-mime
1672     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1673     "p" gnus-article-verify-x-pgp-sig
1674     "d" gnus-article-treat-dumbquotes
1675     "s" gnus-smiley-display)
1676
1677   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1678     "a" gnus-article-hide
1679     "h" gnus-article-toggle-headers
1680     "b" gnus-article-hide-boring-headers
1681     "s" gnus-article-hide-signature
1682     "c" gnus-article-hide-citation
1683     "C" gnus-article-hide-citation-in-followups
1684     "l" gnus-article-hide-list-identifiers
1685     "p" gnus-article-hide-pgp
1686     "B" gnus-article-strip-banner
1687     "P" gnus-article-hide-pem
1688     "\C-c" gnus-article-hide-citation-maybe)
1689
1690   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1691     "a" gnus-article-highlight
1692     "h" gnus-article-highlight-headers
1693     "c" gnus-article-highlight-citation
1694     "s" gnus-article-highlight-signature)
1695
1696   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1697     "z" gnus-article-date-ut
1698     "u" gnus-article-date-ut
1699     "l" gnus-article-date-local
1700     "p" gnus-article-date-english
1701     "e" gnus-article-date-lapsed
1702     "o" gnus-article-date-original
1703     "i" gnus-article-date-iso8601
1704     "s" gnus-article-date-user)
1705
1706   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1707     "t" gnus-article-remove-trailing-blank-lines
1708     "l" gnus-article-strip-leading-blank-lines
1709     "m" gnus-article-strip-multiple-blank-lines
1710     "a" gnus-article-strip-blank-lines
1711     "A" gnus-article-strip-all-blank-lines
1712     "s" gnus-article-strip-leading-space
1713     "e" gnus-article-strip-trailing-space
1714     "w" gnus-article-remove-leading-whitespace)
1715
1716   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1717     "v" gnus-version
1718     "f" gnus-summary-fetch-faq
1719     "d" gnus-summary-describe-group
1720     "h" gnus-summary-describe-briefly
1721     "i" gnus-info-find-node)
1722
1723   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1724     "e" gnus-summary-expire-articles
1725     "\M-\C-e" gnus-summary-expire-articles-now
1726     "\177" gnus-summary-delete-article
1727     [delete] gnus-summary-delete-article
1728     [backspace] gnus-summary-delete-article
1729     "m" gnus-summary-move-article
1730     "r" gnus-summary-respool-article
1731     "w" gnus-summary-edit-article
1732     "c" gnus-summary-copy-article
1733     "B" gnus-summary-crosspost-article
1734     "q" gnus-summary-respool-query
1735     "t" gnus-summary-respool-trace
1736     "i" gnus-summary-import-article
1737     "I" gnus-summary-create-article
1738     "p" gnus-summary-article-posted-p)
1739
1740   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1741     "o" gnus-summary-save-article
1742     "m" gnus-summary-save-article-mail
1743     "F" gnus-summary-write-article-file
1744     "r" gnus-summary-save-article-rmail
1745     "f" gnus-summary-save-article-file
1746     "b" gnus-summary-save-article-body-file
1747     "h" gnus-summary-save-article-folder
1748     "v" gnus-summary-save-article-vm
1749     "p" gnus-summary-pipe-output
1750     "s" gnus-soup-add-article)
1751
1752   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1753     "b" gnus-summary-display-buttonized
1754     "m" gnus-summary-repair-multipart
1755     "v" gnus-article-view-part
1756     "o" gnus-article-save-part
1757     "c" gnus-article-copy-part
1758     "C" gnus-article-view-part-as-charset
1759     "e" gnus-article-externalize-part
1760     "E" gnus-article-encrypt-body
1761     "i" gnus-article-inline-part
1762     "|" gnus-article-pipe-part))
1763
1764 (defvar gnus-article-post-menu nil)
1765
1766 (defun gnus-summary-make-menu-bar ()
1767   (gnus-turn-off-edit-menu 'summary)
1768
1769   (unless (boundp 'gnus-summary-misc-menu)
1770
1771     (easy-menu-define
1772      gnus-summary-kill-menu gnus-summary-mode-map ""
1773      (cons
1774       "Score"
1775       (nconc
1776        (list
1777         ["Customize" gnus-score-customize t])
1778        (gnus-make-score-map 'increase)
1779        (gnus-make-score-map 'lower)
1780        '(("Mark"
1781           ["Kill below" gnus-summary-kill-below t]
1782           ["Mark above" gnus-summary-mark-above t]
1783           ["Tick above" gnus-summary-tick-above t]
1784           ["Clear above" gnus-summary-clear-above t])
1785          ["Current score" gnus-summary-current-score t]
1786          ["Set score" gnus-summary-set-score t]
1787          ["Switch current score file..." gnus-score-change-score-file t]
1788          ["Set mark below..." gnus-score-set-mark-below t]
1789          ["Set expunge below..." gnus-score-set-expunge-below t]
1790          ["Edit current score file" gnus-score-edit-current-scores t]
1791          ["Edit score file" gnus-score-edit-file t]
1792          ["Trace score" gnus-score-find-trace t]
1793          ["Find words" gnus-score-find-favourite-words t]
1794          ["Rescore buffer" gnus-summary-rescore t]
1795          ["Increase score..." gnus-summary-increase-score t]
1796          ["Lower score..." gnus-summary-lower-score t]))))
1797
1798     ;; Define both the Article menu in the summary buffer and the equivalent
1799     ;; Commands menu in the article buffer here for consistency.
1800     (let ((innards
1801            `(("Hide"
1802               ["All" gnus-article-hide t]
1803               ["Headers" gnus-article-toggle-headers t]
1804               ["Signature" gnus-article-hide-signature t]
1805               ["Citation" gnus-article-hide-citation t]
1806               ["List identifiers" gnus-article-hide-list-identifiers t]
1807               ["PGP" gnus-article-hide-pgp t]
1808               ["Banner" gnus-article-strip-banner t]
1809               ["Boring headers" gnus-article-hide-boring-headers t])
1810              ("Highlight"
1811               ["All" gnus-article-highlight t]
1812               ["Headers" gnus-article-highlight-headers t]
1813               ["Signature" gnus-article-highlight-signature t]
1814               ["Citation" gnus-article-highlight-citation t])
1815              ("Date"
1816               ["Local" gnus-article-date-local t]
1817               ["ISO8601" gnus-article-date-iso8601 t]
1818               ["UT" gnus-article-date-ut t]
1819               ["Original" gnus-article-date-original t]
1820               ["Lapsed" gnus-article-date-lapsed t]
1821               ["User-defined" gnus-article-date-user t])
1822              ("Washing"
1823               ("Remove Blanks"
1824                ["Leading" gnus-article-strip-leading-blank-lines t]
1825                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1826                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1827                ["All of the above" gnus-article-strip-blank-lines t]
1828                ["All" gnus-article-strip-all-blank-lines t]
1829                ["Leading space" gnus-article-strip-leading-space t]
1830                ["Trailing space" gnus-article-strip-trailing-space t]
1831                ["Leading space in headers" 
1832                 gnus-article-remove-leading-whitespace t])
1833               ["Overstrike" gnus-article-treat-overstrike t]
1834               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1835               ["Emphasis" gnus-article-emphasize t]
1836               ["Word wrap" gnus-article-fill-cited-article t]
1837               ["Fill long lines" gnus-article-fill-long-lines t]
1838               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1839               ["CR" gnus-article-remove-cr t]
1840               ["Show X-Face" gnus-article-display-x-face t]
1841               ["Rot 13" gnus-summary-caesar-message
1842                ,@(if (featurep 'xemacs) '(t)
1843                    '(:help "\"Caesar rotate\" article by 13"))]
1844               ["Unix pipe" gnus-summary-pipe-message t]
1845               ["Add buttons" gnus-article-add-buttons t]
1846               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1847               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1848               ["Toggle MIME" gnus-summary-toggle-mime t]
1849               ["Verbose header" gnus-summary-verbose-headers t]
1850               ["Toggle header" gnus-summary-toggle-header t]
1851               ["Toggle smileys" gnus-smiley-display t]
1852               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1853               ["HZ" gnus-article-decode-HZ t])
1854              ("Output"
1855               ["Save in default format" gnus-summary-save-article
1856                ,@(if (featurep 'xemacs) '(t)
1857                    '(:help "Save article using default method"))]
1858               ["Save in file" gnus-summary-save-article-file
1859                ,@(if (featurep 'xemacs) '(t)
1860                    '(:help "Save article in file"))]
1861               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1862               ["Save in MH folder" gnus-summary-save-article-folder t]
1863               ["Save in VM folder" gnus-summary-save-article-vm t]
1864               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1865               ["Save body in file" gnus-summary-save-article-body-file t]
1866               ["Pipe through a filter" gnus-summary-pipe-output t]
1867               ["Add to SOUP packet" gnus-soup-add-article t]
1868               ["Print" gnus-summary-print-article t])
1869              ("Backend"
1870               ["Respool article..." gnus-summary-respool-article t]
1871               ["Move article..." gnus-summary-move-article
1872                (gnus-check-backend-function
1873                 'request-move-article gnus-newsgroup-name)]
1874               ["Copy article..." gnus-summary-copy-article t]
1875               ["Crosspost article..." gnus-summary-crosspost-article
1876                (gnus-check-backend-function
1877                 'request-replace-article gnus-newsgroup-name)]
1878               ["Import file..." gnus-summary-import-article t]
1879               ["Create article..." gnus-summary-create-article t]
1880               ["Check if posted" gnus-summary-article-posted-p t]
1881               ["Edit article" gnus-summary-edit-article
1882                (not (gnus-group-read-only-p))]
1883               ["Delete article" gnus-summary-delete-article
1884                (gnus-check-backend-function
1885                 'request-expire-articles gnus-newsgroup-name)]
1886               ["Query respool" gnus-summary-respool-query t]
1887               ["Trace respool" gnus-summary-respool-trace t]
1888               ["Delete expirable articles" gnus-summary-expire-articles-now
1889                (gnus-check-backend-function
1890                 'request-expire-articles gnus-newsgroup-name)])
1891              ("Extract"
1892               ["Uudecode" gnus-uu-decode-uu
1893                ,@(if (featurep 'xemacs) '(t)
1894                    '(:help "Decode uuencoded article(s)"))]
1895               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1896               ["Unshar" gnus-uu-decode-unshar t]
1897               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1898               ["Save" gnus-uu-decode-save t]
1899               ["Binhex" gnus-uu-decode-binhex t]
1900               ["Postscript" gnus-uu-decode-postscript t])
1901              ("Cache"
1902               ["Enter article" gnus-cache-enter-article t]
1903               ["Remove article" gnus-cache-remove-article t])
1904              ["Translate" gnus-article-babel t]
1905              ["Select article buffer" gnus-summary-select-article-buffer t]
1906              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1907              ["Isearch article..." gnus-summary-isearch-article t]
1908              ["Beginning of the article" gnus-summary-beginning-of-article t]
1909              ["End of the article" gnus-summary-end-of-article t]
1910              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1911              ["Fetch referenced articles" gnus-summary-refer-references t]
1912              ["Fetch current thread" gnus-summary-refer-thread t]
1913              ["Fetch article with id..." gnus-summary-refer-article t]
1914              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1915              ["Redisplay" gnus-summary-show-article t])))
1916       (easy-menu-define
1917        gnus-summary-article-menu gnus-summary-mode-map ""
1918        (cons "Article" innards))
1919
1920       (if (not (keymapp gnus-summary-article-menu))
1921           (easy-menu-define
1922             gnus-article-commands-menu gnus-article-mode-map ""
1923             (cons "Commands" innards))
1924         ;; in Emacs, don't share menu.
1925         (setq gnus-article-commands-menu 
1926               (copy-keymap gnus-summary-article-menu))
1927         (define-key gnus-article-mode-map [menu-bar commands]
1928           (cons "Commands" gnus-article-commands-menu))))
1929
1930     (easy-menu-define
1931      gnus-summary-thread-menu gnus-summary-mode-map ""
1932      '("Threads"
1933        ["Toggle threading" gnus-summary-toggle-threads t]
1934        ["Hide threads" gnus-summary-hide-all-threads t]
1935        ["Show threads" gnus-summary-show-all-threads t]
1936        ["Hide thread" gnus-summary-hide-thread t]
1937        ["Show thread" gnus-summary-show-thread t]
1938        ["Go to next thread" gnus-summary-next-thread t]
1939        ["Go to previous thread" gnus-summary-prev-thread t]
1940        ["Go down thread" gnus-summary-down-thread t]
1941        ["Go up thread" gnus-summary-up-thread t]
1942        ["Top of thread" gnus-summary-top-thread t]
1943        ["Mark thread as read" gnus-summary-kill-thread t]
1944        ["Lower thread score" gnus-summary-lower-thread t]
1945        ["Raise thread score" gnus-summary-raise-thread t]
1946        ["Rethread current" gnus-summary-rethread-current t]))
1947
1948     (easy-menu-define
1949      gnus-summary-post-menu gnus-summary-mode-map ""
1950      `("Post"
1951        ["Post an article" gnus-summary-post-news
1952         ,@(if (featurep 'xemacs) '(t)
1953             '(:help "Post an article"))]
1954        ["Followup" gnus-summary-followup
1955         ,@(if (featurep 'xemacs) '(t)
1956             '(:help "Post followup to this article"))]
1957        ["Followup and yank" gnus-summary-followup-with-original
1958         ,@(if (featurep 'xemacs) '(t)
1959             '(:help "Post followup to this article, quoting its contents"))]
1960        ["Supersede article" gnus-summary-supersede-article t]
1961        ["Cancel article" gnus-summary-cancel-article
1962         ,@(if (featurep 'xemacs) '(t)
1963             '(:help "Cancel an article you posted"))]
1964        ["Reply" gnus-summary-reply t]
1965        ["Reply and yank" gnus-summary-reply-with-original t]
1966        ["Wide reply" gnus-summary-wide-reply t]
1967        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1968         ,@(if (featurep 'xemacs) '(t)
1969             '(:help "Mail a reply, quoting this article"))]
1970        ["Mail forward" gnus-summary-mail-forward t]
1971        ["Post forward" gnus-summary-post-forward t]
1972        ["Digest and mail" gnus-summary-digest-mail-forward t]
1973        ["Digest and post" gnus-summary-digest-post-forward t]
1974        ["Resend message" gnus-summary-resend-message t]
1975        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1976        ["Send a mail" gnus-summary-mail-other-window t]
1977        ["Uuencode and post" gnus-uu-post-news
1978         ,@(if (featurep 'xemacs) '(t)
1979             '(:help "Post a uuencoded article"))]
1980        ["Followup via news" gnus-summary-followup-to-mail t]
1981        ["Followup via news and yank"
1982         gnus-summary-followup-to-mail-with-original t]
1983        ;;("Draft"
1984        ;;["Send" gnus-summary-send-draft t]
1985        ;;["Send bounced" gnus-resend-bounced-mail t])
1986        ))
1987
1988     (cond 
1989      ((not (keymapp gnus-summary-post-menu))
1990       (setq gnus-article-post-menu gnus-summary-post-menu))
1991      ((not gnus-article-post-menu)
1992       ;; Don't share post menu.
1993       (setq gnus-article-post-menu
1994             (copy-keymap gnus-summary-post-menu))))
1995     (define-key gnus-article-mode-map [menu-bar post]
1996       (cons "Post" gnus-article-post-menu))
1997
1998     (easy-menu-define
1999      gnus-summary-misc-menu gnus-summary-mode-map ""
2000      `("Misc"
2001        ("Mark Read"
2002         ["Mark as read" gnus-summary-mark-as-read-forward t]
2003         ["Mark same subject and select"
2004          gnus-summary-kill-same-subject-and-select t]
2005         ["Mark same subject" gnus-summary-kill-same-subject t]
2006         ["Catchup" gnus-summary-catchup
2007          ,@(if (featurep 'xemacs) '(t)
2008              '(:help "Mark unread articles in this group as read"))]
2009         ["Catchup all" gnus-summary-catchup-all t]
2010         ["Catchup to here" gnus-summary-catchup-to-here t]
2011         ["Catchup region" gnus-summary-mark-region-as-read t]
2012         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2013        ("Mark Various"
2014         ["Tick" gnus-summary-tick-article-forward t]
2015         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2016         ["Remove marks" gnus-summary-clear-mark-forward t]
2017         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2018         ["Set bookmark" gnus-summary-set-bookmark t]
2019         ["Remove bookmark" gnus-summary-remove-bookmark t])
2020        ("Mark Limit"
2021         ["Marks..." gnus-summary-limit-to-marks t]
2022         ["Subject..." gnus-summary-limit-to-subject t]
2023         ["Author..." gnus-summary-limit-to-author t]
2024         ["Age..." gnus-summary-limit-to-age t]
2025         ["Extra..." gnus-summary-limit-to-extra t]
2026         ["Score" gnus-summary-limit-to-score t]
2027         ["Unread" gnus-summary-limit-to-unread t]
2028         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2029         ["Articles" gnus-summary-limit-to-articles t]
2030         ["Pop limit" gnus-summary-pop-limit t]
2031         ["Show dormant" gnus-summary-limit-include-dormant t]
2032         ["Hide childless dormant"
2033          gnus-summary-limit-exclude-childless-dormant t]
2034         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2035         ["Hide marked" gnus-summary-limit-exclude-marks t]
2036         ["Show expunged" gnus-summary-limit-include-expunged t])
2037        ("Process Mark"
2038         ["Set mark" gnus-summary-mark-as-processable t]
2039         ["Remove mark" gnus-summary-unmark-as-processable t]
2040         ["Remove all marks" gnus-summary-unmark-all-processable t]
2041         ["Mark above" gnus-uu-mark-over t]
2042         ["Mark series" gnus-uu-mark-series t]
2043         ["Mark region" gnus-uu-mark-region t]
2044         ["Unmark region" gnus-uu-unmark-region t]
2045         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2046         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2047         ["Mark all" gnus-uu-mark-all t]
2048         ["Mark buffer" gnus-uu-mark-buffer t]
2049         ["Mark sparse" gnus-uu-mark-sparse t]
2050         ["Mark thread" gnus-uu-mark-thread t]
2051         ["Unmark thread" gnus-uu-unmark-thread t]
2052         ("Process Mark Sets"
2053          ["Kill" gnus-summary-kill-process-mark t]
2054          ["Yank" gnus-summary-yank-process-mark
2055           gnus-newsgroup-process-stack]
2056          ["Save" gnus-summary-save-process-mark t]))
2057        ("Scroll article"
2058         ["Page forward" gnus-summary-next-page
2059          ,@(if (featurep 'xemacs) '(t)
2060              '(:help "Show next page of article"))]
2061         ["Page backward" gnus-summary-prev-page
2062          ,@(if (featurep 'xemacs) '(t)
2063              '(:help "Show previous page of article"))]
2064         ["Line forward" gnus-summary-scroll-up t])
2065        ("Move"
2066         ["Next unread article" gnus-summary-next-unread-article t]
2067         ["Previous unread article" gnus-summary-prev-unread-article t]
2068         ["Next article" gnus-summary-next-article t]
2069         ["Previous article" gnus-summary-prev-article t]
2070         ["Next unread subject" gnus-summary-next-unread-subject t]
2071         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2072         ["Next article same subject" gnus-summary-next-same-subject t]
2073         ["Previous article same subject" gnus-summary-prev-same-subject t]
2074         ["First unread article" gnus-summary-first-unread-article t]
2075         ["Best unread article" gnus-summary-best-unread-article t]
2076         ["Go to subject number..." gnus-summary-goto-subject t]
2077         ["Go to article number..." gnus-summary-goto-article t]
2078         ["Go to the last article" gnus-summary-goto-last-article t]
2079         ["Pop article off history" gnus-summary-pop-article t])
2080        ("Sort"
2081         ["Sort by number" gnus-summary-sort-by-number t]
2082         ["Sort by author" gnus-summary-sort-by-author t]
2083         ["Sort by subject" gnus-summary-sort-by-subject t]
2084         ["Sort by date" gnus-summary-sort-by-date t]
2085         ["Sort by score" gnus-summary-sort-by-score t]
2086         ["Sort by lines" gnus-summary-sort-by-lines t]
2087         ["Sort by characters" gnus-summary-sort-by-chars t]
2088         ["Original sort" gnus-summary-sort-by-original t])
2089        ("Help"
2090         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2091         ["Describe group" gnus-summary-describe-group t]
2092         ["Read manual" gnus-info-find-node t])
2093        ("Modes"
2094         ["Pick and read" gnus-pick-mode t]
2095         ["Binary" gnus-binary-mode t])
2096        ("Regeneration"
2097         ["Regenerate" gnus-summary-prepare t]
2098         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2099         ["Toggle threading" gnus-summary-toggle-threads t])
2100        ["See old articles" gnus-summary-insert-old-articles t]
2101        ["See new articles" gnus-summary-insert-new-articles t]
2102        ["Filter articles..." gnus-summary-execute-command t]
2103        ["Run command on subjects..." gnus-summary-universal-argument t]
2104        ["Search articles forward..." gnus-summary-search-article-forward t]
2105        ["Search articles backward..." gnus-summary-search-article-backward t]
2106        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2107        ["Expand window" gnus-summary-expand-window t]
2108        ["Expire expirable articles" gnus-summary-expire-articles
2109         (gnus-check-backend-function
2110          'request-expire-articles gnus-newsgroup-name)]
2111        ["Edit local kill file" gnus-summary-edit-local-kill t]
2112        ["Edit main kill file" gnus-summary-edit-global-kill t]
2113        ["Edit group parameters" gnus-summary-edit-parameters t]
2114        ["Customize group parameters" gnus-summary-customize-parameters t]
2115        ["Send a bug report" gnus-bug t]
2116        ("Exit"
2117         ["Catchup and exit" gnus-summary-catchup-and-exit
2118          ,@(if (featurep 'xemacs) '(t)
2119              '(:help "Mark unread articles in this group as read, then exit"))]
2120         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2121         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2122         ["Exit group" gnus-summary-exit
2123          ,@(if (featurep 'xemacs) '(t)
2124              '(:help "Exit current group, return to group selection mode"))]
2125         ["Exit group without updating" gnus-summary-exit-no-update t]
2126         ["Exit and goto next group" gnus-summary-next-group t]
2127         ["Exit and goto prev group" gnus-summary-prev-group t]
2128         ["Reselect group" gnus-summary-reselect-current-group t]
2129         ["Rescan group" gnus-summary-rescan-group t]
2130         ["Update dribble" gnus-summary-save-newsrc t])))
2131
2132     (gnus-run-hooks 'gnus-summary-menu-hook)))
2133
2134 (defvar gnus-summary-tool-bar-map nil)
2135
2136 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2137 (defun gnus-summary-make-tool-bar ()
2138   (if (and (fboundp 'tool-bar-add-item-from-menu)
2139            (default-value 'tool-bar-mode)
2140            (not gnus-summary-tool-bar-map))
2141       (setq gnus-summary-tool-bar-map
2142             (let ((tool-bar-map (make-sparse-keymap))
2143                   (load-path (mm-image-load-path)))
2144               (tool-bar-add-item-from-menu
2145                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2146               (tool-bar-add-item-from-menu
2147                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2148               (tool-bar-add-item-from-menu
2149                'gnus-summary-post-news "post" gnus-summary-mode-map)
2150               (tool-bar-add-item-from-menu
2151                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2152               (tool-bar-add-item-from-menu
2153                'gnus-summary-followup "followup" gnus-summary-mode-map)
2154               (tool-bar-add-item-from-menu
2155                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2156               (tool-bar-add-item-from-menu
2157                'gnus-summary-reply "reply" gnus-summary-mode-map)
2158               (tool-bar-add-item-from-menu
2159                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2160               (tool-bar-add-item-from-menu
2161                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2162               (tool-bar-add-item-from-menu
2163                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2164               (tool-bar-add-item-from-menu
2165                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2166               (tool-bar-add-item-from-menu
2167                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2168               (tool-bar-add-item-from-menu
2169                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2170               (tool-bar-add-item-from-menu
2171                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2172               (tool-bar-add-item-from-menu
2173                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2174               tool-bar-map)))
2175   (if gnus-summary-tool-bar-map
2176       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2177
2178 (defun gnus-score-set-default (var value)
2179   "A version of set that updates the GNU Emacs menu-bar."
2180   (set var value)
2181   ;; It is the message that forces the active status to be updated.
2182   (message ""))
2183
2184 (defun gnus-make-score-map (type)
2185   "Make a summary score map of type TYPE."
2186   (if t
2187       nil
2188     (let ((headers '(("author" "from" string)
2189                      ("subject" "subject" string)
2190                      ("article body" "body" string)
2191                      ("article head" "head" string)
2192                      ("xref" "xref" string)
2193                      ("extra header" "extra" string)
2194                      ("lines" "lines" number)
2195                      ("followups to author" "followup" string)))
2196           (types '((number ("less than" <)
2197                            ("greater than" >)
2198                            ("equal" =))
2199                    (string ("substring" s)
2200                            ("exact string" e)
2201                            ("fuzzy string" f)
2202                            ("regexp" r))))
2203           (perms '(("temporary" (current-time-string))
2204                    ("permanent" nil)
2205                    ("immediate" now)))
2206           header)
2207       (list
2208        (apply
2209         'nconc
2210         (list
2211          (if (eq type 'lower)
2212              "Lower score"
2213            "Increase score"))
2214         (let (outh)
2215           (while headers
2216             (setq header (car headers))
2217             (setq outh
2218                   (cons
2219                    (apply
2220                     'nconc
2221                     (list (car header))
2222                     (let ((ts (cdr (assoc (nth 2 header) types)))
2223                           outt)
2224                       (while ts
2225                         (setq outt
2226                               (cons
2227                                (apply
2228                                 'nconc
2229                                 (list (caar ts))
2230                                 (let ((ps perms)
2231                                       outp)
2232                                   (while ps
2233                                     (setq outp
2234                                           (cons
2235                                            (vector
2236                                             (caar ps)
2237                                             (list
2238                                              'gnus-summary-score-entry
2239                                              (nth 1 header)
2240                                              (if (or (string= (nth 1 header)
2241                                                               "head")
2242                                                      (string= (nth 1 header)
2243                                                               "body"))
2244                                                  ""
2245                                                (list 'gnus-summary-header
2246                                                      (nth 1 header)))
2247                                              (list 'quote (nth 1 (car ts)))
2248                                              (list 'gnus-score-delta-default
2249                                                    nil)
2250                                              (nth 1 (car ps))
2251                                              t)
2252                                             t)
2253                                            outp))
2254                                     (setq ps (cdr ps)))
2255                                   (list (nreverse outp))))
2256                                outt))
2257                         (setq ts (cdr ts)))
2258                       (list (nreverse outt))))
2259                    outh))
2260             (setq headers (cdr headers)))
2261           (list (nreverse outh))))))))
2262
2263 \f
2264
2265 (defun gnus-summary-mode (&optional group)
2266   "Major mode for reading articles.
2267
2268 All normal editing commands are switched off.
2269 \\<gnus-summary-mode-map>
2270 Each line in this buffer represents one article.  To read an
2271 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2272 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2273 respectively.
2274
2275 You can also post articles and send mail from this buffer.  To
2276 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2277 of an article, type `\\[gnus-summary-reply]'.
2278
2279 There are approx. one gazillion commands you can execute in this
2280 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2281
2282 The following commands are available:
2283
2284 \\{gnus-summary-mode-map}"
2285   (interactive)
2286   (kill-all-local-variables)
2287   (when (gnus-visual-p 'summary-menu 'menu)
2288     (gnus-summary-make-menu-bar)
2289     (gnus-summary-make-tool-bar))
2290   (gnus-summary-make-local-variables)
2291   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2292     (gnus-summary-make-local-variables))
2293   (gnus-make-thread-indent-array)
2294   (gnus-simplify-mode-line)
2295   (setq major-mode 'gnus-summary-mode)
2296   (setq mode-name "Summary")
2297   (make-local-variable 'minor-mode-alist)
2298   (use-local-map gnus-summary-mode-map)
2299   (buffer-disable-undo)
2300   (setq buffer-read-only t)             ;Disable modification
2301   (setq truncate-lines t)
2302   (setq selective-display t)
2303   (setq selective-display-ellipses t)   ;Display `...'
2304   (gnus-summary-set-display-table)
2305   (gnus-set-default-directory)
2306   (setq gnus-newsgroup-name group)
2307   (unless (gnus-news-group-p group)
2308     (setq gnus-newsgroup-incorporated
2309           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2310   (make-local-variable 'gnus-summary-line-format)
2311   (make-local-variable 'gnus-summary-line-format-spec)
2312   (make-local-variable 'gnus-summary-dummy-line-format)
2313   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2314   (make-local-variable 'gnus-summary-mark-positions)
2315   (make-local-hook 'pre-command-hook)
2316   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2317   (gnus-run-hooks 'gnus-summary-mode-hook)
2318   (turn-on-gnus-mailing-list-mode)
2319   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2320   (gnus-update-summary-mark-positions))
2321
2322 (defun gnus-summary-make-local-variables ()
2323   "Make all the local summary buffer variables."
2324   (let (global)
2325     (dolist (local gnus-summary-local-variables)
2326       (if (consp local)
2327           (progn
2328             (if (eq (cdr local) 'global)
2329                 ;; Copy the global value of the variable.
2330                 (setq global (symbol-value (car local)))
2331               ;; Use the value from the list.
2332               (setq global (eval (cdr local))))
2333             (set (make-local-variable (car local)) global))
2334         ;; Simple nil-valued local variable.
2335         (set (make-local-variable local) nil)))))
2336
2337 (defun gnus-summary-clear-local-variables ()
2338   (let ((locals gnus-summary-local-variables))
2339     (while locals
2340       (if (consp (car locals))
2341           (and (vectorp (caar locals))
2342                (set (caar locals) nil))
2343         (and (vectorp (car locals))
2344              (set (car locals) nil)))
2345       (setq locals (cdr locals)))))
2346
2347 ;; Summary data functions.
2348
2349 (defmacro gnus-data-number (data)
2350   `(car ,data))
2351
2352 (defmacro gnus-data-set-number (data number)
2353   `(setcar ,data ,number))
2354
2355 (defmacro gnus-data-mark (data)
2356   `(nth 1 ,data))
2357
2358 (defmacro gnus-data-set-mark (data mark)
2359   `(setcar (nthcdr 1 ,data) ,mark))
2360
2361 (defmacro gnus-data-pos (data)
2362   `(nth 2 ,data))
2363
2364 (defmacro gnus-data-set-pos (data pos)
2365   `(setcar (nthcdr 2 ,data) ,pos))
2366
2367 (defmacro gnus-data-header (data)
2368   `(nth 3 ,data))
2369
2370 (defmacro gnus-data-set-header (data header)
2371   `(setcar (nthcdr 3 ,data) ,header))
2372
2373 (defmacro gnus-data-level (data)
2374   `(nth 4 ,data))
2375
2376 (defmacro gnus-data-unread-p (data)
2377   `(= (nth 1 ,data) gnus-unread-mark))
2378
2379 (defmacro gnus-data-read-p (data)
2380   `(/= (nth 1 ,data) gnus-unread-mark))
2381
2382 (defmacro gnus-data-pseudo-p (data)
2383   `(consp (nth 3 ,data)))
2384
2385 (defmacro gnus-data-find (number)
2386   `(assq ,number gnus-newsgroup-data))
2387
2388 (defmacro gnus-data-find-list (number &optional data)
2389   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2390      (memq (assq ,number bdata)
2391            bdata)))
2392
2393 (defmacro gnus-data-make (number mark pos header level)
2394   `(list ,number ,mark ,pos ,header ,level))
2395
2396 (defun gnus-data-enter (after-article number mark pos header level offset)
2397   (let ((data (gnus-data-find-list after-article)))
2398     (unless data
2399       (error "No such article: %d" after-article))
2400     (setcdr data (cons (gnus-data-make number mark pos header level)
2401                        (cdr data)))
2402     (setq gnus-newsgroup-data-reverse nil)
2403     (gnus-data-update-list (cddr data) offset)))
2404
2405 (defun gnus-data-enter-list (after-article list &optional offset)
2406   (when list
2407     (let ((data (and after-article (gnus-data-find-list after-article)))
2408           (ilist list))
2409       (if (not (or data
2410                    after-article))
2411           (let ((odata gnus-newsgroup-data))
2412             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2413             (when offset
2414               (gnus-data-update-list odata offset)))
2415         ;; Find the last element in the list to be spliced into the main
2416         ;; list.
2417         (while (cdr list)
2418           (setq list (cdr list)))
2419         (if (not data)
2420             (progn
2421               (setcdr list gnus-newsgroup-data)
2422               (setq gnus-newsgroup-data ilist)
2423               (when offset
2424                 (gnus-data-update-list (cdr list) offset)))
2425           (setcdr list (cdr data))
2426           (setcdr data ilist)
2427           (when offset
2428             (gnus-data-update-list (cdr list) offset))))
2429       (setq gnus-newsgroup-data-reverse nil))))
2430
2431 (defun gnus-data-remove (article &optional offset)
2432   (let ((data gnus-newsgroup-data))
2433     (if (= (gnus-data-number (car data)) article)
2434         (progn
2435           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2436                 gnus-newsgroup-data-reverse nil)
2437           (when offset
2438             (gnus-data-update-list gnus-newsgroup-data offset)))
2439       (while (cdr data)
2440         (when (= (gnus-data-number (cadr data)) article)
2441           (setcdr data (cddr data))
2442           (when offset
2443             (gnus-data-update-list (cdr data) offset))
2444           (setq data nil
2445                 gnus-newsgroup-data-reverse nil))
2446         (setq data (cdr data))))))
2447
2448 (defmacro gnus-data-list (backward)
2449   `(if ,backward
2450        (or gnus-newsgroup-data-reverse
2451            (setq gnus-newsgroup-data-reverse
2452                  (reverse gnus-newsgroup-data)))
2453      gnus-newsgroup-data))
2454
2455 (defun gnus-data-update-list (data offset)
2456   "Add OFFSET to the POS of all data entries in DATA."
2457   (setq gnus-newsgroup-data-reverse nil)
2458   (while data
2459     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2460     (setq data (cdr data))))
2461
2462 (defun gnus-summary-article-pseudo-p (article)
2463   "Say whether this article is a pseudo article or not."
2464   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2465
2466 (defmacro gnus-summary-article-sparse-p (article)
2467   "Say whether this article is a sparse article or not."
2468   `(memq ,article gnus-newsgroup-sparse))
2469
2470 (defmacro gnus-summary-article-ancient-p (article)
2471   "Say whether this article is a sparse article or not."
2472   `(memq ,article gnus-newsgroup-ancient))
2473
2474 (defun gnus-article-parent-p (number)
2475   "Say whether this article is a parent or not."
2476   (let ((data (gnus-data-find-list number)))
2477     (and (cdr data)                     ; There has to be an article after...
2478          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2479             (gnus-data-level (nth 1 data))))))
2480
2481 (defun gnus-article-children (number)
2482   "Return a list of all children to NUMBER."
2483   (let* ((data (gnus-data-find-list number))
2484          (level (gnus-data-level (car data)))
2485          children)
2486     (setq data (cdr data))
2487     (while (and data
2488                 (= (gnus-data-level (car data)) (1+ level)))
2489       (push (gnus-data-number (car data)) children)
2490       (setq data (cdr data)))
2491     children))
2492
2493 (defmacro gnus-summary-skip-intangible ()
2494   "If the current article is intangible, then jump to a different article."
2495   '(let ((to (get-text-property (point) 'gnus-intangible)))
2496      (and to (gnus-summary-goto-subject to))))
2497
2498 (defmacro gnus-summary-article-intangible-p ()
2499   "Say whether this article is intangible or not."
2500   '(get-text-property (point) 'gnus-intangible))
2501
2502 (defun gnus-article-read-p (article)
2503   "Say whether ARTICLE is read or not."
2504   (not (or (memq article gnus-newsgroup-marked)
2505            (memq article gnus-newsgroup-unreads)
2506            (memq article gnus-newsgroup-unselected)
2507            (memq article gnus-newsgroup-dormant))))
2508
2509 ;; Some summary mode macros.
2510
2511 (defmacro gnus-summary-article-number ()
2512   "The article number of the article on the current line.
2513 If there isn's an article number here, then we return the current
2514 article number."
2515   '(progn
2516      (gnus-summary-skip-intangible)
2517      (or (get-text-property (point) 'gnus-number)
2518          (gnus-summary-last-subject))))
2519
2520 (defmacro gnus-summary-article-header (&optional number)
2521   "Return the header of article NUMBER."
2522   `(gnus-data-header (gnus-data-find
2523                       ,(or number '(gnus-summary-article-number)))))
2524
2525 (defmacro gnus-summary-thread-level (&optional number)
2526   "Return the level of thread that starts with article NUMBER."
2527   `(if (and (eq gnus-summary-make-false-root 'dummy)
2528             (get-text-property (point) 'gnus-intangible))
2529        0
2530      (gnus-data-level (gnus-data-find
2531                        ,(or number '(gnus-summary-article-number))))))
2532
2533 (defmacro gnus-summary-article-mark (&optional number)
2534   "Return the mark of article NUMBER."
2535   `(gnus-data-mark (gnus-data-find
2536                     ,(or number '(gnus-summary-article-number)))))
2537
2538 (defmacro gnus-summary-article-pos (&optional number)
2539   "Return the position of the line of article NUMBER."
2540   `(gnus-data-pos (gnus-data-find
2541                    ,(or number '(gnus-summary-article-number)))))
2542
2543 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2544 (defmacro gnus-summary-article-subject (&optional number)
2545   "Return current subject string or nil if nothing."
2546   `(let ((headers
2547           ,(if number
2548                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2549              '(gnus-data-header (assq (gnus-summary-article-number)
2550                                       gnus-newsgroup-data)))))
2551      (and headers
2552           (vectorp headers)
2553           (mail-header-subject headers))))
2554
2555 (defmacro gnus-summary-article-score (&optional number)
2556   "Return current article score."
2557   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2558                   gnus-newsgroup-scored))
2559        gnus-summary-default-score 0))
2560
2561 (defun gnus-summary-article-children (&optional number)
2562   "Return a list of article numbers that are children of article NUMBER."
2563   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2564          (level (gnus-data-level (car data)))
2565          l children)
2566     (while (and (setq data (cdr data))
2567                 (> (setq l (gnus-data-level (car data))) level))
2568       (and (= (1+ level) l)
2569            (push (gnus-data-number (car data))
2570                  children)))
2571     (nreverse children)))
2572
2573 (defun gnus-summary-article-parent (&optional number)
2574   "Return the article number of the parent of article NUMBER."
2575   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2576                                     (gnus-data-list t)))
2577          (level (gnus-data-level (car data))))
2578     (if (zerop level)
2579         ()                              ; This is a root.
2580       ;; We search until we find an article with a level less than
2581       ;; this one.  That function has to be the parent.
2582       (while (and (setq data (cdr data))
2583                   (not (< (gnus-data-level (car data)) level))))
2584       (and data (gnus-data-number (car data))))))
2585
2586 (defun gnus-unread-mark-p (mark)
2587   "Say whether MARK is the unread mark."
2588   (= mark gnus-unread-mark))
2589
2590 (defun gnus-read-mark-p (mark)
2591   "Say whether MARK is one of the marks that mark as read.
2592 This is all marks except unread, ticked, dormant, and expirable."
2593   (not (or (= mark gnus-unread-mark)
2594            (= mark gnus-ticked-mark)
2595            (= mark gnus-dormant-mark)
2596            (= mark gnus-expirable-mark))))
2597
2598 (defmacro gnus-article-mark (number)
2599   "Return the MARK of article NUMBER.
2600 This macro should only be used when computing the mark the \"first\"
2601 time; i.e., when generating the summary lines.  After that,
2602 `gnus-summary-article-mark' should be used to examine the
2603 marks of articles."
2604   `(cond
2605     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2606     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2607     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2608     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2609     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2610     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2611     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2612     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2613            gnus-ancient-mark))))
2614
2615 ;; Saving hidden threads.
2616
2617 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2618 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2619
2620 (defmacro gnus-save-hidden-threads (&rest forms)
2621   "Save hidden threads, eval FORMS, and restore the hidden threads."
2622   (let ((config (make-symbol "config")))
2623     `(let ((,config (gnus-hidden-threads-configuration)))
2624        (unwind-protect
2625            (save-excursion
2626              ,@forms)
2627          (gnus-restore-hidden-threads-configuration ,config)))))
2628
2629 (defun gnus-data-compute-positions ()
2630   "Compute the positions of all articles."
2631   (setq gnus-newsgroup-data-reverse nil)
2632   (let ((data gnus-newsgroup-data))
2633     (save-excursion
2634       (gnus-save-hidden-threads
2635         (gnus-summary-show-all-threads)
2636         (goto-char (point-min))
2637         (while data
2638           (while (get-text-property (point) 'gnus-intangible)
2639             (forward-line 1))
2640           (gnus-data-set-pos (car data) (+ (point) 3))
2641           (setq data (cdr data))
2642           (forward-line 1))))))
2643
2644 (defun gnus-hidden-threads-configuration ()
2645   "Return the current hidden threads configuration."
2646   (save-excursion
2647     (let (config)
2648       (goto-char (point-min))
2649       (while (search-forward "\r" nil t)
2650         (push (1- (point)) config))
2651       config)))
2652
2653 (defun gnus-restore-hidden-threads-configuration (config)
2654   "Restore hidden threads configuration from CONFIG."
2655   (save-excursion
2656     (let (point buffer-read-only)
2657       (while (setq point (pop config))
2658         (when (and (< point (point-max))
2659                    (goto-char point)
2660                    (eq (char-after) ?\n))
2661           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2662
2663 ;; Various summary mode internalish functions.
2664
2665 (defun gnus-mouse-pick-article (e)
2666   (interactive "e")
2667   (mouse-set-point e)
2668   (gnus-summary-next-page nil t))
2669
2670 (defun gnus-summary-set-display-table ()
2671   "Change the display table.
2672 Odd characters have a tendency to mess
2673 up nicely formatted displays - we make all possible glyphs
2674 display only a single character."
2675
2676   ;; We start from the standard display table, if any.
2677   (let ((table (or (copy-sequence standard-display-table)
2678                    (make-display-table)))
2679         (i 32))
2680     ;; Nix out all the control chars...
2681     (while (>= (setq i (1- i)) 0)
2682       (aset table i [??]))
2683     ;; ... but not newline and cr, of course.  (cr is necessary for the
2684     ;; selective display).
2685     (aset table ?\n nil)
2686     (aset table ?\r nil)
2687     ;; We keep TAB as well.
2688     (aset table ?\t nil)
2689     ;; We nix out any glyphs over 126 that are not set already.
2690     (let ((i 256))
2691       (while (>= (setq i (1- i)) 127)
2692         ;; Only modify if the entry is nil.
2693         (unless (aref table i)
2694           (aset table i [??]))))
2695     (setq buffer-display-table table)))
2696
2697 (defun gnus-summary-buffer-name (group)
2698   "Return the summary buffer name of GROUP."
2699   (concat "*Summary " group "*"))
2700
2701 (defun gnus-summary-setup-buffer (group)
2702   "Initialize summary buffer."
2703   (let ((buffer (gnus-summary-buffer-name group)))
2704     (if (get-buffer buffer)
2705         (progn
2706           (set-buffer buffer)
2707           (setq gnus-summary-buffer (current-buffer))
2708           (not gnus-newsgroup-prepared))
2709       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2710       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2711       (gnus-summary-mode group)
2712       (when gnus-carpal
2713         (gnus-carpal-setup-buffer 'summary))
2714       (unless gnus-single-article-buffer
2715         (make-local-variable 'gnus-article-buffer)
2716         (make-local-variable 'gnus-article-current)
2717         (make-local-variable 'gnus-original-article-buffer))
2718       (setq gnus-newsgroup-name group)
2719       ;; Set any local variables in the group parameters.
2720       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2721       t)))
2722
2723 (defun gnus-set-global-variables ()
2724   "Set the global equivalents of the buffer-local variables.
2725 They are set to the latest values they had.  These reflect the summary
2726 buffer that was in action when the last article was fetched."
2727   (when (eq major-mode 'gnus-summary-mode)
2728     (setq gnus-summary-buffer (current-buffer))
2729     (let ((name gnus-newsgroup-name)
2730           (marked gnus-newsgroup-marked)
2731           (unread gnus-newsgroup-unreads)
2732           (headers gnus-current-headers)
2733           (data gnus-newsgroup-data)
2734           (summary gnus-summary-buffer)
2735           (article-buffer gnus-article-buffer)
2736           (original gnus-original-article-buffer)
2737           (gac gnus-article-current)
2738           (reffed gnus-reffed-article-number)
2739           (score-file gnus-current-score-file)
2740           (default-charset gnus-newsgroup-charset)
2741           vlist)
2742       (let ((locals gnus-newsgroup-variables))
2743         (while locals
2744           (if (consp (car locals))
2745               (push (eval (caar locals)) vlist)
2746             (push (eval (car locals)) vlist))
2747           (setq locals (cdr locals)))
2748         (setq vlist (nreverse vlist)))
2749       (save-excursion
2750         (set-buffer gnus-group-buffer)
2751         (setq gnus-newsgroup-name name
2752               gnus-newsgroup-marked marked
2753               gnus-newsgroup-unreads unread
2754               gnus-current-headers headers
2755               gnus-newsgroup-data data
2756               gnus-article-current gac
2757               gnus-summary-buffer summary
2758               gnus-article-buffer article-buffer
2759               gnus-original-article-buffer original
2760               gnus-reffed-article-number reffed
2761               gnus-current-score-file score-file
2762               gnus-newsgroup-charset default-charset)
2763         (let ((locals gnus-newsgroup-variables))
2764           (while locals
2765             (if (consp (car locals))
2766                 (set (caar locals) (pop vlist))
2767               (set (car locals) (pop vlist)))
2768             (setq locals (cdr locals))))
2769         ;; The article buffer also has local variables.
2770         (when (gnus-buffer-live-p gnus-article-buffer)
2771           (set-buffer gnus-article-buffer)
2772           (setq gnus-summary-buffer summary))))))
2773
2774 (defun gnus-summary-article-unread-p (article)
2775   "Say whether ARTICLE is unread or not."
2776   (memq article gnus-newsgroup-unreads))
2777
2778 (defun gnus-summary-first-article-p (&optional article)
2779   "Return whether ARTICLE is the first article in the buffer."
2780   (if (not (setq article (or article (gnus-summary-article-number))))
2781       nil
2782     (eq article (caar gnus-newsgroup-data))))
2783
2784 (defun gnus-summary-last-article-p (&optional article)
2785   "Return whether ARTICLE is the last article in the buffer."
2786   (if (not (setq article (or article (gnus-summary-article-number))))
2787       ;; All non-existent numbers are the last article.  :-)
2788       t
2789     (not (cdr (gnus-data-find-list article)))))
2790
2791 (defun gnus-make-thread-indent-array ()
2792   (let ((n 200))
2793     (unless (and gnus-thread-indent-array
2794                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2795       (setq gnus-thread-indent-array (make-vector 201 "")
2796             gnus-thread-indent-array-level gnus-thread-indent-level)
2797       (while (>= n 0)
2798         (aset gnus-thread-indent-array n
2799               (make-string (* n gnus-thread-indent-level) ? ))
2800         (setq n (1- n))))))
2801
2802 (defun gnus-update-summary-mark-positions ()
2803   "Compute where the summary marks are to go."
2804   (save-excursion
2805     (when (gnus-buffer-exists-p gnus-summary-buffer)
2806       (set-buffer gnus-summary-buffer))
2807     (let ((gnus-replied-mark 129)
2808           (gnus-score-below-mark 130)
2809           (gnus-score-over-mark 130)
2810           (gnus-download-mark 131)
2811           (spec gnus-summary-line-format-spec)
2812           gnus-visual pos)
2813       (save-excursion
2814         (gnus-set-work-buffer)
2815         (let ((gnus-summary-line-format-spec spec)
2816               (gnus-newsgroup-downloadable '((0 . t))))
2817           (gnus-summary-insert-line
2818            (make-full-mail-header 0 "" "nobody"
2819                                   "05 Apr 2001 23:33:09 +0400"
2820                                   "" "" 0 0 "" nil)
2821            0 nil 128 t nil "" nil 1)
2822           (goto-char (point-min))
2823           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2824                                              (- (point) 2)))))
2825           (goto-char (point-min))
2826           (push (cons 'replied (and (search-forward "\201" nil t)
2827                                     (- (point) 2)))
2828                 pos)
2829           (goto-char (point-min))
2830           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2831                 pos)
2832           (goto-char (point-min))
2833           (push (cons 'download
2834                       (and (search-forward "\203" nil t) (- (point) 2)))
2835                 pos)))
2836       (setq gnus-summary-mark-positions pos))))
2837
2838 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2839   "Insert a dummy root in the summary buffer."
2840   (beginning-of-line)
2841   (gnus-add-text-properties
2842    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2843    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2844
2845 (defun gnus-summary-from-or-to-or-newsgroups (header)
2846   (let ((to (cdr (assq 'To (mail-header-extra header))))
2847         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2848         (default-mime-charset (with-current-buffer gnus-summary-buffer
2849                                 default-mime-charset)))
2850     (cond
2851      ((and to
2852            gnus-ignored-from-addresses
2853            (string-match gnus-ignored-from-addresses
2854                          (mail-header-from header)))
2855       (concat "-> "
2856               (or (car (funcall gnus-extract-address-components
2857                                 (funcall
2858                                  gnus-decode-encoded-word-function to)))
2859                   (funcall gnus-decode-encoded-word-function to))))
2860      ((and newsgroups
2861            gnus-ignored-from-addresses
2862            (string-match gnus-ignored-from-addresses
2863                          (mail-header-from header)))
2864       (concat "=> " newsgroups))
2865      (t
2866       (or (car (funcall gnus-extract-address-components
2867                         (mail-header-from header)))
2868           (mail-header-from header))))))
2869
2870 (defun gnus-summary-insert-line (gnus-tmp-header
2871                                  gnus-tmp-level gnus-tmp-current
2872                                  gnus-tmp-unread gnus-tmp-replied
2873                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2874                                  &optional gnus-tmp-dummy gnus-tmp-score
2875                                  gnus-tmp-process)
2876   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2877          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2878          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2879          (gnus-tmp-score-char
2880           (if (or (null gnus-summary-default-score)
2881                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2882                       gnus-summary-zcore-fuzz))
2883               ?  ;Whitespace
2884             (if (< gnus-tmp-score gnus-summary-default-score)
2885                 gnus-score-below-mark gnus-score-over-mark)))
2886          (gnus-tmp-replied
2887           (cond (gnus-tmp-process gnus-process-mark)
2888                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2889                  gnus-cached-mark)
2890                 (gnus-tmp-replied gnus-replied-mark)
2891                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2892                  gnus-saved-mark)
2893                 (t gnus-no-mark)))
2894          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2895          (gnus-tmp-name
2896           (cond
2897            ((string-match "<[^>]+> *$" gnus-tmp-from)
2898             (let ((beg (match-beginning 0)))
2899               (or (and (string-match "^\".+\"" gnus-tmp-from)
2900                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2901                   (substring gnus-tmp-from 0 beg))))
2902            ((string-match "(.+)" gnus-tmp-from)
2903             (substring gnus-tmp-from
2904                        (1+ (match-beginning 0)) (1- (match-end 0))))
2905            (t gnus-tmp-from)))
2906          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2907          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2908          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2909          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2910          (buffer-read-only nil))
2911     (when (string= gnus-tmp-name "")
2912       (setq gnus-tmp-name gnus-tmp-from))
2913     (unless (numberp gnus-tmp-lines)
2914       (setq gnus-tmp-lines -1))
2915     (when (= gnus-tmp-lines -1)
2916       (setq gnus-tmp-lines "?"))
2917     (gnus-put-text-property-excluding-characters-with-faces
2918      (point)
2919      (progn (eval gnus-summary-line-format-spec) (point))
2920      'gnus-number gnus-tmp-number)
2921     (when (gnus-visual-p 'summary-highlight 'highlight)
2922       (forward-line -1)
2923       (gnus-run-hooks 'gnus-summary-update-hook)
2924       (forward-line 1))))
2925
2926 (defun gnus-summary-update-line (&optional dont-update)
2927   "Update summary line after change."
2928   (when (and gnus-summary-default-score
2929              (not gnus-summary-inhibit-highlight))
2930     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2931            (article (gnus-summary-article-number))
2932            (score (gnus-summary-article-score article)))
2933       (unless dont-update
2934         (if (and gnus-summary-mark-below
2935                  (< (gnus-summary-article-score)
2936                     gnus-summary-mark-below))
2937             ;; This article has a low score, so we mark it as read.
2938             (when (memq article gnus-newsgroup-unreads)
2939               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2940           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2941             ;; This article was previously marked as read on account
2942             ;; of a low score, but now it has risen, so we mark it as
2943             ;; unread.
2944             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2945         (gnus-summary-update-mark
2946          (if (or (null gnus-summary-default-score)
2947                  (<= (abs (- score gnus-summary-default-score))
2948                      gnus-summary-zcore-fuzz))
2949              ?  ;Whitespace
2950            (if (< score gnus-summary-default-score)
2951                gnus-score-below-mark gnus-score-over-mark))
2952          'score))
2953       ;; Do visual highlighting.
2954       (when (gnus-visual-p 'summary-highlight 'highlight)
2955         (gnus-run-hooks 'gnus-summary-update-hook)))))
2956
2957 (defvar gnus-tmp-new-adopts nil)
2958
2959 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2960   "Return the number of articles in THREAD.
2961 This may be 0 in some cases -- if none of the articles in
2962 the thread are to be displayed."
2963   (let* ((number
2964           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2965           (cond
2966            ((not (listp thread))
2967             1)
2968            ((and (consp thread) (cdr thread))
2969             (apply
2970              '+ 1 (mapcar
2971                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2972            ((null thread)
2973             1)
2974            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2975             1)
2976            (t 0))))
2977     (when (and level (zerop level) gnus-tmp-new-adopts)
2978       (incf number
2979             (apply '+ (mapcar
2980                        'gnus-summary-number-of-articles-in-thread
2981                        gnus-tmp-new-adopts))))
2982     (if char
2983         (if (> number 1) gnus-not-empty-thread-mark
2984           gnus-empty-thread-mark)
2985       number)))
2986
2987 (defun gnus-summary-set-local-parameters (group)
2988   "Go through the local params of GROUP and set all variable specs in that list."
2989   (let ((params (gnus-group-find-parameter group))
2990         elem)
2991     (while params
2992       (setq elem (car params)
2993             params (cdr params))
2994       (and (consp elem)                 ; Has to be a cons.
2995            (consp (cdr elem))           ; The cdr has to be a list.
2996            (symbolp (car elem))         ; Has to be a symbol in there.
2997            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2998            (ignore-errors               ; So we set it.
2999              (make-local-variable (car elem))
3000              (set (car elem) (eval (nth 1 elem))))))))
3001
3002 (defun gnus-summary-read-group (group &optional show-all no-article
3003                                       kill-buffer no-display backward
3004                                       select-articles)
3005   "Start reading news in newsgroup GROUP.
3006 If SHOW-ALL is non-nil, already read articles are also listed.
3007 If NO-ARTICLE is non-nil, no article is selected initially.
3008 If NO-DISPLAY, don't generate a summary buffer."
3009   (let (result)
3010     (while (and group
3011                 (null (setq result
3012                             (let ((gnus-auto-select-next nil))
3013                               (or (gnus-summary-read-group-1
3014                                    group show-all no-article
3015                                    kill-buffer no-display
3016                                    select-articles)
3017                                   (setq show-all nil
3018                                         select-articles nil)))))
3019                 (eq gnus-auto-select-next 'quietly))
3020       (set-buffer gnus-group-buffer)
3021       ;; The entry function called above goes to the next
3022       ;; group automatically, so we go two groups back
3023       ;; if we are searching for the previous group.
3024       (when backward
3025         (gnus-group-prev-unread-group 2))
3026       (if (not (equal group (gnus-group-group-name)))
3027           (setq group (gnus-group-group-name))
3028         (setq group nil)))
3029     result))
3030
3031 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3032   "Directly jump to the other GROUP from summary buffer.
3033 If SHOW-ALL is non-nil, already read articles are also listed."
3034   (interactive
3035    (if (eq gnus-summary-buffer (current-buffer))
3036        (list (completing-read
3037               "Group: " gnus-active-hashtb nil t
3038               (when (and gnus-newsgroup-name
3039                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3040                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3041               'gnus-group-history)
3042              current-prefix-arg)
3043      (error "%s must be invoked from a gnus summary buffer." this-command)))
3044   (unless (or (zerop (length group))
3045               (and gnus-newsgroup-name
3046                    (string-equal gnus-newsgroup-name group)))
3047     (gnus-summary-exit)
3048     (gnus-summary-read-group group show-all
3049                              gnus-dont-select-after-jump-to-other-group)))
3050
3051 (defun gnus-summary-read-group-1 (group show-all no-article
3052                                         kill-buffer no-display
3053                                         &optional select-articles)
3054   ;; Killed foreign groups can't be entered.
3055   ;;  (when (and (not (gnus-group-native-p group))
3056   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3057   ;;    (error "Dead non-native groups can't be entered"))
3058   (gnus-message 5 "Retrieving newsgroup: %s..."
3059                 (gnus-group-decoded-name group))
3060   (let* ((new-group (gnus-summary-setup-buffer group))
3061          (quit-config (gnus-group-quit-config group))
3062          (did-select (and new-group (gnus-select-newsgroup
3063                                      group show-all select-articles))))
3064     (cond
3065      ;; This summary buffer exists already, so we just select it.
3066      ((not new-group)
3067       (gnus-set-global-variables)
3068       (when kill-buffer
3069         (gnus-kill-or-deaden-summary kill-buffer))
3070       (gnus-configure-windows 'summary 'force)
3071       (gnus-set-mode-line 'summary)
3072       (gnus-summary-position-point)
3073       (message "")
3074       t)
3075      ;; We couldn't select this group.
3076      ((null did-select)
3077       (when (and (eq major-mode 'gnus-summary-mode)
3078                  (not (equal (current-buffer) kill-buffer)))
3079         (kill-buffer (current-buffer))
3080         (if (not quit-config)
3081             (progn
3082               ;; Update the info -- marks might need to be removed,
3083               ;; for instance.
3084               (gnus-summary-update-info)
3085               (set-buffer gnus-group-buffer)
3086               (gnus-group-jump-to-group group)
3087               (gnus-group-next-unread-group 1))
3088           (gnus-handle-ephemeral-exit quit-config)))
3089       (let ((grpinfo (gnus-get-info group)))
3090         (if (null (gnus-info-read grpinfo))
3091             (gnus-message 3 "Group %s contains no messages" 
3092                           (gnus-group-decoded-name group))
3093           (gnus-message 3 "Can't select group")))
3094       nil)
3095      ;; The user did a `C-g' while prompting for number of articles,
3096      ;; so we exit this group.
3097      ((eq did-select 'quit)
3098       (and (eq major-mode 'gnus-summary-mode)
3099            (not (equal (current-buffer) kill-buffer))
3100            (kill-buffer (current-buffer)))
3101       (when kill-buffer
3102         (gnus-kill-or-deaden-summary kill-buffer))
3103       (if (not quit-config)
3104           (progn
3105             (set-buffer gnus-group-buffer)
3106             (gnus-group-jump-to-group group)
3107             (gnus-group-next-unread-group 1)
3108             (gnus-configure-windows 'group 'force))
3109         (gnus-handle-ephemeral-exit quit-config))
3110       ;; Finally signal the quit.
3111       (signal 'quit nil))
3112      ;; The group was successfully selected.
3113      (t
3114       (gnus-set-global-variables)
3115       ;; Save the active value in effect when the group was entered.
3116       (setq gnus-newsgroup-active
3117             (gnus-copy-sequence
3118              (gnus-active gnus-newsgroup-name)))
3119       ;; You can change the summary buffer in some way with this hook.
3120       (gnus-run-hooks 'gnus-select-group-hook)
3121       (gnus-update-format-specifications
3122        nil 'summary 'summary-mode 'summary-dummy)
3123       (gnus-update-summary-mark-positions)
3124       ;; Do score processing.
3125       (when gnus-use-scoring
3126         (gnus-possibly-score-headers))
3127       ;; Check whether to fill in the gaps in the threads.
3128       (when gnus-build-sparse-threads
3129         (gnus-build-sparse-threads))
3130       ;; Find the initial limit.
3131       (if gnus-show-threads
3132           (if show-all
3133               (let ((gnus-newsgroup-dormant nil))
3134                 (gnus-summary-initial-limit show-all))
3135             (gnus-summary-initial-limit show-all))
3136         ;; When untreaded, all articles are always shown.
3137         (setq gnus-newsgroup-limit
3138               (mapcar
3139                (lambda (header) (mail-header-number header))
3140                gnus-newsgroup-headers)))
3141       ;; Generate the summary buffer.
3142       (unless no-display
3143         (gnus-summary-prepare))
3144       (when gnus-use-trees
3145         (gnus-tree-open group)
3146         (setq gnus-summary-highlight-line-function
3147               'gnus-tree-highlight-article))
3148       ;; If the summary buffer is empty, but there are some low-scored
3149       ;; articles or some excluded dormants, we include these in the
3150       ;; buffer.
3151       (when (and (zerop (buffer-size))
3152                  (not no-display))
3153         (cond (gnus-newsgroup-dormant
3154                (gnus-summary-limit-include-dormant))
3155               ((and gnus-newsgroup-scored show-all)
3156                (gnus-summary-limit-include-expunged t))))
3157       ;; Function `gnus-apply-kill-file' must be called in this hook.
3158       (gnus-run-hooks 'gnus-apply-kill-hook)
3159       (if (and (zerop (buffer-size))
3160                (not no-display))
3161           (progn
3162             ;; This newsgroup is empty.
3163             (gnus-summary-catchup-and-exit nil t)
3164             (gnus-message 6 "No unread news")
3165             (when kill-buffer
3166               (gnus-kill-or-deaden-summary kill-buffer))
3167             ;; Return nil from this function.
3168             nil)
3169         ;; Hide conversation thread subtrees.  We cannot do this in
3170         ;; gnus-summary-prepare-hook since kill processing may not
3171         ;; work with hidden articles.
3172         (and gnus-show-threads
3173              gnus-thread-hide-subtree
3174              (gnus-summary-hide-all-threads))
3175         (when kill-buffer
3176           (gnus-kill-or-deaden-summary kill-buffer))
3177         ;; Show first unread article if requested.
3178         (if (and (not no-article)
3179                  (not no-display)
3180                  gnus-newsgroup-unreads
3181                  gnus-auto-select-first)
3182             (progn
3183               (gnus-configure-windows 'summary)
3184               (cond
3185                ((eq gnus-auto-select-first 'best)
3186                 (gnus-summary-best-unread-article))
3187                ((eq gnus-auto-select-first t)
3188                 (gnus-summary-first-unread-article))
3189                ((gnus-functionp gnus-auto-select-first)
3190                 (funcall gnus-auto-select-first))))
3191           ;; Don't select any articles, just move point to the first
3192           ;; article in the group.
3193           (goto-char (point-min))
3194           (gnus-summary-position-point)
3195           (gnus-configure-windows 'summary 'force)
3196           (gnus-set-mode-line 'summary))
3197         (when (get-buffer-window gnus-group-buffer t)
3198           ;; Gotta use windows, because recenter does weird stuff if
3199           ;; the current buffer ain't the displayed window.
3200           (let ((owin (selected-window)))
3201             (select-window (get-buffer-window gnus-group-buffer t))
3202             (when (gnus-group-goto-group group)
3203               (recenter))
3204             (select-window owin)))
3205         ;; Mark this buffer as "prepared".
3206         (setq gnus-newsgroup-prepared t)
3207         (gnus-run-hooks 'gnus-summary-prepared-hook)
3208         t)))))
3209
3210 (defun gnus-summary-prepare ()
3211   "Generate the summary buffer."
3212   (interactive)
3213   (let ((buffer-read-only nil))
3214     (erase-buffer)
3215     (setq gnus-newsgroup-data nil
3216           gnus-newsgroup-data-reverse nil)
3217     (gnus-run-hooks 'gnus-summary-generate-hook)
3218     ;; Generate the buffer, either with threads or without.
3219     (when gnus-newsgroup-headers
3220       (gnus-summary-prepare-threads
3221        (if gnus-show-threads
3222            (gnus-sort-gathered-threads
3223             (funcall gnus-summary-thread-gathering-function
3224                      (gnus-sort-threads
3225                       (gnus-cut-threads (gnus-make-threads)))))
3226          ;; Unthreaded display.
3227          (gnus-sort-articles gnus-newsgroup-headers))))
3228     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3229     ;; Call hooks for modifying summary buffer.
3230     (goto-char (point-min))
3231     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3232
3233 (defsubst gnus-general-simplify-subject (subject)
3234   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3235   (setq subject
3236         (cond
3237          ;; Truncate the subject.
3238          (gnus-simplify-subject-functions
3239           (gnus-map-function gnus-simplify-subject-functions subject))
3240          ((numberp gnus-summary-gather-subject-limit)
3241           (setq subject (gnus-simplify-subject-re subject))
3242           (if (> (length subject) gnus-summary-gather-subject-limit)
3243               (substring subject 0 gnus-summary-gather-subject-limit)
3244             subject))
3245          ;; Fuzzily simplify it.
3246          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3247           (gnus-simplify-subject-fuzzy subject))
3248          ;; Just remove the leading "Re:".
3249          (t
3250           (gnus-simplify-subject-re subject))))
3251
3252   (if (and gnus-summary-gather-exclude-subject
3253            (string-match gnus-summary-gather-exclude-subject subject))
3254       nil                               ; This article shouldn't be gathered
3255     subject))
3256
3257 (defun gnus-summary-simplify-subject-query ()
3258   "Query where the respool algorithm would put this article."
3259   (interactive)
3260   (gnus-summary-select-article)
3261   (message "%s"
3262            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3263
3264 (defun gnus-gather-threads-by-subject (threads)
3265   "Gather threads by looking at Subject headers."
3266   (if (not gnus-summary-make-false-root)
3267       threads
3268     (let ((hashtb (gnus-make-hashtable 1024))
3269           (prev threads)
3270           (result threads)
3271           subject hthread whole-subject)
3272       (while threads
3273         (setq subject (gnus-general-simplify-subject
3274                        (setq whole-subject (mail-header-subject
3275                                             (caar threads)))))
3276         (when subject
3277           (if (setq hthread (gnus-gethash subject hashtb))
3278               (progn
3279                 ;; We enter a dummy root into the thread, if we
3280                 ;; haven't done that already.
3281                 (unless (stringp (caar hthread))
3282                   (setcar hthread (list whole-subject (car hthread))))
3283                 ;; We add this new gathered thread to this gathered
3284                 ;; thread.
3285                 (setcdr (car hthread)
3286                         (nconc (cdar hthread) (list (car threads))))
3287                 ;; Remove it from the list of threads.
3288                 (setcdr prev (cdr threads))
3289                 (setq threads prev))
3290             ;; Enter this thread into the hash table.
3291             (gnus-sethash subject threads hashtb)))
3292         (setq prev threads)
3293         (setq threads (cdr threads)))
3294       result)))
3295
3296 (defun gnus-gather-threads-by-references (threads)
3297   "Gather threads by looking at References headers."
3298   (let ((idhashtb (gnus-make-hashtable 1024))
3299         (thhashtb (gnus-make-hashtable 1024))
3300         (prev threads)
3301         (result threads)
3302         ids references id gthread gid entered ref)
3303     (while threads
3304       (when (setq references (mail-header-references (caar threads)))
3305         (setq id (mail-header-id (caar threads))
3306               ids (gnus-split-references references)
3307               entered nil)
3308         (while (setq ref (pop ids))
3309           (setq ids (delete ref ids))
3310           (if (not (setq gid (gnus-gethash ref idhashtb)))
3311               (progn
3312                 (gnus-sethash ref id idhashtb)
3313                 (gnus-sethash id threads thhashtb))
3314             (setq gthread (gnus-gethash gid thhashtb))
3315             (unless entered
3316               ;; We enter a dummy root into the thread, if we
3317               ;; haven't done that already.
3318               (unless (stringp (caar gthread))
3319                 (setcar gthread (list (mail-header-subject (caar gthread))
3320                                       (car gthread))))
3321               ;; We add this new gathered thread to this gathered
3322               ;; thread.
3323               (setcdr (car gthread)
3324                       (nconc (cdar gthread) (list (car threads)))))
3325             ;; Add it into the thread hash table.
3326             (gnus-sethash id gthread thhashtb)
3327             (setq entered t)
3328             ;; Remove it from the list of threads.
3329             (setcdr prev (cdr threads))
3330             (setq threads prev))))
3331       (setq prev threads)
3332       (setq threads (cdr threads)))
3333     result))
3334
3335 (defun gnus-sort-gathered-threads (threads)
3336   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3337   (let ((result threads))
3338     (while threads
3339       (when (stringp (caar threads))
3340         (setcdr (car threads)
3341                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3342       (setq threads (cdr threads)))
3343     result))
3344
3345 (defun gnus-thread-loop-p (root thread)
3346   "Say whether ROOT is in THREAD."
3347   (let ((stack (list thread))
3348         (infloop 0)
3349         th)
3350     (while (setq thread (pop stack))
3351       (setq th (cdr thread))
3352       (while (and th
3353                   (not (eq (caar th) root)))
3354         (pop th))
3355       (if th
3356           ;; We have found a loop.
3357           (let (ref-dep)
3358             (setcdr thread (delq (car th) (cdr thread)))
3359             (if (boundp (setq ref-dep (intern "none"
3360                                               gnus-newsgroup-dependencies)))
3361                 (setcdr (symbol-value ref-dep)
3362                         (nconc (cdr (symbol-value ref-dep))
3363                                (list (car th))))
3364               (set ref-dep (list nil (car th))))
3365             (setq infloop 1
3366                   stack nil))
3367         ;; Push all the subthreads onto the stack.
3368         (push (cdr thread) stack)))
3369     infloop))
3370
3371 (defun gnus-make-threads ()
3372   "Go through the dependency hashtb and find the roots.  Return all threads."
3373   (let (threads)
3374     (while (catch 'infloop
3375              (mapatoms
3376               (lambda (refs)
3377                 ;; Deal with self-referencing References loops.
3378                 (when (and (car (symbol-value refs))
3379                            (not (zerop
3380                                  (apply
3381                                   '+
3382                                   (mapcar
3383                                    (lambda (thread)
3384                                      (gnus-thread-loop-p
3385                                       (car (symbol-value refs)) thread))
3386                                    (cdr (symbol-value refs)))))))
3387                   (setq threads nil)
3388                   (throw 'infloop t))
3389                 (unless (car (symbol-value refs))
3390                   ;; These threads do not refer back to any other articles,
3391                   ;; so they're roots.
3392                   (setq threads (append (cdr (symbol-value refs)) threads))))
3393               gnus-newsgroup-dependencies)))
3394     threads))
3395
3396 ;; Build the thread tree.
3397 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3398   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3399
3400 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3401 if it was already present.
3402
3403 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3404 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3405 Message-IDs will be renamed be renamed to a unique Message-ID before
3406 being entered.
3407
3408 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3409   (let* ((id (mail-header-id header))
3410          (id-dep (and id (intern id dependencies)))
3411          ref ref-dep ref-header)
3412     ;; Enter this `header' in the `dependencies' table.
3413     (cond
3414      ((not id-dep)
3415       (setq header nil))
3416      ;; The first two cases do the normal part: enter a new `header'
3417      ;; in the `dependencies' table.
3418      ((not (boundp id-dep))
3419       (set id-dep (list header)))
3420      ((null (car (symbol-value id-dep)))
3421       (setcar (symbol-value id-dep) header))
3422
3423      ;; From here the `header' was already present in the
3424      ;; `dependencies' table.
3425      (force-new
3426       ;; Overrides an existing entry;
3427       ;; just set the header part of the entry.
3428       (setcar (symbol-value id-dep) header))
3429
3430      ;; Renames the existing `header' to a unique Message-ID.
3431      ((not gnus-summary-ignore-duplicates)
3432       ;; An article with this Message-ID has already been seen.
3433       ;; We rename the Message-ID.
3434       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3435            (list header))
3436       (mail-header-set-id header id))
3437
3438      ;; The last case ignores an existing entry, except it adds any
3439      ;; additional Xrefs (in case the two articles came from different
3440      ;; servers.
3441      ;; Also sets `header' to `nil' meaning that the `dependencies'
3442      ;; table was *not* modified.
3443      (t
3444       (mail-header-set-xref
3445        (car (symbol-value id-dep))
3446        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3447                    "")
3448                (or (mail-header-xref header) "")))
3449       (setq header nil)))
3450
3451     (when header
3452       ;; First check if that we are not creating a References loop.
3453       (setq ref (gnus-parent-id (mail-header-references header)))
3454       (while (and ref
3455                   (setq ref-dep (intern-soft ref dependencies))
3456                   (boundp ref-dep)
3457                   (setq ref-header (car (symbol-value ref-dep))))
3458         (if (string= id ref)
3459             ;; Yuk!  This is a reference loop.  Make the article be a
3460             ;; root article.
3461             (progn
3462               (mail-header-set-references (car (symbol-value id-dep)) "none")
3463               (setq ref nil))
3464           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3465       (setq ref (gnus-parent-id (mail-header-references header)))
3466       (setq ref-dep (intern (or ref "none") dependencies))
3467       (if (boundp ref-dep)
3468           (setcdr (symbol-value ref-dep)
3469                   (nconc (cdr (symbol-value ref-dep))
3470                          (list (symbol-value id-dep))))
3471         (set ref-dep (list nil (symbol-value id-dep)))))
3472     header))
3473
3474 (defun gnus-build-sparse-threads ()
3475   (let ((headers gnus-newsgroup-headers)
3476         (mail-parse-charset gnus-newsgroup-charset)
3477         (gnus-summary-ignore-duplicates t)
3478         header references generation relations
3479         subject child end new-child date)
3480     ;; First we create an alist of generations/relations, where
3481     ;; generations is how much we trust the relation, and the relation
3482     ;; is parent/child.
3483     (gnus-message 7 "Making sparse threads...")
3484     (save-excursion
3485       (nnheader-set-temp-buffer " *gnus sparse threads*")
3486       (while (setq header (pop headers))
3487         (when (and (setq references (mail-header-references header))
3488                    (not (string= references "")))
3489           (insert references)
3490           (setq child (mail-header-id header)
3491                 subject (mail-header-subject header)
3492                 date (mail-header-date header)
3493                 generation 0)
3494           (while (search-backward ">" nil t)
3495             (setq end (1+ (point)))
3496             (when (search-backward "<" nil t)
3497               (setq new-child (buffer-substring (point) end))
3498               (push (list (incf generation)
3499                           child (setq child new-child)
3500                           subject date)
3501                     relations)))
3502           (when child
3503             (push (list (1+ generation) child nil subject) relations))
3504           (erase-buffer)))
3505       (kill-buffer (current-buffer)))
3506     ;; Sort over trustworthiness.
3507     (mapcar
3508      (lambda (relation)
3509        (when (gnus-dependencies-add-header
3510               (make-full-mail-header-from-decoded-header
3511                gnus-reffed-article-number
3512                (nth 3 relation) "" (or (nth 4 relation) "")
3513                (nth 1 relation)
3514                (or (nth 2 relation) "") 0 0 "")
3515               gnus-newsgroup-dependencies nil)
3516          (push gnus-reffed-article-number gnus-newsgroup-limit)
3517          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3518          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3519                gnus-newsgroup-reads)
3520          (decf gnus-reffed-article-number)))
3521      (sort relations 'car-less-than-car))
3522     (gnus-message 7 "Making sparse threads...done")))
3523
3524 (defun gnus-build-old-threads ()
3525   ;; Look at all the articles that refer back to old articles, and
3526   ;; fetch the headers for the articles that aren't there.  This will
3527   ;; build complete threads - if the roots haven't been expired by the
3528   ;; server, that is.
3529   (let ((mail-parse-charset gnus-newsgroup-charset)
3530         id heads)
3531     (mapatoms
3532      (lambda (refs)
3533        (when (not (car (symbol-value refs)))
3534          (setq heads (cdr (symbol-value refs)))
3535          (while heads
3536            (if (memq (mail-header-number (caar heads))
3537                      gnus-newsgroup-dormant)
3538                (setq heads (cdr heads))
3539              (setq id (symbol-name refs))
3540              (while (and (setq id (gnus-build-get-header id))
3541                          (not (car (gnus-id-to-thread id)))))
3542              (setq heads nil)))))
3543      gnus-newsgroup-dependencies)))
3544
3545 ;; This function has to be called with point after the article number
3546 ;; on the beginning of the line.
3547 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3548   (let ((eol (gnus-point-at-eol))
3549         (buffer (current-buffer))
3550         header)
3551
3552     ;; overview: [num subject from date id refs chars lines misc]
3553     (unwind-protect
3554         (progn
3555           (narrow-to-region (point) eol)
3556           (unless (eobp)
3557             (forward-char))
3558
3559           (setq header
3560                 (make-full-mail-header
3561                  number                         ; number
3562                  (nnheader-nov-field)           ; subject
3563                  (nnheader-nov-field)           ; from
3564                  (nnheader-nov-field)           ; date
3565                  (nnheader-nov-read-message-id) ; id
3566                  (nnheader-nov-field)           ; refs
3567                  (nnheader-nov-read-integer)    ; chars
3568                  (nnheader-nov-read-integer)    ; lines
3569                  (unless (eobp)
3570                    (if (looking-at "Xref: ")
3571                        (goto-char (match-end 0)))
3572                    (nnheader-nov-field))        ; Xref
3573                  (nnheader-nov-parse-extra))))  ; extra
3574
3575       (widen))
3576
3577     (when gnus-alter-header-function
3578       (funcall gnus-alter-header-function header))
3579     (gnus-dependencies-add-header header dependencies force-new)))
3580
3581 (defun gnus-build-get-header (id)
3582   "Look through the buffer of NOV lines and find the header to ID.
3583 Enter this line into the dependencies hash table, and return
3584 the id of the parent article (if any)."
3585   (let ((deps gnus-newsgroup-dependencies)
3586         found header)
3587     (prog1
3588         (save-excursion
3589           (set-buffer nntp-server-buffer)
3590           (let ((case-fold-search nil))
3591             (goto-char (point-min))
3592             (while (and (not found)
3593                         (search-forward id nil t))
3594               (beginning-of-line)
3595               (setq found (looking-at
3596                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3597                                    (regexp-quote id))))
3598               (or found (beginning-of-line 2)))
3599             (when found
3600               (beginning-of-line)
3601               (and
3602                (setq header (gnus-nov-parse-line
3603                              (read (current-buffer)) deps))
3604                (gnus-parent-id (mail-header-references header))))))
3605       (when header
3606         (let ((number (mail-header-number header)))
3607           (push number gnus-newsgroup-limit)
3608           (push header gnus-newsgroup-headers)
3609           (if (memq number gnus-newsgroup-unselected)
3610               (progn
3611                 (push number gnus-newsgroup-unreads)
3612                 (setq gnus-newsgroup-unselected
3613                       (delq number gnus-newsgroup-unselected)))
3614             (push number gnus-newsgroup-ancient)))))))
3615
3616 (defun gnus-build-all-threads ()
3617   "Read all the headers."
3618   (let ((gnus-summary-ignore-duplicates t)
3619         (mail-parse-charset gnus-newsgroup-charset)
3620         (dependencies gnus-newsgroup-dependencies)
3621         header article)
3622     (save-excursion
3623       (set-buffer nntp-server-buffer)
3624       (let ((case-fold-search nil))
3625         (goto-char (point-min))
3626         (while (not (eobp))
3627           (ignore-errors
3628             (setq article (read (current-buffer))
3629                   header (gnus-nov-parse-line article dependencies)))
3630           (when header
3631             (save-excursion
3632               (set-buffer gnus-summary-buffer)
3633               (push header gnus-newsgroup-headers)
3634               (if (memq (setq article (mail-header-number header))
3635                         gnus-newsgroup-unselected)
3636                   (progn
3637                     (push article gnus-newsgroup-unreads)
3638                     (setq gnus-newsgroup-unselected
3639                           (delq article gnus-newsgroup-unselected)))
3640                 (push article gnus-newsgroup-ancient)))
3641             (forward-line 1)))))))
3642
3643 (defun gnus-summary-update-article-line (article header)
3644   "Update the line for ARTICLE using HEADERS."
3645   (let* ((id (mail-header-id header))
3646          (thread (gnus-id-to-thread id)))
3647     (unless thread
3648       (error "Article in no thread"))
3649     ;; Update the thread.
3650     (setcar thread header)
3651     (gnus-summary-goto-subject article)
3652     (let* ((datal (gnus-data-find-list article))
3653            (data (car datal))
3654            (length (when (cdr datal)
3655                      (- (gnus-data-pos data)
3656                         (gnus-data-pos (cadr datal)))))
3657            (buffer-read-only nil)
3658            (level (gnus-summary-thread-level)))
3659       (gnus-delete-line)
3660       (gnus-summary-insert-line
3661        header level nil (gnus-article-mark article)
3662        (memq article gnus-newsgroup-replied)
3663        (memq article gnus-newsgroup-expirable)
3664        ;; Only insert the Subject string when it's different
3665        ;; from the previous Subject string.
3666        (if (and
3667             gnus-show-threads
3668             (gnus-subject-equal
3669              (condition-case ()
3670                  (mail-header-subject
3671                   (gnus-data-header
3672                    (cadr
3673                     (gnus-data-find-list
3674                      article
3675                      (gnus-data-list t)))))
3676                ;; Error on the side of excessive subjects.
3677                (error ""))
3678              (mail-header-subject header)))
3679            ""
3680          (mail-header-subject header))
3681        nil (cdr (assq article gnus-newsgroup-scored))
3682        (memq article gnus-newsgroup-processable))
3683       (when length
3684         (gnus-data-update-list
3685          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3686
3687 (defun gnus-summary-update-article (article &optional iheader)
3688   "Update ARTICLE in the summary buffer."
3689   (set-buffer gnus-summary-buffer)
3690   (let* ((header (gnus-summary-article-header article))
3691          (id (mail-header-id header))
3692          (data (gnus-data-find article))
3693          (thread (gnus-id-to-thread id))
3694          (references (mail-header-references header))
3695          (parent
3696           (gnus-id-to-thread
3697            (or (gnus-parent-id
3698                 (when (and references
3699                            (not (equal "" references)))
3700                   references))
3701                "none")))
3702          (buffer-read-only nil)
3703          (old (car thread)))
3704     (when thread
3705       (unless iheader
3706         (setcar thread nil)
3707         (when parent
3708           (delq thread parent)))
3709       (if (gnus-summary-insert-subject id header)
3710           ;; Set the (possibly) new article number in the data structure.
3711           (gnus-data-set-number data (gnus-id-to-article id))
3712         (setcar thread old)
3713         nil))))
3714
3715 (defun gnus-rebuild-thread (id &optional line)
3716   "Rebuild the thread containing ID.
3717 If LINE, insert the rebuilt thread starting on line LINE."
3718   (let ((buffer-read-only nil)
3719         old-pos current thread data)
3720     (if (not gnus-show-threads)
3721         (setq thread (list (car (gnus-id-to-thread id))))
3722       ;; Get the thread this article is part of.
3723       (setq thread (gnus-remove-thread id)))
3724     (setq old-pos (gnus-point-at-bol))
3725     (setq current (save-excursion
3726                     (and (zerop (forward-line -1))
3727                          (gnus-summary-article-number))))
3728     ;; If this is a gathered thread, we have to go some re-gathering.
3729     (when (stringp (car thread))
3730       (let ((subject (car thread))
3731             roots thr)
3732         (setq thread (cdr thread))
3733         (while thread
3734           (unless (memq (setq thr (gnus-id-to-thread
3735                                    (gnus-root-id
3736                                     (mail-header-id (caar thread)))))
3737                         roots)
3738             (push thr roots))
3739           (setq thread (cdr thread)))
3740         ;; We now have all (unique) roots.
3741         (if (= (length roots) 1)
3742             ;; All the loose roots are now one solid root.
3743             (setq thread (car roots))
3744           (setq thread (cons subject (gnus-sort-threads roots))))))
3745     (let (threads)
3746       ;; We then insert this thread into the summary buffer.
3747       (when line
3748         (goto-char (point-min))
3749         (forward-line (1- line)))
3750       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3751         (if gnus-show-threads
3752             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3753           (gnus-summary-prepare-unthreaded thread))
3754         (setq data (nreverse gnus-newsgroup-data))
3755         (setq threads gnus-newsgroup-threads))
3756       ;; We splice the new data into the data structure.
3757       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3758       ;;!!! then we want to insert at the beginning of the buffer.
3759       ;;!!! That happens to be true with Gnus now, but that may
3760       ;;!!! change in the future.  Perhaps.
3761       (gnus-data-enter-list
3762        (if line nil current) data (- (point) old-pos))
3763       (setq gnus-newsgroup-threads
3764             (nconc threads gnus-newsgroup-threads))
3765       (gnus-data-compute-positions))))
3766
3767 (defun gnus-number-to-header (number)
3768   "Return the header for article NUMBER."
3769   (let ((headers gnus-newsgroup-headers))
3770     (while (and headers
3771                 (not (= number (mail-header-number (car headers)))))
3772       (pop headers))
3773     (when headers
3774       (car headers))))
3775
3776 (defun gnus-parent-headers (in-headers &optional generation)
3777   "Return the headers of the GENERATIONeth parent of HEADERS."
3778   (unless generation
3779     (setq generation 1))
3780   (let ((parent t)
3781         (headers in-headers)
3782         references)
3783     (while (and parent
3784                 (not (zerop generation))
3785                 (setq references (mail-header-references headers)))
3786       (setq headers (if (and references
3787                              (setq parent (gnus-parent-id references)))
3788                         (car (gnus-id-to-thread parent))
3789                       nil))
3790       (decf generation))
3791     (and (not (eq headers in-headers))
3792          headers)))
3793
3794 (defun gnus-id-to-thread (id)
3795   "Return the (sub-)thread where ID appears."
3796   (gnus-gethash id gnus-newsgroup-dependencies))
3797
3798 (defun gnus-id-to-article (id)
3799   "Return the article number of ID."
3800   (let ((thread (gnus-id-to-thread id)))
3801     (when (and thread
3802                (car thread))
3803       (mail-header-number (car thread)))))
3804
3805 (defun gnus-id-to-header (id)
3806   "Return the article headers of ID."
3807   (car (gnus-id-to-thread id)))
3808
3809 (defun gnus-article-displayed-root-p (article)
3810   "Say whether ARTICLE is a root(ish) article."
3811   (let ((level (gnus-summary-thread-level article))
3812         (refs (mail-header-references  (gnus-summary-article-header article)))
3813         particle)
3814     (cond
3815      ((null level) nil)
3816      ((zerop level) t)
3817      ((null refs) t)
3818      ((null (gnus-parent-id refs)) t)
3819      ((and (= 1 level)
3820            (null (setq particle (gnus-id-to-article
3821                                  (gnus-parent-id refs))))
3822            (null (gnus-summary-thread-level particle)))))))
3823
3824 (defun gnus-root-id (id)
3825   "Return the id of the root of the thread where ID appears."
3826   (let (last-id prev)
3827     (while (and id (setq prev (car (gnus-id-to-thread id))))
3828       (setq last-id id
3829             id (gnus-parent-id (mail-header-references prev))))
3830     last-id))
3831
3832 (defun gnus-articles-in-thread (thread)
3833   "Return the list of articles in THREAD."
3834   (cons (mail-header-number (car thread))
3835         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3836
3837 (defun gnus-remove-thread (id &optional dont-remove)
3838   "Remove the thread that has ID in it."
3839   (let (headers thread last-id)
3840     ;; First go up in this thread until we find the root.
3841     (setq last-id (gnus-root-id id)
3842           headers (message-flatten-list (gnus-id-to-thread last-id)))
3843     ;; We have now found the real root of this thread.  It might have
3844     ;; been gathered into some loose thread, so we have to search
3845     ;; through the threads to find the thread we wanted.
3846     (let ((threads gnus-newsgroup-threads)
3847           sub)
3848       (while threads
3849         (setq sub (car threads))
3850         (if (stringp (car sub))
3851             ;; This is a gathered thread, so we look at the roots
3852             ;; below it to find whether this article is in this
3853             ;; gathered root.
3854             (progn
3855               (setq sub (cdr sub))
3856               (while sub
3857                 (when (member (caar sub) headers)
3858                   (setq thread (car threads)
3859                         threads nil
3860                         sub nil))
3861                 (setq sub (cdr sub))))
3862           ;; It's an ordinary thread, so we check it.
3863           (when (eq (car sub) (car headers))
3864             (setq thread sub
3865                   threads nil)))
3866         (setq threads (cdr threads)))
3867       ;; If this article is in no thread, then it's a root.
3868       (if thread
3869           (unless dont-remove
3870             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3871         (setq thread (gnus-id-to-thread last-id)))
3872       (when thread
3873         (prog1
3874             thread                      ; We return this thread.
3875           (unless dont-remove
3876             (if (stringp (car thread))
3877                 (progn
3878                   ;; If we use dummy roots, then we have to remove the
3879                   ;; dummy root as well.
3880                   (when (eq gnus-summary-make-false-root 'dummy)
3881                     ;; We go to the dummy root by going to
3882                     ;; the first sub-"thread", and then one line up.
3883                     (gnus-summary-goto-article
3884                      (mail-header-number (caadr thread)))
3885                     (forward-line -1)
3886                     (gnus-delete-line)
3887                     (gnus-data-compute-positions))
3888                   (setq thread (cdr thread))
3889                   (while thread
3890                     (gnus-remove-thread-1 (car thread))
3891                     (setq thread (cdr thread))))
3892               (gnus-remove-thread-1 thread))))))))
3893
3894 (defun gnus-remove-thread-1 (thread)
3895   "Remove the thread THREAD recursively."
3896   (let ((number (mail-header-number (pop thread)))
3897         d)
3898     (setq thread (reverse thread))
3899     (while thread
3900       (gnus-remove-thread-1 (pop thread)))
3901     (when (setq d (gnus-data-find number))
3902       (goto-char (gnus-data-pos d))
3903       (gnus-summary-show-thread)
3904       (gnus-data-remove
3905        number
3906        (- (gnus-point-at-bol)
3907           (prog1
3908               (1+ (gnus-point-at-eol))
3909             (gnus-delete-line)))))))
3910
3911 (defun gnus-sort-threads-1 (threads func)
3912   (sort (mapcar (lambda (thread)
3913                   (cons (car thread)
3914                         (and (cdr thread)
3915                              (gnus-sort-threads-1 (cdr thread) func))))
3916                 threads) func))
3917
3918 (defun gnus-sort-threads (threads)
3919   "Sort THREADS."
3920   (if (not gnus-thread-sort-functions)
3921       threads
3922     (gnus-message 8 "Sorting threads...")
3923     (prog1
3924         (gnus-sort-threads-1
3925          threads
3926          (gnus-make-sort-function gnus-thread-sort-functions))
3927       (gnus-message 8 "Sorting threads...done"))))
3928
3929 (defun gnus-sort-articles (articles)
3930   "Sort ARTICLES."
3931   (when gnus-article-sort-functions
3932     (gnus-message 7 "Sorting articles...")
3933     (prog1
3934         (setq gnus-newsgroup-headers
3935               (sort articles (gnus-make-sort-function
3936                               gnus-article-sort-functions)))
3937       (gnus-message 7 "Sorting articles...done"))))
3938
3939 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3940 (defmacro gnus-thread-header (thread)
3941   "Return header of first article in THREAD.
3942 Note that THREAD must never, ever be anything else than a variable -
3943 using some other form will lead to serious barfage."
3944   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3945   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3946   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3947         (vector thread) 2))
3948
3949 (defsubst gnus-article-sort-by-number (h1 h2)
3950   "Sort articles by article number."
3951   (< (mail-header-number h1)
3952      (mail-header-number h2)))
3953
3954 (defun gnus-thread-sort-by-number (h1 h2)
3955   "Sort threads by root article number."
3956   (gnus-article-sort-by-number
3957    (gnus-thread-header h1) (gnus-thread-header h2)))
3958
3959 (defsubst gnus-article-sort-by-lines (h1 h2)
3960   "Sort articles by article Lines header."
3961   (< (mail-header-lines h1)
3962      (mail-header-lines h2)))
3963
3964 (defun gnus-thread-sort-by-lines (h1 h2)
3965   "Sort threads by root article Lines header."
3966   (gnus-article-sort-by-lines
3967    (gnus-thread-header h1) (gnus-thread-header h2)))
3968
3969 (defsubst gnus-article-sort-by-chars (h1 h2)
3970   "Sort articles by octet length."
3971   (< (mail-header-chars h1)
3972      (mail-header-chars h2)))
3973
3974 (defun gnus-thread-sort-by-chars (h1 h2)
3975   "Sort threads by root article octet length."
3976   (gnus-article-sort-by-chars
3977    (gnus-thread-header h1) (gnus-thread-header h2)))
3978
3979 (defsubst gnus-article-sort-by-author (h1 h2)
3980   "Sort articles by root author."
3981   (string-lessp
3982    (let ((addr (car (mime-entity-read-field h1 'From))))
3983      (or (std11-full-name-string addr)
3984          (std11-address-string addr)
3985          ""))
3986    (let ((addr (car (mime-entity-read-field h2 'From))))
3987      (or (std11-full-name-string addr)
3988          (std11-address-string addr)
3989          ""))
3990    ))
3991
3992 (defun gnus-thread-sort-by-author (h1 h2)
3993   "Sort threads by root author."
3994   (gnus-article-sort-by-author
3995    (gnus-thread-header h1)  (gnus-thread-header h2)))
3996
3997 (defsubst gnus-article-sort-by-subject (h1 h2)
3998   "Sort articles by root subject."
3999   (string-lessp
4000    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4001    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4002
4003 (defun gnus-thread-sort-by-subject (h1 h2)
4004   "Sort threads by root subject."
4005   (gnus-article-sort-by-subject
4006    (gnus-thread-header h1) (gnus-thread-header h2)))
4007
4008 (defsubst gnus-article-sort-by-date (h1 h2)
4009   "Sort articles by root article date."
4010   (time-less-p
4011    (gnus-date-get-time (mail-header-date h1))
4012    (gnus-date-get-time (mail-header-date h2))))
4013
4014 (defun gnus-thread-sort-by-date (h1 h2)
4015   "Sort threads by root article date."
4016   (gnus-article-sort-by-date
4017    (gnus-thread-header h1) (gnus-thread-header h2)))
4018
4019 (defsubst gnus-article-sort-by-score (h1 h2)
4020   "Sort articles by root article score.
4021 Unscored articles will be counted as having a score of zero."
4022   (> (or (cdr (assq (mail-header-number h1)
4023                     gnus-newsgroup-scored))
4024          gnus-summary-default-score 0)
4025      (or (cdr (assq (mail-header-number h2)
4026                     gnus-newsgroup-scored))
4027          gnus-summary-default-score 0)))
4028
4029 (defun gnus-thread-sort-by-score (h1 h2)
4030   "Sort threads by root article score."
4031   (gnus-article-sort-by-score
4032    (gnus-thread-header h1) (gnus-thread-header h2)))
4033
4034 (defun gnus-thread-sort-by-total-score (h1 h2)
4035   "Sort threads by the sum of all scores in the thread.
4036 Unscored articles will be counted as having a score of zero."
4037   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4038
4039 (defun gnus-thread-total-score (thread)
4040   ;; This function find the total score of THREAD.
4041   (cond ((null thread)
4042          0)
4043         ((consp thread)
4044          (if (stringp (car thread))
4045              (apply gnus-thread-score-function 0
4046                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4047            (gnus-thread-total-score-1 thread)))
4048         (t
4049          (gnus-thread-total-score-1 (list thread)))))
4050
4051 (defun gnus-thread-total-score-1 (root)
4052   ;; This function find the total score of the thread below ROOT.
4053   (setq root (car root))
4054   (apply gnus-thread-score-function
4055          (or (append
4056               (mapcar 'gnus-thread-total-score
4057                       (cdr (gnus-id-to-thread (mail-header-id root))))
4058               (when (> (mail-header-number root) 0)
4059                 (list (or (cdr (assq (mail-header-number root)
4060                                      gnus-newsgroup-scored))
4061                           gnus-summary-default-score 0))))
4062              (list gnus-summary-default-score)
4063              '(0))))
4064
4065 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4066 (defvar gnus-tmp-prev-subject nil)
4067 (defvar gnus-tmp-false-parent nil)
4068 (defvar gnus-tmp-root-expunged nil)
4069 (defvar gnus-tmp-dummy-line nil)
4070
4071 (eval-when-compile (defvar gnus-tmp-header))
4072 (defun gnus-extra-header (type &optional header)
4073   "Return the extra header of TYPE."
4074   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4075       ""))
4076
4077 (defun gnus-summary-prepare-threads (threads)
4078   "Prepare summary buffer from THREADS and indentation LEVEL.
4079 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4080 or a straight list of headers."
4081   (gnus-message 7 "Generating summary...")
4082
4083   (setq gnus-newsgroup-threads threads)
4084   (beginning-of-line)
4085
4086   (let ((gnus-tmp-level 0)
4087         (default-score (or gnus-summary-default-score 0))
4088         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4089         thread number subject stack state gnus-tmp-gathered beg-match
4090         new-roots gnus-tmp-new-adopts thread-end
4091         gnus-tmp-header gnus-tmp-unread
4092         gnus-tmp-replied gnus-tmp-subject-or-nil
4093         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4094         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4095         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4096
4097     (setq gnus-tmp-prev-subject nil)
4098
4099     (if (vectorp (car threads))
4100         ;; If this is a straight (sic) list of headers, then a
4101         ;; threaded summary display isn't required, so we just create
4102         ;; an unthreaded one.
4103         (gnus-summary-prepare-unthreaded threads)
4104
4105       ;; Do the threaded display.
4106
4107       (while (or threads stack gnus-tmp-new-adopts new-roots)
4108
4109         (if (and (= gnus-tmp-level 0)
4110                  (or (not stack)
4111                      (= (caar stack) 0))
4112                  (not gnus-tmp-false-parent)
4113                  (or gnus-tmp-new-adopts new-roots))
4114             (if gnus-tmp-new-adopts
4115                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4116                       thread (list (car gnus-tmp-new-adopts))
4117                       gnus-tmp-header (caar thread)
4118                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4119               (when new-roots
4120                 (setq thread (list (car new-roots))
4121                       gnus-tmp-header (caar thread)
4122                       new-roots (cdr new-roots))))
4123
4124           (if threads
4125               ;; If there are some threads, we do them before the
4126               ;; threads on the stack.
4127               (setq thread threads
4128                     gnus-tmp-header (caar thread))
4129             ;; There were no current threads, so we pop something off
4130             ;; the stack.
4131             (setq state (car stack)
4132                   gnus-tmp-level (car state)
4133                   thread (cdr state)
4134                   stack (cdr stack)
4135                   gnus-tmp-header (caar thread))))
4136
4137         (setq gnus-tmp-false-parent nil)
4138         (setq gnus-tmp-root-expunged nil)
4139         (setq thread-end nil)
4140
4141         (if (stringp gnus-tmp-header)
4142             ;; The header is a dummy root.
4143             (cond
4144              ((eq gnus-summary-make-false-root 'adopt)
4145               ;; We let the first article adopt the rest.
4146               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4147                                                (cddar thread)))
4148               (setq gnus-tmp-gathered
4149                     (nconc (mapcar
4150                             (lambda (h) (mail-header-number (car h)))
4151                             (cddar thread))
4152                            gnus-tmp-gathered))
4153               (setq thread (cons (list (caar thread)
4154                                        (cadar thread))
4155                                  (cdr thread)))
4156               (setq gnus-tmp-level -1
4157                     gnus-tmp-false-parent t))
4158              ((eq gnus-summary-make-false-root 'empty)
4159               ;; We print adopted articles with empty subject fields.
4160               (setq gnus-tmp-gathered
4161                     (nconc (mapcar
4162                             (lambda (h) (mail-header-number (car h)))
4163                             (cddar thread))
4164                            gnus-tmp-gathered))
4165               (setq gnus-tmp-level -1))
4166              ((eq gnus-summary-make-false-root 'dummy)
4167               ;; We remember that we probably want to output a dummy
4168               ;; root.
4169               (setq gnus-tmp-dummy-line gnus-tmp-header)
4170               (setq gnus-tmp-prev-subject gnus-tmp-header))
4171              (t
4172               ;; We do not make a root for the gathered
4173               ;; sub-threads at all.
4174               (setq gnus-tmp-level -1)))
4175
4176           (setq number (mail-header-number gnus-tmp-header)
4177                 subject (mail-header-subject gnus-tmp-header))
4178
4179           (cond
4180            ;; If the thread has changed subject, we might want to make
4181            ;; this subthread into a root.
4182            ((and (null gnus-thread-ignore-subject)
4183                  (not (zerop gnus-tmp-level))
4184                  gnus-tmp-prev-subject
4185                  (not (inline
4186                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4187             (setq new-roots (nconc new-roots (list (car thread)))
4188                   thread-end t
4189                   gnus-tmp-header nil))
4190            ;; If the article lies outside the current limit,
4191            ;; then we do not display it.
4192            ((not (memq number gnus-newsgroup-limit))
4193             (setq gnus-tmp-gathered
4194                   (nconc (mapcar
4195                           (lambda (h) (mail-header-number (car h)))
4196                           (cdar thread))
4197                          gnus-tmp-gathered))
4198             (setq gnus-tmp-new-adopts (if (cdar thread)
4199                                           (append gnus-tmp-new-adopts
4200                                                   (cdar thread))
4201                                         gnus-tmp-new-adopts)
4202                   thread-end t
4203                   gnus-tmp-header nil)
4204             (when (zerop gnus-tmp-level)
4205               (setq gnus-tmp-root-expunged t)))
4206            ;; Perhaps this article is to be marked as read?
4207            ((and gnus-summary-mark-below
4208                  (< (or (cdr (assq number gnus-newsgroup-scored))
4209                         default-score)
4210                     gnus-summary-mark-below)
4211                  ;; Don't touch sparse articles.
4212                  (not (gnus-summary-article-sparse-p number))
4213                  (not (gnus-summary-article-ancient-p number)))
4214             (setq gnus-newsgroup-unreads
4215                   (delq number gnus-newsgroup-unreads))
4216             (if gnus-newsgroup-auto-expire
4217                 (push number gnus-newsgroup-expirable)
4218               (push (cons number gnus-low-score-mark)
4219                     gnus-newsgroup-reads))))
4220
4221           (when gnus-tmp-header
4222             ;; We may have an old dummy line to output before this
4223             ;; article.
4224             (when (and gnus-tmp-dummy-line
4225                        (gnus-subject-equal
4226                         gnus-tmp-dummy-line
4227                         (mail-header-subject gnus-tmp-header)))
4228               (gnus-summary-insert-dummy-line
4229                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4230               (setq gnus-tmp-dummy-line nil))
4231
4232             ;; Compute the mark.
4233             (setq gnus-tmp-unread (gnus-article-mark number))
4234
4235             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4236                                   gnus-tmp-header gnus-tmp-level)
4237                   gnus-newsgroup-data)
4238
4239             ;; Actually insert the line.
4240             (setq
4241              gnus-tmp-subject-or-nil
4242              (cond
4243               ((and gnus-thread-ignore-subject
4244                     gnus-tmp-prev-subject
4245                     (not (inline (gnus-subject-equal
4246                                   gnus-tmp-prev-subject subject))))
4247                subject)
4248               ((zerop gnus-tmp-level)
4249                (if (and (eq gnus-summary-make-false-root 'empty)
4250                         (memq number gnus-tmp-gathered)
4251                         gnus-tmp-prev-subject
4252                         (inline (gnus-subject-equal
4253                                  gnus-tmp-prev-subject subject)))
4254                    gnus-summary-same-subject
4255                  subject))
4256               (t gnus-summary-same-subject)))
4257             (if (and (eq gnus-summary-make-false-root 'adopt)
4258                      (= gnus-tmp-level 1)
4259                      (memq number gnus-tmp-gathered))
4260                 (setq gnus-tmp-opening-bracket ?\<
4261                       gnus-tmp-closing-bracket ?\>)
4262               (setq gnus-tmp-opening-bracket ?\[
4263                     gnus-tmp-closing-bracket ?\]))
4264             (setq
4265              gnus-tmp-indentation
4266              (aref gnus-thread-indent-array gnus-tmp-level)
4267              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4268              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4269                                 gnus-summary-default-score 0)
4270              gnus-tmp-score-char
4271              (if (or (null gnus-summary-default-score)
4272                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4273                          gnus-summary-zcore-fuzz))
4274                  ?  ;Whitespace
4275                (if (< gnus-tmp-score gnus-summary-default-score)
4276                    gnus-score-below-mark gnus-score-over-mark))
4277              gnus-tmp-replied
4278              (cond ((memq number gnus-newsgroup-processable)
4279                     gnus-process-mark)
4280                    ((memq number gnus-newsgroup-cached)
4281                     gnus-cached-mark)
4282                    ((memq number gnus-newsgroup-replied)
4283                     gnus-replied-mark)
4284                    ((memq number gnus-newsgroup-forwarded)
4285                     gnus-forwarded-mark)
4286                    ((memq number gnus-newsgroup-saved)
4287                     gnus-saved-mark)
4288                    (t gnus-no-mark))
4289              gnus-tmp-from (mail-header-from gnus-tmp-header)
4290              gnus-tmp-name
4291              (cond
4292               ((string-match "<[^>]+> *$" gnus-tmp-from)
4293                (setq beg-match (match-beginning 0))
4294                (or (and (string-match "^\".+\"" gnus-tmp-from)
4295                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4296                    (substring gnus-tmp-from 0 beg-match)))
4297               ((string-match "(.+)" gnus-tmp-from)
4298                (substring gnus-tmp-from
4299                           (1+ (match-beginning 0)) (1- (match-end 0))))
4300               (t gnus-tmp-from)))
4301             (when (string= gnus-tmp-name "")
4302               (setq gnus-tmp-name gnus-tmp-from))
4303             (unless (numberp gnus-tmp-lines)
4304               (setq gnus-tmp-lines -1))
4305             (when (= gnus-tmp-lines -1)
4306               (setq gnus-tmp-lines "?"))
4307             (gnus-put-text-property
4308              (point)
4309              (progn (eval gnus-summary-line-format-spec) (point))
4310              'gnus-number number)
4311             (when gnus-visual-p
4312               (forward-line -1)
4313               (gnus-run-hooks 'gnus-summary-update-hook)
4314               (forward-line 1))
4315
4316             (setq gnus-tmp-prev-subject subject)))
4317
4318         (when (nth 1 thread)
4319           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4320         (incf gnus-tmp-level)
4321         (setq threads (if thread-end nil (cdar thread)))
4322         (unless threads
4323           (setq gnus-tmp-level 0)))))
4324   (gnus-message 7 "Generating summary...done"))
4325
4326 (defun gnus-summary-prepare-unthreaded (headers)
4327   "Generate an unthreaded summary buffer based on HEADERS."
4328   (let (header number mark)
4329
4330     (beginning-of-line)
4331
4332     (while headers
4333       ;; We may have to root out some bad articles...
4334       (when (memq (setq number (mail-header-number
4335                                 (setq header (pop headers))))
4336                   gnus-newsgroup-limit)
4337         ;; Mark article as read when it has a low score.
4338         (when (and gnus-summary-mark-below
4339                    (< (or (cdr (assq number gnus-newsgroup-scored))
4340                           gnus-summary-default-score 0)
4341                       gnus-summary-mark-below)
4342                    (not (gnus-summary-article-ancient-p number)))
4343           (setq gnus-newsgroup-unreads
4344                 (delq number gnus-newsgroup-unreads))
4345           (if gnus-newsgroup-auto-expire
4346               (push number gnus-newsgroup-expirable)
4347             (push (cons number gnus-low-score-mark)
4348                   gnus-newsgroup-reads)))
4349
4350         (setq mark (gnus-article-mark number))
4351         (push (gnus-data-make number mark (1+ (point)) header 0)
4352               gnus-newsgroup-data)
4353         (gnus-summary-insert-line
4354          header 0 number
4355          mark (memq number gnus-newsgroup-replied)
4356          (memq number gnus-newsgroup-expirable)
4357          (mail-header-subject header) nil
4358          (cdr (assq number gnus-newsgroup-scored))
4359          (memq number gnus-newsgroup-processable))))))
4360
4361 (defun gnus-summary-remove-list-identifiers ()
4362   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4363   (let ((regexp (if (consp gnus-list-identifiers)
4364                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4365                   gnus-list-identifiers))
4366         changed subject)
4367     (when regexp
4368       (dolist (header gnus-newsgroup-headers)
4369         (setq subject (mail-header-subject header)
4370               changed nil)
4371         (while (string-match
4372                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4373                 subject)
4374           (setq subject
4375                 (concat (substring subject 0 (match-beginning 2))
4376                         (substring subject (match-end 0)))
4377                 changed t))
4378         (when (and changed
4379                    (string-match
4380                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4381           (setq subject
4382                 (concat (substring subject 0 (match-beginning 1))
4383                         (substring subject (match-end 1)))))
4384         (when changed
4385           (mail-header-set-subject header subject))))))
4386
4387 (defun gnus-fetch-headers (articles)
4388   "Fetch headers of ARTICLES."
4389   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4390     (gnus-message 5 "Fetching headers for %s..." name)
4391     (prog1
4392         ;;;!!! FIXME: temporary fix for an infloop on nnimap.
4393         (if (eq 'nnimap (car (gnus-find-method-for-group name)))
4394             (if (eq 'nov
4395                     (setq gnus-headers-retrieved-by
4396                           (gnus-retrieve-headers
4397                            articles gnus-newsgroup-name
4398                            ;; We might want to fetch old headers, but
4399                            ;; not if there is only 1 article.
4400                            (and (or (and
4401                                      (not (eq gnus-fetch-old-headers 'some))
4402                                      (not (numberp gnus-fetch-old-headers)))
4403                                     (> (length articles) 1))
4404                                 gnus-fetch-old-headers))))
4405                 (gnus-get-newsgroup-headers-xover
4406                  articles nil nil gnus-newsgroup-name t)
4407               (gnus-get-newsgroup-headers))
4408           (gnus-retrieve-parsed-headers
4409            articles gnus-newsgroup-name
4410            ;; We might want to fetch old headers, but
4411            ;; not if there is only 1 article.
4412            (and (or (and (not (eq gnus-fetch-old-headers 'some))
4413                          (not (numberp gnus-fetch-old-headers)))
4414                     (> (length articles) 1))
4415                 gnus-fetch-old-headers)))
4416       (gnus-message 5 "Fetching headers for %s...done" name))))
4417
4418 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4419   "Select newsgroup GROUP.
4420 If READ-ALL is non-nil, all articles in the group are selected.
4421 If SELECT-ARTICLES, only select those articles from GROUP."
4422   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4423          ;;!!! Dirty hack; should be removed.
4424          (gnus-summary-ignore-duplicates
4425           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4426               t
4427             gnus-summary-ignore-duplicates))
4428          (info (nth 2 entry))
4429          articles fetched-articles cached)
4430
4431     (unless (gnus-check-server
4432              (setq gnus-current-select-method
4433                    (gnus-find-method-for-group group)))
4434       (error "Couldn't open server"))
4435
4436     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4437         (gnus-activate-group group)     ; Or we can activate it...
4438         (progn                          ; Or we bug out.
4439           (when (equal major-mode 'gnus-summary-mode)
4440             (kill-buffer (current-buffer)))
4441           (error "Couldn't activate group %s: %s"
4442                  group (gnus-status-message group))))
4443
4444     (unless (gnus-request-group group t)
4445       (when (equal major-mode 'gnus-summary-mode)
4446         (kill-buffer (current-buffer)))
4447       (error "Couldn't request group %s: %s"
4448              group (gnus-status-message group)))
4449
4450     (setq gnus-newsgroup-name group)
4451     (setq gnus-newsgroup-unselected nil)
4452     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4453     (gnus-summary-setup-default-charset)
4454
4455     ;; Adjust and set lists of article marks.
4456     (when info
4457       (gnus-adjust-marked-articles info))
4458
4459     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4460     (setq cached
4461           (if (gnus-virtual-group-p group)
4462               gnus-newsgroup-cached
4463             (gnus-cache-articles-in-group group)))
4464
4465     (setq gnus-newsgroup-unreads
4466           (gnus-set-difference
4467            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4468            gnus-newsgroup-dormant))
4469
4470     (setq gnus-newsgroup-processable nil)
4471
4472     (gnus-update-read-articles group gnus-newsgroup-unreads)
4473
4474     (if (setq articles select-articles)
4475         (setq gnus-newsgroup-unselected
4476               (gnus-sorted-intersection
4477                gnus-newsgroup-unreads
4478                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4479       (setq articles (gnus-articles-to-read group read-all)))
4480
4481     (cond
4482      ((null articles)
4483       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4484       'quit)
4485      ((eq articles 0) nil)
4486      (t
4487       ;; Init the dependencies hash table.
4488       (setq gnus-newsgroup-dependencies
4489             (gnus-make-hashtable (length articles)))
4490       (gnus-set-global-variables)
4491       ;; Retrieve the headers and read them in.
4492       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4493
4494       ;; Suppress duplicates?
4495       (when gnus-suppress-duplicates
4496         (gnus-dup-suppress-articles))
4497
4498       ;; Set the initial limit.
4499       (setq gnus-newsgroup-limit (copy-sequence articles))
4500       ;; Remove canceled articles from the list of unread articles.
4501       (setq gnus-newsgroup-unreads
4502             (gnus-set-sorted-intersection
4503              gnus-newsgroup-unreads
4504              (setq fetched-articles
4505                    (mapcar (lambda (headers) (mail-header-number headers))
4506                            gnus-newsgroup-headers))))
4507       ;; Removed marked articles that do not exist.
4508       (gnus-update-missing-marks
4509        (gnus-sorted-complement fetched-articles articles))
4510
4511       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4512       (when cached
4513         (setq gnus-newsgroup-cached cached))
4514
4515       ;; We might want to build some more threads first.
4516       (when (and gnus-fetch-old-headers
4517                  (eq gnus-headers-retrieved-by 'nov))
4518         (if (eq gnus-fetch-old-headers 'invisible)
4519             (gnus-build-all-threads)
4520           (gnus-build-old-threads)))
4521       ;; Let the Gnus agent mark articles as read.
4522       (when gnus-agent
4523         (gnus-agent-get-undownloaded-list))
4524       ;; Remove list identifiers from subject
4525       (when gnus-list-identifiers
4526         (gnus-summary-remove-list-identifiers))
4527       ;; Check whether auto-expire is to be done in this group.
4528       (setq gnus-newsgroup-auto-expire
4529             (gnus-group-auto-expirable-p group))
4530       ;; Set up the article buffer now, if necessary.
4531       (unless gnus-single-article-buffer
4532         (gnus-article-setup-buffer))
4533       ;; First and last article in this newsgroup.
4534       (when gnus-newsgroup-headers
4535         (setq gnus-newsgroup-begin
4536               (mail-header-number (car gnus-newsgroup-headers))
4537               gnus-newsgroup-end
4538               (mail-header-number
4539                (gnus-last-element gnus-newsgroup-headers))))
4540       ;; GROUP is successfully selected.
4541       (or gnus-newsgroup-headers t)))))
4542
4543 (defun gnus-articles-to-read (group &optional read-all)
4544   "Find out what articles the user wants to read."
4545   (let* ((articles
4546           ;; Select all articles if `read-all' is non-nil, or if there
4547           ;; are no unread articles.
4548           (if (or read-all
4549                   (and (zerop (length gnus-newsgroup-marked))
4550                        (zerop (length gnus-newsgroup-unreads)))
4551                   (eq (gnus-group-find-parameter group 'display)
4552                       'all))
4553               (or
4554                (gnus-uncompress-range (gnus-active group))
4555                (gnus-cache-articles-in-group group))
4556             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4557                           (copy-sequence gnus-newsgroup-unreads))
4558                   '<)))
4559          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4560          (scored (length scored-list))
4561          (number (length articles))
4562          (marked (+ (length gnus-newsgroup-marked)
4563                     (length gnus-newsgroup-dormant)))
4564          (select
4565           (cond
4566            ((numberp read-all)
4567             read-all)
4568            (t
4569             (condition-case ()
4570                 (cond
4571                  ((and (or (<= scored marked) (= scored number))
4572                        (natnump gnus-large-newsgroup)
4573                        (> number gnus-large-newsgroup))
4574                   (let* ((cursor-in-echo-area nil)
4575                          (input (read-from-minibuffer
4576                                  (format
4577                                   "How many articles from %s (max %d): "
4578                                   (gnus-limit-string gnus-newsgroup-name 35)
4579                                   number)
4580                                  (cons (number-to-string gnus-large-newsgroup)
4581                                        0))))
4582                     (if (string-match "^[ \t]*$" input)
4583                         number
4584                       input)))
4585                  ((and (> scored marked) (< scored number)
4586                        (> (- scored number) 20))
4587                   (let ((input
4588                          (read-string
4589                           (format "%s %s (%d scored, %d total): "
4590                                   "How many articles from"
4591                                   group scored number))))
4592                     (if (string-match "^[ \t]*$" input)
4593                         number input)))
4594                  (t number))
4595               (quit
4596                (message "Quit getting the articles to read")
4597                nil))))))
4598     (setq select (if (stringp select) (string-to-number select) select))
4599     (if (or (null select) (zerop select))
4600         select
4601       (if (and (not (zerop scored)) (<= (abs select) scored))
4602           (progn
4603             (setq articles (sort scored-list '<))
4604             (setq number (length articles)))
4605         (setq articles (copy-sequence articles)))
4606
4607       (when (< (abs select) number)
4608         (if (< select 0)
4609             ;; Select the N oldest articles.
4610             (setcdr (nthcdr (1- (abs select)) articles) nil)
4611           ;; Select the N most recent articles.
4612           (setq articles (nthcdr (- number select) articles))))
4613       (setq gnus-newsgroup-unselected
4614             (gnus-sorted-intersection
4615              gnus-newsgroup-unreads
4616              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4617       (when gnus-alter-articles-to-read-function
4618         (setq gnus-newsgroup-unreads
4619               (sort
4620                (funcall gnus-alter-articles-to-read-function
4621                         gnus-newsgroup-name gnus-newsgroup-unreads)
4622                '<)))
4623       articles)))
4624
4625 (defun gnus-killed-articles (killed articles)
4626   (let (out)
4627     (while articles
4628       (when (inline (gnus-member-of-range (car articles) killed))
4629         (push (car articles) out))
4630       (setq articles (cdr articles)))
4631     out))
4632
4633 (defun gnus-uncompress-marks (marks)
4634   "Uncompress the mark ranges in MARKS."
4635   (let ((uncompressed '(score bookmark))
4636         out)
4637     (while marks
4638       (if (memq (caar marks) uncompressed)
4639           (push (car marks) out)
4640         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4641       (setq marks (cdr marks)))
4642     out))
4643
4644 (defun gnus-adjust-marked-articles (info)
4645   "Set all article lists and remove all marks that are no longer valid."
4646   (let* ((marked-lists (gnus-info-marks info))
4647          (active (gnus-active (gnus-info-group info)))
4648          (min (car active))
4649          (max (cdr active))
4650          (types gnus-article-mark-lists)
4651          (uncompressed '(score bookmark killed))
4652          marks var articles article mark)
4653
4654     (while marked-lists
4655       (setq marks (pop marked-lists))
4656       (set (setq var (intern (format "gnus-newsgroup-%s"
4657                                      (car (rassq (setq mark (car marks))
4658                                                  types)))))
4659            (if (memq (car marks) uncompressed) (cdr marks)
4660              (gnus-uncompress-range (cdr marks))))
4661
4662       (setq articles (symbol-value var))
4663
4664       ;; All articles have to be subsets of the active articles.
4665       (cond
4666        ;; Adjust "simple" lists.
4667        ((memq mark '(tick dormant expire reply save))
4668         (while articles
4669           (when (or (< (setq article (pop articles)) min) (> article max))
4670             (set var (delq article (symbol-value var))))))
4671        ;; Adjust assocs.
4672        ((memq mark uncompressed)
4673         (when (not (listp (cdr (symbol-value var))))
4674           (set var (list (symbol-value var))))
4675         (when (not (listp (cdr articles)))
4676           (setq articles (list articles)))
4677         (while articles
4678           (when (or (not (consp (setq article (pop articles))))
4679                     (< (car article) min)
4680                     (> (car article) max))
4681             (set var (delq article (symbol-value var))))))))))
4682
4683 (defun gnus-update-missing-marks (missing)
4684   "Go through the list of MISSING articles and remove them from the mark lists."
4685   (when missing
4686     (let ((types gnus-article-mark-lists)
4687           var m)
4688       ;; Go through all types.
4689       (while types
4690         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4691         (when (symbol-value var)
4692           ;; This list has articles.  So we delete all missing articles
4693           ;; from it.
4694           (setq m missing)
4695           (while m
4696             (set var (delq (pop m) (symbol-value var)))))))))
4697
4698 (defun gnus-update-marks ()
4699   "Enter the various lists of marked articles into the newsgroup info list."
4700   (let ((types gnus-article-mark-lists)
4701         (info (gnus-get-info gnus-newsgroup-name))
4702         (uncompressed '(score bookmark killed))
4703         type list newmarked symbol delta-marks)
4704     (when info
4705       ;; Add all marks lists to the list of marks lists.
4706       (while (setq type (pop types))
4707         (setq list (symbol-value
4708                     (setq symbol
4709                           (intern (format "gnus-newsgroup-%s"
4710                                           (car type))))))
4711
4712         (when list
4713           ;; Get rid of the entries of the articles that have the
4714           ;; default score.
4715           (when (and (eq (cdr type) 'score)
4716                      gnus-save-score
4717                      list)
4718             (let* ((arts list)
4719                    (prev (cons nil list))
4720                    (all prev))
4721               (while arts
4722                 (if (or (not (consp (car arts)))
4723                         (= (cdar arts) gnus-summary-default-score))
4724                     (setcdr prev (cdr arts))
4725                   (setq prev arts))
4726                 (setq arts (cdr arts)))
4727               (setq list (cdr all)))))
4728
4729         (unless (memq (cdr type) uncompressed)
4730           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4731
4732         (when (gnus-check-backend-function
4733                'request-set-mark gnus-newsgroup-name)
4734           ;; propagate flags to server, with the following exceptions:
4735           ;; uncompressed:s are not proper flags (they are cons cells)
4736           ;; cache is a internal gnus flag
4737           ;; download are local to one gnus installation (well)
4738           ;; unsend are for nndraft groups only
4739           ;; xxx: generality of this?  this suits nnimap anyway
4740           (unless (memq (cdr type) (append '(cache download unsend)
4741                                            uncompressed))
4742             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4743                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4744                    (add (gnus-remove-from-range
4745                          (gnus-copy-sequence list) old)))
4746               (when add
4747                 (push (list add 'add (list (cdr type))) delta-marks))
4748               (when del
4749                 (push (list del 'del (list (cdr type))) delta-marks)))))
4750
4751         (when list
4752           (push (cons (cdr type) list) newmarked)))
4753
4754       (when delta-marks
4755         (unless (gnus-check-group gnus-newsgroup-name)
4756           (error "Can't open server for %s" gnus-newsgroup-name))
4757         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4758
4759       ;; Enter these new marks into the info of the group.
4760       (if (nthcdr 3 info)
4761           (setcar (nthcdr 3 info) newmarked)
4762         ;; Add the marks lists to the end of the info.
4763         (when newmarked
4764           (setcdr (nthcdr 2 info) (list newmarked))))
4765
4766       ;; Cut off the end of the info if there's nothing else there.
4767       (let ((i 5))
4768         (while (and (> i 2)
4769                     (not (nth i info)))
4770           (when (nthcdr (decf i) info)
4771             (setcdr (nthcdr i info) nil)))))))
4772
4773 (defun gnus-set-mode-line (where)
4774   "Set the mode line of the article or summary buffers.
4775 If WHERE is `summary', the summary mode line format will be used."
4776   ;; Is this mode line one we keep updated?
4777   (when (and (memq where gnus-updated-mode-lines)
4778              (symbol-value
4779               (intern (format "gnus-%s-mode-line-format-spec" where))))
4780     (let (mode-string)
4781       (save-excursion
4782         ;; We evaluate this in the summary buffer since these
4783         ;; variables are buffer-local to that buffer.
4784         (set-buffer gnus-summary-buffer)
4785         ;; We bind all these variables that are used in the `eval' form
4786         ;; below.
4787         (let* ((mformat (symbol-value
4788                          (intern
4789                           (format "gnus-%s-mode-line-format-spec" where))))
4790                (gnus-tmp-group-name (gnus-group-decoded-name 
4791                                      gnus-newsgroup-name))
4792                (gnus-tmp-article-number (or gnus-current-article 0))
4793                (gnus-tmp-unread gnus-newsgroup-unreads)
4794                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4795                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4796                (gnus-tmp-unread-and-unselected
4797                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4798                             (zerop gnus-tmp-unselected))
4799                        "")
4800                       ((zerop gnus-tmp-unselected)
4801                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4802                       (t (format "{%d(+%d) more}"
4803                                  gnus-tmp-unread-and-unticked
4804                                  gnus-tmp-unselected))))
4805                (gnus-tmp-subject
4806                 (if (and gnus-current-headers
4807                          (vectorp gnus-current-headers))
4808                     (gnus-mode-string-quote
4809                      (mail-header-subject gnus-current-headers))
4810                   ""))
4811                bufname-length max-len
4812                gnus-tmp-header);; passed as argument to any user-format-funcs
4813           (setq mode-string (eval mformat))
4814           (setq bufname-length (if (string-match "%b" mode-string)
4815                                    (- (length
4816                                        (buffer-name
4817                                         (if (eq where 'summary)
4818                                             nil
4819                                           (get-buffer gnus-article-buffer))))
4820                                       2)
4821                                  0))
4822           (setq max-len (max 4 (if gnus-mode-non-string-length
4823                                    (- (window-width)
4824                                       gnus-mode-non-string-length
4825                                       bufname-length)
4826                                  (length mode-string))))
4827           ;; We might have to chop a bit of the string off...
4828           (when (> (length mode-string) max-len)
4829             (setq mode-string
4830                   (concat (gnus-truncate-string mode-string (- max-len 3))
4831                           "...")))
4832           ;; Pad the mode string a bit.
4833           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4834       ;; Update the mode line.
4835       (setq mode-line-buffer-identification
4836             (gnus-mode-line-buffer-identification (list mode-string)))
4837       (set-buffer-modified-p t))))
4838
4839 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4840   "Go through the HEADERS list and add all Xrefs to a hash table.
4841 The resulting hash table is returned, or nil if no Xrefs were found."
4842   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4843          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4844          (xref-hashtb (gnus-make-hashtable))
4845          start group entry number xrefs header)
4846     (while headers
4847       (setq header (pop headers))
4848       (when (and (setq xrefs (mail-header-xref header))
4849                  (not (memq (setq number (mail-header-number header))
4850                             unreads)))
4851         (setq start 0)
4852         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4853           (setq start (match-end 0))
4854           (setq group (if prefix
4855                           (concat prefix (substring xrefs (match-beginning 1)
4856                                                     (match-end 1)))
4857                         (substring xrefs (match-beginning 1) (match-end 1))))
4858           (setq number
4859                 (string-to-int (substring xrefs (match-beginning 2)
4860                                           (match-end 2))))
4861           (if (setq entry (gnus-gethash group xref-hashtb))
4862               (setcdr entry (cons number (cdr entry)))
4863             (gnus-sethash group (cons number nil) xref-hashtb)))))
4864     (and start xref-hashtb)))
4865
4866 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4867   "Look through all the headers and mark the Xrefs as read."
4868   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4869         name entry info xref-hashtb idlist method nth4)
4870     (save-excursion
4871       (set-buffer gnus-group-buffer)
4872       (when (setq xref-hashtb
4873                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4874         (mapatoms
4875          (lambda (group)
4876            (unless (string= from-newsgroup (setq name (symbol-name group)))
4877              (setq idlist (symbol-value group))
4878              ;; Dead groups are not updated.
4879              (and (prog1
4880                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4881                             info (nth 2 entry))
4882                     (when (stringp (setq nth4 (gnus-info-method info)))
4883                       (setq nth4 (gnus-server-to-method nth4))))
4884                   ;; Only do the xrefs if the group has the same
4885                   ;; select method as the group we have just read.
4886                   (or (gnus-methods-equal-p
4887                        nth4 (gnus-find-method-for-group from-newsgroup))
4888                       virtual
4889                       (equal nth4 (setq method (gnus-find-method-for-group
4890                                                 from-newsgroup)))
4891                       (and (equal (car nth4) (car method))
4892                            (equal (nth 1 nth4) (nth 1 method))))
4893                   gnus-use-cross-reference
4894                   (or (not (eq gnus-use-cross-reference t))
4895                       virtual
4896                       ;; Only do cross-references on subscribed
4897                       ;; groups, if that is what is wanted.
4898                       (<= (gnus-info-level info) gnus-level-subscribed))
4899                   (gnus-group-make-articles-read name idlist))))
4900          xref-hashtb)))))
4901
4902 (defun gnus-compute-read-articles (group articles)
4903   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4904          (info (nth 2 entry))
4905          (active (gnus-active group))
4906          ninfo)
4907     (when entry
4908       ;; First peel off all invalid article numbers.
4909       (when active
4910         (let ((ids articles)
4911               id first)
4912           (while (setq id (pop ids))
4913             (when (and first (> id (cdr active)))
4914               ;; We'll end up in this situation in one particular
4915               ;; obscure situation.  If you re-scan a group and get
4916               ;; a new article that is cross-posted to a different
4917               ;; group that has not been re-scanned, you might get
4918               ;; crossposted article that has a higher number than
4919               ;; Gnus believes possible.  So we re-activate this
4920               ;; group as well.  This might mean doing the
4921               ;; crossposting thingy will *increase* the number
4922               ;; of articles in some groups.  Tsk, tsk.
4923               (setq active (or (gnus-activate-group group) active)))
4924             (when (or (> id (cdr active))
4925                       (< id (car active)))
4926               (setq articles (delq id articles))))))
4927       ;; If the read list is nil, we init it.
4928       (if (and active
4929                (null (gnus-info-read info))
4930                (> (car active) 1))
4931           (setq ninfo (cons 1 (1- (car active))))
4932         (setq ninfo (gnus-info-read info)))
4933       ;; Then we add the read articles to the range.
4934       (gnus-add-to-range
4935        ninfo (setq articles (sort articles '<))))))
4936
4937 (defun gnus-group-make-articles-read (group articles)
4938   "Update the info of GROUP to say that ARTICLES are read."
4939   (let* ((num 0)
4940          (entry (gnus-gethash group gnus-newsrc-hashtb))
4941          (info (nth 2 entry))
4942          (active (gnus-active group))
4943          range)
4944     (when entry
4945       (setq range (gnus-compute-read-articles group articles))
4946       (save-excursion
4947         (set-buffer gnus-group-buffer)
4948         (gnus-undo-register
4949           `(progn
4950              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4951              (gnus-info-set-read ',info ',(gnus-info-read info))
4952              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4953              (gnus-group-update-group ,group t))))
4954       ;; Add the read articles to the range.
4955       (gnus-info-set-read info range)
4956       ;; Then we have to re-compute how many unread
4957       ;; articles there are in this group.
4958       (when active
4959         (cond
4960          ((not range)
4961           (setq num (- (1+ (cdr active)) (car active))))
4962          ((not (listp (cdr range)))
4963           (setq num (- (cdr active) (- (1+ (cdr range))
4964                                        (car range)))))
4965          (t
4966           (while range
4967             (if (numberp (car range))
4968                 (setq num (1+ num))
4969               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4970             (setq range (cdr range)))
4971           (setq num (- (cdr active) num))))
4972         ;; Update the number of unread articles.
4973         (setcar entry num)
4974         ;; Update the group buffer.
4975         (gnus-group-update-group group t)))))
4976
4977 (defvar gnus-newsgroup-none-id 0)
4978
4979 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4980   (let ((cur nntp-server-buffer)
4981         (dependencies
4982          (or dependencies
4983              (save-excursion (set-buffer gnus-summary-buffer)
4984                              gnus-newsgroup-dependencies)))
4985         headers id end ref
4986         (mail-parse-charset gnus-newsgroup-charset)
4987         (mail-parse-ignored-charsets
4988          (save-excursion (condition-case nil
4989                              (set-buffer gnus-summary-buffer)
4990                            (error))
4991                          gnus-newsgroup-ignored-charsets)))
4992     (save-excursion
4993       (set-buffer nntp-server-buffer)
4994       ;; Translate all TAB characters into SPACE characters.
4995       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4996       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4997       (gnus-run-hooks 'gnus-parse-headers-hook)
4998       (let ((case-fold-search t)
4999             in-reply-to header p lines chars ctype)
5000         (goto-char (point-min))
5001         ;; Search to the beginning of the next header.  Error messages
5002         ;; do not begin with 2 or 3.
5003         (while (re-search-forward "^[23][0-9]+ " nil t)
5004           (setq id nil
5005                 ref nil)
5006           ;; This implementation of this function, with nine
5007           ;; search-forwards instead of the one re-search-forward and
5008           ;; a case (which basically was the old function) is actually
5009           ;; about twice as fast, even though it looks messier.  You
5010           ;; can't have everything, I guess.  Speed and elegance
5011           ;; doesn't always go hand in hand.
5012           (setq
5013            header
5014            (make-full-mail-header
5015             ;; Number.
5016             (prog1
5017                 (read cur)
5018               (end-of-line)
5019               (setq p (point))
5020               (narrow-to-region (point)
5021                                 (or (and (search-forward "\n.\n" nil t)
5022                                          (- (point) 2))
5023                                     (point))))
5024             ;; Subject.
5025             (progn
5026               (goto-char p)
5027               (if (search-forward "\nsubject: " nil t)
5028                   (nnheader-header-value)
5029                 "(none)"))
5030             ;; From.
5031             (progn
5032               (goto-char p)
5033               (if (or (search-forward "\nfrom: " nil t)
5034                       (search-forward "\nfrom:" nil t))
5035                   (nnheader-header-value)
5036                 "(nobody)"))
5037             ;; Date.
5038             (progn
5039               (goto-char p)
5040               (if (search-forward "\ndate: " nil t)
5041                   (nnheader-header-value)
5042                 ""))
5043             ;; Message-ID.
5044             (progn
5045               (goto-char p)
5046               (setq id (if (re-search-forward
5047                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5048                            ;; We do it this way to make sure the Message-ID
5049                            ;; is (somewhat) syntactically valid.
5050                            (buffer-substring (match-beginning 1)
5051                                              (match-end 1))
5052                          ;; If there was no message-id, we just fake one
5053                          ;; to make subsequent routines simpler.
5054                          (nnheader-generate-fake-message-id))))
5055             ;; References.
5056             (progn
5057               (goto-char p)
5058               (if (search-forward "\nreferences: " nil t)
5059                   (progn
5060                     (setq end (point))
5061                     (prog1
5062                         (nnheader-header-value)
5063                       (setq ref
5064                             (buffer-substring
5065                              (progn
5066                                ;; (end-of-line)
5067                                (search-backward ">" end t)
5068                                (1+ (point)))
5069                              (progn
5070                                (search-backward "<" end t)
5071                                (point))))))
5072                 ;; Get the references from the in-reply-to header if there
5073                 ;; were no references and the in-reply-to header looks
5074                 ;; promising.
5075                 (if (and (search-forward "\nin-reply-to: " nil t)
5076                          (setq in-reply-to (nnheader-header-value))
5077                          (string-match "<[^>]+>" in-reply-to))
5078                     (let (ref2)
5079                       (setq ref (substring in-reply-to (match-beginning 0)
5080                                            (match-end 0)))
5081                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5082                         (setq ref2 (substring in-reply-to (match-beginning 0)
5083                                               (match-end 0)))
5084                         (when (> (length ref2) (length ref))
5085                           (setq ref ref2)))
5086                       ref)
5087                   (setq ref nil))))
5088             ;; Chars.
5089             (progn
5090               (goto-char p)
5091               (if (search-forward "\nchars: " nil t)
5092                   (if (numberp (setq chars (ignore-errors (read cur))))
5093                       chars -1)
5094                 -1))
5095             ;; Lines.
5096             (progn
5097               (goto-char p)
5098               (if (search-forward "\nlines: " nil t)
5099                   (if (numberp (setq lines (ignore-errors (read cur))))
5100                       lines -1)
5101                 -1))
5102             ;; Xref.
5103             (progn
5104               (goto-char p)
5105               (and (search-forward "\nxref: " nil t)
5106                    (nnheader-header-value)))
5107             ;; Extra.
5108             (when gnus-extra-headers
5109               (let ((extra gnus-extra-headers)
5110                     out)
5111                 (while extra
5112                   (goto-char p)
5113                   (when (search-forward
5114                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5115                     (push (cons (car extra) (nnheader-header-value)) out))
5116                   (pop extra))
5117                 out))))
5118           (goto-char p)
5119           (if (and (search-forward "\ncontent-type: " nil t)
5120                    (setq ctype (nnheader-header-value)))
5121               (mime-entity-set-content-type-internal
5122                header (mime-parse-Content-Type ctype)))
5123           (when (equal id ref)
5124             (setq ref nil))
5125
5126           (when gnus-alter-header-function
5127             (funcall gnus-alter-header-function header)
5128             (setq id (mail-header-id header)
5129                   ref (gnus-parent-id (mail-header-references header))))
5130
5131           (when (setq header
5132                       (gnus-dependencies-add-header
5133                        header dependencies force-new))
5134             (push header headers))
5135           (goto-char (point-max))
5136           (widen))
5137         (nreverse headers)))))
5138
5139 ;; Goes through the xover lines and returns a list of vectors
5140 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5141                                                   force-new dependencies
5142                                                   group also-fetch-heads)
5143   "Parse the news overview data in the server buffer.
5144 Return a list of headers that match SEQUENCE (see
5145 `nntp-retrieve-headers')."
5146   ;; Get the Xref when the users reads the articles since most/some
5147   ;; NNTP servers do not include Xrefs when using XOVER.
5148   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5149   (let ((mail-parse-charset gnus-newsgroup-charset)
5150         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5151         (cur nntp-server-buffer)
5152         (dependencies (or dependencies gnus-newsgroup-dependencies))
5153         (allp (cond
5154                ((eq gnus-read-all-available-headers t)
5155                 t)
5156                ((stringp gnus-read-all-available-headers)
5157                 (string-match gnus-read-all-available-headers group))
5158                (t
5159                 nil)))
5160         number headers header)
5161     (save-excursion
5162       (set-buffer nntp-server-buffer)
5163       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5164       ;; Allow the user to mangle the headers before parsing them.
5165       (gnus-run-hooks 'gnus-parse-headers-hook)
5166       (goto-char (point-min))
5167       (while (not (eobp))
5168         (condition-case ()
5169             (while (and (or sequence allp)
5170                         (not (eobp)))
5171               (setq number (read cur))
5172               (when (not allp)
5173                 (while (and sequence
5174                             (< (car sequence) number))
5175                   (setq sequence (cdr sequence))))
5176               (when (and (or allp
5177                              (and sequence
5178                                   (eq number (car sequence))))
5179                          (progn
5180                            (setq sequence (cdr sequence))
5181                            (setq header (inline
5182                                           (gnus-nov-parse-line
5183                                            number dependencies force-new)))))
5184                 (push header headers))
5185               (forward-line 1))
5186           (error
5187            (gnus-error 4 "Strange nov line (%d)"
5188                        (count-lines (point-min) (point)))))
5189         (forward-line 1))
5190       ;; A common bug in inn is that if you have posted an article and
5191       ;; then retrieves the active file, it will answer correctly --
5192       ;; the new article is included.  However, a NOV entry for the
5193       ;; article may not have been generated yet, so this may fail.
5194       ;; We work around this problem by retrieving the last few
5195       ;; headers using HEAD.
5196       (if (or (not also-fetch-heads)
5197               (not sequence))
5198           ;; We (probably) got all the headers.
5199           (nreverse headers)
5200         (let ((gnus-nov-is-evil t))
5201           (nconc
5202            (nreverse headers)
5203            ;;;!!! FIXME: temporary fix for an infloop on nnimap.
5204            (if (eq 'nnimap (car (gnus-find-method-for-group group)))
5205                (when (gnus-retrieve-headers sequence group)
5206                  (gnus-get-newsgroup-headers))
5207              (gnus-retrieve-parsed-headers sequence group))))))))
5208
5209 (defun gnus-article-get-xrefs ()
5210   "Fill in the Xref value in `gnus-current-headers', if necessary.
5211 This is meant to be called in `gnus-article-internal-prepare-hook'."
5212   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5213                                  gnus-current-headers)))
5214     (or (not gnus-use-cross-reference)
5215         (not headers)
5216         (and (mail-header-xref headers)
5217              (not (string= (mail-header-xref headers) "")))
5218         (let ((case-fold-search t)
5219               xref)
5220           (save-restriction
5221             (nnheader-narrow-to-headers)
5222             (goto-char (point-min))
5223             (when (or (and (not (eobp))
5224                            (eq (downcase (char-after)) ?x)
5225                            (looking-at "Xref:"))
5226                       (search-forward "\nXref:" nil t))
5227               (goto-char (1+ (match-end 0)))
5228               (setq xref (buffer-substring (point)
5229                                            (progn (end-of-line) (point))))
5230               (mail-header-set-xref headers xref)))))))
5231
5232 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5233   "Find article ID and insert the summary line for that article.
5234 OLD-HEADER can either be a header or a line number to insert
5235 the subject line on."
5236   (let* ((line (and (numberp old-header) old-header))
5237          (old-header (and (vectorp old-header) old-header))
5238          (header (cond ((and old-header use-old-header)
5239                         old-header)
5240                        ((and (numberp id)
5241                              (gnus-number-to-header id))
5242                         (gnus-number-to-header id))
5243                        (t
5244                         (gnus-read-header id))))
5245          (number (and (numberp id) id))
5246          d)
5247     (when header
5248       ;; Rebuild the thread that this article is part of and go to the
5249       ;; article we have fetched.
5250       (when (and (not gnus-show-threads)
5251                  old-header)
5252         (when (and number
5253                    (setq d (gnus-data-find (mail-header-number old-header))))
5254           (goto-char (gnus-data-pos d))
5255           (gnus-data-remove
5256            number
5257            (- (gnus-point-at-bol)
5258               (prog1
5259                   (1+ (gnus-point-at-eol))
5260                 (gnus-delete-line))))))
5261       (when old-header
5262         (mail-header-set-number header (mail-header-number old-header)))
5263       (setq gnus-newsgroup-sparse
5264             (delq (setq number (mail-header-number header))
5265                   gnus-newsgroup-sparse))
5266       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5267       (push number gnus-newsgroup-limit)
5268       (gnus-rebuild-thread (mail-header-id header) line)
5269       (gnus-summary-goto-subject number nil t))
5270     (when (and (numberp number)
5271                (> number 0))
5272       ;; We have to update the boundaries even if we can't fetch the
5273       ;; article if ID is a number -- so that the next `P' or `N'
5274       ;; command will fetch the previous (or next) article even
5275       ;; if the one we tried to fetch this time has been canceled.
5276       (when (> number gnus-newsgroup-end)
5277         (setq gnus-newsgroup-end number))
5278       (when (< number gnus-newsgroup-begin)
5279         (setq gnus-newsgroup-begin number))
5280       (setq gnus-newsgroup-unselected
5281             (delq number gnus-newsgroup-unselected)))
5282     ;; Report back a success?
5283     (and header (mail-header-number header))))
5284
5285 ;;; Process/prefix in the summary buffer
5286
5287 (defun gnus-summary-work-articles (n)
5288   "Return a list of articles to be worked upon.
5289 The prefix argument, the list of process marked articles, and the
5290 current article will be taken into consideration."
5291   (save-excursion
5292     (set-buffer gnus-summary-buffer)
5293     (cond
5294      (n
5295       ;; A numerical prefix has been given.
5296       (setq n (prefix-numeric-value n))
5297       (let ((backward (< n 0))
5298             (n (abs (prefix-numeric-value n)))
5299             articles article)
5300         (save-excursion
5301           (while
5302               (and (> n 0)
5303                    (push (setq article (gnus-summary-article-number))
5304                          articles)
5305                    (if backward
5306                        (gnus-summary-find-prev nil article)
5307                      (gnus-summary-find-next nil article)))
5308             (decf n)))
5309         (nreverse articles)))
5310      ((and (gnus-region-active-p) (mark))
5311       (message "region active")
5312       ;; Work on the region between point and mark.
5313       (let ((max (max (point) (mark)))
5314             articles article)
5315         (save-excursion
5316           (goto-char (min (point) (mark)))
5317           (while
5318               (and
5319                (push (setq article (gnus-summary-article-number)) articles)
5320                (gnus-summary-find-next nil article)
5321                (< (point) max)))
5322           (nreverse articles))))
5323      (gnus-newsgroup-processable
5324       ;; There are process-marked articles present.
5325       ;; Save current state.
5326       (gnus-summary-save-process-mark)
5327       ;; Return the list.
5328       (reverse gnus-newsgroup-processable))
5329      (t
5330       ;; Just return the current article.
5331       (list (gnus-summary-article-number))))))
5332
5333 (defmacro gnus-summary-iterate (arg &rest forms)
5334   "Iterate over the process/prefixed articles and do FORMS.
5335 ARG is the interactive prefix given to the command.  FORMS will be
5336 executed with point over the summary line of the articles."
5337   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5338     `(let ((,articles (gnus-summary-work-articles ,arg)))
5339        (while ,articles
5340          (gnus-summary-goto-subject (car ,articles))
5341          ,@forms
5342          (pop ,articles)))))
5343
5344 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5345 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5346
5347 (defun gnus-summary-save-process-mark ()
5348   "Push the current set of process marked articles on the stack."
5349   (interactive)
5350   (push (copy-sequence gnus-newsgroup-processable)
5351         gnus-newsgroup-process-stack))
5352
5353 (defun gnus-summary-kill-process-mark ()
5354   "Push the current set of process marked articles on the stack and unmark."
5355   (interactive)
5356   (gnus-summary-save-process-mark)
5357   (gnus-summary-unmark-all-processable))
5358
5359 (defun gnus-summary-yank-process-mark ()
5360   "Pop the last process mark state off the stack and restore it."
5361   (interactive)
5362   (unless gnus-newsgroup-process-stack
5363     (error "Empty mark stack"))
5364   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5365
5366 (defun gnus-summary-process-mark-set (set)
5367   "Make SET into the current process marked articles."
5368   (gnus-summary-unmark-all-processable)
5369   (while set
5370     (gnus-summary-set-process-mark (pop set))))
5371
5372 ;;; Searching and stuff
5373
5374 (defun gnus-summary-search-group (&optional backward use-level)
5375   "Search for next unread newsgroup.
5376 If optional argument BACKWARD is non-nil, search backward instead."
5377   (save-excursion
5378     (set-buffer gnus-group-buffer)
5379     (when (gnus-group-search-forward
5380            backward nil (if use-level (gnus-group-group-level) nil))
5381       (gnus-group-group-name))))
5382
5383 (defun gnus-summary-best-group (&optional exclude-group)
5384   "Find the name of the best unread group.
5385 If EXCLUDE-GROUP, do not go to this group."
5386   (save-excursion
5387     (set-buffer gnus-group-buffer)
5388     (save-excursion
5389       (gnus-group-best-unread-group exclude-group))))
5390
5391 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5392   (if backward (gnus-summary-find-prev)
5393     (let* ((dummy (gnus-summary-article-intangible-p))
5394            (article (or article (gnus-summary-article-number)))
5395            (arts (gnus-data-find-list article))
5396            result)
5397       (when (and (not dummy)
5398                  (or (not gnus-summary-check-current)
5399                      (not unread)
5400                      (not (gnus-data-unread-p (car arts)))))
5401         (setq arts (cdr arts)))
5402       (when (setq result
5403                   (if unread
5404                       (progn
5405                         (while arts
5406                           (when (or (and undownloaded
5407                                          (eq gnus-undownloaded-mark
5408                                              (gnus-data-mark (car arts))))
5409                                     (gnus-data-unread-p (car arts)))
5410                             (setq result (car arts)
5411                                   arts nil))
5412                           (setq arts (cdr arts)))
5413                         result)
5414                     (car arts)))
5415         (goto-char (gnus-data-pos result))
5416         (gnus-data-number result)))))
5417
5418 (defun gnus-summary-find-prev (&optional unread article)
5419   (let* ((eobp (eobp))
5420          (article (or article (gnus-summary-article-number)))
5421          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5422          result)
5423     (when (and (not eobp)
5424                (or (not gnus-summary-check-current)
5425                    (not unread)
5426                    (not (gnus-data-unread-p (car arts)))))
5427       (setq arts (cdr arts)))
5428     (when (setq result
5429                 (if unread
5430                     (progn
5431                       (while arts
5432                         (when (gnus-data-unread-p (car arts))
5433                           (setq result (car arts)
5434                                 arts nil))
5435                         (setq arts (cdr arts)))
5436                       result)
5437                   (car arts)))
5438       (goto-char (gnus-data-pos result))
5439       (gnus-data-number result))))
5440
5441 (defun gnus-summary-find-subject (subject &optional unread backward article)
5442   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5443          (article (or article (gnus-summary-article-number)))
5444          (articles (gnus-data-list backward))
5445          (arts (gnus-data-find-list article articles))
5446          result)
5447     (when (or (not gnus-summary-check-current)
5448               (not unread)
5449               (not (gnus-data-unread-p (car arts))))
5450       (setq arts (cdr arts)))
5451     (while arts
5452       (and (or (not unread)
5453                (gnus-data-unread-p (car arts)))
5454            (vectorp (gnus-data-header (car arts)))
5455            (gnus-subject-equal
5456             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5457            (setq result (car arts)
5458                  arts nil))
5459       (setq arts (cdr arts)))
5460     (and result
5461          (goto-char (gnus-data-pos result))
5462          (gnus-data-number result))))
5463
5464 (defun gnus-summary-search-forward (&optional unread subject backward)
5465   "Search forward for an article.
5466 If UNREAD, look for unread articles.  If SUBJECT, look for
5467 articles with that subject.  If BACKWARD, search backward instead."
5468   (cond (subject (gnus-summary-find-subject subject unread backward))
5469         (backward (gnus-summary-find-prev unread))
5470         (t (gnus-summary-find-next unread))))
5471
5472 (defun gnus-recenter (&optional n)
5473   "Center point in window and redisplay frame.
5474 Also do horizontal recentering."
5475   (interactive "P")
5476   (when (and gnus-auto-center-summary
5477              (not (eq gnus-auto-center-summary 'vertical)))
5478     (gnus-horizontal-recenter))
5479   (recenter n))
5480
5481 (defun gnus-summary-recenter ()
5482   "Center point in the summary window.
5483 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5484 displayed, no centering will be performed."
5485   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5486   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5487   (interactive)
5488   (let* ((top (cond ((< (window-height) 4) 0)
5489                     ((< (window-height) 7) 1)
5490                     (t (if (numberp gnus-auto-center-summary)
5491                            gnus-auto-center-summary
5492                          2))))
5493          (height (1- (window-height)))
5494          (bottom (save-excursion (goto-char (point-max))
5495                                  (forward-line (- height))
5496                                  (point)))
5497          (window (get-buffer-window (current-buffer))))
5498     ;; The user has to want it.
5499     (when gnus-auto-center-summary
5500       (when (get-buffer-window gnus-article-buffer)
5501         ;; Only do recentering when the article buffer is displayed,
5502         ;; Set the window start to either `bottom', which is the biggest
5503         ;; possible valid number, or the second line from the top,
5504         ;; whichever is the least.
5505         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5506           (if (> bottom top-pos)
5507               ;; Keep the second line from the top visible
5508               (set-window-start window top-pos t)
5509             ;; Try to keep the bottom line visible; if it's partially
5510             ;; obscured, either scroll one more line to make it fully
5511             ;; visible, or revert to using TOP-POS.
5512             (save-excursion
5513               (goto-char (point-max))
5514               (forward-line -1)
5515               (let ((last-line-start (point)))
5516                 (goto-char bottom)
5517                 (set-window-start window (point) t)
5518                 (when (not (pos-visible-in-window-p last-line-start window))
5519                   (forward-line 1)
5520                   (set-window-start window (min (point) top-pos) t)))))))
5521       ;; Do horizontal recentering while we're at it.
5522       (when (and (get-buffer-window (current-buffer) t)
5523                  (not (eq gnus-auto-center-summary 'vertical)))
5524         (let ((selected (selected-window)))
5525           (select-window (get-buffer-window (current-buffer) t))
5526           (gnus-summary-position-point)
5527           (gnus-horizontal-recenter)
5528           (select-window selected))))))
5529
5530 (defun gnus-summary-jump-to-group (newsgroup)
5531   "Move point to NEWSGROUP in group mode buffer."
5532   ;; Keep update point of group mode buffer if visible.
5533   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5534       (save-window-excursion
5535         ;; Take care of tree window mode.
5536         (when (get-buffer-window gnus-group-buffer)
5537           (pop-to-buffer gnus-group-buffer))
5538         (gnus-group-jump-to-group newsgroup))
5539     (save-excursion
5540       ;; Take care of tree window mode.
5541       (if (get-buffer-window gnus-group-buffer)
5542           (pop-to-buffer gnus-group-buffer)
5543         (set-buffer gnus-group-buffer))
5544       (gnus-group-jump-to-group newsgroup))))
5545
5546 ;; This function returns a list of article numbers based on the
5547 ;; difference between the ranges of read articles in this group and
5548 ;; the range of active articles.
5549 (defun gnus-list-of-unread-articles (group)
5550   (let* ((read (gnus-info-read (gnus-get-info group)))
5551          (active (or (gnus-active group) (gnus-activate-group group)))
5552          (last (cdr active))
5553          first nlast unread)
5554     ;; If none are read, then all are unread.
5555     (if (not read)
5556         (setq first (car active))
5557       ;; If the range of read articles is a single range, then the
5558       ;; first unread article is the article after the last read
5559       ;; article.  Sounds logical, doesn't it?
5560       (if (and (not (listp (cdr read)))
5561                (or (< (car read) (car active))
5562                    (progn (setq read (list read))
5563                           nil)))
5564           (setq first (max (car active) (1+ (cdr read))))
5565         ;; `read' is a list of ranges.
5566         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5567                                   (caar read)))
5568                   1)
5569           (setq first (car active)))
5570         (while read
5571           (when first
5572             (while (< first nlast)
5573               (push first unread)
5574               (setq first (1+ first))))
5575           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5576           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5577           (setq read (cdr read)))))
5578     ;; And add the last unread articles.
5579     (while (<= first last)
5580       (push first unread)
5581       (setq first (1+ first)))
5582     ;; Return the list of unread articles.
5583     (delq 0 (nreverse unread))))
5584
5585 (defun gnus-list-of-read-articles (group)
5586   "Return a list of unread, unticked and non-dormant articles."
5587   (let* ((info (gnus-get-info group))
5588          (marked (gnus-info-marks info))
5589          (active (gnus-active group)))
5590     (and info active
5591          (gnus-set-difference
5592           (gnus-sorted-complement
5593            (gnus-uncompress-range active)
5594            (gnus-list-of-unread-articles group))
5595           (append
5596            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5597            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5598
5599 ;; Various summary commands
5600
5601 (defun gnus-summary-select-article-buffer ()
5602   "Reconfigure windows to show article buffer."
5603   (interactive)
5604   (if (not (gnus-buffer-live-p gnus-article-buffer))
5605       (error "There is no article buffer for this summary buffer")
5606     (gnus-configure-windows 'article)
5607     (select-window (get-buffer-window gnus-article-buffer))))
5608
5609 (defun gnus-summary-universal-argument (arg)
5610   "Perform any operation on all articles that are process/prefixed."
5611   (interactive "P")
5612   (let ((articles (gnus-summary-work-articles arg))
5613         func article)
5614     (if (eq
5615          (setq
5616           func
5617           (key-binding
5618            (read-key-sequence
5619             (substitute-command-keys
5620              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5621          'undefined)
5622         (gnus-error 1 "Undefined key")
5623       (save-excursion
5624         (while articles
5625           (gnus-summary-goto-subject (setq article (pop articles)))
5626           (let (gnus-newsgroup-processable)
5627             (command-execute func))
5628           (gnus-summary-remove-process-mark article)))))
5629   (gnus-summary-position-point))
5630
5631 (defun gnus-summary-toggle-truncation (&optional arg)
5632   "Toggle truncation of summary lines.
5633 With arg, turn line truncation on iff arg is positive."
5634   (interactive "P")
5635   (setq truncate-lines
5636         (if (null arg) (not truncate-lines)
5637           (> (prefix-numeric-value arg) 0)))
5638   (redraw-display))
5639
5640 (defun gnus-summary-reselect-current-group (&optional all rescan)
5641   "Exit and then reselect the current newsgroup.
5642 The prefix argument ALL means to select all articles."
5643   (interactive "P")
5644   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5645     (error "Ephemeral groups can't be reselected"))
5646   (let ((current-subject (gnus-summary-article-number))
5647         (group gnus-newsgroup-name))
5648     (setq gnus-newsgroup-begin nil)
5649     (gnus-summary-exit)
5650     ;; We have to adjust the point of group mode buffer because
5651     ;; point was moved to the next unread newsgroup by exiting.
5652     (gnus-summary-jump-to-group group)
5653     (when rescan
5654       (save-excursion
5655         (save-window-excursion
5656           ;; Don't show group contents.
5657           (set-window-start (selected-window) (point-max))
5658           (gnus-group-get-new-news-this-group 1))))
5659     (gnus-group-read-group all t)
5660     (gnus-summary-goto-subject current-subject nil t)))
5661
5662 (defun gnus-summary-rescan-group (&optional all)
5663   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5664   (interactive "P")
5665   (gnus-summary-reselect-current-group all t))
5666
5667 (defun gnus-summary-update-info (&optional non-destructive)
5668   (save-excursion
5669     (let ((group gnus-newsgroup-name))
5670       (when group
5671         (when gnus-newsgroup-kill-headers
5672           (setq gnus-newsgroup-killed
5673                 (gnus-compress-sequence
5674                  (nconc
5675                   (gnus-set-sorted-intersection
5676                    (gnus-uncompress-range gnus-newsgroup-killed)
5677                    (setq gnus-newsgroup-unselected
5678                          (sort gnus-newsgroup-unselected '<)))
5679                   (setq gnus-newsgroup-unreads
5680                         (sort gnus-newsgroup-unreads '<)))
5681                  t)))
5682         (unless (listp (cdr gnus-newsgroup-killed))
5683           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5684         (let ((headers gnus-newsgroup-headers))
5685           ;; Set the new ranges of read articles.
5686           (save-excursion
5687             (set-buffer gnus-group-buffer)
5688             (gnus-undo-force-boundary))
5689           (gnus-update-read-articles
5690            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5691           ;; Set the current article marks.
5692           (let ((gnus-newsgroup-scored
5693                  (if (and (not gnus-save-score)
5694                           (not non-destructive))
5695                      nil
5696                    gnus-newsgroup-scored)))
5697             (save-excursion
5698               (gnus-update-marks)))
5699           ;; Do the cross-ref thing.
5700           (when gnus-use-cross-reference
5701             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5702           ;; Do not switch windows but change the buffer to work.
5703           (set-buffer gnus-group-buffer)
5704           (unless (gnus-ephemeral-group-p group)
5705             (gnus-group-update-group group)))))))
5706
5707 (defun gnus-summary-save-newsrc (&optional force)
5708   "Save the current number of read/marked articles in the dribble buffer.
5709 The dribble buffer will then be saved.
5710 If FORCE (the prefix), also save the .newsrc file(s)."
5711   (interactive "P")
5712   (gnus-summary-update-info t)
5713   (if force
5714       (gnus-save-newsrc-file)
5715     (gnus-dribble-save)))
5716
5717 (defun gnus-summary-exit (&optional temporary)
5718   "Exit reading current newsgroup, and then return to group selection mode.
5719 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5720   (interactive)
5721   (gnus-set-global-variables)
5722   (gnus-kill-save-kill-buffer)
5723   (gnus-async-halt-prefetch)
5724   (let* ((group gnus-newsgroup-name)
5725          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5726          (mode major-mode)
5727          (group-point nil)
5728          (buf (current-buffer)))
5729     (unless quit-config
5730       ;; Do adaptive scoring, and possibly save score files.
5731       (when gnus-newsgroup-adaptive
5732         (gnus-score-adaptive))
5733       (when gnus-use-scoring
5734         (gnus-score-save)))
5735     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5736     ;; If we have several article buffers, we kill them at exit.
5737     (unless gnus-single-article-buffer
5738       (gnus-kill-buffer gnus-original-article-buffer)
5739       (setq gnus-article-current nil))
5740     (when gnus-use-cache
5741       (gnus-cache-possibly-remove-articles)
5742       (gnus-cache-save-buffers))
5743     (gnus-async-prefetch-remove-group group)
5744     (when gnus-suppress-duplicates
5745       (gnus-dup-enter-articles))
5746     (when gnus-use-trees
5747       (gnus-tree-close group))
5748     (when gnus-use-cache
5749       (gnus-cache-write-active))
5750     ;; Remove entries for this group.
5751     (nnmail-purge-split-history (gnus-group-real-name group))
5752     ;; Make all changes in this group permanent.
5753     (unless quit-config
5754       (gnus-run-hooks 'gnus-exit-group-hook)
5755       (gnus-summary-update-info))
5756     (gnus-close-group group)
5757     ;; Make sure where we were, and go to next newsgroup.
5758     (set-buffer gnus-group-buffer)
5759     (unless quit-config
5760       (gnus-group-jump-to-group group))
5761     (gnus-run-hooks 'gnus-summary-exit-hook)
5762     (unless (or quit-config
5763                 ;; If this group has disappeared from the summary
5764                 ;; buffer, don't skip forwards.
5765                 (not (string= group (gnus-group-group-name))))
5766       (gnus-group-next-unread-group 1))
5767     (setq group-point (point))
5768     (if temporary
5769         nil                             ;Nothing to do.
5770       ;; If we have several article buffers, we kill them at exit.
5771       (unless gnus-single-article-buffer
5772         (gnus-kill-buffer gnus-article-buffer)
5773         (gnus-kill-buffer gnus-original-article-buffer)
5774         (setq gnus-article-current nil))
5775       (set-buffer buf)
5776       (if (not gnus-kill-summary-on-exit)
5777           (progn
5778             (gnus-deaden-summary)
5779             (setq mode nil))
5780         ;; We set all buffer-local variables to nil.  It is unclear why
5781         ;; this is needed, but if we don't, buffer-local variables are
5782         ;; not garbage-collected, it seems.  This would the lead to en
5783         ;; ever-growing Emacs.
5784         (gnus-summary-clear-local-variables)
5785         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5786           (gnus-summary-clear-local-variables))
5787         (when (get-buffer gnus-article-buffer)
5788           (bury-buffer gnus-article-buffer))
5789         ;; We clear the global counterparts of the buffer-local
5790         ;; variables as well, just to be on the safe side.
5791         (set-buffer gnus-group-buffer)
5792         (gnus-summary-clear-local-variables)
5793         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5794           (gnus-summary-clear-local-variables)))
5795       (setq gnus-current-select-method gnus-select-method)
5796       (pop-to-buffer gnus-group-buffer)
5797       (if (not quit-config)
5798           (progn
5799             (goto-char group-point)
5800             (gnus-configure-windows 'group 'force)
5801             (unless (pos-visible-in-window-p)
5802               (forward-line (/ (static-if (featurep 'xemacs)
5803                                    (window-displayed-height)
5804                                  (1- (window-height)))
5805                                -2))
5806               (set-window-start (selected-window) (point))
5807               (goto-char group-point)))
5808         (gnus-handle-ephemeral-exit quit-config))
5809       ;; Return to group mode buffer.
5810       (when (eq mode 'gnus-summary-mode)
5811         (gnus-kill-buffer buf))
5812       ;; Clear the current group name.
5813       (unless quit-config
5814         (setq gnus-newsgroup-name nil)))))
5815
5816 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5817 (defun gnus-summary-exit-no-update (&optional no-questions)
5818   "Quit reading current newsgroup without updating read article info."
5819   (interactive)
5820   (let* ((group gnus-newsgroup-name)
5821          (quit-config (gnus-group-quit-config group)))
5822     (when (or no-questions
5823               gnus-expert-user
5824               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5825       (gnus-async-halt-prefetch)
5826       (mapcar 'funcall
5827               (delq 'gnus-summary-expire-articles
5828                     (copy-sequence gnus-summary-prepare-exit-hook)))
5829       ;; If we have several article buffers, we kill them at exit.
5830       (unless gnus-single-article-buffer
5831         (gnus-kill-buffer gnus-article-buffer)
5832         (gnus-kill-buffer gnus-original-article-buffer)
5833         (setq gnus-article-current nil))
5834       (if (not gnus-kill-summary-on-exit)
5835           (gnus-deaden-summary)
5836         (gnus-close-group group)
5837         (gnus-summary-clear-local-variables)
5838         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5839           (gnus-summary-clear-local-variables))
5840         (set-buffer gnus-group-buffer)
5841         (gnus-summary-clear-local-variables)
5842         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5843           (gnus-summary-clear-local-variables))
5844         (when (get-buffer gnus-summary-buffer)
5845           (kill-buffer gnus-summary-buffer)))
5846       (unless gnus-single-article-buffer
5847         (setq gnus-article-current nil))
5848       (when gnus-use-trees
5849         (gnus-tree-close group))
5850       (gnus-async-prefetch-remove-group group)
5851       (when (get-buffer gnus-article-buffer)
5852         (bury-buffer gnus-article-buffer))
5853       ;; Return to the group buffer.
5854       (gnus-configure-windows 'group 'force)
5855       ;; Clear the current group name.
5856       (setq gnus-newsgroup-name nil)
5857       (when (equal (gnus-group-group-name) group)
5858         (gnus-group-next-unread-group 1))
5859       (when quit-config
5860         (gnus-handle-ephemeral-exit quit-config)))))
5861
5862 (defun gnus-handle-ephemeral-exit (quit-config)
5863   "Handle movement when leaving an ephemeral group.
5864 The state which existed when entering the ephemeral is reset."
5865   (if (not (buffer-name (car quit-config)))
5866       (gnus-configure-windows 'group 'force)
5867     (set-buffer (car quit-config))
5868     (cond ((eq major-mode 'gnus-summary-mode)
5869            (gnus-set-global-variables))
5870           ((eq major-mode 'gnus-article-mode)
5871            (save-excursion
5872              ;; The `gnus-summary-buffer' variable may point
5873              ;; to the old summary buffer when using a single
5874              ;; article buffer.
5875              (unless (gnus-buffer-live-p gnus-summary-buffer)
5876                (set-buffer gnus-group-buffer))
5877              (set-buffer gnus-summary-buffer)
5878              (gnus-set-global-variables))))
5879     (if (or (eq (cdr quit-config) 'article)
5880             (eq (cdr quit-config) 'pick))
5881         (progn
5882           ;; The current article may be from the ephemeral group
5883           ;; thus it is best that we reload this article
5884           (gnus-summary-show-article)
5885           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5886               (gnus-configure-windows 'pick 'force)
5887             (gnus-configure-windows (cdr quit-config) 'force)))
5888       (gnus-configure-windows (cdr quit-config) 'force))
5889     (when (eq major-mode 'gnus-summary-mode)
5890       (gnus-summary-next-subject 1 nil t)
5891       (gnus-summary-recenter)
5892       (gnus-summary-position-point))))
5893
5894 (defun gnus-summary-preview-mime-message ()
5895   "MIME decode and play this message."
5896   (interactive)
5897   (let ((gnus-break-pages nil)
5898         (gnus-show-mime t))
5899     (gnus-summary-select-article gnus-show-all-headers t))
5900   (select-window (get-buffer-window gnus-article-buffer)))
5901
5902 ;;; Dead summaries.
5903
5904 (defvar gnus-dead-summary-mode-map nil)
5905
5906 (unless gnus-dead-summary-mode-map
5907   (setq gnus-dead-summary-mode-map (make-keymap))
5908   (suppress-keymap gnus-dead-summary-mode-map)
5909   (substitute-key-definition
5910    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5911   (let ((keys '("\C-d" "\r" "\177" [delete])))
5912     (while keys
5913       (define-key gnus-dead-summary-mode-map
5914         (pop keys) 'gnus-summary-wake-up-the-dead))))
5915
5916 (defvar gnus-dead-summary-mode nil
5917   "Minor mode for Gnus summary buffers.")
5918
5919 (defun gnus-dead-summary-mode (&optional arg)
5920   "Minor mode for Gnus summary buffers."
5921   (interactive "P")
5922   (when (eq major-mode 'gnus-summary-mode)
5923     (make-local-variable 'gnus-dead-summary-mode)
5924     (setq gnus-dead-summary-mode
5925           (if (null arg) (not gnus-dead-summary-mode)
5926             (> (prefix-numeric-value arg) 0)))
5927     (when gnus-dead-summary-mode
5928       (gnus-add-minor-mode
5929        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5930
5931 (defun gnus-deaden-summary ()
5932   "Make the current summary buffer into a dead summary buffer."
5933   ;; Kill any previous dead summary buffer.
5934   (when (and gnus-dead-summary
5935              (buffer-name gnus-dead-summary))
5936     (save-excursion
5937       (set-buffer gnus-dead-summary)
5938       (when gnus-dead-summary-mode
5939         (kill-buffer (current-buffer)))))
5940   ;; Make this the current dead summary.
5941   (setq gnus-dead-summary (current-buffer))
5942   (gnus-dead-summary-mode 1)
5943   (let ((name (buffer-name)))
5944     (when (string-match "Summary" name)
5945       (rename-buffer
5946        (concat (substring name 0 (match-beginning 0)) "Dead "
5947                (substring name (match-beginning 0)))
5948        t)
5949       (bury-buffer))))
5950
5951 (defun gnus-kill-or-deaden-summary (buffer)
5952   "Kill or deaden the summary BUFFER."
5953   (save-excursion
5954     (when (and (buffer-name buffer)
5955                (not gnus-single-article-buffer))
5956       (save-excursion
5957         (set-buffer buffer)
5958         (gnus-kill-buffer gnus-article-buffer)
5959         (gnus-kill-buffer gnus-original-article-buffer)))
5960     (cond (gnus-kill-summary-on-exit
5961            (when (and gnus-use-trees
5962                       (gnus-buffer-exists-p buffer))
5963              (save-excursion
5964                (set-buffer buffer)
5965                (gnus-tree-close gnus-newsgroup-name)))
5966            (gnus-kill-buffer buffer))
5967           ((gnus-buffer-exists-p buffer)
5968            (save-excursion
5969              (set-buffer buffer)
5970              (gnus-deaden-summary))))))
5971
5972 (defun gnus-summary-wake-up-the-dead (&rest args)
5973   "Wake up the dead summary buffer."
5974   (interactive)
5975   (gnus-dead-summary-mode -1)
5976   (let ((name (buffer-name)))
5977     (when (string-match "Dead " name)
5978       (rename-buffer
5979        (concat (substring name 0 (match-beginning 0))
5980                (substring name (match-end 0)))
5981        t)))
5982   (gnus-message 3 "This dead summary is now alive again"))
5983
5984 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5985 (defun gnus-summary-fetch-faq (&optional faq-dir)
5986   "Fetch the FAQ for the current group.
5987 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5988 in."
5989   (interactive
5990    (list
5991     (when current-prefix-arg
5992       (completing-read
5993        "Faq dir: " (and (listp gnus-group-faq-directory)
5994                         (mapcar (lambda (file) (list file))
5995                                 gnus-group-faq-directory))))))
5996   (let (gnus-faq-buffer)
5997     (when (setq gnus-faq-buffer
5998                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5999       (gnus-configure-windows 'summary-faq))))
6000
6001 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6002 (defun gnus-summary-describe-group (&optional force)
6003   "Describe the current newsgroup."
6004   (interactive "P")
6005   (gnus-group-describe-group force gnus-newsgroup-name))
6006
6007 (defun gnus-summary-describe-briefly ()
6008   "Describe summary mode commands briefly."
6009   (interactive)
6010   (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")))
6011
6012 ;; Walking around group mode buffer from summary mode.
6013
6014 (defun gnus-summary-next-group (&optional no-article target-group backward)
6015   "Exit current newsgroup and then select next unread newsgroup.
6016 If prefix argument NO-ARTICLE is non-nil, no article is selected
6017 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6018 previous group instead."
6019   (interactive "P")
6020   ;; Stop pre-fetching.
6021   (gnus-async-halt-prefetch)
6022   (let ((current-group gnus-newsgroup-name)
6023         (current-buffer (current-buffer))
6024         entered)
6025     ;; First we semi-exit this group to update Xrefs and all variables.
6026     ;; We can't do a real exit, because the window conf must remain
6027     ;; the same in case the user is prompted for info, and we don't
6028     ;; want the window conf to change before that...
6029     (gnus-summary-exit t)
6030     (while (not entered)
6031       ;; Then we find what group we are supposed to enter.
6032       (set-buffer gnus-group-buffer)
6033       (gnus-group-jump-to-group current-group)
6034       (setq target-group
6035             (or target-group
6036                 (if (eq gnus-keep-same-level 'best)
6037                     (gnus-summary-best-group gnus-newsgroup-name)
6038                   (gnus-summary-search-group backward gnus-keep-same-level))))
6039       (if (not target-group)
6040           ;; There are no further groups, so we return to the group
6041           ;; buffer.
6042           (progn
6043             (gnus-message 5 "Returning to the group buffer")
6044             (setq entered t)
6045             (when (gnus-buffer-live-p current-buffer)
6046               (set-buffer current-buffer)
6047               (gnus-summary-exit))
6048             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6049         ;; We try to enter the target group.
6050         (gnus-group-jump-to-group target-group)
6051         (let ((unreads (gnus-group-group-unread)))
6052           (if (and (or (eq t unreads)
6053                        (and unreads (not (zerop unreads))))
6054                    (gnus-summary-read-group
6055                     target-group nil no-article
6056                     (and (buffer-name current-buffer) current-buffer)
6057                     nil backward))
6058               (setq entered t)
6059             (setq current-group target-group
6060                   target-group nil)))))))
6061
6062 (defun gnus-summary-prev-group (&optional no-article)
6063   "Exit current newsgroup and then select previous unread newsgroup.
6064 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6065   (interactive "P")
6066   (gnus-summary-next-group no-article nil t))
6067
6068 ;; Walking around summary lines.
6069
6070 (defun gnus-summary-first-subject (&optional unread undownloaded)
6071   "Go to the first unread subject.
6072 If UNREAD is non-nil, go to the first unread article.
6073 Returns the article selected or nil if there are no unread articles."
6074   (interactive "P")
6075   (prog1
6076       (cond
6077        ;; Empty summary.
6078        ((null gnus-newsgroup-data)
6079         (gnus-message 3 "No articles in the group")
6080         nil)
6081        ;; Pick the first article.
6082        ((not unread)
6083         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6084         (gnus-data-number (car gnus-newsgroup-data)))
6085        ;; No unread articles.
6086        ((null gnus-newsgroup-unreads)
6087         (gnus-message 3 "No more unread articles")
6088         nil)
6089        ;; Find the first unread article.
6090        (t
6091         (let ((data gnus-newsgroup-data))
6092           (while (and data
6093                       (and (not (and undownloaded
6094                                      (eq gnus-undownloaded-mark
6095                                          (gnus-data-mark (car data)))))
6096                            (not (gnus-data-unread-p (car data)))))
6097             (setq data (cdr data)))
6098           (when data
6099             (goto-char (gnus-data-pos (car data)))
6100             (gnus-data-number (car data))))))
6101     (gnus-summary-position-point)))
6102
6103 (defun gnus-summary-next-subject (n &optional unread dont-display)
6104   "Go to next N'th summary line.
6105 If N is negative, go to the previous N'th subject line.
6106 If UNREAD is non-nil, only unread articles are selected.
6107 The difference between N and the actual number of steps taken is
6108 returned."
6109   (interactive "p")
6110   (let ((backward (< n 0))
6111         (n (abs n)))
6112     (while (and (> n 0)
6113                 (if backward
6114                     (gnus-summary-find-prev unread)
6115                   (gnus-summary-find-next unread)))
6116       (unless (zerop (setq n (1- n)))
6117         (gnus-summary-show-thread)))
6118     (when (/= 0 n)
6119       (gnus-message 7 "No more%s articles"
6120                     (if unread " unread" "")))
6121     (unless dont-display
6122       (gnus-summary-recenter)
6123       (gnus-summary-position-point))
6124     n))
6125
6126 (defun gnus-summary-next-unread-subject (n)
6127   "Go to next N'th unread summary line."
6128   (interactive "p")
6129   (gnus-summary-next-subject n t))
6130
6131 (defun gnus-summary-prev-subject (n &optional unread)
6132   "Go to previous N'th summary line.
6133 If optional argument UNREAD is non-nil, only unread article is selected."
6134   (interactive "p")
6135   (gnus-summary-next-subject (- n) unread))
6136
6137 (defun gnus-summary-prev-unread-subject (n)
6138   "Go to previous N'th unread summary line."
6139   (interactive "p")
6140   (gnus-summary-next-subject (- n) t))
6141
6142 (defun gnus-summary-goto-subject (article &optional force silent)
6143   "Go the subject line of ARTICLE.
6144 If FORCE, also allow jumping to articles not currently shown."
6145   (interactive "nArticle number: ")
6146   (let ((b (point))
6147         (data (gnus-data-find article)))
6148     ;; We read in the article if we have to.
6149     (and (not data)
6150          force
6151          (gnus-summary-insert-subject
6152           article
6153           (if (or (numberp force) (vectorp force)) force)
6154           t)
6155          (setq data (gnus-data-find article)))
6156     (goto-char b)
6157     (if (not data)
6158         (progn
6159           (unless silent
6160             (gnus-message 3 "Can't find article %d" article))
6161           nil)
6162       (goto-char (gnus-data-pos data))
6163       (gnus-summary-position-point)
6164       article)))
6165
6166 ;; Walking around summary lines with displaying articles.
6167
6168 (defun gnus-summary-expand-window (&optional arg)
6169   "Make the summary buffer take up the entire Emacs frame.
6170 Given a prefix, will force an `article' buffer configuration."
6171   (interactive "P")
6172   (if arg
6173       (gnus-configure-windows 'article 'force)
6174     (gnus-configure-windows 'summary 'force)))
6175
6176 (defun gnus-summary-display-article (article &optional all-header)
6177   "Display ARTICLE in article buffer."
6178   (when (gnus-buffer-live-p gnus-article-buffer)
6179     (with-current-buffer gnus-article-buffer
6180       (set-buffer-multibyte t)))
6181   (gnus-set-global-variables)
6182   (when (gnus-buffer-live-p gnus-article-buffer)
6183     (with-current-buffer gnus-article-buffer
6184       (setq gnus-article-charset gnus-newsgroup-charset)
6185       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6186   (if (null article)
6187       nil
6188     (prog1
6189         (if gnus-summary-display-article-function
6190             (funcall gnus-summary-display-article-function article all-header)
6191           (gnus-article-prepare article all-header))
6192       (with-current-buffer gnus-article-buffer
6193         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6194              nil))
6195       (gnus-run-hooks 'gnus-select-article-hook)
6196       (when (and gnus-current-article
6197                  (not (zerop gnus-current-article)))
6198         (gnus-summary-goto-subject gnus-current-article))
6199       (gnus-summary-recenter)
6200       (when (and gnus-use-trees gnus-show-threads)
6201         (gnus-possibly-generate-tree article)
6202         (gnus-highlight-selected-tree article))
6203       ;; Successfully display article.
6204       (gnus-article-set-window-start
6205        (cdr (assq article gnus-newsgroup-bookmarks))))))
6206
6207 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6208   "Select the current article.
6209 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6210 non-nil, the article will be re-fetched even if it already present in
6211 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6212 be displayed."
6213   ;; Make sure we are in the summary buffer to work around bbdb bug.
6214   (unless (eq major-mode 'gnus-summary-mode)
6215     (set-buffer gnus-summary-buffer))
6216   (let ((article (or article (gnus-summary-article-number)))
6217         (all-headers (not (not all-headers))) ;Must be T or NIL.
6218         gnus-summary-display-article-function)
6219     (and (not pseudo)
6220          (gnus-summary-article-pseudo-p article)
6221          (error "This is a pseudo-article"))
6222     (save-excursion
6223       (set-buffer gnus-summary-buffer)
6224       (if (or (and gnus-single-article-buffer
6225                    (or (null gnus-current-article)
6226                        (null gnus-article-current)
6227                        (null (get-buffer gnus-article-buffer))
6228                        (not (eq article (cdr gnus-article-current)))
6229                        (not (equal (car gnus-article-current)
6230                                    gnus-newsgroup-name))))
6231               (and (not gnus-single-article-buffer)
6232                    (or (null gnus-current-article)
6233                        (not (eq gnus-current-article article))))
6234               force)
6235           ;; The requested article is different from the current article.
6236           (progn
6237             (gnus-summary-display-article article all-headers)
6238             (when (or all-headers gnus-show-all-headers)
6239               (gnus-article-show-all-headers))
6240             (gnus-article-set-window-start
6241              (cdr (assq article gnus-newsgroup-bookmarks)))
6242             article)
6243         (when (or all-headers gnus-show-all-headers)
6244           (gnus-article-show-all-headers))
6245         'old))))
6246
6247 (defun gnus-summary-force-verify-and-decrypt ()
6248   (interactive)
6249   (let ((mm-verify-option 'known)
6250         (mm-decrypt-option 'known))
6251     (gnus-summary-select-article nil 'force)))
6252
6253 (defun gnus-summary-set-current-mark (&optional current-mark)
6254   "Obsolete function."
6255   nil)
6256
6257 (defun gnus-summary-next-article (&optional unread subject backward push)
6258   "Select the next article.
6259 If UNREAD, only unread articles are selected.
6260 If SUBJECT, only articles with SUBJECT are selected.
6261 If BACKWARD, the previous article is selected instead of the next."
6262   (interactive "P")
6263   (cond
6264    ;; Is there such an article?
6265    ((and (gnus-summary-search-forward unread subject backward)
6266          (or (gnus-summary-display-article (gnus-summary-article-number))
6267              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6268     (gnus-summary-position-point))
6269    ;; If not, we try the first unread, if that is wanted.
6270    ((and subject
6271          gnus-auto-select-same
6272          (gnus-summary-first-unread-article))
6273     (gnus-summary-position-point)
6274     (gnus-message 6 "Wrapped"))
6275    ;; Try to get next/previous article not displayed in this group.
6276    ((and gnus-auto-extend-newsgroup
6277          (not unread) (not subject))
6278     (gnus-summary-goto-article
6279      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6280      nil (count-lines (point-min) (point))))
6281    ;; Go to next/previous group.
6282    (t
6283     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6284       (gnus-summary-jump-to-group gnus-newsgroup-name))
6285     (let ((cmd last-command-char)
6286           (point
6287            (save-excursion
6288              (set-buffer gnus-group-buffer)
6289              (point)))
6290           (group
6291            (if (eq gnus-keep-same-level 'best)
6292                (gnus-summary-best-group gnus-newsgroup-name)
6293              (gnus-summary-search-group backward gnus-keep-same-level))))
6294       ;; For some reason, the group window gets selected.  We change
6295       ;; it back.
6296       (select-window (get-buffer-window (current-buffer)))
6297       ;; Select next unread newsgroup automagically.
6298       (cond
6299        ((or (not gnus-auto-select-next)
6300             (not cmd))
6301         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6302        ((or (eq gnus-auto-select-next 'quietly)
6303             (and (eq gnus-auto-select-next 'slightly-quietly)
6304                  push)
6305             (and (eq gnus-auto-select-next 'almost-quietly)
6306                  (gnus-summary-last-article-p)))
6307         ;; Select quietly.
6308         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6309             (gnus-summary-exit)
6310           (gnus-message 7 "No more%s articles (%s)..."
6311                         (if unread " unread" "")
6312                         (if group (concat "selecting " group)
6313                           "exiting"))
6314           (gnus-summary-next-group nil group backward)))
6315        (t
6316         (when (gnus-key-press-event-p last-input-event)
6317           (gnus-summary-walk-group-buffer
6318            gnus-newsgroup-name cmd unread backward point))))))))
6319
6320 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6321   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6322                       (?\C-p (gnus-group-prev-unread-group 1))))
6323         (cursor-in-echo-area t)
6324         keve key group ended)
6325     (save-excursion
6326       (set-buffer gnus-group-buffer)
6327       (goto-char start)
6328       (setq group
6329             (if (eq gnus-keep-same-level 'best)
6330                 (gnus-summary-best-group gnus-newsgroup-name)
6331               (gnus-summary-search-group backward gnus-keep-same-level))))
6332     (while (not ended)
6333       (gnus-message
6334        5 "No more%s articles%s" (if unread " unread" "")
6335        (if (and group
6336                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6337            (format " (Type %s for %s [%s])"
6338                    (single-key-description cmd) group
6339                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6340          (format " (Type %s to exit %s)"
6341                  (single-key-description cmd)
6342                  gnus-newsgroup-name)))
6343       ;; Confirm auto selection.
6344       (setq key (car (setq keve (gnus-read-event-char))))
6345       (setq ended t)
6346       (cond
6347        ((assq key keystrokes)
6348         (let ((obuf (current-buffer)))
6349           (switch-to-buffer gnus-group-buffer)
6350           (when group
6351             (gnus-group-jump-to-group group))
6352           (eval (cadr (assq key keystrokes)))
6353           (setq group (gnus-group-group-name))
6354           (switch-to-buffer obuf))
6355         (setq ended nil))
6356        ((equal key cmd)
6357         (if (or (not group)
6358                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6359             (gnus-summary-exit)
6360           (gnus-summary-next-group nil group backward)))
6361        (t
6362         (push (cdr keve) unread-command-events))))))
6363
6364 (defun gnus-summary-next-unread-article ()
6365   "Select unread article after current one."
6366   (interactive)
6367   (gnus-summary-next-article
6368    (or (not (eq gnus-summary-goto-unread 'never))
6369        (gnus-summary-last-article-p (gnus-summary-article-number)))
6370    (and gnus-auto-select-same
6371         (gnus-summary-article-subject))))
6372
6373 (defun gnus-summary-prev-article (&optional unread subject)
6374   "Select the article after the current one.
6375 If UNREAD is non-nil, only unread articles are selected."
6376   (interactive "P")
6377   (gnus-summary-next-article unread subject t))
6378
6379 (defun gnus-summary-prev-unread-article ()
6380   "Select unread article before current one."
6381   (interactive)
6382   (gnus-summary-prev-article
6383    (or (not (eq gnus-summary-goto-unread 'never))
6384        (gnus-summary-first-article-p (gnus-summary-article-number)))
6385    (and gnus-auto-select-same
6386         (gnus-summary-article-subject))))
6387
6388 (defun gnus-summary-next-page (&optional lines circular)
6389   "Show next page of the selected article.
6390 If at the end of the current article, select the next article.
6391 LINES says how many lines should be scrolled up.
6392
6393 If CIRCULAR is non-nil, go to the start of the article instead of
6394 selecting the next article when reaching the end of the current
6395 article."
6396   (interactive "P")
6397   (setq gnus-summary-buffer (current-buffer))
6398   (gnus-set-global-variables)
6399   (let ((article (gnus-summary-article-number))
6400         (article-window (get-buffer-window gnus-article-buffer t))
6401         endp)
6402     ;; If the buffer is empty, we have no article.
6403     (unless article
6404       (error "No article to select"))
6405     (gnus-configure-windows 'article)
6406     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6407         (if (and (eq gnus-summary-goto-unread 'never)
6408                  (not (gnus-summary-last-article-p article)))
6409             (gnus-summary-next-article)
6410           (gnus-summary-next-unread-article))
6411       (if (or (null gnus-current-article)
6412               (null gnus-article-current)
6413               (/= article (cdr gnus-article-current))
6414               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6415           ;; Selected subject is different from current article's.
6416           (gnus-summary-display-article article)
6417         (when article-window
6418           (gnus-eval-in-buffer-window gnus-article-buffer
6419             (setq endp (gnus-article-next-page lines)))
6420           (when endp
6421             (cond (circular
6422                    (gnus-summary-beginning-of-article))
6423                   (lines
6424                    (gnus-message 3 "End of message"))
6425                   ((null lines)
6426                    (if (and (eq gnus-summary-goto-unread 'never)
6427                             (not (gnus-summary-last-article-p article)))
6428                        (gnus-summary-next-article)
6429                      (gnus-summary-next-unread-article))))))))
6430     (gnus-summary-recenter)
6431     (gnus-summary-position-point)))
6432
6433 (defun gnus-summary-prev-page (&optional lines move)
6434   "Show previous page of selected article.
6435 Argument LINES specifies lines to be scrolled down.
6436 If MOVE, move to the previous unread article if point is at
6437 the beginning of the buffer."
6438   (interactive "P")
6439   (let ((article (gnus-summary-article-number))
6440         (article-window (get-buffer-window gnus-article-buffer t))
6441         endp)
6442     (gnus-configure-windows 'article)
6443     (if (or (null gnus-current-article)
6444             (null gnus-article-current)
6445             (/= article (cdr gnus-article-current))
6446             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6447         ;; Selected subject is different from current article's.
6448         (gnus-summary-display-article article)
6449       (gnus-summary-recenter)
6450       (when article-window
6451         (gnus-eval-in-buffer-window gnus-article-buffer
6452           (setq endp (gnus-article-prev-page lines)))
6453         (when (and move endp)
6454           (cond (lines
6455                  (gnus-message 3 "Beginning of message"))
6456                 ((null lines)
6457                  (if (and (eq gnus-summary-goto-unread 'never)
6458                           (not (gnus-summary-first-article-p article)))
6459                      (gnus-summary-prev-article)
6460                    (gnus-summary-prev-unread-article))))))))
6461   (gnus-summary-position-point))
6462
6463 (defun gnus-summary-prev-page-or-article (&optional lines)
6464   "Show previous page of selected article.
6465 Argument LINES specifies lines to be scrolled down.
6466 If at the beginning of the article, go to the next article."
6467   (interactive "P")
6468   (gnus-summary-prev-page lines t))
6469
6470 (defun gnus-summary-scroll-up (lines)
6471   "Scroll up (or down) one line current article.
6472 Argument LINES specifies lines to be scrolled up (or down if negative)."
6473   (interactive "p")
6474   (gnus-configure-windows 'article)
6475   (gnus-summary-show-thread)
6476   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6477     (gnus-eval-in-buffer-window gnus-article-buffer
6478       (cond ((> lines 0)
6479              (when (gnus-article-next-page lines)
6480                (gnus-message 3 "End of message")))
6481             ((< lines 0)
6482              (gnus-article-prev-page (- lines))))))
6483   (gnus-summary-recenter)
6484   (gnus-summary-position-point))
6485
6486 (defun gnus-summary-scroll-down (lines)
6487   "Scroll down (or up) one line current article.
6488 Argument LINES specifies lines to be scrolled down (or up if negative)."
6489   (interactive "p")
6490   (gnus-summary-scroll-up (- lines)))
6491
6492 (defun gnus-summary-next-same-subject ()
6493   "Select next article which has the same subject as current one."
6494   (interactive)
6495   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6496
6497 (defun gnus-summary-prev-same-subject ()
6498   "Select previous article which has the same subject as current one."
6499   (interactive)
6500   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6501
6502 (defun gnus-summary-next-unread-same-subject ()
6503   "Select next unread article which has the same subject as current one."
6504   (interactive)
6505   (gnus-summary-next-article t (gnus-summary-article-subject)))
6506
6507 (defun gnus-summary-prev-unread-same-subject ()
6508   "Select previous unread article which has the same subject as current one."
6509   (interactive)
6510   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6511
6512 (defun gnus-summary-first-unread-article ()
6513   "Select the first unread article.
6514 Return nil if there are no unread articles."
6515   (interactive)
6516   (prog1
6517       (when (gnus-summary-first-subject t)
6518         (gnus-summary-show-thread)
6519         (gnus-summary-first-subject t)
6520         (gnus-summary-display-article (gnus-summary-article-number)))
6521     (gnus-summary-position-point)))
6522
6523 (defun gnus-summary-first-unread-subject ()
6524   "Place the point on the subject line of the first unread article.
6525 Return nil if there are no unread articles."
6526   (interactive)
6527   (prog1
6528       (when (gnus-summary-first-subject t)
6529         (gnus-summary-show-thread)
6530         (gnus-summary-first-subject t))
6531     (gnus-summary-position-point)))
6532
6533 (defun gnus-summary-first-article ()
6534   "Select the first article.
6535 Return nil if there are no articles."
6536   (interactive)
6537   (prog1
6538       (when (gnus-summary-first-subject)
6539         (gnus-summary-show-thread)
6540         (gnus-summary-first-subject)
6541         (gnus-summary-display-article (gnus-summary-article-number)))
6542     (gnus-summary-position-point)))
6543
6544 (defun gnus-summary-best-unread-article ()
6545   "Select the unread article with the highest score."
6546   (interactive)
6547   (let ((best -1000000)
6548         (data gnus-newsgroup-data)
6549         article score)
6550     (while data
6551       (and (gnus-data-unread-p (car data))
6552            (> (setq score
6553                     (gnus-summary-article-score (gnus-data-number (car data))))
6554               best)
6555            (setq best score
6556                  article (gnus-data-number (car data))))
6557       (setq data (cdr data)))
6558     (prog1
6559         (if article
6560             (gnus-summary-goto-article article)
6561           (error "No unread articles"))
6562       (gnus-summary-position-point))))
6563
6564 (defun gnus-summary-last-subject ()
6565   "Go to the last displayed subject line in the group."
6566   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6567     (when article
6568       (gnus-summary-goto-subject article))))
6569
6570 (defun gnus-summary-goto-article (article &optional all-headers force)
6571   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6572 If ALL-HEADERS is non-nil, no header lines are hidden.
6573 If FORCE, go to the article even if it isn't displayed.  If FORCE
6574 is a number, it is the line the article is to be displayed on."
6575   (interactive
6576    (list
6577     (completing-read
6578      "Article number or Message-ID: "
6579      (mapcar (lambda (number) (list (int-to-string number)))
6580              gnus-newsgroup-limit))
6581     current-prefix-arg
6582     t))
6583   (prog1
6584       (if (and (stringp article)
6585                (string-match "@" article))
6586           (gnus-summary-refer-article article)
6587         (when (stringp article)
6588           (setq article (string-to-number article)))
6589         (if (gnus-summary-goto-subject article force)
6590             (gnus-summary-display-article article all-headers)
6591           (gnus-message 4 "Couldn't go to article %s" article) nil))
6592     (gnus-summary-position-point)))
6593
6594 (defun gnus-summary-goto-last-article ()
6595   "Go to the previously read article."
6596   (interactive)
6597   (prog1
6598       (when gnus-last-article
6599         (gnus-summary-goto-article gnus-last-article nil t))
6600     (gnus-summary-position-point)))
6601
6602 (defun gnus-summary-pop-article (number)
6603   "Pop one article off the history and go to the previous.
6604 NUMBER articles will be popped off."
6605   (interactive "p")
6606   (let (to)
6607     (setq gnus-newsgroup-history
6608           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6609     (if to
6610         (gnus-summary-goto-article (car to) nil t)
6611       (error "Article history empty")))
6612   (gnus-summary-position-point))
6613
6614 ;; Summary commands and functions for limiting the summary buffer.
6615
6616 (defun gnus-summary-limit-to-articles (n)
6617   "Limit the summary buffer to the next N articles.
6618 If not given a prefix, use the process marked articles instead."
6619   (interactive "P")
6620   (prog1
6621       (let ((articles (gnus-summary-work-articles n)))
6622         (setq gnus-newsgroup-processable nil)
6623         (gnus-summary-limit articles))
6624     (gnus-summary-position-point)))
6625
6626 (defun gnus-summary-pop-limit (&optional total)
6627   "Restore the previous limit.
6628 If given a prefix, remove all limits."
6629   (interactive "P")
6630   (when total
6631     (setq gnus-newsgroup-limits
6632           (list (mapcar (lambda (h) (mail-header-number h))
6633                         gnus-newsgroup-headers))))
6634   (unless gnus-newsgroup-limits
6635     (error "No limit to pop"))
6636   (prog1
6637       (gnus-summary-limit nil 'pop)
6638     (gnus-summary-position-point)))
6639
6640 (defun gnus-summary-limit-to-subject (subject &optional header)
6641   "Limit the summary buffer to articles that have subjects that match a regexp."
6642   (interactive "sLimit to subject (regexp): ")
6643   (unless header
6644     (setq header "subject"))
6645   (when (not (equal "" subject))
6646     (prog1
6647         (let ((articles (gnus-summary-find-matching
6648                          (or header "subject") subject 'all)))
6649           (unless articles
6650             (error "Found no matches for \"%s\"" subject))
6651           (gnus-summary-limit articles))
6652       (gnus-summary-position-point))))
6653
6654 (defun gnus-summary-limit-to-author (from)
6655   "Limit the summary buffer to articles that have authors that match a regexp."
6656   (interactive "sLimit to author (regexp): ")
6657   (gnus-summary-limit-to-subject from "from"))
6658
6659 (defun gnus-summary-limit-to-age (age &optional younger-p)
6660   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6661 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6662 articles that are younger than AGE days."
6663   (interactive
6664    (let ((younger current-prefix-arg)
6665          (days-got nil)
6666          days)
6667      (while (not days-got)
6668        (setq days (if younger
6669                       (read-string "Limit to articles within (in days): ")
6670                     (read-string "Limit to articles older than (in days): ")))
6671        (when (> (length days) 0)
6672          (setq days (read days)))
6673        (if (numberp days)
6674            (setq days-got t)
6675          (message "Please enter a number.")
6676          (sleep-for 1)))
6677      (list days younger)))
6678   (prog1
6679       (let ((data gnus-newsgroup-data)
6680             (cutoff (days-to-time age))
6681             articles d date is-younger)
6682         (while (setq d (pop data))
6683           (when (and (vectorp (gnus-data-header d))
6684                      (setq date (mail-header-date (gnus-data-header d))))
6685             (setq is-younger (time-less-p
6686                               (time-since (condition-case ()
6687                                               (date-to-time date)
6688                                             (error '(0 0))))
6689                               cutoff))
6690             (when (if younger-p
6691                       is-younger
6692                     (not is-younger))
6693               (push (gnus-data-number d) articles))))
6694         (gnus-summary-limit (nreverse articles)))
6695     (gnus-summary-position-point)))
6696
6697 (defun gnus-summary-limit-to-extra (header regexp)
6698   "Limit the summary buffer to articles that match an 'extra' header."
6699   (interactive
6700    (let ((header
6701           (intern
6702            (gnus-completing-read
6703             (symbol-name (car gnus-extra-headers))
6704             "Limit extra header:"
6705             (mapcar (lambda (x)
6706                       (cons (symbol-name x) x))
6707                     gnus-extra-headers)
6708             nil
6709             t))))
6710      (list header
6711            (read-string (format "Limit to header %s (regexp): " header)))))
6712   (when (not (equal "" regexp))
6713     (prog1
6714         (let ((articles (gnus-summary-find-matching
6715                          (cons 'extra header) regexp 'all)))
6716           (unless articles
6717             (error "Found no matches for \"%s\"" regexp))
6718           (gnus-summary-limit articles))
6719       (gnus-summary-position-point))))
6720
6721 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6722 (make-obsolete
6723  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6724
6725 (defun gnus-summary-limit-to-unread (&optional all)
6726   "Limit the summary buffer to articles that are not marked as read.
6727 If ALL is non-nil, limit strictly to unread articles."
6728   (interactive "P")
6729   (if all
6730       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6731     (gnus-summary-limit-to-marks
6732      ;; Concat all the marks that say that an article is read and have
6733      ;; those removed.
6734      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6735            gnus-killed-mark gnus-kill-file-mark
6736            gnus-low-score-mark gnus-expirable-mark
6737            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6738            gnus-duplicate-mark gnus-souped-mark)
6739      'reverse)))
6740
6741 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6742 (make-obsolete 'gnus-summary-delete-marked-with
6743                'gnus-summary-limit-exlude-marks)
6744
6745 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6746   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6747 If REVERSE, limit the summary buffer to articles that are marked
6748 with MARKS.  MARKS can either be a string of marks or a list of marks.
6749 Returns how many articles were removed."
6750   (interactive "sMarks: ")
6751   (gnus-summary-limit-to-marks marks t))
6752
6753 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6754   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6755 If REVERSE (the prefix), limit the summary buffer to articles that are
6756 not marked with MARKS.  MARKS can either be a string of marks or a
6757 list of marks.
6758 Returns how many articles were removed."
6759   (interactive "sMarks: \nP")
6760   (prog1
6761       (let ((data gnus-newsgroup-data)
6762             (marks (if (listp marks) marks
6763                      (append marks nil))) ; Transform to list.
6764             articles)
6765         (while data
6766           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6767                   (memq (gnus-data-mark (car data)) marks))
6768             (push (gnus-data-number (car data)) articles))
6769           (setq data (cdr data)))
6770         (gnus-summary-limit articles))
6771     (gnus-summary-position-point)))
6772
6773 (defun gnus-summary-limit-to-score (&optional score)
6774   "Limit to articles with score at or above SCORE."
6775   (interactive "P")
6776   (setq score (if score
6777                   (prefix-numeric-value score)
6778                 (or gnus-summary-default-score 0)))
6779   (let ((data gnus-newsgroup-data)
6780         articles)
6781     (while data
6782       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6783                 score)
6784         (push (gnus-data-number (car data)) articles))
6785       (setq data (cdr data)))
6786     (prog1
6787         (gnus-summary-limit articles)
6788       (gnus-summary-position-point))))
6789
6790 (defun gnus-summary-limit-include-thread (id)
6791   "Display all the hidden articles that is in the thread with ID in it.
6792 When called interactively, ID is the Message-ID of the current
6793 article."
6794   (interactive (list (mail-header-id (gnus-summary-article-header))))
6795   (let ((articles (gnus-articles-in-thread
6796                    (gnus-id-to-thread (gnus-root-id id)))))
6797     (prog1
6798         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6799         (gnus-summary-limit-include-matching-articles
6800          "subject"
6801          (regexp-quote (gnus-simplify-subject-re
6802                         (mail-header-subject (gnus-id-to-header id)))))
6803       (gnus-summary-position-point))))
6804
6805 (defun gnus-summary-limit-include-matching-articles (header regexp)
6806   "Display all the hidden articles that have HEADERs that match REGEXP."
6807   (interactive (list (read-string "Match on header: ")
6808                      (read-string "Regexp: ")))
6809   (let ((articles (gnus-find-matching-articles header regexp)))
6810     (prog1
6811         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6812       (gnus-summary-position-point))))
6813
6814 (defun gnus-summary-limit-include-dormant ()
6815   "Display all the hidden articles that are marked as dormant.
6816 Note that this command only works on a subset of the articles currently
6817 fetched for this group."
6818   (interactive)
6819   (unless gnus-newsgroup-dormant
6820     (error "There are no dormant articles in this group"))
6821   (prog1
6822       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6823     (gnus-summary-position-point)))
6824
6825 (defun gnus-summary-limit-exclude-dormant ()
6826   "Hide all dormant articles."
6827   (interactive)
6828   (prog1
6829       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6830     (gnus-summary-position-point)))
6831
6832 (defun gnus-summary-limit-exclude-childless-dormant ()
6833   "Hide all dormant articles that have no children."
6834   (interactive)
6835   (let ((data (gnus-data-list t))
6836         articles d children)
6837     ;; Find all articles that are either not dormant or have
6838     ;; children.
6839     (while (setq d (pop data))
6840       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6841                 (and (setq children
6842                            (gnus-article-children (gnus-data-number d)))
6843                      (let (found)
6844                        (while children
6845                          (when (memq (car children) articles)
6846                            (setq children nil
6847                                  found t))
6848                          (pop children))
6849                        found)))
6850         (push (gnus-data-number d) articles)))
6851     ;; Do the limiting.
6852     (prog1
6853         (gnus-summary-limit articles)
6854       (gnus-summary-position-point))))
6855
6856 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6857   "Mark all unread excluded articles as read.
6858 If ALL, mark even excluded ticked and dormants as read."
6859   (interactive "P")
6860   (let ((articles (gnus-sorted-complement
6861                    (sort
6862                     (mapcar (lambda (h) (mail-header-number h))
6863                             gnus-newsgroup-headers)
6864                     '<)
6865                    (sort gnus-newsgroup-limit '<)))
6866         article)
6867     (setq gnus-newsgroup-unreads
6868           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6869     (if all
6870         (setq gnus-newsgroup-dormant nil
6871               gnus-newsgroup-marked nil
6872               gnus-newsgroup-reads
6873               (nconc
6874                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6875                gnus-newsgroup-reads))
6876       (while (setq article (pop articles))
6877         (unless (or (memq article gnus-newsgroup-dormant)
6878                     (memq article gnus-newsgroup-marked))
6879           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6880
6881 (defun gnus-summary-limit (articles &optional pop)
6882   (if pop
6883       ;; We pop the previous limit off the stack and use that.
6884       (setq articles (car gnus-newsgroup-limits)
6885             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6886     ;; We use the new limit, so we push the old limit on the stack.
6887     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6888   ;; Set the limit.
6889   (setq gnus-newsgroup-limit articles)
6890   (let ((total (length gnus-newsgroup-data))
6891         (data (gnus-data-find-list (gnus-summary-article-number)))
6892         (gnus-summary-mark-below nil)   ; Inhibit this.
6893         found)
6894     ;; This will do all the work of generating the new summary buffer
6895     ;; according to the new limit.
6896     (gnus-summary-prepare)
6897     ;; Hide any threads, possibly.
6898     (and gnus-show-threads
6899          gnus-thread-hide-subtree
6900          (gnus-summary-hide-all-threads))
6901     ;; Try to return to the article you were at, or one in the
6902     ;; neighborhood.
6903     (when data
6904       ;; We try to find some article after the current one.
6905       (while data
6906         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6907           (setq data nil
6908                 found t))
6909         (setq data (cdr data))))
6910     (unless found
6911       ;; If there is no data, that means that we were after the last
6912       ;; article.  The same goes when we can't find any articles
6913       ;; after the current one.
6914       (goto-char (point-max))
6915       (gnus-summary-find-prev))
6916     (gnus-set-mode-line 'summary)
6917     ;; We return how many articles were removed from the summary
6918     ;; buffer as a result of the new limit.
6919     (- total (length gnus-newsgroup-data))))
6920
6921 (defsubst gnus-invisible-cut-children (threads)
6922   (let ((num 0))
6923     (while threads
6924       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6925         (incf num))
6926       (pop threads))
6927     (< num 2)))
6928
6929 (defsubst gnus-cut-thread (thread)
6930   "Go forwards in the thread until we find an article that we want to display."
6931   (when (or (eq gnus-fetch-old-headers 'some)
6932             (eq gnus-fetch-old-headers 'invisible)
6933             (numberp gnus-fetch-old-headers)
6934             (eq gnus-build-sparse-threads 'some)
6935             (eq gnus-build-sparse-threads 'more))
6936     ;; Deal with old-fetched headers and sparse threads.
6937     (while (and
6938             thread
6939             (or
6940              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6941              (gnus-summary-article-ancient-p
6942               (mail-header-number (car thread))))
6943             (if (or (<= (length (cdr thread)) 1)
6944                     (eq gnus-fetch-old-headers 'invisible))
6945                 (setq gnus-newsgroup-limit
6946                       (delq (mail-header-number (car thread))
6947                             gnus-newsgroup-limit)
6948                       thread (cadr thread))
6949               (when (gnus-invisible-cut-children (cdr thread))
6950                 (let ((th (cdr thread)))
6951                   (while th
6952                     (if (memq (mail-header-number (caar th))
6953                               gnus-newsgroup-limit)
6954                         (setq thread (car th)
6955                               th nil)
6956                       (setq th (cdr th))))))))))
6957   thread)
6958
6959 (defun gnus-cut-threads (threads)
6960   "Cut off all uninteresting articles from the beginning of threads."
6961   (when (or (eq gnus-fetch-old-headers 'some)
6962             (eq gnus-fetch-old-headers 'invisible)
6963             (numberp gnus-fetch-old-headers)
6964             (eq gnus-build-sparse-threads 'some)
6965             (eq gnus-build-sparse-threads 'more))
6966     (let ((th threads))
6967       (while th
6968         (setcar th (gnus-cut-thread (car th)))
6969         (setq th (cdr th)))))
6970   ;; Remove nixed out threads.
6971   (delq nil threads))
6972
6973 (defun gnus-summary-initial-limit (&optional show-if-empty)
6974   "Figure out what the initial limit is supposed to be on group entry.
6975 This entails weeding out unwanted dormants, low-scored articles,
6976 fetch-old-headers verbiage, and so on."
6977   ;; Most groups have nothing to remove.
6978   (if (or gnus-inhibit-limiting
6979           (and (null gnus-newsgroup-dormant)
6980                (not (eq gnus-fetch-old-headers 'some))
6981                (not (numberp gnus-fetch-old-headers))
6982                (not (eq gnus-fetch-old-headers 'invisible))
6983                (null gnus-summary-expunge-below)
6984                (not (eq gnus-build-sparse-threads 'some))
6985                (not (eq gnus-build-sparse-threads 'more))
6986                (null gnus-thread-expunge-below)
6987                (not gnus-use-nocem)))
6988       ()                                ; Do nothing.
6989     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6990     (setq gnus-newsgroup-limit nil)
6991     (mapatoms
6992      (lambda (node)
6993        (unless (car (symbol-value node))
6994          ;; These threads have no parents -- they are roots.
6995          (let ((nodes (cdr (symbol-value node)))
6996                thread)
6997            (while nodes
6998              (if (and gnus-thread-expunge-below
6999                       (< (gnus-thread-total-score (car nodes))
7000                          gnus-thread-expunge-below))
7001                  (gnus-expunge-thread (pop nodes))
7002                (setq thread (pop nodes))
7003                (gnus-summary-limit-children thread))))))
7004      gnus-newsgroup-dependencies)
7005     ;; If this limitation resulted in an empty group, we might
7006     ;; pop the previous limit and use it instead.
7007     (when (and (not gnus-newsgroup-limit)
7008                show-if-empty)
7009       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7010     gnus-newsgroup-limit))
7011
7012 (defun gnus-summary-limit-children (thread)
7013   "Return 1 if this subthread is visible and 0 if it is not."
7014   ;; First we get the number of visible children to this thread.  This
7015   ;; is done by recursing down the thread using this function, so this
7016   ;; will really go down to a leaf article first, before slowly
7017   ;; working its way up towards the root.
7018   (when thread
7019     (let ((children
7020            (if (cdr thread)
7021                (apply '+ (mapcar 'gnus-summary-limit-children
7022                                  (cdr thread)))
7023              0))
7024           (number (mail-header-number (car thread)))
7025           score)
7026       (if (and
7027            (not (memq number gnus-newsgroup-marked))
7028            (or
7029             ;; If this article is dormant and has absolutely no visible
7030             ;; children, then this article isn't visible.
7031             (and (memq number gnus-newsgroup-dormant)
7032                  (zerop children))
7033             ;; If this is "fetch-old-headered" and there is no
7034             ;; visible children, then we don't want this article.
7035             (and (or (eq gnus-fetch-old-headers 'some)
7036                      (numberp gnus-fetch-old-headers))
7037                  (gnus-summary-article-ancient-p number)
7038                  (zerop children))
7039             ;; If this is "fetch-old-headered" and `invisible', then
7040             ;; we don't want this article.
7041             (and (eq gnus-fetch-old-headers 'invisible)
7042                  (gnus-summary-article-ancient-p number))
7043             ;; If this is a sparsely inserted article with no children,
7044             ;; we don't want it.
7045             (and (eq gnus-build-sparse-threads 'some)
7046                  (gnus-summary-article-sparse-p number)
7047                  (zerop children))
7048             ;; If we use expunging, and this article is really
7049             ;; low-scored, then we don't want this article.
7050             (when (and gnus-summary-expunge-below
7051                        (< (setq score
7052                                 (or (cdr (assq number gnus-newsgroup-scored))
7053                                     gnus-summary-default-score))
7054                           gnus-summary-expunge-below))
7055               ;; We increase the expunge-tally here, but that has
7056               ;; nothing to do with the limits, really.
7057               (incf gnus-newsgroup-expunged-tally)
7058               ;; We also mark as read here, if that's wanted.
7059               (when (and gnus-summary-mark-below
7060                          (< score gnus-summary-mark-below))
7061                 (setq gnus-newsgroup-unreads
7062                       (delq number gnus-newsgroup-unreads))
7063                 (if gnus-newsgroup-auto-expire
7064                     (push number gnus-newsgroup-expirable)
7065                   (push (cons number gnus-low-score-mark)
7066                         gnus-newsgroup-reads)))
7067               t)
7068             ;; Check NoCeM things.
7069             (if (and gnus-use-nocem
7070                      (gnus-nocem-unwanted-article-p
7071                       (mail-header-id (car thread))))
7072                 (progn
7073                   (setq gnus-newsgroup-unreads
7074                         (delq number gnus-newsgroup-unreads))
7075                   t))))
7076           ;; Nope, invisible article.
7077           0
7078         ;; Ok, this article is to be visible, so we add it to the limit
7079         ;; and return 1.
7080         (push number gnus-newsgroup-limit)
7081         1))))
7082
7083 (defun gnus-expunge-thread (thread)
7084   "Mark all articles in THREAD as read."
7085   (let* ((number (mail-header-number (car thread))))
7086     (incf gnus-newsgroup-expunged-tally)
7087     ;; We also mark as read here, if that's wanted.
7088     (setq gnus-newsgroup-unreads
7089           (delq number gnus-newsgroup-unreads))
7090     (if gnus-newsgroup-auto-expire
7091         (push number gnus-newsgroup-expirable)
7092       (push (cons number gnus-low-score-mark)
7093             gnus-newsgroup-reads)))
7094   ;; Go recursively through all subthreads.
7095   (mapcar 'gnus-expunge-thread (cdr thread)))
7096
7097 ;; Summary article oriented commands
7098
7099 (defun gnus-summary-refer-parent-article (n)
7100   "Refer parent article N times.
7101 If N is negative, go to ancestor -N instead.
7102 The difference between N and the number of articles fetched is returned."
7103   (interactive "p")
7104   (let ((skip 1)
7105         error header ref)
7106     (when (not (natnump n))
7107       (setq skip (abs n)
7108             n 1))
7109     (while (and (> n 0)
7110                 (not error))
7111       (setq header (gnus-summary-article-header))
7112       (if (and (eq (mail-header-number header)
7113                    (cdr gnus-article-current))
7114                (equal gnus-newsgroup-name
7115                       (car gnus-article-current)))
7116           ;; If we try to find the parent of the currently
7117           ;; displayed article, then we take a look at the actual
7118           ;; References header, since this is slightly more
7119           ;; reliable than the References field we got from the
7120           ;; server.
7121           (save-excursion
7122             (set-buffer gnus-original-article-buffer)
7123             (nnheader-narrow-to-headers)
7124             (unless (setq ref (message-fetch-field "references"))
7125               (setq ref (message-fetch-field "in-reply-to")))
7126             (widen))
7127         (setq ref
7128               ;; It's not the current article, so we take a bet on
7129               ;; the value we got from the server.
7130               (mail-header-references header)))
7131       (if (and ref
7132                (not (equal ref "")))
7133           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7134             (gnus-message 1 "Couldn't find parent"))
7135         (gnus-message 1 "No references in article %d"
7136                       (gnus-summary-article-number))
7137         (setq error t))
7138       (decf n))
7139     (gnus-summary-position-point)
7140     n))
7141
7142 (defun gnus-summary-refer-references ()
7143   "Fetch all articles mentioned in the References header.
7144 Return the number of articles fetched."
7145   (interactive)
7146   (let ((ref (mail-header-references (gnus-summary-article-header)))
7147         (current (gnus-summary-article-number))
7148         (n 0))
7149     (if (or (not ref)
7150             (equal ref ""))
7151         (error "No References in the current article")
7152       ;; For each Message-ID in the References header...
7153       (while (string-match "<[^>]*>" ref)
7154         (incf n)
7155         ;; ... fetch that article.
7156         (gnus-summary-refer-article
7157          (prog1 (match-string 0 ref)
7158            (setq ref (substring ref (match-end 0))))))
7159       (gnus-summary-goto-subject current)
7160       (gnus-summary-position-point)
7161       n)))
7162
7163 (defun gnus-summary-refer-thread (&optional limit)
7164   "Fetch all articles in the current thread.
7165 If LIMIT (the numerical prefix), fetch that many old headers instead
7166 of what's specified by the `gnus-refer-thread-limit' variable."
7167   (interactive "P")
7168   (let ((id (mail-header-id (gnus-summary-article-header)))
7169         (limit (if limit (prefix-numeric-value limit)
7170                  gnus-refer-thread-limit)))
7171     ;; We want to fetch LIMIT *old* headers, but we also have to
7172     ;; re-fetch all the headers in the current buffer, because many of
7173     ;; them may be undisplayed.  So we adjust LIMIT.
7174     (when (numberp limit)
7175       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7176     (unless (eq gnus-fetch-old-headers 'invisible)
7177       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7178       ;; Retrieve the headers and read them in.
7179       (if (eq (gnus-retrieve-headers
7180                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7181               'nov)
7182           (gnus-build-all-threads)
7183         (error "Can't fetch thread from backends that don't support NOV"))
7184       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7185     (gnus-summary-limit-include-thread id)))
7186
7187 (defun gnus-summary-refer-article (message-id)
7188   "Fetch an article specified by MESSAGE-ID."
7189   (interactive "sMessage-ID: ")
7190   (when (and (stringp message-id)
7191              (not (zerop (length message-id))))
7192     ;; Construct the correct Message-ID if necessary.
7193     ;; Suggested by tale@pawl.rpi.edu.
7194     (unless (string-match "^<" message-id)
7195       (setq message-id (concat "<" message-id)))
7196     (unless (string-match ">$" message-id)
7197       (setq message-id (concat message-id ">")))
7198     (let* ((header (gnus-id-to-header message-id))
7199            (sparse (and header
7200                         (gnus-summary-article-sparse-p
7201                          (mail-header-number header))
7202                         (memq (mail-header-number header)
7203                               gnus-newsgroup-limit)))
7204            number)
7205       (cond
7206        ;; If the article is present in the buffer we just go to it.
7207        ((and header
7208              (or (not (gnus-summary-article-sparse-p
7209                        (mail-header-number header)))
7210                  sparse))
7211         (prog1
7212             (gnus-summary-goto-article
7213              (mail-header-number header) nil t)
7214           (when sparse
7215             (gnus-summary-update-article (mail-header-number header)))))
7216        (t
7217         ;; We fetch the article.
7218         (catch 'found
7219           (dolist (gnus-override-method (gnus-refer-article-methods))
7220             (gnus-check-server gnus-override-method)
7221             ;; Fetch the header, and display the article.
7222             (when (setq number (gnus-summary-insert-subject message-id))
7223               (gnus-summary-select-article nil nil nil number)
7224               (throw 'found t)))
7225           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7226
7227 (defun gnus-refer-article-methods ()
7228   "Return a list of referrable methods."
7229   (cond
7230    ;; No method, so we default to current and native.
7231    ((null gnus-refer-article-method)
7232     (list gnus-current-select-method gnus-select-method))
7233    ;; Current.
7234    ((eq 'current gnus-refer-article-method)
7235     (list gnus-current-select-method))
7236    ;; List of select methods.
7237    ((not (and (symbolp (car gnus-refer-article-method))
7238               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7239     (let (out)
7240       (dolist (method gnus-refer-article-method)
7241         (push (if (eq 'current method)
7242                   gnus-current-select-method
7243                 method)
7244               out))
7245       (nreverse out)))
7246    ;; One single select method.
7247    (t
7248     (list gnus-refer-article-method))))
7249
7250 (defun gnus-summary-edit-parameters ()
7251   "Edit the group parameters of the current group."
7252   (interactive)
7253   (gnus-group-edit-group gnus-newsgroup-name 'params))
7254
7255 (defun gnus-summary-customize-parameters ()
7256   "Customize the group parameters of the current group."
7257   (interactive)
7258   (gnus-group-customize gnus-newsgroup-name))
7259
7260 (defun gnus-summary-enter-digest-group (&optional force)
7261   "Enter an nndoc group based on the current article.
7262 If FORCE, force a digest interpretation.  If not, try
7263 to guess what the document format is."
7264   (interactive "P")
7265   (let ((conf gnus-current-window-configuration))
7266     (save-excursion
7267       (gnus-summary-select-article))
7268     (setq gnus-current-window-configuration conf)
7269     (let* ((name (format "%s-%d"
7270                          (gnus-group-prefixed-name
7271                           gnus-newsgroup-name (list 'nndoc ""))
7272                          (save-excursion
7273                            (set-buffer gnus-summary-buffer)
7274                            gnus-current-article)))
7275            (ogroup gnus-newsgroup-name)
7276            (params (append (gnus-info-params (gnus-get-info ogroup))
7277                            (list (cons 'to-group ogroup))
7278                            (list (cons 'save-article-group ogroup))))
7279            (case-fold-search t)
7280            (buf (current-buffer))
7281            dig to-address)
7282       (save-excursion
7283         (set-buffer gnus-original-article-buffer)
7284         ;; Have the digest group inherit the main mail address of
7285         ;; the parent article.
7286         (when (setq to-address (or (message-fetch-field "reply-to")
7287                                    (message-fetch-field "from")))
7288           (setq params (append
7289                         (list (cons 'to-address
7290                                     (funcall gnus-decode-encoded-word-function
7291                                              to-address))))))
7292         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7293         (insert-buffer-substring gnus-original-article-buffer)
7294         ;; Remove lines that may lead nndoc to misinterpret the
7295         ;; document type.
7296         (narrow-to-region
7297          (goto-char (point-min))
7298          (or (search-forward "\n\n" nil t) (point)))
7299         (goto-char (point-min))
7300         (delete-matching-lines "^Path:\\|^From ")
7301         (widen))
7302       (unwind-protect
7303           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7304                     (gnus-newsgroup-ephemeral-ignored-charsets
7305                      gnus-newsgroup-ignored-charsets))
7306                 (gnus-group-read-ephemeral-group
7307                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7308                               (nndoc-article-type
7309                                ,(if force 'mbox 'guess))) t))
7310               ;; Make all postings to this group go to the parent group.
7311               (nconc (gnus-info-params (gnus-get-info name))
7312                      params)
7313             ;; Couldn't select this doc group.
7314             (switch-to-buffer buf)
7315             (gnus-set-global-variables)
7316             (gnus-configure-windows 'summary)
7317             (gnus-message 3 "Article couldn't be entered?"))
7318         (kill-buffer dig)))))
7319
7320 (defun gnus-summary-read-document (n)
7321   "Open a new group based on the current article(s).
7322 This will allow you to read digests and other similar
7323 documents as newsgroups.
7324 Obeys the standard process/prefix convention."
7325   (interactive "P")
7326   (let* ((articles (gnus-summary-work-articles n))
7327          (ogroup gnus-newsgroup-name)
7328          (params (append (gnus-info-params (gnus-get-info ogroup))
7329                          (list (cons 'to-group ogroup))))
7330          article group egroup groups vgroup)
7331     (while (setq article (pop articles))
7332       (setq group (format "%s-%d" gnus-newsgroup-name article))
7333       (gnus-summary-remove-process-mark article)
7334       (when (gnus-summary-display-article article)
7335         (save-excursion
7336           (with-temp-buffer
7337             (insert-buffer-substring gnus-original-article-buffer)
7338             ;; Remove some headers that may lead nndoc to make
7339             ;; the wrong guess.
7340             (message-narrow-to-head)
7341             (goto-char (point-min))
7342             (delete-matching-lines "^\\(Path\\):\\|^From ")
7343             (widen)
7344             (if (setq egroup
7345                       (gnus-group-read-ephemeral-group
7346                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7347                                      (nndoc-article-type guess))
7348                        t nil t))
7349                 (progn
7350                   ;; Make all postings to this group go to the parent group.
7351                   (nconc (gnus-info-params (gnus-get-info egroup))
7352                          params)
7353                   (push egroup groups))
7354               ;; Couldn't select this doc group.
7355               (gnus-error 3 "Article couldn't be entered"))))))
7356     ;; Now we have selected all the documents.
7357     (cond
7358      ((not groups)
7359       (error "None of the articles could be interpreted as documents"))
7360      ((gnus-group-read-ephemeral-group
7361        (setq vgroup (format
7362                      "nnvirtual:%s-%s" gnus-newsgroup-name
7363                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7364        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7365        t
7366        (cons (current-buffer) 'summary)))
7367      (t
7368       (error "Couldn't select virtual nndoc group")))))
7369
7370 (defun gnus-summary-isearch-article (&optional regexp-p)
7371   "Do incremental search forward on the current article.
7372 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7373   (interactive "P")
7374   (let* ((gnus-inhibit-treatment t)
7375          (old (gnus-summary-select-article)))
7376     (gnus-configure-windows 'article)
7377     (gnus-eval-in-buffer-window gnus-article-buffer
7378       (save-restriction
7379         (widen)
7380         (when (eq 'old old)
7381           (gnus-article-show-all-headers))
7382         (goto-char (point-min))
7383         (isearch-forward regexp-p)))))
7384
7385 (defun gnus-summary-search-article-forward (regexp &optional backward)
7386   "Search for an article containing REGEXP forward.
7387 If BACKWARD, search backward instead."
7388   (interactive
7389    (list (read-string
7390           (format "Search article %s (regexp%s): "
7391                   (if current-prefix-arg "backward" "forward")
7392                   (if gnus-last-search-regexp
7393                       (concat ", default " gnus-last-search-regexp)
7394                     "")))
7395          current-prefix-arg))
7396   (if (string-equal regexp "")
7397       (setq regexp (or gnus-last-search-regexp ""))
7398     (setq gnus-last-search-regexp regexp)
7399     (setq gnus-article-before-search gnus-current-article))
7400   ;; Intentionally set gnus-last-article.
7401   (setq gnus-last-article gnus-article-before-search)
7402   (let ((gnus-last-article gnus-last-article))
7403     (if (gnus-summary-search-article regexp backward)
7404         (gnus-summary-show-thread)
7405       (error "Search failed: \"%s\"" regexp))))
7406
7407 (defun gnus-summary-search-article-backward (regexp)
7408   "Search for an article containing REGEXP backward."
7409   (interactive
7410    (list (read-string
7411           (format "Search article backward (regexp%s): "
7412                   (if gnus-last-search-regexp
7413                       (concat ", default " gnus-last-search-regexp)
7414                     "")))))
7415   (gnus-summary-search-article-forward regexp 'backward))
7416
7417 (eval-when-compile
7418   (defmacro gnus-summary-search-article-position-point (regexp backward)
7419     "Dehighlight the last matched text and goto the beginning position."
7420     (` (if (and gnus-summary-search-article-matched-data
7421                 (let ((text (caddr gnus-summary-search-article-matched-data))
7422                       (inhibit-read-only t)
7423                       buffer-read-only)
7424                   (delete-region
7425                    (goto-char (car gnus-summary-search-article-matched-data))
7426                    (cadr gnus-summary-search-article-matched-data))
7427                   (insert text)
7428                   (string-match (, regexp) text)))
7429            (if (, backward) (beginning-of-line) (end-of-line))
7430          (goto-char (if (, backward) (point-max) (point-min))))))
7431
7432   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7433     "Place point where X-Face image is displayed."
7434     (if (featurep 'xemacs)
7435         (` (let ((end (if (search-forward "\n\n" nil t)
7436                           (goto-char (1- (point)))
7437                         (point-min)))
7438                  extent)
7439              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7440              (unless (and (re-search-forward "^From:" end t)
7441                           (setq extent (extent-at (point)))
7442                           (extent-begin-glyph extent))
7443                (goto-char (, opoint)))))
7444       (` (let ((end (if (search-forward "\n\n" nil t)
7445                         (goto-char (1- (point)))
7446                       (point-min)))
7447                (start (or (search-backward "\n\n" nil t) (point-min))))
7448            (goto-char
7449             (or (text-property-any start end 'x-face-image t);; x-face-e21
7450                 (text-property-any start end 'x-face-mule-bitmap-image t)
7451                 (, opoint)))))))
7452
7453   (defmacro gnus-summary-search-article-highlight-matched-text
7454     (backward treated x-face)
7455     "Highlight matched text in the function `gnus-summary-search-article'."
7456     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7457              (end (set-marker (make-marker) (match-end 0)))
7458              (inhibit-read-only t)
7459              buffer-read-only)
7460          (unless treated
7461            (let ((,@
7462                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7463                     (mapcar
7464                      (lambda (item) (setq items (delq item items)))
7465                      '(gnus-treat-buttonize
7466                        gnus-treat-fill-article
7467                        gnus-treat-fill-long-lines
7468                        gnus-treat-emphasize
7469                        gnus-treat-highlight-headers
7470                        gnus-treat-highlight-citation
7471                        gnus-treat-highlight-signature
7472                        gnus-treat-overstrike
7473                        gnus-treat-display-xface
7474                        gnus-treat-buttonize-head
7475                        gnus-treat-decode-article-as-default-mime-charset))
7476                     (static-if (featurep 'xemacs)
7477                         items
7478                       (cons '(x-face-mule-delete-x-face-field
7479                               (quote never))
7480                             items))))
7481                  (gnus-treat-display-xface
7482                   (when (, x-face) gnus-treat-display-xface)))
7483              (gnus-article-prepare-mime-display)))
7484          (goto-char (if (, backward) start end))
7485          (when (, x-face)
7486            (gnus-summary-search-article-highlight-goto-x-face (point)))
7487          (setq gnus-summary-search-article-matched-data
7488                (list start end (buffer-substring start end)))
7489          (unless (eq start end);; matched text has been deleted. :-<
7490            (put-text-property start end 'face
7491                               (or (find-face 'isearch)
7492                                   'secondary-selection))))))
7493   )
7494
7495 (defun gnus-summary-search-article (regexp &optional backward)
7496   "Search for an article containing REGEXP.
7497 Optional argument BACKWARD means do search for backward.
7498 `gnus-select-article-hook' is not called during the search."
7499   ;; We have to require this here to make sure that the following
7500   ;; dynamic binding isn't shadowed by autoloading.
7501   (require 'gnus-async)
7502   (require 'gnus-art)
7503   (let ((gnus-select-article-hook nil)  ;Disable hook.
7504         (gnus-article-prepare-hook nil)
7505         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7506         (gnus-use-article-prefetch nil)
7507         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7508         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7509         (sum (current-buffer))
7510         (found nil)
7511         point treated)
7512     (gnus-save-hidden-threads
7513       (static-if (featurep 'xemacs)
7514           (let ((gnus-inhibit-treatment t))
7515             (setq treated (eq 'old (gnus-summary-select-article)))
7516             (when (and treated
7517                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7518                                  (window-live-p (get-buffer-window
7519                                                  gnus-article-buffer t)))))
7520               (gnus-summary-select-article nil t)
7521               (setq treated nil)))
7522         (let ((gnus-inhibit-treatment t)
7523               (x-face-mule-delete-x-face-field 'never))
7524           (setq treated (eq 'old (gnus-summary-select-article)))
7525           (when (and treated
7526                      (not
7527                       (and (gnus-buffer-live-p gnus-article-buffer)
7528                            (window-live-p (get-buffer-window
7529                                            gnus-article-buffer t))
7530                            (or (not (string-match "^\\^X-Face:" regexp))
7531                                (with-current-buffer gnus-article-buffer
7532                                  gnus-summary-search-article-matched-data)))))
7533             (gnus-summary-select-article nil t)
7534             (setq treated nil))))
7535       (set-buffer gnus-article-buffer)
7536       (widen)
7537       (if treated
7538           (progn
7539             (gnus-article-show-all-headers)
7540             (gnus-summary-search-article-position-point regexp backward))
7541         (goto-char (if backward (point-max) (point-min))))
7542       (while (not found)
7543         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7544         (if (if backward
7545                 (re-search-backward regexp nil t)
7546               (re-search-forward regexp nil t))
7547             ;; We found the regexp.
7548             (progn
7549               (gnus-summary-search-article-highlight-matched-text
7550                backward treated (string-match "^\\^X-Face:" regexp))
7551               (setq found 'found)
7552               (forward-line
7553                (/ (- 2 (window-height
7554                         (get-buffer-window gnus-article-buffer t)))
7555                   2))
7556               (set-window-start
7557                (get-buffer-window (current-buffer))
7558                (point))
7559               (set-buffer sum)
7560               (setq point (point)))
7561           ;; We didn't find it, so we go to the next article.
7562           (set-buffer sum)
7563           (setq found 'not)
7564           (while (eq found 'not)
7565             (if (not (if backward (gnus-summary-find-prev)
7566                        (gnus-summary-find-next)))
7567                 ;; No more articles.
7568                 (setq found t)
7569               ;; Select the next article and adjust point.
7570               (unless (gnus-summary-article-sparse-p
7571                        (gnus-summary-article-number))
7572                 (setq found nil)
7573                 (let ((gnus-inhibit-treatment t))
7574                   (gnus-summary-select-article))
7575                 (setq treated nil)
7576                 (set-buffer gnus-article-buffer)
7577                 (widen)
7578                 (goto-char (if backward (point-max) (point-min))))))))
7579       (gnus-message 7 ""))
7580     ;; Return whether we found the regexp.
7581     (when (eq found 'found)
7582       (goto-char point)
7583       (gnus-summary-show-thread)
7584       (gnus-summary-goto-subject gnus-current-article)
7585       (gnus-summary-position-point)
7586       t)))
7587
7588 (defun gnus-find-matching-articles (header regexp)
7589   "Return a list of all articles that match REGEXP on HEADER.
7590 This search includes all articles in the current group that Gnus has
7591 fetched headers for, whether they are displayed or not."
7592   (let ((articles nil)
7593         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7594         (case-fold-search t))
7595     (dolist (header gnus-newsgroup-headers)
7596       (when (string-match regexp (funcall func header))
7597         (push (mail-header-number header) articles)))
7598     (nreverse articles)))
7599
7600 (defun gnus-summary-find-matching (header regexp &optional backward unread
7601                                           not-case-fold)
7602   "Return a list of all articles that match REGEXP on HEADER.
7603 The search stars on the current article and goes forwards unless
7604 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7605 If UNREAD is non-nil, only unread articles will
7606 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7607 in the comparisons."
7608   (let ((case-fold-search (not not-case-fold))
7609         articles d func)
7610     (if (consp header)
7611         (if (eq (car header) 'extra)
7612             (setq func
7613                   `(lambda (h)
7614                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7615                          "")))
7616           (error "%s is an invalid header" header))
7617       (unless (fboundp (intern (concat "mail-header-" header)))
7618         (error "%s is not a valid header" header))
7619       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7620     (dolist (d (if (eq backward 'all)
7621                    gnus-newsgroup-data
7622                  (gnus-data-find-list
7623                   (gnus-summary-article-number)
7624                   (gnus-data-list backward))))
7625       (when (and (or (not unread)       ; We want all articles...
7626                      (gnus-data-unread-p d)) ; Or just unreads.
7627                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7628                  (string-match regexp
7629                                (funcall func (gnus-data-header d)))) ; Match.
7630         (push (gnus-data-number d) articles))) ; Success!
7631     (nreverse articles)))
7632
7633 (defun gnus-summary-execute-command (header regexp command &optional backward)
7634   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7635 If HEADER is an empty string (or nil), the match is done on the entire
7636 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7637   (interactive
7638    (list (let ((completion-ignore-case t))
7639            (completing-read
7640             "Header name: "
7641             (mapcar (lambda (header) (list (format "%s" header)))
7642                     (append
7643                      '("Number" "Subject" "From" "Lines" "Date"
7644                        "Message-ID" "Xref" "References" "Body")
7645                      gnus-extra-headers))
7646             nil 'require-match))
7647          (read-string "Regexp: ")
7648          (read-key-sequence "Command: ")
7649          current-prefix-arg))
7650   (when (equal header "Body")
7651     (setq header ""))
7652   ;; Hidden thread subtrees must be searched as well.
7653   (gnus-summary-show-all-threads)
7654   ;; We don't want to change current point nor window configuration.
7655   (save-excursion
7656     (save-window-excursion
7657       (gnus-message 6 "Executing %s..." (key-description command))
7658       ;; We'd like to execute COMMAND interactively so as to give arguments.
7659       (gnus-execute header regexp
7660                     `(call-interactively ',(key-binding command))
7661                     backward)
7662       (gnus-message 6 "Executing %s...done" (key-description command)))))
7663
7664 (defun gnus-summary-beginning-of-article ()
7665   "Scroll the article back to the beginning."
7666   (interactive)
7667   (gnus-summary-select-article)
7668   (gnus-configure-windows 'article)
7669   (gnus-eval-in-buffer-window gnus-article-buffer
7670     (widen)
7671     (goto-char (point-min))
7672     (when gnus-page-broken
7673       (gnus-narrow-to-page))))
7674
7675 (defun gnus-summary-end-of-article ()
7676   "Scroll to the end of the article."
7677   (interactive)
7678   (gnus-summary-select-article)
7679   (gnus-configure-windows 'article)
7680   (gnus-eval-in-buffer-window gnus-article-buffer
7681     (widen)
7682     (goto-char (point-max))
7683     (recenter -3)
7684     (when gnus-page-broken
7685       (gnus-narrow-to-page))))
7686
7687 (defun gnus-summary-print-article (&optional filename n)
7688   "Generate and print a PostScript image of the N next (mail) articles.
7689
7690 If N is negative, print the N previous articles.  If N is nil and articles
7691 have been marked with the process mark, print these instead.
7692
7693 If the optional first argument FILENAME is nil, send the image to the
7694 printer.  If FILENAME is a string, save the PostScript image in a file with
7695 that name.  If FILENAME is a number, prompt the user for the name of the file
7696 to save in."
7697   (interactive (list (ps-print-preprint current-prefix-arg)))
7698   (dolist (article (gnus-summary-work-articles n))
7699     (gnus-summary-select-article nil nil 'pseudo article)
7700     (gnus-eval-in-buffer-window gnus-article-buffer
7701       (let ((buffer (generate-new-buffer " *print*")))
7702         (unwind-protect
7703             (progn
7704               (copy-to-buffer buffer (point-min) (point-max))
7705               (set-buffer buffer)
7706               (gnus-article-delete-invisible-text)
7707               (when (gnus-visual-p 'article-highlight 'highlight)
7708                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7709                 ;; highlight.
7710                 (let ((gnus-article-buffer buffer))
7711                   (gnus-article-highlight-citation t)
7712                   (gnus-article-highlight-signature)))
7713               (let ((ps-left-header
7714                      (list
7715                       (concat "("
7716                               (mail-header-subject gnus-current-headers) ")")
7717                       (concat "("
7718                               (mail-header-from gnus-current-headers) ")")))
7719                     (ps-right-header
7720                      (list
7721                       "/pagenumberstring load"
7722                       (concat "("
7723                               (mail-header-date gnus-current-headers) ")"))))
7724                 (gnus-run-hooks 'gnus-ps-print-hook)
7725                 (save-excursion
7726                   (if window-system
7727                       (ps-spool-buffer-with-faces)
7728                     (ps-spool-buffer)))))
7729           (kill-buffer buffer))))
7730     (gnus-summary-remove-process-mark article))
7731   (ps-despool filename))
7732
7733 (defun gnus-summary-show-article (&optional arg)
7734   "Force re-fetching of the current article.
7735 If ARG (the prefix) is a number, show the article with the charset
7736 defined in `gnus-summary-show-article-charset-alist', or the charset
7737 inputed.
7738 If ARG (the prefix) is non-nil and not a number, show the raw article
7739 without any article massaging functions being run."
7740   (interactive "P")
7741   (cond
7742    ((numberp arg)
7743     (let ((gnus-newsgroup-charset
7744            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7745                (read-coding-system "Charset: ")))
7746           (gnus-newsgroup-ignored-charsets 'gnus-all))
7747       (gnus-summary-select-article nil 'force)
7748       (let ((deps gnus-newsgroup-dependencies)
7749             head header)
7750         (save-excursion
7751           (set-buffer gnus-original-article-buffer)
7752           (save-restriction
7753             (message-narrow-to-head)
7754             (setq head (buffer-string)))
7755           (with-temp-buffer
7756             (insert (format "211 %d Article retrieved.\n"
7757                             (cdr gnus-article-current)))
7758             (insert head)
7759             (insert ".\n")
7760             (let ((nntp-server-buffer (current-buffer)))
7761               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7762         (gnus-data-set-header
7763          (gnus-data-find (cdr gnus-article-current))
7764          header)
7765         (gnus-summary-update-article-line
7766          (cdr gnus-article-current) header))))
7767    ((not arg)
7768     ;; Select the article the normal way.
7769     (gnus-summary-select-article nil 'force))
7770    (t
7771     ;; We have to require this here to make sure that the following
7772     ;; dynamic binding isn't shadowed by autoloading.
7773     (require 'gnus-async)
7774     (require 'gnus-art)
7775     ;; Bind the article treatment functions to nil.
7776     (let ((gnus-have-all-headers t)
7777           gnus-article-prepare-hook
7778           gnus-article-decode-hook
7779           gnus-break-pages
7780           gnus-show-mime
7781           (gnus-inhibit-treatment t))
7782       (gnus-summary-select-article nil 'force))))
7783   (gnus-summary-goto-subject gnus-current-article)
7784   (gnus-summary-position-point))
7785
7786 (defun gnus-summary-verbose-headers (&optional arg)
7787   "Toggle permanent full header display.
7788 If ARG is a positive number, turn header display on.
7789 If ARG is a negative number, turn header display off."
7790   (interactive "P")
7791   (setq gnus-show-all-headers
7792         (cond ((or (not (numberp arg))
7793                    (zerop arg))
7794                (not gnus-show-all-headers))
7795               ((natnump arg)
7796                t)))
7797   (gnus-summary-show-article))
7798
7799 (defun gnus-summary-toggle-header (&optional arg)
7800   "Show the headers if they are hidden, or hide them if they are shown.
7801 If ARG is a positive number, show the entire header.
7802 If ARG is a negative number, hide the unwanted header lines."
7803   (interactive "P")
7804   (save-excursion
7805     (set-buffer gnus-article-buffer)
7806     (save-restriction
7807       (let* ((buffer-read-only nil)
7808              (inhibit-point-motion-hooks t)
7809              hidden e)
7810         (setq hidden
7811               (if (numberp arg)
7812                   (>= arg 0)
7813                 (save-restriction
7814                   (article-narrow-to-head)
7815                   (gnus-article-hidden-text-p 'headers))))
7816         (goto-char (point-min))
7817         (when (search-forward "\n\n" nil t)
7818           (delete-region (point-min) (1- (point))))
7819         (goto-char (point-min))
7820         (save-excursion
7821           (set-buffer gnus-original-article-buffer)
7822           (goto-char (point-min))
7823           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7824         (insert-buffer-substring gnus-original-article-buffer 1 e)
7825         (save-restriction
7826           (narrow-to-region (point-min) (point))
7827           (article-decode-encoded-words)
7828           (if  hidden
7829               (let ((gnus-treat-hide-headers nil)
7830                     (gnus-treat-hide-boring-headers nil))
7831                 (setq gnus-article-wash-types
7832                       (delq 'headers gnus-article-wash-types))
7833                 (gnus-treat-article 'head))
7834             (gnus-treat-article 'head)))
7835         (gnus-set-mode-line 'article)))))
7836
7837 (defun gnus-summary-show-all-headers ()
7838   "Make all header lines visible."
7839   (interactive)
7840   (gnus-summary-toggle-header 1))
7841
7842 (defun gnus-summary-toggle-mime (&optional arg)
7843   "Toggle MIME processing.
7844 If ARG is a positive number, turn MIME processing on."
7845   (interactive "P")
7846   (setq gnus-show-mime
7847         (if (null arg)
7848             (not gnus-show-mime)
7849           (> (prefix-numeric-value arg) 0)))
7850   (gnus-summary-select-article t 'force))
7851
7852 (defun gnus-summary-caesar-message (&optional arg)
7853   "Caesar rotate the current article by 13.
7854 The numerical prefix specifies how many places to rotate each letter
7855 forward."
7856   (interactive "P")
7857   (gnus-summary-select-article)
7858   (let ((mail-header-separator ""))
7859     (gnus-eval-in-buffer-window gnus-article-buffer
7860       (save-restriction
7861         (widen)
7862         (let ((start (window-start))
7863               buffer-read-only)
7864           (message-caesar-buffer-body arg)
7865           (set-window-start (get-buffer-window (current-buffer)) start))))))
7866
7867 (defun gnus-summary-stop-page-breaking ()
7868   "Stop page breaking in the current article."
7869   (interactive)
7870   (gnus-summary-select-article)
7871   (gnus-eval-in-buffer-window gnus-article-buffer
7872     (widen)
7873     (when (gnus-visual-p 'page-marker)
7874       (let ((buffer-read-only nil))
7875         (gnus-remove-text-with-property 'gnus-prev)
7876         (gnus-remove-text-with-property 'gnus-next))
7877       (setq gnus-page-broken nil))))
7878
7879 (defun gnus-summary-move-article (&optional n to-newsgroup
7880                                             select-method action)
7881   "Move the current article to a different newsgroup.
7882 If N is a positive number, move the N next articles.
7883 If N is a negative number, move the N previous articles.
7884 If N is nil and any articles have been marked with the process mark,
7885 move those articles instead.
7886 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7887 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7888 re-spool using this method.
7889
7890 For this function to work, both the current newsgroup and the
7891 newsgroup that you want to move to have to support the `request-move'
7892 and `request-accept' functions.
7893
7894 ACTION can be either `move' (the default), `crosspost' or `copy'."
7895   (interactive "P")
7896   (unless action
7897     (setq action 'move))
7898   ;; Disable marking as read.
7899   (let (gnus-mark-article-hook)
7900     (save-window-excursion
7901       (gnus-summary-select-article)))
7902   ;; Check whether the source group supports the required functions.
7903   (cond ((and (eq action 'move)
7904               (not (gnus-check-backend-function
7905                     'request-move-article gnus-newsgroup-name)))
7906          (error "The current group does not support article moving"))
7907         ((and (eq action 'crosspost)
7908               (not (gnus-check-backend-function
7909                     'request-replace-article gnus-newsgroup-name)))
7910          (error "The current group does not support article editing")))
7911   (let ((articles (gnus-summary-work-articles n))
7912         (prefix (if (gnus-check-backend-function
7913                     'request-move-article gnus-newsgroup-name)
7914                     (gnus-group-real-prefix gnus-newsgroup-name)
7915                   ""))
7916         (names '((move "Move" "Moving")
7917                  (copy "Copy" "Copying")
7918                  (crosspost "Crosspost" "Crossposting")))
7919         (copy-buf (save-excursion
7920                     (nnheader-set-temp-buffer " *copy article*")))
7921         (default-marks gnus-article-mark-lists)
7922         (no-expire-marks (delete '(expirable . expire)
7923                                  (copy-sequence gnus-article-mark-lists)))
7924         art-group to-method new-xref article to-groups)
7925     (unless (assq action names)
7926       (error "Unknown action %s" action))
7927     ;; Read the newsgroup name.
7928     (when (and (not to-newsgroup)
7929                (not select-method))
7930       (setq to-newsgroup
7931             (gnus-read-move-group-name
7932              (cadr (assq action names))
7933              (symbol-value (intern (format "gnus-current-%s-group" action)))
7934              articles prefix))
7935       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7936     (setq to-method (or select-method
7937                         (gnus-server-to-method
7938                          (gnus-group-method to-newsgroup))))
7939     ;; Check the method we are to move this article to...
7940     (unless (gnus-check-backend-function
7941              'request-accept-article (car to-method))
7942       (error "%s does not support article copying" (car to-method)))
7943     (unless (gnus-check-server to-method)
7944       (error "Can't open server %s" (car to-method)))
7945     (gnus-message 6 "%s to %s: %s..."
7946                   (caddr (assq action names))
7947                   (or (car select-method) to-newsgroup) articles)
7948     (while articles
7949       (setq article (pop articles))
7950       (setq
7951        art-group
7952        (cond
7953         ;; Move the article.
7954         ((eq action 'move)
7955          ;; Remove this article from future suppression.
7956          (gnus-dup-unsuppress-article article)
7957          (gnus-request-move-article
7958           article                       ; Article to move
7959           gnus-newsgroup-name           ; From newsgroup
7960           (nth 1 (gnus-find-method-for-group
7961                   gnus-newsgroup-name)) ; Server
7962           (list 'gnus-request-accept-article
7963                 to-newsgroup (list 'quote select-method)
7964                 (not articles) t)       ; Accept form
7965           (not articles)))              ; Only save nov last time
7966         ;; Copy the article.
7967         ((eq action 'copy)
7968          (save-excursion
7969            (set-buffer copy-buf)
7970            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7971              (gnus-request-accept-article
7972               to-newsgroup select-method (not articles) t))))
7973         ;; Crosspost the article.
7974         ((eq action 'crosspost)
7975          (let ((xref (message-tokenize-header
7976                       (mail-header-xref (gnus-summary-article-header article))
7977                       " ")))
7978            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7979                                   ":" article))
7980            (unless xref
7981              (setq xref (list (system-name))))
7982            (setq new-xref
7983                  (concat
7984                   (mapconcat 'identity
7985                              (delete "Xref:" (delete new-xref xref))
7986                              " ")
7987                   " " new-xref))
7988            (save-excursion
7989              (set-buffer copy-buf)
7990              ;; First put the article in the destination group.
7991              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7992              (when (consp (setq art-group
7993                                 (gnus-request-accept-article
7994                                  to-newsgroup select-method (not articles))))
7995                (setq new-xref (concat new-xref " " (car art-group)
7996                                       ":" (cdr art-group)))
7997                ;; Now we have the new Xrefs header, so we insert
7998                ;; it and replace the new article.
7999                (nnheader-replace-header "Xref" new-xref)
8000                (gnus-request-replace-article
8001                 (cdr art-group) to-newsgroup (current-buffer))
8002                art-group))))))
8003       (cond
8004        ((not art-group)
8005         (gnus-message 1 "Couldn't %s article %s: %s"
8006                       (cadr (assq action names)) article
8007                       (nnheader-get-report (car to-method))))
8008        ((eq art-group 'junk)
8009         (when (eq action 'move)
8010           (gnus-summary-mark-article article gnus-canceled-mark)
8011           (gnus-message 4 "Deleted article %s" article)))
8012        (t
8013         (let* ((pto-group (gnus-group-prefixed-name
8014                            (car art-group) to-method))
8015                (entry
8016                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8017                (info (nth 2 entry))
8018                (to-group (gnus-info-group info))
8019                to-marks)
8020           ;; Update the group that has been moved to.
8021           (when (and info
8022                      (memq action '(move copy)))
8023             (unless (member to-group to-groups)
8024               (push to-group to-groups))
8025
8026             (unless (memq article gnus-newsgroup-unreads)
8027               (push 'read to-marks)
8028               (gnus-info-set-read
8029                info (gnus-add-to-range (gnus-info-read info)
8030                                        (list (cdr art-group)))))
8031
8032             ;; See whether the article is to be put in the cache.
8033             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8034                              default-marks
8035                            no-expire-marks))
8036                   (to-article (cdr art-group)))
8037
8038               ;; Enter the article into the cache in the new group,
8039               ;; if that is required.
8040               (when gnus-use-cache
8041                 (gnus-cache-possibly-enter-article
8042                  to-group to-article
8043                  (let ((header (copy-sequence
8044                                 (gnus-summary-article-header article))))
8045                    (mail-header-set-number header to-article)
8046                    header)
8047                  (memq article gnus-newsgroup-marked)
8048                  (memq article gnus-newsgroup-dormant)
8049                  (memq article gnus-newsgroup-unreads)))
8050
8051               (when gnus-preserve-marks
8052                 ;; Copy any marks over to the new group.
8053                 (when (and (equal to-group gnus-newsgroup-name)
8054                            (not (memq article gnus-newsgroup-unreads)))
8055                   ;; Mark this article as read in this group.
8056                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8057                   (setcdr (gnus-active to-group) to-article)
8058                   (setcdr gnus-newsgroup-active to-article))
8059
8060                 (while marks
8061                   (when (memq article (symbol-value
8062                                        (intern (format "gnus-newsgroup-%s"
8063                                                        (caar marks)))))
8064                     (push (cdar marks) to-marks)
8065                     ;; If the other group is the same as this group,
8066                     ;; then we have to add the mark to the list.
8067                     (when (equal to-group gnus-newsgroup-name)
8068                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8069                            (cons to-article
8070                                  (symbol-value
8071                                   (intern (format "gnus-newsgroup-%s"
8072                                                   (caar marks)))))))
8073                     ;; Copy the marks to other group.
8074                     (gnus-add-marked-articles
8075                      to-group (cdar marks) (list to-article) info))
8076                   (setq marks (cdr marks)))
8077
8078                 (gnus-request-set-mark to-group (list (list (list to-article)
8079                                                             'set
8080                                                             to-marks))))
8081
8082               (gnus-dribble-enter
8083                (concat "(gnus-group-set-info '"
8084                        (gnus-prin1-to-string (gnus-get-info to-group))
8085                        ")"))))
8086
8087           ;; Update the Xref header in this article to point to
8088           ;; the new crossposted article we have just created.
8089           (when (eq action 'crosspost)
8090             (save-excursion
8091               (set-buffer copy-buf)
8092               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8093               (nnheader-replace-header "Xref" new-xref)
8094               (gnus-request-replace-article
8095                article gnus-newsgroup-name (current-buffer)))))
8096
8097         ;;;!!!Why is this necessary?
8098         (set-buffer gnus-summary-buffer)
8099
8100         (gnus-summary-goto-subject article)
8101         (when (eq action 'move)
8102           (gnus-summary-mark-article article gnus-canceled-mark))))
8103       (gnus-summary-remove-process-mark article))
8104     ;; Re-activate all groups that have been moved to.
8105     (while to-groups
8106       (save-excursion
8107         (set-buffer gnus-group-buffer)
8108         (when (gnus-group-goto-group (car to-groups) t)
8109           (gnus-group-get-new-news-this-group 1 t))
8110         (pop to-groups)))
8111
8112     (gnus-kill-buffer copy-buf)
8113     (gnus-summary-position-point)
8114     (gnus-set-mode-line 'summary)))
8115
8116 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8117   "Move the current article to a different newsgroup.
8118 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8119 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8120 re-spool using this method."
8121   (interactive "P")
8122   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8123
8124 (defun gnus-summary-crosspost-article (&optional n)
8125   "Crosspost the current article to some other group."
8126   (interactive "P")
8127   (gnus-summary-move-article n nil nil 'crosspost))
8128
8129 (defcustom gnus-summary-respool-default-method nil
8130   "Default method for respooling an article.
8131 If nil, use to the current newsgroup method."
8132   :type '(choice (gnus-select-method :value (nnml ""))
8133                  (const nil))
8134   :group 'gnus-summary-mail)
8135
8136 (defun gnus-summary-respool-article (&optional n method)
8137   "Respool the current article.
8138 The article will be squeezed through the mail spooling process again,
8139 which means that it will be put in some mail newsgroup or other
8140 depending on `nnmail-split-methods'.
8141 If N is a positive number, respool the N next articles.
8142 If N is a negative number, respool the N previous articles.
8143 If N is nil and any articles have been marked with the process mark,
8144 respool those articles instead.
8145
8146 Respooling can be done both from mail groups and \"real\" newsgroups.
8147 In the former case, the articles in question will be moved from the
8148 current group into whatever groups they are destined to.  In the
8149 latter case, they will be copied into the relevant groups."
8150   (interactive
8151    (list current-prefix-arg
8152          (let* ((methods (gnus-methods-using 'respool))
8153                 (methname
8154                  (symbol-name (or gnus-summary-respool-default-method
8155                                   (car (gnus-find-method-for-group
8156                                         gnus-newsgroup-name)))))
8157                 (method
8158                  (gnus-completing-read
8159                   methname "What backend do you want to use when respooling?"
8160                   methods nil t nil 'gnus-mail-method-history))
8161                 ms)
8162            (cond
8163             ((zerop (length (setq ms (gnus-servers-using-backend
8164                                       (intern method)))))
8165              (list (intern method) ""))
8166             ((= 1 (length ms))
8167              (car ms))
8168             (t
8169              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8170                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8171                            ms-alist))))))))
8172   (unless method
8173     (error "No method given for respooling"))
8174   (if (assoc (symbol-name
8175               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8176              (gnus-methods-using 'respool))
8177       (gnus-summary-move-article n nil method)
8178     (gnus-summary-copy-article n nil method)))
8179
8180 (defun gnus-summary-import-article (file &optional edit)
8181   "Import an arbitrary file into a mail newsgroup."
8182   (interactive "fImport file: \nP")
8183   (let ((group gnus-newsgroup-name)
8184         (now (current-time))
8185         atts lines group-art)
8186     (unless (gnus-check-backend-function 'request-accept-article group)
8187       (error "%s does not support article importing" group))
8188     (or (file-readable-p file)
8189         (not (file-regular-p file))
8190         (error "Can't read %s" file))
8191     (save-excursion
8192       (set-buffer (gnus-get-buffer-create " *import file*"))
8193       (erase-buffer)
8194       (nnheader-insert-file-contents file)
8195       (goto-char (point-min))
8196       (unless (nnheader-article-p)
8197         ;; This doesn't look like an article, so we fudge some headers.
8198         (setq atts (file-attributes file)
8199               lines (count-lines (point-min) (point-max)))
8200         (insert "From: " (read-string "From: ") "\n"
8201                 "Subject: " (read-string "Subject: ") "\n"
8202                 "Date: " (message-make-date (nth 5 atts)) "\n"
8203                 "Message-ID: " (message-make-message-id) "\n"
8204                 "Lines: " (int-to-string lines) "\n"
8205                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8206       (setq group-art (gnus-request-accept-article group nil t))
8207       (kill-buffer (current-buffer)))
8208     (setq gnus-newsgroup-active (gnus-activate-group group))
8209     (forward-line 1)
8210     (gnus-summary-goto-article (cdr group-art) nil t)
8211     (when edit
8212       (gnus-summary-edit-article))))
8213
8214 (defun gnus-summary-create-article ()
8215   "Create an article in a mail newsgroup."
8216   (interactive)
8217   (let ((group gnus-newsgroup-name)
8218         (now (current-time))
8219         group-art)
8220     (unless (gnus-check-backend-function 'request-accept-article group)
8221       (error "%s does not support article importing" group))
8222     (save-excursion
8223       (set-buffer (gnus-get-buffer-create " *import file*"))
8224       (erase-buffer)
8225       (goto-char (point-min))
8226       ;; This doesn't look like an article, so we fudge some headers.
8227       (insert "From: " (read-string "From: ") "\n"
8228               "Subject: " (read-string "Subject: ") "\n"
8229               "Date: " (message-make-date now) "\n"
8230               "Message-ID: " (message-make-message-id) "\n")
8231       (setq group-art (gnus-request-accept-article group nil t))
8232       (kill-buffer (current-buffer)))
8233     (setq gnus-newsgroup-active (gnus-activate-group group))
8234     (forward-line 1)
8235     (gnus-summary-goto-article (cdr group-art) nil t)
8236     (gnus-summary-edit-article)))
8237
8238 (defun gnus-summary-article-posted-p ()
8239   "Say whether the current (mail) article is available from news as well.
8240 This will be the case if the article has both been mailed and posted."
8241   (interactive)
8242   (let ((id (mail-header-references (gnus-summary-article-header)))
8243         (gnus-override-method (car (gnus-refer-article-methods))))
8244     (if (gnus-request-head id "")
8245         (gnus-message 2 "The current message was found on %s"
8246                       gnus-override-method)
8247       (gnus-message 2 "The current message couldn't be found on %s"
8248                     gnus-override-method)
8249       nil)))
8250
8251 (defun gnus-summary-expire-articles (&optional now)
8252   "Expire all articles that are marked as expirable in the current group."
8253   (interactive)
8254   (when (gnus-check-backend-function
8255          'request-expire-articles gnus-newsgroup-name)
8256     ;; This backend supports expiry.
8257     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8258            (expirable (if total
8259                           (progn
8260                             ;; We need to update the info for
8261                             ;; this group for `gnus-list-of-read-articles'
8262                             ;; to give us the right answer.
8263                             (gnus-run-hooks 'gnus-exit-group-hook)
8264                             (gnus-summary-update-info)
8265                             (gnus-list-of-read-articles gnus-newsgroup-name))
8266                         (setq gnus-newsgroup-expirable
8267                               (sort gnus-newsgroup-expirable '<))))
8268            (expiry-wait (if now 'immediate
8269                           (gnus-group-find-parameter
8270                            gnus-newsgroup-name 'expiry-wait)))
8271            (nnmail-expiry-target
8272             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8273                 nnmail-expiry-target))
8274            es)
8275       (when expirable
8276         ;; There are expirable articles in this group, so we run them
8277         ;; through the expiry process.
8278         (gnus-message 6 "Expiring articles...")
8279         (unless (gnus-check-group gnus-newsgroup-name)
8280           (error "Can't open server for %s" gnus-newsgroup-name))
8281         ;; The list of articles that weren't expired is returned.
8282         (save-excursion
8283           (if expiry-wait
8284               (let ((nnmail-expiry-wait-function nil)
8285                     (nnmail-expiry-wait expiry-wait))
8286                 (setq es (gnus-request-expire-articles
8287                           expirable gnus-newsgroup-name)))
8288             (setq es (gnus-request-expire-articles
8289                       expirable gnus-newsgroup-name)))
8290           (unless total
8291             (setq gnus-newsgroup-expirable es))
8292           ;; We go through the old list of expirable, and mark all
8293           ;; really expired articles as nonexistent.
8294           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8295             (let ((gnus-use-cache nil))
8296               (while expirable
8297                 (unless (memq (car expirable) es)
8298                   (when (gnus-data-find (car expirable))
8299                     (gnus-summary-mark-article
8300                      (car expirable) gnus-canceled-mark)))
8301                 (setq expirable (cdr expirable))))))
8302         (gnus-message 6 "Expiring articles...done")))))
8303
8304 (defun gnus-summary-expire-articles-now ()
8305   "Expunge all expirable articles in the current group.
8306 This means that *all* articles that are marked as expirable will be
8307 deleted forever, right now."
8308   (interactive)
8309   (or gnus-expert-user
8310       (gnus-yes-or-no-p
8311        "Are you really, really, really sure you want to delete all these messages? ")
8312       (error "Phew!"))
8313   (gnus-summary-expire-articles t))
8314
8315 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8316 (defun gnus-summary-delete-article (&optional n)
8317   "Delete the N next (mail) articles.
8318 This command actually deletes articles.  This is not a marking
8319 command.  The article will disappear forever from your life, never to
8320 return.
8321 If N is negative, delete backwards.
8322 If N is nil and articles have been marked with the process mark,
8323 delete these instead."
8324   (interactive "P")
8325   (unless (gnus-check-backend-function 'request-expire-articles
8326                                        gnus-newsgroup-name)
8327     (error "The current newsgroup does not support article deletion"))
8328   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8329     (error "Couldn't open server"))
8330   ;; Compute the list of articles to delete.
8331   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8332         not-deleted)
8333     (if (and gnus-novice-user
8334              (not (gnus-yes-or-no-p
8335                    (format "Do you really want to delete %s forever? "
8336                            (if (> (length articles) 1)
8337                                (format "these %s articles" (length articles))
8338                              "this article")))))
8339         ()
8340       ;; Delete the articles.
8341       (setq not-deleted (gnus-request-expire-articles
8342                          articles gnus-newsgroup-name 'force))
8343       (while articles
8344         (gnus-summary-remove-process-mark (car articles))
8345         ;; The backend might not have been able to delete the article
8346         ;; after all.
8347         (unless (memq (car articles) not-deleted)
8348           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8349         (setq articles (cdr articles)))
8350       (when not-deleted
8351         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8352     (gnus-summary-position-point)
8353     (gnus-set-mode-line 'summary)
8354     not-deleted))
8355
8356 (defun gnus-summary-edit-article (&optional force)
8357   "Edit the current article.
8358 This will have permanent effect only in mail groups.
8359 If FORCE is non-nil, allow editing of articles even in read-only
8360 groups."
8361   (interactive "P")
8362   (save-excursion
8363     (set-buffer gnus-summary-buffer)
8364     (let ((mail-parse-charset gnus-newsgroup-charset)
8365           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8366       (gnus-set-global-variables)
8367       (when (and (not force)
8368                  (gnus-group-read-only-p))
8369         (error "The current newsgroup does not support article editing"))
8370       (gnus-summary-show-article t)
8371       (gnus-article-edit-article
8372        'ignore
8373        `(lambda (no-highlight)
8374           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8375                 (message-options message-options)
8376                 (message-options-set-recipient)
8377                 (mail-parse-ignored-charsets
8378                  ',gnus-newsgroup-ignored-charsets))
8379             (gnus-summary-edit-article-done
8380              ,(or (mail-header-references gnus-current-headers) "")
8381              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8382
8383 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8384
8385 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8386                                                  no-highlight)
8387   "Make edits to the current article permanent."
8388   (interactive)
8389   (save-excursion
8390     ;; The buffer restriction contains the entire article if it exists.
8391     (when (article-goto-body)
8392       (let ((lines (count-lines (point) (point-max)))
8393             (length (- (point-max) (point)))
8394             (case-fold-search t)
8395             (body (copy-marker (point))))
8396         (goto-char (point-min))
8397         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8398           (delete-region (match-beginning 1) (match-end 1))
8399           (insert (number-to-string length)))
8400         (goto-char (point-min))
8401         (when (re-search-forward
8402                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8403           (delete-region (match-beginning 1) (match-end 1))
8404           (insert (number-to-string length)))
8405         (goto-char (point-min))
8406         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8407           (delete-region (match-beginning 1) (match-end 1))
8408           (insert (number-to-string lines))))))
8409   ;; Replace the article.
8410   (let ((buf (current-buffer)))
8411     (with-temp-buffer
8412       (insert-buffer-substring buf)
8413
8414       (if (and (not read-only)
8415                (not (gnus-request-replace-article
8416                      (cdr gnus-article-current) (car gnus-article-current)
8417                      (current-buffer) t)))
8418           (error "Couldn't replace article")
8419         ;; Update the summary buffer.
8420         (if (and references
8421                  (equal (message-tokenize-header references " ")
8422                         (message-tokenize-header
8423                          (or (message-fetch-field "references") "") " ")))
8424             ;; We only have to update this line.
8425             (save-excursion
8426               (save-restriction
8427                 (message-narrow-to-head)
8428                 (let ((head (buffer-string))
8429                       header)
8430                   (with-temp-buffer
8431                     (insert (format "211 %d Article retrieved.\n"
8432                                     (cdr gnus-article-current)))
8433                     (insert head)
8434                     (insert ".\n")
8435                     (let ((nntp-server-buffer (current-buffer)))
8436                       (setq header (car (gnus-get-newsgroup-headers
8437                                          (save-excursion
8438                                            (set-buffer gnus-summary-buffer)
8439                                            gnus-newsgroup-dependencies)
8440                                          t))))
8441                     (save-excursion
8442                       (set-buffer gnus-summary-buffer)
8443                       (gnus-data-set-header
8444                        (gnus-data-find (cdr gnus-article-current))
8445                        header)
8446                       (gnus-summary-update-article-line
8447                        (cdr gnus-article-current) header))))))
8448           ;; Update threads.
8449           (set-buffer (or buffer gnus-summary-buffer))
8450           (gnus-summary-update-article (cdr gnus-article-current)))
8451         ;; Prettify the article buffer again.
8452         (unless no-highlight
8453           (save-excursion
8454             (set-buffer gnus-article-buffer)
8455             ;;;!!! Fix this -- article should be rehighlighted.
8456             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8457             (set-buffer gnus-original-article-buffer)
8458             (gnus-request-article
8459              (cdr gnus-article-current)
8460              (car gnus-article-current) (current-buffer))))
8461         ;; Prettify the summary buffer line.
8462         (when (gnus-visual-p 'summary-highlight 'highlight)
8463           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8464
8465 (defun gnus-summary-edit-wash (key)
8466   "Perform editing command KEY in the article buffer."
8467   (interactive
8468    (list
8469     (progn
8470       (message "%s" (concat (this-command-keys) "- "))
8471       (read-char))))
8472   (message "")
8473   (gnus-summary-edit-article)
8474   (execute-kbd-macro (concat (this-command-keys) key))
8475   (gnus-article-edit-done))
8476
8477 ;;; Respooling
8478
8479 (defun gnus-summary-respool-query (&optional silent trace)
8480   "Query where the respool algorithm would put this article."
8481   (interactive)
8482   (let (gnus-mark-article-hook)
8483     (gnus-summary-select-article)
8484     (save-excursion
8485       (set-buffer gnus-original-article-buffer)
8486       (save-restriction
8487         (message-narrow-to-head)
8488         (let ((groups (nnmail-article-group 'identity trace)))
8489           (unless silent
8490             (if groups
8491                 (message "This message would go to %s"
8492                          (mapconcat 'car groups ", "))
8493               (message "This message would go to no groups"))
8494             groups))))))
8495
8496 (defun gnus-summary-respool-trace ()
8497   "Trace where the respool algorithm would put this article.
8498 Display a buffer showing all fancy splitting patterns which matched."
8499   (interactive)
8500   (gnus-summary-respool-query nil t))
8501
8502 ;; Summary marking commands.
8503
8504 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8505   "Mark articles which has the same subject as read, and then select the next.
8506 If UNMARK is positive, remove any kind of mark.
8507 If UNMARK is negative, tick articles."
8508   (interactive "P")
8509   (when unmark
8510     (setq unmark (prefix-numeric-value unmark)))
8511   (let ((count
8512          (gnus-summary-mark-same-subject
8513           (gnus-summary-article-subject) unmark)))
8514     ;; Select next unread article.  If auto-select-same mode, should
8515     ;; select the first unread article.
8516     (gnus-summary-next-article t (and gnus-auto-select-same
8517                                       (gnus-summary-article-subject)))
8518     (gnus-message 7 "%d article%s marked as %s"
8519                   count (if (= count 1) " is" "s are")
8520                   (if unmark "unread" "read"))))
8521
8522 (defun gnus-summary-kill-same-subject (&optional unmark)
8523   "Mark articles which has the same subject as read.
8524 If UNMARK is positive, remove any kind of mark.
8525 If UNMARK is negative, tick articles."
8526   (interactive "P")
8527   (when unmark
8528     (setq unmark (prefix-numeric-value unmark)))
8529   (let ((count
8530          (gnus-summary-mark-same-subject
8531           (gnus-summary-article-subject) unmark)))
8532     ;; If marked as read, go to next unread subject.
8533     (when (null unmark)
8534       ;; Go to next unread subject.
8535       (gnus-summary-next-subject 1 t))
8536     (gnus-message 7 "%d articles are marked as %s"
8537                   count (if unmark "unread" "read"))))
8538
8539 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8540   "Mark articles with same SUBJECT as read, and return marked number.
8541 If optional argument UNMARK is positive, remove any kinds of marks.
8542 If optional argument UNMARK is negative, mark articles as unread instead."
8543   (let ((count 1))
8544     (save-excursion
8545       (cond
8546        ((null unmark)                   ; Mark as read.
8547         (while (and
8548                 (progn
8549                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8550                   (gnus-summary-show-thread) t)
8551                 (gnus-summary-find-subject subject))
8552           (setq count (1+ count))))
8553        ((> unmark 0)                    ; Tick.
8554         (while (and
8555                 (progn
8556                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8557                   (gnus-summary-show-thread) t)
8558                 (gnus-summary-find-subject subject))
8559           (setq count (1+ count))))
8560        (t                               ; Mark as unread.
8561         (while (and
8562                 (progn
8563                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8564                   (gnus-summary-show-thread) t)
8565                 (gnus-summary-find-subject subject))
8566           (setq count (1+ count)))))
8567       (gnus-set-mode-line 'summary)
8568       ;; Return the number of marked articles.
8569       count)))
8570
8571 (defun gnus-summary-mark-as-processable (n &optional unmark)
8572   "Set the process mark on the next N articles.
8573 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8574 the process mark instead.  The difference between N and the actual
8575 number of articles marked is returned."
8576   (interactive "P")
8577   (if (and (null n) (gnus-region-active-p))
8578       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8579     (setq n (prefix-numeric-value n))
8580     (let ((backward (< n 0))
8581           (n (abs n)))
8582       (while (and
8583               (> n 0)
8584               (if unmark
8585                 (gnus-summary-remove-process-mark
8586                  (gnus-summary-article-number))
8587                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8588               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8589         (setq n (1- n)))
8590       (when (/= 0 n)
8591         (gnus-message 7 "No more articles"))
8592       (gnus-summary-recenter)
8593       (gnus-summary-position-point)
8594       n)))
8595
8596 (defun gnus-summary-unmark-as-processable (n)
8597   "Remove the process mark from the next N articles.
8598 If N is negative, unmark backward instead.  The difference between N and
8599 the actual number of articles unmarked is returned."
8600   (interactive "P")
8601   (gnus-summary-mark-as-processable n t))
8602
8603 (defun gnus-summary-unmark-all-processable ()
8604   "Remove the process mark from all articles."
8605   (interactive)
8606   (save-excursion
8607     (while gnus-newsgroup-processable
8608       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8609   (gnus-summary-position-point))
8610
8611 (defun gnus-summary-add-mark (article type)
8612   "Mark ARTICLE with a mark of TYPE."
8613   (let ((vtype (car (assq type gnus-article-mark-lists)))
8614         var)
8615     (if (not vtype)
8616         (error "No such mark type: %s" type)
8617       (setq var (intern (format "gnus-newsgroup-%s" type)))
8618       (set var (cons article (symbol-value var)))
8619       (if (memq type '(processable cached replied forwarded saved))
8620           (gnus-summary-update-secondary-mark article)
8621         ;;; !!! This is bobus.  We should find out what primary
8622         ;;; !!! mark we want to set.
8623         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8624
8625 (defun gnus-summary-mark-as-expirable (n)
8626   "Mark N articles forward as expirable.
8627 If N is negative, mark backward instead.  The difference between N and
8628 the actual number of articles marked is returned."
8629   (interactive "p")
8630   (gnus-summary-mark-forward n gnus-expirable-mark))
8631
8632 (defun gnus-summary-mark-article-as-replied (article)
8633   "Mark ARTICLE as replied to and update the summary line.
8634 ARTICLE can also be a list of articles."
8635   (interactive (list (gnus-summary-article-number)))
8636   (let ((articles (if (listp article) article (list article))))
8637     (dolist (article articles)
8638       (push article gnus-newsgroup-replied)
8639       (let ((buffer-read-only nil))
8640         (when (gnus-summary-goto-subject article nil t)
8641           (gnus-summary-update-secondary-mark article))))))
8642
8643 (defun gnus-summary-mark-article-as-forwarded (article)
8644   "Mark ARTICLE as forwarded and update the summary line.
8645 ARTICLE can also be a list of articles."
8646   (let ((articles (if (listp article) article (list article))))
8647     (dolist (article articles)
8648       (push article gnus-newsgroup-forwarded)
8649       (let ((buffer-read-only nil))
8650         (when (gnus-summary-goto-subject article nil t)
8651           (gnus-summary-update-secondary-mark article))))))
8652
8653 (defun gnus-summary-set-bookmark (article)
8654   "Set a bookmark in current article."
8655   (interactive (list (gnus-summary-article-number)))
8656   (when (or (not (get-buffer gnus-article-buffer))
8657             (not gnus-current-article)
8658             (not gnus-article-current)
8659             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8660     (error "No current article selected"))
8661   ;; Remove old bookmark, if one exists.
8662   (let ((old (assq article gnus-newsgroup-bookmarks)))
8663     (when old
8664       (setq gnus-newsgroup-bookmarks
8665             (delq old gnus-newsgroup-bookmarks))))
8666   ;; Set the new bookmark, which is on the form
8667   ;; (article-number . line-number-in-body).
8668   (push
8669    (cons article
8670          (save-excursion
8671            (set-buffer gnus-article-buffer)
8672            (count-lines
8673             (min (point)
8674                  (save-excursion
8675                    (goto-char (point-min))
8676                    (search-forward "\n\n" nil t)
8677                    (point)))
8678             (point))))
8679    gnus-newsgroup-bookmarks)
8680   (gnus-message 6 "A bookmark has been added to the current article."))
8681
8682 (defun gnus-summary-remove-bookmark (article)
8683   "Remove the bookmark from the current article."
8684   (interactive (list (gnus-summary-article-number)))
8685   ;; Remove old bookmark, if one exists.
8686   (let ((old (assq article gnus-newsgroup-bookmarks)))
8687     (if old
8688         (progn
8689           (setq gnus-newsgroup-bookmarks
8690                 (delq old gnus-newsgroup-bookmarks))
8691           (gnus-message 6 "Removed bookmark."))
8692       (gnus-message 6 "No bookmark in current article."))))
8693
8694 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8695 (defun gnus-summary-mark-as-dormant (n)
8696   "Mark N articles forward as dormant.
8697 If N is negative, mark backward instead.  The difference between N and
8698 the actual number of articles marked is returned."
8699   (interactive "p")
8700   (gnus-summary-mark-forward n gnus-dormant-mark))
8701
8702 (defun gnus-summary-set-process-mark (article)
8703   "Set the process mark on ARTICLE and update the summary line."
8704   (setq gnus-newsgroup-processable
8705         (cons article
8706               (delq article gnus-newsgroup-processable)))
8707   (when (gnus-summary-goto-subject article)
8708     (gnus-summary-show-thread)
8709     (gnus-summary-goto-subject article)
8710     (gnus-summary-update-secondary-mark article)))
8711
8712 (defun gnus-summary-remove-process-mark (article)
8713   "Remove the process mark from ARTICLE and update the summary line."
8714   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8715   (when (gnus-summary-goto-subject article)
8716     (gnus-summary-show-thread)
8717     (gnus-summary-goto-subject article)
8718     (gnus-summary-update-secondary-mark article)))
8719
8720 (defun gnus-summary-set-saved-mark (article)
8721   "Set the process mark on ARTICLE and update the summary line."
8722   (push article gnus-newsgroup-saved)
8723   (when (gnus-summary-goto-subject article)
8724     (gnus-summary-update-secondary-mark article)))
8725
8726 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8727   "Mark N articles as read forwards.
8728 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8729 The difference between N and the actual number of articles marked is
8730 returned.
8731 Iff NO-EXPIRE, auto-expiry will be inhibited."
8732   (interactive "p")
8733   (gnus-summary-show-thread)
8734   (let ((backward (< n 0))
8735         (gnus-summary-goto-unread
8736          (and gnus-summary-goto-unread
8737               (not (eq gnus-summary-goto-unread 'never))
8738               (not (memq mark (list gnus-unread-mark
8739                                     gnus-ticked-mark gnus-dormant-mark)))))
8740         (n (abs n))
8741         (mark (or mark gnus-del-mark)))
8742     (while (and (> n 0)
8743                 (gnus-summary-mark-article nil mark no-expire)
8744                 (zerop (gnus-summary-next-subject
8745                         (if backward -1 1)
8746                         (and gnus-summary-goto-unread
8747                              (not (eq gnus-summary-goto-unread 'never)))
8748                         t)))
8749       (setq n (1- n)))
8750     (when (/= 0 n)
8751       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8752     (gnus-summary-recenter)
8753     (gnus-summary-position-point)
8754     (gnus-set-mode-line 'summary)
8755     n))
8756
8757 (defun gnus-summary-mark-article-as-read (mark)
8758   "Mark the current article quickly as read with MARK."
8759   (let ((article (gnus-summary-article-number)))
8760     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8761     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8762     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8763     (push (cons article mark) gnus-newsgroup-reads)
8764     ;; Possibly remove from cache, if that is used.
8765     (when gnus-use-cache
8766       (gnus-cache-enter-remove-article article))
8767     ;; Allow the backend to change the mark.
8768     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8769     ;; Check for auto-expiry.
8770     (when (and gnus-newsgroup-auto-expire
8771                (memq mark gnus-auto-expirable-marks))
8772       (setq mark gnus-expirable-mark)
8773       ;; Let the backend know about the mark change.
8774       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8775       (push article gnus-newsgroup-expirable))
8776     ;; Set the mark in the buffer.
8777     (gnus-summary-update-mark mark 'unread)
8778     t))
8779
8780 (defun gnus-summary-mark-article-as-unread (mark)
8781   "Mark the current article quickly as unread with MARK."
8782   (let* ((article (gnus-summary-article-number))
8783          (old-mark (gnus-summary-article-mark article)))
8784     ;; Allow the backend to change the mark.
8785     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8786     (if (eq mark old-mark)
8787         t
8788       (if (<= article 0)
8789           (progn
8790             (gnus-error 1 "Can't mark negative article numbers")
8791             nil)
8792         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8793         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8794         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8795         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8796         (cond ((= mark gnus-ticked-mark)
8797                (push article gnus-newsgroup-marked))
8798               ((= mark gnus-dormant-mark)
8799                (push article gnus-newsgroup-dormant))
8800               (t
8801                (push article gnus-newsgroup-unreads)))
8802         (gnus-pull article gnus-newsgroup-reads)
8803
8804         ;; See whether the article is to be put in the cache.
8805         (and gnus-use-cache
8806              (vectorp (gnus-summary-article-header article))
8807              (save-excursion
8808                (gnus-cache-possibly-enter-article
8809                 gnus-newsgroup-name article
8810                 (gnus-summary-article-header article)
8811                 (= mark gnus-ticked-mark)
8812                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8813
8814         ;; Fix the mark.
8815         (gnus-summary-update-mark mark 'unread)
8816         t))))
8817
8818 (defun gnus-summary-mark-article (&optional article mark no-expire)
8819   "Mark ARTICLE with MARK.  MARK can be any character.
8820 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8821 `??' (dormant) and `?E' (expirable).
8822 If MARK is nil, then the default character `?r' is used.
8823 If ARTICLE is nil, then the article on the current line will be
8824 marked.
8825 Iff NO-EXPIRE, auto-expiry will be inhibited."
8826   ;; The mark might be a string.
8827   (when (stringp mark)
8828     (setq mark (aref mark 0)))
8829   ;; If no mark is given, then we check auto-expiring.
8830   (when (null mark)
8831     (setq mark gnus-del-mark))
8832   (when (and (not no-expire)
8833              gnus-newsgroup-auto-expire
8834              (memq mark gnus-auto-expirable-marks))
8835     (setq mark gnus-expirable-mark))
8836   (let ((article (or article (gnus-summary-article-number)))
8837         (old-mark (gnus-summary-article-mark article)))
8838     ;; Allow the backend to change the mark.
8839     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8840     (if (eq mark old-mark)
8841         t
8842       (unless article
8843         (error "No article on current line"))
8844       (if (not (if (or (= mark gnus-unread-mark)
8845                        (= mark gnus-ticked-mark)
8846                        (= mark gnus-dormant-mark))
8847                    (gnus-mark-article-as-unread article mark)
8848                  (gnus-mark-article-as-read article mark)))
8849           t
8850         ;; See whether the article is to be put in the cache.
8851         (and gnus-use-cache
8852              (not (= mark gnus-canceled-mark))
8853              (vectorp (gnus-summary-article-header article))
8854              (save-excursion
8855                (gnus-cache-possibly-enter-article
8856                 gnus-newsgroup-name article
8857                 (gnus-summary-article-header article)
8858                 (= mark gnus-ticked-mark)
8859                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8860
8861         (when (gnus-summary-goto-subject article nil t)
8862           (let ((buffer-read-only nil))
8863             (gnus-summary-show-thread)
8864             ;; Fix the mark.
8865             (gnus-summary-update-mark mark 'unread)
8866             t))))))
8867
8868 (defun gnus-summary-update-secondary-mark (article)
8869   "Update the secondary (read, process, cache) mark."
8870   (gnus-summary-update-mark
8871    (cond ((memq article gnus-newsgroup-processable)
8872           gnus-process-mark)
8873          ((memq article gnus-newsgroup-cached)
8874           gnus-cached-mark)
8875          ((memq article gnus-newsgroup-replied)
8876           gnus-replied-mark)
8877          ((memq article gnus-newsgroup-forwarded)
8878           gnus-forwarded-mark)
8879          ((memq article gnus-newsgroup-saved)
8880           gnus-saved-mark)
8881          (t gnus-no-mark))
8882    'replied)
8883   (when (gnus-visual-p 'summary-highlight 'highlight)
8884     (gnus-run-hooks 'gnus-summary-update-hook))
8885   t)
8886
8887 (defun gnus-summary-update-mark (mark type)
8888   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8889         (buffer-read-only nil))
8890     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8891     (when forward
8892       (when (looking-at "\r")
8893         (incf forward))
8894       (when (<= (+ forward (point)) (point-max))
8895         ;; Go to the right position on the line.
8896         (goto-char (+ forward (point)))
8897         ;; Replace the old mark with the new mark.
8898         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8899         ;; Optionally update the marks by some user rule.
8900         (when (eq type 'unread)
8901           (gnus-data-set-mark
8902            (gnus-data-find (gnus-summary-article-number)) mark)
8903           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8904
8905 (defun gnus-mark-article-as-read (article &optional mark)
8906   "Enter ARTICLE in the pertinent lists and remove it from others."
8907   ;; Make the article expirable.
8908   (let ((mark (or mark gnus-del-mark)))
8909     (if (= mark gnus-expirable-mark)
8910         (push article gnus-newsgroup-expirable)
8911       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8912     ;; Remove from unread and marked lists.
8913     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8914     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8915     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8916     (push (cons article mark) gnus-newsgroup-reads)
8917     ;; Possibly remove from cache, if that is used.
8918     (when gnus-use-cache
8919       (gnus-cache-enter-remove-article article))
8920     t))
8921
8922 (defun gnus-mark-article-as-unread (article &optional mark)
8923   "Enter ARTICLE in the pertinent lists and remove it from others."
8924   (let ((mark (or mark gnus-ticked-mark)))
8925     (if (<= article 0)
8926         (progn
8927           (gnus-error 1 "Can't mark negative article numbers")
8928           nil)
8929       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8930             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8931             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8932             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8933
8934       ;; Unsuppress duplicates?
8935       (when gnus-suppress-duplicates
8936         (gnus-dup-unsuppress-article article))
8937
8938       (cond ((= mark gnus-ticked-mark)
8939              (push article gnus-newsgroup-marked))
8940             ((= mark gnus-dormant-mark)
8941              (push article gnus-newsgroup-dormant))
8942             (t
8943              (push article gnus-newsgroup-unreads)))
8944       (gnus-pull article gnus-newsgroup-reads)
8945       t)))
8946
8947 (defalias 'gnus-summary-mark-as-unread-forward
8948   'gnus-summary-tick-article-forward)
8949 (make-obsolete 'gnus-summary-mark-as-unread-forward
8950                'gnus-summary-tick-article-forward)
8951 (defun gnus-summary-tick-article-forward (n)
8952   "Tick N articles forwards.
8953 If N is negative, tick backwards instead.
8954 The difference between N and the number of articles ticked is returned."
8955   (interactive "p")
8956   (gnus-summary-mark-forward n gnus-ticked-mark))
8957
8958 (defalias 'gnus-summary-mark-as-unread-backward
8959   'gnus-summary-tick-article-backward)
8960 (make-obsolete 'gnus-summary-mark-as-unread-backward
8961                'gnus-summary-tick-article-backward)
8962 (defun gnus-summary-tick-article-backward (n)
8963   "Tick N articles backwards.
8964 The difference between N and the number of articles ticked is returned."
8965   (interactive "p")
8966   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8967
8968 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8969 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8970 (defun gnus-summary-tick-article (&optional article clear-mark)
8971   "Mark current article as unread.
8972 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8973 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8974   (interactive)
8975   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8976                                        gnus-ticked-mark)))
8977
8978 (defun gnus-summary-mark-as-read-forward (n)
8979   "Mark N articles as read forwards.
8980 If N is negative, mark backwards instead.
8981 The difference between N and the actual number of articles marked is
8982 returned."
8983   (interactive "p")
8984   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8985
8986 (defun gnus-summary-mark-as-read-backward (n)
8987   "Mark the N articles as read backwards.
8988 The difference between N and the actual number of articles marked is
8989 returned."
8990   (interactive "p")
8991   (gnus-summary-mark-forward
8992    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8993
8994 (defun gnus-summary-mark-as-read (&optional article mark)
8995   "Mark current article as read.
8996 ARTICLE specifies the article to be marked as read.
8997 MARK specifies a string to be inserted at the beginning of the line."
8998   (gnus-summary-mark-article article mark))
8999
9000 (defun gnus-summary-clear-mark-forward (n)
9001   "Clear marks from N articles forward.
9002 If N is negative, clear backward instead.
9003 The difference between N and the number of marks cleared is returned."
9004   (interactive "p")
9005   (gnus-summary-mark-forward n gnus-unread-mark))
9006
9007 (defun gnus-summary-clear-mark-backward (n)
9008   "Clear marks from N articles backward.
9009 The difference between N and the number of marks cleared is returned."
9010   (interactive "p")
9011   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9012
9013 (defun gnus-summary-mark-unread-as-read ()
9014   "Intended to be used by `gnus-summary-mark-article-hook'."
9015   (when (memq gnus-current-article gnus-newsgroup-unreads)
9016     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9017
9018 (defun gnus-summary-mark-read-and-unread-as-read ()
9019   "Intended to be used by `gnus-summary-mark-article-hook'."
9020   (let ((mark (gnus-summary-article-mark)))
9021     (when (or (gnus-unread-mark-p mark)
9022               (gnus-read-mark-p mark))
9023       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9024
9025 (defun gnus-summary-mark-unread-as-ticked ()
9026    "Intended to be used by `gnus-summary-mark-article-hook'."
9027   (when (memq gnus-current-article gnus-newsgroup-unreads)
9028     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9029
9030 (defun gnus-summary-mark-region-as-read (point mark all)
9031   "Mark all unread articles between point and mark as read.
9032 If given a prefix, mark all articles between point and mark as read,
9033 even ticked and dormant ones."
9034   (interactive "r\nP")
9035   (save-excursion
9036     (let (article)
9037       (goto-char point)
9038       (beginning-of-line)
9039       (while (and
9040               (< (point) mark)
9041               (progn
9042                 (when (or all
9043                           (memq (setq article (gnus-summary-article-number))
9044                                 gnus-newsgroup-unreads))
9045                   (gnus-summary-mark-article article gnus-del-mark))
9046                 t)
9047               (gnus-summary-find-next))))))
9048
9049 (defun gnus-summary-mark-below (score mark)
9050   "Mark articles with score less than SCORE with MARK."
9051   (interactive "P\ncMark: ")
9052   (setq score (if score
9053                   (prefix-numeric-value score)
9054                 (or gnus-summary-default-score 0)))
9055   (save-excursion
9056     (set-buffer gnus-summary-buffer)
9057     (goto-char (point-min))
9058     (while
9059         (progn
9060           (and (< (gnus-summary-article-score) score)
9061                (gnus-summary-mark-article nil mark))
9062           (gnus-summary-find-next)))))
9063
9064 (defun gnus-summary-kill-below (&optional score)
9065   "Mark articles with score below SCORE as read."
9066   (interactive "P")
9067   (gnus-summary-mark-below score gnus-killed-mark))
9068
9069 (defun gnus-summary-clear-above (&optional score)
9070   "Clear all marks from articles with score above SCORE."
9071   (interactive "P")
9072   (gnus-summary-mark-above score gnus-unread-mark))
9073
9074 (defun gnus-summary-tick-above (&optional score)
9075   "Tick all articles with score above SCORE."
9076   (interactive "P")
9077   (gnus-summary-mark-above score gnus-ticked-mark))
9078
9079 (defun gnus-summary-mark-above (score mark)
9080   "Mark articles with score over SCORE with MARK."
9081   (interactive "P\ncMark: ")
9082   (setq score (if score
9083                   (prefix-numeric-value score)
9084                 (or gnus-summary-default-score 0)))
9085   (save-excursion
9086     (set-buffer gnus-summary-buffer)
9087     (goto-char (point-min))
9088     (while (and (progn
9089                   (when (> (gnus-summary-article-score) score)
9090                     (gnus-summary-mark-article nil mark))
9091                   t)
9092                 (gnus-summary-find-next)))))
9093
9094 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9095 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9096 (defun gnus-summary-limit-include-expunged (&optional no-error)
9097   "Display all the hidden articles that were expunged for low scores."
9098   (interactive)
9099   (let ((buffer-read-only nil))
9100     (let ((scored gnus-newsgroup-scored)
9101           headers h)
9102       (while scored
9103         (unless (gnus-summary-article-header (caar scored))
9104           (and (setq h (gnus-number-to-header (caar scored)))
9105                (< (cdar scored) gnus-summary-expunge-below)
9106                (push h headers)))
9107         (setq scored (cdr scored)))
9108       (if (not headers)
9109           (when (not no-error)
9110             (error "No expunged articles hidden"))
9111         (goto-char (point-min))
9112         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9113         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9114         (mapcar (lambda (x) (push (mail-header-number x) 
9115                                   gnus-newsgroup-limit))
9116                 headers)
9117         (gnus-summary-prepare-unthreaded (nreverse headers))
9118         (goto-char (point-min))
9119         (gnus-summary-position-point)
9120         t))))
9121
9122 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9123   "Mark all unread articles in this newsgroup as read.
9124 If prefix argument ALL is non-nil, ticked and dormant articles will
9125 also be marked as read.
9126 If QUIETLY is non-nil, no questions will be asked.
9127 If TO-HERE is non-nil, it should be a point in the buffer.  All
9128 articles before this point will be marked as read.
9129 Note that this function will only catch up the unread article
9130 in the current summary buffer limitation.
9131 The number of articles marked as read is returned."
9132   (interactive "P")
9133   (prog1
9134       (save-excursion
9135         (when (or quietly
9136                   (not gnus-interactive-catchup) ;Without confirmation?
9137                   gnus-expert-user
9138                   (gnus-y-or-n-p
9139                    (if all
9140                        "Mark absolutely all articles as read? "
9141                      "Mark all unread articles as read? ")))
9142           (if (and not-mark
9143                    (not gnus-newsgroup-adaptive)
9144                    (not gnus-newsgroup-auto-expire)
9145                    (not gnus-suppress-duplicates)
9146                    (or (not gnus-use-cache)
9147                        (eq gnus-use-cache 'passive)))
9148               (progn
9149                 (when all
9150                   (setq gnus-newsgroup-marked nil
9151                         gnus-newsgroup-dormant nil))
9152                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9153             ;; We actually mark all articles as canceled, which we
9154             ;; have to do when using auto-expiry or adaptive scoring.
9155             (gnus-summary-show-all-threads)
9156             (when (gnus-summary-first-subject (not all) t)
9157               (while (and
9158                       (if to-here (< (point) to-here) t)
9159                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
9160                       (gnus-summary-find-next (not all) nil nil t))))
9161             (gnus-set-mode-line 'summary))
9162           t))
9163     (gnus-summary-position-point)))
9164
9165 (defun gnus-summary-catchup-to-here (&optional all)
9166   "Mark all unticked articles before the current one as read.
9167 If ALL is non-nil, also mark ticked and dormant articles as read."
9168   (interactive "P")
9169   (save-excursion
9170     (gnus-save-hidden-threads
9171       (let ((beg (point)))
9172         ;; We check that there are unread articles.
9173         (when (or all (gnus-summary-find-prev))
9174           (gnus-summary-catchup all t beg)))))
9175   (gnus-summary-position-point))
9176
9177 (defun gnus-summary-catchup-all (&optional quietly)
9178   "Mark all articles in this newsgroup as read."
9179   (interactive "P")
9180   (gnus-summary-catchup t quietly))
9181
9182 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9183   "Mark all unread articles in this group as read, then exit.
9184 If prefix argument ALL is non-nil, all articles are marked as read.
9185 If QUIETLY is non-nil, no questions will be asked."
9186   (interactive "P")
9187   (when (gnus-summary-catchup all quietly nil 'fast)
9188     ;; Select next newsgroup or exit.
9189     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9190              (eq gnus-auto-select-next 'quietly))
9191         (gnus-summary-next-group nil)
9192       (gnus-summary-exit))))
9193
9194 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9195   "Mark all articles in this newsgroup as read, and then exit."
9196   (interactive "P")
9197   (gnus-summary-catchup-and-exit t quietly))
9198
9199 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9200   "Mark all articles in this group as read and select the next group.
9201 If given a prefix, mark all articles, unread as well as ticked, as
9202 read."
9203   (interactive "P")
9204   (save-excursion
9205     (gnus-summary-catchup all))
9206   (gnus-summary-next-group))
9207
9208 ;;;
9209 ;;; with article
9210 ;;;
9211
9212 (defmacro gnus-with-article (article &rest forms)
9213   "Select ARTICLE and perform FORMS in the original article buffer.
9214 Then replace the article with the result."
9215   `(progn
9216      ;; We don't want the article to be marked as read.
9217      (let (gnus-mark-article-hook)
9218        (gnus-summary-select-article t t nil ,article))
9219      (set-buffer gnus-original-article-buffer)
9220      ,@forms
9221      (if (not (gnus-check-backend-function
9222                'request-replace-article (car gnus-article-current)))
9223          (gnus-message 5 "Read-only group; not replacing")
9224        (unless (gnus-request-replace-article
9225                 ,article (car gnus-article-current)
9226                 (current-buffer) t)
9227          (error "Couldn't replace article")))
9228      ;; The cache and backlog have to be flushed somewhat.
9229      (when gnus-keep-backlog
9230        (gnus-backlog-remove-article
9231         (car gnus-article-current) (cdr gnus-article-current)))
9232      (when gnus-use-cache
9233        (gnus-cache-update-article
9234         (car gnus-article-current) (cdr gnus-article-current)))))
9235
9236 (put 'gnus-with-article 'lisp-indent-function 1)
9237 (put 'gnus-with-article 'edebug-form-spec '(form body))
9238
9239 ;; Thread-based commands.
9240
9241 (defun gnus-summary-articles-in-thread (&optional article)
9242   "Return a list of all articles in the current thread.
9243 If ARTICLE is non-nil, return all articles in the thread that starts
9244 with that article."
9245   (let* ((article (or article (gnus-summary-article-number)))
9246          (data (gnus-data-find-list article))
9247          (top-level (gnus-data-level (car data)))
9248          (top-subject
9249           (cond ((null gnus-thread-operation-ignore-subject)
9250                  (gnus-simplify-subject-re
9251                   (mail-header-subject (gnus-data-header (car data)))))
9252                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9253                  (gnus-simplify-subject-fuzzy
9254                   (mail-header-subject (gnus-data-header (car data)))))
9255                 (t nil)))
9256          (end-point (save-excursion
9257                       (if (gnus-summary-go-to-next-thread)
9258                           (point) (point-max))))
9259          articles)
9260     (while (and data
9261                 (< (gnus-data-pos (car data)) end-point))
9262       (when (or (not top-subject)
9263                 (string= top-subject
9264                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9265                              (gnus-simplify-subject-fuzzy
9266                               (mail-header-subject
9267                                (gnus-data-header (car data))))
9268                            (gnus-simplify-subject-re
9269                             (mail-header-subject
9270                              (gnus-data-header (car data)))))))
9271         (push (gnus-data-number (car data)) articles))
9272       (unless (and (setq data (cdr data))
9273                    (> (gnus-data-level (car data)) top-level))
9274         (setq data nil)))
9275     ;; Return the list of articles.
9276     (nreverse articles)))
9277
9278 (defun gnus-summary-rethread-current ()
9279   "Rethread the thread the current article is part of."
9280   (interactive)
9281   (let* ((gnus-show-threads t)
9282          (article (gnus-summary-article-number))
9283          (id (mail-header-id (gnus-summary-article-header)))
9284          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9285     (unless id
9286       (error "No article on the current line"))
9287     (gnus-rebuild-thread id)
9288     (gnus-summary-goto-subject article)))
9289
9290 (defun gnus-summary-reparent-thread ()
9291   "Make the current article child of the marked (or previous) article.
9292
9293 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9294 is non-nil or the Subject: of both articles are the same."
9295   (interactive)
9296   (unless (not (gnus-group-read-only-p))
9297     (error "The current newsgroup does not support article editing"))
9298   (unless (<= (length gnus-newsgroup-processable) 1)
9299     (error "No more than one article may be marked"))
9300   (save-window-excursion
9301     (let ((gnus-article-buffer " *reparent*")
9302           (current-article (gnus-summary-article-number))
9303           ;; First grab the marked article, otherwise one line up.
9304           (parent-article (if (not (null gnus-newsgroup-processable))
9305                               (car gnus-newsgroup-processable)
9306                             (save-excursion
9307                               (if (eq (forward-line -1) 0)
9308                                   (gnus-summary-article-number)
9309                                 (error "Beginning of summary buffer"))))))
9310       (unless (not (eq current-article parent-article))
9311         (error "An article may not be self-referential"))
9312       (let ((message-id (mail-header-id
9313                          (gnus-summary-article-header parent-article))))
9314         (unless (and message-id (not (equal message-id "")))
9315           (error "No message-id in desired parent"))
9316         (gnus-with-article current-article
9317           (save-restriction
9318             (goto-char (point-min))
9319             (message-narrow-to-head)
9320             (if (re-search-forward "^References: " nil t)
9321                 (progn
9322                   (re-search-forward "^[^ \t]" nil t)
9323                   (forward-line -1)
9324                   (end-of-line)
9325                   (insert " " message-id))
9326               (insert "References: " message-id "\n"))))
9327         (set-buffer gnus-summary-buffer)
9328         (gnus-summary-unmark-all-processable)
9329         (gnus-summary-update-article current-article)
9330         (gnus-summary-rethread-current)
9331         (gnus-message 3 "Article %d is now the child of article %d"
9332                       current-article parent-article)))))
9333
9334 (defun gnus-summary-toggle-threads (&optional arg)
9335   "Toggle showing conversation threads.
9336 If ARG is positive number, turn showing conversation threads on."
9337   (interactive "P")
9338   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9339     (setq gnus-show-threads
9340           (if (null arg) (not gnus-show-threads)
9341             (> (prefix-numeric-value arg) 0)))
9342     (gnus-summary-prepare)
9343     (gnus-summary-goto-subject current)
9344     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9345     (gnus-summary-position-point)))
9346
9347 (defun gnus-summary-show-all-threads ()
9348   "Show all threads."
9349   (interactive)
9350   (save-excursion
9351     (let ((buffer-read-only nil))
9352       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9353   (gnus-summary-position-point))
9354
9355 (defun gnus-summary-show-thread ()
9356   "Show thread subtrees.
9357 Returns nil if no thread was there to be shown."
9358   (interactive)
9359   (let ((buffer-read-only nil)
9360         (orig (point))
9361         ;; first goto end then to beg, to have point at beg after let
9362         (end (progn (end-of-line) (point)))
9363         (beg (progn (beginning-of-line) (point))))
9364     (prog1
9365         ;; Any hidden lines here?
9366         (search-forward "\r" end t)
9367       (subst-char-in-region beg end ?\^M ?\n t)
9368       (goto-char orig)
9369       (gnus-summary-position-point))))
9370
9371 (defun gnus-summary-hide-all-threads ()
9372   "Hide all thread subtrees."
9373   (interactive)
9374   (save-excursion
9375     (goto-char (point-min))
9376     (gnus-summary-hide-thread)
9377     (while (zerop (gnus-summary-next-thread 1 t))
9378       (gnus-summary-hide-thread)))
9379   (gnus-summary-position-point))
9380
9381 (defun gnus-summary-hide-thread ()
9382   "Hide thread subtrees.
9383 Returns nil if no threads were there to be hidden."
9384   (interactive)
9385   (let ((buffer-read-only nil)
9386         (start (point))
9387         (article (gnus-summary-article-number)))
9388     (goto-char start)
9389     ;; Go forward until either the buffer ends or the subthread
9390     ;; ends.
9391     (when (and (not (eobp))
9392                (or (zerop (gnus-summary-next-thread 1 t))
9393                    (goto-char (point-max))))
9394       (prog1
9395           (if (and (> (point) start)
9396                    (search-backward "\n" start t))
9397               (progn
9398                 (subst-char-in-region start (point) ?\n ?\^M)
9399                 (gnus-summary-goto-subject article))
9400             (goto-char start)
9401             nil)))))
9402
9403 (defun gnus-summary-go-to-next-thread (&optional previous)
9404   "Go to the same level (or less) next thread.
9405 If PREVIOUS is non-nil, go to previous thread instead.
9406 Return the article number moved to, or nil if moving was impossible."
9407   (let ((level (gnus-summary-thread-level))
9408         (way (if previous -1 1))
9409         (beg (point)))
9410     (forward-line way)
9411     (while (and (not (eobp))
9412                 (< level (gnus-summary-thread-level)))
9413       (forward-line way))
9414     (if (eobp)
9415         (progn
9416           (goto-char beg)
9417           nil)
9418       (setq beg (point))
9419       (prog1
9420           (gnus-summary-article-number)
9421         (goto-char beg)))))
9422
9423 (defun gnus-summary-next-thread (n &optional silent)
9424   "Go to the same level next N'th thread.
9425 If N is negative, search backward instead.
9426 Returns the difference between N and the number of skips actually
9427 done.
9428
9429 If SILENT, don't output messages."
9430   (interactive "p")
9431   (let ((backward (< n 0))
9432         (n (abs n)))
9433     (while (and (> n 0)
9434                 (gnus-summary-go-to-next-thread backward))
9435       (decf n))
9436     (unless silent
9437       (gnus-summary-position-point))
9438     (when (and (not silent) (/= 0 n))
9439       (gnus-message 7 "No more threads"))
9440     n))
9441
9442 (defun gnus-summary-prev-thread (n)
9443   "Go to the same level previous N'th thread.
9444 Returns the difference between N and the number of skips actually
9445 done."
9446   (interactive "p")
9447   (gnus-summary-next-thread (- n)))
9448
9449 (defun gnus-summary-go-down-thread ()
9450   "Go down one level in the current thread."
9451   (let ((children (gnus-summary-article-children)))
9452     (when children
9453       (gnus-summary-goto-subject (car children)))))
9454
9455 (defun gnus-summary-go-up-thread ()
9456   "Go up one level in the current thread."
9457   (let ((parent (gnus-summary-article-parent)))
9458     (when parent
9459       (gnus-summary-goto-subject parent))))
9460
9461 (defun gnus-summary-down-thread (n)
9462   "Go down thread N steps.
9463 If N is negative, go up instead.
9464 Returns the difference between N and how many steps down that were
9465 taken."
9466   (interactive "p")
9467   (let ((up (< n 0))
9468         (n (abs n)))
9469     (while (and (> n 0)
9470                 (if up (gnus-summary-go-up-thread)
9471                   (gnus-summary-go-down-thread)))
9472       (setq n (1- n)))
9473     (gnus-summary-position-point)
9474     (when (/= 0 n)
9475       (gnus-message 7 "Can't go further"))
9476     n))
9477
9478 (defun gnus-summary-up-thread (n)
9479   "Go up thread N steps.
9480 If N is negative, go down instead.
9481 Returns the difference between N and how many steps down that were
9482 taken."
9483   (interactive "p")
9484   (gnus-summary-down-thread (- n)))
9485
9486 (defun gnus-summary-top-thread ()
9487   "Go to the top of the thread."
9488   (interactive)
9489   (while (gnus-summary-go-up-thread))
9490   (gnus-summary-article-number))
9491
9492 (defun gnus-summary-kill-thread (&optional unmark)
9493   "Mark articles under current thread as read.
9494 If the prefix argument is positive, remove any kinds of marks.
9495 If the prefix argument is negative, tick articles instead."
9496   (interactive "P")
9497   (when unmark
9498     (setq unmark (prefix-numeric-value unmark)))
9499   (let ((articles (gnus-summary-articles-in-thread)))
9500     (save-excursion
9501       ;; Expand the thread.
9502       (gnus-summary-show-thread)
9503       ;; Mark all the articles.
9504       (while articles
9505         (gnus-summary-goto-subject (car articles))
9506         (cond ((null unmark)
9507                (gnus-summary-mark-article-as-read gnus-killed-mark))
9508               ((> unmark 0)
9509                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9510               (t
9511                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9512         (setq articles (cdr articles))))
9513     ;; Hide killed subtrees.
9514     (and (null unmark)
9515          gnus-thread-hide-killed
9516          (gnus-summary-hide-thread))
9517     ;; If marked as read, go to next unread subject.
9518     (when (null unmark)
9519       ;; Go to next unread subject.
9520       (gnus-summary-next-subject 1 t)))
9521   (gnus-set-mode-line 'summary))
9522
9523 ;; Summary sorting commands
9524
9525 (defun gnus-summary-sort-by-number (&optional reverse)
9526   "Sort the summary buffer by article number.
9527 Argument REVERSE means reverse order."
9528   (interactive "P")
9529   (gnus-summary-sort 'number reverse))
9530
9531 (defun gnus-summary-sort-by-author (&optional reverse)
9532   "Sort the summary buffer by author name alphabetically.
9533 If `case-fold-search' is non-nil, case of letters is ignored.
9534 Argument REVERSE means reverse order."
9535   (interactive "P")
9536   (gnus-summary-sort 'author reverse))
9537
9538 (defun gnus-summary-sort-by-subject (&optional reverse)
9539   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9540 If `case-fold-search' is non-nil, case of letters is ignored.
9541 Argument REVERSE means reverse order."
9542   (interactive "P")
9543   (gnus-summary-sort 'subject reverse))
9544
9545 (defun gnus-summary-sort-by-date (&optional reverse)
9546   "Sort the summary buffer by date.
9547 Argument REVERSE means reverse order."
9548   (interactive "P")
9549   (gnus-summary-sort 'date reverse))
9550
9551 (defun gnus-summary-sort-by-score (&optional reverse)
9552   "Sort the summary buffer by score.
9553 Argument REVERSE means reverse order."
9554   (interactive "P")
9555   (gnus-summary-sort 'score reverse))
9556
9557 (defun gnus-summary-sort-by-lines (&optional reverse)
9558   "Sort the summary buffer by the number of lines.
9559 Argument REVERSE means reverse order."
9560   (interactive "P")
9561   (gnus-summary-sort 'lines reverse))
9562
9563 (defun gnus-summary-sort-by-chars (&optional reverse)
9564   "Sort the summary buffer by article length.
9565 Argument REVERSE means reverse order."
9566   (interactive "P")
9567   (gnus-summary-sort 'chars reverse))
9568
9569 (defun gnus-summary-sort-by-original (&optional reverse)
9570   "Sort the summary buffer using the default sorting method.
9571 Argument REVERSE means reverse order."
9572   (interactive "P")
9573   (let* ((buffer-read-only)
9574          (gnus-summary-prepare-hook nil))
9575     ;; We do the sorting by regenerating the threads.
9576     (gnus-summary-prepare)
9577     ;; Hide subthreads if needed.
9578     (when (and gnus-show-threads gnus-thread-hide-subtree)
9579       (gnus-summary-hide-all-threads))))
9580
9581 (defun gnus-summary-sort (predicate reverse)
9582   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9583   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9584          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9585          (gnus-thread-sort-functions
9586           (if (not reverse)
9587               thread
9588             `(lambda (t1 t2)
9589                (,thread t2 t1))))
9590          (gnus-sort-gathered-threads-function
9591           gnus-thread-sort-functions)
9592          (gnus-article-sort-functions
9593           (if (not reverse)
9594               article
9595             `(lambda (t1 t2)
9596                (,article t2 t1))))
9597          (buffer-read-only)
9598          (gnus-summary-prepare-hook nil))
9599     ;; We do the sorting by regenerating the threads.
9600     (gnus-summary-prepare)
9601     ;; Hide subthreads if needed.
9602     (when (and gnus-show-threads gnus-thread-hide-subtree)
9603       (gnus-summary-hide-all-threads))))
9604
9605 ;; Summary saving commands.
9606
9607 (defun gnus-summary-save-article (&optional n not-saved)
9608   "Save the current article using the default saver function.
9609 If N is a positive number, save the N next articles.
9610 If N is a negative number, save the N previous articles.
9611 If N is nil and any articles have been marked with the process mark,
9612 save those articles instead.
9613 The variable `gnus-default-article-saver' specifies the saver function."
9614   (interactive "P")
9615   (let* ((articles (gnus-summary-work-articles n))
9616          (save-buffer (save-excursion
9617                         (nnheader-set-temp-buffer " *Gnus Save*")))
9618          (num (length articles))
9619          header file)
9620     (dolist (article articles)
9621       (setq header (gnus-summary-article-header article))
9622       (if (not (vectorp header))
9623           ;; This is a pseudo-article.
9624           (if (assq 'name header)
9625               (gnus-copy-file (cdr (assq 'name header)))
9626             (gnus-message 1 "Article %d is unsaveable" article))
9627         ;; This is a real article.
9628         (save-window-excursion
9629           (gnus-summary-select-article t nil nil article))
9630         (save-excursion
9631           (set-buffer save-buffer)
9632           (erase-buffer)
9633           (insert-buffer-substring gnus-original-article-buffer))
9634         (setq file (gnus-article-save save-buffer file num))
9635         (gnus-summary-remove-process-mark article)
9636         (unless not-saved
9637           (gnus-summary-set-saved-mark article))))
9638     (gnus-kill-buffer save-buffer)
9639     (gnus-summary-position-point)
9640     (gnus-set-mode-line 'summary)
9641     n))
9642
9643 (defun gnus-summary-pipe-output (&optional arg)
9644   "Pipe the current article to a subprocess.
9645 If N is a positive number, pipe the N next articles.
9646 If N is a negative number, pipe the N previous articles.
9647 If N is nil and any articles have been marked with the process mark,
9648 pipe those articles instead."
9649   (interactive "P")
9650   (require 'gnus-art)
9651   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9652     (gnus-summary-save-article arg t))
9653   (gnus-configure-windows 'pipe))
9654
9655 (defun gnus-summary-save-article-mail (&optional arg)
9656   "Append the current article to an mail file.
9657 If N is a positive number, save the N next articles.
9658 If N is a negative number, save the N previous articles.
9659 If N is nil and any articles have been marked with the process mark,
9660 save those articles instead."
9661   (interactive "P")
9662   (require 'gnus-art)
9663   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9664     (gnus-summary-save-article arg)))
9665
9666 (defun gnus-summary-save-article-rmail (&optional arg)
9667   "Append the current article to an rmail file.
9668 If N is a positive number, save the N next articles.
9669 If N is a negative number, save the N previous articles.
9670 If N is nil and any articles have been marked with the process mark,
9671 save those articles instead."
9672   (interactive "P")
9673   (require 'gnus-art)
9674   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9675     (gnus-summary-save-article arg)))
9676
9677 (defun gnus-summary-save-article-file (&optional arg)
9678   "Append the current article to a file.
9679 If N is a positive number, save the N next articles.
9680 If N is a negative number, save the N previous articles.
9681 If N is nil and any articles have been marked with the process mark,
9682 save those articles instead."
9683   (interactive "P")
9684   (require 'gnus-art)
9685   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9686     (gnus-summary-save-article arg)))
9687
9688 (defun gnus-summary-write-article-file (&optional arg)
9689   "Write the current article to a file, deleting the previous file.
9690 If N is a positive number, save the N next articles.
9691 If N is a negative number, save the N previous articles.
9692 If N is nil and any articles have been marked with the process mark,
9693 save those articles instead."
9694   (interactive "P")
9695   (require 'gnus-art)
9696   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9697     (gnus-summary-save-article arg)))
9698
9699 (defun gnus-summary-save-article-body-file (&optional arg)
9700   "Append the current article body to a file.
9701 If N is a positive number, save the N next articles.
9702 If N is a negative number, save the N previous articles.
9703 If N is nil and any articles have been marked with the process mark,
9704 save those articles instead."
9705   (interactive "P")
9706   (require 'gnus-art)
9707   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9708     (gnus-summary-save-article arg)))
9709
9710 (defun gnus-summary-pipe-message (program)
9711   "Pipe the current article through PROGRAM."
9712   (interactive "sProgram: ")
9713   (gnus-summary-select-article)
9714   (let ((mail-header-separator ""))
9715     (gnus-eval-in-buffer-window gnus-article-buffer
9716       (save-restriction
9717         (widen)
9718         (let ((start (window-start))
9719               buffer-read-only)
9720           (message-pipe-buffer-body program)
9721           (set-window-start (get-buffer-window (current-buffer)) start))))))
9722
9723 (defun gnus-get-split-value (methods)
9724   "Return a value based on the split METHODS."
9725   (let (split-name method result match)
9726     (when methods
9727       (save-excursion
9728         (set-buffer gnus-original-article-buffer)
9729         (save-restriction
9730           (nnheader-narrow-to-headers)
9731           (while (and methods (not split-name))
9732             (goto-char (point-min))
9733             (setq method (pop methods))
9734             (setq match (car method))
9735             (when (cond
9736                    ((stringp match)
9737                     ;; Regular expression.
9738                     (ignore-errors
9739                       (re-search-forward match nil t)))
9740                    ((gnus-functionp match)
9741                     ;; Function.
9742                     (save-restriction
9743                       (widen)
9744                       (setq result (funcall match gnus-newsgroup-name))))
9745                    ((consp match)
9746                     ;; Form.
9747                     (save-restriction
9748                       (widen)
9749                       (setq result (eval match)))))
9750               (setq split-name (cdr method))
9751               (cond ((stringp result)
9752                      (push (expand-file-name
9753                             result gnus-article-save-directory)
9754                            split-name))
9755                     ((consp result)
9756                      (setq split-name (append result split-name)))))))))
9757     (nreverse split-name)))
9758
9759 (defun gnus-valid-move-group-p (group)
9760   (and (boundp group)
9761        (symbol-name group)
9762        (symbol-value group)
9763        (gnus-get-function (gnus-find-method-for-group
9764                            (symbol-name group)) 'request-accept-article t)))
9765
9766 (defun gnus-read-move-group-name (prompt default articles prefix)
9767   "Read a group name."
9768   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9769          (minibuffer-confirm-incomplete nil) ; XEmacs
9770          (prom
9771           (format "%s %s to:"
9772                   prompt
9773                   (if (> (length articles) 1)
9774                       (format "these %d articles" (length articles))
9775                     "this article")))
9776          (to-newsgroup
9777           (cond
9778            ((null split-name)
9779             (gnus-completing-read default prom
9780                                   gnus-active-hashtb
9781                                   'gnus-valid-move-group-p
9782                                   nil prefix
9783                                   'gnus-group-history))
9784            ((= 1 (length split-name))
9785             (gnus-completing-read (car split-name) prom
9786                                   gnus-active-hashtb
9787                                   'gnus-valid-move-group-p
9788                                   nil nil
9789                                   'gnus-group-history))
9790            (t
9791             (gnus-completing-read nil prom
9792                                   (mapcar (lambda (el) (list el))
9793                                           (nreverse split-name))
9794                                   nil nil nil
9795                                   'gnus-group-history))))
9796          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9797     (when to-newsgroup
9798       (if (or (string= to-newsgroup "")
9799               (string= to-newsgroup prefix))
9800           (setq to-newsgroup default))
9801       (unless to-newsgroup
9802         (error "No group name entered"))
9803       (or (gnus-active to-newsgroup)
9804           (gnus-activate-group to-newsgroup nil nil to-method)
9805           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9806                                      to-newsgroup))
9807               (or (and (gnus-request-create-group to-newsgroup to-method)
9808                        (gnus-activate-group
9809                         to-newsgroup nil nil to-method)
9810                        (gnus-subscribe-group to-newsgroup))
9811                   (error "Couldn't create group %s" to-newsgroup)))
9812           (error "No such group: %s" to-newsgroup)))
9813     to-newsgroup))
9814
9815 (defun gnus-summary-save-parts (type dir n &optional reverse)
9816   "Save parts matching TYPE to DIR.
9817 If REVERSE, save parts that do not match TYPE."
9818   (interactive
9819    (list (read-string "Save parts of type: "
9820                       (or (car gnus-summary-save-parts-type-history)
9821                           gnus-summary-save-parts-default-mime)
9822                       'gnus-summary-save-parts-type-history)
9823          (setq gnus-summary-save-parts-last-directory
9824                (read-file-name "Save to directory: "
9825                                gnus-summary-save-parts-last-directory
9826                                nil t))
9827          current-prefix-arg))
9828   (gnus-summary-iterate n
9829     (let ((gnus-display-mime-function nil)
9830           (gnus-inhibit-treatment t))
9831       (gnus-summary-select-article))
9832     (save-excursion
9833       (set-buffer gnus-article-buffer)
9834       (let ((handles (or gnus-article-mime-handles
9835                          (mm-dissect-buffer) (mm-uu-dissect))))
9836         (when handles
9837           (gnus-summary-save-parts-1 type dir handles reverse)
9838           (unless gnus-article-mime-handles ;; Don't destroy this case.
9839             (mm-destroy-parts handles)))))))
9840
9841 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9842   (if (stringp (car handle))
9843       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9844               (cdr handle))
9845     (when (if reverse
9846               (not (string-match type (mm-handle-media-type handle)))
9847             (string-match type (mm-handle-media-type handle)))
9848       (let ((file (expand-file-name
9849                    (file-name-nondirectory
9850                     (or
9851                      (mail-content-type-get
9852                       (mm-handle-disposition handle) 'filename)
9853                      (concat gnus-newsgroup-name
9854                              "." (number-to-string
9855                                   (cdr gnus-article-current)))))
9856                    dir)))
9857         (unless (file-exists-p file)
9858           (mm-save-part-to-file handle file))))))
9859
9860 ;; Summary extract commands
9861
9862 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9863   (let ((buffer-read-only nil)
9864         (article (gnus-summary-article-number))
9865         after-article b e)
9866     (unless (gnus-summary-goto-subject article)
9867       (error "No such article: %d" article))
9868     (gnus-summary-position-point)
9869     ;; If all commands are to be bunched up on one line, we collect
9870     ;; them here.
9871     (unless gnus-view-pseudos-separately
9872       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9873             files action)
9874         (while ps
9875           (setq action (cdr (assq 'action (car ps))))
9876           (setq files (list (cdr (assq 'name (car ps)))))
9877           (while (and ps (cdr ps)
9878                       (string= (or action "1")
9879                                (or (cdr (assq 'action (cadr ps))) "2")))
9880             (push (cdr (assq 'name (cadr ps))) files)
9881             (setcdr ps (cddr ps)))
9882           (when files
9883             (when (not (string-match "%s" action))
9884               (push " " files))
9885             (push " " files)
9886             (when (assq 'execute (car ps))
9887               (setcdr (assq 'execute (car ps))
9888                       (funcall (if (string-match "%s" action)
9889                                    'format 'concat)
9890                                action
9891                                (mapconcat
9892                                 (lambda (f)
9893                                   (if (equal f " ")
9894                                       f
9895                                     (gnus-quote-arg-for-sh-or-csh f)))
9896                                 files " ")))))
9897           (setq ps (cdr ps)))))
9898     (if (and gnus-view-pseudos (not not-view))
9899         (while pslist
9900           (when (assq 'execute (car pslist))
9901             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9902                                   (eq gnus-view-pseudos 'not-confirm)))
9903           (setq pslist (cdr pslist)))
9904       (save-excursion
9905         (while pslist
9906           (setq after-article (or (cdr (assq 'article (car pslist)))
9907                                   (gnus-summary-article-number)))
9908           (gnus-summary-goto-subject after-article)
9909           (forward-line 1)
9910           (setq b (point))
9911           (insert "    " (file-name-nondirectory
9912                           (cdr (assq 'name (car pslist))))
9913                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9914           (setq e (point))
9915           (forward-line -1)             ; back to `b'
9916           (gnus-add-text-properties
9917            b (1- e) (list 'gnus-number gnus-reffed-article-number
9918                           gnus-mouse-face-prop gnus-mouse-face))
9919           (gnus-data-enter
9920            after-article gnus-reffed-article-number
9921            gnus-unread-mark b (car pslist) 0 (- e b))
9922           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9923           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9924           (setq pslist (cdr pslist)))))))
9925
9926 (defun gnus-pseudos< (p1 p2)
9927   (let ((c1 (cdr (assq 'action p1)))
9928         (c2 (cdr (assq 'action p2))))
9929     (and c1 c2 (string< c1 c2))))
9930
9931 (defun gnus-request-pseudo-article (props)
9932   (cond ((assq 'execute props)
9933          (gnus-execute-command (cdr (assq 'execute props)))))
9934   (let ((gnus-current-article (gnus-summary-article-number)))
9935     (gnus-run-hooks 'gnus-mark-article-hook)))
9936
9937 (defun gnus-execute-command (command &optional automatic)
9938   (save-excursion
9939     (gnus-article-setup-buffer)
9940     (set-buffer gnus-article-buffer)
9941     (setq buffer-read-only nil)
9942     (let ((command (if automatic command
9943                      (read-string "Command: " (cons command 0)))))
9944       (erase-buffer)
9945       (insert "$ " command "\n\n")
9946       (if gnus-view-pseudo-asynchronously
9947           (start-process "gnus-execute" (current-buffer) shell-file-name
9948                          shell-command-switch command)
9949         (call-process shell-file-name nil t nil
9950                       shell-command-switch command)))))
9951
9952 ;; Summary kill commands.
9953
9954 (defun gnus-summary-edit-global-kill (article)
9955   "Edit the \"global\" kill file."
9956   (interactive (list (gnus-summary-article-number)))
9957   (gnus-group-edit-global-kill article))
9958
9959 (defun gnus-summary-edit-local-kill ()
9960   "Edit a local kill file applied to the current newsgroup."
9961   (interactive)
9962   (setq gnus-current-headers (gnus-summary-article-header))
9963   (gnus-group-edit-local-kill
9964    (gnus-summary-article-number) gnus-newsgroup-name))
9965
9966 ;;; Header reading.
9967
9968 (defun gnus-read-header (id &optional header)
9969   "Read the headers of article ID and enter them into the Gnus system."
9970   (let ((group gnus-newsgroup-name)
9971         (gnus-override-method
9972          (or
9973           gnus-override-method
9974           (and (gnus-news-group-p gnus-newsgroup-name)
9975                (car (gnus-refer-article-methods)))))
9976         where)
9977     ;; First we check to see whether the header in question is already
9978     ;; fetched.
9979     (if (stringp id)
9980         ;; This is a Message-ID.
9981         (setq header (or header (gnus-id-to-header id)))
9982       ;; This is an article number.
9983       (setq header (or header (gnus-summary-article-header id))))
9984     (if (and header
9985              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9986         ;; We have found the header.
9987         header
9988       ;; If this is a sparse article, we have to nix out its
9989       ;; previous entry in the thread hashtb.
9990       (when (and header
9991                  (gnus-summary-article-sparse-p (mail-header-number header)))
9992         (let* ((parent (gnus-parent-id (mail-header-references header)))
9993                (thread (and parent (gnus-id-to-thread parent))))
9994           (when thread
9995             (delq (assq header thread) thread))))
9996       ;; We have to really fetch the header to this article.
9997       (save-excursion
9998         (set-buffer nntp-server-buffer)
9999         (when (setq where (gnus-request-head id group))
10000           (nnheader-fold-continuation-lines)
10001           (goto-char (point-max))
10002           (insert ".\n")
10003           (goto-char (point-min))
10004           (insert "211 ")
10005           (princ (cond
10006                   ((numberp id) id)
10007                   ((cdr where) (cdr where))
10008                   (header (mail-header-number header))
10009                   (t gnus-reffed-article-number))
10010                  (current-buffer))
10011           (insert " Article retrieved.\n"))
10012         (if (or (not where)
10013                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10014             ()                          ; Malformed head.
10015           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10016             (when (and (stringp id)
10017                        (not (string= (gnus-group-real-name group)
10018                                      (car where))))
10019               ;; If we fetched by Message-ID and the article came
10020               ;; from a different group, we fudge some bogus article
10021               ;; numbers for this article.
10022               (mail-header-set-number header gnus-reffed-article-number))
10023             (save-excursion
10024               (set-buffer gnus-summary-buffer)
10025               (decf gnus-reffed-article-number)
10026               (gnus-remove-header (mail-header-number header))
10027               (push header gnus-newsgroup-headers)
10028               (setq gnus-current-headers header)
10029               (push (mail-header-number header) gnus-newsgroup-limit)))
10030           header)))))
10031
10032 (defun gnus-remove-header (number)
10033   "Remove header NUMBER from `gnus-newsgroup-headers'."
10034   (if (and gnus-newsgroup-headers
10035            (= number (mail-header-number (car gnus-newsgroup-headers))))
10036       (pop gnus-newsgroup-headers)
10037     (let ((headers gnus-newsgroup-headers))
10038       (while (and (cdr headers)
10039                   (not (= number (mail-header-number (cadr headers)))))
10040         (pop headers))
10041       (when (cdr headers)
10042         (setcdr headers (cddr headers))))))
10043
10044 ;;;
10045 ;;; summary highlights
10046 ;;;
10047
10048 (defun gnus-highlight-selected-summary ()
10049   "Highlight selected article in summary buffer."
10050   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10051   (when gnus-summary-selected-face
10052     (save-excursion
10053       (let* ((beg (progn (beginning-of-line) (point)))
10054              (end (progn (end-of-line) (point)))
10055              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10056              (from (if (get-text-property beg gnus-mouse-face-prop)
10057                        beg
10058                      (or (next-single-property-change
10059                           beg gnus-mouse-face-prop nil end)
10060                          beg)))
10061              (to
10062               (if (= from end)
10063                   (- from 2)
10064                 (or (next-single-property-change
10065                      from gnus-mouse-face-prop nil end)
10066                     end))))
10067         ;; If no mouse-face prop on line we will have to = from = end,
10068         ;; so we highlight the entire line instead.
10069         (when (= (+ to 2) from)
10070           (setq from beg)
10071           (setq to end))
10072         (if gnus-newsgroup-selected-overlay
10073             ;; Move old overlay.
10074             (gnus-move-overlay
10075              gnus-newsgroup-selected-overlay from to (current-buffer))
10076           ;; Create new overlay.
10077           (gnus-overlay-put
10078            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10079            'face gnus-summary-selected-face))))))
10080
10081 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10082 (defun gnus-summary-highlight-line ()
10083   "Highlight current line according to `gnus-summary-highlight'."
10084   (let* ((list gnus-summary-highlight)
10085          (p (point))
10086          (end (progn (end-of-line) (point)))
10087          ;; now find out where the line starts and leave point there.
10088          (beg (progn (beginning-of-line) (point)))
10089          (article (gnus-summary-article-number))
10090          (score (or (cdr (assq (or article gnus-current-article)
10091                                gnus-newsgroup-scored))
10092                     gnus-summary-default-score 0))
10093          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10094          (inhibit-read-only t))
10095     ;; Eval the cars of the lists until we find a match.
10096     (let ((default gnus-summary-default-score))
10097       (while (and list
10098                   (not (eval (caar list))))
10099         (setq list (cdr list))))
10100     (let ((face (cdar list)))
10101       (unless (eq face (get-text-property beg 'face))
10102         (gnus-put-text-property-excluding-characters-with-faces
10103          beg end 'face
10104          (setq face (if (boundp face) (symbol-value face) face)))
10105         (when gnus-summary-highlight-line-function
10106           (funcall gnus-summary-highlight-line-function article face))))
10107     (goto-char p)))
10108
10109 (defun gnus-update-read-articles (group unread &optional compute)
10110   "Update the list of read articles in GROUP."
10111   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10112          (entry (gnus-gethash group gnus-newsrc-hashtb))
10113          (info (nth 2 entry))
10114          (prev 1)
10115          (unread (sort (copy-sequence unread) '<))
10116          read)
10117     (if (or (not info) (not active))
10118         ;; There is no info on this group if it was, in fact,
10119         ;; killed.  Gnus stores no information on killed groups, so
10120         ;; there's nothing to be done.
10121         ;; One could store the information somewhere temporarily,
10122         ;; perhaps...  Hmmm...
10123         ()
10124       ;; Remove any negative articles numbers.
10125       (while (and unread (< (car unread) 0))
10126         (setq unread (cdr unread)))
10127       ;; Remove any expired article numbers
10128       (while (and unread (< (car unread) (car active)))
10129         (setq unread (cdr unread)))
10130       ;; Compute the ranges of read articles by looking at the list of
10131       ;; unread articles.
10132       (while unread
10133         (when (/= (car unread) prev)
10134           (push (if (= prev (1- (car unread))) prev
10135                   (cons prev (1- (car unread))))
10136                 read))
10137         (setq prev (1+ (car unread)))
10138         (setq unread (cdr unread)))
10139       (when (<= prev (cdr active))
10140         (push (cons prev (cdr active)) read))
10141       (setq read (if (> (length read) 1) (nreverse read) read))
10142       (if compute
10143           read
10144         (save-excursion
10145           (let (setmarkundo)
10146             ;; Propagate the read marks to the backend.
10147             (when (gnus-check-backend-function 'request-set-mark group)
10148               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10149                     (add (gnus-remove-from-range read (gnus-info-read info))))
10150                 (when (or add del)
10151                   (unless (gnus-check-group group)
10152                     (error "Can't open server for %s" group))
10153                   (gnus-request-set-mark
10154                    group (delq nil (list (if add (list add 'add '(read)))
10155                                          (if del (list del 'del '(read))))))
10156                   (setq setmarkundo
10157                         `(gnus-request-set-mark
10158                           ,group
10159                           ',(delq nil (list
10160                                        (if del (list del 'add '(read)))
10161                                        (if add (list add 'del '(read))))))))))
10162             (set-buffer gnus-group-buffer)
10163             (gnus-undo-register
10164               `(progn
10165                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10166                  (gnus-info-set-read ',info ',(gnus-info-read info))
10167                  (gnus-get-unread-articles-in-group ',info
10168                                                     (gnus-active ,group))
10169                  (gnus-group-update-group ,group t)
10170                  ,setmarkundo))))
10171         ;; Enter this list into the group info.
10172         (gnus-info-set-read info read)
10173         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10174         (gnus-get-unread-articles-in-group info (gnus-active group))
10175         t))))
10176
10177 (defun gnus-offer-save-summaries ()
10178   "Offer to save all active summary buffers."
10179   (let (buffers)
10180     ;; Go through all buffers and find all summaries.
10181     (dolist (buffer (buffer-list))
10182       (when (and (setq buffer (buffer-name buffer))
10183                  (string-match "Summary" buffer)
10184                  (save-excursion
10185                    (set-buffer buffer)
10186                    ;; We check that this is, indeed, a summary buffer.
10187                    (and (eq major-mode 'gnus-summary-mode)
10188                         ;; Also make sure this isn't bogus.
10189                         gnus-newsgroup-prepared
10190                         ;; Also make sure that this isn't a
10191                         ;; dead summary buffer.
10192                         (not gnus-dead-summary-mode))))
10193         (push buffer buffers)))
10194     ;; Go through all these summary buffers and offer to save them.
10195     (when buffers
10196       (save-excursion
10197         (map-y-or-n-p
10198          "Update summary buffer %s? "
10199          (lambda (buf)
10200            (switch-to-buffer buf)
10201            (gnus-summary-exit))
10202          buffers)))))
10203
10204
10205 ;;; @ for mime-partial
10206 ;;;
10207
10208 (defun gnus-request-partial-message ()
10209   (save-excursion
10210     (let ((number (gnus-summary-article-number))
10211           (group gnus-newsgroup-name)
10212           (mother gnus-article-buffer))
10213       (set-buffer (get-buffer-create " *Partial Article*"))
10214       (erase-buffer)
10215       (setq mime-preview-buffer mother)
10216       (gnus-request-article-this-buffer number group)
10217       (mime-parse-buffer)
10218       )))
10219
10220 (autoload 'mime-combine-message/partial-pieces-automatically
10221   "mime-partial"
10222   "Internal method to combine message/partial messages automatically.")
10223
10224 (mime-add-condition
10225  'action '((type . message)(subtype . partial)
10226            (major-mode . gnus-original-article-mode)
10227            (method . mime-combine-message/partial-pieces-automatically)
10228            (summary-buffer-exp . gnus-summary-buffer)
10229            (request-partial-message-method . gnus-request-partial-message)
10230            ))
10231
10232
10233 ;;; @ for message/rfc822
10234 ;;;
10235
10236 (defun gnus-mime-extract-message/rfc822 (entity situation)
10237   (let (group article num cwin swin cur)
10238     (with-temp-buffer
10239       (mime-insert-entity-content entity)
10240       (setq group (or (cdr (assq 'group situation))
10241                       (completing-read "Group: "
10242                                        gnus-active-hashtb
10243                                        nil
10244                                        (gnus-read-active-file-p)
10245                                        gnus-newsgroup-name))
10246             article (gnus-request-accept-article group)))
10247     (when (and (consp article)
10248                (numberp (setq article (cdr article))))
10249       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10250             cwin (get-buffer-window (current-buffer) t))
10251       (save-window-excursion
10252         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10253             (select-window swin)
10254           (set-buffer gnus-summary-buffer))
10255         (setq cur gnus-current-article)
10256         (forward-line num)
10257         (let (gnus-show-threads)
10258           (gnus-summary-goto-subject article t))
10259         (gnus-summary-clear-mark-forward 1)
10260         (gnus-summary-goto-subject cur))
10261       (when (and cwin (window-frame cwin))
10262         (select-frame (window-frame cwin)))
10263       (when (boundp 'mime-acting-situation-to-override)
10264         (set-alist 'mime-acting-situation-to-override
10265                    'group
10266                    group)
10267         (set-alist 'mime-acting-situation-to-override
10268                    'after-method
10269                    `(progn
10270                       (save-current-buffer
10271                         (set-buffer gnus-group-buffer)
10272                         (gnus-activate-group ,group))
10273                       (gnus-summary-goto-article ,cur
10274                                                  gnus-show-all-headers)))
10275         (set-alist 'mime-acting-situation-to-override
10276                    'number num)))))
10277
10278 (mime-add-condition
10279  'action '((type . message)(subtype . rfc822)
10280            (major-mode . gnus-original-article-mode)
10281            (method . gnus-mime-extract-message/rfc822)
10282            (mode . "extract")
10283            ))
10284
10285 (mime-add-condition
10286  'action '((type . message)(subtype . news)
10287            (major-mode . gnus-original-article-mode)
10288            (method . gnus-mime-extract-message/rfc822)
10289            (mode . "extract")
10290            ))
10291
10292 (defun gnus-mime-extract-multipart (entity situation)
10293   (let ((children (mime-entity-children entity))
10294         mime-acting-situation-to-override
10295         f)
10296     (while children
10297       (mime-play-entity (car children)
10298                         (cons (assq 'mode situation)
10299                               mime-acting-situation-to-override))
10300       (setq children (cdr children)))
10301     (if (setq f (cdr (assq 'after-method
10302                            mime-acting-situation-to-override)))
10303         (eval f)
10304       )))
10305
10306 (mime-add-condition
10307  'action '((type . multipart)
10308            (method . gnus-mime-extract-multipart)
10309            (mode . "extract")
10310            )
10311  'with-default)
10312
10313
10314 ;;; @ end
10315 ;;;
10316
10317 (defun gnus-summary-setup-default-charset ()
10318   "Setup newsgroup default charset."
10319   (if (equal gnus-newsgroup-name "nndraft:drafts")
10320       (setq gnus-newsgroup-charset nil)
10321     (let* ((ignored-charsets
10322             (or gnus-newsgroup-ephemeral-ignored-charsets
10323                 (append
10324                  (and gnus-newsgroup-name
10325                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10326                  gnus-newsgroup-ignored-charsets))))
10327       (setq gnus-newsgroup-charset
10328             (or gnus-newsgroup-ephemeral-charset
10329                 (and gnus-newsgroup-name
10330                      (gnus-parameter-charset gnus-newsgroup-name))
10331                 gnus-default-charset))
10332       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10333            ignored-charsets))))
10334
10335 ;;;
10336 ;;; Mime Commands
10337 ;;;
10338
10339 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10340   "Display the current article buffer fully MIME-buttonized.
10341 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10342 treated as multipart/mixed."
10343   (interactive "P")
10344   (require 'gnus-art)
10345   (let ((gnus-unbuttonized-mime-types nil)
10346         (gnus-mime-display-multipart-as-mixed show-all-parts))
10347     (gnus-summary-show-article)))
10348
10349 (defun gnus-summary-repair-multipart (article)
10350   "Add a Content-Type header to a multipart article without one."
10351   (interactive (list (gnus-summary-article-number)))
10352   (gnus-with-article article
10353     (message-narrow-to-head)
10354     (message-remove-header "Mime-Version")
10355     (goto-char (point-max))
10356     (insert "Mime-Version: 1.0\n")
10357     (widen)
10358     (when (search-forward "\n--" nil t)
10359       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10360         (message-narrow-to-head)
10361         (message-remove-header "Content-Type")
10362         (goto-char (point-max))
10363         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10364                         separator))
10365         (widen))))
10366   (let (gnus-mark-article-hook)
10367     (gnus-summary-select-article t t nil article)))
10368
10369 (defun gnus-summary-toggle-display-buttonized ()
10370   "Toggle the buttonizing of the article buffer."
10371   (interactive)
10372   (require 'gnus-art)
10373   (if (setq gnus-inhibit-mime-unbuttonizing
10374             (not gnus-inhibit-mime-unbuttonizing))
10375       (let ((gnus-unbuttonized-mime-types nil))
10376         (gnus-summary-show-article))
10377     (gnus-summary-show-article)))
10378
10379 ;;;
10380 ;;; Intelli-mouse commmands
10381 ;;;
10382
10383 (defun gnus-wheel-summary-scroll (event)
10384   (interactive "e")
10385   (let ((amount (if (memq 'shift (event-modifiers event))
10386                     (car gnus-wheel-scroll-amount)
10387                   (cdr gnus-wheel-scroll-amount)))
10388         (direction (- (* (static-if (featurep 'xemacs)
10389                              (event-button event)
10390                            (cond ((eq 'mouse-4 (event-basic-type event))
10391                                   4)
10392                                  ((eq 'mouse-5 (event-basic-type event))
10393                                   5)))
10394                          2) 9))
10395         edge)
10396     (gnus-summary-scroll-up (* amount direction))
10397     (when (gnus-eval-in-buffer-window gnus-article-buffer
10398             (save-restriction
10399               (widen)
10400               (and (if (< 0 direction)
10401                        (gnus-article-next-page 0)
10402                      (gnus-article-prev-page 0)
10403                      (bobp))
10404                    (if (setq edge (get-text-property
10405                                    (point-min) 'gnus-wheel-edge))
10406                        (setq edge (* edge direction))
10407                      (setq edge -1))
10408                    (or (plusp edge)
10409                        (let ((buffer-read-only nil)
10410                              (inhibit-read-only t))
10411                          (put-text-property (point-min) (point-max)
10412                                             'gnus-wheel-edge direction)
10413                          nil))
10414                    (or (> edge gnus-wheel-edge-resistance)
10415                        (let ((buffer-read-only nil)
10416                              (inhibit-read-only t))
10417                          (put-text-property (point-min) (point-max)
10418                                             'gnus-wheel-edge
10419                                             (* (1+ edge) direction))
10420                          nil))
10421                    (eq last-command 'gnus-wheel-summary-scroll))))
10422       (gnus-summary-next-article nil nil (minusp direction)))))
10423
10424 (defun gnus-wheel-install ()
10425   "Enable mouse wheel support on summary window."
10426   (when gnus-use-wheel
10427     (let ((keys
10428            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10429       (dolist (key keys)
10430         (define-key gnus-summary-mode-map key
10431           'gnus-wheel-summary-scroll)))))
10432
10433 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10434
10435 ;;;
10436 ;;; Traditional PGP commmands
10437 ;;;
10438
10439 (defun gnus-summary-decrypt-article (&optional force)
10440   "Decrypt the current article in traditional PGP way.
10441 This will have permanent effect only in mail groups.
10442 If FORCE is non-nil, allow editing of articles even in read-only
10443 groups."
10444   (interactive "P")
10445   (gnus-summary-select-article t)
10446   (gnus-eval-in-buffer-window gnus-article-buffer
10447     (save-excursion
10448       (save-restriction
10449         (widen)
10450         (goto-char (point-min))
10451         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10452           (error "Not a traditional PGP message!"))
10453         (let ((armor-start (match-beginning 0)))
10454           (if (and (pgg-decrypt-region armor-start (point-max))
10455                    (or force (not (gnus-group-read-only-p))))
10456               (let ((inhibit-read-only t)
10457                     buffer-read-only)
10458                 (delete-region armor-start
10459                                (progn
10460                                  (re-search-forward "^-+END PGP" nil t)
10461                                  (beginning-of-line 2)
10462                                  (point)))
10463                 (insert-buffer-substring pgg-output-buffer))))))))
10464
10465 (defun gnus-summary-verify-article ()
10466   "Verify the current article in traditional PGP way."
10467   (interactive)
10468   (save-excursion
10469     (set-buffer gnus-original-article-buffer)
10470     (goto-char (point-min))
10471     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10472       (error "Not a traditional PGP message!"))
10473     (re-search-forward "^-+END PGP" nil t)
10474     (beginning-of-line 2)
10475     (call-interactively (function pgg-verify-region))))
10476
10477 ;;;
10478 ;;; Generic summary marking commands
10479 ;;;
10480
10481 (defvar gnus-summary-marking-alist
10482   '((read gnus-del-mark "d")
10483     (unread gnus-unread-mark "u")
10484     (ticked gnus-ticked-mark "!")
10485     (dormant gnus-dormant-mark "?")
10486     (expirable gnus-expirable-mark "e"))
10487   "An alist of names/marks/keystrokes.")
10488
10489 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10490 (defvar gnus-summary-mark-map)
10491
10492 (defun gnus-summary-make-all-marking-commands ()
10493   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10494   (dolist (elem gnus-summary-marking-alist)
10495     (apply 'gnus-summary-make-marking-command elem)))
10496
10497 (defun gnus-summary-make-marking-command (name mark keystroke)
10498   (let ((map (make-sparse-keymap)))
10499     (define-key gnus-summary-generic-mark-map keystroke map)
10500     (dolist (lway `((next "next" next nil "n")
10501                     (next-unread "next unread" next t "N")
10502                     (prev "previous" prev nil "p")
10503                     (prev-unread "previous unread" prev t "P")
10504                     (nomove "" nil nil ,keystroke)))
10505       (let ((func (gnus-summary-make-marking-command-1
10506                    mark (car lway) lway name)))
10507         (setq func (eval func))
10508         (define-key map (nth 4 lway) func)))))
10509
10510 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10511   `(defun ,(intern
10512             (format "gnus-summary-put-mark-as-%s%s"
10513                     name (if (eq way 'nomove)
10514                              ""
10515                            (concat "-" (symbol-name way)))))
10516      (n)
10517      ,(format
10518        "Mark the current article as %s%s.
10519 If N, the prefix, then repeat N times.
10520 If N is negative, move in reverse order.
10521 The difference between N and the actual number of articles marked is
10522 returned."
10523        name (car (cdr lway)))
10524      (interactive "p")
10525      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10526
10527 (defun gnus-summary-generic-mark (n mark move unread)
10528   "Mark N articles with MARK."
10529   (unless (eq major-mode 'gnus-summary-mode)
10530     (error "This command can only be used in the summary buffer"))
10531   (gnus-summary-show-thread)
10532   (let ((nummove
10533          (cond
10534           ((eq move 'next) 1)
10535           ((eq move 'prev) -1)
10536           (t 0))))
10537     (if (zerop nummove)
10538         (setq n 1)
10539       (when (< n 0)
10540         (setq n (abs n)
10541               nummove (* -1 nummove))))
10542     (while (and (> n 0)
10543                 (gnus-summary-mark-article nil mark)
10544                 (zerop (gnus-summary-next-subject nummove unread t)))
10545       (setq n (1- n)))
10546     (when (/= 0 n)
10547       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10548     (gnus-summary-recenter)
10549     (gnus-summary-position-point)
10550     (gnus-set-mode-line 'summary)
10551     n))
10552
10553 (defun gnus-summary-insert-articles (articles)
10554   (when (setq articles
10555               (gnus-set-difference articles
10556                                    (mapcar (lambda (h) (mail-header-number h))
10557                                            gnus-newsgroup-headers)))
10558     (setq gnus-newsgroup-headers 
10559           (merge 'list
10560                  gnus-newsgroup-headers
10561                  (gnus-fetch-headers articles)
10562                  'gnus-article-sort-by-number))
10563     ;; Suppress duplicates?
10564     (when gnus-suppress-duplicates
10565       (gnus-dup-suppress-articles))
10566     
10567     ;; We might want to build some more threads first.
10568     (when (and gnus-fetch-old-headers
10569                (eq gnus-headers-retrieved-by 'nov))
10570       (if (eq gnus-fetch-old-headers 'invisible)
10571         (gnus-build-all-threads)
10572         (gnus-build-old-threads)))
10573     ;; Let the Gnus agent mark articles as read.
10574     (when gnus-agent
10575       (gnus-agent-get-undownloaded-list))
10576     ;; Remove list identifiers from subject
10577     (when gnus-list-identifiers
10578       (gnus-summary-remove-list-identifiers))
10579     ;; First and last article in this newsgroup.
10580     (when gnus-newsgroup-headers
10581       (setq gnus-newsgroup-begin
10582             (mail-header-number (car gnus-newsgroup-headers))
10583             gnus-newsgroup-end
10584             (mail-header-number
10585              (gnus-last-element gnus-newsgroup-headers))))
10586     (when gnus-use-scoring
10587       (gnus-possibly-score-headers))))
10588
10589 (defun gnus-summary-insert-old-articles (&optional all)
10590   "Insert all old articles in this group.
10591 If ALL is non-nil, already read articles become readable.
10592 If ALL is a number, fetch this number of articles."
10593   (interactive "P")
10594   (prog1
10595       (let ((old (mapcar 'car gnus-newsgroup-data))
10596             (i (car gnus-newsgroup-active))
10597             older len)
10598         (while (<= i (cdr gnus-newsgroup-active))
10599           (or (memq i old) (push i older))
10600           (incf i))
10601         (setq len (length older))
10602         (cond 
10603          ((null older) nil)
10604          ((numberp all) 
10605           (if (< all len)
10606               (setq older (subseq older 0 all))))
10607          (all nil)
10608          (t
10609           (if (and (numberp gnus-large-newsgroup)
10610                    (> len gnus-large-newsgroup))
10611               (let ((input
10612                      (read-string
10613                       (format
10614                        "How many articles from %s (default %d): "
10615                        (gnus-limit-string 
10616                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10617                        len))))
10618                 (unless (string-match "^[ \t]*$" input) 
10619                   (setq all (string-to-number input))
10620                   (if (< all len)
10621                       (setq older (subseq older 0 all))))))))
10622         (if (not older)
10623             (message "No old news.")
10624           (gnus-summary-insert-articles older)
10625           (gnus-summary-limit (gnus-union older old))))
10626     (gnus-summary-position-point)))
10627
10628 (defun gnus-summary-insert-new-articles ()
10629   "Insert all new articles in this group."
10630   (interactive)
10631   (prog1
10632       (let ((old (mapcar 'car gnus-newsgroup-data))
10633             (old-active gnus-newsgroup-active)
10634             (nnmail-fetched-sources (list t))
10635             i new)
10636         (setq gnus-newsgroup-active 
10637               (gnus-activate-group gnus-newsgroup-name 'scan))
10638         (setq i (1+ (cdr old-active)))
10639         (while (<= i (cdr gnus-newsgroup-active))
10640           (push i new)
10641           (incf i))
10642         (if (not new)
10643             (message "No gnus is bad news.")
10644           (setq new (nreverse new))
10645           (gnus-summary-insert-articles new)
10646           (setq gnus-newsgroup-unreads
10647                 (append gnus-newsgroup-unreads new))
10648           (gnus-summary-limit (gnus-union old new))))
10649     (gnus-summary-position-point)))
10650
10651 (gnus-summary-make-all-marking-commands)
10652
10653 (gnus-ems-redefine)
10654
10655 (provide 'gnus-sum)
10656
10657 (run-hooks 'gnus-sum-load-hook)
10658
10659 ;;; gnus-sum.el ends here