* gnus-kill.el (gnus-execute): Work with the extra headers.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 ;; Recursive :-(.
42 ;; (require 'gnus-art)
43 (require 'nnoo)
44 (require 'mime-view)
45
46 (eval-when-compile
47   (require 'mime-play)
48   (require 'static))
49
50 (eval-and-compile
51   (autoload 'gnus-cache-articles-in-group "gnus-cache")
52   (autoload 'pgg-decrypt-region "pgg" nil t)
53   (autoload 'pgg-verify-region "pgg" nil t))
54
55 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
56 (autoload 'gnus-cache-write-active "gnus-cache")
57 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
58 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
59 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
60 (autoload 'mm-uu-dissect "mm-uu")
61
62 (defcustom gnus-kill-summary-on-exit t
63   "*If non-nil, kill the summary buffer when you exit from it.
64 If nil, the summary will become a \"*Dead Summary*\" buffer, and
65 it will be killed sometime later."
66   :group 'gnus-summary-exit
67   :type 'boolean)
68
69 (defcustom gnus-fetch-old-headers nil
70   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
71 If an unread article in the group refers to an older, already read (or
72 just marked as read) article, the old article will not normally be
73 displayed in the Summary buffer.  If this variable is non-nil, Gnus
74 will attempt to grab the headers to the old articles, and thereby
75 build complete threads.  If it has the value `some', only enough
76 headers to connect otherwise loose threads will be displayed.  This
77 variable can also be a number.  In that case, no more than that number
78 of old headers will be fetched.  If it has the value `invisible', all
79 old headers will be fetched, but none will be displayed.
80
81 The server has to support NOV for any of this to work."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const some)
85                  number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-refer-thread-limit 200
89   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
90 If t, fetch all the available old headers."
91   :group 'gnus-thread
92   :type '(choice number
93                  (sexp :menu-tag "other" t)))
94
95 (defcustom gnus-summary-make-false-root 'adopt
96   "*nil means that Gnus won't gather loose threads.
97 If the root of a thread has expired or been read in a previous
98 session, the information necessary to build a complete thread has been
99 lost.  Instead of having many small sub-threads from this original thread
100 scattered all over the summary buffer, Gnus can gather them.
101
102 If non-nil, Gnus will try to gather all loose sub-threads from an
103 original thread into one large thread.
104
105 If this variable is non-nil, it should be one of `none', `adopt',
106 `dummy' or `empty'.
107
108 If this variable is `none', Gnus will not make a false root, but just
109 present the sub-threads after another.
110 If this variable is `dummy', Gnus will create a dummy root that will
111 have all the sub-threads as children.
112 If this variable is `adopt', Gnus will make one of the \"children\"
113 the parent and mark all the step-children as such.
114 If this variable is `empty', the \"children\" are printed with empty
115 subject fields.  (Or rather, they will be printed with a string
116 given by the `gnus-summary-same-subject' variable.)"
117   :group 'gnus-thread
118   :type '(choice (const :tag "off" nil)
119                  (const none)
120                  (const dummy)
121                  (const adopt)
122                  (const empty)))
123
124 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
125   "*A regexp to match subjects to be excluded from loose thread gathering.
126 As loose thread gathering is done on subjects only, that means that
127 there can be many false gatherings performed.  By rooting out certain
128 common subjects, gathering might become saner."
129   :group 'gnus-thread
130   :type 'regexp)
131
132 (defcustom gnus-summary-gather-subject-limit nil
133   "*Maximum length of subject comparisons when gathering loose threads.
134 Use nil to compare full subjects.  Setting this variable to a low
135 number will help gather threads that have been corrupted by
136 newsreaders chopping off subject lines, but it might also mean that
137 unrelated articles that have subject that happen to begin with the
138 same few characters will be incorrectly gathered.
139
140 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
141 comparing subjects."
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const fuzzy)
145                  (sexp :menu-tag "on" t)))
146
147 (defcustom gnus-simplify-subject-functions nil
148   "List of functions taking a string argument that simplify subjects.
149 The functions are applied recursively.
150
151 Useful functions to put in this list include: `gnus-simplify-subject-re',
152 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 (they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-zcore-fuzz 0
217   "*Fuzziness factor for the zcore in the summary buffer.
218 Articles with scores closer than this to `gnus-summary-default-score'
219 will not be marked."
220   :group 'gnus-summary-format
221   :type 'integer)
222
223 (defcustom gnus-simplify-subject-fuzzy-regexp nil
224   "*Strings to be removed when doing fuzzy matches.
225 This can either be a regular expression or list of regular expressions
226 that will be removed from subject strings if fuzzy subject
227 simplification is selected."
228   :group 'gnus-thread
229   :type '(repeat regexp))
230
231 (defcustom gnus-show-threads t
232   "*If non-nil, display threads in summary mode."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-subtree nil
237   "*If non-nil, hide all threads initially.
238 If threads are hidden, you have to run the command
239 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
240 to expose hidden threads."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-killed t
245   "*If non-nil, hide killed threads automatically."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-ignore-subject t
250   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
251 If nil, articles that have different subjects from their parents will
252 start separate threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-operation-ignore-subject t
257   "*If non-nil, subjects will be ignored when doing thread commands.
258 This affects commands like `gnus-summary-kill-thread' and
259 `gnus-summary-lower-thread'.
260
261 If this variable is nil, articles in the same thread with different
262 subjects will not be included in the operation in question.  If this
263 variable is `fuzzy', only articles that have subjects that are fuzzily
264 equal will be included."
265   :group 'gnus-thread
266   :type '(choice (const :tag "off" nil)
267                  (const fuzzy)
268                  (sexp :tag "on" t)))
269
270 (defcustom gnus-thread-indent-level 4
271   "*Number that says how much each sub-thread should be indented."
272   :group 'gnus-thread
273   :type 'integer)
274
275 (defcustom gnus-auto-extend-newsgroup t
276   "*If non-nil, extend newsgroup forward and backward when requested."
277   :group 'gnus-summary-choose
278   :type 'boolean)
279
280 (defcustom gnus-auto-select-first t
281   "*If nil, don't select the first unread article when entering a group.
282 If this variable is `best', select the highest-scored unread article
283 in the group.  If t, select the first unread article.
284
285 This variable can also be a function to place point on a likely
286 subject line.  Useful values include `gnus-summary-first-unread-subject',
287 `gnus-summary-first-unread-article' and
288 `gnus-summary-best-unread-article'.
289
290 If you want to prevent automatic selection of the first unread article
291 in some newsgroups, set the variable to nil in
292 `gnus-select-group-hook'."
293   :group 'gnus-group-select
294   :type '(choice (const :tag "none" nil)
295                  (const best)
296                  (sexp :menu-tag "first" t)
297                  (function-item gnus-summary-first-unread-subject)
298                  (function-item gnus-summary-first-unread-article)
299                  (function-item gnus-summary-best-unread-article)))
300
301 (defcustom gnus-dont-select-after-jump-to-other-group nil
302   "If non-nil, don't select the first unread article after entering the
303 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
304 it is depend on the value of `gnus-auto-select-first' whether to select
305 or not."
306   :group 'gnus-group-select
307   :type 'boolean)
308
309 (defcustom gnus-auto-select-next t
310   "*If non-nil, offer to go to the next group from the end of the previous.
311 If the value is t and the next newsgroup is empty, Gnus will exit
312 summary mode and go back to group mode.  If the value is neither nil
313 nor t, Gnus will select the following unread newsgroup.  In
314 particular, if the value is the symbol `quietly', the next unread
315 newsgroup will be selected without any confirmation, and if it is
316 `almost-quietly', the next group will be selected without any
317 confirmation if you are located on the last article in the group.
318 Finally, if this variable is `slightly-quietly', the `Z n' command
319 will go to the next group without confirmation."
320   :group 'gnus-summary-maneuvering
321   :type '(choice (const :tag "off" nil)
322                  (const quietly)
323                  (const almost-quietly)
324                  (const slightly-quietly)
325                  (sexp :menu-tag "on" t)))
326
327 (defcustom gnus-auto-select-same nil
328   "*If non-nil, select the next article with the same subject.
329 If there are no more articles with the same subject, go to
330 the first unread article."
331   :group 'gnus-summary-maneuvering
332   :type 'boolean)
333
334 (defcustom gnus-summary-check-current nil
335   "*If non-nil, consider the current article when moving.
336 The \"unread\" movement commands will stay on the same line if the
337 current article is unread."
338   :group 'gnus-summary-maneuvering
339   :type 'boolean)
340
341 (defcustom gnus-auto-center-summary t
342   "*If non-nil, always center the current summary buffer.
343 In particular, if `vertical' do only vertical recentering.  If non-nil
344 and non-`vertical', do both horizontal and vertical recentering."
345   :group 'gnus-summary-maneuvering
346   :type '(choice (const :tag "none" nil)
347                  (const vertical)
348                  (integer :tag "height")
349                  (sexp :menu-tag "both" t)))
350
351 (defcustom gnus-show-all-headers nil
352   "*If non-nil, don't hide any headers."
353   :group 'gnus-article-hiding
354   :group 'gnus-article-headers
355   :type 'boolean)
356
357 (defcustom gnus-summary-ignore-duplicates nil
358   "*If non-nil, ignore articles with identical Message-ID headers."
359   :group 'gnus-summary
360   :type 'boolean)
361
362 (defcustom gnus-single-article-buffer t
363   "*If non-nil, display all articles in the same buffer.
364 If nil, each group will get its own article buffer."
365   :group 'gnus-article-various
366   :type 'boolean)
367
368 (defcustom gnus-break-pages t
369   "*If non-nil, do page breaking on articles.
370 The page delimiter is specified by the `gnus-page-delimiter'
371 variable."
372   :group 'gnus-article-various
373   :type 'boolean)
374
375 (defcustom gnus-show-mime t
376   "*If non-nil, do mime processing of articles.
377 The articles will simply be fed to the function given by
378 `gnus-article-display-method-for-mime'."
379   :group 'gnus-article-mime
380   :type 'boolean)
381
382 (defcustom gnus-move-split-methods nil
383   "*Variable used to suggest where articles are to be moved to.
384 It uses the same syntax as the `gnus-split-methods' variable.
385 However, whereas `gnus-split-methods' specifies file names as targets,
386 this variable specifies group names."
387   :group 'gnus-summary-mail
388   :type '(repeat (choice (list :value (fun) function)
389                          (cons :value ("" "") regexp (repeat string))
390                          (sexp :value nil))))
391
392 (defcustom gnus-unread-mark ?  ;Whitespace
393   "*Mark used for unread articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-ticked-mark ?!
398   "*Mark used for ticked articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-dormant-mark ??
403   "*Mark used for dormant articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-del-mark ?r
408   "*Mark used for del'd articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-read-mark ?R
413   "*Mark used for read articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-expirable-mark ?E
418   "*Mark used for expirable articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-killed-mark ?K
423   "*Mark used for killed articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-souped-mark ?F
428   "*Mark used for killed articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-kill-file-mark ?X
433   "*Mark used for articles killed by kill files."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-low-score-mark ?Y
438   "*Mark used for articles with a low score."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-catchup-mark ?C
443   "*Mark used for articles that are caught up."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-replied-mark ?A
448   "*Mark used for articles that have been replied to."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-cached-mark ?*
453   "*Mark used for articles that are in the cache."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-saved-mark ?S
458   "*Mark used for articles that have been saved to."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-no-mark ?  ;Whitespace
463   "*Mark used for articles that have no other secondary mark."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-ancient-mark ?O
468   "*Mark used for ancient articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-sparse-mark ?Q
473   "*Mark used for sparsely reffed articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-canceled-mark ?G
478   "*Mark used for canceled articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-duplicate-mark ?M
483   "*Mark used for duplicate articles."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-undownloaded-mark ?@
488   "*Mark used for articles that weren't downloaded."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-downloadable-mark ?%
493   "*Mark used for articles that are to be downloaded."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-unsendable-mark ?=
498   "*Mark used for articles that won't be sent."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-score-over-mark ?+
503   "*Score mark used for articles with high scores."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-score-below-mark ?-
508   "*Score mark used for articles with low scores."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-empty-thread-mark ?  ;Whitespace
513   "*There is no thread under the article."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-not-empty-thread-mark ?=
518   "*There is a thread under the article."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-view-pseudo-asynchronously nil
523   "*If non-nil, Gnus will view pseudo-articles asynchronously."
524   :group 'gnus-extract-view
525   :type 'boolean)
526
527 (defcustom gnus-auto-expirable-marks
528   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
529         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
530         gnus-souped-mark gnus-duplicate-mark)
531   "*The list of marks converted into expiration if a group is auto-expirable."
532   :version "21.1"
533   :group 'gnus-summary
534   :type '(repeat character))
535
536 (defcustom gnus-inhibit-user-auto-expire t
537   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
538   :version "21.1"
539   :group 'gnus-summary
540   :type 'boolean)
541
542 (defcustom gnus-view-pseudos nil
543   "*If `automatic', pseudo-articles will be viewed automatically.
544 If `not-confirm', pseudos will be viewed automatically, and the user
545 will not be asked to confirm the command."
546   :group 'gnus-extract-view
547   :type '(choice (const :tag "off" nil)
548                  (const automatic)
549                  (const not-confirm)))
550
551 (defcustom gnus-view-pseudos-separately t
552   "*If non-nil, one pseudo-article will be created for each file to be viewed.
553 If nil, all files that use the same viewing command will be given as a
554 list of parameters to that command."
555   :group 'gnus-extract-view
556   :type 'boolean)
557
558 (defcustom gnus-insert-pseudo-articles t
559   "*If non-nil, insert pseudo-articles when decoding articles."
560   :group 'gnus-extract-view
561   :type 'boolean)
562
563 (defcustom gnus-summary-dummy-line-format
564   "  %(:                          :%) %S\n"
565   "*The format specification for the dummy roots in the summary buffer.
566 It works along the same lines as a normal formatting string,
567 with some simple extensions.
568
569 %S  The subject"
570   :group 'gnus-threading
571   :type 'string)
572
573 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
574   "*The format specification for the summary mode line.
575 It works along the same lines as a normal formatting string,
576 with some simple extensions:
577
578 %G  Group name
579 %p  Unprefixed group name
580 %A  Current article number
581 %z  Current article score
582 %V  Gnus version
583 %U  Number of unread articles in the group
584 %e  Number of unselected articles in the group
585 %Z  A string with unread/unselected article counts
586 %g  Shortish group name
587 %S  Subject of the current article
588 %u  User-defined spec
589 %s  Current score file name
590 %d  Number of dormant articles
591 %r  Number of articles that have been marked as read in this session
592 %E  Number of articles expunged by the score files"
593   :group 'gnus-summary-format
594   :type 'string)
595
596 (defcustom gnus-list-identifiers nil
597   "Regexp that matches list identifiers to be removed from subject.
598 This can also be a list of regexps."
599   :version "21.1"
600   :group 'gnus-summary-format
601   :group 'gnus-article-hiding
602   :type '(choice (const :tag "none" nil)
603                  (regexp :value ".*")
604                  (repeat :value (".*") regexp)))
605
606 (defcustom gnus-summary-mark-below 0
607   "*Mark all articles with a score below this variable as read.
608 This variable is local to each summary buffer and usually set by the
609 score file."
610   :group 'gnus-score-default
611   :type 'integer)
612
613 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
614   "*List of functions used for sorting articles in the summary buffer.
615
616 Each function takes two articles and returns non-nil if the first
617 article should be sorted before the other.  If you use more than one
618 function, the primary sort function should be the last.  You should
619 probably always include `gnus-article-sort-by-number' in the list of
620 sorting functions -- preferably first.  Also note that sorting by date
621 is often much slower than sorting by number, and the sorting order is
622 very similar.  (Sorting by date means sorting by the time the message
623 was sent, sorting by number means sorting by arrival time.)
624
625 Ready-made functions include `gnus-article-sort-by-number',
626 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
627 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
628
629 When threading is turned on, the variable `gnus-thread-sort-functions'
630 controls how articles are sorted."
631   :group 'gnus-summary-sort
632   :type '(repeat (choice (function-item gnus-article-sort-by-number)
633                          (function-item gnus-article-sort-by-author)
634                          (function-item gnus-article-sort-by-subject)
635                          (function-item gnus-article-sort-by-date)
636                          (function-item gnus-article-sort-by-score)
637                          (function :tag "other"))))
638
639 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
640   "*List of functions used for sorting threads in the summary buffer.
641 By default, threads are sorted by article number.
642
643 Each function takes two threads and returns non-nil if the first
644 thread should be sorted before the other.  If you use more than one
645 function, the primary sort function should be the last.  You should
646 probably always include `gnus-thread-sort-by-number' in the list of
647 sorting functions -- preferably first.  Also note that sorting by date
648 is often much slower than sorting by number, and the sorting order is
649 very similar.  (Sorting by date means sorting by the time the message
650 was sent, sorting by number means sorting by arrival time.)
651
652 Ready-made functions include `gnus-thread-sort-by-number',
653 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
654 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
655 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
656
657 When threading is turned off, the variable
658 `gnus-article-sort-functions' controls how articles are sorted."
659   :group 'gnus-summary-sort
660   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
661                          (function-item gnus-thread-sort-by-author)
662                          (function-item gnus-thread-sort-by-subject)
663                          (function-item gnus-thread-sort-by-date)
664                          (function-item gnus-thread-sort-by-score)
665                          (function-item gnus-thread-sort-by-total-score)
666                          (function :tag "other"))))
667
668 (defcustom gnus-thread-score-function '+
669   "*Function used for calculating the total score of a thread.
670
671 The function is called with the scores of the article and each
672 subthread and should then return the score of the thread.
673
674 Some functions you can use are `+', `max', or `min'."
675   :group 'gnus-summary-sort
676   :type 'function)
677
678 (defcustom gnus-summary-expunge-below nil
679   "All articles that have a score less than this variable will be expunged.
680 This variable is local to the summary buffers."
681   :group 'gnus-score-default
682   :type '(choice (const :tag "off" nil)
683                  integer))
684
685 (defcustom gnus-thread-expunge-below nil
686   "All threads that have a total score less than this variable will be expunged.
687 See `gnus-thread-score-function' for en explanation of what a
688 \"thread score\" is.
689
690 This variable is local to the summary buffers."
691   :group 'gnus-threading
692   :group 'gnus-score-default
693   :type '(choice (const :tag "off" nil)
694                  integer))
695
696 (defcustom gnus-summary-mode-hook nil
697   "*A hook for Gnus summary mode.
698 This hook is run before any variables are set in the summary buffer."
699   :options '(turn-on-gnus-mailing-list-mode)
700   :group 'gnus-summary-various
701   :type 'hook)
702
703 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
704 (when (featurep 'xemacs)
705   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
706   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
707   (add-hook 'gnus-summary-mode-hook
708             'gnus-xmas-switch-horizontal-scrollbar-off))
709
710 (defcustom gnus-summary-menu-hook nil
711   "*Hook run after the creation of the summary mode menu."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-summary-exit-hook nil
716   "*A hook called on exit from the summary buffer.
717 It will be called with point in the group buffer."
718   :group 'gnus-summary-exit
719   :type 'hook)
720
721 (defcustom gnus-summary-prepare-hook nil
722   "*A hook called after the summary buffer has been generated.
723 If you want to modify the summary buffer, you can use this hook."
724   :group 'gnus-summary-various
725   :type 'hook)
726
727 (defcustom gnus-summary-prepared-hook nil
728   "*A hook called as the last thing after the summary buffer has been generated."
729   :group 'gnus-summary-various
730   :type 'hook)
731
732 (defcustom gnus-summary-generate-hook nil
733   "*A hook run just before generating the summary buffer.
734 This hook is commonly used to customize threading variables and the
735 like."
736   :group 'gnus-summary-various
737   :type 'hook)
738
739 (defcustom gnus-select-group-hook nil
740   "*A hook called when a newsgroup is selected.
741
742 If you'd like to simplify subjects like the
743 `gnus-summary-next-same-subject' command does, you can use the
744 following hook:
745
746  (setq gnus-select-group-hook
747       (list
748         (lambda ()
749           (mapcar (lambda (header)
750                      (mail-header-set-subject
751                       header
752                       (gnus-simplify-subject
753                        (mail-header-subject header) 're-only)))
754                   gnus-newsgroup-headers))))"
755   :group 'gnus-group-select
756   :type 'hook)
757
758 (defcustom gnus-select-article-hook nil
759   "*A hook called when an article is selected."
760   :group 'gnus-summary-choose
761   :type 'hook)
762
763 (defcustom gnus-visual-mark-article-hook
764   (list 'gnus-highlight-selected-summary)
765   "*Hook run after selecting an article in the summary buffer.
766 It is meant to be used for highlighting the article in some way.  It
767 is not run if `gnus-visual' is nil."
768   :group 'gnus-summary-visual
769   :type 'hook)
770
771 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
772   "*A hook called before parsing the headers."
773   :group 'gnus-various
774   :type 'hook)
775
776 (defcustom gnus-exit-group-hook nil
777   "*A hook called when exiting summary mode.
778 This hook is not called from the non-updating exit commands like `Q'."
779   :group 'gnus-various
780   :type 'hook)
781
782 (defcustom gnus-summary-update-hook
783   (list 'gnus-summary-highlight-line)
784   "*A hook called when a summary line is changed.
785 The hook will not be called if `gnus-visual' is nil.
786
787 The default function `gnus-summary-highlight-line' will
788 highlight the line according to the `gnus-summary-highlight'
789 variable."
790   :group 'gnus-summary-visual
791   :type 'hook)
792
793 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
794   "*A hook called when an article is selected for the first time.
795 The hook is intended to mark an article as read (or unread)
796 automatically when it is selected."
797   :group 'gnus-summary-choose
798   :type 'hook)
799
800 (defcustom gnus-group-no-more-groups-hook nil
801   "*A hook run when returning to group mode having no more (unread) groups."
802   :group 'gnus-group-select
803   :type 'hook)
804
805 (defcustom gnus-ps-print-hook nil
806   "*A hook run before ps-printing something from Gnus."
807   :group 'gnus-summary
808   :type 'hook)
809
810 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
811   "Face used for highlighting the current article in the summary buffer."
812   :group 'gnus-summary-visual
813   :type 'face)
814
815 (defcustom gnus-summary-highlight
816   '(((= mark gnus-canceled-mark)
817      . gnus-summary-cancelled-face)
818     ((and (> score default)
819           (or (= mark gnus-dormant-mark)
820               (= mark gnus-ticked-mark)))
821      . gnus-summary-high-ticked-face)
822     ((and (< score default)
823           (or (= mark gnus-dormant-mark)
824               (= mark gnus-ticked-mark)))
825      . gnus-summary-low-ticked-face)
826     ((or (= mark gnus-dormant-mark)
827          (= mark gnus-ticked-mark))
828      . gnus-summary-normal-ticked-face)
829     ((and (> score default) (= mark gnus-ancient-mark))
830      . gnus-summary-high-ancient-face)
831     ((and (< score default) (= mark gnus-ancient-mark))
832      . gnus-summary-low-ancient-face)
833     ((= mark gnus-ancient-mark)
834      . gnus-summary-normal-ancient-face)
835     ((and (> score default) (= mark gnus-unread-mark))
836      . gnus-summary-high-unread-face)
837     ((and (< score default) (= mark gnus-unread-mark))
838      . gnus-summary-low-unread-face)
839     ((and (memq article gnus-newsgroup-incorporated)
840           (= mark gnus-unread-mark))
841      . gnus-summary-incorporated-face)
842     ((= mark gnus-unread-mark)
843      . gnus-summary-normal-unread-face)
844     ((and (> score default) (memq mark (list gnus-downloadable-mark
845                                              gnus-undownloaded-mark)))
846      . gnus-summary-high-unread-face)
847     ((and (< score default) (memq mark (list gnus-downloadable-mark
848                                              gnus-undownloaded-mark)))
849      . gnus-summary-low-unread-face)
850     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
851      . gnus-summary-normal-unread-face)
852     ((> score default)
853      . gnus-summary-high-read-face)
854     ((< score default)
855      . gnus-summary-low-read-face)
856     (t
857      . gnus-summary-normal-read-face))
858   "*Controls the highlighting of summary buffer lines.
859
860 A list of (FORM . FACE) pairs.  When deciding how a a particular
861 summary line should be displayed, each form is evaluated.  The content
862 of the face field after the first true form is used.  You can change
863 how those summary lines are displayed, by editing the face field.
864
865 You can use the following variables in the FORM field.
866
867 score:   The articles score
868 default: The default article score.
869 below:   The score below which articles are automatically marked as read.
870 mark:    The articles mark."
871   :group 'gnus-summary-visual
872   :type '(repeat (cons (sexp :tag "Form" nil)
873                        face)))
874
875 (defcustom gnus-alter-header-function nil
876   "Function called to allow alteration of article header structures.
877 The function is called with one parameter, the article header vector,
878 which it may alter in any way.")
879
880 (defvar gnus-decode-encoded-word-function
881   (mime-find-field-decoder 'From 'nov)
882   "Variable that says which function should be used to decode a string with encoded words.")
883
884 (defcustom gnus-extra-headers nil
885   "*Extra headers to parse."
886   :version "21.1"
887   :group 'gnus-summary
888   :type '(repeat symbol))
889
890 (defcustom gnus-ignored-from-addresses
891   (and user-mail-address (regexp-quote user-mail-address))
892   "*Regexp of From headers that may be suppressed in favor of To headers."
893   :version "21.1"
894   :group 'gnus-summary
895   :type 'regexp)
896
897 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
898   "List of charsets that should be ignored.
899 When these charsets are used in the \"charset\" parameter, the
900 default charset will be used instead."
901   :version "21.1"
902   :type '(repeat symbol)
903   :group 'gnus-charset)
904
905 (gnus-define-group-parameter
906  ignored-charsets
907  :type list
908  :function-document
909  "Return the ignored charsets of GROUP."
910  :variable gnus-group-ignored-charsets-alist
911  :variable-default 
912  '(("alt\\.chinese\\.text" iso-8859-1))
913  :variable-document
914  "Alist of regexps (to match group names) and charsets that should be ignored.
915 When these charsets are used in the \"charset\" parameter, the
916 default charset will be used instead."
917  :variable-group gnus-charset
918  :variable-type '(repeat (cons (regexp :tag "Group")
919                                (repeat symbol)))
920  :parameter-type '(choice :tag "Ignored charsets" 
921                           :value nil
922                           (repeat (symbol)))
923  :parameter-document       "\
924 List of charsets that should be ignored.
925
926 When these charsets are used in the \"charset\" parameter, the
927 default charset will be used instead.")
928
929 (defcustom gnus-group-highlight-words-alist nil
930   "Alist of group regexps and highlight regexps.
931 This variable uses the same syntax as `gnus-emphasis-alist'."
932   :version "21.1"
933   :type '(repeat (cons (regexp :tag "Group")
934                        (repeat (list (regexp :tag "Highlight regexp")
935                                      (number :tag "Group for entire word" 0)
936                                      (number :tag "Group for displayed part" 0)
937                                      (symbol :tag "Face"
938                                              gnus-emphasis-highlight-words)))))
939   :group 'gnus-summary-visual)
940
941 (defcustom gnus-use-wheel nil
942   "Use Intelli-mouse on summary movement"
943   :type 'boolean
944   :group 'gnus-summary-maneuvering)
945
946 (defcustom gnus-wheel-scroll-amount '(5 . 1)
947   "Amount to scroll messages by spinning the mouse wheel.
948 This is actually a cons cell, where the first item is the amount to scroll
949 on a normal wheel event, and the second is the amount to scroll when the
950 wheel is moved with the shift key depressed."
951   :type '(cons (integer :tag "Shift") integer)
952   :group 'gnus-summary-maneuvering)
953
954 (defcustom gnus-wheel-edge-resistance 2
955   "How hard it should be to change the current article
956 by moving the mouse over the edge of the article window."
957   :type 'integer
958   :group 'gnus-summary-maneuvering)
959
960 (defcustom gnus-summary-show-article-charset-alist
961   nil
962   "Alist of number and charset.
963 The article will be shown with the charset corresponding to the
964 numbered argument.
965 For example: ((1 . cn-gb-2312) (2 . big5))."
966   :version "21.1"
967   :type '(repeat (cons (number :tag "Argument" 1)
968                        (symbol :tag "Charset")))
969   :group 'gnus-charset)
970
971 (defcustom gnus-preserve-marks t
972   "Whether marks are preserved when moving, copying and respooling messages."
973   :version "21.1"
974   :type 'boolean
975   :group 'gnus-summary-marks)
976
977 (defcustom gnus-alter-articles-to-read-function nil
978   "Function to be called to alter the list of articles to be selected."
979   :type '(choice (const nil) function)
980   :group 'gnus-summary)
981
982 (defcustom gnus-orphan-score nil
983   "*All orphans get this score added.  Set in the score file."
984   :group 'gnus-score-default
985   :type '(choice (const nil)
986                  integer))
987
988 (defcustom gnus-summary-save-parts-default-mime "image/.*"
989   "*A regexp to match MIME parts when saving multiple parts of a message
990 with gnus-summary-save-parts (X m). This regexp will be used by default
991 when prompting the user for which type of files to save."
992   :group 'gnus-summary
993   :type 'regexp)
994
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 ;;; Internal variables
1005
1006 (defvar gnus-article-mime-handles nil)
1007 (defvar gnus-article-decoded-p nil)
1008 (defvar gnus-article-charset nil)
1009 (defvar gnus-article-ignored-charsets nil)
1010 (defvar gnus-scores-exclude-files nil)
1011 (defvar gnus-page-broken nil)
1012 (defvar gnus-inhibit-mime-unbuttonizing nil)
1013
1014 (defvar gnus-original-article nil)
1015 (defvar gnus-article-internal-prepare-hook nil)
1016 (defvar gnus-newsgroup-process-stack nil)
1017
1018 (defvar gnus-thread-indent-array nil)
1019 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1020 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1021   "Function called to sort the articles within a thread after it has been gathered together.")
1022
1023 (defvar gnus-summary-save-parts-type-history nil)
1024 (defvar gnus-summary-save-parts-last-directory nil)
1025
1026 (defvar gnus-summary-save-parts-type-history nil)
1027 (defvar gnus-summary-save-parts-last-directory nil)
1028
1029 ;; Avoid highlighting in kill files.
1030 (defvar gnus-summary-inhibit-highlight nil)
1031 (defvar gnus-newsgroup-selected-overlay nil)
1032 (defvar gnus-inhibit-limiting nil)
1033 (defvar gnus-newsgroup-adaptive-score-file nil)
1034 (defvar gnus-current-score-file nil)
1035 (defvar gnus-current-move-group nil)
1036 (defvar gnus-current-copy-group nil)
1037 (defvar gnus-current-crosspost-group nil)
1038
1039 (defvar gnus-newsgroup-dependencies nil)
1040 (defvar gnus-newsgroup-adaptive nil)
1041 (defvar gnus-summary-display-article-function nil)
1042 (defvar gnus-summary-highlight-line-function nil
1043   "Function called after highlighting a summary line.")
1044
1045 (defvar gnus-summary-line-format-alist
1046   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1047     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1048     (?s gnus-tmp-subject-or-nil ?s)
1049     (?n gnus-tmp-name ?s)
1050     (?A (std11-address-string
1051          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1052     (?a (or (std11-full-name-string
1053              (car (mime-entity-read-field gnus-tmp-header 'From)))
1054             gnus-tmp-from) ?s)
1055     (?F gnus-tmp-from ?s)
1056     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1057     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1058     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1059     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1060     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1061     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1062     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1063     (?L gnus-tmp-lines ?d)
1064     (?I gnus-tmp-indentation ?s)
1065     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1066     (?R gnus-tmp-replied ?c)
1067     (?\[ gnus-tmp-opening-bracket ?c)
1068     (?\] gnus-tmp-closing-bracket ?c)
1069     (?\> (make-string gnus-tmp-level ? ) ?s)
1070     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1071     (?i gnus-tmp-score ?d)
1072     (?z gnus-tmp-score-char ?c)
1073     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1074     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1075     (?U gnus-tmp-unread ?c)
1076     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1077     (?t (gnus-summary-number-of-articles-in-thread
1078          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1079         ?d)
1080     (?e (gnus-summary-number-of-articles-in-thread
1081          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1082         ?c)
1083     (?u gnus-tmp-user-defined ?s)
1084     (?P (gnus-pick-line-number) ?d))
1085   "An alist of format specifications that can appear in summary lines.
1086 These are paired with what variables they correspond with, along with
1087 the type of the variable (string, integer, character, etc).")
1088
1089 (defvar gnus-summary-dummy-line-format-alist
1090   `((?S gnus-tmp-subject ?s)
1091     (?N gnus-tmp-number ?d)
1092     (?u gnus-tmp-user-defined ?s)))
1093
1094 (defvar gnus-summary-mode-line-format-alist
1095   `((?G gnus-tmp-group-name ?s)
1096     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1097     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1098     (?A gnus-tmp-article-number ?d)
1099     (?Z gnus-tmp-unread-and-unselected ?s)
1100     (?V gnus-version ?s)
1101     (?U gnus-tmp-unread-and-unticked ?d)
1102     (?S gnus-tmp-subject ?s)
1103     (?e gnus-tmp-unselected ?d)
1104     (?u gnus-tmp-user-defined ?s)
1105     (?d (length gnus-newsgroup-dormant) ?d)
1106     (?t (length gnus-newsgroup-marked) ?d)
1107     (?r (length gnus-newsgroup-reads) ?d)
1108     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1109     (?E gnus-newsgroup-expunged-tally ?d)
1110     (?s (gnus-current-score-file-nondirectory) ?s)))
1111
1112 (defvar gnus-last-search-regexp nil
1113   "Default regexp for article search command.")
1114
1115 (defvar gnus-summary-search-article-matched-data nil
1116   "Last matched data of article search command.  It is the local variable
1117 in `gnus-article-buffer' which consists of the list of start position,
1118 end position and text.")
1119
1120 (defvar gnus-last-shell-command nil
1121   "Default shell command on article.")
1122
1123 (defvar gnus-newsgroup-begin nil)
1124 (defvar gnus-newsgroup-end nil)
1125 (defvar gnus-newsgroup-last-rmail nil)
1126 (defvar gnus-newsgroup-last-mail nil)
1127 (defvar gnus-newsgroup-last-folder nil)
1128 (defvar gnus-newsgroup-last-file nil)
1129 (defvar gnus-newsgroup-auto-expire nil)
1130 (defvar gnus-newsgroup-active nil)
1131
1132 (defvar gnus-newsgroup-data nil)
1133 (defvar gnus-newsgroup-data-reverse nil)
1134 (defvar gnus-newsgroup-limit nil)
1135 (defvar gnus-newsgroup-limits nil)
1136
1137 (defvar gnus-newsgroup-unreads nil
1138   "List of unread articles in the current newsgroup.")
1139
1140 (defvar gnus-newsgroup-unselected nil
1141   "List of unselected unread articles in the current newsgroup.")
1142
1143 (defvar gnus-newsgroup-reads nil
1144   "Alist of read articles and article marks in the current newsgroup.")
1145
1146 (defvar gnus-newsgroup-expunged-tally nil)
1147
1148 (defvar gnus-newsgroup-marked nil
1149   "List of ticked articles in the current newsgroup (a subset of unread art).")
1150
1151 (defvar gnus-newsgroup-killed nil
1152   "List of ranges of articles that have been through the scoring process.")
1153
1154 (defvar gnus-newsgroup-cached nil
1155   "List of articles that come from the article cache.")
1156
1157 (defvar gnus-newsgroup-saved nil
1158   "List of articles that have been saved.")
1159
1160 (defvar gnus-newsgroup-kill-headers nil)
1161
1162 (defvar gnus-newsgroup-replied nil
1163   "List of articles that have been replied to in the current newsgroup.")
1164
1165 (defvar gnus-newsgroup-expirable nil
1166   "List of articles in the current newsgroup that can be expired.")
1167
1168 (defvar gnus-newsgroup-processable nil
1169   "List of articles in the current newsgroup that can be processed.")
1170
1171 (defvar gnus-newsgroup-downloadable nil
1172   "List of articles in the current newsgroup that can be processed.")
1173
1174 (defvar gnus-newsgroup-undownloaded nil
1175   "List of articles in the current newsgroup that haven't been downloaded..")
1176
1177 (defvar gnus-newsgroup-unsendable nil
1178   "List of articles in the current newsgroup that won't be sent.")
1179
1180 (defvar gnus-newsgroup-bookmarks nil
1181   "List of articles in the current newsgroup that have bookmarks.")
1182
1183 (defvar gnus-newsgroup-dormant nil
1184   "List of dormant articles in the current newsgroup.")
1185
1186 (defvar gnus-newsgroup-scored nil
1187   "List of scored articles in the current newsgroup.")
1188
1189 (defvar gnus-newsgroup-incorporated nil
1190   "List of incorporated articles in the current newsgroup.")
1191
1192 (defvar gnus-newsgroup-headers nil
1193   "List of article headers in the current newsgroup.")
1194
1195 (defvar gnus-newsgroup-threads nil)
1196
1197 (defvar gnus-newsgroup-prepared nil
1198   "Whether the current group has been prepared properly.")
1199
1200 (defvar gnus-newsgroup-ancient nil
1201   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1202
1203 (defvar gnus-newsgroup-sparse nil)
1204
1205 (defvar gnus-current-article nil)
1206 (defvar gnus-article-current nil)
1207 (defvar gnus-current-headers nil)
1208 (defvar gnus-have-all-headers nil)
1209 (defvar gnus-last-article nil)
1210 (defvar gnus-newsgroup-history nil)
1211 (defvar gnus-newsgroup-charset nil)
1212 (defvar gnus-newsgroup-ephemeral-charset nil)
1213 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1214
1215 (defvar gnus-article-before-search nil)
1216
1217 (defconst gnus-summary-local-variables
1218   '(gnus-newsgroup-name
1219     gnus-newsgroup-begin gnus-newsgroup-end
1220     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1221     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1222     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1223     gnus-newsgroup-unselected gnus-newsgroup-marked
1224     gnus-newsgroup-reads gnus-newsgroup-saved
1225     gnus-newsgroup-replied gnus-newsgroup-expirable
1226     gnus-newsgroup-processable gnus-newsgroup-killed
1227     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1228     gnus-newsgroup-unsendable
1229     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1230     gnus-newsgroup-headers gnus-newsgroup-threads
1231     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1232     gnus-current-article gnus-current-headers gnus-have-all-headers
1233     gnus-last-article gnus-article-internal-prepare-hook
1234     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1235     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1236     gnus-thread-expunge-below
1237     gnus-score-alist gnus-current-score-file
1238     (gnus-summary-expunge-below . global)
1239     (gnus-summary-mark-below . global)
1240     (gnus-orphan-score . global)
1241     gnus-newsgroup-active gnus-scores-exclude-files
1242     gnus-newsgroup-history gnus-newsgroup-ancient
1243     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1244     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1245     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1246     (gnus-newsgroup-expunged-tally . 0)
1247     gnus-cache-removable-articles gnus-newsgroup-cached
1248     gnus-newsgroup-data gnus-newsgroup-data-reverse
1249     gnus-newsgroup-limit gnus-newsgroup-limits
1250     gnus-newsgroup-charset
1251     gnus-newsgroup-incorporated)
1252   "Variables that are buffer-local to the summary buffers.")
1253
1254 (defvar gnus-newsgroup-variables nil
1255   "Variables that have separate values in the newsgroups.")
1256
1257 ;; Byte-compiler warning.
1258 (eval-when-compile (defvar gnus-article-mode-map))
1259
1260 ;; Subject simplification.
1261
1262 (defun gnus-simplify-whitespace (str)
1263   "Remove excessive whitespace from STR."
1264   (let ((mystr str))
1265     ;; Multiple spaces.
1266     (while (string-match "[ \t][ \t]+" mystr)
1267       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1268                           " "
1269                           (substring mystr (match-end 0)))))
1270     ;; Leading spaces.
1271     (when (string-match "^[ \t]+" mystr)
1272       (setq mystr (substring mystr (match-end 0))))
1273     ;; Trailing spaces.
1274     (when (string-match "[ \t]+$" mystr)
1275       (setq mystr (substring mystr 0 (match-beginning 0))))
1276     mystr))
1277
1278 (defsubst gnus-simplify-subject-re (subject)
1279   "Remove \"Re:\" from subject lines."
1280   (if (string-match message-subject-re-regexp subject)
1281       (substring subject (match-end 0))
1282     subject))
1283
1284 (defun gnus-simplify-subject (subject &optional re-only)
1285   "Remove `Re:' and words in parentheses.
1286 If RE-ONLY is non-nil, strip leading `Re:'s only."
1287   (let ((case-fold-search t))           ;Ignore case.
1288     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1289     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1290       (setq subject (substring subject (match-end 0))))
1291     ;; Remove uninteresting prefixes.
1292     (when (and (not re-only)
1293                gnus-simplify-ignored-prefixes
1294                (string-match gnus-simplify-ignored-prefixes subject))
1295       (setq subject (substring subject (match-end 0))))
1296     ;; Remove words in parentheses from end.
1297     (unless re-only
1298       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1299         (setq subject (substring subject 0 (match-beginning 0)))))
1300     ;; Return subject string.
1301     subject))
1302
1303 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1304 ;; all whitespace.
1305 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1306   (goto-char (point-min))
1307   (while (re-search-forward regexp nil t)
1308     (replace-match (or newtext ""))))
1309
1310 (defun gnus-simplify-buffer-fuzzy ()
1311   "Simplify string in the buffer fuzzily.
1312 The string in the accessible portion of the current buffer is simplified.
1313 It is assumed to be a single-line subject.
1314 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1315 matter is removed.  Additional things can be deleted by setting
1316 `gnus-simplify-subject-fuzzy-regexp'."
1317   (let ((case-fold-search t)
1318         (modified-tick))
1319     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1320
1321     (while (not (eq modified-tick (buffer-modified-tick)))
1322       (setq modified-tick (buffer-modified-tick))
1323       (cond
1324        ((listp gnus-simplify-subject-fuzzy-regexp)
1325         (mapcar 'gnus-simplify-buffer-fuzzy-step
1326                 gnus-simplify-subject-fuzzy-regexp))
1327        (gnus-simplify-subject-fuzzy-regexp
1328         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1329       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1330       (gnus-simplify-buffer-fuzzy-step
1331        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1332       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1333
1334     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1335     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1336     (gnus-simplify-buffer-fuzzy-step " $")
1337     (gnus-simplify-buffer-fuzzy-step "^ +")))
1338
1339 (defun gnus-simplify-subject-fuzzy (subject)
1340   "Simplify a subject string fuzzily.
1341 See `gnus-simplify-buffer-fuzzy' for details."
1342   (save-excursion
1343     (gnus-set-work-buffer)
1344     (let ((case-fold-search t))
1345       ;; Remove uninteresting prefixes.
1346       (when (and gnus-simplify-ignored-prefixes
1347                  (string-match gnus-simplify-ignored-prefixes subject))
1348         (setq subject (substring subject (match-end 0))))
1349       (insert subject)
1350       (inline (gnus-simplify-buffer-fuzzy))
1351       (buffer-string))))
1352
1353 (defsubst gnus-simplify-subject-fully (subject)
1354   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1355   (cond
1356    (gnus-simplify-subject-functions
1357     (gnus-map-function gnus-simplify-subject-functions subject))
1358    ((null gnus-summary-gather-subject-limit)
1359     (gnus-simplify-subject-re subject))
1360    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1361     (gnus-simplify-subject-fuzzy subject))
1362    ((numberp gnus-summary-gather-subject-limit)
1363     (gnus-limit-string (gnus-simplify-subject-re subject)
1364                        gnus-summary-gather-subject-limit))
1365    (t
1366     subject)))
1367
1368 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1369   "Check whether two subjects are equal.
1370 If optional argument simple-first is t, first argument is already
1371 simplified."
1372   (cond
1373    ((null simple-first)
1374     (equal (gnus-simplify-subject-fully s1)
1375            (gnus-simplify-subject-fully s2)))
1376    (t
1377     (equal s1
1378            (gnus-simplify-subject-fully s2)))))
1379
1380 (defun gnus-summary-bubble-group ()
1381   "Increase the score of the current group.
1382 This is a handy function to add to `gnus-summary-exit-hook' to
1383 increase the score of each group you read."
1384   (gnus-group-add-score gnus-newsgroup-name))
1385
1386 \f
1387 ;;;
1388 ;;; Gnus summary mode
1389 ;;;
1390
1391 (put 'gnus-summary-mode 'mode-class 'special)
1392
1393 (defvar gnus-article-commands-menu)
1394
1395 (when t
1396   ;; Non-orthogonal keys
1397
1398   (gnus-define-keys gnus-summary-mode-map
1399     " " gnus-summary-next-page
1400     "\177" gnus-summary-prev-page
1401     [delete] gnus-summary-prev-page
1402     [backspace] gnus-summary-prev-page
1403     "\r" gnus-summary-scroll-up
1404     "\M-\r" gnus-summary-scroll-down
1405     "n" gnus-summary-next-unread-article
1406     "p" gnus-summary-prev-unread-article
1407     "N" gnus-summary-next-article
1408     "P" gnus-summary-prev-article
1409     "\M-\C-n" gnus-summary-next-same-subject
1410     "\M-\C-p" gnus-summary-prev-same-subject
1411     "\M-n" gnus-summary-next-unread-subject
1412     "\M-p" gnus-summary-prev-unread-subject
1413     "." gnus-summary-first-unread-article
1414     "," gnus-summary-best-unread-article
1415     "\M-s" gnus-summary-search-article-forward
1416     "\M-r" gnus-summary-search-article-backward
1417     "<" gnus-summary-beginning-of-article
1418     ">" gnus-summary-end-of-article
1419     "j" gnus-summary-goto-article
1420     "^" gnus-summary-refer-parent-article
1421     "\M-^" gnus-summary-refer-article
1422     "u" gnus-summary-tick-article-forward
1423     "!" gnus-summary-tick-article-forward
1424     "U" gnus-summary-tick-article-backward
1425     "d" gnus-summary-mark-as-read-forward
1426     "D" gnus-summary-mark-as-read-backward
1427     "E" gnus-summary-mark-as-expirable
1428     "\M-u" gnus-summary-clear-mark-forward
1429     "\M-U" gnus-summary-clear-mark-backward
1430     "k" gnus-summary-kill-same-subject-and-select
1431     "\C-k" gnus-summary-kill-same-subject
1432     "\M-\C-k" gnus-summary-kill-thread
1433     "\M-\C-l" gnus-summary-lower-thread
1434     "e" gnus-summary-edit-article
1435     "#" gnus-summary-mark-as-processable
1436     "\M-#" gnus-summary-unmark-as-processable
1437     "\M-\C-t" gnus-summary-toggle-threads
1438     "\M-\C-s" gnus-summary-show-thread
1439     "\M-\C-h" gnus-summary-hide-thread
1440     "\M-\C-f" gnus-summary-next-thread
1441     "\M-\C-b" gnus-summary-prev-thread
1442     [(meta down)] gnus-summary-next-thread
1443     [(meta up)] gnus-summary-prev-thread
1444     "\M-\C-u" gnus-summary-up-thread
1445     "\M-\C-d" gnus-summary-down-thread
1446     "&" gnus-summary-execute-command
1447     "c" gnus-summary-catchup-and-exit
1448     "\C-w" gnus-summary-mark-region-as-read
1449     "\C-t" gnus-summary-toggle-truncation
1450     "?" gnus-summary-mark-as-dormant
1451     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1452     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1453     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1454     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1455     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1456     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1457     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1458     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1459     "=" gnus-summary-expand-window
1460     "\C-x\C-s" gnus-summary-reselect-current-group
1461     "\M-g" gnus-summary-rescan-group
1462     "w" gnus-summary-stop-page-breaking
1463     "\C-c\C-r" gnus-summary-caesar-message
1464     "\M-t" gnus-summary-toggle-mime
1465     "f" gnus-summary-followup
1466     "F" gnus-summary-followup-with-original
1467     "C" gnus-summary-cancel-article
1468     "r" gnus-summary-reply
1469     "R" gnus-summary-reply-with-original
1470     "\C-c\C-f" gnus-summary-mail-forward
1471     "o" gnus-summary-save-article
1472     "\C-o" gnus-summary-save-article-mail
1473     "|" gnus-summary-pipe-output
1474     "\M-k" gnus-summary-edit-local-kill
1475     "\M-K" gnus-summary-edit-global-kill
1476     ;; "V" gnus-version
1477     "\C-c\C-d" gnus-summary-describe-group
1478     "q" gnus-summary-exit
1479     "Q" gnus-summary-exit-no-update
1480     "\C-c\C-i" gnus-info-find-node
1481     gnus-mouse-2 gnus-mouse-pick-article
1482     "m" gnus-summary-mail-other-window
1483     "a" gnus-summary-post-news
1484     "x" gnus-summary-limit-to-unread
1485     "s" gnus-summary-isearch-article
1486     "t" gnus-article-toggle-headers
1487     "g" gnus-summary-show-article
1488     "l" gnus-summary-goto-last-article
1489     "v" gnus-summary-preview-mime-message
1490     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1491     "\C-d" gnus-summary-enter-digest-group
1492     "\M-\C-d" gnus-summary-read-document
1493     "\M-\C-e" gnus-summary-edit-parameters
1494     "\M-\C-a" gnus-summary-customize-parameters
1495     "\C-c\C-b" gnus-bug
1496     "*" gnus-cache-enter-article
1497     "\M-*" gnus-cache-remove-article
1498     "\M-&" gnus-summary-universal-argument
1499     "\C-l" gnus-recenter
1500     "I" gnus-summary-increase-score
1501     "L" gnus-summary-lower-score
1502     "\M-i" gnus-symbolic-argument
1503     "h" gnus-summary-select-article-buffer
1504
1505     "V" gnus-summary-score-map
1506     "X" gnus-uu-extract-map
1507     "S" gnus-summary-send-map)
1508
1509   ;; Sort of orthogonal keymap
1510   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1511     "t" gnus-summary-tick-article-forward
1512     "!" gnus-summary-tick-article-forward
1513     "d" gnus-summary-mark-as-read-forward
1514     "r" gnus-summary-mark-as-read-forward
1515     "c" gnus-summary-clear-mark-forward
1516     " " gnus-summary-clear-mark-forward
1517     "e" gnus-summary-mark-as-expirable
1518     "x" gnus-summary-mark-as-expirable
1519     "?" gnus-summary-mark-as-dormant
1520     "b" gnus-summary-set-bookmark
1521     "B" gnus-summary-remove-bookmark
1522     "#" gnus-summary-mark-as-processable
1523     "\M-#" gnus-summary-unmark-as-processable
1524     "S" gnus-summary-limit-include-expunged
1525     "C" gnus-summary-catchup
1526     "H" gnus-summary-catchup-to-here
1527     "\C-c" gnus-summary-catchup-all
1528     "k" gnus-summary-kill-same-subject-and-select
1529     "K" gnus-summary-kill-same-subject
1530     "P" gnus-uu-mark-map)
1531
1532   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1533     "c" gnus-summary-clear-above
1534     "u" gnus-summary-tick-above
1535     "m" gnus-summary-mark-above
1536     "k" gnus-summary-kill-below)
1537
1538   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1539     "/" gnus-summary-limit-to-subject
1540     "n" gnus-summary-limit-to-articles
1541     "w" gnus-summary-pop-limit
1542     "s" gnus-summary-limit-to-subject
1543     "a" gnus-summary-limit-to-author
1544     "u" gnus-summary-limit-to-unread
1545     "m" gnus-summary-limit-to-marks
1546     "M" gnus-summary-limit-exclude-marks
1547     "v" gnus-summary-limit-to-score
1548     "*" gnus-summary-limit-include-cached
1549     "D" gnus-summary-limit-include-dormant
1550     "T" gnus-summary-limit-include-thread
1551     "d" gnus-summary-limit-exclude-dormant
1552     "t" gnus-summary-limit-to-age
1553     "x" gnus-summary-limit-to-extra
1554     "E" gnus-summary-limit-include-expunged
1555     "c" gnus-summary-limit-exclude-childless-dormant
1556     "C" gnus-summary-limit-mark-excluded-as-read
1557     "o" gnus-summary-insert-old-articles
1558     "N" gnus-summary-insert-new-articles)
1559
1560   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1561     "n" gnus-summary-next-unread-article
1562     "p" gnus-summary-prev-unread-article
1563     "N" gnus-summary-next-article
1564     "P" gnus-summary-prev-article
1565     "\C-n" gnus-summary-next-same-subject
1566     "\C-p" gnus-summary-prev-same-subject
1567     "\M-n" gnus-summary-next-unread-subject
1568     "\M-p" gnus-summary-prev-unread-subject
1569     "f" gnus-summary-first-unread-article
1570     "b" gnus-summary-best-unread-article
1571     "j" gnus-summary-goto-article
1572     "g" gnus-summary-goto-subject
1573     "l" gnus-summary-goto-last-article
1574     "o" gnus-summary-pop-article)
1575
1576   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1577     "k" gnus-summary-kill-thread
1578     "l" gnus-summary-lower-thread
1579     "i" gnus-summary-raise-thread
1580     "T" gnus-summary-toggle-threads
1581     "t" gnus-summary-rethread-current
1582     "^" gnus-summary-reparent-thread
1583     "s" gnus-summary-show-thread
1584     "S" gnus-summary-show-all-threads
1585     "h" gnus-summary-hide-thread
1586     "H" gnus-summary-hide-all-threads
1587     "n" gnus-summary-next-thread
1588     "p" gnus-summary-prev-thread
1589     "u" gnus-summary-up-thread
1590     "o" gnus-summary-top-thread
1591     "d" gnus-summary-down-thread
1592     "#" gnus-uu-mark-thread
1593     "\M-#" gnus-uu-unmark-thread)
1594
1595   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1596     "g" gnus-summary-prepare
1597     "c" gnus-summary-insert-cached-articles)
1598
1599   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1600     "c" gnus-summary-catchup-and-exit
1601     "C" gnus-summary-catchup-all-and-exit
1602     "E" gnus-summary-exit-no-update
1603     "J" gnus-summary-jump-to-other-group
1604     "Q" gnus-summary-exit
1605     "Z" gnus-summary-exit
1606     "n" gnus-summary-catchup-and-goto-next-group
1607     "R" gnus-summary-reselect-current-group
1608     "G" gnus-summary-rescan-group
1609     "N" gnus-summary-next-group
1610     "s" gnus-summary-save-newsrc
1611     "P" gnus-summary-prev-group)
1612
1613   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1614     " " gnus-summary-next-page
1615     "n" gnus-summary-next-page
1616     "\177" gnus-summary-prev-page
1617     [delete] gnus-summary-prev-page
1618     "p" gnus-summary-prev-page
1619     "\r" gnus-summary-scroll-up
1620     "\M-\r" gnus-summary-scroll-down
1621     "<" gnus-summary-beginning-of-article
1622     ">" gnus-summary-end-of-article
1623     "b" gnus-summary-beginning-of-article
1624     "e" gnus-summary-end-of-article
1625     "^" gnus-summary-refer-parent-article
1626     "r" gnus-summary-refer-parent-article
1627     "D" gnus-summary-enter-digest-group
1628     "R" gnus-summary-refer-references
1629     "T" gnus-summary-refer-thread
1630     "g" gnus-summary-show-article
1631     "s" gnus-summary-isearch-article
1632     "P" gnus-summary-print-article
1633     "M" gnus-mailing-list-insinuate
1634     "t" gnus-article-babel)
1635
1636   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1637     "b" gnus-article-add-buttons
1638     "B" gnus-article-add-buttons-to-head
1639     "o" gnus-article-treat-overstrike
1640     "e" gnus-article-emphasize
1641     "w" gnus-article-fill-cited-article
1642     "Q" gnus-article-fill-long-lines
1643     "C" gnus-article-capitalize-sentences
1644     "c" gnus-article-remove-cr
1645     "Z" gnus-article-decode-HZ
1646     "f" gnus-article-display-x-face
1647     "l" gnus-summary-stop-page-breaking
1648     "r" gnus-summary-caesar-message
1649     "t" gnus-article-toggle-headers
1650     "v" gnus-summary-verbose-headers
1651     "m" gnus-summary-toggle-mime
1652     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1653     "p" gnus-article-verify-x-pgp-sig
1654     "d" gnus-article-treat-dumbquotes
1655     "s" gnus-smiley-display)
1656
1657   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1658     "a" gnus-article-hide
1659     "h" gnus-article-toggle-headers
1660     "b" gnus-article-hide-boring-headers
1661     "s" gnus-article-hide-signature
1662     "c" gnus-article-hide-citation
1663     "C" gnus-article-hide-citation-in-followups
1664     "l" gnus-article-hide-list-identifiers
1665     "p" gnus-article-hide-pgp
1666     "B" gnus-article-strip-banner
1667     "P" gnus-article-hide-pem
1668     "\C-c" gnus-article-hide-citation-maybe)
1669
1670   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1671     "a" gnus-article-highlight
1672     "h" gnus-article-highlight-headers
1673     "c" gnus-article-highlight-citation
1674     "s" gnus-article-highlight-signature)
1675
1676   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1677     "z" gnus-article-date-ut
1678     "u" gnus-article-date-ut
1679     "l" gnus-article-date-local
1680     "p" gnus-article-date-english
1681     "e" gnus-article-date-lapsed
1682     "o" gnus-article-date-original
1683     "i" gnus-article-date-iso8601
1684     "s" gnus-article-date-user)
1685
1686   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1687     "t" gnus-article-remove-trailing-blank-lines
1688     "l" gnus-article-strip-leading-blank-lines
1689     "m" gnus-article-strip-multiple-blank-lines
1690     "a" gnus-article-strip-blank-lines
1691     "A" gnus-article-strip-all-blank-lines
1692     "s" gnus-article-strip-leading-space
1693     "e" gnus-article-strip-trailing-space
1694     "w" gnus-article-remove-leading-whitespace)
1695
1696   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1697     "v" gnus-version
1698     "f" gnus-summary-fetch-faq
1699     "d" gnus-summary-describe-group
1700     "h" gnus-summary-describe-briefly
1701     "i" gnus-info-find-node)
1702
1703   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1704     "e" gnus-summary-expire-articles
1705     "\M-\C-e" gnus-summary-expire-articles-now
1706     "\177" gnus-summary-delete-article
1707     [delete] gnus-summary-delete-article
1708     [backspace] gnus-summary-delete-article
1709     "m" gnus-summary-move-article
1710     "r" gnus-summary-respool-article
1711     "w" gnus-summary-edit-article
1712     "c" gnus-summary-copy-article
1713     "B" gnus-summary-crosspost-article
1714     "q" gnus-summary-respool-query
1715     "t" gnus-summary-respool-trace
1716     "i" gnus-summary-import-article
1717     "I" gnus-summary-create-article
1718     "p" gnus-summary-article-posted-p)
1719
1720   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1721     "o" gnus-summary-save-article
1722     "m" gnus-summary-save-article-mail
1723     "F" gnus-summary-write-article-file
1724     "r" gnus-summary-save-article-rmail
1725     "f" gnus-summary-save-article-file
1726     "b" gnus-summary-save-article-body-file
1727     "h" gnus-summary-save-article-folder
1728     "v" gnus-summary-save-article-vm
1729     "p" gnus-summary-pipe-output
1730     "s" gnus-soup-add-article)
1731
1732   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1733     "b" gnus-summary-display-buttonized
1734     "m" gnus-summary-repair-multipart
1735     "v" gnus-article-view-part
1736     "o" gnus-article-save-part
1737     "c" gnus-article-copy-part
1738     "C" gnus-article-view-part-as-charset
1739     "e" gnus-article-externalize-part
1740     "E" gnus-article-encrypt-body
1741     "i" gnus-article-inline-part
1742     "|" gnus-article-pipe-part))
1743
1744 (defvar gnus-article-post-menu nil)
1745
1746 (defun gnus-summary-make-menu-bar ()
1747   (gnus-turn-off-edit-menu 'summary)
1748
1749   (unless (boundp 'gnus-summary-misc-menu)
1750
1751     (easy-menu-define
1752      gnus-summary-kill-menu gnus-summary-mode-map ""
1753      (cons
1754       "Score"
1755       (nconc
1756        (list
1757         ["Customize" gnus-score-customize t])
1758        (gnus-make-score-map 'increase)
1759        (gnus-make-score-map 'lower)
1760        '(("Mark"
1761           ["Kill below" gnus-summary-kill-below t]
1762           ["Mark above" gnus-summary-mark-above t]
1763           ["Tick above" gnus-summary-tick-above t]
1764           ["Clear above" gnus-summary-clear-above t])
1765          ["Current score" gnus-summary-current-score t]
1766          ["Set score" gnus-summary-set-score t]
1767          ["Switch current score file..." gnus-score-change-score-file t]
1768          ["Set mark below..." gnus-score-set-mark-below t]
1769          ["Set expunge below..." gnus-score-set-expunge-below t]
1770          ["Edit current score file" gnus-score-edit-current-scores t]
1771          ["Edit score file" gnus-score-edit-file t]
1772          ["Trace score" gnus-score-find-trace t]
1773          ["Find words" gnus-score-find-favourite-words t]
1774          ["Rescore buffer" gnus-summary-rescore t]
1775          ["Increase score..." gnus-summary-increase-score t]
1776          ["Lower score..." gnus-summary-lower-score t]))))
1777
1778     ;; Define both the Article menu in the summary buffer and the equivalent
1779     ;; Commands menu in the article buffer here for consistency.
1780     (let ((innards
1781            `(("Hide"
1782               ["All" gnus-article-hide t]
1783               ["Headers" gnus-article-toggle-headers t]
1784               ["Signature" gnus-article-hide-signature t]
1785               ["Citation" gnus-article-hide-citation t]
1786               ["List identifiers" gnus-article-hide-list-identifiers t]
1787               ["PGP" gnus-article-hide-pgp t]
1788               ["Banner" gnus-article-strip-banner t]
1789               ["Boring headers" gnus-article-hide-boring-headers t])
1790              ("Highlight"
1791               ["All" gnus-article-highlight t]
1792               ["Headers" gnus-article-highlight-headers t]
1793               ["Signature" gnus-article-highlight-signature t]
1794               ["Citation" gnus-article-highlight-citation t])
1795              ("Date"
1796               ["Local" gnus-article-date-local t]
1797               ["ISO8601" gnus-article-date-iso8601 t]
1798               ["UT" gnus-article-date-ut t]
1799               ["Original" gnus-article-date-original t]
1800               ["Lapsed" gnus-article-date-lapsed t]
1801               ["User-defined" gnus-article-date-user t])
1802              ("Washing"
1803               ("Remove Blanks"
1804                ["Leading" gnus-article-strip-leading-blank-lines t]
1805                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1806                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1807                ["All of the above" gnus-article-strip-blank-lines t]
1808                ["All" gnus-article-strip-all-blank-lines t]
1809                ["Leading space" gnus-article-strip-leading-space t]
1810                ["Trailing space" gnus-article-strip-trailing-space t]
1811                ["Leading space in headers" 
1812                 gnus-article-remove-leading-whitespace t])
1813               ["Overstrike" gnus-article-treat-overstrike t]
1814               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1815               ["Emphasis" gnus-article-emphasize t]
1816               ["Word wrap" gnus-article-fill-cited-article t]
1817               ["Fill long lines" gnus-article-fill-long-lines t]
1818               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1819               ["CR" gnus-article-remove-cr t]
1820               ["Show X-Face" gnus-article-display-x-face t]
1821               ["Rot 13" gnus-summary-caesar-message
1822                ,@(if (featurep 'xemacs) '(t)
1823                    '(:help "\"Caesar rotate\" article by 13"))]
1824               ["Unix pipe" gnus-summary-pipe-message t]
1825               ["Add buttons" gnus-article-add-buttons t]
1826               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1827               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1828               ["Toggle MIME" gnus-summary-toggle-mime t]
1829               ["Verbose header" gnus-summary-verbose-headers t]
1830               ["Toggle header" gnus-summary-toggle-header t]
1831               ["Toggle smileys" gnus-smiley-display t]
1832               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1833               ["HZ" gnus-article-decode-HZ t])
1834              ("Output"
1835               ["Save in default format" gnus-summary-save-article
1836                ,@(if (featurep 'xemacs) '(t)
1837                    '(:help "Save article using default method"))]
1838               ["Save in file" gnus-summary-save-article-file
1839                ,@(if (featurep 'xemacs) '(t)
1840                    '(:help "Save article in file"))]
1841               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1842               ["Save in MH folder" gnus-summary-save-article-folder t]
1843               ["Save in VM folder" gnus-summary-save-article-vm t]
1844               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1845               ["Save body in file" gnus-summary-save-article-body-file t]
1846               ["Pipe through a filter" gnus-summary-pipe-output t]
1847               ["Add to SOUP packet" gnus-soup-add-article t]
1848               ["Print" gnus-summary-print-article t])
1849              ("Backend"
1850               ["Respool article..." gnus-summary-respool-article t]
1851               ["Move article..." gnus-summary-move-article
1852                (gnus-check-backend-function
1853                 'request-move-article gnus-newsgroup-name)]
1854               ["Copy article..." gnus-summary-copy-article t]
1855               ["Crosspost article..." gnus-summary-crosspost-article
1856                (gnus-check-backend-function
1857                 'request-replace-article gnus-newsgroup-name)]
1858               ["Import file..." gnus-summary-import-article t]
1859               ["Create article..." gnus-summary-create-article t]
1860               ["Check if posted" gnus-summary-article-posted-p t]
1861               ["Edit article" gnus-summary-edit-article
1862                (not (gnus-group-read-only-p))]
1863               ["Delete article" gnus-summary-delete-article
1864                (gnus-check-backend-function
1865                 'request-expire-articles gnus-newsgroup-name)]
1866               ["Query respool" gnus-summary-respool-query t]
1867               ["Trace respool" gnus-summary-respool-trace t]
1868               ["Delete expirable articles" gnus-summary-expire-articles-now
1869                (gnus-check-backend-function
1870                 'request-expire-articles gnus-newsgroup-name)])
1871              ("Extract"
1872               ["Uudecode" gnus-uu-decode-uu
1873                ,@(if (featurep 'xemacs) '(t)
1874                    '(:help "Decode uuencoded article(s)"))]
1875               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1876               ["Unshar" gnus-uu-decode-unshar t]
1877               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1878               ["Save" gnus-uu-decode-save t]
1879               ["Binhex" gnus-uu-decode-binhex t]
1880               ["Postscript" gnus-uu-decode-postscript t])
1881              ("Cache"
1882               ["Enter article" gnus-cache-enter-article t]
1883               ["Remove article" gnus-cache-remove-article t])
1884              ["Translate" gnus-article-babel t]
1885              ["Select article buffer" gnus-summary-select-article-buffer t]
1886              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1887              ["Isearch article..." gnus-summary-isearch-article t]
1888              ["Beginning of the article" gnus-summary-beginning-of-article t]
1889              ["End of the article" gnus-summary-end-of-article t]
1890              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1891              ["Fetch referenced articles" gnus-summary-refer-references t]
1892              ["Fetch current thread" gnus-summary-refer-thread t]
1893              ["Fetch article with id..." gnus-summary-refer-article t]
1894              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1895              ["Redisplay" gnus-summary-show-article t])))
1896       (easy-menu-define
1897        gnus-summary-article-menu gnus-summary-mode-map ""
1898        (cons "Article" innards))
1899
1900       (if (not (keymapp gnus-summary-article-menu))
1901           (easy-menu-define
1902             gnus-article-commands-menu gnus-article-mode-map ""
1903             (cons "Commands" innards))
1904         ;; in Emacs, don't share menu.
1905         (setq gnus-article-commands-menu 
1906               (copy-keymap gnus-summary-article-menu))
1907         (define-key gnus-article-mode-map [menu-bar commands]
1908           (cons "Commands" gnus-article-commands-menu))))
1909
1910     (easy-menu-define
1911      gnus-summary-thread-menu gnus-summary-mode-map ""
1912      '("Threads"
1913        ["Toggle threading" gnus-summary-toggle-threads t]
1914        ["Hide threads" gnus-summary-hide-all-threads t]
1915        ["Show threads" gnus-summary-show-all-threads t]
1916        ["Hide thread" gnus-summary-hide-thread t]
1917        ["Show thread" gnus-summary-show-thread t]
1918        ["Go to next thread" gnus-summary-next-thread t]
1919        ["Go to previous thread" gnus-summary-prev-thread t]
1920        ["Go down thread" gnus-summary-down-thread t]
1921        ["Go up thread" gnus-summary-up-thread t]
1922        ["Top of thread" gnus-summary-top-thread t]
1923        ["Mark thread as read" gnus-summary-kill-thread t]
1924        ["Lower thread score" gnus-summary-lower-thread t]
1925        ["Raise thread score" gnus-summary-raise-thread t]
1926        ["Rethread current" gnus-summary-rethread-current t]))
1927
1928     (easy-menu-define
1929      gnus-summary-post-menu gnus-summary-mode-map ""
1930      `("Post"
1931        ["Post an article" gnus-summary-post-news
1932         ,@(if (featurep 'xemacs) '(t)
1933             '(:help "Post an article"))]
1934        ["Followup" gnus-summary-followup
1935         ,@(if (featurep 'xemacs) '(t)
1936             '(:help "Post followup to this article"))]
1937        ["Followup and yank" gnus-summary-followup-with-original
1938         ,@(if (featurep 'xemacs) '(t)
1939             '(:help "Post followup to this article, quoting its contents"))]
1940        ["Supersede article" gnus-summary-supersede-article t]
1941        ["Cancel article" gnus-summary-cancel-article
1942         ,@(if (featurep 'xemacs) '(t)
1943             '(:help "Cancel an article you posted"))]
1944        ["Reply" gnus-summary-reply t]
1945        ["Reply and yank" gnus-summary-reply-with-original t]
1946        ["Wide reply" gnus-summary-wide-reply t]
1947        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1948         ,@(if (featurep 'xemacs) '(t)
1949             '(:help "Mail a reply, quoting this article"))]
1950        ["Mail forward" gnus-summary-mail-forward t]
1951        ["Post forward" gnus-summary-post-forward t]
1952        ["Digest and mail" gnus-summary-digest-mail-forward t]
1953        ["Digest and post" gnus-summary-digest-post-forward t]
1954        ["Resend message" gnus-summary-resend-message t]
1955        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1956        ["Send a mail" gnus-summary-mail-other-window t]
1957        ["Uuencode and post" gnus-uu-post-news
1958         ,@(if (featurep 'xemacs) '(t)
1959             '(:help "Post a uuencoded article"))]
1960        ["Followup via news" gnus-summary-followup-to-mail t]
1961        ["Followup via news and yank"
1962         gnus-summary-followup-to-mail-with-original t]
1963        ;;("Draft"
1964        ;;["Send" gnus-summary-send-draft t]
1965        ;;["Send bounced" gnus-resend-bounced-mail t])
1966        ))
1967
1968     (cond 
1969      ((not (keymapp gnus-summary-post-menu))
1970       (setq gnus-article-post-menu gnus-summary-post-menu))
1971      ((not gnus-article-post-menu)
1972       ;; Don't share post menu.
1973       (setq gnus-article-post-menu
1974             (copy-keymap gnus-summary-post-menu))))
1975     (define-key gnus-article-mode-map [menu-bar post]
1976       (cons "Post" gnus-article-post-menu))
1977
1978     (easy-menu-define
1979      gnus-summary-misc-menu gnus-summary-mode-map ""
1980      `("Misc"
1981        ("Mark Read"
1982         ["Mark as read" gnus-summary-mark-as-read-forward t]
1983         ["Mark same subject and select"
1984          gnus-summary-kill-same-subject-and-select t]
1985         ["Mark same subject" gnus-summary-kill-same-subject t]
1986         ["Catchup" gnus-summary-catchup
1987          ,@(if (featurep 'xemacs) '(t)
1988              '(:help "Mark unread articles in this group as read"))]
1989         ["Catchup all" gnus-summary-catchup-all t]
1990         ["Catchup to here" gnus-summary-catchup-to-here t]
1991         ["Catchup region" gnus-summary-mark-region-as-read t]
1992         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1993        ("Mark Various"
1994         ["Tick" gnus-summary-tick-article-forward t]
1995         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1996         ["Remove marks" gnus-summary-clear-mark-forward t]
1997         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1998         ["Set bookmark" gnus-summary-set-bookmark t]
1999         ["Remove bookmark" gnus-summary-remove-bookmark t])
2000        ("Mark Limit"
2001         ["Marks..." gnus-summary-limit-to-marks t]
2002         ["Subject..." gnus-summary-limit-to-subject t]
2003         ["Author..." gnus-summary-limit-to-author t]
2004         ["Age..." gnus-summary-limit-to-age t]
2005         ["Extra..." gnus-summary-limit-to-extra t]
2006         ["Score" gnus-summary-limit-to-score t]
2007         ["Unread" gnus-summary-limit-to-unread t]
2008         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2009         ["Articles" gnus-summary-limit-to-articles t]
2010         ["Pop limit" gnus-summary-pop-limit t]
2011         ["Show dormant" gnus-summary-limit-include-dormant t]
2012         ["Hide childless dormant"
2013          gnus-summary-limit-exclude-childless-dormant t]
2014         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2015         ["Hide marked" gnus-summary-limit-exclude-marks t]
2016         ["Show expunged" gnus-summary-limit-include-expunged t])
2017        ("Process Mark"
2018         ["Set mark" gnus-summary-mark-as-processable t]
2019         ["Remove mark" gnus-summary-unmark-as-processable t]
2020         ["Remove all marks" gnus-summary-unmark-all-processable t]
2021         ["Mark above" gnus-uu-mark-over t]
2022         ["Mark series" gnus-uu-mark-series t]
2023         ["Mark region" gnus-uu-mark-region t]
2024         ["Unmark region" gnus-uu-unmark-region t]
2025         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2026         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2027         ["Mark all" gnus-uu-mark-all t]
2028         ["Mark buffer" gnus-uu-mark-buffer t]
2029         ["Mark sparse" gnus-uu-mark-sparse t]
2030         ["Mark thread" gnus-uu-mark-thread t]
2031         ["Unmark thread" gnus-uu-unmark-thread t]
2032         ("Process Mark Sets"
2033          ["Kill" gnus-summary-kill-process-mark t]
2034          ["Yank" gnus-summary-yank-process-mark
2035           gnus-newsgroup-process-stack]
2036          ["Save" gnus-summary-save-process-mark t]))
2037        ("Scroll article"
2038         ["Page forward" gnus-summary-next-page
2039          ,@(if (featurep 'xemacs) '(t)
2040              '(:help "Show next page of article"))]
2041         ["Page backward" gnus-summary-prev-page
2042          ,@(if (featurep 'xemacs) '(t)
2043              '(:help "Show previous page of article"))]
2044         ["Line forward" gnus-summary-scroll-up t])
2045        ("Move"
2046         ["Next unread article" gnus-summary-next-unread-article t]
2047         ["Previous unread article" gnus-summary-prev-unread-article t]
2048         ["Next article" gnus-summary-next-article t]
2049         ["Previous article" gnus-summary-prev-article t]
2050         ["Next unread subject" gnus-summary-next-unread-subject t]
2051         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2052         ["Next article same subject" gnus-summary-next-same-subject t]
2053         ["Previous article same subject" gnus-summary-prev-same-subject t]
2054         ["First unread article" gnus-summary-first-unread-article t]
2055         ["Best unread article" gnus-summary-best-unread-article t]
2056         ["Go to subject number..." gnus-summary-goto-subject t]
2057         ["Go to article number..." gnus-summary-goto-article t]
2058         ["Go to the last article" gnus-summary-goto-last-article t]
2059         ["Pop article off history" gnus-summary-pop-article t])
2060        ("Sort"
2061         ["Sort by number" gnus-summary-sort-by-number t]
2062         ["Sort by author" gnus-summary-sort-by-author t]
2063         ["Sort by subject" gnus-summary-sort-by-subject t]
2064         ["Sort by date" gnus-summary-sort-by-date t]
2065         ["Sort by score" gnus-summary-sort-by-score t]
2066         ["Sort by lines" gnus-summary-sort-by-lines t]
2067         ["Sort by characters" gnus-summary-sort-by-chars t])
2068        ("Help"
2069         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2070         ["Describe group" gnus-summary-describe-group t]
2071         ["Read manual" gnus-info-find-node t])
2072        ("Modes"
2073         ["Pick and read" gnus-pick-mode t]
2074         ["Binary" gnus-binary-mode t])
2075        ("Regeneration"
2076         ["Regenerate" gnus-summary-prepare t]
2077         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2078         ["Toggle threading" gnus-summary-toggle-threads t])
2079        ["See old articles" gnus-summary-insert-old-articles t]
2080        ["See new articles" gnus-summary-insert-new-articles t]
2081        ["Filter articles..." gnus-summary-execute-command t]
2082        ["Run command on subjects..." gnus-summary-universal-argument t]
2083        ["Search articles forward..." gnus-summary-search-article-forward t]
2084        ["Search articles backward..." gnus-summary-search-article-backward t]
2085        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2086        ["Expand window" gnus-summary-expand-window t]
2087        ["Expire expirable articles" gnus-summary-expire-articles
2088         (gnus-check-backend-function
2089          'request-expire-articles gnus-newsgroup-name)]
2090        ["Edit local kill file" gnus-summary-edit-local-kill t]
2091        ["Edit main kill file" gnus-summary-edit-global-kill t]
2092        ["Edit group parameters" gnus-summary-edit-parameters t]
2093        ["Customize group parameters" gnus-summary-customize-parameters t]
2094        ["Send a bug report" gnus-bug t]
2095        ("Exit"
2096         ["Catchup and exit" gnus-summary-catchup-and-exit
2097          ,@(if (featurep 'xemacs) '(t)
2098              '(:help "Mark unread articles in this group as read, then exit"))]
2099         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2100         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2101         ["Exit group" gnus-summary-exit
2102          ,@(if (featurep 'xemacs) '(t)
2103              '(:help "Exit current group, return to group selection mode"))]
2104         ["Exit group without updating" gnus-summary-exit-no-update t]
2105         ["Exit and goto next group" gnus-summary-next-group t]
2106         ["Exit and goto prev group" gnus-summary-prev-group t]
2107         ["Reselect group" gnus-summary-reselect-current-group t]
2108         ["Rescan group" gnus-summary-rescan-group t]
2109         ["Update dribble" gnus-summary-save-newsrc t])))
2110
2111     (gnus-run-hooks 'gnus-summary-menu-hook)))
2112
2113 (defvar gnus-summary-tool-bar-map nil)
2114
2115 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2116 (defun gnus-summary-make-tool-bar ()
2117   (if (and (fboundp 'tool-bar-add-item-from-menu)
2118            (default-value 'tool-bar-mode)
2119            (not gnus-summary-tool-bar-map))
2120       (setq gnus-summary-tool-bar-map
2121             (let ((tool-bar-map (make-sparse-keymap))
2122                   (load-path (mm-image-load-path)))
2123               (tool-bar-add-item-from-menu
2124                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2125               (tool-bar-add-item-from-menu
2126                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2127               (tool-bar-add-item-from-menu
2128                'gnus-summary-post-news "post" gnus-summary-mode-map)
2129               (tool-bar-add-item-from-menu
2130                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2131               (tool-bar-add-item-from-menu
2132                'gnus-summary-followup "followup" gnus-summary-mode-map)
2133               (tool-bar-add-item-from-menu
2134                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2135               (tool-bar-add-item-from-menu
2136                'gnus-summary-reply "reply" gnus-summary-mode-map)
2137               (tool-bar-add-item-from-menu
2138                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2139               (tool-bar-add-item-from-menu
2140                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2141               (tool-bar-add-item-from-menu
2142                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2143               (tool-bar-add-item-from-menu
2144                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2145               (tool-bar-add-item-from-menu
2146                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2147               (tool-bar-add-item-from-menu
2148                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2149               (tool-bar-add-item-from-menu
2150                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2151               (tool-bar-add-item-from-menu
2152                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2153               tool-bar-map)))
2154   (if gnus-summary-tool-bar-map
2155       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2156
2157 (defun gnus-score-set-default (var value)
2158   "A version of set that updates the GNU Emacs menu-bar."
2159   (set var value)
2160   ;; It is the message that forces the active status to be updated.
2161   (message ""))
2162
2163 (defun gnus-make-score-map (type)
2164   "Make a summary score map of type TYPE."
2165   (if t
2166       nil
2167     (let ((headers '(("author" "from" string)
2168                      ("subject" "subject" string)
2169                      ("article body" "body" string)
2170                      ("article head" "head" string)
2171                      ("xref" "xref" string)
2172                      ("extra header" "extra" string)
2173                      ("lines" "lines" number)
2174                      ("followups to author" "followup" string)))
2175           (types '((number ("less than" <)
2176                            ("greater than" >)
2177                            ("equal" =))
2178                    (string ("substring" s)
2179                            ("exact string" e)
2180                            ("fuzzy string" f)
2181                            ("regexp" r))))
2182           (perms '(("temporary" (current-time-string))
2183                    ("permanent" nil)
2184                    ("immediate" now)))
2185           header)
2186       (list
2187        (apply
2188         'nconc
2189         (list
2190          (if (eq type 'lower)
2191              "Lower score"
2192            "Increase score"))
2193         (let (outh)
2194           (while headers
2195             (setq header (car headers))
2196             (setq outh
2197                   (cons
2198                    (apply
2199                     'nconc
2200                     (list (car header))
2201                     (let ((ts (cdr (assoc (nth 2 header) types)))
2202                           outt)
2203                       (while ts
2204                         (setq outt
2205                               (cons
2206                                (apply
2207                                 'nconc
2208                                 (list (caar ts))
2209                                 (let ((ps perms)
2210                                       outp)
2211                                   (while ps
2212                                     (setq outp
2213                                           (cons
2214                                            (vector
2215                                             (caar ps)
2216                                             (list
2217                                              'gnus-summary-score-entry
2218                                              (nth 1 header)
2219                                              (if (or (string= (nth 1 header)
2220                                                               "head")
2221                                                      (string= (nth 1 header)
2222                                                               "body"))
2223                                                  ""
2224                                                (list 'gnus-summary-header
2225                                                      (nth 1 header)))
2226                                              (list 'quote (nth 1 (car ts)))
2227                                              (list 'gnus-score-delta-default
2228                                                    nil)
2229                                              (nth 1 (car ps))
2230                                              t)
2231                                             t)
2232                                            outp))
2233                                     (setq ps (cdr ps)))
2234                                   (list (nreverse outp))))
2235                                outt))
2236                         (setq ts (cdr ts)))
2237                       (list (nreverse outt))))
2238                    outh))
2239             (setq headers (cdr headers)))
2240           (list (nreverse outh))))))))
2241
2242 \f
2243
2244 (defun gnus-summary-mode (&optional group)
2245   "Major mode for reading articles.
2246
2247 All normal editing commands are switched off.
2248 \\<gnus-summary-mode-map>
2249 Each line in this buffer represents one article.  To read an
2250 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2251 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2252 respectively.
2253
2254 You can also post articles and send mail from this buffer.  To
2255 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2256 of an article, type `\\[gnus-summary-reply]'.
2257
2258 There are approx. one gazillion commands you can execute in this
2259 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2260
2261 The following commands are available:
2262
2263 \\{gnus-summary-mode-map}"
2264   (interactive)
2265   (kill-all-local-variables)
2266   (when (gnus-visual-p 'summary-menu 'menu)
2267     (gnus-summary-make-menu-bar)
2268     (gnus-summary-make-tool-bar))
2269   (gnus-summary-make-local-variables)
2270   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2271     (gnus-summary-make-local-variables))
2272   (gnus-make-thread-indent-array)
2273   (gnus-simplify-mode-line)
2274   (setq major-mode 'gnus-summary-mode)
2275   (setq mode-name "Summary")
2276   (make-local-variable 'minor-mode-alist)
2277   (use-local-map gnus-summary-mode-map)
2278   (buffer-disable-undo)
2279   (setq buffer-read-only t)             ;Disable modification
2280   (setq truncate-lines t)
2281   (setq selective-display t)
2282   (setq selective-display-ellipses t)   ;Display `...'
2283   (gnus-summary-set-display-table)
2284   (gnus-set-default-directory)
2285   (setq gnus-newsgroup-name group)
2286   (unless (gnus-news-group-p group)
2287     (setq gnus-newsgroup-incorporated
2288           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2289   (make-local-variable 'gnus-summary-line-format)
2290   (make-local-variable 'gnus-summary-line-format-spec)
2291   (make-local-variable 'gnus-summary-dummy-line-format)
2292   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2293   (make-local-variable 'gnus-summary-mark-positions)
2294   (make-local-hook 'pre-command-hook)
2295   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2296   (gnus-run-hooks 'gnus-summary-mode-hook)
2297   (turn-on-gnus-mailing-list-mode)
2298   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2299   (gnus-update-summary-mark-positions))
2300
2301 (defun gnus-summary-make-local-variables ()
2302   "Make all the local summary buffer variables."
2303   (let (global)
2304     (dolist (local gnus-summary-local-variables)
2305       (if (consp local)
2306           (progn
2307             (if (eq (cdr local) 'global)
2308                 ;; Copy the global value of the variable.
2309                 (setq global (symbol-value (car local)))
2310               ;; Use the value from the list.
2311               (setq global (eval (cdr local))))
2312             (set (make-local-variable (car local)) global))
2313         ;; Simple nil-valued local variable.
2314         (set (make-local-variable local) nil)))))
2315
2316 (defun gnus-summary-clear-local-variables ()
2317   (let ((locals gnus-summary-local-variables))
2318     (while locals
2319       (if (consp (car locals))
2320           (and (vectorp (caar locals))
2321                (set (caar locals) nil))
2322         (and (vectorp (car locals))
2323              (set (car locals) nil)))
2324       (setq locals (cdr locals)))))
2325
2326 ;; Summary data functions.
2327
2328 (defmacro gnus-data-number (data)
2329   `(car ,data))
2330
2331 (defmacro gnus-data-set-number (data number)
2332   `(setcar ,data ,number))
2333
2334 (defmacro gnus-data-mark (data)
2335   `(nth 1 ,data))
2336
2337 (defmacro gnus-data-set-mark (data mark)
2338   `(setcar (nthcdr 1 ,data) ,mark))
2339
2340 (defmacro gnus-data-pos (data)
2341   `(nth 2 ,data))
2342
2343 (defmacro gnus-data-set-pos (data pos)
2344   `(setcar (nthcdr 2 ,data) ,pos))
2345
2346 (defmacro gnus-data-header (data)
2347   `(nth 3 ,data))
2348
2349 (defmacro gnus-data-set-header (data header)
2350   `(setcar (nthcdr 3 ,data) ,header))
2351
2352 (defmacro gnus-data-level (data)
2353   `(nth 4 ,data))
2354
2355 (defmacro gnus-data-unread-p (data)
2356   `(= (nth 1 ,data) gnus-unread-mark))
2357
2358 (defmacro gnus-data-read-p (data)
2359   `(/= (nth 1 ,data) gnus-unread-mark))
2360
2361 (defmacro gnus-data-pseudo-p (data)
2362   `(consp (nth 3 ,data)))
2363
2364 (defmacro gnus-data-find (number)
2365   `(assq ,number gnus-newsgroup-data))
2366
2367 (defmacro gnus-data-find-list (number &optional data)
2368   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2369      (memq (assq ,number bdata)
2370            bdata)))
2371
2372 (defmacro gnus-data-make (number mark pos header level)
2373   `(list ,number ,mark ,pos ,header ,level))
2374
2375 (defun gnus-data-enter (after-article number mark pos header level offset)
2376   (let ((data (gnus-data-find-list after-article)))
2377     (unless data
2378       (error "No such article: %d" after-article))
2379     (setcdr data (cons (gnus-data-make number mark pos header level)
2380                        (cdr data)))
2381     (setq gnus-newsgroup-data-reverse nil)
2382     (gnus-data-update-list (cddr data) offset)))
2383
2384 (defun gnus-data-enter-list (after-article list &optional offset)
2385   (when list
2386     (let ((data (and after-article (gnus-data-find-list after-article)))
2387           (ilist list))
2388       (if (not (or data
2389                    after-article))
2390           (let ((odata gnus-newsgroup-data))
2391             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2392             (when offset
2393               (gnus-data-update-list odata offset)))
2394         ;; Find the last element in the list to be spliced into the main
2395         ;; list.
2396         (while (cdr list)
2397           (setq list (cdr list)))
2398         (if (not data)
2399             (progn
2400               (setcdr list gnus-newsgroup-data)
2401               (setq gnus-newsgroup-data ilist)
2402               (when offset
2403                 (gnus-data-update-list (cdr list) offset)))
2404           (setcdr list (cdr data))
2405           (setcdr data ilist)
2406           (when offset
2407             (gnus-data-update-list (cdr list) offset))))
2408       (setq gnus-newsgroup-data-reverse nil))))
2409
2410 (defun gnus-data-remove (article &optional offset)
2411   (let ((data gnus-newsgroup-data))
2412     (if (= (gnus-data-number (car data)) article)
2413         (progn
2414           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2415                 gnus-newsgroup-data-reverse nil)
2416           (when offset
2417             (gnus-data-update-list gnus-newsgroup-data offset)))
2418       (while (cdr data)
2419         (when (= (gnus-data-number (cadr data)) article)
2420           (setcdr data (cddr data))
2421           (when offset
2422             (gnus-data-update-list (cdr data) offset))
2423           (setq data nil
2424                 gnus-newsgroup-data-reverse nil))
2425         (setq data (cdr data))))))
2426
2427 (defmacro gnus-data-list (backward)
2428   `(if ,backward
2429        (or gnus-newsgroup-data-reverse
2430            (setq gnus-newsgroup-data-reverse
2431                  (reverse gnus-newsgroup-data)))
2432      gnus-newsgroup-data))
2433
2434 (defun gnus-data-update-list (data offset)
2435   "Add OFFSET to the POS of all data entries in DATA."
2436   (setq gnus-newsgroup-data-reverse nil)
2437   (while data
2438     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2439     (setq data (cdr data))))
2440
2441 (defun gnus-summary-article-pseudo-p (article)
2442   "Say whether this article is a pseudo article or not."
2443   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2444
2445 (defmacro gnus-summary-article-sparse-p (article)
2446   "Say whether this article is a sparse article or not."
2447   `(memq ,article gnus-newsgroup-sparse))
2448
2449 (defmacro gnus-summary-article-ancient-p (article)
2450   "Say whether this article is a sparse article or not."
2451   `(memq ,article gnus-newsgroup-ancient))
2452
2453 (defun gnus-article-parent-p (number)
2454   "Say whether this article is a parent or not."
2455   (let ((data (gnus-data-find-list number)))
2456     (and (cdr data)                     ; There has to be an article after...
2457          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2458             (gnus-data-level (nth 1 data))))))
2459
2460 (defun gnus-article-children (number)
2461   "Return a list of all children to NUMBER."
2462   (let* ((data (gnus-data-find-list number))
2463          (level (gnus-data-level (car data)))
2464          children)
2465     (setq data (cdr data))
2466     (while (and data
2467                 (= (gnus-data-level (car data)) (1+ level)))
2468       (push (gnus-data-number (car data)) children)
2469       (setq data (cdr data)))
2470     children))
2471
2472 (defmacro gnus-summary-skip-intangible ()
2473   "If the current article is intangible, then jump to a different article."
2474   '(let ((to (get-text-property (point) 'gnus-intangible)))
2475      (and to (gnus-summary-goto-subject to))))
2476
2477 (defmacro gnus-summary-article-intangible-p ()
2478   "Say whether this article is intangible or not."
2479   '(get-text-property (point) 'gnus-intangible))
2480
2481 (defun gnus-article-read-p (article)
2482   "Say whether ARTICLE is read or not."
2483   (not (or (memq article gnus-newsgroup-marked)
2484            (memq article gnus-newsgroup-unreads)
2485            (memq article gnus-newsgroup-unselected)
2486            (memq article gnus-newsgroup-dormant))))
2487
2488 ;; Some summary mode macros.
2489
2490 (defmacro gnus-summary-article-number ()
2491   "The article number of the article on the current line.
2492 If there isn's an article number here, then we return the current
2493 article number."
2494   '(progn
2495      (gnus-summary-skip-intangible)
2496      (or (get-text-property (point) 'gnus-number)
2497          (gnus-summary-last-subject))))
2498
2499 (defmacro gnus-summary-article-header (&optional number)
2500   "Return the header of article NUMBER."
2501   `(gnus-data-header (gnus-data-find
2502                       ,(or number '(gnus-summary-article-number)))))
2503
2504 (defmacro gnus-summary-thread-level (&optional number)
2505   "Return the level of thread that starts with article NUMBER."
2506   `(if (and (eq gnus-summary-make-false-root 'dummy)
2507             (get-text-property (point) 'gnus-intangible))
2508        0
2509      (gnus-data-level (gnus-data-find
2510                        ,(or number '(gnus-summary-article-number))))))
2511
2512 (defmacro gnus-summary-article-mark (&optional number)
2513   "Return the mark of article NUMBER."
2514   `(gnus-data-mark (gnus-data-find
2515                     ,(or number '(gnus-summary-article-number)))))
2516
2517 (defmacro gnus-summary-article-pos (&optional number)
2518   "Return the position of the line of article NUMBER."
2519   `(gnus-data-pos (gnus-data-find
2520                    ,(or number '(gnus-summary-article-number)))))
2521
2522 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2523 (defmacro gnus-summary-article-subject (&optional number)
2524   "Return current subject string or nil if nothing."
2525   `(let ((headers
2526           ,(if number
2527                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2528              '(gnus-data-header (assq (gnus-summary-article-number)
2529                                       gnus-newsgroup-data)))))
2530      (and headers
2531           (vectorp headers)
2532           (mail-header-subject headers))))
2533
2534 (defmacro gnus-summary-article-score (&optional number)
2535   "Return current article score."
2536   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2537                   gnus-newsgroup-scored))
2538        gnus-summary-default-score 0))
2539
2540 (defun gnus-summary-article-children (&optional number)
2541   "Return a list of article numbers that are children of article NUMBER."
2542   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2543          (level (gnus-data-level (car data)))
2544          l children)
2545     (while (and (setq data (cdr data))
2546                 (> (setq l (gnus-data-level (car data))) level))
2547       (and (= (1+ level) l)
2548            (push (gnus-data-number (car data))
2549                  children)))
2550     (nreverse children)))
2551
2552 (defun gnus-summary-article-parent (&optional number)
2553   "Return the article number of the parent of article NUMBER."
2554   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2555                                     (gnus-data-list t)))
2556          (level (gnus-data-level (car data))))
2557     (if (zerop level)
2558         ()                              ; This is a root.
2559       ;; We search until we find an article with a level less than
2560       ;; this one.  That function has to be the parent.
2561       (while (and (setq data (cdr data))
2562                   (not (< (gnus-data-level (car data)) level))))
2563       (and data (gnus-data-number (car data))))))
2564
2565 (defun gnus-unread-mark-p (mark)
2566   "Say whether MARK is the unread mark."
2567   (= mark gnus-unread-mark))
2568
2569 (defun gnus-read-mark-p (mark)
2570   "Say whether MARK is one of the marks that mark as read.
2571 This is all marks except unread, ticked, dormant, and expirable."
2572   (not (or (= mark gnus-unread-mark)
2573            (= mark gnus-ticked-mark)
2574            (= mark gnus-dormant-mark)
2575            (= mark gnus-expirable-mark))))
2576
2577 (defmacro gnus-article-mark (number)
2578   "Return the MARK of article NUMBER.
2579 This macro should only be used when computing the mark the \"first\"
2580 time; i.e., when generating the summary lines.  After that,
2581 `gnus-summary-article-mark' should be used to examine the
2582 marks of articles."
2583   `(cond
2584     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2585     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2586     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2587     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2588     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2589     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2590     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2591     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2592            gnus-ancient-mark))))
2593
2594 ;; Saving hidden threads.
2595
2596 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2597 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2598
2599 (defmacro gnus-save-hidden-threads (&rest forms)
2600   "Save hidden threads, eval FORMS, and restore the hidden threads."
2601   (let ((config (make-symbol "config")))
2602     `(let ((,config (gnus-hidden-threads-configuration)))
2603        (unwind-protect
2604            (save-excursion
2605              ,@forms)
2606          (gnus-restore-hidden-threads-configuration ,config)))))
2607
2608 (defun gnus-data-compute-positions ()
2609   "Compute the positions of all articles."
2610   (setq gnus-newsgroup-data-reverse nil)
2611   (let ((data gnus-newsgroup-data))
2612     (save-excursion
2613       (gnus-save-hidden-threads
2614         (gnus-summary-show-all-threads)
2615         (goto-char (point-min))
2616         (while data
2617           (while (get-text-property (point) 'gnus-intangible)
2618             (forward-line 1))
2619           (gnus-data-set-pos (car data) (+ (point) 3))
2620           (setq data (cdr data))
2621           (forward-line 1))))))
2622
2623 (defun gnus-hidden-threads-configuration ()
2624   "Return the current hidden threads configuration."
2625   (save-excursion
2626     (let (config)
2627       (goto-char (point-min))
2628       (while (search-forward "\r" nil t)
2629         (push (1- (point)) config))
2630       config)))
2631
2632 (defun gnus-restore-hidden-threads-configuration (config)
2633   "Restore hidden threads configuration from CONFIG."
2634   (save-excursion
2635     (let (point buffer-read-only)
2636       (while (setq point (pop config))
2637         (when (and (< point (point-max))
2638                    (goto-char point)
2639                    (eq (char-after) ?\n))
2640           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2641
2642 ;; Various summary mode internalish functions.
2643
2644 (defun gnus-mouse-pick-article (e)
2645   (interactive "e")
2646   (mouse-set-point e)
2647   (gnus-summary-next-page nil t))
2648
2649 (defun gnus-summary-set-display-table ()
2650   "Change the display table.
2651 Odd characters have a tendency to mess
2652 up nicely formatted displays - we make all possible glyphs
2653 display only a single character."
2654
2655   ;; We start from the standard display table, if any.
2656   (let ((table (or (copy-sequence standard-display-table)
2657                    (make-display-table)))
2658         (i 32))
2659     ;; Nix out all the control chars...
2660     (while (>= (setq i (1- i)) 0)
2661       (aset table i [??]))
2662     ;; ... but not newline and cr, of course.  (cr is necessary for the
2663     ;; selective display).
2664     (aset table ?\n nil)
2665     (aset table ?\r nil)
2666     ;; We keep TAB as well.
2667     (aset table ?\t nil)
2668     ;; We nix out any glyphs over 126 that are not set already.
2669     (let ((i 256))
2670       (while (>= (setq i (1- i)) 127)
2671         ;; Only modify if the entry is nil.
2672         (unless (aref table i)
2673           (aset table i [??]))))
2674     (setq buffer-display-table table)))
2675
2676 (defun gnus-summary-buffer-name (group)
2677   "Return the summary buffer name of GROUP."
2678   (concat "*Summary " group "*"))
2679
2680 (defun gnus-summary-setup-buffer (group)
2681   "Initialize summary buffer."
2682   (let ((buffer (gnus-summary-buffer-name group)))
2683     (if (get-buffer buffer)
2684         (progn
2685           (set-buffer buffer)
2686           (setq gnus-summary-buffer (current-buffer))
2687           (not gnus-newsgroup-prepared))
2688       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2689       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2690       (gnus-summary-mode group)
2691       (when gnus-carpal
2692         (gnus-carpal-setup-buffer 'summary))
2693       (unless gnus-single-article-buffer
2694         (make-local-variable 'gnus-article-buffer)
2695         (make-local-variable 'gnus-article-current)
2696         (make-local-variable 'gnus-original-article-buffer))
2697       (setq gnus-newsgroup-name group)
2698       ;; Set any local variables in the group parameters.
2699       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2700       t)))
2701
2702 (defun gnus-set-global-variables ()
2703   "Set the global equivalents of the buffer-local variables.
2704 They are set to the latest values they had.  These reflect the summary
2705 buffer that was in action when the last article was fetched."
2706   (when (eq major-mode 'gnus-summary-mode)
2707     (setq gnus-summary-buffer (current-buffer))
2708     (let ((name gnus-newsgroup-name)
2709           (marked gnus-newsgroup-marked)
2710           (unread gnus-newsgroup-unreads)
2711           (headers gnus-current-headers)
2712           (data gnus-newsgroup-data)
2713           (summary gnus-summary-buffer)
2714           (article-buffer gnus-article-buffer)
2715           (original gnus-original-article-buffer)
2716           (gac gnus-article-current)
2717           (reffed gnus-reffed-article-number)
2718           (score-file gnus-current-score-file)
2719           (default-charset gnus-newsgroup-charset)
2720           vlist)
2721       (let ((locals gnus-newsgroup-variables))
2722         (while locals
2723           (if (consp (car locals))
2724               (push (eval (caar locals)) vlist)
2725             (push (eval (car locals)) vlist))
2726           (setq locals (cdr locals)))
2727         (setq vlist (nreverse vlist)))
2728       (save-excursion
2729         (set-buffer gnus-group-buffer)
2730         (setq gnus-newsgroup-name name
2731               gnus-newsgroup-marked marked
2732               gnus-newsgroup-unreads unread
2733               gnus-current-headers headers
2734               gnus-newsgroup-data data
2735               gnus-article-current gac
2736               gnus-summary-buffer summary
2737               gnus-article-buffer article-buffer
2738               gnus-original-article-buffer original
2739               gnus-reffed-article-number reffed
2740               gnus-current-score-file score-file
2741               gnus-newsgroup-charset default-charset)
2742         (let ((locals gnus-newsgroup-variables))
2743           (while locals
2744             (if (consp (car locals))
2745                 (set (caar locals) (pop vlist))
2746               (set (car locals) (pop vlist)))
2747             (setq locals (cdr locals))))
2748         ;; The article buffer also has local variables.
2749         (when (gnus-buffer-live-p gnus-article-buffer)
2750           (set-buffer gnus-article-buffer)
2751           (setq gnus-summary-buffer summary))))))
2752
2753 (defun gnus-summary-article-unread-p (article)
2754   "Say whether ARTICLE is unread or not."
2755   (memq article gnus-newsgroup-unreads))
2756
2757 (defun gnus-summary-first-article-p (&optional article)
2758   "Return whether ARTICLE is the first article in the buffer."
2759   (if (not (setq article (or article (gnus-summary-article-number))))
2760       nil
2761     (eq article (caar gnus-newsgroup-data))))
2762
2763 (defun gnus-summary-last-article-p (&optional article)
2764   "Return whether ARTICLE is the last article in the buffer."
2765   (if (not (setq article (or article (gnus-summary-article-number))))
2766       ;; All non-existent numbers are the last article.  :-)
2767       t
2768     (not (cdr (gnus-data-find-list article)))))
2769
2770 (defun gnus-make-thread-indent-array ()
2771   (let ((n 200))
2772     (unless (and gnus-thread-indent-array
2773                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2774       (setq gnus-thread-indent-array (make-vector 201 "")
2775             gnus-thread-indent-array-level gnus-thread-indent-level)
2776       (while (>= n 0)
2777         (aset gnus-thread-indent-array n
2778               (make-string (* n gnus-thread-indent-level) ? ))
2779         (setq n (1- n))))))
2780
2781 (defun gnus-update-summary-mark-positions ()
2782   "Compute where the summary marks are to go."
2783   (save-excursion
2784     (when (gnus-buffer-exists-p gnus-summary-buffer)
2785       (set-buffer gnus-summary-buffer))
2786     (let ((gnus-replied-mark 129)
2787           (gnus-score-below-mark 130)
2788           (gnus-score-over-mark 130)
2789           (gnus-download-mark 131)
2790           (spec gnus-summary-line-format-spec)
2791           gnus-visual pos)
2792       (save-excursion
2793         (gnus-set-work-buffer)
2794         (let ((gnus-summary-line-format-spec spec)
2795               (gnus-newsgroup-downloadable '((0 . t))))
2796           (gnus-summary-insert-line
2797            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2798            0 nil 128 t nil "" nil 1)
2799           (goto-char (point-min))
2800           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2801                                              (- (point) 2)))))
2802           (goto-char (point-min))
2803           (push (cons 'replied (and (search-forward "\201" nil t)
2804                                     (- (point) 2)))
2805                 pos)
2806           (goto-char (point-min))
2807           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2808                 pos)
2809           (goto-char (point-min))
2810           (push (cons 'download
2811                       (and (search-forward "\203" nil t) (- (point) 2)))
2812                 pos)))
2813       (setq gnus-summary-mark-positions pos))))
2814
2815 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2816   "Insert a dummy root in the summary buffer."
2817   (beginning-of-line)
2818   (gnus-add-text-properties
2819    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2820    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2821
2822 (defun gnus-summary-from-or-to-or-newsgroups (header)
2823   (let ((to (cdr (assq 'To (mail-header-extra header))))
2824         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2825         (default-mime-charset (with-current-buffer gnus-summary-buffer
2826                                 default-mime-charset)))
2827     (cond
2828      ((and to
2829            gnus-ignored-from-addresses
2830            (string-match gnus-ignored-from-addresses
2831                          (mail-header-from header)))
2832       (concat "-> "
2833               (or (car (funcall gnus-extract-address-components
2834                                 (funcall
2835                                  gnus-decode-encoded-word-function to)))
2836                   (funcall gnus-decode-encoded-word-function to))))
2837      ((and newsgroups
2838            gnus-ignored-from-addresses
2839            (string-match gnus-ignored-from-addresses
2840                          (mail-header-from header)))
2841       (concat "=> " newsgroups))
2842      (t
2843       (or (car (funcall gnus-extract-address-components
2844                         (mail-header-from header)))
2845           (mail-header-from header))))))
2846
2847 (defun gnus-summary-insert-line (gnus-tmp-header
2848                                  gnus-tmp-level gnus-tmp-current
2849                                  gnus-tmp-unread gnus-tmp-replied
2850                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2851                                  &optional gnus-tmp-dummy gnus-tmp-score
2852                                  gnus-tmp-process)
2853   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2854          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2855          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2856          (gnus-tmp-score-char
2857           (if (or (null gnus-summary-default-score)
2858                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2859                       gnus-summary-zcore-fuzz))
2860               ?  ;Whitespace
2861             (if (< gnus-tmp-score gnus-summary-default-score)
2862                 gnus-score-below-mark gnus-score-over-mark)))
2863          (gnus-tmp-replied
2864           (cond (gnus-tmp-process gnus-process-mark)
2865                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2866                  gnus-cached-mark)
2867                 (gnus-tmp-replied gnus-replied-mark)
2868                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2869                  gnus-saved-mark)
2870                 (t gnus-no-mark)))
2871          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2872          (gnus-tmp-name
2873           (cond
2874            ((string-match "<[^>]+> *$" gnus-tmp-from)
2875             (let ((beg (match-beginning 0)))
2876               (or (and (string-match "^\".+\"" gnus-tmp-from)
2877                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2878                   (substring gnus-tmp-from 0 beg))))
2879            ((string-match "(.+)" gnus-tmp-from)
2880             (substring gnus-tmp-from
2881                        (1+ (match-beginning 0)) (1- (match-end 0))))
2882            (t gnus-tmp-from)))
2883          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2884          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2885          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2886          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2887          (buffer-read-only nil))
2888     (when (string= gnus-tmp-name "")
2889       (setq gnus-tmp-name gnus-tmp-from))
2890     (unless (numberp gnus-tmp-lines)
2891       (setq gnus-tmp-lines 0))
2892     (gnus-put-text-property-excluding-characters-with-faces
2893      (point)
2894      (progn (eval gnus-summary-line-format-spec) (point))
2895      'gnus-number gnus-tmp-number)
2896     (when (gnus-visual-p 'summary-highlight 'highlight)
2897       (forward-line -1)
2898       (gnus-run-hooks 'gnus-summary-update-hook)
2899       (forward-line 1))))
2900
2901 (defun gnus-summary-update-line (&optional dont-update)
2902   "Update summary line after change."
2903   (when (and gnus-summary-default-score
2904              (not gnus-summary-inhibit-highlight))
2905     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2906            (article (gnus-summary-article-number))
2907            (score (gnus-summary-article-score article)))
2908       (unless dont-update
2909         (if (and gnus-summary-mark-below
2910                  (< (gnus-summary-article-score)
2911                     gnus-summary-mark-below))
2912             ;; This article has a low score, so we mark it as read.
2913             (when (memq article gnus-newsgroup-unreads)
2914               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2915           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2916             ;; This article was previously marked as read on account
2917             ;; of a low score, but now it has risen, so we mark it as
2918             ;; unread.
2919             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2920         (gnus-summary-update-mark
2921          (if (or (null gnus-summary-default-score)
2922                  (<= (abs (- score gnus-summary-default-score))
2923                      gnus-summary-zcore-fuzz))
2924              ?  ;Whitespace
2925            (if (< score gnus-summary-default-score)
2926                gnus-score-below-mark gnus-score-over-mark))
2927          'score))
2928       ;; Do visual highlighting.
2929       (when (gnus-visual-p 'summary-highlight 'highlight)
2930         (gnus-run-hooks 'gnus-summary-update-hook)))))
2931
2932 (defvar gnus-tmp-new-adopts nil)
2933
2934 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2935   "Return the number of articles in THREAD.
2936 This may be 0 in some cases -- if none of the articles in
2937 the thread are to be displayed."
2938   (let* ((number
2939           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2940           (cond
2941            ((not (listp thread))
2942             1)
2943            ((and (consp thread) (cdr thread))
2944             (apply
2945              '+ 1 (mapcar
2946                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2947            ((null thread)
2948             1)
2949            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2950             1)
2951            (t 0))))
2952     (when (and level (zerop level) gnus-tmp-new-adopts)
2953       (incf number
2954             (apply '+ (mapcar
2955                        'gnus-summary-number-of-articles-in-thread
2956                        gnus-tmp-new-adopts))))
2957     (if char
2958         (if (> number 1) gnus-not-empty-thread-mark
2959           gnus-empty-thread-mark)
2960       number)))
2961
2962 (defun gnus-summary-set-local-parameters (group)
2963   "Go through the local params of GROUP and set all variable specs in that list."
2964   (let ((params (gnus-group-find-parameter group))
2965         elem)
2966     (while params
2967       (setq elem (car params)
2968             params (cdr params))
2969       (and (consp elem)                 ; Has to be a cons.
2970            (consp (cdr elem))           ; The cdr has to be a list.
2971            (symbolp (car elem))         ; Has to be a symbol in there.
2972            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2973            (ignore-errors               ; So we set it.
2974              (make-local-variable (car elem))
2975              (set (car elem) (eval (nth 1 elem))))))))
2976
2977 (defun gnus-summary-read-group (group &optional show-all no-article
2978                                       kill-buffer no-display backward
2979                                       select-articles)
2980   "Start reading news in newsgroup GROUP.
2981 If SHOW-ALL is non-nil, already read articles are also listed.
2982 If NO-ARTICLE is non-nil, no article is selected initially.
2983 If NO-DISPLAY, don't generate a summary buffer."
2984   (let (result)
2985     (while (and group
2986                 (null (setq result
2987                             (let ((gnus-auto-select-next nil))
2988                               (or (gnus-summary-read-group-1
2989                                    group show-all no-article
2990                                    kill-buffer no-display
2991                                    select-articles)
2992                                   (setq show-all nil
2993                                         select-articles nil)))))
2994                 (eq gnus-auto-select-next 'quietly))
2995       (set-buffer gnus-group-buffer)
2996       ;; The entry function called above goes to the next
2997       ;; group automatically, so we go two groups back
2998       ;; if we are searching for the previous group.
2999       (when backward
3000         (gnus-group-prev-unread-group 2))
3001       (if (not (equal group (gnus-group-group-name)))
3002           (setq group (gnus-group-group-name))
3003         (setq group nil)))
3004     result))
3005
3006 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3007   "Directly jump to the other GROUP from summary buffer.
3008 If SHOW-ALL is non-nil, already read articles are also listed."
3009   (interactive
3010    (if (eq gnus-summary-buffer (current-buffer))
3011        (list (completing-read
3012               "Group: " gnus-active-hashtb nil t
3013               (when (and gnus-newsgroup-name
3014                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3015                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3016               'gnus-group-history)
3017              current-prefix-arg)
3018      (error "%s must be invoked from a gnus summary buffer." this-command)))
3019   (unless (or (zerop (length group))
3020               (and gnus-newsgroup-name
3021                    (string-equal gnus-newsgroup-name group)))
3022     (gnus-summary-exit)
3023     (gnus-summary-read-group group show-all
3024                              gnus-dont-select-after-jump-to-other-group)))
3025
3026 (defun gnus-summary-read-group-1 (group show-all no-article
3027                                         kill-buffer no-display
3028                                         &optional select-articles)
3029   ;; Killed foreign groups can't be entered.
3030   ;;  (when (and (not (gnus-group-native-p group))
3031   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3032   ;;    (error "Dead non-native groups can't be entered"))
3033   (gnus-message 5 "Retrieving newsgroup: %s..."
3034                 (gnus-group-decoded-name group))
3035   (let* ((new-group (gnus-summary-setup-buffer group))
3036          (quit-config (gnus-group-quit-config group))
3037          (did-select (and new-group (gnus-select-newsgroup
3038                                      group show-all select-articles))))
3039     (cond
3040      ;; This summary buffer exists already, so we just select it.
3041      ((not new-group)
3042       (gnus-set-global-variables)
3043       (when kill-buffer
3044         (gnus-kill-or-deaden-summary kill-buffer))
3045       (gnus-configure-windows 'summary 'force)
3046       (gnus-set-mode-line 'summary)
3047       (gnus-summary-position-point)
3048       (message "")
3049       t)
3050      ;; We couldn't select this group.
3051      ((null did-select)
3052       (when (and (eq major-mode 'gnus-summary-mode)
3053                  (not (equal (current-buffer) kill-buffer)))
3054         (kill-buffer (current-buffer))
3055         (if (not quit-config)
3056             (progn
3057               ;; Update the info -- marks might need to be removed,
3058               ;; for instance.
3059               (gnus-summary-update-info)
3060               (set-buffer gnus-group-buffer)
3061               (gnus-group-jump-to-group group)
3062               (gnus-group-next-unread-group 1))
3063           (gnus-handle-ephemeral-exit quit-config)))
3064       (let ((grpinfo (gnus-get-info group)))
3065         (if (null (gnus-info-read grpinfo))
3066             (gnus-message 3 "Group %s contains no messages" 
3067                           (gnus-group-decoded-name group))
3068           (gnus-message 3 "Can't select group")))
3069       nil)
3070      ;; The user did a `C-g' while prompting for number of articles,
3071      ;; so we exit this group.
3072      ((eq did-select 'quit)
3073       (and (eq major-mode 'gnus-summary-mode)
3074            (not (equal (current-buffer) kill-buffer))
3075            (kill-buffer (current-buffer)))
3076       (when kill-buffer
3077         (gnus-kill-or-deaden-summary kill-buffer))
3078       (if (not quit-config)
3079           (progn
3080             (set-buffer gnus-group-buffer)
3081             (gnus-group-jump-to-group group)
3082             (gnus-group-next-unread-group 1)
3083             (gnus-configure-windows 'group 'force))
3084         (gnus-handle-ephemeral-exit quit-config))
3085       ;; Finally signal the quit.
3086       (signal 'quit nil))
3087      ;; The group was successfully selected.
3088      (t
3089       (gnus-set-global-variables)
3090       ;; Save the active value in effect when the group was entered.
3091       (setq gnus-newsgroup-active
3092             (gnus-copy-sequence
3093              (gnus-active gnus-newsgroup-name)))
3094       ;; You can change the summary buffer in some way with this hook.
3095       (gnus-run-hooks 'gnus-select-group-hook)
3096       (gnus-update-format-specifications
3097        nil 'summary 'summary-mode 'summary-dummy)
3098       (gnus-update-summary-mark-positions)
3099       ;; Do score processing.
3100       (when gnus-use-scoring
3101         (gnus-possibly-score-headers))
3102       ;; Check whether to fill in the gaps in the threads.
3103       (when gnus-build-sparse-threads
3104         (gnus-build-sparse-threads))
3105       ;; Find the initial limit.
3106       (if gnus-show-threads
3107           (if show-all
3108               (let ((gnus-newsgroup-dormant nil))
3109                 (gnus-summary-initial-limit show-all))
3110             (gnus-summary-initial-limit show-all))
3111         ;; When untreaded, all articles are always shown.
3112         (setq gnus-newsgroup-limit
3113               (mapcar
3114                (lambda (header) (mail-header-number header))
3115                gnus-newsgroup-headers)))
3116       ;; Generate the summary buffer.
3117       (unless no-display
3118         (gnus-summary-prepare))
3119       (when gnus-use-trees
3120         (gnus-tree-open group)
3121         (setq gnus-summary-highlight-line-function
3122               'gnus-tree-highlight-article))
3123       ;; If the summary buffer is empty, but there are some low-scored
3124       ;; articles or some excluded dormants, we include these in the
3125       ;; buffer.
3126       (when (and (zerop (buffer-size))
3127                  (not no-display))
3128         (cond (gnus-newsgroup-dormant
3129                (gnus-summary-limit-include-dormant))
3130               ((and gnus-newsgroup-scored show-all)
3131                (gnus-summary-limit-include-expunged t))))
3132       ;; Function `gnus-apply-kill-file' must be called in this hook.
3133       (gnus-run-hooks 'gnus-apply-kill-hook)
3134       (if (and (zerop (buffer-size))
3135                (not no-display))
3136           (progn
3137             ;; This newsgroup is empty.
3138             (gnus-summary-catchup-and-exit nil t)
3139             (gnus-message 6 "No unread news")
3140             (when kill-buffer
3141               (gnus-kill-or-deaden-summary kill-buffer))
3142             ;; Return nil from this function.
3143             nil)
3144         ;; Hide conversation thread subtrees.  We cannot do this in
3145         ;; gnus-summary-prepare-hook since kill processing may not
3146         ;; work with hidden articles.
3147         (and gnus-show-threads
3148              gnus-thread-hide-subtree
3149              (gnus-summary-hide-all-threads))
3150         (when kill-buffer
3151           (gnus-kill-or-deaden-summary kill-buffer))
3152         ;; Show first unread article if requested.
3153         (if (and (not no-article)
3154                  (not no-display)
3155                  gnus-newsgroup-unreads
3156                  gnus-auto-select-first)
3157             (progn
3158               (gnus-configure-windows 'summary)
3159               (cond
3160                ((eq gnus-auto-select-first 'best)
3161                 (gnus-summary-best-unread-article))
3162                ((eq gnus-auto-select-first t)
3163                 (gnus-summary-first-unread-article))
3164                ((gnus-functionp gnus-auto-select-first)
3165                 (funcall gnus-auto-select-first))))
3166           ;; Don't select any articles, just move point to the first
3167           ;; article in the group.
3168           (goto-char (point-min))
3169           (gnus-summary-position-point)
3170           (gnus-configure-windows 'summary 'force)
3171           (gnus-set-mode-line 'summary))
3172         (when (get-buffer-window gnus-group-buffer t)
3173           ;; Gotta use windows, because recenter does weird stuff if
3174           ;; the current buffer ain't the displayed window.
3175           (let ((owin (selected-window)))
3176             (select-window (get-buffer-window gnus-group-buffer t))
3177             (when (gnus-group-goto-group group)
3178               (recenter))
3179             (select-window owin)))
3180         ;; Mark this buffer as "prepared".
3181         (setq gnus-newsgroup-prepared t)
3182         (gnus-run-hooks 'gnus-summary-prepared-hook)
3183         t)))))
3184
3185 (defun gnus-summary-prepare ()
3186   "Generate the summary buffer."
3187   (interactive)
3188   (let ((buffer-read-only nil))
3189     (erase-buffer)
3190     (setq gnus-newsgroup-data nil
3191           gnus-newsgroup-data-reverse nil)
3192     (gnus-run-hooks 'gnus-summary-generate-hook)
3193     ;; Generate the buffer, either with threads or without.
3194     (when gnus-newsgroup-headers
3195       (gnus-summary-prepare-threads
3196        (if gnus-show-threads
3197            (gnus-sort-gathered-threads
3198             (funcall gnus-summary-thread-gathering-function
3199                      (gnus-sort-threads
3200                       (gnus-cut-threads (gnus-make-threads)))))
3201          ;; Unthreaded display.
3202          (gnus-sort-articles gnus-newsgroup-headers))))
3203     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3204     ;; Call hooks for modifying summary buffer.
3205     (goto-char (point-min))
3206     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3207
3208 (defsubst gnus-general-simplify-subject (subject)
3209   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3210   (setq subject
3211         (cond
3212          ;; Truncate the subject.
3213          (gnus-simplify-subject-functions
3214           (gnus-map-function gnus-simplify-subject-functions subject))
3215          ((numberp gnus-summary-gather-subject-limit)
3216           (setq subject (gnus-simplify-subject-re subject))
3217           (if (> (length subject) gnus-summary-gather-subject-limit)
3218               (substring subject 0 gnus-summary-gather-subject-limit)
3219             subject))
3220          ;; Fuzzily simplify it.
3221          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3222           (gnus-simplify-subject-fuzzy subject))
3223          ;; Just remove the leading "Re:".
3224          (t
3225           (gnus-simplify-subject-re subject))))
3226
3227   (if (and gnus-summary-gather-exclude-subject
3228            (string-match gnus-summary-gather-exclude-subject subject))
3229       nil                               ; This article shouldn't be gathered
3230     subject))
3231
3232 (defun gnus-summary-simplify-subject-query ()
3233   "Query where the respool algorithm would put this article."
3234   (interactive)
3235   (gnus-summary-select-article)
3236   (message "%s"
3237            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3238
3239 (defun gnus-gather-threads-by-subject (threads)
3240   "Gather threads by looking at Subject headers."
3241   (if (not gnus-summary-make-false-root)
3242       threads
3243     (let ((hashtb (gnus-make-hashtable 1024))
3244           (prev threads)
3245           (result threads)
3246           subject hthread whole-subject)
3247       (while threads
3248         (setq subject (gnus-general-simplify-subject
3249                        (setq whole-subject (mail-header-subject
3250                                             (caar threads)))))
3251         (when subject
3252           (if (setq hthread (gnus-gethash subject hashtb))
3253               (progn
3254                 ;; We enter a dummy root into the thread, if we
3255                 ;; haven't done that already.
3256                 (unless (stringp (caar hthread))
3257                   (setcar hthread (list whole-subject (car hthread))))
3258                 ;; We add this new gathered thread to this gathered
3259                 ;; thread.
3260                 (setcdr (car hthread)
3261                         (nconc (cdar hthread) (list (car threads))))
3262                 ;; Remove it from the list of threads.
3263                 (setcdr prev (cdr threads))
3264                 (setq threads prev))
3265             ;; Enter this thread into the hash table.
3266             (gnus-sethash subject threads hashtb)))
3267         (setq prev threads)
3268         (setq threads (cdr threads)))
3269       result)))
3270
3271 (defun gnus-gather-threads-by-references (threads)
3272   "Gather threads by looking at References headers."
3273   (let ((idhashtb (gnus-make-hashtable 1024))
3274         (thhashtb (gnus-make-hashtable 1024))
3275         (prev threads)
3276         (result threads)
3277         ids references id gthread gid entered ref)
3278     (while threads
3279       (when (setq references (mail-header-references (caar threads)))
3280         (setq id (mail-header-id (caar threads))
3281               ids (gnus-split-references references)
3282               entered nil)
3283         (while (setq ref (pop ids))
3284           (setq ids (delete ref ids))
3285           (if (not (setq gid (gnus-gethash ref idhashtb)))
3286               (progn
3287                 (gnus-sethash ref id idhashtb)
3288                 (gnus-sethash id threads thhashtb))
3289             (setq gthread (gnus-gethash gid thhashtb))
3290             (unless entered
3291               ;; We enter a dummy root into the thread, if we
3292               ;; haven't done that already.
3293               (unless (stringp (caar gthread))
3294                 (setcar gthread (list (mail-header-subject (caar gthread))
3295                                       (car gthread))))
3296               ;; We add this new gathered thread to this gathered
3297               ;; thread.
3298               (setcdr (car gthread)
3299                       (nconc (cdar gthread) (list (car threads)))))
3300             ;; Add it into the thread hash table.
3301             (gnus-sethash id gthread thhashtb)
3302             (setq entered t)
3303             ;; Remove it from the list of threads.
3304             (setcdr prev (cdr threads))
3305             (setq threads prev))))
3306       (setq prev threads)
3307       (setq threads (cdr threads)))
3308     result))
3309
3310 (defun gnus-sort-gathered-threads (threads)
3311   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3312   (let ((result threads))
3313     (while threads
3314       (when (stringp (caar threads))
3315         (setcdr (car threads)
3316                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3317       (setq threads (cdr threads)))
3318     result))
3319
3320 (defun gnus-thread-loop-p (root thread)
3321   "Say whether ROOT is in THREAD."
3322   (let ((stack (list thread))
3323         (infloop 0)
3324         th)
3325     (while (setq thread (pop stack))
3326       (setq th (cdr thread))
3327       (while (and th
3328                   (not (eq (caar th) root)))
3329         (pop th))
3330       (if th
3331           ;; We have found a loop.
3332           (let (ref-dep)
3333             (setcdr thread (delq (car th) (cdr thread)))
3334             (if (boundp (setq ref-dep (intern "none"
3335                                               gnus-newsgroup-dependencies)))
3336                 (setcdr (symbol-value ref-dep)
3337                         (nconc (cdr (symbol-value ref-dep))
3338                                (list (car th))))
3339               (set ref-dep (list nil (car th))))
3340             (setq infloop 1
3341                   stack nil))
3342         ;; Push all the subthreads onto the stack.
3343         (push (cdr thread) stack)))
3344     infloop))
3345
3346 (defun gnus-make-threads ()
3347   "Go through the dependency hashtb and find the roots.  Return all threads."
3348   (let (threads)
3349     (while (catch 'infloop
3350              (mapatoms
3351               (lambda (refs)
3352                 ;; Deal with self-referencing References loops.
3353                 (when (and (car (symbol-value refs))
3354                            (not (zerop
3355                                  (apply
3356                                   '+
3357                                   (mapcar
3358                                    (lambda (thread)
3359                                      (gnus-thread-loop-p
3360                                       (car (symbol-value refs)) thread))
3361                                    (cdr (symbol-value refs)))))))
3362                   (setq threads nil)
3363                   (throw 'infloop t))
3364                 (unless (car (symbol-value refs))
3365                   ;; These threads do not refer back to any other articles,
3366                   ;; so they're roots.
3367                   (setq threads (append (cdr (symbol-value refs)) threads))))
3368               gnus-newsgroup-dependencies)))
3369     threads))
3370
3371 ;; Build the thread tree.
3372 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3373   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3374
3375 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3376 if it was already present.
3377
3378 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3379 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3380 Message-IDs will be renamed be renamed to a unique Message-ID before
3381 being entered.
3382
3383 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3384   (let* ((id (mail-header-id header))
3385          (id-dep (and id (intern id dependencies)))
3386          ref ref-dep ref-header)
3387     ;; Enter this `header' in the `dependencies' table.
3388     (cond
3389      ((not id-dep)
3390       (setq header nil))
3391      ;; The first two cases do the normal part: enter a new `header'
3392      ;; in the `dependencies' table.
3393      ((not (boundp id-dep))
3394       (set id-dep (list header)))
3395      ((null (car (symbol-value id-dep)))
3396       (setcar (symbol-value id-dep) header))
3397
3398      ;; From here the `header' was already present in the
3399      ;; `dependencies' table.
3400      (force-new
3401       ;; Overrides an existing entry;
3402       ;; just set the header part of the entry.
3403       (setcar (symbol-value id-dep) header))
3404
3405      ;; Renames the existing `header' to a unique Message-ID.
3406      ((not gnus-summary-ignore-duplicates)
3407       ;; An article with this Message-ID has already been seen.
3408       ;; We rename the Message-ID.
3409       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3410            (list header))
3411       (mail-header-set-id header id))
3412
3413      ;; The last case ignores an existing entry, except it adds any
3414      ;; additional Xrefs (in case the two articles came from different
3415      ;; servers.
3416      ;; Also sets `header' to `nil' meaning that the `dependencies'
3417      ;; table was *not* modified.
3418      (t
3419       (mail-header-set-xref
3420        (car (symbol-value id-dep))
3421        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3422                    "")
3423                (or (mail-header-xref header) "")))
3424       (setq header nil)))
3425
3426     (when header
3427       ;; First check if that we are not creating a References loop.
3428       (setq ref (gnus-parent-id (mail-header-references header)))
3429       (while (and ref
3430                   (setq ref-dep (intern-soft ref dependencies))
3431                   (boundp ref-dep)
3432                   (setq ref-header (car (symbol-value ref-dep))))
3433         (if (string= id ref)
3434             ;; Yuk!  This is a reference loop.  Make the article be a
3435             ;; root article.
3436             (progn
3437               (mail-header-set-references (car (symbol-value id-dep)) "none")
3438               (setq ref nil))
3439           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3440       (setq ref (gnus-parent-id (mail-header-references header)))
3441       (setq ref-dep (intern (or ref "none") dependencies))
3442       (if (boundp ref-dep)
3443           (setcdr (symbol-value ref-dep)
3444                   (nconc (cdr (symbol-value ref-dep))
3445                          (list (symbol-value id-dep))))
3446         (set ref-dep (list nil (symbol-value id-dep)))))
3447     header))
3448
3449 (defun gnus-build-sparse-threads ()
3450   (let ((headers gnus-newsgroup-headers)
3451         (mail-parse-charset gnus-newsgroup-charset)
3452         (gnus-summary-ignore-duplicates t)
3453         header references generation relations
3454         subject child end new-child date)
3455     ;; First we create an alist of generations/relations, where
3456     ;; generations is how much we trust the relation, and the relation
3457     ;; is parent/child.
3458     (gnus-message 7 "Making sparse threads...")
3459     (save-excursion
3460       (nnheader-set-temp-buffer " *gnus sparse threads*")
3461       (while (setq header (pop headers))
3462         (when (and (setq references (mail-header-references header))
3463                    (not (string= references "")))
3464           (insert references)
3465           (setq child (mail-header-id header)
3466                 subject (mail-header-subject header)
3467                 date (mail-header-date header)
3468                 generation 0)
3469           (while (search-backward ">" nil t)
3470             (setq end (1+ (point)))
3471             (when (search-backward "<" nil t)
3472               (setq new-child (buffer-substring (point) end))
3473               (push (list (incf generation)
3474                           child (setq child new-child)
3475                           subject date)
3476                     relations)))
3477           (when child
3478             (push (list (1+ generation) child nil subject) relations))
3479           (erase-buffer)))
3480       (kill-buffer (current-buffer)))
3481     ;; Sort over trustworthiness.
3482     (mapcar
3483      (lambda (relation)
3484        (when (gnus-dependencies-add-header
3485               (make-full-mail-header-from-decoded-header
3486                gnus-reffed-article-number
3487                (nth 3 relation) "" (or (nth 4 relation) "")
3488                (nth 1 relation)
3489                (or (nth 2 relation) "") 0 0 "")
3490               gnus-newsgroup-dependencies nil)
3491          (push gnus-reffed-article-number gnus-newsgroup-limit)
3492          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3493          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3494                gnus-newsgroup-reads)
3495          (decf gnus-reffed-article-number)))
3496      (sort relations 'car-less-than-car))
3497     (gnus-message 7 "Making sparse threads...done")))
3498
3499 (defun gnus-build-old-threads ()
3500   ;; Look at all the articles that refer back to old articles, and
3501   ;; fetch the headers for the articles that aren't there.  This will
3502   ;; build complete threads - if the roots haven't been expired by the
3503   ;; server, that is.
3504   (let ((mail-parse-charset gnus-newsgroup-charset)
3505         id heads)
3506     (mapatoms
3507      (lambda (refs)
3508        (when (not (car (symbol-value refs)))
3509          (setq heads (cdr (symbol-value refs)))
3510          (while heads
3511            (if (memq (mail-header-number (caar heads))
3512                      gnus-newsgroup-dormant)
3513                (setq heads (cdr heads))
3514              (setq id (symbol-name refs))
3515              (while (and (setq id (gnus-build-get-header id))
3516                          (not (car (gnus-id-to-thread id)))))
3517              (setq heads nil)))))
3518      gnus-newsgroup-dependencies)))
3519
3520 ;; This function has to be called with point after the article number
3521 ;; on the beginning of the line.
3522 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3523   (let ((eol (gnus-point-at-eol))
3524         (buffer (current-buffer))
3525         header)
3526
3527     ;; overview: [num subject from date id refs chars lines misc]
3528     (unwind-protect
3529         (progn
3530           (narrow-to-region (point) eol)
3531           (unless (eobp)
3532             (forward-char))
3533
3534           (setq header
3535                 (make-full-mail-header
3536                  number                         ; number
3537                  (nnheader-nov-field)           ; subject
3538                  (nnheader-nov-field)           ; from
3539                  (nnheader-nov-field)           ; date
3540                  (nnheader-nov-read-message-id) ; id
3541                  (nnheader-nov-field)           ; refs
3542                  (nnheader-nov-read-integer)    ; chars
3543                  (nnheader-nov-read-integer)    ; lines
3544                  (unless (eobp)
3545                    (if (looking-at "Xref: ")
3546                        (goto-char (match-end 0)))
3547                    (nnheader-nov-field))        ; Xref
3548                  (nnheader-nov-parse-extra))))  ; extra
3549
3550       (widen))
3551
3552     (when gnus-alter-header-function
3553       (funcall gnus-alter-header-function header))
3554     (gnus-dependencies-add-header header dependencies force-new)))
3555
3556 (defun gnus-build-get-header (id)
3557   "Look through the buffer of NOV lines and find the header to ID.
3558 Enter this line into the dependencies hash table, and return
3559 the id of the parent article (if any)."
3560   (let ((deps gnus-newsgroup-dependencies)
3561         found header)
3562     (prog1
3563         (save-excursion
3564           (set-buffer nntp-server-buffer)
3565           (let ((case-fold-search nil))
3566             (goto-char (point-min))
3567             (while (and (not found)
3568                         (search-forward id nil t))
3569               (beginning-of-line)
3570               (setq found (looking-at
3571                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3572                                    (regexp-quote id))))
3573               (or found (beginning-of-line 2)))
3574             (when found
3575               (beginning-of-line)
3576               (and
3577                (setq header (gnus-nov-parse-line
3578                              (read (current-buffer)) deps))
3579                (gnus-parent-id (mail-header-references header))))))
3580       (when header
3581         (let ((number (mail-header-number header)))
3582           (push number gnus-newsgroup-limit)
3583           (push header gnus-newsgroup-headers)
3584           (if (memq number gnus-newsgroup-unselected)
3585               (progn
3586                 (push number gnus-newsgroup-unreads)
3587                 (setq gnus-newsgroup-unselected
3588                       (delq number gnus-newsgroup-unselected)))
3589             (push number gnus-newsgroup-ancient)))))))
3590
3591 (defun gnus-build-all-threads ()
3592   "Read all the headers."
3593   (let ((gnus-summary-ignore-duplicates t)
3594         (mail-parse-charset gnus-newsgroup-charset)
3595         (dependencies gnus-newsgroup-dependencies)
3596         header article)
3597     (save-excursion
3598       (set-buffer nntp-server-buffer)
3599       (let ((case-fold-search nil))
3600         (goto-char (point-min))
3601         (while (not (eobp))
3602           (ignore-errors
3603             (setq article (read (current-buffer))
3604                   header (gnus-nov-parse-line article dependencies)))
3605           (when header
3606             (save-excursion
3607               (set-buffer gnus-summary-buffer)
3608               (push header gnus-newsgroup-headers)
3609               (if (memq (setq article (mail-header-number header))
3610                         gnus-newsgroup-unselected)
3611                   (progn
3612                     (push article gnus-newsgroup-unreads)
3613                     (setq gnus-newsgroup-unselected
3614                           (delq article gnus-newsgroup-unselected)))
3615                 (push article gnus-newsgroup-ancient)))
3616             (forward-line 1)))))))
3617
3618 (defun gnus-summary-update-article-line (article header)
3619   "Update the line for ARTICLE using HEADERS."
3620   (let* ((id (mail-header-id header))
3621          (thread (gnus-id-to-thread id)))
3622     (unless thread
3623       (error "Article in no thread"))
3624     ;; Update the thread.
3625     (setcar thread header)
3626     (gnus-summary-goto-subject article)
3627     (let* ((datal (gnus-data-find-list article))
3628            (data (car datal))
3629            (length (when (cdr datal)
3630                      (- (gnus-data-pos data)
3631                         (gnus-data-pos (cadr datal)))))
3632            (buffer-read-only nil)
3633            (level (gnus-summary-thread-level)))
3634       (gnus-delete-line)
3635       (gnus-summary-insert-line
3636        header level nil (gnus-article-mark article)
3637        (memq article gnus-newsgroup-replied)
3638        (memq article gnus-newsgroup-expirable)
3639        ;; Only insert the Subject string when it's different
3640        ;; from the previous Subject string.
3641        (if (and
3642             gnus-show-threads
3643             (gnus-subject-equal
3644              (condition-case ()
3645                  (mail-header-subject
3646                   (gnus-data-header
3647                    (cadr
3648                     (gnus-data-find-list
3649                      article
3650                      (gnus-data-list t)))))
3651                ;; Error on the side of excessive subjects.
3652                (error ""))
3653              (mail-header-subject header)))
3654            ""
3655          (mail-header-subject header))
3656        nil (cdr (assq article gnus-newsgroup-scored))
3657        (memq article gnus-newsgroup-processable))
3658       (when length
3659         (gnus-data-update-list
3660          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3661
3662 (defun gnus-summary-update-article (article &optional iheader)
3663   "Update ARTICLE in the summary buffer."
3664   (set-buffer gnus-summary-buffer)
3665   (let* ((header (gnus-summary-article-header article))
3666          (id (mail-header-id header))
3667          (data (gnus-data-find article))
3668          (thread (gnus-id-to-thread id))
3669          (references (mail-header-references header))
3670          (parent
3671           (gnus-id-to-thread
3672            (or (gnus-parent-id
3673                 (when (and references
3674                            (not (equal "" references)))
3675                   references))
3676                "none")))
3677          (buffer-read-only nil)
3678          (old (car thread)))
3679     (when thread
3680       (unless iheader
3681         (setcar thread nil)
3682         (when parent
3683           (delq thread parent)))
3684       (if (gnus-summary-insert-subject id header)
3685           ;; Set the (possibly) new article number in the data structure.
3686           (gnus-data-set-number data (gnus-id-to-article id))
3687         (setcar thread old)
3688         nil))))
3689
3690 (defun gnus-rebuild-thread (id &optional line)
3691   "Rebuild the thread containing ID.
3692 If LINE, insert the rebuilt thread starting on line LINE."
3693   (let ((buffer-read-only nil)
3694         old-pos current thread data)
3695     (if (not gnus-show-threads)
3696         (setq thread (list (car (gnus-id-to-thread id))))
3697       ;; Get the thread this article is part of.
3698       (setq thread (gnus-remove-thread id)))
3699     (setq old-pos (gnus-point-at-bol))
3700     (setq current (save-excursion
3701                     (and (zerop (forward-line -1))
3702                          (gnus-summary-article-number))))
3703     ;; If this is a gathered thread, we have to go some re-gathering.
3704     (when (stringp (car thread))
3705       (let ((subject (car thread))
3706             roots thr)
3707         (setq thread (cdr thread))
3708         (while thread
3709           (unless (memq (setq thr (gnus-id-to-thread
3710                                    (gnus-root-id
3711                                     (mail-header-id (caar thread)))))
3712                         roots)
3713             (push thr roots))
3714           (setq thread (cdr thread)))
3715         ;; We now have all (unique) roots.
3716         (if (= (length roots) 1)
3717             ;; All the loose roots are now one solid root.
3718             (setq thread (car roots))
3719           (setq thread (cons subject (gnus-sort-threads roots))))))
3720     (let (threads)
3721       ;; We then insert this thread into the summary buffer.
3722       (when line
3723         (goto-char (point-min))
3724         (forward-line (1- line)))
3725       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3726         (if gnus-show-threads
3727             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3728           (gnus-summary-prepare-unthreaded thread))
3729         (setq data (nreverse gnus-newsgroup-data))
3730         (setq threads gnus-newsgroup-threads))
3731       ;; We splice the new data into the data structure.
3732       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3733       ;;!!! then we want to insert at the beginning of the buffer.
3734       ;;!!! That happens to be true with Gnus now, but that may
3735       ;;!!! change in the future.  Perhaps.
3736       (gnus-data-enter-list
3737        (if line nil current) data (- (point) old-pos))
3738       (setq gnus-newsgroup-threads
3739             (nconc threads gnus-newsgroup-threads))
3740       (gnus-data-compute-positions))))
3741
3742 (defun gnus-number-to-header (number)
3743   "Return the header for article NUMBER."
3744   (let ((headers gnus-newsgroup-headers))
3745     (while (and headers
3746                 (not (= number (mail-header-number (car headers)))))
3747       (pop headers))
3748     (when headers
3749       (car headers))))
3750
3751 (defun gnus-parent-headers (in-headers &optional generation)
3752   "Return the headers of the GENERATIONeth parent of HEADERS."
3753   (unless generation
3754     (setq generation 1))
3755   (let ((parent t)
3756         (headers in-headers)
3757         references)
3758     (while (and parent
3759                 (not (zerop generation))
3760                 (setq references (mail-header-references headers)))
3761       (setq headers (if (and references
3762                              (setq parent (gnus-parent-id references)))
3763                         (car (gnus-id-to-thread parent))
3764                       nil))
3765       (decf generation))
3766     (and (not (eq headers in-headers))
3767          headers)))
3768
3769 (defun gnus-id-to-thread (id)
3770   "Return the (sub-)thread where ID appears."
3771   (gnus-gethash id gnus-newsgroup-dependencies))
3772
3773 (defun gnus-id-to-article (id)
3774   "Return the article number of ID."
3775   (let ((thread (gnus-id-to-thread id)))
3776     (when (and thread
3777                (car thread))
3778       (mail-header-number (car thread)))))
3779
3780 (defun gnus-id-to-header (id)
3781   "Return the article headers of ID."
3782   (car (gnus-id-to-thread id)))
3783
3784 (defun gnus-article-displayed-root-p (article)
3785   "Say whether ARTICLE is a root(ish) article."
3786   (let ((level (gnus-summary-thread-level article))
3787         (refs (mail-header-references  (gnus-summary-article-header article)))
3788         particle)
3789     (cond
3790      ((null level) nil)
3791      ((zerop level) t)
3792      ((null refs) t)
3793      ((null (gnus-parent-id refs)) t)
3794      ((and (= 1 level)
3795            (null (setq particle (gnus-id-to-article
3796                                  (gnus-parent-id refs))))
3797            (null (gnus-summary-thread-level particle)))))))
3798
3799 (defun gnus-root-id (id)
3800   "Return the id of the root of the thread where ID appears."
3801   (let (last-id prev)
3802     (while (and id (setq prev (car (gnus-id-to-thread id))))
3803       (setq last-id id
3804             id (gnus-parent-id (mail-header-references prev))))
3805     last-id))
3806
3807 (defun gnus-articles-in-thread (thread)
3808   "Return the list of articles in THREAD."
3809   (cons (mail-header-number (car thread))
3810         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3811
3812 (defun gnus-remove-thread (id &optional dont-remove)
3813   "Remove the thread that has ID in it."
3814   (let (headers thread last-id)
3815     ;; First go up in this thread until we find the root.
3816     (setq last-id (gnus-root-id id)
3817           headers (message-flatten-list (gnus-id-to-thread last-id)))
3818     ;; We have now found the real root of this thread.  It might have
3819     ;; been gathered into some loose thread, so we have to search
3820     ;; through the threads to find the thread we wanted.
3821     (let ((threads gnus-newsgroup-threads)
3822           sub)
3823       (while threads
3824         (setq sub (car threads))
3825         (if (stringp (car sub))
3826             ;; This is a gathered thread, so we look at the roots
3827             ;; below it to find whether this article is in this
3828             ;; gathered root.
3829             (progn
3830               (setq sub (cdr sub))
3831               (while sub
3832                 (when (member (caar sub) headers)
3833                   (setq thread (car threads)
3834                         threads nil
3835                         sub nil))
3836                 (setq sub (cdr sub))))
3837           ;; It's an ordinary thread, so we check it.
3838           (when (eq (car sub) (car headers))
3839             (setq thread sub
3840                   threads nil)))
3841         (setq threads (cdr threads)))
3842       ;; If this article is in no thread, then it's a root.
3843       (if thread
3844           (unless dont-remove
3845             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3846         (setq thread (gnus-id-to-thread last-id)))
3847       (when thread
3848         (prog1
3849             thread                      ; We return this thread.
3850           (unless dont-remove
3851             (if (stringp (car thread))
3852                 (progn
3853                   ;; If we use dummy roots, then we have to remove the
3854                   ;; dummy root as well.
3855                   (when (eq gnus-summary-make-false-root 'dummy)
3856                     ;; We go to the dummy root by going to
3857                     ;; the first sub-"thread", and then one line up.
3858                     (gnus-summary-goto-article
3859                      (mail-header-number (caadr thread)))
3860                     (forward-line -1)
3861                     (gnus-delete-line)
3862                     (gnus-data-compute-positions))
3863                   (setq thread (cdr thread))
3864                   (while thread
3865                     (gnus-remove-thread-1 (car thread))
3866                     (setq thread (cdr thread))))
3867               (gnus-remove-thread-1 thread))))))))
3868
3869 (defun gnus-remove-thread-1 (thread)
3870   "Remove the thread THREAD recursively."
3871   (let ((number (mail-header-number (pop thread)))
3872         d)
3873     (setq thread (reverse thread))
3874     (while thread
3875       (gnus-remove-thread-1 (pop thread)))
3876     (when (setq d (gnus-data-find number))
3877       (goto-char (gnus-data-pos d))
3878       (gnus-summary-show-thread)
3879       (gnus-data-remove
3880        number
3881        (- (gnus-point-at-bol)
3882           (prog1
3883               (1+ (gnus-point-at-eol))
3884             (gnus-delete-line)))))))
3885
3886 (defun gnus-sort-threads-1 (threads func)
3887   (sort (mapcar (lambda (thread)
3888                   (cons (car thread)
3889                         (and (cdr thread)
3890                              (gnus-sort-threads-1 (cdr thread) func))))
3891                 threads) func))
3892
3893 (defun gnus-sort-threads (threads)
3894   "Sort THREADS."
3895   (if (not gnus-thread-sort-functions)
3896       threads
3897     (gnus-message 8 "Sorting threads...")
3898     (prog1
3899         (gnus-sort-threads-1
3900          threads
3901          (gnus-make-sort-function gnus-thread-sort-functions))
3902       (gnus-message 8 "Sorting threads...done"))))
3903
3904 (defun gnus-sort-articles (articles)
3905   "Sort ARTICLES."
3906   (when gnus-article-sort-functions
3907     (gnus-message 7 "Sorting articles...")
3908     (prog1
3909         (setq gnus-newsgroup-headers
3910               (sort articles (gnus-make-sort-function
3911                               gnus-article-sort-functions)))
3912       (gnus-message 7 "Sorting articles...done"))))
3913
3914 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3915 (defmacro gnus-thread-header (thread)
3916   "Return header of first article in THREAD.
3917 Note that THREAD must never, ever be anything else than a variable -
3918 using some other form will lead to serious barfage."
3919   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3920   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3921   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3922         (vector thread) 2))
3923
3924 (defsubst gnus-article-sort-by-number (h1 h2)
3925   "Sort articles by article number."
3926   (< (mail-header-number h1)
3927      (mail-header-number h2)))
3928
3929 (defun gnus-thread-sort-by-number (h1 h2)
3930   "Sort threads by root article number."
3931   (gnus-article-sort-by-number
3932    (gnus-thread-header h1) (gnus-thread-header h2)))
3933
3934 (defsubst gnus-article-sort-by-lines (h1 h2)
3935   "Sort articles by article Lines header."
3936   (< (mail-header-lines h1)
3937      (mail-header-lines h2)))
3938
3939 (defun gnus-thread-sort-by-lines (h1 h2)
3940   "Sort threads by root article Lines header."
3941   (gnus-article-sort-by-lines
3942    (gnus-thread-header h1) (gnus-thread-header h2)))
3943
3944 (defsubst gnus-article-sort-by-chars (h1 h2)
3945   "Sort articles by octet length."
3946   (< (mail-header-chars h1)
3947      (mail-header-chars h2)))
3948
3949 (defun gnus-thread-sort-by-chars (h1 h2)
3950   "Sort threads by root article octet length."
3951   (gnus-article-sort-by-chars
3952    (gnus-thread-header h1) (gnus-thread-header h2)))
3953
3954 (defsubst gnus-article-sort-by-author (h1 h2)
3955   "Sort articles by root author."
3956   (string-lessp
3957    (let ((addr (car (mime-entity-read-field h1 'From))))
3958      (or (std11-full-name-string addr)
3959          (std11-address-string addr)
3960          ""))
3961    (let ((addr (car (mime-entity-read-field h2 'From))))
3962      (or (std11-full-name-string addr)
3963          (std11-address-string addr)
3964          ""))
3965    ))
3966
3967 (defun gnus-thread-sort-by-author (h1 h2)
3968   "Sort threads by root author."
3969   (gnus-article-sort-by-author
3970    (gnus-thread-header h1)  (gnus-thread-header h2)))
3971
3972 (defsubst gnus-article-sort-by-subject (h1 h2)
3973   "Sort articles by root subject."
3974   (string-lessp
3975    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3976    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3977
3978 (defun gnus-thread-sort-by-subject (h1 h2)
3979   "Sort threads by root subject."
3980   (gnus-article-sort-by-subject
3981    (gnus-thread-header h1) (gnus-thread-header h2)))
3982
3983 (defsubst gnus-article-sort-by-date (h1 h2)
3984   "Sort articles by root article date."
3985   (time-less-p
3986    (gnus-date-get-time (mail-header-date h1))
3987    (gnus-date-get-time (mail-header-date h2))))
3988
3989 (defun gnus-thread-sort-by-date (h1 h2)
3990   "Sort threads by root article date."
3991   (gnus-article-sort-by-date
3992    (gnus-thread-header h1) (gnus-thread-header h2)))
3993
3994 (defsubst gnus-article-sort-by-score (h1 h2)
3995   "Sort articles by root article score.
3996 Unscored articles will be counted as having a score of zero."
3997   (> (or (cdr (assq (mail-header-number h1)
3998                     gnus-newsgroup-scored))
3999          gnus-summary-default-score 0)
4000      (or (cdr (assq (mail-header-number h2)
4001                     gnus-newsgroup-scored))
4002          gnus-summary-default-score 0)))
4003
4004 (defun gnus-thread-sort-by-score (h1 h2)
4005   "Sort threads by root article score."
4006   (gnus-article-sort-by-score
4007    (gnus-thread-header h1) (gnus-thread-header h2)))
4008
4009 (defun gnus-thread-sort-by-total-score (h1 h2)
4010   "Sort threads by the sum of all scores in the thread.
4011 Unscored articles will be counted as having a score of zero."
4012   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4013
4014 (defun gnus-thread-total-score (thread)
4015   ;; This function find the total score of THREAD.
4016   (cond ((null thread)
4017          0)
4018         ((consp thread)
4019          (if (stringp (car thread))
4020              (apply gnus-thread-score-function 0
4021                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4022            (gnus-thread-total-score-1 thread)))
4023         (t
4024          (gnus-thread-total-score-1 (list thread)))))
4025
4026 (defun gnus-thread-total-score-1 (root)
4027   ;; This function find the total score of the thread below ROOT.
4028   (setq root (car root))
4029   (apply gnus-thread-score-function
4030          (or (append
4031               (mapcar 'gnus-thread-total-score
4032                       (cdr (gnus-id-to-thread (mail-header-id root))))
4033               (when (> (mail-header-number root) 0)
4034                 (list (or (cdr (assq (mail-header-number root)
4035                                      gnus-newsgroup-scored))
4036                           gnus-summary-default-score 0))))
4037              (list gnus-summary-default-score)
4038              '(0))))
4039
4040 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4041 (defvar gnus-tmp-prev-subject nil)
4042 (defvar gnus-tmp-false-parent nil)
4043 (defvar gnus-tmp-root-expunged nil)
4044 (defvar gnus-tmp-dummy-line nil)
4045
4046 (eval-when-compile (defvar gnus-tmp-header))
4047 (defun gnus-extra-header (type &optional header)
4048   "Return the extra header of TYPE."
4049   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4050       ""))
4051
4052 (defun gnus-summary-prepare-threads (threads)
4053   "Prepare summary buffer from THREADS and indentation LEVEL.
4054 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4055 or a straight list of headers."
4056   (gnus-message 7 "Generating summary...")
4057
4058   (setq gnus-newsgroup-threads threads)
4059   (beginning-of-line)
4060
4061   (let ((gnus-tmp-level 0)
4062         (default-score (or gnus-summary-default-score 0))
4063         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4064         thread number subject stack state gnus-tmp-gathered beg-match
4065         new-roots gnus-tmp-new-adopts thread-end
4066         gnus-tmp-header gnus-tmp-unread
4067         gnus-tmp-replied gnus-tmp-subject-or-nil
4068         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4069         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4070         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4071
4072     (setq gnus-tmp-prev-subject nil)
4073
4074     (if (vectorp (car threads))
4075         ;; If this is a straight (sic) list of headers, then a
4076         ;; threaded summary display isn't required, so we just create
4077         ;; an unthreaded one.
4078         (gnus-summary-prepare-unthreaded threads)
4079
4080       ;; Do the threaded display.
4081
4082       (while (or threads stack gnus-tmp-new-adopts new-roots)
4083
4084         (if (and (= gnus-tmp-level 0)
4085                  (or (not stack)
4086                      (= (caar stack) 0))
4087                  (not gnus-tmp-false-parent)
4088                  (or gnus-tmp-new-adopts new-roots))
4089             (if gnus-tmp-new-adopts
4090                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4091                       thread (list (car gnus-tmp-new-adopts))
4092                       gnus-tmp-header (caar thread)
4093                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4094               (when new-roots
4095                 (setq thread (list (car new-roots))
4096                       gnus-tmp-header (caar thread)
4097                       new-roots (cdr new-roots))))
4098
4099           (if threads
4100               ;; If there are some threads, we do them before the
4101               ;; threads on the stack.
4102               (setq thread threads
4103                     gnus-tmp-header (caar thread))
4104             ;; There were no current threads, so we pop something off
4105             ;; the stack.
4106             (setq state (car stack)
4107                   gnus-tmp-level (car state)
4108                   thread (cdr state)
4109                   stack (cdr stack)
4110                   gnus-tmp-header (caar thread))))
4111
4112         (setq gnus-tmp-false-parent nil)
4113         (setq gnus-tmp-root-expunged nil)
4114         (setq thread-end nil)
4115
4116         (if (stringp gnus-tmp-header)
4117             ;; The header is a dummy root.
4118             (cond
4119              ((eq gnus-summary-make-false-root 'adopt)
4120               ;; We let the first article adopt the rest.
4121               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4122                                                (cddar thread)))
4123               (setq gnus-tmp-gathered
4124                     (nconc (mapcar
4125                             (lambda (h) (mail-header-number (car h)))
4126                             (cddar thread))
4127                            gnus-tmp-gathered))
4128               (setq thread (cons (list (caar thread)
4129                                        (cadar thread))
4130                                  (cdr thread)))
4131               (setq gnus-tmp-level -1
4132                     gnus-tmp-false-parent t))
4133              ((eq gnus-summary-make-false-root 'empty)
4134               ;; We print adopted articles with empty subject fields.
4135               (setq gnus-tmp-gathered
4136                     (nconc (mapcar
4137                             (lambda (h) (mail-header-number (car h)))
4138                             (cddar thread))
4139                            gnus-tmp-gathered))
4140               (setq gnus-tmp-level -1))
4141              ((eq gnus-summary-make-false-root 'dummy)
4142               ;; We remember that we probably want to output a dummy
4143               ;; root.
4144               (setq gnus-tmp-dummy-line gnus-tmp-header)
4145               (setq gnus-tmp-prev-subject gnus-tmp-header))
4146              (t
4147               ;; We do not make a root for the gathered
4148               ;; sub-threads at all.
4149               (setq gnus-tmp-level -1)))
4150
4151           (setq number (mail-header-number gnus-tmp-header)
4152                 subject (mail-header-subject gnus-tmp-header))
4153
4154           (cond
4155            ;; If the thread has changed subject, we might want to make
4156            ;; this subthread into a root.
4157            ((and (null gnus-thread-ignore-subject)
4158                  (not (zerop gnus-tmp-level))
4159                  gnus-tmp-prev-subject
4160                  (not (inline
4161                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4162             (setq new-roots (nconc new-roots (list (car thread)))
4163                   thread-end t
4164                   gnus-tmp-header nil))
4165            ;; If the article lies outside the current limit,
4166            ;; then we do not display it.
4167            ((not (memq number gnus-newsgroup-limit))
4168             (setq gnus-tmp-gathered
4169                   (nconc (mapcar
4170                           (lambda (h) (mail-header-number (car h)))
4171                           (cdar thread))
4172                          gnus-tmp-gathered))
4173             (setq gnus-tmp-new-adopts (if (cdar thread)
4174                                           (append gnus-tmp-new-adopts
4175                                                   (cdar thread))
4176                                         gnus-tmp-new-adopts)
4177                   thread-end t
4178                   gnus-tmp-header nil)
4179             (when (zerop gnus-tmp-level)
4180               (setq gnus-tmp-root-expunged t)))
4181            ;; Perhaps this article is to be marked as read?
4182            ((and gnus-summary-mark-below
4183                  (< (or (cdr (assq number gnus-newsgroup-scored))
4184                         default-score)
4185                     gnus-summary-mark-below)
4186                  ;; Don't touch sparse articles.
4187                  (not (gnus-summary-article-sparse-p number))
4188                  (not (gnus-summary-article-ancient-p number)))
4189             (setq gnus-newsgroup-unreads
4190                   (delq number gnus-newsgroup-unreads))
4191             (if gnus-newsgroup-auto-expire
4192                 (push number gnus-newsgroup-expirable)
4193               (push (cons number gnus-low-score-mark)
4194                     gnus-newsgroup-reads))))
4195
4196           (when gnus-tmp-header
4197             ;; We may have an old dummy line to output before this
4198             ;; article.
4199             (when (and gnus-tmp-dummy-line
4200                        (gnus-subject-equal
4201                         gnus-tmp-dummy-line
4202                         (mail-header-subject gnus-tmp-header)))
4203               (gnus-summary-insert-dummy-line
4204                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4205               (setq gnus-tmp-dummy-line nil))
4206
4207             ;; Compute the mark.
4208             (setq gnus-tmp-unread (gnus-article-mark number))
4209
4210             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4211                                   gnus-tmp-header gnus-tmp-level)
4212                   gnus-newsgroup-data)
4213
4214             ;; Actually insert the line.
4215             (setq
4216              gnus-tmp-subject-or-nil
4217              (cond
4218               ((and gnus-thread-ignore-subject
4219                     gnus-tmp-prev-subject
4220                     (not (inline (gnus-subject-equal
4221                                   gnus-tmp-prev-subject subject))))
4222                subject)
4223               ((zerop gnus-tmp-level)
4224                (if (and (eq gnus-summary-make-false-root 'empty)
4225                         (memq number gnus-tmp-gathered)
4226                         gnus-tmp-prev-subject
4227                         (inline (gnus-subject-equal
4228                                  gnus-tmp-prev-subject subject)))
4229                    gnus-summary-same-subject
4230                  subject))
4231               (t gnus-summary-same-subject)))
4232             (if (and (eq gnus-summary-make-false-root 'adopt)
4233                      (= gnus-tmp-level 1)
4234                      (memq number gnus-tmp-gathered))
4235                 (setq gnus-tmp-opening-bracket ?\<
4236                       gnus-tmp-closing-bracket ?\>)
4237               (setq gnus-tmp-opening-bracket ?\[
4238                     gnus-tmp-closing-bracket ?\]))
4239             (setq
4240              gnus-tmp-indentation
4241              (aref gnus-thread-indent-array gnus-tmp-level)
4242              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4243              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4244                                 gnus-summary-default-score 0)
4245              gnus-tmp-score-char
4246              (if (or (null gnus-summary-default-score)
4247                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4248                          gnus-summary-zcore-fuzz))
4249                  ?  ;Whitespace
4250                (if (< gnus-tmp-score gnus-summary-default-score)
4251                    gnus-score-below-mark gnus-score-over-mark))
4252              gnus-tmp-replied
4253              (cond ((memq number gnus-newsgroup-processable)
4254                     gnus-process-mark)
4255                    ((memq number gnus-newsgroup-cached)
4256                     gnus-cached-mark)
4257                    ((memq number gnus-newsgroup-replied)
4258                     gnus-replied-mark)
4259                    ((memq number gnus-newsgroup-saved)
4260                     gnus-saved-mark)
4261                    (t gnus-no-mark))
4262              gnus-tmp-from (mail-header-from gnus-tmp-header)
4263              gnus-tmp-name
4264              (cond
4265               ((string-match "<[^>]+> *$" gnus-tmp-from)
4266                (setq beg-match (match-beginning 0))
4267                (or (and (string-match "^\".+\"" gnus-tmp-from)
4268                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4269                    (substring gnus-tmp-from 0 beg-match)))
4270               ((string-match "(.+)" gnus-tmp-from)
4271                (substring gnus-tmp-from
4272                           (1+ (match-beginning 0)) (1- (match-end 0))))
4273               (t gnus-tmp-from)))
4274             (when (string= gnus-tmp-name "")
4275               (setq gnus-tmp-name gnus-tmp-from))
4276             (unless (numberp gnus-tmp-lines)
4277               (setq gnus-tmp-lines 0))
4278             (gnus-put-text-property
4279              (point)
4280              (progn (eval gnus-summary-line-format-spec) (point))
4281              'gnus-number number)
4282             (when gnus-visual-p
4283               (forward-line -1)
4284               (gnus-run-hooks 'gnus-summary-update-hook)
4285               (forward-line 1))
4286
4287             (setq gnus-tmp-prev-subject subject)))
4288
4289         (when (nth 1 thread)
4290           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4291         (incf gnus-tmp-level)
4292         (setq threads (if thread-end nil (cdar thread)))
4293         (unless threads
4294           (setq gnus-tmp-level 0)))))
4295   (gnus-message 7 "Generating summary...done"))
4296
4297 (defun gnus-summary-prepare-unthreaded (headers)
4298   "Generate an unthreaded summary buffer based on HEADERS."
4299   (let (header number mark)
4300
4301     (beginning-of-line)
4302
4303     (while headers
4304       ;; We may have to root out some bad articles...
4305       (when (memq (setq number (mail-header-number
4306                                 (setq header (pop headers))))
4307                   gnus-newsgroup-limit)
4308         ;; Mark article as read when it has a low score.
4309         (when (and gnus-summary-mark-below
4310                    (< (or (cdr (assq number gnus-newsgroup-scored))
4311                           gnus-summary-default-score 0)
4312                       gnus-summary-mark-below)
4313                    (not (gnus-summary-article-ancient-p number)))
4314           (setq gnus-newsgroup-unreads
4315                 (delq number gnus-newsgroup-unreads))
4316           (if gnus-newsgroup-auto-expire
4317               (push number gnus-newsgroup-expirable)
4318             (push (cons number gnus-low-score-mark)
4319                   gnus-newsgroup-reads)))
4320
4321         (setq mark (gnus-article-mark number))
4322         (push (gnus-data-make number mark (1+ (point)) header 0)
4323               gnus-newsgroup-data)
4324         (gnus-summary-insert-line
4325          header 0 number
4326          mark (memq number gnus-newsgroup-replied)
4327          (memq number gnus-newsgroup-expirable)
4328          (mail-header-subject header) nil
4329          (cdr (assq number gnus-newsgroup-scored))
4330          (memq number gnus-newsgroup-processable))))))
4331
4332 (defun gnus-summary-remove-list-identifiers ()
4333   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4334   (let ((regexp (if (consp gnus-list-identifiers)
4335                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4336                   gnus-list-identifiers))
4337         changed subject)
4338     (when regexp
4339       (dolist (header gnus-newsgroup-headers)
4340         (setq subject (mail-header-subject header)
4341               changed nil)
4342         (while (string-match
4343                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4344                 subject)
4345           (setq subject
4346                 (concat (substring subject 0 (match-beginning 2))
4347                         (substring subject (match-end 0)))
4348                 changed t))
4349         (when (and changed
4350                    (string-match
4351                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4352           (setq subject
4353                 (concat (substring subject 0 (match-beginning 1))
4354                         (substring subject (match-end 1)))))
4355         (when changed
4356           (mail-header-set-subject header subject))))))
4357
4358 (defun gnus-fetch-headers (articles)
4359   "Fetch headers of ARTICLES."
4360   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4361     (gnus-message 5 "Fetching headers for %s..." name)
4362     (prog1
4363         ;;;!!! FIXME: temporary fix for an infloop on nnimap.
4364         (if (eq 'nnimap (car (gnus-find-method-for-group name)))
4365             (if (eq 'nov
4366                     (setq gnus-headers-retrieved-by
4367                           (gnus-retrieve-headers
4368                            articles gnus-newsgroup-name
4369                            ;; We might want to fetch old headers, but
4370                            ;; not if there is only 1 article.
4371                            (and (or (and
4372                                      (not (eq gnus-fetch-old-headers 'some))
4373                                      (not (numberp gnus-fetch-old-headers)))
4374                                     (> (length articles) 1))
4375                                 gnus-fetch-old-headers))))
4376                 (gnus-get-newsgroup-headers-xover
4377                  articles nil nil gnus-newsgroup-name t)
4378               (gnus-get-newsgroup-headers))
4379           (gnus-retrieve-parsed-headers
4380            articles gnus-newsgroup-name
4381            ;; We might want to fetch old headers, but
4382            ;; not if there is only 1 article.
4383            (and (or (and (not (eq gnus-fetch-old-headers 'some))
4384                          (not (numberp gnus-fetch-old-headers)))
4385                     (> (length articles) 1))
4386                 gnus-fetch-old-headers)))
4387       (gnus-message 5 "Fetching headers for %s...done" name))))
4388
4389 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4390   "Select newsgroup GROUP.
4391 If READ-ALL is non-nil, all articles in the group are selected.
4392 If SELECT-ARTICLES, only select those articles from GROUP."
4393   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4394          ;;!!! Dirty hack; should be removed.
4395          (gnus-summary-ignore-duplicates
4396           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4397               t
4398             gnus-summary-ignore-duplicates))
4399          (info (nth 2 entry))
4400          articles fetched-articles cached)
4401
4402     (unless (gnus-check-server
4403              (setq gnus-current-select-method
4404                    (gnus-find-method-for-group group)))
4405       (error "Couldn't open server"))
4406
4407     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4408         (gnus-activate-group group)     ; Or we can activate it...
4409         (progn                          ; Or we bug out.
4410           (when (equal major-mode 'gnus-summary-mode)
4411             (kill-buffer (current-buffer)))
4412           (error "Couldn't activate group %s: %s"
4413                  group (gnus-status-message group))))
4414
4415     (unless (gnus-request-group group t)
4416       (when (equal major-mode 'gnus-summary-mode)
4417         (kill-buffer (current-buffer)))
4418       (error "Couldn't request group %s: %s"
4419              group (gnus-status-message group)))
4420
4421     (setq gnus-newsgroup-name group)
4422     (setq gnus-newsgroup-unselected nil)
4423     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4424     (gnus-summary-setup-default-charset)
4425
4426     ;; Adjust and set lists of article marks.
4427     (when info
4428       (gnus-adjust-marked-articles info))
4429
4430     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4431     (setq cached
4432           (if (gnus-virtual-group-p group)
4433               gnus-newsgroup-cached
4434             (gnus-cache-articles-in-group group)))
4435
4436     (setq gnus-newsgroup-unreads
4437           (gnus-set-difference
4438            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4439            gnus-newsgroup-dormant))
4440
4441     (setq gnus-newsgroup-processable nil)
4442
4443     (gnus-update-read-articles group gnus-newsgroup-unreads)
4444
4445     (if (setq articles select-articles)
4446         (setq gnus-newsgroup-unselected
4447               (gnus-sorted-intersection
4448                gnus-newsgroup-unreads
4449                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4450       (setq articles (gnus-articles-to-read group read-all)))
4451
4452     (cond
4453      ((null articles)
4454       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4455       'quit)
4456      ((eq articles 0) nil)
4457      (t
4458       ;; Init the dependencies hash table.
4459       (setq gnus-newsgroup-dependencies
4460             (gnus-make-hashtable (length articles)))
4461       (gnus-set-global-variables)
4462       ;; Retrieve the headers and read them in.
4463       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4464
4465       ;; Suppress duplicates?
4466       (when gnus-suppress-duplicates
4467         (gnus-dup-suppress-articles))
4468
4469       ;; Set the initial limit.
4470       (setq gnus-newsgroup-limit (copy-sequence articles))
4471       ;; Remove canceled articles from the list of unread articles.
4472       (setq gnus-newsgroup-unreads
4473             (gnus-set-sorted-intersection
4474              gnus-newsgroup-unreads
4475              (setq fetched-articles
4476                    (mapcar (lambda (headers) (mail-header-number headers))
4477                            gnus-newsgroup-headers))))
4478       ;; Removed marked articles that do not exist.
4479       (gnus-update-missing-marks
4480        (gnus-sorted-complement fetched-articles articles))
4481
4482       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4483       (when cached
4484         (setq gnus-newsgroup-cached cached))
4485
4486       ;; We might want to build some more threads first.
4487       (when (and gnus-fetch-old-headers
4488                  (eq gnus-headers-retrieved-by 'nov))
4489         (if (eq gnus-fetch-old-headers 'invisible)
4490             (gnus-build-all-threads)
4491           (gnus-build-old-threads)))
4492       ;; Let the Gnus agent mark articles as read.
4493       (when gnus-agent
4494         (gnus-agent-get-undownloaded-list))
4495       ;; Remove list identifiers from subject
4496       (when gnus-list-identifiers
4497         (gnus-summary-remove-list-identifiers))
4498       ;; Check whether auto-expire is to be done in this group.
4499       (setq gnus-newsgroup-auto-expire
4500             (gnus-group-auto-expirable-p group))
4501       ;; Set up the article buffer now, if necessary.
4502       (unless gnus-single-article-buffer
4503         (gnus-article-setup-buffer))
4504       ;; First and last article in this newsgroup.
4505       (when gnus-newsgroup-headers
4506         (setq gnus-newsgroup-begin
4507               (mail-header-number (car gnus-newsgroup-headers))
4508               gnus-newsgroup-end
4509               (mail-header-number
4510                (gnus-last-element gnus-newsgroup-headers))))
4511       ;; GROUP is successfully selected.
4512       (or gnus-newsgroup-headers t)))))
4513
4514 (defun gnus-articles-to-read (group &optional read-all)
4515   "Find out what articles the user wants to read."
4516   (let* ((articles
4517           ;; Select all articles if `read-all' is non-nil, or if there
4518           ;; are no unread articles.
4519           (if (or read-all
4520                   (and (zerop (length gnus-newsgroup-marked))
4521                        (zerop (length gnus-newsgroup-unreads)))
4522                   (eq (gnus-group-find-parameter group 'display)
4523                       'all))
4524               (or
4525                (gnus-uncompress-range (gnus-active group))
4526                (gnus-cache-articles-in-group group))
4527             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4528                           (copy-sequence gnus-newsgroup-unreads))
4529                   '<)))
4530          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4531          (scored (length scored-list))
4532          (number (length articles))
4533          (marked (+ (length gnus-newsgroup-marked)
4534                     (length gnus-newsgroup-dormant)))
4535          (select
4536           (cond
4537            ((numberp read-all)
4538             read-all)
4539            (t
4540             (condition-case ()
4541                 (cond
4542                  ((and (or (<= scored marked) (= scored number))
4543                        (natnump gnus-large-newsgroup)
4544                        (> number gnus-large-newsgroup))
4545                   (let* ((cursor-in-echo-area nil)
4546                          (input (read-from-minibuffer
4547                                  (format
4548                                   "How many articles from %s (max %d): "
4549                                   (gnus-limit-string gnus-newsgroup-name 35)
4550                                   number)
4551                                  (cons (number-to-string gnus-large-newsgroup)
4552                                        0))))
4553                     (if (string-match "^[ \t]*$" input)
4554                         number
4555                       input)))
4556                  ((and (> scored marked) (< scored number)
4557                        (> (- scored number) 20))
4558                   (let ((input
4559                          (read-string
4560                           (format "%s %s (%d scored, %d total): "
4561                                   "How many articles from"
4562                                   group scored number))))
4563                     (if (string-match "^[ \t]*$" input)
4564                         number input)))
4565                  (t number))
4566               (quit
4567                (message "Quit getting the articles to read")
4568                nil))))))
4569     (setq select (if (stringp select) (string-to-number select) select))
4570     (if (or (null select) (zerop select))
4571         select
4572       (if (and (not (zerop scored)) (<= (abs select) scored))
4573           (progn
4574             (setq articles (sort scored-list '<))
4575             (setq number (length articles)))
4576         (setq articles (copy-sequence articles)))
4577
4578       (when (< (abs select) number)
4579         (if (< select 0)
4580             ;; Select the N oldest articles.
4581             (setcdr (nthcdr (1- (abs select)) articles) nil)
4582           ;; Select the N most recent articles.
4583           (setq articles (nthcdr (- number select) articles))))
4584       (setq gnus-newsgroup-unselected
4585             (gnus-sorted-intersection
4586              gnus-newsgroup-unreads
4587              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4588       (when gnus-alter-articles-to-read-function
4589         (setq gnus-newsgroup-unreads
4590               (sort
4591                (funcall gnus-alter-articles-to-read-function
4592                         gnus-newsgroup-name gnus-newsgroup-unreads)
4593                '<)))
4594       articles)))
4595
4596 (defun gnus-killed-articles (killed articles)
4597   (let (out)
4598     (while articles
4599       (when (inline (gnus-member-of-range (car articles) killed))
4600         (push (car articles) out))
4601       (setq articles (cdr articles)))
4602     out))
4603
4604 (defun gnus-uncompress-marks (marks)
4605   "Uncompress the mark ranges in MARKS."
4606   (let ((uncompressed '(score bookmark))
4607         out)
4608     (while marks
4609       (if (memq (caar marks) uncompressed)
4610           (push (car marks) out)
4611         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4612       (setq marks (cdr marks)))
4613     out))
4614
4615 (defun gnus-adjust-marked-articles (info)
4616   "Set all article lists and remove all marks that are no longer valid."
4617   (let* ((marked-lists (gnus-info-marks info))
4618          (active (gnus-active (gnus-info-group info)))
4619          (min (car active))
4620          (max (cdr active))
4621          (types gnus-article-mark-lists)
4622          (uncompressed '(score bookmark killed))
4623          marks var articles article mark)
4624
4625     (while marked-lists
4626       (setq marks (pop marked-lists))
4627       (set (setq var (intern (format "gnus-newsgroup-%s"
4628                                      (car (rassq (setq mark (car marks))
4629                                                  types)))))
4630            (if (memq (car marks) uncompressed) (cdr marks)
4631              (gnus-uncompress-range (cdr marks))))
4632
4633       (setq articles (symbol-value var))
4634
4635       ;; All articles have to be subsets of the active articles.
4636       (cond
4637        ;; Adjust "simple" lists.
4638        ((memq mark '(tick dormant expire reply save))
4639         (while articles
4640           (when (or (< (setq article (pop articles)) min) (> article max))
4641             (set var (delq article (symbol-value var))))))
4642        ;; Adjust assocs.
4643        ((memq mark uncompressed)
4644         (when (not (listp (cdr (symbol-value var))))
4645           (set var (list (symbol-value var))))
4646         (when (not (listp (cdr articles)))
4647           (setq articles (list articles)))
4648         (while articles
4649           (when (or (not (consp (setq article (pop articles))))
4650                     (< (car article) min)
4651                     (> (car article) max))
4652             (set var (delq article (symbol-value var))))))))))
4653
4654 (defun gnus-update-missing-marks (missing)
4655   "Go through the list of MISSING articles and remove them from the mark lists."
4656   (when missing
4657     (let ((types gnus-article-mark-lists)
4658           var m)
4659       ;; Go through all types.
4660       (while types
4661         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4662         (when (symbol-value var)
4663           ;; This list has articles.  So we delete all missing articles
4664           ;; from it.
4665           (setq m missing)
4666           (while m
4667             (set var (delq (pop m) (symbol-value var)))))))))
4668
4669 (defun gnus-update-marks ()
4670   "Enter the various lists of marked articles into the newsgroup info list."
4671   (let ((types gnus-article-mark-lists)
4672         (info (gnus-get-info gnus-newsgroup-name))
4673         (uncompressed '(score bookmark killed))
4674         type list newmarked symbol delta-marks)
4675     (when info
4676       ;; Add all marks lists to the list of marks lists.
4677       (while (setq type (pop types))
4678         (setq list (symbol-value
4679                     (setq symbol
4680                           (intern (format "gnus-newsgroup-%s"
4681                                           (car type))))))
4682
4683         (when list
4684           ;; Get rid of the entries of the articles that have the
4685           ;; default score.
4686           (when (and (eq (cdr type) 'score)
4687                      gnus-save-score
4688                      list)
4689             (let* ((arts list)
4690                    (prev (cons nil list))
4691                    (all prev))
4692               (while arts
4693                 (if (or (not (consp (car arts)))
4694                         (= (cdar arts) gnus-summary-default-score))
4695                     (setcdr prev (cdr arts))
4696                   (setq prev arts))
4697                 (setq arts (cdr arts)))
4698               (setq list (cdr all)))))
4699
4700         (unless (memq (cdr type) uncompressed)
4701           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4702
4703         (when (gnus-check-backend-function
4704                'request-set-mark gnus-newsgroup-name)
4705           ;; propagate flags to server, with the following exceptions:
4706           ;; uncompressed:s are not proper flags (they are cons cells)
4707           ;; cache is a internal gnus flag
4708           ;; download are local to one gnus installation (well)
4709           ;; unsend are for nndraft groups only
4710           ;; xxx: generality of this?  this suits nnimap anyway
4711           (unless (memq (cdr type) (append '(cache download unsend)
4712                                            uncompressed))
4713             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4714                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4715                    (add (gnus-remove-from-range
4716                          (gnus-copy-sequence list) old)))
4717               (when add
4718                 (push (list add 'add (list (cdr type))) delta-marks))
4719               (when del
4720                 (push (list del 'del (list (cdr type))) delta-marks)))))
4721
4722         (when list
4723           (push (cons (cdr type) list) newmarked)))
4724
4725       (when delta-marks
4726         (unless (gnus-check-group gnus-newsgroup-name)
4727           (error "Can't open server for %s" gnus-newsgroup-name))
4728         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4729
4730       ;; Enter these new marks into the info of the group.
4731       (if (nthcdr 3 info)
4732           (setcar (nthcdr 3 info) newmarked)
4733         ;; Add the marks lists to the end of the info.
4734         (when newmarked
4735           (setcdr (nthcdr 2 info) (list newmarked))))
4736
4737       ;; Cut off the end of the info if there's nothing else there.
4738       (let ((i 5))
4739         (while (and (> i 2)
4740                     (not (nth i info)))
4741           (when (nthcdr (decf i) info)
4742             (setcdr (nthcdr i info) nil)))))))
4743
4744 (defun gnus-set-mode-line (where)
4745   "Set the mode line of the article or summary buffers.
4746 If WHERE is `summary', the summary mode line format will be used."
4747   ;; Is this mode line one we keep updated?
4748   (when (and (memq where gnus-updated-mode-lines)
4749              (symbol-value
4750               (intern (format "gnus-%s-mode-line-format-spec" where))))
4751     (let (mode-string)
4752       (save-excursion
4753         ;; We evaluate this in the summary buffer since these
4754         ;; variables are buffer-local to that buffer.
4755         (set-buffer gnus-summary-buffer)
4756         ;; We bind all these variables that are used in the `eval' form
4757         ;; below.
4758         (let* ((mformat (symbol-value
4759                          (intern
4760                           (format "gnus-%s-mode-line-format-spec" where))))
4761                (gnus-tmp-group-name (gnus-group-decoded-name 
4762                                      gnus-newsgroup-name))
4763                (gnus-tmp-article-number (or gnus-current-article 0))
4764                (gnus-tmp-unread gnus-newsgroup-unreads)
4765                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4766                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4767                (gnus-tmp-unread-and-unselected
4768                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4769                             (zerop gnus-tmp-unselected))
4770                        "")
4771                       ((zerop gnus-tmp-unselected)
4772                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4773                       (t (format "{%d(+%d) more}"
4774                                  gnus-tmp-unread-and-unticked
4775                                  gnus-tmp-unselected))))
4776                (gnus-tmp-subject
4777                 (if (and gnus-current-headers
4778                          (vectorp gnus-current-headers))
4779                     (gnus-mode-string-quote
4780                      (mail-header-subject gnus-current-headers))
4781                   ""))
4782                bufname-length max-len
4783                gnus-tmp-header);; passed as argument to any user-format-funcs
4784           (setq mode-string (eval mformat))
4785           (setq bufname-length (if (string-match "%b" mode-string)
4786                                    (- (length
4787                                        (buffer-name
4788                                         (if (eq where 'summary)
4789                                             nil
4790                                           (get-buffer gnus-article-buffer))))
4791                                       2)
4792                                  0))
4793           (setq max-len (max 4 (if gnus-mode-non-string-length
4794                                    (- (window-width)
4795                                       gnus-mode-non-string-length
4796                                       bufname-length)
4797                                  (length mode-string))))
4798           ;; We might have to chop a bit of the string off...
4799           (when (> (length mode-string) max-len)
4800             (setq mode-string
4801                   (concat (gnus-truncate-string mode-string (- max-len 3))
4802                           "...")))
4803           ;; Pad the mode string a bit.
4804           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4805       ;; Update the mode line.
4806       (setq mode-line-buffer-identification
4807             (gnus-mode-line-buffer-identification (list mode-string)))
4808       (set-buffer-modified-p t))))
4809
4810 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4811   "Go through the HEADERS list and add all Xrefs to a hash table.
4812 The resulting hash table is returned, or nil if no Xrefs were found."
4813   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4814          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4815          (xref-hashtb (gnus-make-hashtable))
4816          start group entry number xrefs header)
4817     (while headers
4818       (setq header (pop headers))
4819       (when (and (setq xrefs (mail-header-xref header))
4820                  (not (memq (setq number (mail-header-number header))
4821                             unreads)))
4822         (setq start 0)
4823         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4824           (setq start (match-end 0))
4825           (setq group (if prefix
4826                           (concat prefix (substring xrefs (match-beginning 1)
4827                                                     (match-end 1)))
4828                         (substring xrefs (match-beginning 1) (match-end 1))))
4829           (setq number
4830                 (string-to-int (substring xrefs (match-beginning 2)
4831                                           (match-end 2))))
4832           (if (setq entry (gnus-gethash group xref-hashtb))
4833               (setcdr entry (cons number (cdr entry)))
4834             (gnus-sethash group (cons number nil) xref-hashtb)))))
4835     (and start xref-hashtb)))
4836
4837 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4838   "Look through all the headers and mark the Xrefs as read."
4839   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4840         name entry info xref-hashtb idlist method nth4)
4841     (save-excursion
4842       (set-buffer gnus-group-buffer)
4843       (when (setq xref-hashtb
4844                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4845         (mapatoms
4846          (lambda (group)
4847            (unless (string= from-newsgroup (setq name (symbol-name group)))
4848              (setq idlist (symbol-value group))
4849              ;; Dead groups are not updated.
4850              (and (prog1
4851                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4852                             info (nth 2 entry))
4853                     (when (stringp (setq nth4 (gnus-info-method info)))
4854                       (setq nth4 (gnus-server-to-method nth4))))
4855                   ;; Only do the xrefs if the group has the same
4856                   ;; select method as the group we have just read.
4857                   (or (gnus-methods-equal-p
4858                        nth4 (gnus-find-method-for-group from-newsgroup))
4859                       virtual
4860                       (equal nth4 (setq method (gnus-find-method-for-group
4861                                                 from-newsgroup)))
4862                       (and (equal (car nth4) (car method))
4863                            (equal (nth 1 nth4) (nth 1 method))))
4864                   gnus-use-cross-reference
4865                   (or (not (eq gnus-use-cross-reference t))
4866                       virtual
4867                       ;; Only do cross-references on subscribed
4868                       ;; groups, if that is what is wanted.
4869                       (<= (gnus-info-level info) gnus-level-subscribed))
4870                   (gnus-group-make-articles-read name idlist))))
4871          xref-hashtb)))))
4872
4873 (defun gnus-compute-read-articles (group articles)
4874   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4875          (info (nth 2 entry))
4876          (active (gnus-active group))
4877          ninfo)
4878     (when entry
4879       ;; First peel off all invalid article numbers.
4880       (when active
4881         (let ((ids articles)
4882               id first)
4883           (while (setq id (pop ids))
4884             (when (and first (> id (cdr active)))
4885               ;; We'll end up in this situation in one particular
4886               ;; obscure situation.  If you re-scan a group and get
4887               ;; a new article that is cross-posted to a different
4888               ;; group that has not been re-scanned, you might get
4889               ;; crossposted article that has a higher number than
4890               ;; Gnus believes possible.  So we re-activate this
4891               ;; group as well.  This might mean doing the
4892               ;; crossposting thingy will *increase* the number
4893               ;; of articles in some groups.  Tsk, tsk.
4894               (setq active (or (gnus-activate-group group) active)))
4895             (when (or (> id (cdr active))
4896                       (< id (car active)))
4897               (setq articles (delq id articles))))))
4898       ;; If the read list is nil, we init it.
4899       (if (and active
4900                (null (gnus-info-read info))
4901                (> (car active) 1))
4902           (setq ninfo (cons 1 (1- (car active))))
4903         (setq ninfo (gnus-info-read info)))
4904       ;; Then we add the read articles to the range.
4905       (gnus-add-to-range
4906        ninfo (setq articles (sort articles '<))))))
4907
4908 (defun gnus-group-make-articles-read (group articles)
4909   "Update the info of GROUP to say that ARTICLES are read."
4910   (let* ((num 0)
4911          (entry (gnus-gethash group gnus-newsrc-hashtb))
4912          (info (nth 2 entry))
4913          (active (gnus-active group))
4914          range)
4915     (when entry
4916       (setq range (gnus-compute-read-articles group articles))
4917       (save-excursion
4918         (set-buffer gnus-group-buffer)
4919         (gnus-undo-register
4920           `(progn
4921              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4922              (gnus-info-set-read ',info ',(gnus-info-read info))
4923              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4924              (gnus-group-update-group ,group t))))
4925       ;; Add the read articles to the range.
4926       (gnus-info-set-read info range)
4927       ;; Then we have to re-compute how many unread
4928       ;; articles there are in this group.
4929       (when active
4930         (cond
4931          ((not range)
4932           (setq num (- (1+ (cdr active)) (car active))))
4933          ((not (listp (cdr range)))
4934           (setq num (- (cdr active) (- (1+ (cdr range))
4935                                        (car range)))))
4936          (t
4937           (while range
4938             (if (numberp (car range))
4939                 (setq num (1+ num))
4940               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4941             (setq range (cdr range)))
4942           (setq num (- (cdr active) num))))
4943         ;; Update the number of unread articles.
4944         (setcar entry num)
4945         ;; Update the group buffer.
4946         (gnus-group-update-group group t)))))
4947
4948 (defvar gnus-newsgroup-none-id 0)
4949
4950 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4951   (let ((cur nntp-server-buffer)
4952         (dependencies
4953          (or dependencies
4954              (save-excursion (set-buffer gnus-summary-buffer)
4955                              gnus-newsgroup-dependencies)))
4956         headers id end ref
4957         (mail-parse-charset gnus-newsgroup-charset)
4958         (mail-parse-ignored-charsets
4959          (save-excursion (condition-case nil
4960                              (set-buffer gnus-summary-buffer)
4961                            (error))
4962                          gnus-newsgroup-ignored-charsets)))
4963     (save-excursion
4964       (set-buffer nntp-server-buffer)
4965       ;; Translate all TAB characters into SPACE characters.
4966       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4967       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4968       (gnus-run-hooks 'gnus-parse-headers-hook)
4969       (let ((case-fold-search t)
4970             in-reply-to header p lines chars ctype)
4971         (goto-char (point-min))
4972         ;; Search to the beginning of the next header.  Error messages
4973         ;; do not begin with 2 or 3.
4974         (while (re-search-forward "^[23][0-9]+ " nil t)
4975           (setq id nil
4976                 ref nil)
4977           ;; This implementation of this function, with nine
4978           ;; search-forwards instead of the one re-search-forward and
4979           ;; a case (which basically was the old function) is actually
4980           ;; about twice as fast, even though it looks messier.  You
4981           ;; can't have everything, I guess.  Speed and elegance
4982           ;; doesn't always go hand in hand.
4983           (setq
4984            header
4985            (make-full-mail-header
4986             ;; Number.
4987             (prog1
4988                 (read cur)
4989               (end-of-line)
4990               (setq p (point))
4991               (narrow-to-region (point)
4992                                 (or (and (search-forward "\n.\n" nil t)
4993                                          (- (point) 2))
4994                                     (point))))
4995             ;; Subject.
4996             (progn
4997               (goto-char p)
4998               (if (search-forward "\nsubject: " nil t)
4999                   (nnheader-header-value)
5000                 "(none)"))
5001             ;; From.
5002             (progn
5003               (goto-char p)
5004               (if (or (search-forward "\nfrom: " nil t)
5005                       (search-forward "\nfrom:" nil t))
5006                   (nnheader-header-value)
5007                 "(nobody)"))
5008             ;; Date.
5009             (progn
5010               (goto-char p)
5011               (if (search-forward "\ndate: " nil t)
5012                   (nnheader-header-value)
5013                 ""))
5014             ;; Message-ID.
5015             (progn
5016               (goto-char p)
5017               (setq id (if (re-search-forward
5018                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5019                            ;; We do it this way to make sure the Message-ID
5020                            ;; is (somewhat) syntactically valid.
5021                            (buffer-substring (match-beginning 1)
5022                                              (match-end 1))
5023                          ;; If there was no message-id, we just fake one
5024                          ;; to make subsequent routines simpler.
5025                          (nnheader-generate-fake-message-id))))
5026             ;; References.
5027             (progn
5028               (goto-char p)
5029               (if (search-forward "\nreferences: " nil t)
5030                   (progn
5031                     (setq end (point))
5032                     (prog1
5033                         (nnheader-header-value)
5034                       (setq ref
5035                             (buffer-substring
5036                              (progn
5037                                ;; (end-of-line)
5038                                (search-backward ">" end t)
5039                                (1+ (point)))
5040                              (progn
5041                                (search-backward "<" end t)
5042                                (point))))))
5043                 ;; Get the references from the in-reply-to header if there
5044                 ;; were no references and the in-reply-to header looks
5045                 ;; promising.
5046                 (if (and (search-forward "\nin-reply-to: " nil t)
5047                          (setq in-reply-to (nnheader-header-value))
5048                          (string-match "<[^>]+>" in-reply-to))
5049                     (let (ref2)
5050                       (setq ref (substring in-reply-to (match-beginning 0)
5051                                            (match-end 0)))
5052                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5053                         (setq ref2 (substring in-reply-to (match-beginning 0)
5054                                               (match-end 0)))
5055                         (when (> (length ref2) (length ref))
5056                           (setq ref ref2)))
5057                       ref)
5058                   (setq ref nil))))
5059             ;; Chars.
5060             (progn
5061               (goto-char p)
5062               (if (search-forward "\nchars: " nil t)
5063                   (if (numberp (setq chars (ignore-errors (read cur))))
5064                       chars 0)
5065                 0))
5066             ;; Lines.
5067             (progn
5068               (goto-char p)
5069               (if (search-forward "\nlines: " nil t)
5070                   (if (numberp (setq lines (ignore-errors (read cur))))
5071                       lines 0)
5072                 0))
5073             ;; Xref.
5074             (progn
5075               (goto-char p)
5076               (and (search-forward "\nxref: " nil t)
5077                    (nnheader-header-value)))
5078             ;; Extra.
5079             (when gnus-extra-headers
5080               (let ((extra gnus-extra-headers)
5081                     out)
5082                 (while extra
5083                   (goto-char p)
5084                   (when (search-forward
5085                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5086                     (push (cons (car extra) (nnheader-header-value)) out))
5087                   (pop extra))
5088                 out))))
5089           (goto-char p)
5090           (if (and (search-forward "\ncontent-type: " nil t)
5091                    (setq ctype (nnheader-header-value)))
5092               (mime-entity-set-content-type-internal
5093                header (mime-parse-Content-Type ctype)))
5094           (when (equal id ref)
5095             (setq ref nil))
5096
5097           (when gnus-alter-header-function
5098             (funcall gnus-alter-header-function header)
5099             (setq id (mail-header-id header)
5100                   ref (gnus-parent-id (mail-header-references header))))
5101
5102           (when (setq header
5103                       (gnus-dependencies-add-header
5104                        header dependencies force-new))
5105             (push header headers))
5106           (goto-char (point-max))
5107           (widen))
5108         (nreverse headers)))))
5109
5110 ;; Goes through the xover lines and returns a list of vectors
5111 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5112                                                   force-new dependencies
5113                                                   group also-fetch-heads)
5114   "Parse the news overview data in the server buffer.
5115 Return a list of headers that match SEQUENCE (see
5116 `nntp-retrieve-headers')."
5117   ;; Get the Xref when the users reads the articles since most/some
5118   ;; NNTP servers do not include Xrefs when using XOVER.
5119   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5120   (let ((mail-parse-charset gnus-newsgroup-charset)
5121         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5122         (cur nntp-server-buffer)
5123         (dependencies (or dependencies gnus-newsgroup-dependencies))
5124         number headers header)
5125     (save-excursion
5126       (set-buffer nntp-server-buffer)
5127       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5128       ;; Allow the user to mangle the headers before parsing them.
5129       (gnus-run-hooks 'gnus-parse-headers-hook)
5130       (goto-char (point-min))
5131       (while (not (eobp))
5132         (condition-case ()
5133             (while (and sequence (not (eobp)))
5134               (setq number (read cur))
5135               (while (and sequence
5136                           (< (car sequence) number))
5137                 (setq sequence (cdr sequence)))
5138               (and sequence
5139                    (eq number (car sequence))
5140                    (progn
5141                      (setq sequence (cdr sequence))
5142                      (setq header (inline
5143                                     (gnus-nov-parse-line
5144                                      number dependencies force-new))))
5145                    (push header headers))
5146               (forward-line 1))
5147           (error
5148            (gnus-error 4 "Strange nov line (%d)"
5149                        (count-lines (point-min) (point)))))
5150         (forward-line 1))
5151       ;; A common bug in inn is that if you have posted an article and
5152       ;; then retrieves the active file, it will answer correctly --
5153       ;; the new article is included.  However, a NOV entry for the
5154       ;; article may not have been generated yet, so this may fail.
5155       ;; We work around this problem by retrieving the last few
5156       ;; headers using HEAD.
5157       (if (or (not also-fetch-heads)
5158               (not sequence))
5159           ;; We (probably) got all the headers.
5160           (nreverse headers)
5161         (let ((gnus-nov-is-evil t))
5162           (nconc
5163            (nreverse headers)
5164            ;;;!!! FIXME: temporary fix for an infloop on nnimap.
5165            (if (eq 'nnimap (car (gnus-find-method-for-group group)))
5166                (when (gnus-retrieve-headers sequence group)
5167                  (gnus-get-newsgroup-headers))
5168              (gnus-retrieve-parsed-headers sequence group))))))))
5169
5170 (defun gnus-article-get-xrefs ()
5171   "Fill in the Xref value in `gnus-current-headers', if necessary.
5172 This is meant to be called in `gnus-article-internal-prepare-hook'."
5173   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5174                                  gnus-current-headers)))
5175     (or (not gnus-use-cross-reference)
5176         (not headers)
5177         (and (mail-header-xref headers)
5178              (not (string= (mail-header-xref headers) "")))
5179         (let ((case-fold-search t)
5180               xref)
5181           (save-restriction
5182             (nnheader-narrow-to-headers)
5183             (goto-char (point-min))
5184             (when (or (and (not (eobp))
5185                            (eq (downcase (char-after)) ?x)
5186                            (looking-at "Xref:"))
5187                       (search-forward "\nXref:" nil t))
5188               (goto-char (1+ (match-end 0)))
5189               (setq xref (buffer-substring (point)
5190                                            (progn (end-of-line) (point))))
5191               (mail-header-set-xref headers xref)))))))
5192
5193 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5194   "Find article ID and insert the summary line for that article.
5195 OLD-HEADER can either be a header or a line number to insert
5196 the subject line on."
5197   (let* ((line (and (numberp old-header) old-header))
5198          (old-header (and (vectorp old-header) old-header))
5199          (header (cond ((and old-header use-old-header)
5200                         old-header)
5201                        ((and (numberp id)
5202                              (gnus-number-to-header id))
5203                         (gnus-number-to-header id))
5204                        (t
5205                         (gnus-read-header id))))
5206          (number (and (numberp id) id))
5207          d)
5208     (when header
5209       ;; Rebuild the thread that this article is part of and go to the
5210       ;; article we have fetched.
5211       (when (and (not gnus-show-threads)
5212                  old-header)
5213         (when (and number
5214                    (setq d (gnus-data-find (mail-header-number old-header))))
5215           (goto-char (gnus-data-pos d))
5216           (gnus-data-remove
5217            number
5218            (- (gnus-point-at-bol)
5219               (prog1
5220                   (1+ (gnus-point-at-eol))
5221                 (gnus-delete-line))))))
5222       (when old-header
5223         (mail-header-set-number header (mail-header-number old-header)))
5224       (setq gnus-newsgroup-sparse
5225             (delq (setq number (mail-header-number header))
5226                   gnus-newsgroup-sparse))
5227       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5228       (push number gnus-newsgroup-limit)
5229       (gnus-rebuild-thread (mail-header-id header) line)
5230       (gnus-summary-goto-subject number nil t))
5231     (when (and (numberp number)
5232                (> number 0))
5233       ;; We have to update the boundaries even if we can't fetch the
5234       ;; article if ID is a number -- so that the next `P' or `N'
5235       ;; command will fetch the previous (or next) article even
5236       ;; if the one we tried to fetch this time has been canceled.
5237       (when (> number gnus-newsgroup-end)
5238         (setq gnus-newsgroup-end number))
5239       (when (< number gnus-newsgroup-begin)
5240         (setq gnus-newsgroup-begin number))
5241       (setq gnus-newsgroup-unselected
5242             (delq number gnus-newsgroup-unselected)))
5243     ;; Report back a success?
5244     (and header (mail-header-number header))))
5245
5246 ;;; Process/prefix in the summary buffer
5247
5248 (defun gnus-summary-work-articles (n)
5249   "Return a list of articles to be worked upon.
5250 The prefix argument, the list of process marked articles, and the
5251 current article will be taken into consideration."
5252   (save-excursion
5253     (set-buffer gnus-summary-buffer)
5254     (cond
5255      (n
5256       ;; A numerical prefix has been given.
5257       (setq n (prefix-numeric-value n))
5258       (let ((backward (< n 0))
5259             (n (abs (prefix-numeric-value n)))
5260             articles article)
5261         (save-excursion
5262           (while
5263               (and (> n 0)
5264                    (push (setq article (gnus-summary-article-number))
5265                          articles)
5266                    (if backward
5267                        (gnus-summary-find-prev nil article)
5268                      (gnus-summary-find-next nil article)))
5269             (decf n)))
5270         (nreverse articles)))
5271      ((and (gnus-region-active-p) (mark))
5272       (message "region active")
5273       ;; Work on the region between point and mark.
5274       (let ((max (max (point) (mark)))
5275             articles article)
5276         (save-excursion
5277           (goto-char (min (point) (mark)))
5278           (while
5279               (and
5280                (push (setq article (gnus-summary-article-number)) articles)
5281                (gnus-summary-find-next nil article)
5282                (< (point) max)))
5283           (nreverse articles))))
5284      (gnus-newsgroup-processable
5285       ;; There are process-marked articles present.
5286       ;; Save current state.
5287       (gnus-summary-save-process-mark)
5288       ;; Return the list.
5289       (reverse gnus-newsgroup-processable))
5290      (t
5291       ;; Just return the current article.
5292       (list (gnus-summary-article-number))))))
5293
5294 (defmacro gnus-summary-iterate (arg &rest forms)
5295   "Iterate over the process/prefixed articles and do FORMS.
5296 ARG is the interactive prefix given to the command.  FORMS will be
5297 executed with point over the summary line of the articles."
5298   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5299     `(let ((,articles (gnus-summary-work-articles ,arg)))
5300        (while ,articles
5301          (gnus-summary-goto-subject (car ,articles))
5302          ,@forms
5303          (pop ,articles)))))
5304
5305 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5306 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5307
5308 (defun gnus-summary-save-process-mark ()
5309   "Push the current set of process marked articles on the stack."
5310   (interactive)
5311   (push (copy-sequence gnus-newsgroup-processable)
5312         gnus-newsgroup-process-stack))
5313
5314 (defun gnus-summary-kill-process-mark ()
5315   "Push the current set of process marked articles on the stack and unmark."
5316   (interactive)
5317   (gnus-summary-save-process-mark)
5318   (gnus-summary-unmark-all-processable))
5319
5320 (defun gnus-summary-yank-process-mark ()
5321   "Pop the last process mark state off the stack and restore it."
5322   (interactive)
5323   (unless gnus-newsgroup-process-stack
5324     (error "Empty mark stack"))
5325   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5326
5327 (defun gnus-summary-process-mark-set (set)
5328   "Make SET into the current process marked articles."
5329   (gnus-summary-unmark-all-processable)
5330   (while set
5331     (gnus-summary-set-process-mark (pop set))))
5332
5333 ;;; Searching and stuff
5334
5335 (defun gnus-summary-search-group (&optional backward use-level)
5336   "Search for next unread newsgroup.
5337 If optional argument BACKWARD is non-nil, search backward instead."
5338   (save-excursion
5339     (set-buffer gnus-group-buffer)
5340     (when (gnus-group-search-forward
5341            backward nil (if use-level (gnus-group-group-level) nil))
5342       (gnus-group-group-name))))
5343
5344 (defun gnus-summary-best-group (&optional exclude-group)
5345   "Find the name of the best unread group.
5346 If EXCLUDE-GROUP, do not go to this group."
5347   (save-excursion
5348     (set-buffer gnus-group-buffer)
5349     (save-excursion
5350       (gnus-group-best-unread-group exclude-group))))
5351
5352 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5353   (if backward (gnus-summary-find-prev)
5354     (let* ((dummy (gnus-summary-article-intangible-p))
5355            (article (or article (gnus-summary-article-number)))
5356            (arts (gnus-data-find-list article))
5357            result)
5358       (when (and (not dummy)
5359                  (or (not gnus-summary-check-current)
5360                      (not unread)
5361                      (not (gnus-data-unread-p (car arts)))))
5362         (setq arts (cdr arts)))
5363       (when (setq result
5364                   (if unread
5365                       (progn
5366                         (while arts
5367                           (when (or (and undownloaded
5368                                          (eq gnus-undownloaded-mark
5369                                              (gnus-data-mark (car arts))))
5370                                     (gnus-data-unread-p (car arts)))
5371                             (setq result (car arts)
5372                                   arts nil))
5373                           (setq arts (cdr arts)))
5374                         result)
5375                     (car arts)))
5376         (goto-char (gnus-data-pos result))
5377         (gnus-data-number result)))))
5378
5379 (defun gnus-summary-find-prev (&optional unread article)
5380   (let* ((eobp (eobp))
5381          (article (or article (gnus-summary-article-number)))
5382          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5383          result)
5384     (when (and (not eobp)
5385                (or (not gnus-summary-check-current)
5386                    (not unread)
5387                    (not (gnus-data-unread-p (car arts)))))
5388       (setq arts (cdr arts)))
5389     (when (setq result
5390                 (if unread
5391                     (progn
5392                       (while arts
5393                         (when (gnus-data-unread-p (car arts))
5394                           (setq result (car arts)
5395                                 arts nil))
5396                         (setq arts (cdr arts)))
5397                       result)
5398                   (car arts)))
5399       (goto-char (gnus-data-pos result))
5400       (gnus-data-number result))))
5401
5402 (defun gnus-summary-find-subject (subject &optional unread backward article)
5403   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5404          (article (or article (gnus-summary-article-number)))
5405          (articles (gnus-data-list backward))
5406          (arts (gnus-data-find-list article articles))
5407          result)
5408     (when (or (not gnus-summary-check-current)
5409               (not unread)
5410               (not (gnus-data-unread-p (car arts))))
5411       (setq arts (cdr arts)))
5412     (while arts
5413       (and (or (not unread)
5414                (gnus-data-unread-p (car arts)))
5415            (vectorp (gnus-data-header (car arts)))
5416            (gnus-subject-equal
5417             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5418            (setq result (car arts)
5419                  arts nil))
5420       (setq arts (cdr arts)))
5421     (and result
5422          (goto-char (gnus-data-pos result))
5423          (gnus-data-number result))))
5424
5425 (defun gnus-summary-search-forward (&optional unread subject backward)
5426   "Search forward for an article.
5427 If UNREAD, look for unread articles.  If SUBJECT, look for
5428 articles with that subject.  If BACKWARD, search backward instead."
5429   (cond (subject (gnus-summary-find-subject subject unread backward))
5430         (backward (gnus-summary-find-prev unread))
5431         (t (gnus-summary-find-next unread))))
5432
5433 (defun gnus-recenter (&optional n)
5434   "Center point in window and redisplay frame.
5435 Also do horizontal recentering."
5436   (interactive "P")
5437   (when (and gnus-auto-center-summary
5438              (not (eq gnus-auto-center-summary 'vertical)))
5439     (gnus-horizontal-recenter))
5440   (recenter n))
5441
5442 (defun gnus-summary-recenter ()
5443   "Center point in the summary window.
5444 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5445 displayed, no centering will be performed."
5446   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5447   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5448   (interactive)
5449   (let* ((top (cond ((< (window-height) 4) 0)
5450                     ((< (window-height) 7) 1)
5451                     (t (if (numberp gnus-auto-center-summary)
5452                            gnus-auto-center-summary
5453                          2))))
5454          (height (1- (window-height)))
5455          (bottom (save-excursion (goto-char (point-max))
5456                                  (forward-line (- height))
5457                                  (point)))
5458          (window (get-buffer-window (current-buffer))))
5459     ;; The user has to want it.
5460     (when gnus-auto-center-summary
5461       (when (get-buffer-window gnus-article-buffer)
5462         ;; Only do recentering when the article buffer is displayed,
5463         ;; Set the window start to either `bottom', which is the biggest
5464         ;; possible valid number, or the second line from the top,
5465         ;; whichever is the least.
5466         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5467           (if (> bottom top-pos)
5468               ;; Keep the second line from the top visible
5469               (set-window-start window top-pos t)
5470             ;; Try to keep the bottom line visible; if it's partially
5471             ;; obscured, either scroll one more line to make it fully
5472             ;; visible, or revert to using TOP-POS.
5473             (save-excursion
5474               (goto-char (point-max))
5475               (forward-line -1)
5476               (let ((last-line-start (point)))
5477                 (goto-char bottom)
5478                 (set-window-start window (point) t)
5479                 (when (not (pos-visible-in-window-p last-line-start window))
5480                   (forward-line 1)
5481                   (set-window-start window (min (point) top-pos) t)))))))
5482       ;; Do horizontal recentering while we're at it.
5483       (when (and (get-buffer-window (current-buffer) t)
5484                  (not (eq gnus-auto-center-summary 'vertical)))
5485         (let ((selected (selected-window)))
5486           (select-window (get-buffer-window (current-buffer) t))
5487           (gnus-summary-position-point)
5488           (gnus-horizontal-recenter)
5489           (select-window selected))))))
5490
5491 (defun gnus-summary-jump-to-group (newsgroup)
5492   "Move point to NEWSGROUP in group mode buffer."
5493   ;; Keep update point of group mode buffer if visible.
5494   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5495       (save-window-excursion
5496         ;; Take care of tree window mode.
5497         (when (get-buffer-window gnus-group-buffer)
5498           (pop-to-buffer gnus-group-buffer))
5499         (gnus-group-jump-to-group newsgroup))
5500     (save-excursion
5501       ;; Take care of tree window mode.
5502       (if (get-buffer-window gnus-group-buffer)
5503           (pop-to-buffer gnus-group-buffer)
5504         (set-buffer gnus-group-buffer))
5505       (gnus-group-jump-to-group newsgroup))))
5506
5507 ;; This function returns a list of article numbers based on the
5508 ;; difference between the ranges of read articles in this group and
5509 ;; the range of active articles.
5510 (defun gnus-list-of-unread-articles (group)
5511   (let* ((read (gnus-info-read (gnus-get-info group)))
5512          (active (or (gnus-active group) (gnus-activate-group group)))
5513          (last (cdr active))
5514          first nlast unread)
5515     ;; If none are read, then all are unread.
5516     (if (not read)
5517         (setq first (car active))
5518       ;; If the range of read articles is a single range, then the
5519       ;; first unread article is the article after the last read
5520       ;; article.  Sounds logical, doesn't it?
5521       (if (and (not (listp (cdr read)))
5522                (or (< (car read) (car active))
5523                    (progn (setq read (list read))
5524                           nil)))
5525           (setq first (max (car active) (1+ (cdr read))))
5526         ;; `read' is a list of ranges.
5527         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5528                                   (caar read)))
5529                   1)
5530           (setq first (car active)))
5531         (while read
5532           (when first
5533             (while (< first nlast)
5534               (push first unread)
5535               (setq first (1+ first))))
5536           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5537           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5538           (setq read (cdr read)))))
5539     ;; And add the last unread articles.
5540     (while (<= first last)
5541       (push first unread)
5542       (setq first (1+ first)))
5543     ;; Return the list of unread articles.
5544     (delq 0 (nreverse unread))))
5545
5546 (defun gnus-list-of-read-articles (group)
5547   "Return a list of unread, unticked and non-dormant articles."
5548   (let* ((info (gnus-get-info group))
5549          (marked (gnus-info-marks info))
5550          (active (gnus-active group)))
5551     (and info active
5552          (gnus-set-difference
5553           (gnus-sorted-complement
5554            (gnus-uncompress-range active)
5555            (gnus-list-of-unread-articles group))
5556           (append
5557            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5558            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5559
5560 ;; Various summary commands
5561
5562 (defun gnus-summary-select-article-buffer ()
5563   "Reconfigure windows to show article buffer."
5564   (interactive)
5565   (if (not (gnus-buffer-live-p gnus-article-buffer))
5566       (error "There is no article buffer for this summary buffer")
5567     (gnus-configure-windows 'article)
5568     (select-window (get-buffer-window gnus-article-buffer))))
5569
5570 (defun gnus-summary-universal-argument (arg)
5571   "Perform any operation on all articles that are process/prefixed."
5572   (interactive "P")
5573   (let ((articles (gnus-summary-work-articles arg))
5574         func article)
5575     (if (eq
5576          (setq
5577           func
5578           (key-binding
5579            (read-key-sequence
5580             (substitute-command-keys
5581              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5582          'undefined)
5583         (gnus-error 1 "Undefined key")
5584       (save-excursion
5585         (while articles
5586           (gnus-summary-goto-subject (setq article (pop articles)))
5587           (let (gnus-newsgroup-processable)
5588             (command-execute func))
5589           (gnus-summary-remove-process-mark article)))))
5590   (gnus-summary-position-point))
5591
5592 (defun gnus-summary-toggle-truncation (&optional arg)
5593   "Toggle truncation of summary lines.
5594 With arg, turn line truncation on iff arg is positive."
5595   (interactive "P")
5596   (setq truncate-lines
5597         (if (null arg) (not truncate-lines)
5598           (> (prefix-numeric-value arg) 0)))
5599   (redraw-display))
5600
5601 (defun gnus-summary-reselect-current-group (&optional all rescan)
5602   "Exit and then reselect the current newsgroup.
5603 The prefix argument ALL means to select all articles."
5604   (interactive "P")
5605   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5606     (error "Ephemeral groups can't be reselected"))
5607   (let ((current-subject (gnus-summary-article-number))
5608         (group gnus-newsgroup-name))
5609     (setq gnus-newsgroup-begin nil)
5610     (gnus-summary-exit)
5611     ;; We have to adjust the point of group mode buffer because
5612     ;; point was moved to the next unread newsgroup by exiting.
5613     (gnus-summary-jump-to-group group)
5614     (when rescan
5615       (save-excursion
5616         (save-window-excursion
5617           ;; Don't show group contents.
5618           (set-window-start (selected-window) (point-max))
5619           (gnus-group-get-new-news-this-group 1))))
5620     (gnus-group-read-group all t)
5621     (gnus-summary-goto-subject current-subject nil t)))
5622
5623 (defun gnus-summary-rescan-group (&optional all)
5624   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5625   (interactive "P")
5626   (gnus-summary-reselect-current-group all t))
5627
5628 (defun gnus-summary-update-info (&optional non-destructive)
5629   (save-excursion
5630     (let ((group gnus-newsgroup-name))
5631       (when group
5632         (when gnus-newsgroup-kill-headers
5633           (setq gnus-newsgroup-killed
5634                 (gnus-compress-sequence
5635                  (nconc
5636                   (gnus-set-sorted-intersection
5637                    (gnus-uncompress-range gnus-newsgroup-killed)
5638                    (setq gnus-newsgroup-unselected
5639                          (sort gnus-newsgroup-unselected '<)))
5640                   (setq gnus-newsgroup-unreads
5641                         (sort gnus-newsgroup-unreads '<)))
5642                  t)))
5643         (unless (listp (cdr gnus-newsgroup-killed))
5644           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5645         (let ((headers gnus-newsgroup-headers))
5646           ;; Set the new ranges of read articles.
5647           (save-excursion
5648             (set-buffer gnus-group-buffer)
5649             (gnus-undo-force-boundary))
5650           (gnus-update-read-articles
5651            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5652           ;; Set the current article marks.
5653           (let ((gnus-newsgroup-scored
5654                  (if (and (not gnus-save-score)
5655                           (not non-destructive))
5656                      nil
5657                    gnus-newsgroup-scored)))
5658             (save-excursion
5659               (gnus-update-marks)))
5660           ;; Do the cross-ref thing.
5661           (when gnus-use-cross-reference
5662             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5663           ;; Do not switch windows but change the buffer to work.
5664           (set-buffer gnus-group-buffer)
5665           (unless (gnus-ephemeral-group-p group)
5666             (gnus-group-update-group group)))))))
5667
5668 (defun gnus-summary-save-newsrc (&optional force)
5669   "Save the current number of read/marked articles in the dribble buffer.
5670 The dribble buffer will then be saved.
5671 If FORCE (the prefix), also save the .newsrc file(s)."
5672   (interactive "P")
5673   (gnus-summary-update-info t)
5674   (if force
5675       (gnus-save-newsrc-file)
5676     (gnus-dribble-save)))
5677
5678 (defun gnus-summary-exit (&optional temporary)
5679   "Exit reading current newsgroup, and then return to group selection mode.
5680 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5681   (interactive)
5682   (gnus-set-global-variables)
5683   (gnus-kill-save-kill-buffer)
5684   (gnus-async-halt-prefetch)
5685   (let* ((group gnus-newsgroup-name)
5686          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5687          (mode major-mode)
5688          (group-point nil)
5689          (buf (current-buffer)))
5690     (unless quit-config
5691       ;; Do adaptive scoring, and possibly save score files.
5692       (when gnus-newsgroup-adaptive
5693         (gnus-score-adaptive))
5694       (when gnus-use-scoring
5695         (gnus-score-save)))
5696     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5697     ;; If we have several article buffers, we kill them at exit.
5698     (unless gnus-single-article-buffer
5699       (gnus-kill-buffer gnus-original-article-buffer)
5700       (setq gnus-article-current nil))
5701     (when gnus-use-cache
5702       (gnus-cache-possibly-remove-articles)
5703       (gnus-cache-save-buffers))
5704     (gnus-async-prefetch-remove-group group)
5705     (when gnus-suppress-duplicates
5706       (gnus-dup-enter-articles))
5707     (when gnus-use-trees
5708       (gnus-tree-close group))
5709     (when gnus-use-cache
5710       (gnus-cache-write-active))
5711     ;; Remove entries for this group.
5712     (nnmail-purge-split-history (gnus-group-real-name group))
5713     ;; Make all changes in this group permanent.
5714     (unless quit-config
5715       (gnus-run-hooks 'gnus-exit-group-hook)
5716       (gnus-summary-update-info))
5717     (gnus-close-group group)
5718     ;; Make sure where we were, and go to next newsgroup.
5719     (set-buffer gnus-group-buffer)
5720     (unless quit-config
5721       (gnus-group-jump-to-group group))
5722     (gnus-run-hooks 'gnus-summary-exit-hook)
5723     (unless (or quit-config
5724                 ;; If this group has disappeared from the summary
5725                 ;; buffer, don't skip forwards.
5726                 (not (string= group (gnus-group-group-name))))
5727       (gnus-group-next-unread-group 1))
5728     (setq group-point (point))
5729     (if temporary
5730         nil                             ;Nothing to do.
5731       ;; If we have several article buffers, we kill them at exit.
5732       (unless gnus-single-article-buffer
5733         (gnus-kill-buffer gnus-article-buffer)
5734         (gnus-kill-buffer gnus-original-article-buffer)
5735         (setq gnus-article-current nil))
5736       (set-buffer buf)
5737       (if (not gnus-kill-summary-on-exit)
5738           (gnus-deaden-summary)
5739         ;; We set all buffer-local variables to nil.  It is unclear why
5740         ;; this is needed, but if we don't, buffer-local variables are
5741         ;; not garbage-collected, it seems.  This would the lead to en
5742         ;; ever-growing Emacs.
5743         (gnus-summary-clear-local-variables)
5744         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5745           (gnus-summary-clear-local-variables))
5746         (when (get-buffer gnus-article-buffer)
5747           (bury-buffer gnus-article-buffer))
5748         ;; We clear the global counterparts of the buffer-local
5749         ;; variables as well, just to be on the safe side.
5750         (set-buffer gnus-group-buffer)
5751         (gnus-summary-clear-local-variables)
5752         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5753           (gnus-summary-clear-local-variables))
5754         ;; Return to group mode buffer.
5755         (when (eq mode 'gnus-summary-mode)
5756           (gnus-kill-buffer buf)))
5757       (setq gnus-current-select-method gnus-select-method)
5758       (pop-to-buffer gnus-group-buffer)
5759       (if (not quit-config)
5760           (progn
5761             (goto-char group-point)
5762             (gnus-configure-windows 'group 'force)
5763             (unless (pos-visible-in-window-p)
5764               (forward-line (/ (static-if (featurep 'xemacs)
5765                                    (window-displayed-height)
5766                                  (1- (window-height)))
5767                                -2))
5768               (set-window-start (selected-window) (point))
5769               (goto-char group-point)))
5770         (gnus-handle-ephemeral-exit quit-config))
5771       ;; Clear the current group name.
5772       (unless quit-config
5773         (setq gnus-newsgroup-name nil)))))
5774
5775 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5776 (defun gnus-summary-exit-no-update (&optional no-questions)
5777   "Quit reading current newsgroup without updating read article info."
5778   (interactive)
5779   (let* ((group gnus-newsgroup-name)
5780          (quit-config (gnus-group-quit-config group)))
5781     (when (or no-questions
5782               gnus-expert-user
5783               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5784       (gnus-async-halt-prefetch)
5785       (mapcar 'funcall
5786               (delq 'gnus-summary-expire-articles
5787                     (copy-sequence gnus-summary-prepare-exit-hook)))
5788       ;; If we have several article buffers, we kill them at exit.
5789       (unless gnus-single-article-buffer
5790         (gnus-kill-buffer gnus-article-buffer)
5791         (gnus-kill-buffer gnus-original-article-buffer)
5792         (setq gnus-article-current nil))
5793       (if (not gnus-kill-summary-on-exit)
5794           (gnus-deaden-summary)
5795         (gnus-close-group group)
5796         (gnus-summary-clear-local-variables)
5797         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5798           (gnus-summary-clear-local-variables))
5799         (set-buffer gnus-group-buffer)
5800         (gnus-summary-clear-local-variables)
5801         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5802           (gnus-summary-clear-local-variables))
5803         (when (get-buffer gnus-summary-buffer)
5804           (kill-buffer gnus-summary-buffer)))
5805       (unless gnus-single-article-buffer
5806         (setq gnus-article-current nil))
5807       (when gnus-use-trees
5808         (gnus-tree-close group))
5809       (gnus-async-prefetch-remove-group group)
5810       (when (get-buffer gnus-article-buffer)
5811         (bury-buffer gnus-article-buffer))
5812       ;; Return to the group buffer.
5813       (gnus-configure-windows 'group 'force)
5814       ;; Clear the current group name.
5815       (setq gnus-newsgroup-name nil)
5816       (when (equal (gnus-group-group-name) group)
5817         (gnus-group-next-unread-group 1))
5818       (when quit-config
5819         (gnus-handle-ephemeral-exit quit-config)))))
5820
5821 (defun gnus-handle-ephemeral-exit (quit-config)
5822   "Handle movement when leaving an ephemeral group.
5823 The state which existed when entering the ephemeral is reset."
5824   (if (not (buffer-name (car quit-config)))
5825       (gnus-configure-windows 'group 'force)
5826     (set-buffer (car quit-config))
5827     (cond ((eq major-mode 'gnus-summary-mode)
5828            (gnus-set-global-variables))
5829           ((eq major-mode 'gnus-article-mode)
5830            (save-excursion
5831              ;; The `gnus-summary-buffer' variable may point
5832              ;; to the old summary buffer when using a single
5833              ;; article buffer.
5834              (unless (gnus-buffer-live-p gnus-summary-buffer)
5835                (set-buffer gnus-group-buffer))
5836              (set-buffer gnus-summary-buffer)
5837              (gnus-set-global-variables))))
5838     (if (or (eq (cdr quit-config) 'article)
5839             (eq (cdr quit-config) 'pick))
5840         (progn
5841           ;; The current article may be from the ephemeral group
5842           ;; thus it is best that we reload this article
5843           (gnus-summary-show-article)
5844           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5845               (gnus-configure-windows 'pick 'force)
5846             (gnus-configure-windows (cdr quit-config) 'force)))
5847       (gnus-configure-windows (cdr quit-config) 'force))
5848     (when (eq major-mode 'gnus-summary-mode)
5849       (gnus-summary-next-subject 1 nil t)
5850       (gnus-summary-recenter)
5851       (gnus-summary-position-point))))
5852
5853 (defun gnus-summary-preview-mime-message ()
5854   "MIME decode and play this message."
5855   (interactive)
5856   (let ((gnus-break-pages nil)
5857         (gnus-show-mime t))
5858     (gnus-summary-select-article gnus-show-all-headers t))
5859   (select-window (get-buffer-window gnus-article-buffer)))
5860
5861 ;;; Dead summaries.
5862
5863 (defvar gnus-dead-summary-mode-map nil)
5864
5865 (unless gnus-dead-summary-mode-map
5866   (setq gnus-dead-summary-mode-map (make-keymap))
5867   (suppress-keymap gnus-dead-summary-mode-map)
5868   (substitute-key-definition
5869    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5870   (let ((keys '("\C-d" "\r" "\177" [delete])))
5871     (while keys
5872       (define-key gnus-dead-summary-mode-map
5873         (pop keys) 'gnus-summary-wake-up-the-dead))))
5874
5875 (defvar gnus-dead-summary-mode nil
5876   "Minor mode for Gnus summary buffers.")
5877
5878 (defun gnus-dead-summary-mode (&optional arg)
5879   "Minor mode for Gnus summary buffers."
5880   (interactive "P")
5881   (when (eq major-mode 'gnus-summary-mode)
5882     (make-local-variable 'gnus-dead-summary-mode)
5883     (setq gnus-dead-summary-mode
5884           (if (null arg) (not gnus-dead-summary-mode)
5885             (> (prefix-numeric-value arg) 0)))
5886     (when gnus-dead-summary-mode
5887       (gnus-add-minor-mode
5888        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5889
5890 (defun gnus-deaden-summary ()
5891   "Make the current summary buffer into a dead summary buffer."
5892   ;; Kill any previous dead summary buffer.
5893   (when (and gnus-dead-summary
5894              (buffer-name gnus-dead-summary))
5895     (save-excursion
5896       (set-buffer gnus-dead-summary)
5897       (when gnus-dead-summary-mode
5898         (kill-buffer (current-buffer)))))
5899   ;; Make this the current dead summary.
5900   (setq gnus-dead-summary (current-buffer))
5901   (gnus-dead-summary-mode 1)
5902   (let ((name (buffer-name)))
5903     (when (string-match "Summary" name)
5904       (rename-buffer
5905        (concat (substring name 0 (match-beginning 0)) "Dead "
5906                (substring name (match-beginning 0)))
5907        t)
5908       (bury-buffer))))
5909
5910 (defun gnus-kill-or-deaden-summary (buffer)
5911   "Kill or deaden the summary BUFFER."
5912   (save-excursion
5913     (when (and (buffer-name buffer)
5914                (not gnus-single-article-buffer))
5915       (save-excursion
5916         (set-buffer buffer)
5917         (gnus-kill-buffer gnus-article-buffer)
5918         (gnus-kill-buffer gnus-original-article-buffer)))
5919     (cond (gnus-kill-summary-on-exit
5920            (when (and gnus-use-trees
5921                       (gnus-buffer-exists-p buffer))
5922              (save-excursion
5923                (set-buffer buffer)
5924                (gnus-tree-close gnus-newsgroup-name)))
5925            (gnus-kill-buffer buffer))
5926           ((gnus-buffer-exists-p buffer)
5927            (save-excursion
5928              (set-buffer buffer)
5929              (gnus-deaden-summary))))))
5930
5931 (defun gnus-summary-wake-up-the-dead (&rest args)
5932   "Wake up the dead summary buffer."
5933   (interactive)
5934   (gnus-dead-summary-mode -1)
5935   (let ((name (buffer-name)))
5936     (when (string-match "Dead " name)
5937       (rename-buffer
5938        (concat (substring name 0 (match-beginning 0))
5939                (substring name (match-end 0)))
5940        t)))
5941   (gnus-message 3 "This dead summary is now alive again"))
5942
5943 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5944 (defun gnus-summary-fetch-faq (&optional faq-dir)
5945   "Fetch the FAQ for the current group.
5946 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5947 in."
5948   (interactive
5949    (list
5950     (when current-prefix-arg
5951       (completing-read
5952        "Faq dir: " (and (listp gnus-group-faq-directory)
5953                         (mapcar (lambda (file) (list file))
5954                                 gnus-group-faq-directory))))))
5955   (let (gnus-faq-buffer)
5956     (when (setq gnus-faq-buffer
5957                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5958       (gnus-configure-windows 'summary-faq))))
5959
5960 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5961 (defun gnus-summary-describe-group (&optional force)
5962   "Describe the current newsgroup."
5963   (interactive "P")
5964   (gnus-group-describe-group force gnus-newsgroup-name))
5965
5966 (defun gnus-summary-describe-briefly ()
5967   "Describe summary mode commands briefly."
5968   (interactive)
5969   (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")))
5970
5971 ;; Walking around group mode buffer from summary mode.
5972
5973 (defun gnus-summary-next-group (&optional no-article target-group backward)
5974   "Exit current newsgroup and then select next unread newsgroup.
5975 If prefix argument NO-ARTICLE is non-nil, no article is selected
5976 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5977 previous group instead."
5978   (interactive "P")
5979   ;; Stop pre-fetching.
5980   (gnus-async-halt-prefetch)
5981   (let ((current-group gnus-newsgroup-name)
5982         (current-buffer (current-buffer))
5983         entered)
5984     ;; First we semi-exit this group to update Xrefs and all variables.
5985     ;; We can't do a real exit, because the window conf must remain
5986     ;; the same in case the user is prompted for info, and we don't
5987     ;; want the window conf to change before that...
5988     (gnus-summary-exit t)
5989     (while (not entered)
5990       ;; Then we find what group we are supposed to enter.
5991       (set-buffer gnus-group-buffer)
5992       (gnus-group-jump-to-group current-group)
5993       (setq target-group
5994             (or target-group
5995                 (if (eq gnus-keep-same-level 'best)
5996                     (gnus-summary-best-group gnus-newsgroup-name)
5997                   (gnus-summary-search-group backward gnus-keep-same-level))))
5998       (if (not target-group)
5999           ;; There are no further groups, so we return to the group
6000           ;; buffer.
6001           (progn
6002             (gnus-message 5 "Returning to the group buffer")
6003             (setq entered t)
6004             (when (gnus-buffer-live-p current-buffer)
6005               (set-buffer current-buffer)
6006               (gnus-summary-exit))
6007             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6008         ;; We try to enter the target group.
6009         (gnus-group-jump-to-group target-group)
6010         (let ((unreads (gnus-group-group-unread)))
6011           (if (and (or (eq t unreads)
6012                        (and unreads (not (zerop unreads))))
6013                    (gnus-summary-read-group
6014                     target-group nil no-article
6015                     (and (buffer-name current-buffer) current-buffer)
6016                     nil backward))
6017               (setq entered t)
6018             (setq current-group target-group
6019                   target-group nil)))))))
6020
6021 (defun gnus-summary-prev-group (&optional no-article)
6022   "Exit current newsgroup and then select previous unread newsgroup.
6023 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6024   (interactive "P")
6025   (gnus-summary-next-group no-article nil t))
6026
6027 ;; Walking around summary lines.
6028
6029 (defun gnus-summary-first-subject (&optional unread undownloaded)
6030   "Go to the first unread subject.
6031 If UNREAD is non-nil, go to the first unread article.
6032 Returns the article selected or nil if there are no unread articles."
6033   (interactive "P")
6034   (prog1
6035       (cond
6036        ;; Empty summary.
6037        ((null gnus-newsgroup-data)
6038         (gnus-message 3 "No articles in the group")
6039         nil)
6040        ;; Pick the first article.
6041        ((not unread)
6042         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6043         (gnus-data-number (car gnus-newsgroup-data)))
6044        ;; No unread articles.
6045        ((null gnus-newsgroup-unreads)
6046         (gnus-message 3 "No more unread articles")
6047         nil)
6048        ;; Find the first unread article.
6049        (t
6050         (let ((data gnus-newsgroup-data))
6051           (while (and data
6052                       (and (not (and undownloaded
6053                                      (eq gnus-undownloaded-mark
6054                                          (gnus-data-mark (car data)))))
6055                            (not (gnus-data-unread-p (car data)))))
6056             (setq data (cdr data)))
6057           (when data
6058             (goto-char (gnus-data-pos (car data)))
6059             (gnus-data-number (car data))))))
6060     (gnus-summary-position-point)))
6061
6062 (defun gnus-summary-next-subject (n &optional unread dont-display)
6063   "Go to next N'th summary line.
6064 If N is negative, go to the previous N'th subject line.
6065 If UNREAD is non-nil, only unread articles are selected.
6066 The difference between N and the actual number of steps taken is
6067 returned."
6068   (interactive "p")
6069   (let ((backward (< n 0))
6070         (n (abs n)))
6071     (while (and (> n 0)
6072                 (if backward
6073                     (gnus-summary-find-prev unread)
6074                   (gnus-summary-find-next unread)))
6075       (unless (zerop (setq n (1- n)))
6076         (gnus-summary-show-thread)))
6077     (when (/= 0 n)
6078       (gnus-message 7 "No more%s articles"
6079                     (if unread " unread" "")))
6080     (unless dont-display
6081       (gnus-summary-recenter)
6082       (gnus-summary-position-point))
6083     n))
6084
6085 (defun gnus-summary-next-unread-subject (n)
6086   "Go to next N'th unread summary line."
6087   (interactive "p")
6088   (gnus-summary-next-subject n t))
6089
6090 (defun gnus-summary-prev-subject (n &optional unread)
6091   "Go to previous N'th summary line.
6092 If optional argument UNREAD is non-nil, only unread article is selected."
6093   (interactive "p")
6094   (gnus-summary-next-subject (- n) unread))
6095
6096 (defun gnus-summary-prev-unread-subject (n)
6097   "Go to previous N'th unread summary line."
6098   (interactive "p")
6099   (gnus-summary-next-subject (- n) t))
6100
6101 (defun gnus-summary-goto-subject (article &optional force silent)
6102   "Go the subject line of ARTICLE.
6103 If FORCE, also allow jumping to articles not currently shown."
6104   (interactive "nArticle number: ")
6105   (let ((b (point))
6106         (data (gnus-data-find article)))
6107     ;; We read in the article if we have to.
6108     (and (not data)
6109          force
6110          (gnus-summary-insert-subject
6111           article
6112           (if (or (numberp force) (vectorp force)) force)
6113           t)
6114          (setq data (gnus-data-find article)))
6115     (goto-char b)
6116     (if (not data)
6117         (progn
6118           (unless silent
6119             (gnus-message 3 "Can't find article %d" article))
6120           nil)
6121       (goto-char (gnus-data-pos data))
6122       (gnus-summary-position-point)
6123       article)))
6124
6125 ;; Walking around summary lines with displaying articles.
6126
6127 (defun gnus-summary-expand-window (&optional arg)
6128   "Make the summary buffer take up the entire Emacs frame.
6129 Given a prefix, will force an `article' buffer configuration."
6130   (interactive "P")
6131   (if arg
6132       (gnus-configure-windows 'article 'force)
6133     (gnus-configure-windows 'summary 'force)))
6134
6135 (defun gnus-summary-display-article (article &optional all-header)
6136   "Display ARTICLE in article buffer."
6137   (when (gnus-buffer-live-p gnus-article-buffer)
6138     (with-current-buffer gnus-article-buffer
6139       (set-buffer-multibyte t)))
6140   (gnus-set-global-variables)
6141   (when (gnus-buffer-live-p gnus-article-buffer)
6142     (with-current-buffer gnus-article-buffer
6143       (setq gnus-article-charset gnus-newsgroup-charset)
6144       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6145   (if (null article)
6146       nil
6147     (prog1
6148         (if gnus-summary-display-article-function
6149             (funcall gnus-summary-display-article-function article all-header)
6150           (gnus-article-prepare article all-header))
6151       (with-current-buffer gnus-article-buffer
6152         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6153              nil))
6154       (gnus-run-hooks 'gnus-select-article-hook)
6155       (when (and gnus-current-article
6156                  (not (zerop gnus-current-article)))
6157         (gnus-summary-goto-subject gnus-current-article))
6158       (gnus-summary-recenter)
6159       (when (and gnus-use-trees gnus-show-threads)
6160         (gnus-possibly-generate-tree article)
6161         (gnus-highlight-selected-tree article))
6162       ;; Successfully display article.
6163       (gnus-article-set-window-start
6164        (cdr (assq article gnus-newsgroup-bookmarks))))))
6165
6166 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6167   "Select the current article.
6168 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6169 non-nil, the article will be re-fetched even if it already present in
6170 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6171 be displayed."
6172   ;; Make sure we are in the summary buffer to work around bbdb bug.
6173   (unless (eq major-mode 'gnus-summary-mode)
6174     (set-buffer gnus-summary-buffer))
6175   (let ((article (or article (gnus-summary-article-number)))
6176         (all-headers (not (not all-headers))) ;Must be T or NIL.
6177         gnus-summary-display-article-function)
6178     (and (not pseudo)
6179          (gnus-summary-article-pseudo-p article)
6180          (error "This is a pseudo-article"))
6181     (save-excursion
6182       (set-buffer gnus-summary-buffer)
6183       (if (or (and gnus-single-article-buffer
6184                    (or (null gnus-current-article)
6185                        (null gnus-article-current)
6186                        (null (get-buffer gnus-article-buffer))
6187                        (not (eq article (cdr gnus-article-current)))
6188                        (not (equal (car gnus-article-current)
6189                                    gnus-newsgroup-name))))
6190               (and (not gnus-single-article-buffer)
6191                    (or (null gnus-current-article)
6192                        (not (eq gnus-current-article article))))
6193               force)
6194           ;; The requested article is different from the current article.
6195           (progn
6196             (gnus-summary-display-article article all-headers)
6197             (when (or all-headers gnus-show-all-headers)
6198               (gnus-article-show-all-headers))
6199             (gnus-article-set-window-start
6200              (cdr (assq article gnus-newsgroup-bookmarks)))
6201             article)
6202         (when (or all-headers gnus-show-all-headers)
6203           (gnus-article-show-all-headers))
6204         'old))))
6205
6206 (defun gnus-summary-force-verify-and-decrypt ()
6207   (interactive)
6208   (let ((mm-verify-option 'known)
6209         (mm-decrypt-option 'known))
6210     (gnus-summary-select-article nil 'force)))
6211
6212 (defun gnus-summary-set-current-mark (&optional current-mark)
6213   "Obsolete function."
6214   nil)
6215
6216 (defun gnus-summary-next-article (&optional unread subject backward push)
6217   "Select the next article.
6218 If UNREAD, only unread articles are selected.
6219 If SUBJECT, only articles with SUBJECT are selected.
6220 If BACKWARD, the previous article is selected instead of the next."
6221   (interactive "P")
6222   (cond
6223    ;; Is there such an article?
6224    ((and (gnus-summary-search-forward unread subject backward)
6225          (or (gnus-summary-display-article (gnus-summary-article-number))
6226              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6227     (gnus-summary-position-point))
6228    ;; If not, we try the first unread, if that is wanted.
6229    ((and subject
6230          gnus-auto-select-same
6231          (gnus-summary-first-unread-article))
6232     (gnus-summary-position-point)
6233     (gnus-message 6 "Wrapped"))
6234    ;; Try to get next/previous article not displayed in this group.
6235    ((and gnus-auto-extend-newsgroup
6236          (not unread) (not subject))
6237     (gnus-summary-goto-article
6238      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6239      nil (count-lines (point-min) (point))))
6240    ;; Go to next/previous group.
6241    (t
6242     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6243       (gnus-summary-jump-to-group gnus-newsgroup-name))
6244     (let ((cmd last-command-char)
6245           (point
6246            (save-excursion
6247              (set-buffer gnus-group-buffer)
6248              (point)))
6249           (group
6250            (if (eq gnus-keep-same-level 'best)
6251                (gnus-summary-best-group gnus-newsgroup-name)
6252              (gnus-summary-search-group backward gnus-keep-same-level))))
6253       ;; For some reason, the group window gets selected.  We change
6254       ;; it back.
6255       (select-window (get-buffer-window (current-buffer)))
6256       ;; Select next unread newsgroup automagically.
6257       (cond
6258        ((or (not gnus-auto-select-next)
6259             (not cmd))
6260         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6261        ((or (eq gnus-auto-select-next 'quietly)
6262             (and (eq gnus-auto-select-next 'slightly-quietly)
6263                  push)
6264             (and (eq gnus-auto-select-next 'almost-quietly)
6265                  (gnus-summary-last-article-p)))
6266         ;; Select quietly.
6267         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6268             (gnus-summary-exit)
6269           (gnus-message 7 "No more%s articles (%s)..."
6270                         (if unread " unread" "")
6271                         (if group (concat "selecting " group)
6272                           "exiting"))
6273           (gnus-summary-next-group nil group backward)))
6274        (t
6275         (when (gnus-key-press-event-p last-input-event)
6276           (gnus-summary-walk-group-buffer
6277            gnus-newsgroup-name cmd unread backward point))))))))
6278
6279 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6280   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6281                       (?\C-p (gnus-group-prev-unread-group 1))))
6282         (cursor-in-echo-area t)
6283         keve key group ended)
6284     (save-excursion
6285       (set-buffer gnus-group-buffer)
6286       (goto-char start)
6287       (setq group
6288             (if (eq gnus-keep-same-level 'best)
6289                 (gnus-summary-best-group gnus-newsgroup-name)
6290               (gnus-summary-search-group backward gnus-keep-same-level))))
6291     (while (not ended)
6292       (gnus-message
6293        5 "No more%s articles%s" (if unread " unread" "")
6294        (if (and group
6295                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6296            (format " (Type %s for %s [%s])"
6297                    (single-key-description cmd) group
6298                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6299          (format " (Type %s to exit %s)"
6300                  (single-key-description cmd)
6301                  gnus-newsgroup-name)))
6302       ;; Confirm auto selection.
6303       (setq key (car (setq keve (gnus-read-event-char))))
6304       (setq ended t)
6305       (cond
6306        ((assq key keystrokes)
6307         (let ((obuf (current-buffer)))
6308           (switch-to-buffer gnus-group-buffer)
6309           (when group
6310             (gnus-group-jump-to-group group))
6311           (eval (cadr (assq key keystrokes)))
6312           (setq group (gnus-group-group-name))
6313           (switch-to-buffer obuf))
6314         (setq ended nil))
6315        ((equal key cmd)
6316         (if (or (not group)
6317                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6318             (gnus-summary-exit)
6319           (gnus-summary-next-group nil group backward)))
6320        (t
6321         (push (cdr keve) unread-command-events))))))
6322
6323 (defun gnus-summary-next-unread-article ()
6324   "Select unread article after current one."
6325   (interactive)
6326   (gnus-summary-next-article
6327    (or (not (eq gnus-summary-goto-unread 'never))
6328        (gnus-summary-last-article-p (gnus-summary-article-number)))
6329    (and gnus-auto-select-same
6330         (gnus-summary-article-subject))))
6331
6332 (defun gnus-summary-prev-article (&optional unread subject)
6333   "Select the article after the current one.
6334 If UNREAD is non-nil, only unread articles are selected."
6335   (interactive "P")
6336   (gnus-summary-next-article unread subject t))
6337
6338 (defun gnus-summary-prev-unread-article ()
6339   "Select unread article before current one."
6340   (interactive)
6341   (gnus-summary-prev-article
6342    (or (not (eq gnus-summary-goto-unread 'never))
6343        (gnus-summary-first-article-p (gnus-summary-article-number)))
6344    (and gnus-auto-select-same
6345         (gnus-summary-article-subject))))
6346
6347 (defun gnus-summary-next-page (&optional lines circular)
6348   "Show next page of the selected article.
6349 If at the end of the current article, select the next article.
6350 LINES says how many lines should be scrolled up.
6351
6352 If CIRCULAR is non-nil, go to the start of the article instead of
6353 selecting the next article when reaching the end of the current
6354 article."
6355   (interactive "P")
6356   (setq gnus-summary-buffer (current-buffer))
6357   (gnus-set-global-variables)
6358   (let ((article (gnus-summary-article-number))
6359         (article-window (get-buffer-window gnus-article-buffer t))
6360         endp)
6361     ;; If the buffer is empty, we have no article.
6362     (unless article
6363       (error "No article to select"))
6364     (gnus-configure-windows 'article)
6365     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6366         (if (and (eq gnus-summary-goto-unread 'never)
6367                  (not (gnus-summary-last-article-p article)))
6368             (gnus-summary-next-article)
6369           (gnus-summary-next-unread-article))
6370       (if (or (null gnus-current-article)
6371               (null gnus-article-current)
6372               (/= article (cdr gnus-article-current))
6373               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6374           ;; Selected subject is different from current article's.
6375           (gnus-summary-display-article article)
6376         (when article-window
6377           (gnus-eval-in-buffer-window gnus-article-buffer
6378             (setq endp (gnus-article-next-page lines)))
6379           (when endp
6380             (cond (circular
6381                    (gnus-summary-beginning-of-article))
6382                   (lines
6383                    (gnus-message 3 "End of message"))
6384                   ((null lines)
6385                    (if (and (eq gnus-summary-goto-unread 'never)
6386                             (not (gnus-summary-last-article-p article)))
6387                        (gnus-summary-next-article)
6388                      (gnus-summary-next-unread-article))))))))
6389     (gnus-summary-recenter)
6390     (gnus-summary-position-point)))
6391
6392 (defun gnus-summary-prev-page (&optional lines move)
6393   "Show previous page of selected article.
6394 Argument LINES specifies lines to be scrolled down.
6395 If MOVE, move to the previous unread article if point is at
6396 the beginning of the buffer."
6397   (interactive "P")
6398   (let ((article (gnus-summary-article-number))
6399         (article-window (get-buffer-window gnus-article-buffer t))
6400         endp)
6401     (gnus-configure-windows 'article)
6402     (if (or (null gnus-current-article)
6403             (null gnus-article-current)
6404             (/= article (cdr gnus-article-current))
6405             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6406         ;; Selected subject is different from current article's.
6407         (gnus-summary-display-article article)
6408       (gnus-summary-recenter)
6409       (when article-window
6410         (gnus-eval-in-buffer-window gnus-article-buffer
6411           (setq endp (gnus-article-prev-page lines)))
6412         (when (and move endp)
6413           (cond (lines
6414                  (gnus-message 3 "Beginning of message"))
6415                 ((null lines)
6416                  (if (and (eq gnus-summary-goto-unread 'never)
6417                           (not (gnus-summary-first-article-p article)))
6418                      (gnus-summary-prev-article)
6419                    (gnus-summary-prev-unread-article))))))))
6420   (gnus-summary-position-point))
6421
6422 (defun gnus-summary-prev-page-or-article (&optional lines)
6423   "Show previous page of selected article.
6424 Argument LINES specifies lines to be scrolled down.
6425 If at the beginning of the article, go to the next article."
6426   (interactive "P")
6427   (gnus-summary-prev-page lines t))
6428
6429 (defun gnus-summary-scroll-up (lines)
6430   "Scroll up (or down) one line current article.
6431 Argument LINES specifies lines to be scrolled up (or down if negative)."
6432   (interactive "p")
6433   (gnus-configure-windows 'article)
6434   (gnus-summary-show-thread)
6435   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6436     (gnus-eval-in-buffer-window gnus-article-buffer
6437       (cond ((> lines 0)
6438              (when (gnus-article-next-page lines)
6439                (gnus-message 3 "End of message")))
6440             ((< lines 0)
6441              (gnus-article-prev-page (- lines))))))
6442   (gnus-summary-recenter)
6443   (gnus-summary-position-point))
6444
6445 (defun gnus-summary-scroll-down (lines)
6446   "Scroll down (or up) one line current article.
6447 Argument LINES specifies lines to be scrolled down (or up if negative)."
6448   (interactive "p")
6449   (gnus-summary-scroll-up (- lines)))
6450
6451 (defun gnus-summary-next-same-subject ()
6452   "Select next article which has the same subject as current one."
6453   (interactive)
6454   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6455
6456 (defun gnus-summary-prev-same-subject ()
6457   "Select previous article which has the same subject as current one."
6458   (interactive)
6459   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6460
6461 (defun gnus-summary-next-unread-same-subject ()
6462   "Select next unread article which has the same subject as current one."
6463   (interactive)
6464   (gnus-summary-next-article t (gnus-summary-article-subject)))
6465
6466 (defun gnus-summary-prev-unread-same-subject ()
6467   "Select previous unread article which has the same subject as current one."
6468   (interactive)
6469   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6470
6471 (defun gnus-summary-first-unread-article ()
6472   "Select the first unread article.
6473 Return nil if there are no unread articles."
6474   (interactive)
6475   (prog1
6476       (when (gnus-summary-first-subject t)
6477         (gnus-summary-show-thread)
6478         (gnus-summary-first-subject t)
6479         (gnus-summary-display-article (gnus-summary-article-number)))
6480     (gnus-summary-position-point)))
6481
6482 (defun gnus-summary-first-unread-subject ()
6483   "Place the point on the subject line of the first unread article.
6484 Return nil if there are no unread articles."
6485   (interactive)
6486   (prog1
6487       (when (gnus-summary-first-subject t)
6488         (gnus-summary-show-thread)
6489         (gnus-summary-first-subject t))
6490     (gnus-summary-position-point)))
6491
6492 (defun gnus-summary-first-article ()
6493   "Select the first article.
6494 Return nil if there are no articles."
6495   (interactive)
6496   (prog1
6497       (when (gnus-summary-first-subject)
6498         (gnus-summary-show-thread)
6499         (gnus-summary-first-subject)
6500         (gnus-summary-display-article (gnus-summary-article-number)))
6501     (gnus-summary-position-point)))
6502
6503 (defun gnus-summary-best-unread-article ()
6504   "Select the unread article with the highest score."
6505   (interactive)
6506   (let ((best -1000000)
6507         (data gnus-newsgroup-data)
6508         article score)
6509     (while data
6510       (and (gnus-data-unread-p (car data))
6511            (> (setq score
6512                     (gnus-summary-article-score (gnus-data-number (car data))))
6513               best)
6514            (setq best score
6515                  article (gnus-data-number (car data))))
6516       (setq data (cdr data)))
6517     (prog1
6518         (if article
6519             (gnus-summary-goto-article article)
6520           (error "No unread articles"))
6521       (gnus-summary-position-point))))
6522
6523 (defun gnus-summary-last-subject ()
6524   "Go to the last displayed subject line in the group."
6525   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6526     (when article
6527       (gnus-summary-goto-subject article))))
6528
6529 (defun gnus-summary-goto-article (article &optional all-headers force)
6530   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6531 If ALL-HEADERS is non-nil, no header lines are hidden.
6532 If FORCE, go to the article even if it isn't displayed.  If FORCE
6533 is a number, it is the line the article is to be displayed on."
6534   (interactive
6535    (list
6536     (completing-read
6537      "Article number or Message-ID: "
6538      (mapcar (lambda (number) (list (int-to-string number)))
6539              gnus-newsgroup-limit))
6540     current-prefix-arg
6541     t))
6542   (prog1
6543       (if (and (stringp article)
6544                (string-match "@" article))
6545           (gnus-summary-refer-article article)
6546         (when (stringp article)
6547           (setq article (string-to-number article)))
6548         (if (gnus-summary-goto-subject article force)
6549             (gnus-summary-display-article article all-headers)
6550           (gnus-message 4 "Couldn't go to article %s" article) nil))
6551     (gnus-summary-position-point)))
6552
6553 (defun gnus-summary-goto-last-article ()
6554   "Go to the previously read article."
6555   (interactive)
6556   (prog1
6557       (when gnus-last-article
6558         (gnus-summary-goto-article gnus-last-article nil t))
6559     (gnus-summary-position-point)))
6560
6561 (defun gnus-summary-pop-article (number)
6562   "Pop one article off the history and go to the previous.
6563 NUMBER articles will be popped off."
6564   (interactive "p")
6565   (let (to)
6566     (setq gnus-newsgroup-history
6567           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6568     (if to
6569         (gnus-summary-goto-article (car to) nil t)
6570       (error "Article history empty")))
6571   (gnus-summary-position-point))
6572
6573 ;; Summary commands and functions for limiting the summary buffer.
6574
6575 (defun gnus-summary-limit-to-articles (n)
6576   "Limit the summary buffer to the next N articles.
6577 If not given a prefix, use the process marked articles instead."
6578   (interactive "P")
6579   (prog1
6580       (let ((articles (gnus-summary-work-articles n)))
6581         (setq gnus-newsgroup-processable nil)
6582         (gnus-summary-limit articles))
6583     (gnus-summary-position-point)))
6584
6585 (defun gnus-summary-pop-limit (&optional total)
6586   "Restore the previous limit.
6587 If given a prefix, remove all limits."
6588   (interactive "P")
6589   (when total
6590     (setq gnus-newsgroup-limits
6591           (list (mapcar (lambda (h) (mail-header-number h))
6592                         gnus-newsgroup-headers))))
6593   (unless gnus-newsgroup-limits
6594     (error "No limit to pop"))
6595   (prog1
6596       (gnus-summary-limit nil 'pop)
6597     (gnus-summary-position-point)))
6598
6599 (defun gnus-summary-limit-to-subject (subject &optional header)
6600   "Limit the summary buffer to articles that have subjects that match a regexp."
6601   (interactive "sLimit to subject (regexp): ")
6602   (unless header
6603     (setq header "subject"))
6604   (when (not (equal "" subject))
6605     (prog1
6606         (let ((articles (gnus-summary-find-matching
6607                          (or header "subject") subject 'all)))
6608           (unless articles
6609             (error "Found no matches for \"%s\"" subject))
6610           (gnus-summary-limit articles))
6611       (gnus-summary-position-point))))
6612
6613 (defun gnus-summary-limit-to-author (from)
6614   "Limit the summary buffer to articles that have authors that match a regexp."
6615   (interactive "sLimit to author (regexp): ")
6616   (gnus-summary-limit-to-subject from "from"))
6617
6618 (defun gnus-summary-limit-to-age (age &optional younger-p)
6619   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6620 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6621 articles that are younger than AGE days."
6622   (interactive
6623    (let ((younger current-prefix-arg)
6624          (days-got nil)
6625          days)
6626      (while (not days-got)
6627        (setq days (if younger
6628                       (read-string "Limit to articles within (in days): ")
6629                     (read-string "Limit to articles older than (in days): ")))
6630        (when (> (length days) 0)
6631          (setq days (read days)))
6632        (if (numberp days)
6633            (setq days-got t)
6634          (message "Please enter a number.")
6635          (sleep-for 1)))
6636      (list days younger)))
6637   (prog1
6638       (let ((data gnus-newsgroup-data)
6639             (cutoff (days-to-time age))
6640             articles d date is-younger)
6641         (while (setq d (pop data))
6642           (when (and (vectorp (gnus-data-header d))
6643                      (setq date (mail-header-date (gnus-data-header d))))
6644             (setq is-younger (time-less-p
6645                               (time-since (condition-case ()
6646                                               (date-to-time date)
6647                                             (error '(0 0))))
6648                               cutoff))
6649             (when (if younger-p
6650                       is-younger
6651                     (not is-younger))
6652               (push (gnus-data-number d) articles))))
6653         (gnus-summary-limit (nreverse articles)))
6654     (gnus-summary-position-point)))
6655
6656 (defun gnus-summary-limit-to-extra (header regexp)
6657   "Limit the summary buffer to articles that match an 'extra' header."
6658   (interactive
6659    (let ((header
6660           (intern
6661            (gnus-completing-read
6662             (symbol-name (car gnus-extra-headers))
6663             "Limit extra header:"
6664             (mapcar (lambda (x)
6665                       (cons (symbol-name x) x))
6666                     gnus-extra-headers)
6667             nil
6668             t))))
6669      (list header
6670            (read-string (format "Limit to header %s (regexp): " header)))))
6671   (when (not (equal "" regexp))
6672     (prog1
6673         (let ((articles (gnus-summary-find-matching
6674                          (cons 'extra header) regexp 'all)))
6675           (unless articles
6676             (error "Found no matches for \"%s\"" regexp))
6677           (gnus-summary-limit articles))
6678       (gnus-summary-position-point))))
6679
6680 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6681 (make-obsolete
6682  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6683
6684 (defun gnus-summary-limit-to-unread (&optional all)
6685   "Limit the summary buffer to articles that are not marked as read.
6686 If ALL is non-nil, limit strictly to unread articles."
6687   (interactive "P")
6688   (if all
6689       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6690     (gnus-summary-limit-to-marks
6691      ;; Concat all the marks that say that an article is read and have
6692      ;; those removed.
6693      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6694            gnus-killed-mark gnus-kill-file-mark
6695            gnus-low-score-mark gnus-expirable-mark
6696            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6697            gnus-duplicate-mark gnus-souped-mark)
6698      'reverse)))
6699
6700 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6701 (make-obsolete 'gnus-summary-delete-marked-with
6702                'gnus-summary-limit-exlude-marks)
6703
6704 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6705   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6706 If REVERSE, limit the summary buffer to articles that are marked
6707 with MARKS.  MARKS can either be a string of marks or a list of marks.
6708 Returns how many articles were removed."
6709   (interactive "sMarks: ")
6710   (gnus-summary-limit-to-marks marks t))
6711
6712 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6713   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6714 If REVERSE (the prefix), limit the summary buffer to articles that are
6715 not marked with MARKS.  MARKS can either be a string of marks or a
6716 list of marks.
6717 Returns how many articles were removed."
6718   (interactive "sMarks: \nP")
6719   (prog1
6720       (let ((data gnus-newsgroup-data)
6721             (marks (if (listp marks) marks
6722                      (append marks nil))) ; Transform to list.
6723             articles)
6724         (while data
6725           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6726                   (memq (gnus-data-mark (car data)) marks))
6727             (push (gnus-data-number (car data)) articles))
6728           (setq data (cdr data)))
6729         (gnus-summary-limit articles))
6730     (gnus-summary-position-point)))
6731
6732 (defun gnus-summary-limit-to-score (&optional score)
6733   "Limit to articles with score at or above SCORE."
6734   (interactive "P")
6735   (setq score (if score
6736                   (prefix-numeric-value score)
6737                 (or gnus-summary-default-score 0)))
6738   (let ((data gnus-newsgroup-data)
6739         articles)
6740     (while data
6741       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6742                 score)
6743         (push (gnus-data-number (car data)) articles))
6744       (setq data (cdr data)))
6745     (prog1
6746         (gnus-summary-limit articles)
6747       (gnus-summary-position-point))))
6748
6749 (defun gnus-summary-limit-include-thread (id)
6750   "Display all the hidden articles that in the current thread."
6751   (interactive (list (mail-header-id (gnus-summary-article-header))))
6752   (let ((articles (gnus-articles-in-thread
6753                    (gnus-id-to-thread (gnus-root-id id)))))
6754     (prog1
6755         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6756       (gnus-summary-position-point))))
6757
6758 (defun gnus-summary-limit-include-dormant ()
6759   "Display all the hidden articles that are marked as dormant.
6760 Note that this command only works on a subset of the articles currently
6761 fetched for this group."
6762   (interactive)
6763   (unless gnus-newsgroup-dormant
6764     (error "There are no dormant articles in this group"))
6765   (prog1
6766       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6767     (gnus-summary-position-point)))
6768
6769 (defun gnus-summary-limit-exclude-dormant ()
6770   "Hide all dormant articles."
6771   (interactive)
6772   (prog1
6773       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6774     (gnus-summary-position-point)))
6775
6776 (defun gnus-summary-limit-exclude-childless-dormant ()
6777   "Hide all dormant articles that have no children."
6778   (interactive)
6779   (let ((data (gnus-data-list t))
6780         articles d children)
6781     ;; Find all articles that are either not dormant or have
6782     ;; children.
6783     (while (setq d (pop data))
6784       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6785                 (and (setq children
6786                            (gnus-article-children (gnus-data-number d)))
6787                      (let (found)
6788                        (while children
6789                          (when (memq (car children) articles)
6790                            (setq children nil
6791                                  found t))
6792                          (pop children))
6793                        found)))
6794         (push (gnus-data-number d) articles)))
6795     ;; Do the limiting.
6796     (prog1
6797         (gnus-summary-limit articles)
6798       (gnus-summary-position-point))))
6799
6800 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6801   "Mark all unread excluded articles as read.
6802 If ALL, mark even excluded ticked and dormants as read."
6803   (interactive "P")
6804   (let ((articles (gnus-sorted-complement
6805                    (sort
6806                     (mapcar (lambda (h) (mail-header-number h))
6807                             gnus-newsgroup-headers)
6808                     '<)
6809                    (sort gnus-newsgroup-limit '<)))
6810         article)
6811     (setq gnus-newsgroup-unreads
6812           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6813     (if all
6814         (setq gnus-newsgroup-dormant nil
6815               gnus-newsgroup-marked nil
6816               gnus-newsgroup-reads
6817               (nconc
6818                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6819                gnus-newsgroup-reads))
6820       (while (setq article (pop articles))
6821         (unless (or (memq article gnus-newsgroup-dormant)
6822                     (memq article gnus-newsgroup-marked))
6823           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6824
6825 (defun gnus-summary-limit (articles &optional pop)
6826   (if pop
6827       ;; We pop the previous limit off the stack and use that.
6828       (setq articles (car gnus-newsgroup-limits)
6829             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6830     ;; We use the new limit, so we push the old limit on the stack.
6831     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6832   ;; Set the limit.
6833   (setq gnus-newsgroup-limit articles)
6834   (let ((total (length gnus-newsgroup-data))
6835         (data (gnus-data-find-list (gnus-summary-article-number)))
6836         (gnus-summary-mark-below nil)   ; Inhibit this.
6837         found)
6838     ;; This will do all the work of generating the new summary buffer
6839     ;; according to the new limit.
6840     (gnus-summary-prepare)
6841     ;; Hide any threads, possibly.
6842     (and gnus-show-threads
6843          gnus-thread-hide-subtree
6844          (gnus-summary-hide-all-threads))
6845     ;; Try to return to the article you were at, or one in the
6846     ;; neighborhood.
6847     (when data
6848       ;; We try to find some article after the current one.
6849       (while data
6850         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6851           (setq data nil
6852                 found t))
6853         (setq data (cdr data))))
6854     (unless found
6855       ;; If there is no data, that means that we were after the last
6856       ;; article.  The same goes when we can't find any articles
6857       ;; after the current one.
6858       (goto-char (point-max))
6859       (gnus-summary-find-prev))
6860     (gnus-set-mode-line 'summary)
6861     ;; We return how many articles were removed from the summary
6862     ;; buffer as a result of the new limit.
6863     (- total (length gnus-newsgroup-data))))
6864
6865 (defsubst gnus-invisible-cut-children (threads)
6866   (let ((num 0))
6867     (while threads
6868       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6869         (incf num))
6870       (pop threads))
6871     (< num 2)))
6872
6873 (defsubst gnus-cut-thread (thread)
6874   "Go forwards in the thread until we find an article that we want to display."
6875   (when (or (eq gnus-fetch-old-headers 'some)
6876             (eq gnus-fetch-old-headers 'invisible)
6877             (numberp gnus-fetch-old-headers)
6878             (eq gnus-build-sparse-threads 'some)
6879             (eq gnus-build-sparse-threads 'more))
6880     ;; Deal with old-fetched headers and sparse threads.
6881     (while (and
6882             thread
6883             (or
6884              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6885              (gnus-summary-article-ancient-p
6886               (mail-header-number (car thread))))
6887             (if (or (<= (length (cdr thread)) 1)
6888                     (eq gnus-fetch-old-headers 'invisible))
6889                 (setq gnus-newsgroup-limit
6890                       (delq (mail-header-number (car thread))
6891                             gnus-newsgroup-limit)
6892                       thread (cadr thread))
6893               (when (gnus-invisible-cut-children (cdr thread))
6894                 (let ((th (cdr thread)))
6895                   (while th
6896                     (if (memq (mail-header-number (caar th))
6897                               gnus-newsgroup-limit)
6898                         (setq thread (car th)
6899                               th nil)
6900                       (setq th (cdr th))))))))))
6901   thread)
6902
6903 (defun gnus-cut-threads (threads)
6904   "Cut off all uninteresting articles from the beginning of threads."
6905   (when (or (eq gnus-fetch-old-headers 'some)
6906             (eq gnus-fetch-old-headers 'invisible)
6907             (numberp gnus-fetch-old-headers)
6908             (eq gnus-build-sparse-threads 'some)
6909             (eq gnus-build-sparse-threads 'more))
6910     (let ((th threads))
6911       (while th
6912         (setcar th (gnus-cut-thread (car th)))
6913         (setq th (cdr th)))))
6914   ;; Remove nixed out threads.
6915   (delq nil threads))
6916
6917 (defun gnus-summary-initial-limit (&optional show-if-empty)
6918   "Figure out what the initial limit is supposed to be on group entry.
6919 This entails weeding out unwanted dormants, low-scored articles,
6920 fetch-old-headers verbiage, and so on."
6921   ;; Most groups have nothing to remove.
6922   (if (or gnus-inhibit-limiting
6923           (and (null gnus-newsgroup-dormant)
6924                (not (eq gnus-fetch-old-headers 'some))
6925                (not (numberp gnus-fetch-old-headers))
6926                (not (eq gnus-fetch-old-headers 'invisible))
6927                (null gnus-summary-expunge-below)
6928                (not (eq gnus-build-sparse-threads 'some))
6929                (not (eq gnus-build-sparse-threads 'more))
6930                (null gnus-thread-expunge-below)
6931                (not gnus-use-nocem)))
6932       ()                                ; Do nothing.
6933     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6934     (setq gnus-newsgroup-limit nil)
6935     (mapatoms
6936      (lambda (node)
6937        (unless (car (symbol-value node))
6938          ;; These threads have no parents -- they are roots.
6939          (let ((nodes (cdr (symbol-value node)))
6940                thread)
6941            (while nodes
6942              (if (and gnus-thread-expunge-below
6943                       (< (gnus-thread-total-score (car nodes))
6944                          gnus-thread-expunge-below))
6945                  (gnus-expunge-thread (pop nodes))
6946                (setq thread (pop nodes))
6947                (gnus-summary-limit-children thread))))))
6948      gnus-newsgroup-dependencies)
6949     ;; If this limitation resulted in an empty group, we might
6950     ;; pop the previous limit and use it instead.
6951     (when (and (not gnus-newsgroup-limit)
6952                show-if-empty)
6953       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6954     gnus-newsgroup-limit))
6955
6956 (defun gnus-summary-limit-children (thread)
6957   "Return 1 if this subthread is visible and 0 if it is not."
6958   ;; First we get the number of visible children to this thread.  This
6959   ;; is done by recursing down the thread using this function, so this
6960   ;; will really go down to a leaf article first, before slowly
6961   ;; working its way up towards the root.
6962   (when thread
6963     (let ((children
6964            (if (cdr thread)
6965                (apply '+ (mapcar 'gnus-summary-limit-children
6966                                  (cdr thread)))
6967              0))
6968           (number (mail-header-number (car thread)))
6969           score)
6970       (if (and
6971            (not (memq number gnus-newsgroup-marked))
6972            (or
6973             ;; If this article is dormant and has absolutely no visible
6974             ;; children, then this article isn't visible.
6975             (and (memq number gnus-newsgroup-dormant)
6976                  (zerop children))
6977             ;; If this is "fetch-old-headered" and there is no
6978             ;; visible children, then we don't want this article.
6979             (and (or (eq gnus-fetch-old-headers 'some)
6980                      (numberp gnus-fetch-old-headers))
6981                  (gnus-summary-article-ancient-p number)
6982                  (zerop children))
6983             ;; If this is "fetch-old-headered" and `invisible', then
6984             ;; we don't want this article.
6985             (and (eq gnus-fetch-old-headers 'invisible)
6986                  (gnus-summary-article-ancient-p number))
6987             ;; If this is a sparsely inserted article with no children,
6988             ;; we don't want it.
6989             (and (eq gnus-build-sparse-threads 'some)
6990                  (gnus-summary-article-sparse-p number)
6991                  (zerop children))
6992             ;; If we use expunging, and this article is really
6993             ;; low-scored, then we don't want this article.
6994             (when (and gnus-summary-expunge-below
6995                        (< (setq score
6996                                 (or (cdr (assq number gnus-newsgroup-scored))
6997                                     gnus-summary-default-score))
6998                           gnus-summary-expunge-below))
6999               ;; We increase the expunge-tally here, but that has
7000               ;; nothing to do with the limits, really.
7001               (incf gnus-newsgroup-expunged-tally)
7002               ;; We also mark as read here, if that's wanted.
7003               (when (and gnus-summary-mark-below
7004                          (< score gnus-summary-mark-below))
7005                 (setq gnus-newsgroup-unreads
7006                       (delq number gnus-newsgroup-unreads))
7007                 (if gnus-newsgroup-auto-expire
7008                     (push number gnus-newsgroup-expirable)
7009                   (push (cons number gnus-low-score-mark)
7010                         gnus-newsgroup-reads)))
7011               t)
7012             ;; Check NoCeM things.
7013             (if (and gnus-use-nocem
7014                      (gnus-nocem-unwanted-article-p
7015                       (mail-header-id (car thread))))
7016                 (progn
7017                   (setq gnus-newsgroup-unreads
7018                         (delq number gnus-newsgroup-unreads))
7019                   t))))
7020           ;; Nope, invisible article.
7021           0
7022         ;; Ok, this article is to be visible, so we add it to the limit
7023         ;; and return 1.
7024         (push number gnus-newsgroup-limit)
7025         1))))
7026
7027 (defun gnus-expunge-thread (thread)
7028   "Mark all articles in THREAD as read."
7029   (let* ((number (mail-header-number (car thread))))
7030     (incf gnus-newsgroup-expunged-tally)
7031     ;; We also mark as read here, if that's wanted.
7032     (setq gnus-newsgroup-unreads
7033           (delq number gnus-newsgroup-unreads))
7034     (if gnus-newsgroup-auto-expire
7035         (push number gnus-newsgroup-expirable)
7036       (push (cons number gnus-low-score-mark)
7037             gnus-newsgroup-reads)))
7038   ;; Go recursively through all subthreads.
7039   (mapcar 'gnus-expunge-thread (cdr thread)))
7040
7041 ;; Summary article oriented commands
7042
7043 (defun gnus-summary-refer-parent-article (n)
7044   "Refer parent article N times.
7045 If N is negative, go to ancestor -N instead.
7046 The difference between N and the number of articles fetched is returned."
7047   (interactive "p")
7048   (let ((skip 1)
7049         error header ref)
7050     (when (not (natnump n))
7051       (setq skip (abs n)
7052             n 1))
7053     (while (and (> n 0)
7054                 (not error))
7055       (setq header (gnus-summary-article-header))
7056       (if (and (eq (mail-header-number header)
7057                    (cdr gnus-article-current))
7058                (equal gnus-newsgroup-name
7059                       (car gnus-article-current)))
7060           ;; If we try to find the parent of the currently
7061           ;; displayed article, then we take a look at the actual
7062           ;; References header, since this is slightly more
7063           ;; reliable than the References field we got from the
7064           ;; server.
7065           (save-excursion
7066             (set-buffer gnus-original-article-buffer)
7067             (nnheader-narrow-to-headers)
7068             (unless (setq ref (message-fetch-field "references"))
7069               (setq ref (message-fetch-field "in-reply-to")))
7070             (widen))
7071         (setq ref
7072               ;; It's not the current article, so we take a bet on
7073               ;; the value we got from the server.
7074               (mail-header-references header)))
7075       (if (and ref
7076                (not (equal ref "")))
7077           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7078             (gnus-message 1 "Couldn't find parent"))
7079         (gnus-message 1 "No references in article %d"
7080                       (gnus-summary-article-number))
7081         (setq error t))
7082       (decf n))
7083     (gnus-summary-position-point)
7084     n))
7085
7086 (defun gnus-summary-refer-references ()
7087   "Fetch all articles mentioned in the References header.
7088 Return the number of articles fetched."
7089   (interactive)
7090   (let ((ref (mail-header-references (gnus-summary-article-header)))
7091         (current (gnus-summary-article-number))
7092         (n 0))
7093     (if (or (not ref)
7094             (equal ref ""))
7095         (error "No References in the current article")
7096       ;; For each Message-ID in the References header...
7097       (while (string-match "<[^>]*>" ref)
7098         (incf n)
7099         ;; ... fetch that article.
7100         (gnus-summary-refer-article
7101          (prog1 (match-string 0 ref)
7102            (setq ref (substring ref (match-end 0))))))
7103       (gnus-summary-goto-subject current)
7104       (gnus-summary-position-point)
7105       n)))
7106
7107 (defun gnus-summary-refer-thread (&optional limit)
7108   "Fetch all articles in the current thread.
7109 If LIMIT (the numerical prefix), fetch that many old headers instead
7110 of what's specified by the `gnus-refer-thread-limit' variable."
7111   (interactive "P")
7112   (let ((id (mail-header-id (gnus-summary-article-header)))
7113         (limit (if limit (prefix-numeric-value limit)
7114                  gnus-refer-thread-limit)))
7115     ;; We want to fetch LIMIT *old* headers, but we also have to
7116     ;; re-fetch all the headers in the current buffer, because many of
7117     ;; them may be undisplayed.  So we adjust LIMIT.
7118     (when (numberp limit)
7119       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7120     (unless (eq gnus-fetch-old-headers 'invisible)
7121       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7122       ;; Retrieve the headers and read them in.
7123       (if (eq (gnus-retrieve-headers
7124                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7125               'nov)
7126           (gnus-build-all-threads)
7127         (error "Can't fetch thread from backends that don't support NOV"))
7128       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7129     (gnus-summary-limit-include-thread id)))
7130
7131 (defun gnus-summary-refer-article (message-id)
7132   "Fetch an article specified by MESSAGE-ID."
7133   (interactive "sMessage-ID: ")
7134   (when (and (stringp message-id)
7135              (not (zerop (length message-id))))
7136     ;; Construct the correct Message-ID if necessary.
7137     ;; Suggested by tale@pawl.rpi.edu.
7138     (unless (string-match "^<" message-id)
7139       (setq message-id (concat "<" message-id)))
7140     (unless (string-match ">$" message-id)
7141       (setq message-id (concat message-id ">")))
7142     (let* ((header (gnus-id-to-header message-id))
7143            (sparse (and header
7144                         (gnus-summary-article-sparse-p
7145                          (mail-header-number header))
7146                         (memq (mail-header-number header)
7147                               gnus-newsgroup-limit)))
7148            number)
7149       (cond
7150        ;; If the article is present in the buffer we just go to it.
7151        ((and header
7152              (or (not (gnus-summary-article-sparse-p
7153                        (mail-header-number header)))
7154                  sparse))
7155         (prog1
7156             (gnus-summary-goto-article
7157              (mail-header-number header) nil t)
7158           (when sparse
7159             (gnus-summary-update-article (mail-header-number header)))))
7160        (t
7161         ;; We fetch the article.
7162         (catch 'found
7163           (dolist (gnus-override-method (gnus-refer-article-methods))
7164             (gnus-check-server gnus-override-method)
7165             ;; Fetch the header, and display the article.
7166             (when (setq number (gnus-summary-insert-subject message-id))
7167               (gnus-summary-select-article nil nil nil number)
7168               (throw 'found t)))
7169           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7170
7171 (defun gnus-refer-article-methods ()
7172   "Return a list of referrable methods."
7173   (cond
7174    ;; No method, so we default to current and native.
7175    ((null gnus-refer-article-method)
7176     (list gnus-current-select-method gnus-select-method))
7177    ;; Current.
7178    ((eq 'current gnus-refer-article-method)
7179     (list gnus-current-select-method))
7180    ;; List of select methods.
7181    ((not (and (symbolp (car gnus-refer-article-method))
7182               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7183     (let (out)
7184       (dolist (method gnus-refer-article-method)
7185         (push (if (eq 'current method)
7186                   gnus-current-select-method
7187                 method)
7188               out))
7189       (nreverse out)))
7190    ;; One single select method.
7191    (t
7192     (list gnus-refer-article-method))))
7193
7194 (defun gnus-summary-edit-parameters ()
7195   "Edit the group parameters of the current group."
7196   (interactive)
7197   (gnus-group-edit-group gnus-newsgroup-name 'params))
7198
7199 (defun gnus-summary-customize-parameters ()
7200   "Customize the group parameters of the current group."
7201   (interactive)
7202   (gnus-group-customize gnus-newsgroup-name))
7203
7204 (defun gnus-summary-enter-digest-group (&optional force)
7205   "Enter an nndoc group based on the current article.
7206 If FORCE, force a digest interpretation.  If not, try
7207 to guess what the document format is."
7208   (interactive "P")
7209   (let ((conf gnus-current-window-configuration))
7210     (save-excursion
7211       (gnus-summary-select-article))
7212     (setq gnus-current-window-configuration conf)
7213     (let* ((name (format "%s-%d"
7214                          (gnus-group-prefixed-name
7215                           gnus-newsgroup-name (list 'nndoc ""))
7216                          (save-excursion
7217                            (set-buffer gnus-summary-buffer)
7218                            gnus-current-article)))
7219            (ogroup gnus-newsgroup-name)
7220            (params (append (gnus-info-params (gnus-get-info ogroup))
7221                            (list (cons 'to-group ogroup))
7222                            (list (cons 'save-article-group ogroup))))
7223            (case-fold-search t)
7224            (buf (current-buffer))
7225            dig to-address)
7226       (save-excursion
7227         (set-buffer gnus-original-article-buffer)
7228         ;; Have the digest group inherit the main mail address of
7229         ;; the parent article.
7230         (when (setq to-address (or (message-fetch-field "reply-to")
7231                                    (message-fetch-field "from")))
7232           (setq params (append
7233                         (list (cons 'to-address
7234                                     (funcall gnus-decode-encoded-word-function
7235                                              to-address))))))
7236         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7237         (insert-buffer-substring gnus-original-article-buffer)
7238         ;; Remove lines that may lead nndoc to misinterpret the
7239         ;; document type.
7240         (narrow-to-region
7241          (goto-char (point-min))
7242          (or (search-forward "\n\n" nil t) (point)))
7243         (goto-char (point-min))
7244         (delete-matching-lines "^Path:\\|^From ")
7245         (widen))
7246       (unwind-protect
7247           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7248                     (gnus-newsgroup-ephemeral-ignored-charsets
7249                      gnus-newsgroup-ignored-charsets))
7250                 (gnus-group-read-ephemeral-group
7251                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7252                               (nndoc-article-type
7253                                ,(if force 'mbox 'guess))) t))
7254               ;; Make all postings to this group go to the parent group.
7255               (nconc (gnus-info-params (gnus-get-info name))
7256                      params)
7257             ;; Couldn't select this doc group.
7258             (switch-to-buffer buf)
7259             (gnus-set-global-variables)
7260             (gnus-configure-windows 'summary)
7261             (gnus-message 3 "Article couldn't be entered?"))
7262         (kill-buffer dig)))))
7263
7264 (defun gnus-summary-read-document (n)
7265   "Open a new group based on the current article(s).
7266 This will allow you to read digests and other similar
7267 documents as newsgroups.
7268 Obeys the standard process/prefix convention."
7269   (interactive "P")
7270   (let* ((articles (gnus-summary-work-articles n))
7271          (ogroup gnus-newsgroup-name)
7272          (params (append (gnus-info-params (gnus-get-info ogroup))
7273                          (list (cons 'to-group ogroup))))
7274          article group egroup groups vgroup)
7275     (while (setq article (pop articles))
7276       (setq group (format "%s-%d" gnus-newsgroup-name article))
7277       (gnus-summary-remove-process-mark article)
7278       (when (gnus-summary-display-article article)
7279         (save-excursion
7280           (with-temp-buffer
7281             (insert-buffer-substring gnus-original-article-buffer)
7282             ;; Remove some headers that may lead nndoc to make
7283             ;; the wrong guess.
7284             (message-narrow-to-head)
7285             (goto-char (point-min))
7286             (delete-matching-lines "^\\(Path\\):\\|^From ")
7287             (widen)
7288             (if (setq egroup
7289                       (gnus-group-read-ephemeral-group
7290                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7291                                      (nndoc-article-type guess))
7292                        t nil t))
7293                 (progn
7294                   ;; Make all postings to this group go to the parent group.
7295                   (nconc (gnus-info-params (gnus-get-info egroup))
7296                          params)
7297                   (push egroup groups))
7298               ;; Couldn't select this doc group.
7299               (gnus-error 3 "Article couldn't be entered"))))))
7300     ;; Now we have selected all the documents.
7301     (cond
7302      ((not groups)
7303       (error "None of the articles could be interpreted as documents"))
7304      ((gnus-group-read-ephemeral-group
7305        (setq vgroup (format
7306                      "nnvirtual:%s-%s" gnus-newsgroup-name
7307                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7308        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7309        t
7310        (cons (current-buffer) 'summary)))
7311      (t
7312       (error "Couldn't select virtual nndoc group")))))
7313
7314 (defun gnus-summary-isearch-article (&optional regexp-p)
7315   "Do incremental search forward on the current article.
7316 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7317   (interactive "P")
7318   (let* ((gnus-inhibit-treatment t)
7319          (old (gnus-summary-select-article)))
7320     (gnus-configure-windows 'article)
7321     (gnus-eval-in-buffer-window gnus-article-buffer
7322       (save-restriction
7323         (widen)
7324         (when (eq 'old old)
7325           (gnus-article-show-all-headers))
7326         (goto-char (point-min))
7327         (isearch-forward regexp-p)))))
7328
7329 (defun gnus-summary-search-article-forward (regexp &optional backward)
7330   "Search for an article containing REGEXP forward.
7331 If BACKWARD, search backward instead."
7332   (interactive
7333    (list (read-string
7334           (format "Search article %s (regexp%s): "
7335                   (if current-prefix-arg "backward" "forward")
7336                   (if gnus-last-search-regexp
7337                       (concat ", default " gnus-last-search-regexp)
7338                     "")))
7339          current-prefix-arg))
7340   (if (string-equal regexp "")
7341       (setq regexp (or gnus-last-search-regexp ""))
7342     (setq gnus-last-search-regexp regexp)
7343     (setq gnus-article-before-search gnus-current-article))
7344   ;; Intentionally set gnus-last-article.
7345   (setq gnus-last-article gnus-article-before-search)
7346   (let ((gnus-last-article gnus-last-article))
7347     (if (gnus-summary-search-article regexp backward)
7348         (gnus-summary-show-thread)
7349       (error "Search failed: \"%s\"" regexp))))
7350
7351 (defun gnus-summary-search-article-backward (regexp)
7352   "Search for an article containing REGEXP backward."
7353   (interactive
7354    (list (read-string
7355           (format "Search article backward (regexp%s): "
7356                   (if gnus-last-search-regexp
7357                       (concat ", default " gnus-last-search-regexp)
7358                     "")))))
7359   (gnus-summary-search-article-forward regexp 'backward))
7360
7361 (eval-when-compile
7362   (defmacro gnus-summary-search-article-position-point (regexp backward)
7363     "Dehighlight the last matched text and goto the beginning position."
7364     (` (if (and gnus-summary-search-article-matched-data
7365                 (let ((text (caddr gnus-summary-search-article-matched-data))
7366                       (inhibit-read-only t)
7367                       buffer-read-only)
7368                   (delete-region
7369                    (goto-char (car gnus-summary-search-article-matched-data))
7370                    (cadr gnus-summary-search-article-matched-data))
7371                   (insert text)
7372                   (string-match (, regexp) text)))
7373            (if (, backward) (beginning-of-line) (end-of-line))
7374          (goto-char (if (, backward) (point-max) (point-min))))))
7375
7376   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7377     "Place point where X-Face image is displayed."
7378     (if (featurep 'xemacs)
7379         (` (let ((end (if (search-forward "\n\n" nil t)
7380                           (goto-char (1- (point)))
7381                         (point-min)))
7382                  extent)
7383              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7384              (unless (and (re-search-forward "^From:" end t)
7385                           (setq extent (extent-at (point)))
7386                           (extent-begin-glyph extent))
7387                (goto-char (, opoint)))))
7388       (` (let ((end (if (search-forward "\n\n" nil t)
7389                         (goto-char (1- (point)))
7390                       (point-min)))
7391                (start (or (search-backward "\n\n" nil t) (point-min))))
7392            (goto-char
7393             (or (text-property-any start end 'x-face-image t);; x-face-e21
7394                 (text-property-any start end 'x-face-mule-bitmap-image t)
7395                 (, opoint)))))))
7396
7397   (defmacro gnus-summary-search-article-highlight-matched-text
7398     (backward treated x-face)
7399     "Highlight matched text in the function `gnus-summary-search-article'."
7400     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7401              (end (set-marker (make-marker) (match-end 0)))
7402              (inhibit-read-only t)
7403              buffer-read-only)
7404          (unless treated
7405            (let ((,@
7406                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7407                     (mapcar
7408                      (lambda (item) (setq items (delq item items)))
7409                      '(gnus-treat-buttonize
7410                        gnus-treat-fill-article
7411                        gnus-treat-fill-long-lines
7412                        gnus-treat-emphasize
7413                        gnus-treat-highlight-headers
7414                        gnus-treat-highlight-citation
7415                        gnus-treat-highlight-signature
7416                        gnus-treat-overstrike
7417                        gnus-treat-display-xface
7418                        gnus-treat-buttonize-head
7419                        gnus-treat-decode-article-as-default-mime-charset))
7420                     (static-if (featurep 'xemacs)
7421                         items
7422                       (cons '(x-face-mule-delete-x-face-field
7423                               (quote never))
7424                             items))))
7425                  (gnus-treat-display-xface
7426                   (when (, x-face) gnus-treat-display-xface)))
7427              (gnus-article-prepare-mime-display)))
7428          (goto-char (if (, backward) start end))
7429          (when (, x-face)
7430            (gnus-summary-search-article-highlight-goto-x-face (point)))
7431          (setq gnus-summary-search-article-matched-data
7432                (list start end (buffer-substring start end)))
7433          (unless (eq start end);; matched text has been deleted. :-<
7434            (put-text-property start end 'face
7435                               (or (find-face 'isearch)
7436                                   'secondary-selection))))))
7437   )
7438
7439 (defun gnus-summary-search-article (regexp &optional backward)
7440   "Search for an article containing REGEXP.
7441 Optional argument BACKWARD means do search for backward.
7442 `gnus-select-article-hook' is not called during the search."
7443   ;; We have to require this here to make sure that the following
7444   ;; dynamic binding isn't shadowed by autoloading.
7445   (require 'gnus-async)
7446   (require 'gnus-art)
7447   (let ((gnus-select-article-hook nil)  ;Disable hook.
7448         (gnus-article-prepare-hook nil)
7449         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7450         (gnus-use-article-prefetch nil)
7451         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7452         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7453         (sum (current-buffer))
7454         (found nil)
7455         point treated)
7456     (gnus-save-hidden-threads
7457       (static-if (featurep 'xemacs)
7458           (let ((gnus-inhibit-treatment t))
7459             (setq treated (eq 'old (gnus-summary-select-article)))
7460             (when (and treated
7461                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7462                                  (window-live-p (get-buffer-window
7463                                                  gnus-article-buffer t)))))
7464               (gnus-summary-select-article nil t)
7465               (setq treated nil)))
7466         (let ((gnus-inhibit-treatment t)
7467               (x-face-mule-delete-x-face-field 'never))
7468           (setq treated (eq 'old (gnus-summary-select-article)))
7469           (when (and treated
7470                      (not
7471                       (and (gnus-buffer-live-p gnus-article-buffer)
7472                            (window-live-p (get-buffer-window
7473                                            gnus-article-buffer t))
7474                            (or (not (string-match "^\\^X-Face:" regexp))
7475                                (with-current-buffer gnus-article-buffer
7476                                  gnus-summary-search-article-matched-data)))))
7477             (gnus-summary-select-article nil t)
7478             (setq treated nil))))
7479       (set-buffer gnus-article-buffer)
7480       (widen)
7481       (if treated
7482           (progn
7483             (gnus-article-show-all-headers)
7484             (gnus-summary-search-article-position-point regexp backward))
7485         (goto-char (if backward (point-max) (point-min))))
7486       (while (not found)
7487         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7488         (if (if backward
7489                 (re-search-backward regexp nil t)
7490               (re-search-forward regexp nil t))
7491             ;; We found the regexp.
7492             (progn
7493               (gnus-summary-search-article-highlight-matched-text
7494                backward treated (string-match "^\\^X-Face:" regexp))
7495               (setq found 'found)
7496               (forward-line
7497                (/ (- 2 (window-height
7498                         (get-buffer-window gnus-article-buffer t)))
7499                   2))
7500               (set-window-start
7501                (get-buffer-window (current-buffer))
7502                (point))
7503               (set-buffer sum)
7504               (setq point (point)))
7505           ;; We didn't find it, so we go to the next article.
7506           (set-buffer sum)
7507           (setq found 'not)
7508           (while (eq found 'not)
7509             (if (not (if backward (gnus-summary-find-prev)
7510                        (gnus-summary-find-next)))
7511                 ;; No more articles.
7512                 (setq found t)
7513               ;; Select the next article and adjust point.
7514               (unless (gnus-summary-article-sparse-p
7515                        (gnus-summary-article-number))
7516                 (setq found nil)
7517                 (let ((gnus-inhibit-treatment t))
7518                   (gnus-summary-select-article))
7519                 (setq treated nil)
7520                 (set-buffer gnus-article-buffer)
7521                 (widen)
7522                 (goto-char (if backward (point-max) (point-min))))))))
7523       (gnus-message 7 ""))
7524     ;; Return whether we found the regexp.
7525     (when (eq found 'found)
7526       (goto-char point)
7527       (gnus-summary-show-thread)
7528       (gnus-summary-goto-subject gnus-current-article)
7529       (gnus-summary-position-point)
7530       t)))
7531
7532 (defun gnus-summary-find-matching (header regexp &optional backward unread
7533                                           not-case-fold)
7534   "Return a list of all articles that match REGEXP on HEADER.
7535 The search stars on the current article and goes forwards unless
7536 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7537 If UNREAD is non-nil, only unread articles will
7538 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7539 in the comparisons."
7540   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7541                 (gnus-data-find-list
7542                  (gnus-summary-article-number) (gnus-data-list backward))))
7543         (case-fold-search (not not-case-fold))
7544         articles d func)
7545     (if (consp header)
7546         (if (eq (car header) 'extra)
7547             (setq func
7548                   `(lambda (h)
7549                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7550                          "")))
7551           (error "%s is an invalid header" header))
7552       (unless (fboundp (intern (concat "mail-header-" header)))
7553         (error "%s is not a valid header" header))
7554       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7555     (while data
7556       (setq d (car data))
7557       (and (or (not unread)             ; We want all articles...
7558                (gnus-data-unread-p d))  ; Or just unreads.
7559            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7560            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7561            (push (gnus-data-number d) articles)) ; Success!
7562       (setq data (cdr data)))
7563     (nreverse articles)))
7564
7565 (defun gnus-summary-execute-command (header regexp command &optional backward)
7566   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7567 If HEADER is an empty string (or nil), the match is done on the entire
7568 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7569   (interactive
7570    (list (let ((completion-ignore-case t))
7571            (completing-read
7572             "Header name: "
7573             (mapcar (lambda (header) (list (format "%s" header)))
7574                     (append
7575                      '("Number" "Subject" "From" "Lines" "Date"
7576                        "Message-ID" "Xref" "References" "Body")
7577                      gnus-extra-headers))
7578             nil 'require-match))
7579          (read-string "Regexp: ")
7580          (read-key-sequence "Command: ")
7581          current-prefix-arg))
7582   (when (equal header "Body")
7583     (setq header ""))
7584   ;; Hidden thread subtrees must be searched as well.
7585   (gnus-summary-show-all-threads)
7586   ;; We don't want to change current point nor window configuration.
7587   (save-excursion
7588     (save-window-excursion
7589       (gnus-message 6 "Executing %s..." (key-description command))
7590       ;; We'd like to execute COMMAND interactively so as to give arguments.
7591       (gnus-execute header regexp
7592                     `(call-interactively ',(key-binding command))
7593                     backward)
7594       (gnus-message 6 "Executing %s...done" (key-description command)))))
7595
7596 (defun gnus-summary-beginning-of-article ()
7597   "Scroll the article back to the beginning."
7598   (interactive)
7599   (gnus-summary-select-article)
7600   (gnus-configure-windows 'article)
7601   (gnus-eval-in-buffer-window gnus-article-buffer
7602     (widen)
7603     (goto-char (point-min))
7604     (when gnus-page-broken
7605       (gnus-narrow-to-page))))
7606
7607 (defun gnus-summary-end-of-article ()
7608   "Scroll to the end of the article."
7609   (interactive)
7610   (gnus-summary-select-article)
7611   (gnus-configure-windows 'article)
7612   (gnus-eval-in-buffer-window gnus-article-buffer
7613     (widen)
7614     (goto-char (point-max))
7615     (recenter -3)
7616     (when gnus-page-broken
7617       (gnus-narrow-to-page))))
7618
7619 (defun gnus-summary-print-article (&optional filename n)
7620   "Generate and print a PostScript image of the N next (mail) articles.
7621
7622 If N is negative, print the N previous articles.  If N is nil and articles
7623 have been marked with the process mark, print these instead.
7624
7625 If the optional first argument FILENAME is nil, send the image to the
7626 printer.  If FILENAME is a string, save the PostScript image in a file with
7627 that name.  If FILENAME is a number, prompt the user for the name of the file
7628 to save in."
7629   (interactive (list (ps-print-preprint current-prefix-arg)))
7630   (dolist (article (gnus-summary-work-articles n))
7631     (gnus-summary-select-article nil nil 'pseudo article)
7632     (gnus-eval-in-buffer-window gnus-article-buffer
7633       (let ((buffer (generate-new-buffer " *print*")))
7634         (unwind-protect
7635             (progn
7636               (copy-to-buffer buffer (point-min) (point-max))
7637               (set-buffer buffer)
7638               (gnus-article-delete-invisible-text)
7639               (when (gnus-visual-p 'article-highlight 'highlight)
7640                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7641                 ;; highlight.
7642                 (let ((gnus-article-buffer buffer))
7643                   (gnus-article-highlight-citation t)
7644                   (gnus-article-highlight-signature)))
7645               (let ((ps-left-header
7646                      (list
7647                       (concat "("
7648                               (mail-header-subject gnus-current-headers) ")")
7649                       (concat "("
7650                               (mail-header-from gnus-current-headers) ")")))
7651                     (ps-right-header
7652                      (list
7653                       "/pagenumberstring load"
7654                       (concat "("
7655                               (mail-header-date gnus-current-headers) ")"))))
7656                 (gnus-run-hooks 'gnus-ps-print-hook)
7657                 (save-excursion
7658                   (ps-spool-buffer-with-faces))))
7659           (kill-buffer buffer))))
7660     (gnus-summary-remove-process-mark article))
7661   (ps-despool filename))
7662
7663 (defun gnus-summary-show-article (&optional arg)
7664   "Force re-fetching of the current article.
7665 If ARG (the prefix) is a number, show the article with the charset
7666 defined in `gnus-summary-show-article-charset-alist', or the charset
7667 inputed.
7668 If ARG (the prefix) is non-nil and not a number, show the raw article
7669 without any article massaging functions being run."
7670   (interactive "P")
7671   (cond
7672    ((numberp arg)
7673     (let ((gnus-newsgroup-charset
7674            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7675                (read-coding-system "Charset: ")))
7676           (gnus-newsgroup-ignored-charsets 'gnus-all))
7677       (gnus-summary-select-article nil 'force)
7678       (let ((deps gnus-newsgroup-dependencies)
7679             head header)
7680         (save-excursion
7681           (set-buffer gnus-original-article-buffer)
7682           (save-restriction
7683             (message-narrow-to-head)
7684             (setq head (buffer-string)))
7685           (with-temp-buffer
7686             (insert (format "211 %d Article retrieved.\n"
7687                             (cdr gnus-article-current)))
7688             (insert head)
7689             (insert ".\n")
7690             (let ((nntp-server-buffer (current-buffer)))
7691               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7692         (gnus-data-set-header
7693          (gnus-data-find (cdr gnus-article-current))
7694          header)
7695         (gnus-summary-update-article-line
7696          (cdr gnus-article-current) header))))
7697    ((not arg)
7698     ;; Select the article the normal way.
7699     (gnus-summary-select-article nil 'force))
7700    (t
7701     ;; We have to require this here to make sure that the following
7702     ;; dynamic binding isn't shadowed by autoloading.
7703     (require 'gnus-async)
7704     (require 'gnus-art)
7705     ;; Bind the article treatment functions to nil.
7706     (let ((gnus-have-all-headers t)
7707           gnus-article-prepare-hook
7708           gnus-article-decode-hook
7709           gnus-break-pages
7710           gnus-show-mime
7711           (gnus-inhibit-treatment t))
7712       (gnus-summary-select-article nil 'force))))
7713   (gnus-summary-goto-subject gnus-current-article)
7714   (gnus-summary-position-point))
7715
7716 (defun gnus-summary-verbose-headers (&optional arg)
7717   "Toggle permanent full header display.
7718 If ARG is a positive number, turn header display on.
7719 If ARG is a negative number, turn header display off."
7720   (interactive "P")
7721   (setq gnus-show-all-headers
7722         (cond ((or (not (numberp arg))
7723                    (zerop arg))
7724                (not gnus-show-all-headers))
7725               ((natnump arg)
7726                t)))
7727   (gnus-summary-show-article))
7728
7729 (defun gnus-summary-toggle-header (&optional arg)
7730   "Show the headers if they are hidden, or hide them if they are shown.
7731 If ARG is a positive number, show the entire header.
7732 If ARG is a negative number, hide the unwanted header lines."
7733   (interactive "P")
7734   (save-excursion
7735     (set-buffer gnus-article-buffer)
7736     (save-restriction
7737       (let* ((buffer-read-only nil)
7738              (inhibit-point-motion-hooks t)
7739              hidden e)
7740         (setq hidden
7741               (if (numberp arg)
7742                   (>= arg 0)
7743                 (save-restriction
7744                   (article-narrow-to-head)
7745                   (gnus-article-hidden-text-p 'headers))))
7746         (goto-char (point-min))
7747         (when (search-forward "\n\n" nil t)
7748           (delete-region (point-min) (1- (point))))
7749         (goto-char (point-min))
7750         (save-excursion
7751           (set-buffer gnus-original-article-buffer)
7752           (goto-char (point-min))
7753           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7754         (insert-buffer-substring gnus-original-article-buffer 1 e)
7755         (save-restriction
7756           (narrow-to-region (point-min) (point))
7757           (article-decode-encoded-words)
7758           (if  hidden
7759               (let ((gnus-treat-hide-headers nil)
7760                     (gnus-treat-hide-boring-headers nil))
7761                 (setq gnus-article-wash-types
7762                       (delq 'headers gnus-article-wash-types))
7763                 (gnus-treat-article 'head))
7764             (gnus-treat-article 'head)))
7765         (gnus-set-mode-line 'article)))))
7766
7767 (defun gnus-summary-show-all-headers ()
7768   "Make all header lines visible."
7769   (interactive)
7770   (gnus-article-show-all-headers))
7771
7772 (defun gnus-summary-toggle-mime (&optional arg)
7773   "Toggle MIME processing.
7774 If ARG is a positive number, turn MIME processing on."
7775   (interactive "P")
7776   (setq gnus-show-mime
7777         (if (null arg)
7778             (not gnus-show-mime)
7779           (> (prefix-numeric-value arg) 0)))
7780   (gnus-summary-select-article t 'force))
7781
7782 (defun gnus-summary-caesar-message (&optional arg)
7783   "Caesar rotate the current article by 13.
7784 The numerical prefix specifies how many places to rotate each letter
7785 forward."
7786   (interactive "P")
7787   (gnus-summary-select-article)
7788   (let ((mail-header-separator ""))
7789     (gnus-eval-in-buffer-window gnus-article-buffer
7790       (save-restriction
7791         (widen)
7792         (let ((start (window-start))
7793               buffer-read-only)
7794           (message-caesar-buffer-body arg)
7795           (set-window-start (get-buffer-window (current-buffer)) start))))))
7796
7797 (defun gnus-summary-stop-page-breaking ()
7798   "Stop page breaking in the current article."
7799   (interactive)
7800   (gnus-summary-select-article)
7801   (gnus-eval-in-buffer-window gnus-article-buffer
7802     (widen)
7803     (when (gnus-visual-p 'page-marker)
7804       (let ((buffer-read-only nil))
7805         (gnus-remove-text-with-property 'gnus-prev)
7806         (gnus-remove-text-with-property 'gnus-next))
7807       (setq gnus-page-broken nil))))
7808
7809 (defun gnus-summary-move-article (&optional n to-newsgroup
7810                                             select-method action)
7811   "Move the current article to a different newsgroup.
7812 If N is a positive number, move the N next articles.
7813 If N is a negative number, move the N previous articles.
7814 If N is nil and any articles have been marked with the process mark,
7815 move those articles instead.
7816 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7817 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7818 re-spool using this method.
7819
7820 For this function to work, both the current newsgroup and the
7821 newsgroup that you want to move to have to support the `request-move'
7822 and `request-accept' functions.
7823
7824 ACTION can be either `move' (the default), `crosspost' or `copy'."
7825   (interactive "P")
7826   (unless action
7827     (setq action 'move))
7828   ;; Disable marking as read.
7829   (let (gnus-mark-article-hook)
7830     (save-window-excursion
7831       (gnus-summary-select-article)))
7832   ;; Check whether the source group supports the required functions.
7833   (cond ((and (eq action 'move)
7834               (not (gnus-check-backend-function
7835                     'request-move-article gnus-newsgroup-name)))
7836          (error "The current group does not support article moving"))
7837         ((and (eq action 'crosspost)
7838               (not (gnus-check-backend-function
7839                     'request-replace-article gnus-newsgroup-name)))
7840          (error "The current group does not support article editing")))
7841   (let ((articles (gnus-summary-work-articles n))
7842         (prefix (if (gnus-check-backend-function
7843                     'request-move-article gnus-newsgroup-name)
7844                     (gnus-group-real-prefix gnus-newsgroup-name)
7845                   ""))
7846         (names '((move "Move" "Moving")
7847                  (copy "Copy" "Copying")
7848                  (crosspost "Crosspost" "Crossposting")))
7849         (copy-buf (save-excursion
7850                     (nnheader-set-temp-buffer " *copy article*")))
7851         (default-marks gnus-article-mark-lists)
7852         (no-expire-marks (delete '(expirable . expire)
7853                                  (copy-sequence gnus-article-mark-lists)))
7854         art-group to-method new-xref article to-groups)
7855     (unless (assq action names)
7856       (error "Unknown action %s" action))
7857     ;; Read the newsgroup name.
7858     (when (and (not to-newsgroup)
7859                (not select-method))
7860       (setq to-newsgroup
7861             (gnus-read-move-group-name
7862              (cadr (assq action names))
7863              (symbol-value (intern (format "gnus-current-%s-group" action)))
7864              articles prefix))
7865       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7866     (setq to-method (or select-method
7867                         (gnus-server-to-method
7868                          (gnus-group-method to-newsgroup))))
7869     ;; Check the method we are to move this article to...
7870     (unless (gnus-check-backend-function
7871              'request-accept-article (car to-method))
7872       (error "%s does not support article copying" (car to-method)))
7873     (unless (gnus-check-server to-method)
7874       (error "Can't open server %s" (car to-method)))
7875     (gnus-message 6 "%s to %s: %s..."
7876                   (caddr (assq action names))
7877                   (or (car select-method) to-newsgroup) articles)
7878     (while articles
7879       (setq article (pop articles))
7880       (setq
7881        art-group
7882        (cond
7883         ;; Move the article.
7884         ((eq action 'move)
7885          ;; Remove this article from future suppression.
7886          (gnus-dup-unsuppress-article article)
7887          (gnus-request-move-article
7888           article                       ; Article to move
7889           gnus-newsgroup-name           ; From newsgroup
7890           (nth 1 (gnus-find-method-for-group
7891                   gnus-newsgroup-name)) ; Server
7892           (list 'gnus-request-accept-article
7893                 to-newsgroup (list 'quote select-method)
7894                 (not articles) t)       ; Accept form
7895           (not articles)))              ; Only save nov last time
7896         ;; Copy the article.
7897         ((eq action 'copy)
7898          (save-excursion
7899            (set-buffer copy-buf)
7900            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7901              (gnus-request-accept-article
7902               to-newsgroup select-method (not articles) t))))
7903         ;; Crosspost the article.
7904         ((eq action 'crosspost)
7905          (let ((xref (message-tokenize-header
7906                       (mail-header-xref (gnus-summary-article-header article))
7907                       " ")))
7908            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7909                                   ":" article))
7910            (unless xref
7911              (setq xref (list (system-name))))
7912            (setq new-xref
7913                  (concat
7914                   (mapconcat 'identity
7915                              (delete "Xref:" (delete new-xref xref))
7916                              " ")
7917                   " " new-xref))
7918            (save-excursion
7919              (set-buffer copy-buf)
7920              ;; First put the article in the destination group.
7921              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7922              (when (consp (setq art-group
7923                                 (gnus-request-accept-article
7924                                  to-newsgroup select-method (not articles))))
7925                (setq new-xref (concat new-xref " " (car art-group)
7926                                       ":" (cdr art-group)))
7927                ;; Now we have the new Xrefs header, so we insert
7928                ;; it and replace the new article.
7929                (nnheader-replace-header "Xref" new-xref)
7930                (gnus-request-replace-article
7931                 (cdr art-group) to-newsgroup (current-buffer))
7932                art-group))))))
7933       (cond
7934        ((not art-group)
7935         (gnus-message 1 "Couldn't %s article %s: %s"
7936                       (cadr (assq action names)) article
7937                       (nnheader-get-report (car to-method))))
7938        ((eq art-group 'junk)
7939         (when (eq action 'move)
7940           (gnus-summary-mark-article article gnus-canceled-mark)
7941           (gnus-message 4 "Deleted article %s" article)))
7942        (t
7943         (let* ((pto-group (gnus-group-prefixed-name
7944                            (car art-group) to-method))
7945                (entry
7946                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7947                (info (nth 2 entry))
7948                (to-group (gnus-info-group info))
7949                to-marks)
7950           ;; Update the group that has been moved to.
7951           (when (and info
7952                      (memq action '(move copy)))
7953             (unless (member to-group to-groups)
7954               (push to-group to-groups))
7955
7956             (unless (memq article gnus-newsgroup-unreads)
7957               (push 'read to-marks)
7958               (gnus-info-set-read
7959                info (gnus-add-to-range (gnus-info-read info)
7960                                        (list (cdr art-group)))))
7961
7962             ;; See whether the article is to be put in the cache.
7963             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7964                              default-marks
7965                            no-expire-marks))
7966                   (to-article (cdr art-group)))
7967
7968               ;; Enter the article into the cache in the new group,
7969               ;; if that is required.
7970               (when gnus-use-cache
7971                 (gnus-cache-possibly-enter-article
7972                  to-group to-article
7973                  (let ((header (copy-sequence
7974                                 (gnus-summary-article-header article))))
7975                    (mail-header-set-number header to-article)
7976                    header)
7977                  (memq article gnus-newsgroup-marked)
7978                  (memq article gnus-newsgroup-dormant)
7979                  (memq article gnus-newsgroup-unreads)))
7980
7981               (when gnus-preserve-marks
7982                 ;; Copy any marks over to the new group.
7983                 (when (and (equal to-group gnus-newsgroup-name)
7984                            (not (memq article gnus-newsgroup-unreads)))
7985                   ;; Mark this article as read in this group.
7986                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7987                   (setcdr (gnus-active to-group) to-article)
7988                   (setcdr gnus-newsgroup-active to-article))
7989
7990                 (while marks
7991                   (when (memq article (symbol-value
7992                                        (intern (format "gnus-newsgroup-%s"
7993                                                        (caar marks)))))
7994                     (push (cdar marks) to-marks)
7995                     ;; If the other group is the same as this group,
7996                     ;; then we have to add the mark to the list.
7997                     (when (equal to-group gnus-newsgroup-name)
7998                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7999                            (cons to-article
8000                                  (symbol-value
8001                                   (intern (format "gnus-newsgroup-%s"
8002                                                   (caar marks)))))))
8003                     ;; Copy the marks to other group.
8004                     (gnus-add-marked-articles
8005                      to-group (cdar marks) (list to-article) info))
8006                   (setq marks (cdr marks)))
8007
8008                 (gnus-request-set-mark to-group (list (list (list to-article)
8009                                                             'set
8010                                                             to-marks))))
8011
8012               (gnus-dribble-enter
8013                (concat "(gnus-group-set-info '"
8014                        (gnus-prin1-to-string (gnus-get-info to-group))
8015                        ")"))))
8016
8017           ;; Update the Xref header in this article to point to
8018           ;; the new crossposted article we have just created.
8019           (when (eq action 'crosspost)
8020             (save-excursion
8021               (set-buffer copy-buf)
8022               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8023               (nnheader-replace-header "Xref" new-xref)
8024               (gnus-request-replace-article
8025                article gnus-newsgroup-name (current-buffer)))))
8026
8027         ;;;!!!Why is this necessary?
8028         (set-buffer gnus-summary-buffer)
8029
8030         (gnus-summary-goto-subject article)
8031         (when (eq action 'move)
8032           (gnus-summary-mark-article article gnus-canceled-mark))))
8033       (gnus-summary-remove-process-mark article))
8034     ;; Re-activate all groups that have been moved to.
8035     (while to-groups
8036       (save-excursion
8037         (set-buffer gnus-group-buffer)
8038         (when (gnus-group-goto-group (car to-groups) t)
8039           (gnus-group-get-new-news-this-group 1 t))
8040         (pop to-groups)))
8041
8042     (gnus-kill-buffer copy-buf)
8043     (gnus-summary-position-point)
8044     (gnus-set-mode-line 'summary)))
8045
8046 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8047   "Move the current article to a different newsgroup.
8048 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8049 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8050 re-spool using this method."
8051   (interactive "P")
8052   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8053
8054 (defun gnus-summary-crosspost-article (&optional n)
8055   "Crosspost the current article to some other group."
8056   (interactive "P")
8057   (gnus-summary-move-article n nil nil 'crosspost))
8058
8059 (defcustom gnus-summary-respool-default-method nil
8060   "Default method for respooling an article.
8061 If nil, use to the current newsgroup method."
8062   :type '(choice (gnus-select-method :value (nnml ""))
8063                  (const nil))
8064   :group 'gnus-summary-mail)
8065
8066 (defun gnus-summary-respool-article (&optional n method)
8067   "Respool the current article.
8068 The article will be squeezed through the mail spooling process again,
8069 which means that it will be put in some mail newsgroup or other
8070 depending on `nnmail-split-methods'.
8071 If N is a positive number, respool the N next articles.
8072 If N is a negative number, respool the N previous articles.
8073 If N is nil and any articles have been marked with the process mark,
8074 respool those articles instead.
8075
8076 Respooling can be done both from mail groups and \"real\" newsgroups.
8077 In the former case, the articles in question will be moved from the
8078 current group into whatever groups they are destined to.  In the
8079 latter case, they will be copied into the relevant groups."
8080   (interactive
8081    (list current-prefix-arg
8082          (let* ((methods (gnus-methods-using 'respool))
8083                 (methname
8084                  (symbol-name (or gnus-summary-respool-default-method
8085                                   (car (gnus-find-method-for-group
8086                                         gnus-newsgroup-name)))))
8087                 (method
8088                  (gnus-completing-read
8089                   methname "What backend do you want to use when respooling?"
8090                   methods nil t nil 'gnus-mail-method-history))
8091                 ms)
8092            (cond
8093             ((zerop (length (setq ms (gnus-servers-using-backend
8094                                       (intern method)))))
8095              (list (intern method) ""))
8096             ((= 1 (length ms))
8097              (car ms))
8098             (t
8099              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8100                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8101                            ms-alist))))))))
8102   (unless method
8103     (error "No method given for respooling"))
8104   (if (assoc (symbol-name
8105               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8106              (gnus-methods-using 'respool))
8107       (gnus-summary-move-article n nil method)
8108     (gnus-summary-copy-article n nil method)))
8109
8110 (defun gnus-summary-import-article (file &optional edit)
8111   "Import an arbitrary file into a mail newsgroup."
8112   (interactive "fImport file: \nP")
8113   (let ((group gnus-newsgroup-name)
8114         (now (current-time))
8115         atts lines group-art)
8116     (unless (gnus-check-backend-function 'request-accept-article group)
8117       (error "%s does not support article importing" group))
8118     (or (file-readable-p file)
8119         (not (file-regular-p file))
8120         (error "Can't read %s" file))
8121     (save-excursion
8122       (set-buffer (gnus-get-buffer-create " *import file*"))
8123       (erase-buffer)
8124       (nnheader-insert-file-contents file)
8125       (goto-char (point-min))
8126       (unless (nnheader-article-p)
8127         ;; This doesn't look like an article, so we fudge some headers.
8128         (setq atts (file-attributes file)
8129               lines (count-lines (point-min) (point-max)))
8130         (insert "From: " (read-string "From: ") "\n"
8131                 "Subject: " (read-string "Subject: ") "\n"
8132                 "Date: " (message-make-date (nth 5 atts)) "\n"
8133                 "Message-ID: " (message-make-message-id) "\n"
8134                 "Lines: " (int-to-string lines) "\n"
8135                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8136       (setq group-art (gnus-request-accept-article group nil t))
8137       (kill-buffer (current-buffer)))
8138     (setq gnus-newsgroup-active (gnus-activate-group group))
8139     (forward-line 1)
8140     (gnus-summary-goto-article (cdr group-art) nil t)
8141     (when edit
8142       (gnus-summary-edit-article))))
8143
8144 (defun gnus-summary-create-article ()
8145   "Create an article in a mail newsgroup."
8146   (interactive)
8147   (let ((group gnus-newsgroup-name)
8148         (now (current-time))
8149         group-art)
8150     (unless (gnus-check-backend-function 'request-accept-article group)
8151       (error "%s does not support article importing" group))
8152     (save-excursion
8153       (set-buffer (gnus-get-buffer-create " *import file*"))
8154       (erase-buffer)
8155       (goto-char (point-min))
8156       ;; This doesn't look like an article, so we fudge some headers.
8157       (insert "From: " (read-string "From: ") "\n"
8158               "Subject: " (read-string "Subject: ") "\n"
8159               "Date: " (message-make-date now) "\n"
8160               "Message-ID: " (message-make-message-id) "\n")
8161       (setq group-art (gnus-request-accept-article group nil t))
8162       (kill-buffer (current-buffer)))
8163     (setq gnus-newsgroup-active (gnus-activate-group group))
8164     (forward-line 1)
8165     (gnus-summary-goto-article (cdr group-art) nil t)
8166     (gnus-summary-edit-article)))
8167
8168 (defun gnus-summary-article-posted-p ()
8169   "Say whether the current (mail) article is available from news as well.
8170 This will be the case if the article has both been mailed and posted."
8171   (interactive)
8172   (let ((id (mail-header-references (gnus-summary-article-header)))
8173         (gnus-override-method (car (gnus-refer-article-methods))))
8174     (if (gnus-request-head id "")
8175         (gnus-message 2 "The current message was found on %s"
8176                       gnus-override-method)
8177       (gnus-message 2 "The current message couldn't be found on %s"
8178                     gnus-override-method)
8179       nil)))
8180
8181 (defun gnus-summary-expire-articles (&optional now)
8182   "Expire all articles that are marked as expirable in the current group."
8183   (interactive)
8184   (when (gnus-check-backend-function
8185          'request-expire-articles gnus-newsgroup-name)
8186     ;; This backend supports expiry.
8187     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8188            (expirable (if total
8189                           (progn
8190                             ;; We need to update the info for
8191                             ;; this group for `gnus-list-of-read-articles'
8192                             ;; to give us the right answer.
8193                             (gnus-run-hooks 'gnus-exit-group-hook)
8194                             (gnus-summary-update-info)
8195                             (gnus-list-of-read-articles gnus-newsgroup-name))
8196                         (setq gnus-newsgroup-expirable
8197                               (sort gnus-newsgroup-expirable '<))))
8198            (expiry-wait (if now 'immediate
8199                           (gnus-group-find-parameter
8200                            gnus-newsgroup-name 'expiry-wait)))
8201            (nnmail-expiry-target
8202             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8203                 nnmail-expiry-target))
8204            es)
8205       (when expirable
8206         ;; There are expirable articles in this group, so we run them
8207         ;; through the expiry process.
8208         (gnus-message 6 "Expiring articles...")
8209         (unless (gnus-check-group gnus-newsgroup-name)
8210           (error "Can't open server for %s" gnus-newsgroup-name))
8211         ;; The list of articles that weren't expired is returned.
8212         (save-excursion
8213           (if expiry-wait
8214               (let ((nnmail-expiry-wait-function nil)
8215                     (nnmail-expiry-wait expiry-wait))
8216                 (setq es (gnus-request-expire-articles
8217                           expirable gnus-newsgroup-name)))
8218             (setq es (gnus-request-expire-articles
8219                       expirable gnus-newsgroup-name)))
8220           (unless total
8221             (setq gnus-newsgroup-expirable es))
8222           ;; We go through the old list of expirable, and mark all
8223           ;; really expired articles as nonexistent.
8224           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8225             (let ((gnus-use-cache nil))
8226               (while expirable
8227                 (unless (memq (car expirable) es)
8228                   (when (gnus-data-find (car expirable))
8229                     (gnus-summary-mark-article
8230                      (car expirable) gnus-canceled-mark)))
8231                 (setq expirable (cdr expirable))))))
8232         (gnus-message 6 "Expiring articles...done")))))
8233
8234 (defun gnus-summary-expire-articles-now ()
8235   "Expunge all expirable articles in the current group.
8236 This means that *all* articles that are marked as expirable will be
8237 deleted forever, right now."
8238   (interactive)
8239   (or gnus-expert-user
8240       (gnus-yes-or-no-p
8241        "Are you really, really, really sure you want to delete all these messages? ")
8242       (error "Phew!"))
8243   (gnus-summary-expire-articles t))
8244
8245 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8246 (defun gnus-summary-delete-article (&optional n)
8247   "Delete the N next (mail) articles.
8248 This command actually deletes articles.  This is not a marking
8249 command.  The article will disappear forever from your life, never to
8250 return.
8251 If N is negative, delete backwards.
8252 If N is nil and articles have been marked with the process mark,
8253 delete these instead."
8254   (interactive "P")
8255   (unless (gnus-check-backend-function 'request-expire-articles
8256                                        gnus-newsgroup-name)
8257     (error "The current newsgroup does not support article deletion"))
8258   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8259     (error "Couldn't open server"))
8260   ;; Compute the list of articles to delete.
8261   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8262         not-deleted)
8263     (if (and gnus-novice-user
8264              (not (gnus-yes-or-no-p
8265                    (format "Do you really want to delete %s forever? "
8266                            (if (> (length articles) 1)
8267                                (format "these %s articles" (length articles))
8268                              "this article")))))
8269         ()
8270       ;; Delete the articles.
8271       (setq not-deleted (gnus-request-expire-articles
8272                          articles gnus-newsgroup-name 'force))
8273       (while articles
8274         (gnus-summary-remove-process-mark (car articles))
8275         ;; The backend might not have been able to delete the article
8276         ;; after all.
8277         (unless (memq (car articles) not-deleted)
8278           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8279         (setq articles (cdr articles)))
8280       (when not-deleted
8281         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8282     (gnus-summary-position-point)
8283     (gnus-set-mode-line 'summary)
8284     not-deleted))
8285
8286 (defun gnus-summary-edit-article (&optional force)
8287   "Edit the current article.
8288 This will have permanent effect only in mail groups.
8289 If FORCE is non-nil, allow editing of articles even in read-only
8290 groups."
8291   (interactive "P")
8292   (save-excursion
8293     (set-buffer gnus-summary-buffer)
8294     (let ((mail-parse-charset gnus-newsgroup-charset)
8295           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8296       (gnus-set-global-variables)
8297       (when (and (not force)
8298                  (gnus-group-read-only-p))
8299         (error "The current newsgroup does not support article editing"))
8300       (gnus-summary-show-article t)
8301       (gnus-article-edit-article
8302        'ignore
8303        `(lambda (no-highlight)
8304           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8305                 (message-options message-options)
8306                 (message-options-set-recipient)
8307                 (mail-parse-ignored-charsets
8308                  ',gnus-newsgroup-ignored-charsets))
8309             (gnus-summary-edit-article-done
8310              ,(or (mail-header-references gnus-current-headers) "")
8311              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8312
8313 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8314
8315 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8316                                                  no-highlight)
8317   "Make edits to the current article permanent."
8318   (interactive)
8319   (save-excursion
8320     ;; The buffer restriction contains the entire article if it exists.
8321     (when (article-goto-body)
8322       (let ((lines (count-lines (point) (point-max)))
8323             (length (- (point-max) (point)))
8324             (case-fold-search t)
8325             (body (copy-marker (point))))
8326         (goto-char (point-min))
8327         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8328           (delete-region (match-beginning 1) (match-end 1))
8329           (insert (number-to-string length)))
8330         (goto-char (point-min))
8331         (when (re-search-forward
8332                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8333           (delete-region (match-beginning 1) (match-end 1))
8334           (insert (number-to-string length)))
8335         (goto-char (point-min))
8336         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8337           (delete-region (match-beginning 1) (match-end 1))
8338           (insert (number-to-string lines))))))
8339   ;; Replace the article.
8340   (let ((buf (current-buffer)))
8341     (with-temp-buffer
8342       (insert-buffer-substring buf)
8343
8344       (if (and (not read-only)
8345                (not (gnus-request-replace-article
8346                      (cdr gnus-article-current) (car gnus-article-current)
8347                      (current-buffer) t)))
8348           (error "Couldn't replace article")
8349         ;; Update the summary buffer.
8350         (if (and references
8351                  (equal (message-tokenize-header references " ")
8352                         (message-tokenize-header
8353                          (or (message-fetch-field "references") "") " ")))
8354             ;; We only have to update this line.
8355             (save-excursion
8356               (save-restriction
8357                 (message-narrow-to-head)
8358                 (let ((head (buffer-string))
8359                       header)
8360                   (with-temp-buffer
8361                     (insert (format "211 %d Article retrieved.\n"
8362                                     (cdr gnus-article-current)))
8363                     (insert head)
8364                     (insert ".\n")
8365                     (let ((nntp-server-buffer (current-buffer)))
8366                       (setq header (car (gnus-get-newsgroup-headers
8367                                          (save-excursion
8368                                            (set-buffer gnus-summary-buffer)
8369                                            gnus-newsgroup-dependencies)
8370                                          t))))
8371                     (save-excursion
8372                       (set-buffer gnus-summary-buffer)
8373                       (gnus-data-set-header
8374                        (gnus-data-find (cdr gnus-article-current))
8375                        header)
8376                       (gnus-summary-update-article-line
8377                        (cdr gnus-article-current) header))))))
8378           ;; Update threads.
8379           (set-buffer (or buffer gnus-summary-buffer))
8380           (gnus-summary-update-article (cdr gnus-article-current)))
8381         ;; Prettify the article buffer again.
8382         (unless no-highlight
8383           (save-excursion
8384             (set-buffer gnus-article-buffer)
8385             ;;;!!! Fix this -- article should be rehighlighted.
8386             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8387             (set-buffer gnus-original-article-buffer)
8388             (gnus-request-article
8389              (cdr gnus-article-current)
8390              (car gnus-article-current) (current-buffer))))
8391         ;; Prettify the summary buffer line.
8392         (when (gnus-visual-p 'summary-highlight 'highlight)
8393           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8394
8395 (defun gnus-summary-edit-wash (key)
8396   "Perform editing command KEY in the article buffer."
8397   (interactive
8398    (list
8399     (progn
8400       (message "%s" (concat (this-command-keys) "- "))
8401       (read-char))))
8402   (message "")
8403   (gnus-summary-edit-article)
8404   (execute-kbd-macro (concat (this-command-keys) key))
8405   (gnus-article-edit-done))
8406
8407 ;;; Respooling
8408
8409 (defun gnus-summary-respool-query (&optional silent trace)
8410   "Query where the respool algorithm would put this article."
8411   (interactive)
8412   (let (gnus-mark-article-hook)
8413     (gnus-summary-select-article)
8414     (save-excursion
8415       (set-buffer gnus-original-article-buffer)
8416       (save-restriction
8417         (message-narrow-to-head)
8418         (let ((groups (nnmail-article-group 'identity trace)))
8419           (unless silent
8420             (if groups
8421                 (message "This message would go to %s"
8422                          (mapconcat 'car groups ", "))
8423               (message "This message would go to no groups"))
8424             groups))))))
8425
8426 (defun gnus-summary-respool-trace ()
8427   "Trace where the respool algorithm would put this article.
8428 Display a buffer showing all fancy splitting patterns which matched."
8429   (interactive)
8430   (gnus-summary-respool-query nil t))
8431
8432 ;; Summary marking commands.
8433
8434 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8435   "Mark articles which has the same subject as read, and then select the next.
8436 If UNMARK is positive, remove any kind of mark.
8437 If UNMARK is negative, tick articles."
8438   (interactive "P")
8439   (when unmark
8440     (setq unmark (prefix-numeric-value unmark)))
8441   (let ((count
8442          (gnus-summary-mark-same-subject
8443           (gnus-summary-article-subject) unmark)))
8444     ;; Select next unread article.  If auto-select-same mode, should
8445     ;; select the first unread article.
8446     (gnus-summary-next-article t (and gnus-auto-select-same
8447                                       (gnus-summary-article-subject)))
8448     (gnus-message 7 "%d article%s marked as %s"
8449                   count (if (= count 1) " is" "s are")
8450                   (if unmark "unread" "read"))))
8451
8452 (defun gnus-summary-kill-same-subject (&optional unmark)
8453   "Mark articles which has the same subject as read.
8454 If UNMARK is positive, remove any kind of mark.
8455 If UNMARK is negative, tick articles."
8456   (interactive "P")
8457   (when unmark
8458     (setq unmark (prefix-numeric-value unmark)))
8459   (let ((count
8460          (gnus-summary-mark-same-subject
8461           (gnus-summary-article-subject) unmark)))
8462     ;; If marked as read, go to next unread subject.
8463     (when (null unmark)
8464       ;; Go to next unread subject.
8465       (gnus-summary-next-subject 1 t))
8466     (gnus-message 7 "%d articles are marked as %s"
8467                   count (if unmark "unread" "read"))))
8468
8469 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8470   "Mark articles with same SUBJECT as read, and return marked number.
8471 If optional argument UNMARK is positive, remove any kinds of marks.
8472 If optional argument UNMARK is negative, mark articles as unread instead."
8473   (let ((count 1))
8474     (save-excursion
8475       (cond
8476        ((null unmark)                   ; Mark as read.
8477         (while (and
8478                 (progn
8479                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8480                   (gnus-summary-show-thread) t)
8481                 (gnus-summary-find-subject subject))
8482           (setq count (1+ count))))
8483        ((> unmark 0)                    ; Tick.
8484         (while (and
8485                 (progn
8486                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8487                   (gnus-summary-show-thread) t)
8488                 (gnus-summary-find-subject subject))
8489           (setq count (1+ count))))
8490        (t                               ; Mark as unread.
8491         (while (and
8492                 (progn
8493                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8494                   (gnus-summary-show-thread) t)
8495                 (gnus-summary-find-subject subject))
8496           (setq count (1+ count)))))
8497       (gnus-set-mode-line 'summary)
8498       ;; Return the number of marked articles.
8499       count)))
8500
8501 (defun gnus-summary-mark-as-processable (n &optional unmark)
8502   "Set the process mark on the next N articles.
8503 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8504 the process mark instead.  The difference between N and the actual
8505 number of articles marked is returned."
8506   (interactive "P")
8507   (if (and (null n) (gnus-region-active-p))
8508       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8509     (setq n (prefix-numeric-value n))
8510     (let ((backward (< n 0))
8511           (n (abs n)))
8512       (while (and
8513               (> n 0)
8514               (if unmark
8515                 (gnus-summary-remove-process-mark
8516                  (gnus-summary-article-number))
8517                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8518               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8519         (setq n (1- n)))
8520       (when (/= 0 n)
8521         (gnus-message 7 "No more articles"))
8522       (gnus-summary-recenter)
8523       (gnus-summary-position-point)
8524       n)))
8525
8526 (defun gnus-summary-unmark-as-processable (n)
8527   "Remove the process mark from the next N articles.
8528 If N is negative, unmark backward instead.  The difference between N and
8529 the actual number of articles unmarked is returned."
8530   (interactive "P")
8531   (gnus-summary-mark-as-processable n t))
8532
8533 (defun gnus-summary-unmark-all-processable ()
8534   "Remove the process mark from all articles."
8535   (interactive)
8536   (save-excursion
8537     (while gnus-newsgroup-processable
8538       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8539   (gnus-summary-position-point))
8540
8541 (defun gnus-summary-add-mark (article type)
8542   "Mark ARTICLE with a mark of TYPE."
8543   (let ((vtype (car (assq type gnus-article-mark-lists)))
8544         var)
8545     (if (not vtype)
8546         (error "No such mark type: %s" type)
8547       (setq var (intern (format "gnus-newsgroup-%s" type)))
8548       (set var (cons article (symbol-value var)))
8549       (if (memq type '(processable cached replied saved))
8550           (gnus-summary-update-secondary-mark article)
8551         ;;; !!! This is bobus.  We should find out what primary
8552         ;;; !!! mark we want to set.
8553         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8554
8555 (defun gnus-summary-mark-as-expirable (n)
8556   "Mark N articles forward as expirable.
8557 If N is negative, mark backward instead.  The difference between N and
8558 the actual number of articles marked is returned."
8559   (interactive "p")
8560   (gnus-summary-mark-forward n gnus-expirable-mark))
8561
8562 (defun gnus-summary-mark-article-as-replied (article)
8563   "Mark ARTICLE replied and update the summary line."
8564   (push article gnus-newsgroup-replied)
8565   (let ((buffer-read-only nil))
8566     (when (gnus-summary-goto-subject article nil t)
8567       (gnus-summary-update-secondary-mark article))))
8568
8569 (defun gnus-summary-set-bookmark (article)
8570   "Set a bookmark in current article."
8571   (interactive (list (gnus-summary-article-number)))
8572   (when (or (not (get-buffer gnus-article-buffer))
8573             (not gnus-current-article)
8574             (not gnus-article-current)
8575             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8576     (error "No current article selected"))
8577   ;; Remove old bookmark, if one exists.
8578   (let ((old (assq article gnus-newsgroup-bookmarks)))
8579     (when old
8580       (setq gnus-newsgroup-bookmarks
8581             (delq old gnus-newsgroup-bookmarks))))
8582   ;; Set the new bookmark, which is on the form
8583   ;; (article-number . line-number-in-body).
8584   (push
8585    (cons article
8586          (save-excursion
8587            (set-buffer gnus-article-buffer)
8588            (count-lines
8589             (min (point)
8590                  (save-excursion
8591                    (goto-char (point-min))
8592                    (search-forward "\n\n" nil t)
8593                    (point)))
8594             (point))))
8595    gnus-newsgroup-bookmarks)
8596   (gnus-message 6 "A bookmark has been added to the current article."))
8597
8598 (defun gnus-summary-remove-bookmark (article)
8599   "Remove the bookmark from the current article."
8600   (interactive (list (gnus-summary-article-number)))
8601   ;; Remove old bookmark, if one exists.
8602   (let ((old (assq article gnus-newsgroup-bookmarks)))
8603     (if old
8604         (progn
8605           (setq gnus-newsgroup-bookmarks
8606                 (delq old gnus-newsgroup-bookmarks))
8607           (gnus-message 6 "Removed bookmark."))
8608       (gnus-message 6 "No bookmark in current article."))))
8609
8610 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8611 (defun gnus-summary-mark-as-dormant (n)
8612   "Mark N articles forward as dormant.
8613 If N is negative, mark backward instead.  The difference between N and
8614 the actual number of articles marked is returned."
8615   (interactive "p")
8616   (gnus-summary-mark-forward n gnus-dormant-mark))
8617
8618 (defun gnus-summary-set-process-mark (article)
8619   "Set the process mark on ARTICLE and update the summary line."
8620   (setq gnus-newsgroup-processable
8621         (cons article
8622               (delq article gnus-newsgroup-processable)))
8623   (when (gnus-summary-goto-subject article)
8624     (gnus-summary-show-thread)
8625     (gnus-summary-goto-subject article)
8626     (gnus-summary-update-secondary-mark article)))
8627
8628 (defun gnus-summary-remove-process-mark (article)
8629   "Remove the process mark from ARTICLE and update the summary line."
8630   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8631   (when (gnus-summary-goto-subject article)
8632     (gnus-summary-show-thread)
8633     (gnus-summary-goto-subject article)
8634     (gnus-summary-update-secondary-mark article)))
8635
8636 (defun gnus-summary-set-saved-mark (article)
8637   "Set the process mark on ARTICLE and update the summary line."
8638   (push article gnus-newsgroup-saved)
8639   (when (gnus-summary-goto-subject article)
8640     (gnus-summary-update-secondary-mark article)))
8641
8642 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8643   "Mark N articles as read forwards.
8644 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8645 The difference between N and the actual number of articles marked is
8646 returned.
8647 Iff NO-EXPIRE, auto-expiry will be inhibited."
8648   (interactive "p")
8649   (gnus-summary-show-thread)
8650   (let ((backward (< n 0))
8651         (gnus-summary-goto-unread
8652          (and gnus-summary-goto-unread
8653               (not (eq gnus-summary-goto-unread 'never))
8654               (not (memq mark (list gnus-unread-mark
8655                                     gnus-ticked-mark gnus-dormant-mark)))))
8656         (n (abs n))
8657         (mark (or mark gnus-del-mark)))
8658     (while (and (> n 0)
8659                 (gnus-summary-mark-article nil mark no-expire)
8660                 (zerop (gnus-summary-next-subject
8661                         (if backward -1 1)
8662                         (and gnus-summary-goto-unread
8663                              (not (eq gnus-summary-goto-unread 'never)))
8664                         t)))
8665       (setq n (1- n)))
8666     (when (/= 0 n)
8667       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8668     (gnus-summary-recenter)
8669     (gnus-summary-position-point)
8670     (gnus-set-mode-line 'summary)
8671     n))
8672
8673 (defun gnus-summary-mark-article-as-read (mark)
8674   "Mark the current article quickly as read with MARK."
8675   (let ((article (gnus-summary-article-number)))
8676     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8677     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8678     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8679     (push (cons article mark) gnus-newsgroup-reads)
8680     ;; Possibly remove from cache, if that is used.
8681     (when gnus-use-cache
8682       (gnus-cache-enter-remove-article article))
8683     ;; Allow the backend to change the mark.
8684     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8685     ;; Check for auto-expiry.
8686     (when (and gnus-newsgroup-auto-expire
8687                (memq mark gnus-auto-expirable-marks))
8688       (setq mark gnus-expirable-mark)
8689       ;; Let the backend know about the mark change.
8690       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8691       (push article gnus-newsgroup-expirable))
8692     ;; Set the mark in the buffer.
8693     (gnus-summary-update-mark mark 'unread)
8694     t))
8695
8696 (defun gnus-summary-mark-article-as-unread (mark)
8697   "Mark the current article quickly as unread with MARK."
8698   (let* ((article (gnus-summary-article-number))
8699          (old-mark (gnus-summary-article-mark article)))
8700     ;; Allow the backend to change the mark.
8701     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8702     (if (eq mark old-mark)
8703         t
8704       (if (<= article 0)
8705           (progn
8706             (gnus-error 1 "Can't mark negative article numbers")
8707             nil)
8708         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8709         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8710         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8711         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8712         (cond ((= mark gnus-ticked-mark)
8713                (push article gnus-newsgroup-marked))
8714               ((= mark gnus-dormant-mark)
8715                (push article gnus-newsgroup-dormant))
8716               (t
8717                (push article gnus-newsgroup-unreads)))
8718         (gnus-pull article gnus-newsgroup-reads)
8719
8720         ;; See whether the article is to be put in the cache.
8721         (and gnus-use-cache
8722              (vectorp (gnus-summary-article-header article))
8723              (save-excursion
8724                (gnus-cache-possibly-enter-article
8725                 gnus-newsgroup-name article
8726                 (gnus-summary-article-header article)
8727                 (= mark gnus-ticked-mark)
8728                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8729
8730         ;; Fix the mark.
8731         (gnus-summary-update-mark mark 'unread)
8732         t))))
8733
8734 (defun gnus-summary-mark-article (&optional article mark no-expire)
8735   "Mark ARTICLE with MARK.  MARK can be any character.
8736 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8737 `??' (dormant) and `?E' (expirable).
8738 If MARK is nil, then the default character `?r' is used.
8739 If ARTICLE is nil, then the article on the current line will be
8740 marked.
8741 Iff NO-EXPIRE, auto-expiry will be inhibited."
8742   ;; The mark might be a string.
8743   (when (stringp mark)
8744     (setq mark (aref mark 0)))
8745   ;; If no mark is given, then we check auto-expiring.
8746   (when (null mark)
8747     (setq mark gnus-del-mark))
8748   (when (and (not no-expire)
8749              gnus-newsgroup-auto-expire
8750              (memq mark gnus-auto-expirable-marks))
8751     (setq mark gnus-expirable-mark))
8752   (let ((article (or article (gnus-summary-article-number)))
8753         (old-mark (gnus-summary-article-mark article)))
8754     ;; Allow the backend to change the mark.
8755     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8756     (if (eq mark old-mark)
8757         t
8758       (unless article
8759         (error "No article on current line"))
8760       (if (not (if (or (= mark gnus-unread-mark)
8761                        (= mark gnus-ticked-mark)
8762                        (= mark gnus-dormant-mark))
8763                    (gnus-mark-article-as-unread article mark)
8764                  (gnus-mark-article-as-read article mark)))
8765           t
8766         ;; See whether the article is to be put in the cache.
8767         (and gnus-use-cache
8768              (not (= mark gnus-canceled-mark))
8769              (vectorp (gnus-summary-article-header article))
8770              (save-excursion
8771                (gnus-cache-possibly-enter-article
8772                 gnus-newsgroup-name article
8773                 (gnus-summary-article-header article)
8774                 (= mark gnus-ticked-mark)
8775                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8776
8777         (when (gnus-summary-goto-subject article nil t)
8778           (let ((buffer-read-only nil))
8779             (gnus-summary-show-thread)
8780             ;; Fix the mark.
8781             (gnus-summary-update-mark mark 'unread)
8782             t))))))
8783
8784 (defun gnus-summary-update-secondary-mark (article)
8785   "Update the secondary (read, process, cache) mark."
8786   (gnus-summary-update-mark
8787    (cond ((memq article gnus-newsgroup-processable)
8788           gnus-process-mark)
8789          ((memq article gnus-newsgroup-cached)
8790           gnus-cached-mark)
8791          ((memq article gnus-newsgroup-replied)
8792           gnus-replied-mark)
8793          ((memq article gnus-newsgroup-saved)
8794           gnus-saved-mark)
8795          (t gnus-no-mark))
8796    'replied)
8797   (when (gnus-visual-p 'summary-highlight 'highlight)
8798     (gnus-run-hooks 'gnus-summary-update-hook))
8799   t)
8800
8801 (defun gnus-summary-update-mark (mark type)
8802   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8803         (buffer-read-only nil))
8804     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8805     (when forward
8806       (when (looking-at "\r")
8807         (incf forward))
8808       (when (<= (+ forward (point)) (point-max))
8809         ;; Go to the right position on the line.
8810         (goto-char (+ forward (point)))
8811         ;; Replace the old mark with the new mark.
8812         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8813         ;; Optionally update the marks by some user rule.
8814         (when (eq type 'unread)
8815           (gnus-data-set-mark
8816            (gnus-data-find (gnus-summary-article-number)) mark)
8817           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8818
8819 (defun gnus-mark-article-as-read (article &optional mark)
8820   "Enter ARTICLE in the pertinent lists and remove it from others."
8821   ;; Make the article expirable.
8822   (let ((mark (or mark gnus-del-mark)))
8823     (if (= mark gnus-expirable-mark)
8824         (push article gnus-newsgroup-expirable)
8825       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8826     ;; Remove from unread and marked lists.
8827     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8828     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8829     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8830     (push (cons article mark) gnus-newsgroup-reads)
8831     ;; Possibly remove from cache, if that is used.
8832     (when gnus-use-cache
8833       (gnus-cache-enter-remove-article article))
8834     t))
8835
8836 (defun gnus-mark-article-as-unread (article &optional mark)
8837   "Enter ARTICLE in the pertinent lists and remove it from others."
8838   (let ((mark (or mark gnus-ticked-mark)))
8839     (if (<= article 0)
8840         (progn
8841           (gnus-error 1 "Can't mark negative article numbers")
8842           nil)
8843       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8844             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8845             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8846             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8847
8848       ;; Unsuppress duplicates?
8849       (when gnus-suppress-duplicates
8850         (gnus-dup-unsuppress-article article))
8851
8852       (cond ((= mark gnus-ticked-mark)
8853              (push article gnus-newsgroup-marked))
8854             ((= mark gnus-dormant-mark)
8855              (push article gnus-newsgroup-dormant))
8856             (t
8857              (push article gnus-newsgroup-unreads)))
8858       (gnus-pull article gnus-newsgroup-reads)
8859       t)))
8860
8861 (defalias 'gnus-summary-mark-as-unread-forward
8862   'gnus-summary-tick-article-forward)
8863 (make-obsolete 'gnus-summary-mark-as-unread-forward
8864                'gnus-summary-tick-article-forward)
8865 (defun gnus-summary-tick-article-forward (n)
8866   "Tick N articles forwards.
8867 If N is negative, tick backwards instead.
8868 The difference between N and the number of articles ticked is returned."
8869   (interactive "p")
8870   (gnus-summary-mark-forward n gnus-ticked-mark))
8871
8872 (defalias 'gnus-summary-mark-as-unread-backward
8873   'gnus-summary-tick-article-backward)
8874 (make-obsolete 'gnus-summary-mark-as-unread-backward
8875                'gnus-summary-tick-article-backward)
8876 (defun gnus-summary-tick-article-backward (n)
8877   "Tick N articles backwards.
8878 The difference between N and the number of articles ticked is returned."
8879   (interactive "p")
8880   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8881
8882 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8883 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8884 (defun gnus-summary-tick-article (&optional article clear-mark)
8885   "Mark current article as unread.
8886 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8887 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8888   (interactive)
8889   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8890                                        gnus-ticked-mark)))
8891
8892 (defun gnus-summary-mark-as-read-forward (n)
8893   "Mark N articles as read forwards.
8894 If N is negative, mark backwards instead.
8895 The difference between N and the actual number of articles marked is
8896 returned."
8897   (interactive "p")
8898   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8899
8900 (defun gnus-summary-mark-as-read-backward (n)
8901   "Mark the N articles as read backwards.
8902 The difference between N and the actual number of articles marked is
8903 returned."
8904   (interactive "p")
8905   (gnus-summary-mark-forward
8906    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8907
8908 (defun gnus-summary-mark-as-read (&optional article mark)
8909   "Mark current article as read.
8910 ARTICLE specifies the article to be marked as read.
8911 MARK specifies a string to be inserted at the beginning of the line."
8912   (gnus-summary-mark-article article mark))
8913
8914 (defun gnus-summary-clear-mark-forward (n)
8915   "Clear marks from N articles forward.
8916 If N is negative, clear backward instead.
8917 The difference between N and the number of marks cleared is returned."
8918   (interactive "p")
8919   (gnus-summary-mark-forward n gnus-unread-mark))
8920
8921 (defun gnus-summary-clear-mark-backward (n)
8922   "Clear marks from N articles backward.
8923 The difference between N and the number of marks cleared is returned."
8924   (interactive "p")
8925   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8926
8927 (defun gnus-summary-mark-unread-as-read ()
8928   "Intended to be used by `gnus-summary-mark-article-hook'."
8929   (when (memq gnus-current-article gnus-newsgroup-unreads)
8930     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8931
8932 (defun gnus-summary-mark-read-and-unread-as-read ()
8933   "Intended to be used by `gnus-summary-mark-article-hook'."
8934   (let ((mark (gnus-summary-article-mark)))
8935     (when (or (gnus-unread-mark-p mark)
8936               (gnus-read-mark-p mark))
8937       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8938
8939 (defun gnus-summary-mark-unread-as-ticked ()
8940    "Intended to be used by `gnus-summary-mark-article-hook'."
8941   (when (memq gnus-current-article gnus-newsgroup-unreads)
8942     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8943
8944 (defun gnus-summary-mark-region-as-read (point mark all)
8945   "Mark all unread articles between point and mark as read.
8946 If given a prefix, mark all articles between point and mark as read,
8947 even ticked and dormant ones."
8948   (interactive "r\nP")
8949   (save-excursion
8950     (let (article)
8951       (goto-char point)
8952       (beginning-of-line)
8953       (while (and
8954               (< (point) mark)
8955               (progn
8956                 (when (or all
8957                           (memq (setq article (gnus-summary-article-number))
8958                                 gnus-newsgroup-unreads))
8959                   (gnus-summary-mark-article article gnus-del-mark))
8960                 t)
8961               (gnus-summary-find-next))))))
8962
8963 (defun gnus-summary-mark-below (score mark)
8964   "Mark articles with score less than SCORE with MARK."
8965   (interactive "P\ncMark: ")
8966   (setq score (if score
8967                   (prefix-numeric-value score)
8968                 (or gnus-summary-default-score 0)))
8969   (save-excursion
8970     (set-buffer gnus-summary-buffer)
8971     (goto-char (point-min))
8972     (while
8973         (progn
8974           (and (< (gnus-summary-article-score) score)
8975                (gnus-summary-mark-article nil mark))
8976           (gnus-summary-find-next)))))
8977
8978 (defun gnus-summary-kill-below (&optional score)
8979   "Mark articles with score below SCORE as read."
8980   (interactive "P")
8981   (gnus-summary-mark-below score gnus-killed-mark))
8982
8983 (defun gnus-summary-clear-above (&optional score)
8984   "Clear all marks from articles with score above SCORE."
8985   (interactive "P")
8986   (gnus-summary-mark-above score gnus-unread-mark))
8987
8988 (defun gnus-summary-tick-above (&optional score)
8989   "Tick all articles with score above SCORE."
8990   (interactive "P")
8991   (gnus-summary-mark-above score gnus-ticked-mark))
8992
8993 (defun gnus-summary-mark-above (score mark)
8994   "Mark articles with score over SCORE with MARK."
8995   (interactive "P\ncMark: ")
8996   (setq score (if score
8997                   (prefix-numeric-value score)
8998                 (or gnus-summary-default-score 0)))
8999   (save-excursion
9000     (set-buffer gnus-summary-buffer)
9001     (goto-char (point-min))
9002     (while (and (progn
9003                   (when (> (gnus-summary-article-score) score)
9004                     (gnus-summary-mark-article nil mark))
9005                   t)
9006                 (gnus-summary-find-next)))))
9007
9008 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9009 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9010 (defun gnus-summary-limit-include-expunged (&optional no-error)
9011   "Display all the hidden articles that were expunged for low scores."
9012   (interactive)
9013   (let ((buffer-read-only nil))
9014     (let ((scored gnus-newsgroup-scored)
9015           headers h)
9016       (while scored
9017         (unless (gnus-summary-article-header (caar scored))
9018           (and (setq h (gnus-number-to-header (caar scored)))
9019                (< (cdar scored) gnus-summary-expunge-below)
9020                (push h headers)))
9021         (setq scored (cdr scored)))
9022       (if (not headers)
9023           (when (not no-error)
9024             (error "No expunged articles hidden"))
9025         (goto-char (point-min))
9026         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9027         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9028         (mapcar (lambda (x) (push (mail-header-number x) 
9029                                   gnus-newsgroup-limit))
9030                 headers)
9031         (gnus-summary-prepare-unthreaded (nreverse headers))
9032         (goto-char (point-min))
9033         (gnus-summary-position-point)
9034         t))))
9035
9036 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9037   "Mark all unread articles in this newsgroup as read.
9038 If prefix argument ALL is non-nil, ticked and dormant articles will
9039 also be marked as read.
9040 If QUIETLY is non-nil, no questions will be asked.
9041 If TO-HERE is non-nil, it should be a point in the buffer.  All
9042 articles before this point will be marked as read.
9043 Note that this function will only catch up the unread article
9044 in the current summary buffer limitation.
9045 The number of articles marked as read is returned."
9046   (interactive "P")
9047   (prog1
9048       (save-excursion
9049         (when (or quietly
9050                   (not gnus-interactive-catchup) ;Without confirmation?
9051                   gnus-expert-user
9052                   (gnus-y-or-n-p
9053                    (if all
9054                        "Mark absolutely all articles as read? "
9055                      "Mark all unread articles as read? ")))
9056           (if (and not-mark
9057                    (not gnus-newsgroup-adaptive)
9058                    (not gnus-newsgroup-auto-expire)
9059                    (not gnus-suppress-duplicates)
9060                    (or (not gnus-use-cache)
9061                        (eq gnus-use-cache 'passive)))
9062               (progn
9063                 (when all
9064                   (setq gnus-newsgroup-marked nil
9065                         gnus-newsgroup-dormant nil))
9066                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9067             ;; We actually mark all articles as canceled, which we
9068             ;; have to do when using auto-expiry or adaptive scoring.
9069             (gnus-summary-show-all-threads)
9070             (when (gnus-summary-first-subject (not all) t)
9071               (while (and
9072                       (if to-here (< (point) to-here) t)
9073                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
9074                       (gnus-summary-find-next (not all) nil nil t))))
9075             (gnus-set-mode-line 'summary))
9076           t))
9077     (gnus-summary-position-point)))
9078
9079 (defun gnus-summary-catchup-to-here (&optional all)
9080   "Mark all unticked articles before the current one as read.
9081 If ALL is non-nil, also mark ticked and dormant articles as read."
9082   (interactive "P")
9083   (save-excursion
9084     (gnus-save-hidden-threads
9085       (let ((beg (point)))
9086         ;; We check that there are unread articles.
9087         (when (or all (gnus-summary-find-prev))
9088           (gnus-summary-catchup all t beg)))))
9089   (gnus-summary-position-point))
9090
9091 (defun gnus-summary-catchup-all (&optional quietly)
9092   "Mark all articles in this newsgroup as read."
9093   (interactive "P")
9094   (gnus-summary-catchup t quietly))
9095
9096 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9097   "Mark all unread articles in this group as read, then exit.
9098 If prefix argument ALL is non-nil, all articles are marked as read."
9099   (interactive "P")
9100   (when (gnus-summary-catchup all quietly nil 'fast)
9101     ;; Select next newsgroup or exit.
9102     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9103              (eq gnus-auto-select-next 'quietly))
9104         (gnus-summary-next-group nil)
9105       (gnus-summary-exit))))
9106
9107 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9108   "Mark all articles in this newsgroup as read, and then exit."
9109   (interactive "P")
9110   (gnus-summary-catchup-and-exit t quietly))
9111
9112 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9113   "Mark all articles in this group as read and select the next group.
9114 If given a prefix, mark all articles, unread as well as ticked, as
9115 read."
9116   (interactive "P")
9117   (save-excursion
9118     (gnus-summary-catchup all))
9119   (gnus-summary-next-group))
9120
9121 ;;;
9122 ;;; with article
9123 ;;;
9124
9125 (defmacro gnus-with-article (article &rest forms)
9126   "Select ARTICLE and perform FORMS in the original article buffer.
9127 Then replace the article with the result."
9128   `(progn
9129      ;; We don't want the article to be marked as read.
9130      (let (gnus-mark-article-hook)
9131        (gnus-summary-select-article t t nil ,article))
9132      (set-buffer gnus-original-article-buffer)
9133      ,@forms
9134      (if (not (gnus-check-backend-function
9135                'request-replace-article (car gnus-article-current)))
9136          (gnus-message 5 "Read-only group; not replacing")
9137        (unless (gnus-request-replace-article
9138                 ,article (car gnus-article-current)
9139                 (current-buffer) t)
9140          (error "Couldn't replace article")))
9141      ;; The cache and backlog have to be flushed somewhat.
9142      (when gnus-keep-backlog
9143        (gnus-backlog-remove-article
9144         (car gnus-article-current) (cdr gnus-article-current)))
9145      (when gnus-use-cache
9146        (gnus-cache-update-article
9147         (car gnus-article-current) (cdr gnus-article-current)))))
9148
9149 (put 'gnus-with-article 'lisp-indent-function 1)
9150 (put 'gnus-with-article 'edebug-form-spec '(form body))
9151
9152 ;; Thread-based commands.
9153
9154 (defun gnus-summary-articles-in-thread (&optional article)
9155   "Return a list of all articles in the current thread.
9156 If ARTICLE is non-nil, return all articles in the thread that starts
9157 with that article."
9158   (let* ((article (or article (gnus-summary-article-number)))
9159          (data (gnus-data-find-list article))
9160          (top-level (gnus-data-level (car data)))
9161          (top-subject
9162           (cond ((null gnus-thread-operation-ignore-subject)
9163                  (gnus-simplify-subject-re
9164                   (mail-header-subject (gnus-data-header (car data)))))
9165                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9166                  (gnus-simplify-subject-fuzzy
9167                   (mail-header-subject (gnus-data-header (car data)))))
9168                 (t nil)))
9169          (end-point (save-excursion
9170                       (if (gnus-summary-go-to-next-thread)
9171                           (point) (point-max))))
9172          articles)
9173     (while (and data
9174                 (< (gnus-data-pos (car data)) end-point))
9175       (when (or (not top-subject)
9176                 (string= top-subject
9177                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9178                              (gnus-simplify-subject-fuzzy
9179                               (mail-header-subject
9180                                (gnus-data-header (car data))))
9181                            (gnus-simplify-subject-re
9182                             (mail-header-subject
9183                              (gnus-data-header (car data)))))))
9184         (push (gnus-data-number (car data)) articles))
9185       (unless (and (setq data (cdr data))
9186                    (> (gnus-data-level (car data)) top-level))
9187         (setq data nil)))
9188     ;; Return the list of articles.
9189     (nreverse articles)))
9190
9191 (defun gnus-summary-rethread-current ()
9192   "Rethread the thread the current article is part of."
9193   (interactive)
9194   (let* ((gnus-show-threads t)
9195          (article (gnus-summary-article-number))
9196          (id (mail-header-id (gnus-summary-article-header)))
9197          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9198     (unless id
9199       (error "No article on the current line"))
9200     (gnus-rebuild-thread id)
9201     (gnus-summary-goto-subject article)))
9202
9203 (defun gnus-summary-reparent-thread ()
9204   "Make the current article child of the marked (or previous) article.
9205
9206 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9207 is non-nil or the Subject: of both articles are the same."
9208   (interactive)
9209   (unless (not (gnus-group-read-only-p))
9210     (error "The current newsgroup does not support article editing"))
9211   (unless (<= (length gnus-newsgroup-processable) 1)
9212     (error "No more than one article may be marked"))
9213   (save-window-excursion
9214     (let ((gnus-article-buffer " *reparent*")
9215           (current-article (gnus-summary-article-number))
9216           ;; First grab the marked article, otherwise one line up.
9217           (parent-article (if (not (null gnus-newsgroup-processable))
9218                               (car gnus-newsgroup-processable)
9219                             (save-excursion
9220                               (if (eq (forward-line -1) 0)
9221                                   (gnus-summary-article-number)
9222                                 (error "Beginning of summary buffer"))))))
9223       (unless (not (eq current-article parent-article))
9224         (error "An article may not be self-referential"))
9225       (let ((message-id (mail-header-id
9226                          (gnus-summary-article-header parent-article))))
9227         (unless (and message-id (not (equal message-id "")))
9228           (error "No message-id in desired parent"))
9229         (gnus-with-article current-article
9230           (save-restriction
9231             (goto-char (point-min))
9232             (message-narrow-to-head)
9233             (if (re-search-forward "^References: " nil t)
9234                 (progn
9235                   (re-search-forward "^[^ \t]" nil t)
9236                   (forward-line -1)
9237                   (end-of-line)
9238                   (insert " " message-id))
9239               (insert "References: " message-id "\n"))))
9240         (set-buffer gnus-summary-buffer)
9241         (gnus-summary-unmark-all-processable)
9242         (gnus-summary-update-article current-article)
9243         (gnus-summary-rethread-current)
9244         (gnus-message 3 "Article %d is now the child of article %d"
9245                       current-article parent-article)))))
9246
9247 (defun gnus-summary-toggle-threads (&optional arg)
9248   "Toggle showing conversation threads.
9249 If ARG is positive number, turn showing conversation threads on."
9250   (interactive "P")
9251   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9252     (setq gnus-show-threads
9253           (if (null arg) (not gnus-show-threads)
9254             (> (prefix-numeric-value arg) 0)))
9255     (gnus-summary-prepare)
9256     (gnus-summary-goto-subject current)
9257     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9258     (gnus-summary-position-point)))
9259
9260 (defun gnus-summary-show-all-threads ()
9261   "Show all threads."
9262   (interactive)
9263   (save-excursion
9264     (let ((buffer-read-only nil))
9265       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9266   (gnus-summary-position-point))
9267
9268 (defun gnus-summary-show-thread ()
9269   "Show thread subtrees.
9270 Returns nil if no thread was there to be shown."
9271   (interactive)
9272   (let ((buffer-read-only nil)
9273         (orig (point))
9274         ;; first goto end then to beg, to have point at beg after let
9275         (end (progn (end-of-line) (point)))
9276         (beg (progn (beginning-of-line) (point))))
9277     (prog1
9278         ;; Any hidden lines here?
9279         (search-forward "\r" end t)
9280       (subst-char-in-region beg end ?\^M ?\n t)
9281       (goto-char orig)
9282       (gnus-summary-position-point))))
9283
9284 (defun gnus-summary-hide-all-threads ()
9285   "Hide all thread subtrees."
9286   (interactive)
9287   (save-excursion
9288     (goto-char (point-min))
9289     (gnus-summary-hide-thread)
9290     (while (zerop (gnus-summary-next-thread 1 t))
9291       (gnus-summary-hide-thread)))
9292   (gnus-summary-position-point))
9293
9294 (defun gnus-summary-hide-thread ()
9295   "Hide thread subtrees.
9296 Returns nil if no threads were there to be hidden."
9297   (interactive)
9298   (let ((buffer-read-only nil)
9299         (start (point))
9300         (article (gnus-summary-article-number)))
9301     (goto-char start)
9302     ;; Go forward until either the buffer ends or the subthread
9303     ;; ends.
9304     (when (and (not (eobp))
9305                (or (zerop (gnus-summary-next-thread 1 t))
9306                    (goto-char (point-max))))
9307       (prog1
9308           (if (and (> (point) start)
9309                    (search-backward "\n" start t))
9310               (progn
9311                 (subst-char-in-region start (point) ?\n ?\^M)
9312                 (gnus-summary-goto-subject article))
9313             (goto-char start)
9314             nil)))))
9315
9316 (defun gnus-summary-go-to-next-thread (&optional previous)
9317   "Go to the same level (or less) next thread.
9318 If PREVIOUS is non-nil, go to previous thread instead.
9319 Return the article number moved to, or nil if moving was impossible."
9320   (let ((level (gnus-summary-thread-level))
9321         (way (if previous -1 1))
9322         (beg (point)))
9323     (forward-line way)
9324     (while (and (not (eobp))
9325                 (< level (gnus-summary-thread-level)))
9326       (forward-line way))
9327     (if (eobp)
9328         (progn
9329           (goto-char beg)
9330           nil)
9331       (setq beg (point))
9332       (prog1
9333           (gnus-summary-article-number)
9334         (goto-char beg)))))
9335
9336 (defun gnus-summary-next-thread (n &optional silent)
9337   "Go to the same level next N'th thread.
9338 If N is negative, search backward instead.
9339 Returns the difference between N and the number of skips actually
9340 done.
9341
9342 If SILENT, don't output messages."
9343   (interactive "p")
9344   (let ((backward (< n 0))
9345         (n (abs n)))
9346     (while (and (> n 0)
9347                 (gnus-summary-go-to-next-thread backward))
9348       (decf n))
9349     (unless silent
9350       (gnus-summary-position-point))
9351     (when (and (not silent) (/= 0 n))
9352       (gnus-message 7 "No more threads"))
9353     n))
9354
9355 (defun gnus-summary-prev-thread (n)
9356   "Go to the same level previous N'th thread.
9357 Returns the difference between N and the number of skips actually
9358 done."
9359   (interactive "p")
9360   (gnus-summary-next-thread (- n)))
9361
9362 (defun gnus-summary-go-down-thread ()
9363   "Go down one level in the current thread."
9364   (let ((children (gnus-summary-article-children)))
9365     (when children
9366       (gnus-summary-goto-subject (car children)))))
9367
9368 (defun gnus-summary-go-up-thread ()
9369   "Go up one level in the current thread."
9370   (let ((parent (gnus-summary-article-parent)))
9371     (when parent
9372       (gnus-summary-goto-subject parent))))
9373
9374 (defun gnus-summary-down-thread (n)
9375   "Go down thread N steps.
9376 If N is negative, go up instead.
9377 Returns the difference between N and how many steps down that were
9378 taken."
9379   (interactive "p")
9380   (let ((up (< n 0))
9381         (n (abs n)))
9382     (while (and (> n 0)
9383                 (if up (gnus-summary-go-up-thread)
9384                   (gnus-summary-go-down-thread)))
9385       (setq n (1- n)))
9386     (gnus-summary-position-point)
9387     (when (/= 0 n)
9388       (gnus-message 7 "Can't go further"))
9389     n))
9390
9391 (defun gnus-summary-up-thread (n)
9392   "Go up thread N steps.
9393 If N is negative, go up instead.
9394 Returns the difference between N and how many steps down that were
9395 taken."
9396   (interactive "p")
9397   (gnus-summary-down-thread (- n)))
9398
9399 (defun gnus-summary-top-thread ()
9400   "Go to the top of the thread."
9401   (interactive)
9402   (while (gnus-summary-go-up-thread))
9403   (gnus-summary-article-number))
9404
9405 (defun gnus-summary-kill-thread (&optional unmark)
9406   "Mark articles under current thread as read.
9407 If the prefix argument is positive, remove any kinds of marks.
9408 If the prefix argument is negative, tick articles instead."
9409   (interactive "P")
9410   (when unmark
9411     (setq unmark (prefix-numeric-value unmark)))
9412   (let ((articles (gnus-summary-articles-in-thread)))
9413     (save-excursion
9414       ;; Expand the thread.
9415       (gnus-summary-show-thread)
9416       ;; Mark all the articles.
9417       (while articles
9418         (gnus-summary-goto-subject (car articles))
9419         (cond ((null unmark)
9420                (gnus-summary-mark-article-as-read gnus-killed-mark))
9421               ((> unmark 0)
9422                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9423               (t
9424                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9425         (setq articles (cdr articles))))
9426     ;; Hide killed subtrees.
9427     (and (null unmark)
9428          gnus-thread-hide-killed
9429          (gnus-summary-hide-thread))
9430     ;; If marked as read, go to next unread subject.
9431     (when (null unmark)
9432       ;; Go to next unread subject.
9433       (gnus-summary-next-subject 1 t)))
9434   (gnus-set-mode-line 'summary))
9435
9436 ;; Summary sorting commands
9437
9438 (defun gnus-summary-sort-by-number (&optional reverse)
9439   "Sort the summary buffer by article number.
9440 Argument REVERSE means reverse order."
9441   (interactive "P")
9442   (gnus-summary-sort 'number reverse))
9443
9444 (defun gnus-summary-sort-by-author (&optional reverse)
9445   "Sort the summary buffer by author name alphabetically.
9446 If `case-fold-search' is non-nil, case of letters is ignored.
9447 Argument REVERSE means reverse order."
9448   (interactive "P")
9449   (gnus-summary-sort 'author reverse))
9450
9451 (defun gnus-summary-sort-by-subject (&optional reverse)
9452   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9453 If `case-fold-search' is non-nil, case of letters is ignored.
9454 Argument REVERSE means reverse order."
9455   (interactive "P")
9456   (gnus-summary-sort 'subject reverse))
9457
9458 (defun gnus-summary-sort-by-date (&optional reverse)
9459   "Sort the summary buffer by date.
9460 Argument REVERSE means reverse order."
9461   (interactive "P")
9462   (gnus-summary-sort 'date reverse))
9463
9464 (defun gnus-summary-sort-by-score (&optional reverse)
9465   "Sort the summary buffer by score.
9466 Argument REVERSE means reverse order."
9467   (interactive "P")
9468   (gnus-summary-sort 'score reverse))
9469
9470 (defun gnus-summary-sort-by-lines (&optional reverse)
9471   "Sort the summary buffer by the number of lines.
9472 Argument REVERSE means reverse order."
9473   (interactive "P")
9474   (gnus-summary-sort 'lines reverse))
9475
9476 (defun gnus-summary-sort-by-chars (&optional reverse)
9477   "Sort the summary buffer by article length.
9478 Argument REVERSE means reverse order."
9479   (interactive "P")
9480   (gnus-summary-sort 'chars reverse))
9481
9482 (defun gnus-summary-sort (predicate reverse)
9483   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9484   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9485          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9486          (gnus-thread-sort-functions
9487           (if (not reverse)
9488               thread
9489             `(lambda (t1 t2)
9490                (,thread t2 t1))))
9491          (gnus-sort-gathered-threads-function
9492           gnus-thread-sort-functions)
9493          (gnus-article-sort-functions
9494           (if (not reverse)
9495               article
9496             `(lambda (t1 t2)
9497                (,article t2 t1))))
9498          (buffer-read-only)
9499          (gnus-summary-prepare-hook nil))
9500     ;; We do the sorting by regenerating the threads.
9501     (gnus-summary-prepare)
9502     ;; Hide subthreads if needed.
9503     (when (and gnus-show-threads gnus-thread-hide-subtree)
9504       (gnus-summary-hide-all-threads))))
9505
9506 ;; Summary saving commands.
9507
9508 (defun gnus-summary-save-article (&optional n not-saved)
9509   "Save the current article using the default saver function.
9510 If N is a positive number, save the N next articles.
9511 If N is a negative number, save the N previous articles.
9512 If N is nil and any articles have been marked with the process mark,
9513 save those articles instead.
9514 The variable `gnus-default-article-saver' specifies the saver function."
9515   (interactive "P")
9516   (let* ((articles (gnus-summary-work-articles n))
9517          (save-buffer (save-excursion
9518                         (nnheader-set-temp-buffer " *Gnus Save*")))
9519          (num (length articles))
9520          header file)
9521     (dolist (article articles)
9522       (setq header (gnus-summary-article-header article))
9523       (if (not (vectorp header))
9524           ;; This is a pseudo-article.
9525           (if (assq 'name header)
9526               (gnus-copy-file (cdr (assq 'name header)))
9527             (gnus-message 1 "Article %d is unsaveable" article))
9528         ;; This is a real article.
9529         (save-window-excursion
9530           (gnus-summary-select-article t nil nil article))
9531         (save-excursion
9532           (set-buffer save-buffer)
9533           (erase-buffer)
9534           (insert-buffer-substring gnus-original-article-buffer))
9535         (setq file (gnus-article-save save-buffer file num))
9536         (gnus-summary-remove-process-mark article)
9537         (unless not-saved
9538           (gnus-summary-set-saved-mark article))))
9539     (gnus-kill-buffer save-buffer)
9540     (gnus-summary-position-point)
9541     (gnus-set-mode-line 'summary)
9542     n))
9543
9544 (defun gnus-summary-pipe-output (&optional arg)
9545   "Pipe the current article to a subprocess.
9546 If N is a positive number, pipe the N next articles.
9547 If N is a negative number, pipe the N previous articles.
9548 If N is nil and any articles have been marked with the process mark,
9549 pipe those articles instead."
9550   (interactive "P")
9551   (require 'gnus-art)
9552   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9553     (gnus-summary-save-article arg t))
9554   (gnus-configure-windows 'pipe))
9555
9556 (defun gnus-summary-save-article-mail (&optional arg)
9557   "Append the current article to an mail file.
9558 If N is a positive number, save the N next articles.
9559 If N is a negative number, save the N previous articles.
9560 If N is nil and any articles have been marked with the process mark,
9561 save those articles instead."
9562   (interactive "P")
9563   (require 'gnus-art)
9564   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9565     (gnus-summary-save-article arg)))
9566
9567 (defun gnus-summary-save-article-rmail (&optional arg)
9568   "Append the current article to an rmail file.
9569 If N is a positive number, save the N next articles.
9570 If N is a negative number, save the N previous articles.
9571 If N is nil and any articles have been marked with the process mark,
9572 save those articles instead."
9573   (interactive "P")
9574   (require 'gnus-art)
9575   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9576     (gnus-summary-save-article arg)))
9577
9578 (defun gnus-summary-save-article-file (&optional arg)
9579   "Append the current article to a file.
9580 If N is a positive number, save the N next articles.
9581 If N is a negative number, save the N previous articles.
9582 If N is nil and any articles have been marked with the process mark,
9583 save those articles instead."
9584   (interactive "P")
9585   (require 'gnus-art)
9586   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9587     (gnus-summary-save-article arg)))
9588
9589 (defun gnus-summary-write-article-file (&optional arg)
9590   "Write the current article to a file, deleting the previous file.
9591 If N is a positive number, save the N next articles.
9592 If N is a negative number, save the N previous articles.
9593 If N is nil and any articles have been marked with the process mark,
9594 save those articles instead."
9595   (interactive "P")
9596   (require 'gnus-art)
9597   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9598     (gnus-summary-save-article arg)))
9599
9600 (defun gnus-summary-save-article-body-file (&optional arg)
9601   "Append the current article body to a file.
9602 If N is a positive number, save the N next articles.
9603 If N is a negative number, save the N previous articles.
9604 If N is nil and any articles have been marked with the process mark,
9605 save those articles instead."
9606   (interactive "P")
9607   (require 'gnus-art)
9608   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9609     (gnus-summary-save-article arg)))
9610
9611 (defun gnus-summary-pipe-message (program)
9612   "Pipe the current article through PROGRAM."
9613   (interactive "sProgram: ")
9614   (gnus-summary-select-article)
9615   (let ((mail-header-separator ""))
9616     (gnus-eval-in-buffer-window gnus-article-buffer
9617       (save-restriction
9618         (widen)
9619         (let ((start (window-start))
9620               buffer-read-only)
9621           (message-pipe-buffer-body program)
9622           (set-window-start (get-buffer-window (current-buffer)) start))))))
9623
9624 (defun gnus-get-split-value (methods)
9625   "Return a value based on the split METHODS."
9626   (let (split-name method result match)
9627     (when methods
9628       (save-excursion
9629         (set-buffer gnus-original-article-buffer)
9630         (save-restriction
9631           (nnheader-narrow-to-headers)
9632           (while (and methods (not split-name))
9633             (goto-char (point-min))
9634             (setq method (pop methods))
9635             (setq match (car method))
9636             (when (cond
9637                    ((stringp match)
9638                     ;; Regular expression.
9639                     (ignore-errors
9640                       (re-search-forward match nil t)))
9641                    ((gnus-functionp match)
9642                     ;; Function.
9643                     (save-restriction
9644                       (widen)
9645                       (setq result (funcall match gnus-newsgroup-name))))
9646                    ((consp match)
9647                     ;; Form.
9648                     (save-restriction
9649                       (widen)
9650                       (setq result (eval match)))))
9651               (setq split-name (cdr method))
9652               (cond ((stringp result)
9653                      (push (expand-file-name
9654                             result gnus-article-save-directory)
9655                            split-name))
9656                     ((consp result)
9657                      (setq split-name (append result split-name)))))))))
9658     (nreverse split-name)))
9659
9660 (defun gnus-valid-move-group-p (group)
9661   (and (boundp group)
9662        (symbol-name group)
9663        (symbol-value group)
9664        (gnus-get-function (gnus-find-method-for-group
9665                            (symbol-name group)) 'request-accept-article t)))
9666
9667 (defun gnus-read-move-group-name (prompt default articles prefix)
9668   "Read a group name."
9669   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9670          (minibuffer-confirm-incomplete nil) ; XEmacs
9671          (prom
9672           (format "%s %s to:"
9673                   prompt
9674                   (if (> (length articles) 1)
9675                       (format "these %d articles" (length articles))
9676                     "this article")))
9677          (to-newsgroup
9678           (cond
9679            ((null split-name)
9680             (gnus-completing-read default prom
9681                                   gnus-active-hashtb
9682                                   'gnus-valid-move-group-p
9683                                   nil prefix
9684                                   'gnus-group-history))
9685            ((= 1 (length split-name))
9686             (gnus-completing-read (car split-name) prom
9687                                   gnus-active-hashtb
9688                                   'gnus-valid-move-group-p
9689                                   nil nil
9690                                   'gnus-group-history))
9691            (t
9692             (gnus-completing-read nil prom
9693                                   (mapcar (lambda (el) (list el))
9694                                           (nreverse split-name))
9695                                   nil nil nil
9696                                   'gnus-group-history))))
9697          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9698     (when to-newsgroup
9699       (if (or (string= to-newsgroup "")
9700               (string= to-newsgroup prefix))
9701           (setq to-newsgroup default))
9702       (unless to-newsgroup
9703         (error "No group name entered"))
9704       (or (gnus-active to-newsgroup)
9705           (gnus-activate-group to-newsgroup nil nil to-method)
9706           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9707                                      to-newsgroup))
9708               (or (and (gnus-request-create-group to-newsgroup to-method)
9709                        (gnus-activate-group
9710                         to-newsgroup nil nil to-method)
9711                        (gnus-subscribe-group to-newsgroup))
9712                   (error "Couldn't create group %s" to-newsgroup)))
9713           (error "No such group: %s" to-newsgroup)))
9714     to-newsgroup))
9715
9716 (defun gnus-summary-save-parts (type dir n &optional reverse)
9717   "Save parts matching TYPE to DIR.
9718 If REVERSE, save parts that do not match TYPE."
9719   (interactive
9720    (list (read-string "Save parts of type: "
9721                       (or (car gnus-summary-save-parts-type-history)
9722                           gnus-summary-save-parts-default-mime)
9723                       'gnus-summary-save-parts-type-history)
9724          (setq gnus-summary-save-parts-last-directory
9725                (read-file-name "Save to directory: "
9726                                gnus-summary-save-parts-last-directory
9727                                nil t))
9728          current-prefix-arg))
9729   (gnus-summary-iterate n
9730     (let ((gnus-display-mime-function nil)
9731           (gnus-inhibit-treatment t))
9732       (gnus-summary-select-article))
9733     (save-excursion
9734       (set-buffer gnus-article-buffer)
9735       (let ((handles (or gnus-article-mime-handles
9736                          (mm-dissect-buffer) (mm-uu-dissect))))
9737         (when handles
9738           (gnus-summary-save-parts-1 type dir handles reverse)
9739           (unless gnus-article-mime-handles ;; Don't destroy this case.
9740             (mm-destroy-parts handles)))))))
9741
9742 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9743   (if (stringp (car handle))
9744       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9745               (cdr handle))
9746     (when (if reverse
9747               (not (string-match type (mm-handle-media-type handle)))
9748             (string-match type (mm-handle-media-type handle)))
9749       (let ((file (expand-file-name
9750                    (file-name-nondirectory
9751                     (or
9752                      (mail-content-type-get
9753                       (mm-handle-disposition handle) 'filename)
9754                      (concat gnus-newsgroup-name
9755                              "." (number-to-string
9756                                   (cdr gnus-article-current)))))
9757                    dir)))
9758         (unless (file-exists-p file)
9759           (mm-save-part-to-file handle file))))))
9760
9761 ;; Summary extract commands
9762
9763 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9764   (let ((buffer-read-only nil)
9765         (article (gnus-summary-article-number))
9766         after-article b e)
9767     (unless (gnus-summary-goto-subject article)
9768       (error "No such article: %d" article))
9769     (gnus-summary-position-point)
9770     ;; If all commands are to be bunched up on one line, we collect
9771     ;; them here.
9772     (unless gnus-view-pseudos-separately
9773       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9774             files action)
9775         (while ps
9776           (setq action (cdr (assq 'action (car ps))))
9777           (setq files (list (cdr (assq 'name (car ps)))))
9778           (while (and ps (cdr ps)
9779                       (string= (or action "1")
9780                                (or (cdr (assq 'action (cadr ps))) "2")))
9781             (push (cdr (assq 'name (cadr ps))) files)
9782             (setcdr ps (cddr ps)))
9783           (when files
9784             (when (not (string-match "%s" action))
9785               (push " " files))
9786             (push " " files)
9787             (when (assq 'execute (car ps))
9788               (setcdr (assq 'execute (car ps))
9789                       (funcall (if (string-match "%s" action)
9790                                    'format 'concat)
9791                                action
9792                                (mapconcat
9793                                 (lambda (f)
9794                                   (if (equal f " ")
9795                                       f
9796                                     (gnus-quote-arg-for-sh-or-csh f)))
9797                                 files " ")))))
9798           (setq ps (cdr ps)))))
9799     (if (and gnus-view-pseudos (not not-view))
9800         (while pslist
9801           (when (assq 'execute (car pslist))
9802             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9803                                   (eq gnus-view-pseudos 'not-confirm)))
9804           (setq pslist (cdr pslist)))
9805       (save-excursion
9806         (while pslist
9807           (setq after-article (or (cdr (assq 'article (car pslist)))
9808                                   (gnus-summary-article-number)))
9809           (gnus-summary-goto-subject after-article)
9810           (forward-line 1)
9811           (setq b (point))
9812           (insert "    " (file-name-nondirectory
9813                           (cdr (assq 'name (car pslist))))
9814                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9815           (setq e (point))
9816           (forward-line -1)             ; back to `b'
9817           (gnus-add-text-properties
9818            b (1- e) (list 'gnus-number gnus-reffed-article-number
9819                           gnus-mouse-face-prop gnus-mouse-face))
9820           (gnus-data-enter
9821            after-article gnus-reffed-article-number
9822            gnus-unread-mark b (car pslist) 0 (- e b))
9823           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9824           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9825           (setq pslist (cdr pslist)))))))
9826
9827 (defun gnus-pseudos< (p1 p2)
9828   (let ((c1 (cdr (assq 'action p1)))
9829         (c2 (cdr (assq 'action p2))))
9830     (and c1 c2 (string< c1 c2))))
9831
9832 (defun gnus-request-pseudo-article (props)
9833   (cond ((assq 'execute props)
9834          (gnus-execute-command (cdr (assq 'execute props)))))
9835   (let ((gnus-current-article (gnus-summary-article-number)))
9836     (gnus-run-hooks 'gnus-mark-article-hook)))
9837
9838 (defun gnus-execute-command (command &optional automatic)
9839   (save-excursion
9840     (gnus-article-setup-buffer)
9841     (set-buffer gnus-article-buffer)
9842     (setq buffer-read-only nil)
9843     (let ((command (if automatic command
9844                      (read-string "Command: " (cons command 0)))))
9845       (erase-buffer)
9846       (insert "$ " command "\n\n")
9847       (if gnus-view-pseudo-asynchronously
9848           (start-process "gnus-execute" (current-buffer) shell-file-name
9849                          shell-command-switch command)
9850         (call-process shell-file-name nil t nil
9851                       shell-command-switch command)))))
9852
9853 ;; Summary kill commands.
9854
9855 (defun gnus-summary-edit-global-kill (article)
9856   "Edit the \"global\" kill file."
9857   (interactive (list (gnus-summary-article-number)))
9858   (gnus-group-edit-global-kill article))
9859
9860 (defun gnus-summary-edit-local-kill ()
9861   "Edit a local kill file applied to the current newsgroup."
9862   (interactive)
9863   (setq gnus-current-headers (gnus-summary-article-header))
9864   (gnus-group-edit-local-kill
9865    (gnus-summary-article-number) gnus-newsgroup-name))
9866
9867 ;;; Header reading.
9868
9869 (defun gnus-read-header (id &optional header)
9870   "Read the headers of article ID and enter them into the Gnus system."
9871   (let ((group gnus-newsgroup-name)
9872         (gnus-override-method
9873          (or
9874           gnus-override-method
9875           (and (gnus-news-group-p gnus-newsgroup-name)
9876                (car (gnus-refer-article-methods)))))
9877         where)
9878     ;; First we check to see whether the header in question is already
9879     ;; fetched.
9880     (if (stringp id)
9881         ;; This is a Message-ID.
9882         (setq header (or header (gnus-id-to-header id)))
9883       ;; This is an article number.
9884       (setq header (or header (gnus-summary-article-header id))))
9885     (if (and header
9886              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9887         ;; We have found the header.
9888         header
9889       ;; If this is a sparse article, we have to nix out its
9890       ;; previous entry in the thread hashtb.
9891       (when (and header
9892                  (gnus-summary-article-sparse-p (mail-header-number header)))
9893         (let* ((parent (gnus-parent-id (mail-header-references header)))
9894                (thread (and parent (gnus-id-to-thread parent))))
9895           (when thread
9896             (delq (assq header thread) thread))))
9897       ;; We have to really fetch the header to this article.
9898       (save-excursion
9899         (set-buffer nntp-server-buffer)
9900         (when (setq where (gnus-request-head id group))
9901           (nnheader-fold-continuation-lines)
9902           (goto-char (point-max))
9903           (insert ".\n")
9904           (goto-char (point-min))
9905           (insert "211 ")
9906           (princ (cond
9907                   ((numberp id) id)
9908                   ((cdr where) (cdr where))
9909                   (header (mail-header-number header))
9910                   (t gnus-reffed-article-number))
9911                  (current-buffer))
9912           (insert " Article retrieved.\n"))
9913         (if (or (not where)
9914                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9915             ()                          ; Malformed head.
9916           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9917             (when (and (stringp id)
9918                        (not (string= (gnus-group-real-name group)
9919                                      (car where))))
9920               ;; If we fetched by Message-ID and the article came
9921               ;; from a different group, we fudge some bogus article
9922               ;; numbers for this article.
9923               (mail-header-set-number header gnus-reffed-article-number))
9924             (save-excursion
9925               (set-buffer gnus-summary-buffer)
9926               (decf gnus-reffed-article-number)
9927               (gnus-remove-header (mail-header-number header))
9928               (push header gnus-newsgroup-headers)
9929               (setq gnus-current-headers header)
9930               (push (mail-header-number header) gnus-newsgroup-limit)))
9931           header)))))
9932
9933 (defun gnus-remove-header (number)
9934   "Remove header NUMBER from `gnus-newsgroup-headers'."
9935   (if (and gnus-newsgroup-headers
9936            (= number (mail-header-number (car gnus-newsgroup-headers))))
9937       (pop gnus-newsgroup-headers)
9938     (let ((headers gnus-newsgroup-headers))
9939       (while (and (cdr headers)
9940                   (not (= number (mail-header-number (cadr headers)))))
9941         (pop headers))
9942       (when (cdr headers)
9943         (setcdr headers (cddr headers))))))
9944
9945 ;;;
9946 ;;; summary highlights
9947 ;;;
9948
9949 (defun gnus-highlight-selected-summary ()
9950   "Highlight selected article in summary buffer."
9951   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9952   (when gnus-summary-selected-face
9953     (save-excursion
9954       (let* ((beg (progn (beginning-of-line) (point)))
9955              (end (progn (end-of-line) (point)))
9956              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9957              (from (if (get-text-property beg gnus-mouse-face-prop)
9958                        beg
9959                      (or (next-single-property-change
9960                           beg gnus-mouse-face-prop nil end)
9961                          beg)))
9962              (to
9963               (if (= from end)
9964                   (- from 2)
9965                 (or (next-single-property-change
9966                      from gnus-mouse-face-prop nil end)
9967                     end))))
9968         ;; If no mouse-face prop on line we will have to = from = end,
9969         ;; so we highlight the entire line instead.
9970         (when (= (+ to 2) from)
9971           (setq from beg)
9972           (setq to end))
9973         (if gnus-newsgroup-selected-overlay
9974             ;; Move old overlay.
9975             (gnus-move-overlay
9976              gnus-newsgroup-selected-overlay from to (current-buffer))
9977           ;; Create new overlay.
9978           (gnus-overlay-put
9979            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9980            'face gnus-summary-selected-face))))))
9981
9982 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9983 (defun gnus-summary-highlight-line ()
9984   "Highlight current line according to `gnus-summary-highlight'."
9985   (let* ((list gnus-summary-highlight)
9986          (p (point))
9987          (end (progn (end-of-line) (point)))
9988          ;; now find out where the line starts and leave point there.
9989          (beg (progn (beginning-of-line) (point)))
9990          (article (gnus-summary-article-number))
9991          (score (or (cdr (assq (or article gnus-current-article)
9992                                gnus-newsgroup-scored))
9993                     gnus-summary-default-score 0))
9994          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9995          (inhibit-read-only t))
9996     ;; Eval the cars of the lists until we find a match.
9997     (let ((default gnus-summary-default-score))
9998       (while (and list
9999                   (not (eval (caar list))))
10000         (setq list (cdr list))))
10001     (let ((face (cdar list)))
10002       (unless (eq face (get-text-property beg 'face))
10003         (gnus-put-text-property-excluding-characters-with-faces
10004          beg end 'face
10005          (setq face (if (boundp face) (symbol-value face) face)))
10006         (when gnus-summary-highlight-line-function
10007           (funcall gnus-summary-highlight-line-function article face))))
10008     (goto-char p)))
10009
10010 (defun gnus-update-read-articles (group unread &optional compute)
10011   "Update the list of read articles in GROUP."
10012   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10013          (entry (gnus-gethash group gnus-newsrc-hashtb))
10014          (info (nth 2 entry))
10015          (prev 1)
10016          (unread (sort (copy-sequence unread) '<))
10017          read)
10018     (if (or (not info) (not active))
10019         ;; There is no info on this group if it was, in fact,
10020         ;; killed.  Gnus stores no information on killed groups, so
10021         ;; there's nothing to be done.
10022         ;; One could store the information somewhere temporarily,
10023         ;; perhaps...  Hmmm...
10024         ()
10025       ;; Remove any negative articles numbers.
10026       (while (and unread (< (car unread) 0))
10027         (setq unread (cdr unread)))
10028       ;; Remove any expired article numbers
10029       (while (and unread (< (car unread) (car active)))
10030         (setq unread (cdr unread)))
10031       ;; Compute the ranges of read articles by looking at the list of
10032       ;; unread articles.
10033       (while unread
10034         (when (/= (car unread) prev)
10035           (push (if (= prev (1- (car unread))) prev
10036                   (cons prev (1- (car unread))))
10037                 read))
10038         (setq prev (1+ (car unread)))
10039         (setq unread (cdr unread)))
10040       (when (<= prev (cdr active))
10041         (push (cons prev (cdr active)) read))
10042       (setq read (if (> (length read) 1) (nreverse read) read))
10043       (if compute
10044           read
10045         (save-excursion
10046           (let (setmarkundo)
10047             ;; Propagate the read marks to the backend.
10048             (when (gnus-check-backend-function 'request-set-mark group)
10049               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10050                     (add (gnus-remove-from-range read (gnus-info-read info))))
10051                 (when (or add del)
10052                   (unless (gnus-check-group group)
10053                     (error "Can't open server for %s" group))
10054                   (gnus-request-set-mark
10055                    group (delq nil (list (if add (list add 'add '(read)))
10056                                          (if del (list del 'del '(read))))))
10057                   (setq setmarkundo
10058                         `(gnus-request-set-mark
10059                           ,group
10060                           ',(delq nil (list
10061                                        (if del (list del 'add '(read)))
10062                                        (if add (list add 'del '(read))))))))))
10063             (set-buffer gnus-group-buffer)
10064             (gnus-undo-register
10065               `(progn
10066                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10067                  (gnus-info-set-read ',info ',(gnus-info-read info))
10068                  (gnus-get-unread-articles-in-group ',info
10069                                                     (gnus-active ,group))
10070                  (gnus-group-update-group ,group t)
10071                  ,setmarkundo))))
10072         ;; Enter this list into the group info.
10073         (gnus-info-set-read info read)
10074         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10075         (gnus-get-unread-articles-in-group info (gnus-active group))
10076         t))))
10077
10078 (defun gnus-offer-save-summaries ()
10079   "Offer to save all active summary buffers."
10080   (save-excursion
10081     (let ((buflist (buffer-list))
10082           buffers bufname)
10083       ;; Go through all buffers and find all summaries.
10084       (while buflist
10085         (and (setq bufname (buffer-name (car buflist)))
10086              (string-match "Summary" bufname)
10087              (save-excursion
10088                (set-buffer bufname)
10089                ;; We check that this is, indeed, a summary buffer.
10090                (and (eq major-mode 'gnus-summary-mode)
10091                     ;; Also make sure this isn't bogus.
10092                     gnus-newsgroup-prepared
10093                     ;; Also make sure that this isn't a dead summary buffer.
10094                     (not gnus-dead-summary-mode)))
10095              (push bufname buffers))
10096         (setq buflist (cdr buflist)))
10097       ;; Go through all these summary buffers and offer to save them.
10098       (when buffers
10099         (map-y-or-n-p
10100          "Update summary buffer %s? "
10101          (lambda (buf)
10102            (switch-to-buffer buf)
10103            (gnus-summary-exit))
10104          buffers)))))
10105
10106
10107 ;;; @ for mime-partial
10108 ;;;
10109
10110 (defun gnus-request-partial-message ()
10111   (save-excursion
10112     (let ((number (gnus-summary-article-number))
10113           (group gnus-newsgroup-name)
10114           (mother gnus-article-buffer))
10115       (set-buffer (get-buffer-create " *Partial Article*"))
10116       (erase-buffer)
10117       (setq mime-preview-buffer mother)
10118       (gnus-request-article-this-buffer number group)
10119       (mime-parse-buffer)
10120       )))
10121
10122 (autoload 'mime-combine-message/partial-pieces-automatically
10123   "mime-partial"
10124   "Internal method to combine message/partial messages automatically.")
10125
10126 (mime-add-condition
10127  'action '((type . message)(subtype . partial)
10128            (major-mode . gnus-original-article-mode)
10129            (method . mime-combine-message/partial-pieces-automatically)
10130            (summary-buffer-exp . gnus-summary-buffer)
10131            (request-partial-message-method . gnus-request-partial-message)
10132            ))
10133
10134
10135 ;;; @ for message/rfc822
10136 ;;;
10137
10138 (defun gnus-mime-extract-message/rfc822 (entity situation)
10139   (let (group article num cwin swin cur)
10140     (with-temp-buffer
10141       (mime-insert-entity-content entity)
10142       (setq group (or (cdr (assq 'group situation))
10143                       (completing-read "Group: "
10144                                        gnus-active-hashtb
10145                                        nil
10146                                        (gnus-read-active-file-p)
10147                                        gnus-newsgroup-name))
10148             article (gnus-request-accept-article group)))
10149     (when (and (consp article)
10150                (numberp (setq article (cdr article))))
10151       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10152             cwin (get-buffer-window (current-buffer) t))
10153       (save-window-excursion
10154         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10155             (select-window swin)
10156           (set-buffer gnus-summary-buffer))
10157         (setq cur gnus-current-article)
10158         (forward-line num)
10159         (let (gnus-show-threads)
10160           (gnus-summary-goto-subject article t))
10161         (gnus-summary-clear-mark-forward 1)
10162         (gnus-summary-goto-subject cur))
10163       (when (and cwin (window-frame cwin))
10164         (select-frame (window-frame cwin)))
10165       (when (boundp 'mime-acting-situation-to-override)
10166         (set-alist 'mime-acting-situation-to-override
10167                    'group
10168                    group)
10169         (set-alist 'mime-acting-situation-to-override
10170                    'after-method
10171                    `(progn
10172                       (save-current-buffer
10173                         (set-buffer gnus-group-buffer)
10174                         (gnus-activate-group ,group))
10175                       (gnus-summary-goto-article ,cur
10176                                                  gnus-show-all-headers)))
10177         (set-alist 'mime-acting-situation-to-override
10178                    'number num)))))
10179
10180 (mime-add-condition
10181  'action '((type . message)(subtype . rfc822)
10182            (major-mode . gnus-original-article-mode)
10183            (method . gnus-mime-extract-message/rfc822)
10184            (mode . "extract")
10185            ))
10186
10187 (mime-add-condition
10188  'action '((type . message)(subtype . news)
10189            (major-mode . gnus-original-article-mode)
10190            (method . gnus-mime-extract-message/rfc822)
10191            (mode . "extract")
10192            ))
10193
10194 (defun gnus-mime-extract-multipart (entity situation)
10195   (let ((children (mime-entity-children entity))
10196         mime-acting-situation-to-override
10197         f)
10198     (while children
10199       (mime-play-entity (car children)
10200                         (cons (assq 'mode situation)
10201                               mime-acting-situation-to-override))
10202       (setq children (cdr children)))
10203     (if (setq f (cdr (assq 'after-method
10204                            mime-acting-situation-to-override)))
10205         (eval f)
10206       )))
10207
10208 (mime-add-condition
10209  'action '((type . multipart)
10210            (method . gnus-mime-extract-multipart)
10211            (mode . "extract")
10212            )
10213  'with-default)
10214
10215
10216 ;;; @ end
10217 ;;;
10218
10219 (defun gnus-summary-setup-default-charset ()
10220   "Setup newsgroup default charset."
10221   (if (equal gnus-newsgroup-name "nndraft:drafts")
10222       (setq gnus-newsgroup-charset nil)
10223     (let* ((ignored-charsets
10224             (or gnus-newsgroup-ephemeral-ignored-charsets
10225                 (append
10226                  (and gnus-newsgroup-name
10227                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10228                  gnus-newsgroup-ignored-charsets))))
10229       (setq gnus-newsgroup-charset
10230             (or gnus-newsgroup-ephemeral-charset
10231                 (and gnus-newsgroup-name
10232                      (gnus-parameter-charset gnus-newsgroup-name))
10233                 gnus-default-charset))
10234       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10235            ignored-charsets))))
10236
10237 ;;;
10238 ;;; Mime Commands
10239 ;;;
10240
10241 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10242   "Display the current article buffer fully MIME-buttonized.
10243 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10244 treated as multipart/mixed."
10245   (interactive "P")
10246   (require 'gnus-art)
10247   (let ((gnus-unbuttonized-mime-types nil)
10248         (gnus-mime-display-multipart-as-mixed show-all-parts))
10249     (gnus-summary-show-article)))
10250
10251 (defun gnus-summary-repair-multipart (article)
10252   "Add a Content-Type header to a multipart article without one."
10253   (interactive (list (gnus-summary-article-number)))
10254   (gnus-with-article article
10255     (message-narrow-to-head)
10256     (message-remove-header "Mime-Version")
10257     (goto-char (point-max))
10258     (insert "Mime-Version: 1.0\n")
10259     (widen)
10260     (when (search-forward "\n--" nil t)
10261       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10262         (message-narrow-to-head)
10263         (message-remove-header "Content-Type")
10264         (goto-char (point-max))
10265         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10266                         separator))
10267         (widen))))
10268   (let (gnus-mark-article-hook)
10269     (gnus-summary-select-article t t nil article)))
10270
10271 (defun gnus-summary-toggle-display-buttonized ()
10272   "Toggle the buttonizing of the article buffer."
10273   (interactive)
10274   (require 'gnus-art)
10275   (if (setq gnus-inhibit-mime-unbuttonizing
10276             (not gnus-inhibit-mime-unbuttonizing))
10277       (let ((gnus-unbuttonized-mime-types nil))
10278         (gnus-summary-show-article))
10279     (gnus-summary-show-article)))
10280
10281 ;;;
10282 ;;; Intelli-mouse commmands
10283 ;;;
10284
10285 (defun gnus-wheel-summary-scroll (event)
10286   (interactive "e")
10287   (let ((amount (if (memq 'shift (event-modifiers event))
10288                     (car gnus-wheel-scroll-amount)
10289                   (cdr gnus-wheel-scroll-amount)))
10290         (direction (- (* (static-if (featurep 'xemacs)
10291                              (event-button event)
10292                            (cond ((eq 'mouse-4 (event-basic-type event))
10293                                   4)
10294                                  ((eq 'mouse-5 (event-basic-type event))
10295                                   5)))
10296                          2) 9))
10297         edge)
10298     (gnus-summary-scroll-up (* amount direction))
10299     (when (gnus-eval-in-buffer-window gnus-article-buffer
10300             (save-restriction
10301               (widen)
10302               (and (if (< 0 direction)
10303                        (gnus-article-next-page 0)
10304                      (gnus-article-prev-page 0)
10305                      (bobp))
10306                    (if (setq edge (get-text-property
10307                                    (point-min) 'gnus-wheel-edge))
10308                        (setq edge (* edge direction))
10309                      (setq edge -1))
10310                    (or (plusp edge)
10311                        (let ((buffer-read-only nil)
10312                              (inhibit-read-only t))
10313                          (put-text-property (point-min) (point-max)
10314                                             'gnus-wheel-edge direction)
10315                          nil))
10316                    (or (> edge gnus-wheel-edge-resistance)
10317                        (let ((buffer-read-only nil)
10318                              (inhibit-read-only t))
10319                          (put-text-property (point-min) (point-max)
10320                                             'gnus-wheel-edge
10321                                             (* (1+ edge) direction))
10322                          nil))
10323                    (eq last-command 'gnus-wheel-summary-scroll))))
10324       (gnus-summary-next-article nil nil (minusp direction)))))
10325
10326 (defun gnus-wheel-install ()
10327   "Enable mouse wheel support on summary window."
10328   (when gnus-use-wheel
10329     (let ((keys
10330            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10331       (dolist (key keys)
10332         (define-key gnus-summary-mode-map key
10333           'gnus-wheel-summary-scroll)))))
10334
10335 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10336
10337 ;;;
10338 ;;; Traditional PGP commmands
10339 ;;;
10340
10341 (defun gnus-summary-decrypt-article (&optional force)
10342   "Decrypt the current article in traditional PGP way.
10343 This will have permanent effect only in mail groups.
10344 If FORCE is non-nil, allow editing of articles even in read-only
10345 groups."
10346   (interactive "P")
10347   (gnus-summary-select-article t)
10348   (gnus-eval-in-buffer-window gnus-article-buffer
10349     (save-excursion
10350       (save-restriction
10351         (widen)
10352         (goto-char (point-min))
10353         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10354           (error "Not a traditional PGP message!"))
10355         (let ((armor-start (match-beginning 0)))
10356           (if (and (pgg-decrypt-region armor-start (point-max))
10357                    (or force (not (gnus-group-read-only-p))))
10358               (let ((inhibit-read-only t)
10359                     buffer-read-only)
10360                 (delete-region armor-start
10361                                (progn
10362                                  (re-search-forward "^-+END PGP" nil t)
10363                                  (beginning-of-line 2)
10364                                  (point)))
10365                 (insert-buffer-substring pgg-output-buffer))))))))
10366
10367 (defun gnus-summary-verify-article ()
10368   "Verify the current article in traditional PGP way."
10369   (interactive)
10370   (save-excursion
10371     (set-buffer gnus-original-article-buffer)
10372     (goto-char (point-min))
10373     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10374       (error "Not a traditional PGP message!"))
10375     (re-search-forward "^-+END PGP" nil t)
10376     (beginning-of-line 2)
10377     (call-interactively (function pgg-verify-region))))
10378
10379 ;;;
10380 ;;; Generic summary marking commands
10381 ;;;
10382
10383 (defvar gnus-summary-marking-alist
10384   '((read gnus-del-mark "d")
10385     (unread gnus-unread-mark "u")
10386     (ticked gnus-ticked-mark "!")
10387     (dormant gnus-dormant-mark "?")
10388     (expirable gnus-expirable-mark "e"))
10389   "An alist of names/marks/keystrokes.")
10390
10391 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10392 (defvar gnus-summary-mark-map)
10393
10394 (defun gnus-summary-make-all-marking-commands ()
10395   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10396   (dolist (elem gnus-summary-marking-alist)
10397     (apply 'gnus-summary-make-marking-command elem)))
10398
10399 (defun gnus-summary-make-marking-command (name mark keystroke)
10400   (let ((map (make-sparse-keymap)))
10401     (define-key gnus-summary-generic-mark-map keystroke map)
10402     (dolist (lway `((next "next" next nil "n")
10403                     (next-unread "next unread" next t "N")
10404                     (prev "previous" prev nil "p")
10405                     (prev-unread "previous unread" prev t "P")
10406                     (nomove "" nil nil ,keystroke)))
10407       (let ((func (gnus-summary-make-marking-command-1
10408                    mark (car lway) lway name)))
10409         (setq func (eval func))
10410         (define-key map (nth 4 lway) func)))))
10411
10412 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10413   `(defun ,(intern
10414             (format "gnus-summary-put-mark-as-%s%s"
10415                     name (if (eq way 'nomove)
10416                              ""
10417                            (concat "-" (symbol-name way)))))
10418      (n)
10419      ,(format
10420        "Mark the current article as %s%s.
10421 If N, the prefix, then repeat N times.
10422 If N is negative, move in reverse order.
10423 The difference between N and the actual number of articles marked is
10424 returned."
10425        name (car (cdr lway)))
10426      (interactive "p")
10427      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10428
10429 (defun gnus-summary-generic-mark (n mark move unread)
10430   "Mark N articles with MARK."
10431   (unless (eq major-mode 'gnus-summary-mode)
10432     (error "This command can only be used in the summary buffer"))
10433   (gnus-summary-show-thread)
10434   (let ((nummove
10435          (cond
10436           ((eq move 'next) 1)
10437           ((eq move 'prev) -1)
10438           (t 0))))
10439     (if (zerop nummove)
10440         (setq n 1)
10441       (when (< n 0)
10442         (setq n (abs n)
10443               nummove (* -1 nummove))))
10444     (while (and (> n 0)
10445                 (gnus-summary-mark-article nil mark)
10446                 (zerop (gnus-summary-next-subject nummove unread t)))
10447       (setq n (1- n)))
10448     (when (/= 0 n)
10449       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10450     (gnus-summary-recenter)
10451     (gnus-summary-position-point)
10452     (gnus-set-mode-line 'summary)
10453     n))
10454
10455 (defun gnus-summary-insert-articles (articles)
10456   (when (setq articles
10457               (gnus-set-difference articles
10458                                    (mapcar (lambda (h) (mail-header-number h))
10459                                            gnus-newsgroup-headers)))
10460     (setq gnus-newsgroup-headers 
10461           (merge 'list
10462                  gnus-newsgroup-headers
10463                  (gnus-fetch-headers articles)
10464                  'gnus-article-sort-by-number))
10465     ;; Suppress duplicates?
10466     (when gnus-suppress-duplicates
10467       (gnus-dup-suppress-articles))
10468     
10469     ;; We might want to build some more threads first.
10470     (when (and gnus-fetch-old-headers
10471                (eq gnus-headers-retrieved-by 'nov))
10472       (if (eq gnus-fetch-old-headers 'invisible)
10473         (gnus-build-all-threads)
10474         (gnus-build-old-threads)))
10475     ;; Let the Gnus agent mark articles as read.
10476     (when gnus-agent
10477       (gnus-agent-get-undownloaded-list))
10478     ;; Remove list identifiers from subject
10479     (when gnus-list-identifiers
10480       (gnus-summary-remove-list-identifiers))
10481     ;; First and last article in this newsgroup.
10482     (when gnus-newsgroup-headers
10483       (setq gnus-newsgroup-begin
10484             (mail-header-number (car gnus-newsgroup-headers))
10485             gnus-newsgroup-end
10486             (mail-header-number
10487              (gnus-last-element gnus-newsgroup-headers))))
10488     (when gnus-use-scoring
10489       (gnus-possibly-score-headers))))
10490
10491 (defun gnus-summary-insert-old-articles (&optional all)
10492   "Insert all old articles in this group.
10493 If ALL is non-nil, already read articles become readable.
10494 If ALL is a number, fetch this number of articles."
10495   (interactive "P")
10496   (prog1
10497       (let ((old (mapcar 'car gnus-newsgroup-data))
10498             (i (car gnus-newsgroup-active))
10499             older len)
10500         (while (<= i (cdr gnus-newsgroup-active))
10501           (or (memq i old) (push i older))
10502           (incf i))
10503         (setq len (length older))
10504         (cond 
10505          ((null older) nil)
10506          ((numberp all) 
10507           (if (< all len)
10508               (setq older (subseq older 0 all))))
10509          (all nil)
10510          (t
10511           (if (and (numberp gnus-large-newsgroup)
10512                    (> len gnus-large-newsgroup))
10513               (let ((input
10514                      (read-string
10515                       (format
10516                        "How many articles from %s (default %d): "
10517                        (gnus-limit-string 
10518                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10519                        len))))
10520                 (unless (string-match "^[ \t]*$" input) 
10521                   (setq all (string-to-number input))
10522                   (if (< all len)
10523                       (setq older (subseq older 0 all))))))))
10524         (if (not older)
10525             (message "No old news.")
10526           (gnus-summary-insert-articles older)
10527           (gnus-summary-limit (gnus-union older old))))
10528     (gnus-summary-position-point)))
10529
10530 (defun gnus-summary-insert-new-articles ()
10531   "Insert all new articles in this group."
10532   (interactive)
10533   (prog1
10534       (let ((old (mapcar 'car gnus-newsgroup-data))
10535             (old-active gnus-newsgroup-active)
10536             (nnmail-fetched-sources (list t))
10537             i new)
10538         (setq gnus-newsgroup-active 
10539               (gnus-activate-group gnus-newsgroup-name 'scan))
10540         (setq i (1+ (cdr old-active)))
10541         (while (<= i (cdr gnus-newsgroup-active))
10542           (push i new)
10543           (incf i))
10544         (if (not new)
10545             (message "No gnus is bad news.")
10546           (gnus-summary-insert-articles new)
10547           (setq gnus-newsgroup-unreads
10548                 (append gnus-newsgroup-unreads new))
10549           (gnus-summary-limit (gnus-union old new))))
10550     (gnus-summary-position-point)))
10551
10552 (gnus-summary-make-all-marking-commands)
10553
10554 (gnus-ems-redefine)
10555
10556 (provide 'gnus-sum)
10557
10558 (run-hooks 'gnus-sum-load-hook)
10559
10560 ;;; gnus-sum.el ends here